Skip to content

Commit

Permalink
Support realms in test262
Browse files Browse the repository at this point in the history
If the `--expose-internals` flag is enabled, this patch adds a
`__nova__.createRealm()` function that creates a new realm and returns
its global. This is then used by the test262 runner to support testing
cross-realm interactions for the first time.
  • Loading branch information
andreubotella committed Feb 16, 2025
1 parent 124147c commit 62f161d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 93 deletions.
42 changes: 42 additions & 0 deletions nova_cli/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,25 @@ pub fn initialize_global_object_with_internals(agent: &mut Agent, global: Object
Ok(Value::Undefined)
}

fn create_realm(
agent: &mut Agent,
_this: Value,
_args: ArgumentsList,
_gc: GcScope,
) -> JsResult<Value> {
let create_global_object: Option<for<'a> fn(&mut Agent, GcScope<'a, '_>) -> Object<'a>> =
None;
let create_global_this_value: Option<
for<'a> fn(&mut Agent, GcScope<'a, '_>) -> Object<'a>,
> = None;
let realm = agent.create_realm(
create_global_object,
create_global_this_value,
Some(initialize_global_object_with_internals),
);
Ok(realm.global_object(agent).into_value())
}

initialize_global_object(agent, global, gc.reborrow());

let nova_obj = OrdinaryObject::create(agent, gc.nogc()).scope(agent, gc.nogc());
Expand Down Expand Up @@ -153,6 +172,29 @@ pub fn initialize_global_object_with_internals(agent: &mut Agent, global: Object
gc.reborrow(),
)
.unwrap();

let function = create_builtin_function(
agent,
Behaviour::Regular(create_realm),
BuiltinFunctionArgs::new(1, "createRealm", agent.current_realm_id()),
gc.nogc(),
);
let property_key = PropertyKey::from_static_str(agent, "createRealm", gc.nogc()).unbind();
nova_obj
.get(agent)
.internal_define_own_property(
agent,
property_key,
PropertyDescriptor {
value: Some(function.into_value()),
writable: Some(true),
enumerable: Some(false),
configurable: Some(true),
..Default::default()
},
gc.reborrow(),
)
.unwrap();
}

/// Exit the program with parse errors.
Expand Down
4 changes: 4 additions & 0 deletions nova_vm/src/ecmascript/execution/realm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ impl RealmIdentifier {
pub(crate) const fn into_u32_index(self) -> u32 {
self.0.get() - 1
}

pub fn global_object(self, agent: &mut Agent) -> Object {
agent[self].global_object
}
}

impl Index<RealmIdentifier> for Agent {
Expand Down
Loading

0 comments on commit 62f161d

Please sign in to comment.