Skip to content

Commit

Permalink
Add a basic ShadowRealm constructor test.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreubotella committed Oct 20, 2022
1 parent 3bb2cea commit 51f68f5
Showing 1 changed file with 66 additions and 8 deletions.
74 changes: 66 additions & 8 deletions core/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,27 @@ fn v8_init(
" --turbo_fast_api_calls",
// This flag prevents "unresolved external reference" panic during
// build, which started happening in V8 10.6
" --noexperimental-async-stack-tagging-api",
" --noexperimental-async-stack-tagging-api"
);

if predictable {
v8::V8::set_flags_from_string(&format!(
"{}{}",
flags, " --predictable --random-seed=42"
));
// TODO(andreubotella): We want to turn on this flag for unit tests, but not
// yet for debug or release builds.
let test_flags = if cfg!(test) {
" --harmony-shadow-realm"
} else {
v8::V8::set_flags_from_string(flags);
}
""
};

let predictable_flags = if predictable {
" --predictable --random-seed=42"
} else {
""
};

v8::V8::set_flags_from_string(&format!(
"{}{}{}",
flags, test_flags, predictable_flags
));

let v8_platform = v8_platform
.unwrap_or_else(|| v8::new_default_platform(0, false).make_shared());
Expand Down Expand Up @@ -4407,4 +4417,52 @@ Deno.core.opAsync('op_async_serialize_object_with_numbers_as_keys', {
));
});
}

#[test]
fn test_shadowrealm_constructor() {
let mut runtime = JsRuntime::new(Default::default());
let main_realm = runtime.global_realm();
let other_realm = runtime.create_realm().unwrap();

fn initialize(isolate: &mut v8::Isolate, realm: &JsRealm) {
realm
.execute_script(
isolate,
"",
r#"
globalThis.sr1 = new ShadowRealm();
globalThis.sr2 = new ShadowRealm();
globalThis.test = (globalThis.test ?? 0) + 1;
globalThis.sr1.evaluate(`
globalThis.test = (globalThis.test ?? 0) + 1;
`);
globalThis.sr2.evaluate(`
globalThis.test = (globalThis.test ?? 0) + 1;
`);
"#,
)
.unwrap();
}
initialize(runtime.v8_isolate(), &main_realm);
initialize(runtime.v8_isolate(), &other_realm);

fn test(isolate: &mut v8::Isolate, realm: &JsRealm) {
let value = realm
.execute_script(
isolate,
"",
r#"
globalThis.test === 1 &&
globalThis.sr1.evaluate(`globalThis.test`) === 1 &&
globalThis.sr2.evaluate(`globalThis.test`) === 1
"#,
)
.unwrap();
let mut scope = realm.handle_scope(isolate);
assert!(value.open(&mut scope).is_true());
}
test(runtime.v8_isolate(), &main_realm);
test(runtime.v8_isolate(), &other_realm);
}
}

0 comments on commit 51f68f5

Please sign in to comment.