summaryrefslogtreecommitdiff
path: root/ext/node/ops/vm_internal.rs
diff options
context:
space:
mode:
Diffstat (limited to 'ext/node/ops/vm_internal.rs')
-rw-r--r--ext/node/ops/vm_internal.rs115
1 files changed, 78 insertions, 37 deletions
diff --git a/ext/node/ops/vm_internal.rs b/ext/node/ops/vm_internal.rs
index 54b9aaa47..4af182595 100644
--- a/ext/node/ops/vm_internal.rs
+++ b/ext/node/ops/vm_internal.rs
@@ -69,9 +69,9 @@ impl ContextifyContext {
scope: &mut v8::HandleScope,
sandbox_obj: v8::Local<v8::Object>,
) {
- let tmp = init_global_template(scope);
+ let tmp = init_global_template(scope, ContextInitMode::UseSnapshot);
- let context = create_v8_context(scope, tmp, None);
+ let context = create_v8_context(scope, tmp, ContextInitMode::UseSnapshot);
Self::from_context(scope, context, sandbox_obj);
}
@@ -84,6 +84,8 @@ impl ContextifyContext {
let context_state = main_context.get_aligned_pointer_from_embedder_data(
deno_core::CONTEXT_STATE_SLOT_INDEX,
);
+ let module_map = main_context
+ .get_aligned_pointer_from_embedder_data(deno_core::MODULE_MAP_SLOT_INDEX);
v8_context.set_security_token(main_context.get_security_token(scope));
// SAFETY: set embedder data from the creation context
@@ -92,6 +94,10 @@ impl ContextifyContext {
deno_core::CONTEXT_STATE_SLOT_INDEX,
context_state,
);
+ v8_context.set_aligned_pointer_in_embedder_data(
+ deno_core::MODULE_MAP_SLOT_INDEX,
+ module_map,
+ );
}
let context = v8::Global::new(scope, v8_context);
@@ -106,7 +112,7 @@ impl ContextifyContext {
// lives longer than the execution context, so this should be safe.
unsafe {
v8_context.set_aligned_pointer_in_embedder_data(
- 2,
+ 3,
ptr as *const ContextifyContext as _,
);
}
@@ -168,7 +174,10 @@ impl ContextifyContext {
) -> Option<&'c ContextifyContext> {
let context = object.get_creation_context(scope)?;
- let context_ptr = context.get_aligned_pointer_from_embedder_data(2);
+ let context_ptr = context.get_aligned_pointer_from_embedder_data(3);
+ if context_ptr.is_null() {
+ return None;
+ }
// SAFETY: We are storing a pointer to the ContextifyContext
// in the embedder data of the v8::Context during creation.
Some(unsafe { &*(context_ptr as *const ContextifyContext) })
@@ -177,17 +186,31 @@ impl ContextifyContext {
pub const VM_CONTEXT_INDEX: usize = 0;
-fn create_v8_context<'a>(
- scope: &mut v8::HandleScope<'a>,
+#[derive(PartialEq)]
+pub enum ContextInitMode {
+ ForSnapshot,
+ UseSnapshot,
+}
+
+pub fn create_v8_context<'a>(
+ scope: &mut v8::HandleScope<'a, ()>,
object_template: v8::Local<v8::ObjectTemplate>,
- snapshot_data: Option<&'static [u8]>,
+ mode: ContextInitMode,
) -> v8::Local<'a, v8::Context> {
let scope = &mut v8::EscapableHandleScope::new(scope);
- let context = if let Some(_snapshot_data) = snapshot_data {
+ let context = if mode == ContextInitMode::UseSnapshot {
v8::Context::from_snapshot(scope, VM_CONTEXT_INDEX).unwrap()
} else {
- v8::Context::new_from_template(scope, object_template)
+ let ctx = v8::Context::new_from_template(scope, object_template);
+ // SAFETY: ContextifyContexts will update this to a pointer to the native object
+ unsafe {
+ ctx.set_aligned_pointer_in_embedder_data(1, std::ptr::null_mut());
+ ctx.set_aligned_pointer_in_embedder_data(2, std::ptr::null_mut());
+ ctx.set_aligned_pointer_in_embedder_data(3, std::ptr::null_mut());
+ ctx.clear_all_slots(scope);
+ };
+ ctx
};
scope.escape(context)
@@ -196,25 +219,31 @@ fn create_v8_context<'a>(
#[derive(Debug, Clone)]
struct SlotContextifyGlobalTemplate(v8::Global<v8::ObjectTemplate>);
-fn init_global_template<'a>(
- scope: &mut v8::HandleScope<'a>,
+pub fn init_global_template<'a>(
+ scope: &mut v8::HandleScope<'a, ()>,
+ mode: ContextInitMode,
) -> v8::Local<'a, v8::ObjectTemplate> {
- let mut maybe_object_template_slot =
+ let maybe_object_template_slot =
scope.get_slot::<SlotContextifyGlobalTemplate>();
if maybe_object_template_slot.is_none() {
- init_global_template_inner(scope);
- maybe_object_template_slot =
- scope.get_slot::<SlotContextifyGlobalTemplate>();
+ let global_object_template = init_global_template_inner(scope);
+
+ if mode == ContextInitMode::UseSnapshot {
+ let contextify_global_template_slot = SlotContextifyGlobalTemplate(
+ v8::Global::new(scope, global_object_template),
+ );
+ scope.set_slot(contextify_global_template_slot);
+ }
+ global_object_template
+ } else {
+ let object_template_slot = maybe_object_template_slot
+ .expect("ContextifyGlobalTemplate slot should be already populated.")
+ .clone();
+ v8::Local::new(scope, object_template_slot.0)
}
- let object_template_slot = maybe_object_template_slot
- .expect("ContextifyGlobalTemplate slot should be already populated.")
- .clone();
- v8::Local::new(scope, object_template_slot.0)
}
-extern "C" fn c_noop(_: *const v8::FunctionCallbackInfo) {}
-
// Using thread_local! to get around compiler bug.
//
// See NOTE in ext/node/global.rs#L12
@@ -235,11 +264,11 @@ thread_local! {
pub static INDEXED_DESCRIPTOR_MAP_FN: v8::IndexedPropertyGetterCallback<'static> = indexed_property_descriptor.map_fn_to();
}
-fn init_global_template_inner(scope: &mut v8::HandleScope) {
- let global_func_template =
- v8::FunctionTemplate::builder_raw(c_noop).build(scope);
- let global_object_template = global_func_template.instance_template(scope);
- global_object_template.set_internal_field_count(2);
+pub fn init_global_template_inner<'a>(
+ scope: &mut v8::HandleScope<'a, ()>,
+) -> v8::Local<'a, v8::ObjectTemplate> {
+ let global_object_template = v8::ObjectTemplate::new(scope);
+ global_object_template.set_internal_field_count(3);
let named_property_handler_config = {
let mut config = v8::NamedPropertyHandlerConfiguration::new()
@@ -279,10 +308,8 @@ fn init_global_template_inner(scope: &mut v8::HandleScope) {
.set_named_property_handler(named_property_handler_config);
global_object_template
.set_indexed_property_handler(indexed_property_handler_config);
- let contextify_global_template_slot = SlotContextifyGlobalTemplate(
- v8::Global::new(scope, global_object_template),
- );
- scope.set_slot(contextify_global_template_slot);
+
+ global_object_template
}
fn property_getter<'s>(
@@ -291,7 +318,9 @@ fn property_getter<'s>(
args: v8::PropertyCallbackArguments<'s>,
mut ret: v8::ReturnValue,
) -> v8::Intercepted {
- let ctx = ContextifyContext::get(scope, args.this()).unwrap();
+ let Some(ctx) = ContextifyContext::get(scope, args.this()) else {
+ return v8::Intercepted::No;
+ };
let sandbox = ctx.sandbox(scope);
@@ -325,7 +354,9 @@ fn property_setter<'s>(
args: v8::PropertyCallbackArguments<'s>,
mut rv: v8::ReturnValue,
) -> v8::Intercepted {
- let ctx = ContextifyContext::get(scope, args.this()).unwrap();
+ let Some(ctx) = ContextifyContext::get(scope, args.this()) else {
+ return v8::Intercepted::No;
+ };
let (attributes, is_declared_on_global_proxy) = match ctx
.global_proxy(scope)
@@ -416,7 +447,9 @@ fn property_deleter<'s>(
args: v8::PropertyCallbackArguments<'s>,
mut rv: v8::ReturnValue,
) -> v8::Intercepted {
- let ctx = ContextifyContext::get(scope, args.this()).unwrap();
+ let Some(ctx) = ContextifyContext::get(scope, args.this()) else {
+ return v8::Intercepted::No;
+ };
let context = ctx.context(scope);
let sandbox = ctx.sandbox(scope);
@@ -434,7 +467,9 @@ fn property_enumerator<'s>(
args: v8::PropertyCallbackArguments<'s>,
mut rv: v8::ReturnValue,
) {
- let ctx = ContextifyContext::get(scope, args.this()).unwrap();
+ let Some(ctx) = ContextifyContext::get(scope, args.this()) else {
+ return;
+ };
let context = ctx.context(scope);
let sandbox = ctx.sandbox(scope);
@@ -455,7 +490,9 @@ fn property_definer<'s>(
args: v8::PropertyCallbackArguments<'s>,
_: v8::ReturnValue,
) -> v8::Intercepted {
- let ctx = ContextifyContext::get(scope, args.this()).unwrap();
+ let Some(ctx) = ContextifyContext::get(scope, args.this()) else {
+ return v8::Intercepted::No;
+ };
let context = ctx.context(scope);
let (attributes, is_declared) = match ctx
@@ -533,7 +570,9 @@ fn property_descriptor<'s>(
args: v8::PropertyCallbackArguments<'s>,
mut rv: v8::ReturnValue,
) -> v8::Intercepted {
- let ctx = ContextifyContext::get(scope, args.this()).unwrap();
+ let Some(ctx) = ContextifyContext::get(scope, args.this()) else {
+ return v8::Intercepted::No;
+ };
let context = ctx.context(scope);
let sandbox = ctx.sandbox(scope);
@@ -585,7 +624,9 @@ fn indexed_property_deleter<'s>(
args: v8::PropertyCallbackArguments<'s>,
mut rv: v8::ReturnValue,
) -> v8::Intercepted {
- let ctx = ContextifyContext::get(scope, args.this()).unwrap();
+ let Some(ctx) = ContextifyContext::get(scope, args.this()) else {
+ return v8::Intercepted::No;
+ };
let context = ctx.context(scope);
let sandbox = ctx.sandbox(scope);