1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-10 03:57:35 +00:00

LibJS: Move intrinsics to the realm

Intrinsics, i.e. mostly constructor and prototype objects, but also
things like empty and new object shape now live on a new heap-allocated
JS::Intrinsics object, thus completing the long journey of taking all
the magic away from the global object.
This represents the Realm's [[Intrinsics]] slot in the spec and matches
its existing [[GlobalObject]] / [[GlobalEnv]] slots in terms of
architecture.

In the majority of cases it should now be possibly to fully allocate a
regular object without the global object existing, and in fact that's
what we do now - the realm is allocated before the global object, and
the intrinsics between both :^)
This commit is contained in:
Linus Groh 2022-08-27 00:54:55 +01:00
parent 84c4b66721
commit 50428ea8d2
217 changed files with 1305 additions and 1039 deletions

View file

@ -33,16 +33,16 @@ ECMAScriptFunctionObject* ECMAScriptFunctionObject::create(Realm& realm, FlyStri
Object* prototype = nullptr;
switch (kind) {
case FunctionKind::Normal:
prototype = realm.global_object().function_prototype();
prototype = realm.intrinsics().function_prototype();
break;
case FunctionKind::Generator:
prototype = realm.global_object().generator_function_prototype();
prototype = realm.intrinsics().generator_function_prototype();
break;
case FunctionKind::Async:
prototype = realm.global_object().async_function_prototype();
prototype = realm.intrinsics().async_function_prototype();
break;
case FunctionKind::AsyncGenerator:
prototype = realm.global_object().async_generator_function_prototype();
prototype = realm.intrinsics().async_generator_function_prototype();
break;
}
return realm.heap().allocate<ECMAScriptFunctionObject>(realm, move(name), move(source_text), ecmascript_code, move(parameters), m_function_length, parent_environment, private_environment, *prototype, kind, is_strict, might_need_arguments_object, contains_direct_call_to_eval, is_arrow_function, move(class_field_initializer_name));
@ -114,17 +114,17 @@ void ECMAScriptFunctionObject::initialize(Realm& realm)
Object* prototype = nullptr;
switch (m_kind) {
case FunctionKind::Normal:
prototype = vm.heap().allocate<Object>(realm, *realm.global_object().new_ordinary_function_prototype_object_shape());
prototype = vm.heap().allocate<Object>(realm, *realm.intrinsics().new_ordinary_function_prototype_object_shape());
MUST(prototype->define_property_or_throw(vm.names.constructor, { .value = this, .writable = true, .enumerable = false, .configurable = true }));
break;
case FunctionKind::Generator:
// prototype is "g1.prototype" in figure-2 (https://tc39.es/ecma262/img/figure-2.png)
prototype = Object::create(realm, realm.global_object().generator_function_prototype_prototype());
prototype = Object::create(realm, realm.intrinsics().generator_function_prototype_prototype());
break;
case FunctionKind::Async:
break;
case FunctionKind::AsyncGenerator:
prototype = Object::create(realm, realm.global_object().async_generator_function_prototype_prototype());
prototype = Object::create(realm, realm.intrinsics().async_generator_function_prototype_prototype());
break;
}
// 27.7.4 AsyncFunction Instances, https://tc39.es/ecma262/#sec-async-function-instances
@ -208,7 +208,7 @@ ThrowCompletionOr<Object*> ECMAScriptFunctionObject::internal_construct(MarkedVe
// 3. If kind is base, then
if (kind == ConstructorKind::Base) {
// a. Let thisArgument be ? OrdinaryCreateFromConstructor(newTarget, "%Object.prototype%").
this_argument = TRY(ordinary_create_from_constructor<Object>(vm, new_target, &GlobalObject::object_prototype));
this_argument = TRY(ordinary_create_from_constructor<Object>(vm, new_target, &Intrinsics::object_prototype));
}
ExecutionContext callee_context(heap());
@ -863,7 +863,7 @@ Completion ECMAScriptFunctionObject::ordinary_call_evaluate_body()
// AsyncFunctionBody : FunctionBody
else if (m_kind == FunctionKind::Async) {
// 1. Let promiseCapability be ! NewPromiseCapability(%Promise%).
auto promise_capability = MUST(new_promise_capability(vm, realm.global_object().promise_constructor()));
auto promise_capability = MUST(new_promise_capability(vm, realm.intrinsics().promise_constructor()));
// 2. Let declResult be Completion(FunctionDeclarationInstantiation(functionObject, argumentsList)).
auto declaration_result = function_declaration_instantiation(ast_interpreter);