1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 13:15:09 +00:00

LibJS: Move builtin prototypes to the global object

This moves us towards being able to run JavaScript in different global
objects without allocating a separate GC heap.
This commit is contained in:
Andreas Kling 2020-04-18 13:18:06 +02:00
parent cbcf317e76
commit fca08bd000
40 changed files with 131 additions and 101 deletions

View file

@ -29,17 +29,29 @@
#include <LibJS/Heap/Heap.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/ArrayConstructor.h>
#include <LibJS/Runtime/ArrayPrototype.h>
#include <LibJS/Runtime/BooleanConstructor.h>
#include <LibJS/Runtime/BooleanPrototype.h>
#include <LibJS/Runtime/ConsoleObject.h>
#include <LibJS/Runtime/DateConstructor.h>
#include <LibJS/Runtime/DatePrototype.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/ErrorConstructor.h>
#include <LibJS/Runtime/ErrorPrototype.h>
#include <LibJS/Runtime/FunctionConstructor.h>
#include <LibJS/Runtime/FunctionPrototype.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/LexicalEnvironment.h>
#include <LibJS/Runtime/MathObject.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/NumberConstructor.h>
#include <LibJS/Runtime/NumberPrototype.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/ObjectConstructor.h>
#include <LibJS/Runtime/ObjectPrototype.h>
#include <LibJS/Runtime/Shape.h>
#include <LibJS/Runtime/StringConstructor.h>
#include <LibJS/Runtime/StringPrototype.h>
#include <LibJS/Runtime/Value.h>
namespace JS {
@ -53,8 +65,25 @@ void GlobalObject::add_constructor(const FlyString& property_name, ConstructorTy
}
GlobalObject::GlobalObject()
: Object(interpreter().object_prototype())
: Object(nullptr)
{
}
void GlobalObject::initialize()
{
// These are done first since other prototypes depend on their presence.
m_object_prototype = heap().allocate<ObjectPrototype>();
m_function_prototype = heap().allocate<FunctionPrototype>();
static_cast<FunctionPrototype*>(m_function_prototype)->initialize();
static_cast<ObjectPrototype*>(m_object_prototype)->initialize();
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
if (!m_##snake_name##_prototype) \
m_##snake_name##_prototype = heap().allocate<PrototypeName>();
JS_ENUMERATE_BUILTIN_TYPES
#undef __JS_ENUMERATE
put_native_function("gc", gc);
put_native_function("isNaN", is_nan, 1);
@ -67,17 +96,17 @@ GlobalObject::GlobalObject()
put("console", heap().allocate<ConsoleObject>());
put("Math", heap().allocate<MathObject>());
add_constructor("Array", m_array_constructor, *interpreter().array_prototype());
add_constructor("Boolean", m_boolean_constructor, *interpreter().boolean_prototype());
add_constructor("Date", m_date_constructor, *interpreter().date_prototype());
add_constructor("Error", m_error_constructor, *interpreter().error_prototype());
add_constructor("Function", m_function_constructor, *interpreter().function_prototype());
add_constructor("Number", m_number_constructor, *interpreter().number_prototype());
add_constructor("Object", m_object_constructor, *interpreter().object_prototype());
add_constructor("String", m_string_constructor, *interpreter().string_prototype());
add_constructor("Array", m_array_constructor, *m_array_prototype);
add_constructor("Boolean", m_boolean_constructor, *m_boolean_prototype);
add_constructor("Date", m_date_constructor, *m_date_prototype);
add_constructor("Error", m_error_constructor, *m_error_prototype);
add_constructor("Function", m_function_constructor, *m_function_prototype);
add_constructor("Number", m_number_constructor, *m_number_prototype);
add_constructor("Object", m_object_constructor, *m_object_prototype);
add_constructor("String", m_string_constructor, *m_string_prototype);
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
add_constructor(#ClassName, m_##snake_name##_constructor, *interpreter().snake_name##_prototype());
add_constructor(#ClassName, m_##snake_name##_constructor, *m_##snake_name##_prototype);
JS_ENUMERATE_ERROR_SUBCLASSES
#undef __JS_ENUMERATE
}