IDLGenerators: Set namespace object prototype to Object.prototype

Previously, namespace objects were constructed with no prototype, so
calling methods like `toString()` on them would unexpectedly throw an
exception.
This commit is contained in:
Tim Ledbetter 2024-04-18 06:35:42 +01:00 committed by Andreas Kling
parent dda730c46b
commit 1127fa1e01
Notes: sideshowbarker 2024-07-17 07:35:03 +09:00
3 changed files with 30 additions and 1 deletions

View file

@ -3738,6 +3738,7 @@ void generate_namespace_implementation(IDL::Interface const& interface, StringBu
generator.set("name", interface.name);
generator.set("namespace_class", interface.namespace_class);
generator.set("interface_name", interface.name);
generator.append(R"~~~(
#include <AK/Function.h>
@ -3795,7 +3796,7 @@ namespace Web::Bindings {
JS_DEFINE_ALLOCATOR(@namespace_class@);
@namespace_class@::@namespace_class@(JS::Realm& realm)
: Object(ConstructWithoutPrototypeTag::Tag, realm)
: Object(ConstructWithPrototypeTag::Tag, realm.intrinsics().object_prototype())
{
}
@ -3810,6 +3811,8 @@ void @namespace_class@::initialize(JS::Realm& realm)
Base::initialize(realm);
define_direct_property(vm.well_known_symbol_to_string_tag(), JS::PrimitiveString::create(vm, "@interface_name@"_string), JS::Attribute::Configurable);
)~~~");
// https://webidl.spec.whatwg.org/#es-operations

View file

@ -0,0 +1,6 @@
Prototype of CSS is Object.prototype: true
CSS.toString(): [object CSS]
Object.prototype.toString.call(CSS): [object CSS]
Prototype of WebAssembly is Object.prototype: true
WebAssembly.toString(): [object WebAssembly]
Object.prototype.toString.call(WebAssembly): [object WebAssembly]

View file

@ -0,0 +1,20 @@
<!DOCTYPE html>
<script src="include.js"></script>
<script>
function testNamespace(testCase) {
println(`Prototype of ${testCase.name} is Object.prototype: ${Object.getPrototypeOf(testCase.namespace) === Object.prototype}`);
println(`${testCase.name}.toString(): ${testCase.namespace.toString()}`);
println(`Object.prototype.toString.call(${testCase.name}): ${Object.prototype.toString.call(testCase.namespace)}`);
}
test(() => {
const testCases = [
{ namespace: CSS, name: "CSS" },
{ namespace: WebAssembly, name: "WebAssembly" },
];
for (const testCase of testCases) {
testNamespace(testCase);
}
});
</script>