Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support realms in test262 #567

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_empty_object(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