dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / external / lib_js / realm.lucb
#==============================================================================================
#
# realm - JS::Realm and JS::ExecutionContext for the web engine
#
# DESCRIPTION:
# What LibWeb needs of JS::Realm before scripting, ported from
# Libraries/LibJS/Runtime/Realm.cpp/.h and ExecutionContext.cpp/.h (DESIGN.md ยง3.6): a
# realm is a cell with a global object and a [[HostDefined]] (the web intrinsics and, for
# a principal realm, its environment settings object and page); realm.create<T>(...)
# allocates a cell of the realm's heap and initializes it for the realm (the generated
# realm_create_<class> helpers, through web_allocate_cell and web_initialize_cell).
# InitializeHostDefinedRealm runs its steps except those that create JS objects: the JS
# intrinsics, the global environment and the default global bindings come with luce-js
# in phase 3.
#
# Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
# Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Realm ===================================================================================
## Ported from Libraries/LibJS/Runtime/Realm.cpp:22 JS::Realm::initialize_host_defined_realm:
## a new realm, made the realm of a new execution context that is pushed onto the VM's execution
## context stack and returned; its global object is the one `create_global_object` makes (and
## `create_global_this_value` the this binding).
pub func realm_initialize_host_defined_realm(vm: Vm*, create_global_object: ak.Function1[Realm*, JsObject*?], create_global_this_value: ak.Function1[Realm*, JsObject*?]) -> JsExecutionContext*!:
let heap = vm_heap(vm)
gc.heap_defer_gc(heap)
defer gc.heap_undefer_gc(heap)
# 1. Let realm be a new Realm Record
let realm = (Realm*)web_heap_allocate_cell(heap, &realm_class)
# 2. Perform CreateIntrinsics(realm).
# Phase 3: the ECMAScript intrinsics are luce-js objects (Intrinsics::create(*realm)).
# FIXME: 3. Set realm.[[AgentSignifier]] to AgentSignifier().
# NOTE: Done on step 1.
# 4. Set realm.[[GlobalObject]] to undefined.
# 5. Set realm.[[GlobalEnv]] to undefined.
# FIXME: 6. Set realm.[[TemplateMap]] to a new empty List.
# 7. Let newContext be a new execution context.
let new_context = js_execution_context_create()
# 8. Set the Function of newContext to null.
new_context.function = none
# 9. Set the Realm of newContext to realm.
new_context.realm = realm
# 10. Set the ScriptOrModule of newContext to null.
# (Phase 3: ScriptOrModule.)
# 11. Push newContext onto the execution context stack; newContext is now the running execution context.
vm_push_execution_context(vm, new_context)
# 12. If the host requires use of an exotic object to serve as realm's global object, then
var global: JsObject*? = none
if let call = create_global_object.call:
# a. Let global be such an object created in a host-defined manner.
global = call(create_global_object.context, realm)
# 13. Else,
else:
# a. Let global be OrdinaryObjectCreate(realm.[[Intrinsics]].[[%Object.prototype%]]).
# NOTE: We allocate a proper GlobalObject directly as this plain object is
# turned into one via SetDefaultGlobalBindings in the spec.
trap("unported (P3): JS::Realm::initialize_host_defined_realm without a host global object (JS::GlobalObject)")
# 14. If the host requires that the this binding in realm's global scope return an object other than the global object, then
var this_value: JsObject*? = none
if let call = create_global_this_value.call:
# a. Let thisValue be such an object created in a host-defined manner.
this_value = call(create_global_this_value.context, realm)
# 15. Else,
else:
# a. Let thisValue be global.
this_value = global
# 16. Set realm.[[GlobalObject]] to global.
realm.m_global_object = global
# 17. Set realm.[[GlobalEnv]] to NewGlobalEnvironment(global, thisValue).
# Phase 3: the global environment (a JS environment over global and this_value). Phase 1
# keeps its [[GlobalThisValue]] (Window::window() returns it).
realm.m_global_this_value = this_value
# 18. Perform ? SetDefaultGlobalBindings(realm).
# Phase 3: the default global bindings are JS properties.
# 19. Create any host-defined global object properties on global.
js_cell_initialize((JsCell*)(global else trap("no global object")), realm)
# 20. Return unused.
return new_context
## Ported from Libraries/LibJS/Runtime/Realm.cpp:103 JS::Realm::visit_edges (override).
pub func realm_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (Realm*)base
gc.cell_visit_edges_impl(base, visitor)
# visitor.visit(m_intrinsics), m_global_environment, m_global_declarative_environment: phase 3.
js_visit_object(visitor, this.m_global_object)
js_visit_object(visitor, this.m_global_this_value)
if let host_defined = this.m_host_defined:
bind_host_defined_visit_edges(host_defined, visitor)
## GC_CELL(Realm, Cell)'s class_name.
func realm_class_name_impl(this: gc.Cell*) -> ak.StringView:
return ak.sv("Realm")
## Ported from Libraries/LibJS/Runtime/Realm.h:53 JS::Realm::global_object.
pub func realm_global_object(this: Realm*) -> JsObject*:
return this.m_global_object else trap("realm has no global object")
## Ported from Libraries/LibJS/Runtime/Realm.h:54 JS::Realm::set_global_object.
pub func realm_set_global_object(this: Realm*, global: JsObject*):
this.m_global_object = global
## `realm.global_environment().global_this_value()` (JS::GlobalEnvironment::global_this_value,
## Libraries/LibJS/Runtime/GlobalEnvironment.h): the this binding of the realm's global scope.
## (Added by region r28 for Window::window, self and frames.)
pub func realm_global_this_value(this: Realm*) -> JsObject*:
return this.m_global_this_value else trap("realm has no global this value")
## Ported from Libraries/LibJS/Runtime/Realm.h:69 JS::Realm::host_defined.
pub func realm_host_defined(this: Realm*) -> BindHostDefined*?:
return this.m_host_defined
## Ported from Libraries/LibJS/Runtime/Realm.h:72 JS::Realm::set_host_defined. The HostDefined
## (an OwnPtr in C++) is managed memory the realm's bytes refer to.
pub func realm_set_host_defined(this: Realm*, host_defined: BindHostDefined*?):
this.m_host_defined = host_defined
## JS::Realm::vm(), JS::Cell's.
pub func realm_vm(this: Realm*) -> Vm*:
return js_cell_vm(this.js_cell())
## JS::Realm::heap(), GC::Cell's: the heap the realm's cells are allocated in.
pub func realm_heap(this: Realm*) -> gc.Heap*:
return gc.cell_heap(this.cell())
# mark: ExecutionContext ========================================================================
## Ported from Libraries/LibJS/Runtime/ExecutionContext.cpp:92 JS::ExecutionContext::create: a
## new execution context with no function and no realm (NonnullOwnPtr: managed memory, kept
## by its owner). The registers, constants and arguments are phase 3.
pub func js_execution_context_create() -> JsExecutionContext*:
return (new JsExecutionContext(function = none, realm = none)) else trap("out of memory")
## Ported from Libraries/LibJS/Runtime/ExecutionContext.cpp:128 JS::ExecutionContext::visit_edges:
## the cells of the context (its owner's visit_edges calls it).
pub func js_execution_context_visit_edges(this: JsExecutionContext*, visitor: gc.CellVisitor*):
gc.cell_visitor_visit_cell(visitor, (gc.Cell*?)this.function)
if let realm = this.realm:
gc.cell_visitor_visit_t_2(visitor, realm)
if let this_value = this.this_value:
js_visit_value(visitor, this_value)
for argument in this.arguments:
js_visit_value(visitor, argument)