Explorar o código

LibJS: Set prototype of GlobalObject to ObjectPrototype

As the global object is constructed and initialized in a different way
than most other objects we were not setting its prototype! This made
things like "globalThis.toString()" fail unexpectedly.
Linus Groh %!s(int64=4) %!d(string=hai) anos
pai
achega
06a3625545

+ 2 - 0
Libraries/LibJS/Runtime/GlobalObject.cpp

@@ -94,6 +94,8 @@ void GlobalObject::initialize()
     static_cast<FunctionPrototype*>(m_function_prototype)->initialize(*this);
     static_cast<FunctionPrototype*>(m_function_prototype)->initialize(*this);
     static_cast<ObjectPrototype*>(m_object_prototype)->initialize(*this);
     static_cast<ObjectPrototype*>(m_object_prototype)->initialize(*this);
 
 
+    set_prototype(m_object_prototype);
+
 #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
 #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
     if (!m_##snake_name##_prototype)                                          \
     if (!m_##snake_name##_prototype)                                          \
         m_##snake_name##_prototype = heap().allocate<PrototypeName>(*this, *this);
         m_##snake_name##_prototype = heap().allocate<PrototypeName>(*this, *this);

+ 2 - 0
Libraries/LibJS/Tests/builtins/Object/Object.prototype.toString.js

@@ -15,4 +15,6 @@ test("result for various object types", () => {
     expect(oToString(new Date())).toBe("[object Date]");
     expect(oToString(new Date())).toBe("[object Date]");
     expect(oToString(new RegExp())).toBe("[object RegExp]");
     expect(oToString(new RegExp())).toBe("[object RegExp]");
     expect(oToString({})).toBe("[object Object]");
     expect(oToString({})).toBe("[object Object]");
+
+    expect(globalThis.toString()).toBe("[object Object]");
 });
 });