Skip to content

Commit

Permalink
[WebIDL] Default toJSON() method should create result object in its r…
Browse files Browse the repository at this point in the history
…ealm

https://bugs.webkit.org/show_bug.cgi?id=244939

Reviewed by NOBODY (OOPS!).

Per ECMA-262 [1], %Object.prototype% notation points to current realm, which
is the realm of toJSON() function [2], and not the relevant one.

Aligns WebKit with Blink and Gecko.

[1] https://tc39.es/ecma262/#sec-well-known-intrinsic-objects (paragraph 2)
[2] https://webidl.spec.whatwg.org/#default-tojson-steps (step 4)

* LayoutTests/imported/w3c/web-platform-tests/WebIDL/ecmascript-binding/default-toJSON-cross-realm-expected.txt: Added.
* LayoutTests/imported/w3c/web-platform-tests/WebIDL/ecmascript-binding/default-toJSON-cross-realm.html: Added.
* Source/WebCore/bindings/scripts/CodeGeneratorJS.pm:
(GenerateDefaultToJSONOperationDefinition):
* Source/WebCore/bindings/scripts/test/JS/*: Updated.
  • Loading branch information
shvaikalesh committed Sep 8, 2022
1 parent 79593cc commit b72b25a
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 6 deletions.
@@ -0,0 +1,4 @@

PASS Cross-realm [Default] toJSON() creates result object in its realm
PASS Cross-realm [Default] toJSON() converts its interface attributes to ECMAScript values of correct realm

@@ -0,0 +1,25 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Cross-realm [Default] toJSON() creates result object in its realm</title>
<link rel="help" href="https://webidl.spec.whatwg.org/#default-tojson-steps">

<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/create-realm.js"></script>

<body>
<script>
promise_test(async t => {
const other = await createRealm(t);
const json = other.DOMRectReadOnly.prototype.toJSON.call(new DOMRectReadOnly());

assert_equals(Object.getPrototypeOf(json), other.Object.prototype);
}, "Cross-realm [Default] toJSON() creates result object in its realm");

promise_test(async t => {
const other = await createRealm(t);
const json = other.DOMQuad.prototype.toJSON.call(new DOMQuad());

assert_equals(Object.getPrototypeOf(json.p1), DOMPoint.prototype);
}, "Cross-realm [Default] toJSON() converts its interface attributes to ECMAScript values of correct realm");
</script>
2 changes: 1 addition & 1 deletion Source/WebCore/bindings/scripts/CodeGeneratorJS.pm
Expand Up @@ -5876,7 +5876,7 @@ sub GenerateDefaultToJSONOperationDefinition
push(@implContent, " auto& impl = castedThis->wrapped();\n");

AddToImplIncludes("<JavaScriptCore/ObjectConstructor.h>");
push(@implContent, " auto* result = constructEmptyObject(lexicalGlobalObject, castedThis->globalObject()->objectPrototype());\n");
push(@implContent, " auto* result = constructEmptyObject(lexicalGlobalObject);\n");

while (@inheritenceStack) {
my $currentInterface = pop(@inheritenceStack);
Expand Down
Expand Up @@ -740,7 +740,7 @@ static inline EncodedJSValue jsTestDefaultToJSONPrototypeFunction_toJSONBody(JSG
auto throwScope = DECLARE_THROW_SCOPE(vm);
UNUSED_PARAM(throwScope);
auto& impl = castedThis->wrapped();
auto* result = constructEmptyObject(lexicalGlobalObject, castedThis->globalObject()->objectPrototype());
auto* result = constructEmptyObject(lexicalGlobalObject);
if (DeprecatedGlobalSettings::testDeprecatedGlobalSettingEnabled()) {
auto longAttributeValue = toJS<IDLLong>(*lexicalGlobalObject, throwScope, impl.longAttribute());
RETURN_IF_EXCEPTION(throwScope, { });
Expand Down
Expand Up @@ -243,7 +243,7 @@ static inline EncodedJSValue jsTestDefaultToJSONFilteredByExposedPrototypeFuncti
auto throwScope = DECLARE_THROW_SCOPE(vm);
UNUSED_PARAM(throwScope);
auto& impl = castedThis->wrapped();
auto* result = constructEmptyObject(lexicalGlobalObject, castedThis->globalObject()->objectPrototype());
auto* result = constructEmptyObject(lexicalGlobalObject);
auto normalAttributeValue = toJS<IDLLong>(*lexicalGlobalObject, throwScope, impl.normalAttribute());
RETURN_IF_EXCEPTION(throwScope, { });
result->putDirect(vm, Identifier::fromString(vm, "normalAttribute"_s), normalAttributeValue);
Expand Down
Expand Up @@ -221,7 +221,7 @@ static inline EncodedJSValue jsTestDefaultToJSONInheritPrototypeFunction_toJSONB
auto throwScope = DECLARE_THROW_SCOPE(vm);
UNUSED_PARAM(throwScope);
auto& impl = castedThis->wrapped();
auto* result = constructEmptyObject(lexicalGlobalObject, castedThis->globalObject()->objectPrototype());
auto* result = constructEmptyObject(lexicalGlobalObject);
if (DeprecatedGlobalSettings::testDeprecatedGlobalSettingEnabled()) {
auto longAttributeValue = toJS<IDLLong>(*lexicalGlobalObject, throwScope, impl.longAttribute());
RETURN_IF_EXCEPTION(throwScope, { });
Expand Down
Expand Up @@ -255,7 +255,7 @@ static inline EncodedJSValue jsTestDefaultToJSONInheritFinalPrototypeFunction_to
auto throwScope = DECLARE_THROW_SCOPE(vm);
UNUSED_PARAM(throwScope);
auto& impl = castedThis->wrapped();
auto* result = constructEmptyObject(lexicalGlobalObject, castedThis->globalObject()->objectPrototype());
auto* result = constructEmptyObject(lexicalGlobalObject);
if (DeprecatedGlobalSettings::testDeprecatedGlobalSettingEnabled()) {
auto longAttributeValue = toJS<IDLLong>(*lexicalGlobalObject, throwScope, impl.longAttribute());
RETURN_IF_EXCEPTION(throwScope, { });
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/bindings/scripts/test/JS/JSTestNode.cpp
Expand Up @@ -374,7 +374,7 @@ static inline EncodedJSValue jsTestNodePrototypeFunction_toJSONBody(JSGlobalObje
auto throwScope = DECLARE_THROW_SCOPE(vm);
UNUSED_PARAM(throwScope);
auto& impl = castedThis->wrapped();
auto* result = constructEmptyObject(lexicalGlobalObject, castedThis->globalObject()->objectPrototype());
auto* result = constructEmptyObject(lexicalGlobalObject);
auto nameValue = toJS<IDLDOMString>(*lexicalGlobalObject, throwScope, impl.name());
RETURN_IF_EXCEPTION(throwScope, { });
result->putDirect(vm, Identifier::fromString(vm, "name"_s), nameValue);
Expand Down

0 comments on commit b72b25a

Please sign in to comment.