Skip to content
Merged
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
1 change: 1 addition & 0 deletions core/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub use crate::resources::Resource;
pub use crate::resources::ResourceId;
pub use crate::resources::ResourceTable;
pub use crate::runtime::CompiledWasmModuleStore;
pub use crate::runtime::CreateRealmOptions;
pub use crate::runtime::CrossIsolateStore;
pub use crate::runtime::JsRealm;
pub use crate::runtime::JsRuntime;
Expand Down
3 changes: 2 additions & 1 deletion core/runtime/jsrealm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ pub(crate) struct ContextState {
/// ```
/// use deno_core::JsRuntime;
/// use deno_core::RuntimeOptions;
/// use deno_core::CreateRealmOptions;
///
/// let mut runtime = JsRuntime::new(RuntimeOptions::default());
/// let new_realm = runtime
/// .create_realm()
/// .create_realm(CreateRealmOptions::default())
/// .expect("Handle the error properly");
/// let source_code = "var a = 0; a + 1";
/// let result = new_realm
Expand Down
18 changes: 17 additions & 1 deletion core/runtime/jsruntime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,16 @@ pub struct RuntimeSnapshotOptions {
pub snapshot_module_load_cb: Option<ExtModuleLoaderCb>,
}

#[derive(Default)]
pub struct CreateRealmOptions {
/// Implementation of `ModuleLoader` which will be
/// called when V8 requests to load ES modules in the realm.
///
/// If not provided, there will be an error if code being
/// executed tries to load modules from the realm.
pub module_loader: Option<Rc<dyn ModuleLoader>>,
}

impl JsRuntime {
/// Only constructor, configuration is done through `options`.
pub fn new(mut options: RuntimeOptions) -> JsRuntime {
Expand Down Expand Up @@ -800,7 +810,12 @@ impl JsRuntime {
/// pre-initialized with all of the extensions that were passed in
/// [`RuntimeOptions::extensions`] when the [`JsRuntime`] was
/// constructed.
pub fn create_realm(&mut self) -> Result<JsRealm, Error> {
// TODO(bartlomieju): options is not used right now - will be used in a follow
// up PR.
pub fn create_realm(
&mut self,
_options: CreateRealmOptions,
) -> Result<JsRealm, Error> {
let realm = {
let context_state = Rc::new(RefCell::new(ContextState::default()));
let op_ctxs: Box<[OpCtx]> = self
Expand Down Expand Up @@ -847,6 +862,7 @@ impl JsRuntime {
self.init_mode,
);
context.set_slot(scope, context_state.clone());

let realm = JsRealmInner::new(
context_state,
v8::Global::new(scope, context),
Expand Down
1 change: 1 addition & 0 deletions core/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub const V8_WRAPPER_OBJECT_INDEX: i32 = 1;
pub(crate) use jsrealm::ContextState;
pub use jsrealm::JsRealm;
pub use jsruntime::CompiledWasmModuleStore;
pub use jsruntime::CreateRealmOptions;
pub use jsruntime::CrossIsolateStore;
pub(crate) use jsruntime::InitMode;
pub use jsruntime::JsRuntime;
Expand Down
18 changes: 9 additions & 9 deletions core/runtime/tests/jsrealm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use std::task::Poll;
async fn test_set_promise_reject_callback_realms() {
let mut runtime = JsRuntime::new(RuntimeOptions::default());
let main_realm = runtime.main_realm();
let realm1 = runtime.create_realm().unwrap();
let realm2 = runtime.create_realm().unwrap();
let realm1 = runtime.create_realm(Default::default()).unwrap();
let realm2 = runtime.create_realm(Default::default()).unwrap();

let realm_expectations = &[
(&main_realm, "main_realm", 42),
Expand Down Expand Up @@ -70,7 +70,7 @@ fn js_realm_simple() {
v8::Global::new(scope, local_global)
};

let realm = runtime.create_realm().unwrap();
let realm = runtime.create_realm(Default::default()).unwrap();
assert_ne!(realm.context(), &main_context);
assert_ne!(realm.global_object(runtime.v8_isolate()), main_global);

Expand All @@ -93,7 +93,7 @@ fn js_realm_init() {
extensions: vec![test_ext::init_ops()],
..Default::default()
});
let realm = runtime.create_realm().unwrap();
let realm = runtime.create_realm(Default::default()).unwrap();
let ret = realm
.execute_script_static(runtime.v8_isolate(), "", "Deno.core.ops.op_test()")
.unwrap();
Expand Down Expand Up @@ -122,7 +122,7 @@ fn js_realm_init_snapshot() {
extensions: vec![test_ext::init_ops()],
..Default::default()
});
let realm = runtime.create_realm().unwrap();
let realm = runtime.create_realm(Default::default()).unwrap();
let ret = realm
.execute_script_static(runtime.v8_isolate(), "", "Deno.core.ops.op_test()")
.unwrap();
Expand Down Expand Up @@ -155,7 +155,7 @@ fn js_realm_sync_ops() {
}),
..Default::default()
});
let new_realm = runtime.create_realm().unwrap();
let new_realm = runtime.create_realm(Default::default()).unwrap();

// Test in both realms
for realm in [runtime.main_realm(), new_realm].into_iter() {
Expand Down Expand Up @@ -205,7 +205,7 @@ async fn js_realm_async_ops() {
});

let main_realm = runtime.main_realm();
let new_realm = runtime.create_realm().unwrap();
let new_realm = runtime.create_realm(Default::default()).unwrap();

let mut rets = vec![];

Expand Down Expand Up @@ -286,7 +286,7 @@ async fn js_realm_gc() {
.put(opstate_drop_detect.clone());
assert_eq!(Rc::strong_count(&opstate_drop_detect), 2);

let other_realm = runtime.create_realm().unwrap();
let other_realm = runtime.create_realm(Default::default()).unwrap();
other_realm
.execute_script(
runtime.v8_isolate(),
Expand Down Expand Up @@ -327,7 +327,7 @@ async fn js_realm_ref_unref_ops() {

poll_fn(move |cx| {
let main_realm = runtime.main_realm();
let other_realm = runtime.create_realm().unwrap();
let other_realm = runtime.create_realm(Default::default()).unwrap();

main_realm
.execute_script_static(
Expand Down