2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-01-06 10:24:44 -05:00
|
|
|
|
2023-01-05 14:29:50 -05:00
|
|
|
use std::option::Option;
|
|
|
|
use std::os::raw::c_void;
|
|
|
|
|
|
|
|
use log::debug;
|
|
|
|
use v8::MapFnTo;
|
|
|
|
|
2021-10-19 12:26:45 -04:00
|
|
|
use crate::error::is_instance_of_error;
|
2023-04-12 07:45:36 -04:00
|
|
|
use crate::error::JsStackFrame;
|
2022-05-07 12:46:35 -04:00
|
|
|
use crate::modules::get_asserted_module_type_from_assertions;
|
2021-12-15 13:22:36 -05:00
|
|
|
use crate::modules::parse_import_assertions;
|
2023-02-08 12:13:33 -05:00
|
|
|
use crate::modules::resolve_helper;
|
2021-12-15 13:22:36 -05:00
|
|
|
use crate::modules::validate_import_assertions;
|
|
|
|
use crate::modules::ImportAssertionsKind;
|
2021-06-28 21:03:02 -04:00
|
|
|
use crate::modules::ModuleMap;
|
2023-01-10 08:35:44 -05:00
|
|
|
use crate::modules::ResolutionKind;
|
2022-04-08 04:32:48 -04:00
|
|
|
use crate::ops::OpCtx;
|
2023-03-16 09:27:16 -04:00
|
|
|
use crate::snapshot_util::SnapshotOptions;
|
2023-01-14 22:39:00 -05:00
|
|
|
use crate::JsRealm;
|
2020-09-06 15:44:29 -04:00
|
|
|
use crate::JsRuntime;
|
2020-01-06 10:24:44 -05:00
|
|
|
|
2023-03-18 18:30:04 -04:00
|
|
|
pub(crate) fn external_references(ops: &[OpCtx]) -> v8::ExternalReferences {
|
|
|
|
// Overallocate a bit, it's better than having to resize the vector.
|
|
|
|
let mut references = Vec::with_capacity(4 + ops.len() * 4);
|
|
|
|
|
|
|
|
references.push(v8::ExternalReference {
|
|
|
|
function: call_console.map_fn_to(),
|
|
|
|
});
|
|
|
|
references.push(v8::ExternalReference {
|
|
|
|
function: import_meta_resolve.map_fn_to(),
|
|
|
|
});
|
|
|
|
references.push(v8::ExternalReference {
|
|
|
|
function: catch_dynamic_import_promise_error.map_fn_to(),
|
|
|
|
});
|
|
|
|
references.push(v8::ExternalReference {
|
|
|
|
function: empty_fn.map_fn_to(),
|
|
|
|
});
|
2022-08-21 08:07:53 -04:00
|
|
|
|
|
|
|
for ctx in ops {
|
|
|
|
let ctx_ptr = ctx as *const OpCtx as _;
|
|
|
|
references.push(v8::ExternalReference { pointer: ctx_ptr });
|
|
|
|
references.push(v8::ExternalReference {
|
|
|
|
function: ctx.decl.v8_fn_ptr,
|
|
|
|
});
|
2023-03-18 18:30:04 -04:00
|
|
|
if let Some(fast_fn) = &ctx.decl.fast_fn {
|
|
|
|
references.push(v8::ExternalReference {
|
2023-03-31 08:42:14 -04:00
|
|
|
pointer: fast_fn.function as _,
|
2023-03-18 18:30:04 -04:00
|
|
|
});
|
|
|
|
references.push(v8::ExternalReference {
|
|
|
|
pointer: ctx.fast_fn_c_info.unwrap().as_ptr() as _,
|
|
|
|
});
|
2022-08-21 08:07:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let refs = v8::ExternalReferences::new(&references);
|
|
|
|
// Leak, V8 takes ownership of the references.
|
|
|
|
std::mem::forget(references);
|
|
|
|
refs
|
|
|
|
}
|
2020-01-06 14:07:35 -05:00
|
|
|
|
2022-06-07 05:25:10 -04:00
|
|
|
// TODO(nayeemrmn): Move to runtime and/or make `pub(crate)`.
|
2020-01-06 14:07:35 -05:00
|
|
|
pub fn script_origin<'a>(
|
2020-06-20 07:18:08 -04:00
|
|
|
s: &mut v8::HandleScope<'a>,
|
2020-01-06 14:07:35 -05:00
|
|
|
resource_name: v8::Local<'a, v8::String>,
|
|
|
|
) -> v8::ScriptOrigin<'a> {
|
2023-03-06 11:37:46 -05:00
|
|
|
let source_map_url = v8::String::empty(s);
|
2020-01-06 14:07:35 -05:00
|
|
|
v8::ScriptOrigin::new(
|
2021-01-18 20:18:42 -05:00
|
|
|
s,
|
2020-01-06 14:07:35 -05:00
|
|
|
resource_name.into(),
|
2021-01-18 20:18:42 -05:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
123,
|
2020-01-06 14:07:35 -05:00
|
|
|
source_map_url.into(),
|
2021-01-18 20:18:42 -05:00
|
|
|
true,
|
|
|
|
false,
|
|
|
|
false,
|
2020-01-06 14:07:35 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn module_origin<'a>(
|
2020-06-20 07:18:08 -04:00
|
|
|
s: &mut v8::HandleScope<'a>,
|
2020-01-06 14:07:35 -05:00
|
|
|
resource_name: v8::Local<'a, v8::String>,
|
|
|
|
) -> v8::ScriptOrigin<'a> {
|
2023-03-06 11:37:46 -05:00
|
|
|
let source_map_url = v8::String::empty(s);
|
2020-01-06 14:07:35 -05:00
|
|
|
v8::ScriptOrigin::new(
|
2021-01-18 20:18:42 -05:00
|
|
|
s,
|
2020-01-06 14:07:35 -05:00
|
|
|
resource_name.into(),
|
2021-01-18 20:18:42 -05:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
123,
|
2020-01-06 14:07:35 -05:00
|
|
|
source_map_url.into(),
|
2021-01-18 20:18:42 -05:00
|
|
|
true,
|
|
|
|
false,
|
|
|
|
true,
|
2020-01-06 14:07:35 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-03-16 09:27:16 -04:00
|
|
|
pub(crate) fn initialize_context<'s>(
|
2020-06-20 07:18:08 -04:00
|
|
|
scope: &mut v8::HandleScope<'s, ()>,
|
2022-04-08 04:32:48 -04:00
|
|
|
op_ctxs: &[OpCtx],
|
2022-10-28 15:31:01 -04:00
|
|
|
snapshot_options: SnapshotOptions,
|
2020-01-21 14:24:31 -05:00
|
|
|
) -> v8::Local<'s, v8::Context> {
|
|
|
|
let context = v8::Context::new(scope);
|
2020-01-06 14:07:35 -05:00
|
|
|
let global = context.global(scope);
|
|
|
|
|
2020-06-20 07:18:08 -04:00
|
|
|
let scope = &mut v8::ContextScope::new(scope, context);
|
2020-01-06 14:07:35 -05:00
|
|
|
|
2023-03-06 11:37:46 -05:00
|
|
|
let deno_str =
|
|
|
|
v8::String::new_external_onebyte_static(scope, b"Deno").unwrap();
|
|
|
|
let core_str =
|
|
|
|
v8::String::new_external_onebyte_static(scope, b"core").unwrap();
|
|
|
|
let ops_str = v8::String::new_external_onebyte_static(scope, b"ops").unwrap();
|
2023-02-21 19:55:31 -05:00
|
|
|
|
2023-03-18 18:30:04 -04:00
|
|
|
let ops_obj = if snapshot_options.loaded() {
|
|
|
|
// Snapshot already registered `Deno.core.ops` but
|
|
|
|
// extensions may provide ops that aren't part of the snapshot.
|
2022-05-24 16:21:32 -04:00
|
|
|
// Grab the Deno.core.ops object & init it
|
2023-02-21 19:55:31 -05:00
|
|
|
let deno_obj: v8::Local<v8::Object> = global
|
|
|
|
.get(scope, deno_str.into())
|
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
|
|
|
let core_obj: v8::Local<v8::Object> = deno_obj
|
|
|
|
.get(scope, core_str.into())
|
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
|
|
|
let ops_obj: v8::Local<v8::Object> = core_obj
|
|
|
|
.get(scope, ops_str.into())
|
|
|
|
.expect("Deno.core.ops to exist")
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
2023-03-18 18:30:04 -04:00
|
|
|
ops_obj
|
|
|
|
} else {
|
|
|
|
// globalThis.Deno = { core: { } };
|
|
|
|
let deno_obj = v8::Object::new(scope);
|
|
|
|
global.set(scope, deno_str.into(), deno_obj.into());
|
|
|
|
|
|
|
|
let core_obj = v8::Object::new(scope);
|
|
|
|
deno_obj.set(scope, core_str.into(), core_obj.into());
|
|
|
|
|
|
|
|
// Bind functions to Deno.core.*
|
|
|
|
set_func(scope, core_obj, "callConsole", call_console);
|
|
|
|
|
|
|
|
// Bind v8 console object to Deno.core.console
|
|
|
|
let extra_binding_obj = context.get_extras_binding_object(scope);
|
|
|
|
let console_str =
|
|
|
|
v8::String::new_external_onebyte_static(scope, b"console").unwrap();
|
|
|
|
let console_obj = extra_binding_obj.get(scope, console_str.into()).unwrap();
|
|
|
|
core_obj.set(scope, console_str.into(), console_obj);
|
|
|
|
|
|
|
|
// Bind functions to Deno.core.ops.*
|
|
|
|
let ops_obj = v8::Object::new(scope);
|
|
|
|
core_obj.set(scope, ops_str.into(), ops_obj.into());
|
|
|
|
ops_obj
|
|
|
|
};
|
2023-03-18 10:59:51 -04:00
|
|
|
|
2023-03-18 18:30:04 -04:00
|
|
|
if matches!(snapshot_options, SnapshotOptions::Load) {
|
|
|
|
// Only register ops that have `force_registration` flag set to true,
|
2023-04-21 09:22:45 -04:00
|
|
|
// the remaining ones should already be in the snapshot. Ignore ops that
|
|
|
|
// are disabled.
|
|
|
|
for op_ctx in op_ctxs {
|
|
|
|
if op_ctx.decl.enabled {
|
|
|
|
if op_ctx.decl.force_registration {
|
|
|
|
add_op_to_deno_core_ops(scope, ops_obj, op_ctx);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
delete_op_from_deno_core_ops(scope, ops_obj, op_ctx)
|
|
|
|
}
|
2023-03-18 18:30:04 -04:00
|
|
|
}
|
|
|
|
} else if matches!(snapshot_options, SnapshotOptions::CreateFromExisting) {
|
2023-04-21 09:22:45 -04:00
|
|
|
// Register all enabled ops, probing for which ones are already registered.
|
2023-03-18 18:30:04 -04:00
|
|
|
for op_ctx in op_ctxs {
|
|
|
|
let key = v8::String::new_external_onebyte_static(
|
|
|
|
scope,
|
|
|
|
op_ctx.decl.name.as_bytes(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2023-04-21 09:22:45 -04:00
|
|
|
|
|
|
|
if op_ctx.decl.enabled {
|
|
|
|
if ops_obj.get(scope, key.into()).is_some() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
add_op_to_deno_core_ops(scope, ops_obj, op_ctx);
|
|
|
|
} else {
|
|
|
|
delete_op_from_deno_core_ops(scope, ops_obj, op_ctx)
|
2023-03-18 18:30:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-04-21 09:22:45 -04:00
|
|
|
// In other cases register all ops enabled unconditionally.
|
2023-03-18 18:30:04 -04:00
|
|
|
for op_ctx in op_ctxs {
|
2023-04-21 09:22:45 -04:00
|
|
|
if op_ctx.decl.enabled {
|
|
|
|
add_op_to_deno_core_ops(scope, ops_obj, op_ctx);
|
|
|
|
}
|
2023-03-18 18:30:04 -04:00
|
|
|
}
|
2022-04-08 04:32:48 -04:00
|
|
|
}
|
2023-03-16 09:27:16 -04:00
|
|
|
|
|
|
|
context
|
2022-04-08 04:32:48 -04:00
|
|
|
}
|
|
|
|
|
2023-03-16 09:27:16 -04:00
|
|
|
fn set_func(
|
2021-02-15 06:53:33 -05:00
|
|
|
scope: &mut v8::HandleScope<'_>,
|
|
|
|
obj: v8::Local<v8::Object>,
|
|
|
|
name: &'static str,
|
|
|
|
callback: impl v8::MapFnTo<v8::FunctionCallback>,
|
|
|
|
) {
|
2023-03-06 11:37:46 -05:00
|
|
|
let key =
|
|
|
|
v8::String::new_external_onebyte_static(scope, name.as_bytes()).unwrap();
|
2022-03-14 13:44:15 -04:00
|
|
|
let val = v8::Function::new(scope, callback).unwrap();
|
|
|
|
val.set_name(key);
|
|
|
|
obj.set(scope, key.into(), val.into());
|
|
|
|
}
|
|
|
|
|
2023-04-21 09:22:45 -04:00
|
|
|
fn delete_op_from_deno_core_ops(
|
|
|
|
scope: &mut v8::HandleScope<'_>,
|
|
|
|
obj: v8::Local<v8::Object>,
|
|
|
|
op_ctx: &OpCtx,
|
|
|
|
) {
|
|
|
|
let key =
|
|
|
|
v8::String::new_external_onebyte_static(scope, op_ctx.decl.name.as_bytes())
|
|
|
|
.unwrap();
|
|
|
|
obj.delete(scope, key.into());
|
|
|
|
}
|
|
|
|
|
2023-03-16 09:27:16 -04:00
|
|
|
fn add_op_to_deno_core_ops(
|
2022-03-14 13:44:15 -04:00
|
|
|
scope: &mut v8::HandleScope<'_>,
|
|
|
|
obj: v8::Local<v8::Object>,
|
2023-03-16 09:27:16 -04:00
|
|
|
op_ctx: &OpCtx,
|
2022-03-14 13:44:15 -04:00
|
|
|
) {
|
2023-03-16 09:27:16 -04:00
|
|
|
let op_ctx_ptr = op_ctx as *const OpCtx as *const c_void;
|
2023-03-06 11:37:46 -05:00
|
|
|
let key =
|
2023-03-16 09:27:16 -04:00
|
|
|
v8::String::new_external_onebyte_static(scope, op_ctx.decl.name.as_bytes())
|
|
|
|
.unwrap();
|
|
|
|
let external = v8::External::new(scope, op_ctx_ptr as *mut c_void);
|
|
|
|
let builder = v8::FunctionTemplate::builder_raw(op_ctx.decl.v8_fn_ptr)
|
|
|
|
.data(external.into());
|
|
|
|
|
2023-03-18 18:30:04 -04:00
|
|
|
let templ = if let Some(fast_function) = &op_ctx.decl.fast_fn {
|
|
|
|
builder.build_fast(
|
|
|
|
scope,
|
2023-03-31 08:42:14 -04:00
|
|
|
fast_function,
|
2023-03-18 18:30:04 -04:00
|
|
|
Some(op_ctx.fast_fn_c_info.unwrap().as_ptr()),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
)
|
2022-08-21 08:07:53 -04:00
|
|
|
} else {
|
|
|
|
builder.build(scope)
|
|
|
|
};
|
|
|
|
let val = templ.get_function(scope).unwrap();
|
2021-10-05 16:55:51 -04:00
|
|
|
val.set_name(key);
|
2021-02-15 06:53:33 -05:00
|
|
|
obj.set(scope, key.into(), val.into());
|
|
|
|
}
|
|
|
|
|
2022-08-15 08:12:11 -04:00
|
|
|
pub extern "C" fn wasm_async_resolve_promise_callback(
|
|
|
|
_isolate: *mut v8::Isolate,
|
|
|
|
context: v8::Local<v8::Context>,
|
|
|
|
resolver: v8::Local<v8::PromiseResolver>,
|
|
|
|
compilation_result: v8::Local<v8::Value>,
|
|
|
|
success: v8::WasmAsyncSuccess,
|
|
|
|
) {
|
|
|
|
// SAFETY: `CallbackScope` can be safely constructed from `Local<Context>`
|
|
|
|
let scope = &mut unsafe { v8::CallbackScope::new(context) };
|
|
|
|
if success == v8::WasmAsyncSuccess::Success {
|
|
|
|
resolver.resolve(scope, compilation_result).unwrap();
|
|
|
|
} else {
|
|
|
|
resolver.reject(scope, compilation_result).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-14 19:53:13 -04:00
|
|
|
pub fn host_import_module_dynamically_callback<'s>(
|
|
|
|
scope: &mut v8::HandleScope<'s>,
|
|
|
|
_host_defined_options: v8::Local<'s, v8::Data>,
|
|
|
|
resource_name: v8::Local<'s, v8::Value>,
|
|
|
|
specifier: v8::Local<'s, v8::String>,
|
|
|
|
import_assertions: v8::Local<'s, v8::FixedArray>,
|
|
|
|
) -> Option<v8::Local<'s, v8::Promise>> {
|
2020-01-06 10:24:44 -05:00
|
|
|
// NOTE(bartlomieju): will crash for non-UTF-8 specifier
|
|
|
|
let specifier_str = specifier
|
|
|
|
.to_string(scope)
|
|
|
|
.unwrap()
|
|
|
|
.to_rust_string_lossy(scope);
|
2022-03-11 09:29:01 -05:00
|
|
|
let referrer_name_str = resource_name
|
2020-01-06 10:24:44 -05:00
|
|
|
.to_string(scope)
|
|
|
|
.unwrap()
|
|
|
|
.to_rust_string_lossy(scope);
|
|
|
|
|
2023-03-08 06:44:54 -05:00
|
|
|
let is_ext_module = specifier_str.starts_with("ext:");
|
2020-06-20 07:18:08 -04:00
|
|
|
let resolver = v8::PromiseResolver::new(scope).unwrap();
|
2020-01-06 10:24:44 -05:00
|
|
|
let promise = resolver.get_promise(scope);
|
|
|
|
|
2023-03-08 06:44:54 -05:00
|
|
|
if !is_ext_module {
|
2023-02-07 14:22:46 -05:00
|
|
|
let assertions = parse_import_assertions(
|
|
|
|
scope,
|
|
|
|
import_assertions,
|
|
|
|
ImportAssertionsKind::DynamicImport,
|
|
|
|
);
|
|
|
|
|
|
|
|
{
|
|
|
|
let tc_scope = &mut v8::TryCatch::new(scope);
|
|
|
|
validate_import_assertions(tc_scope, &assertions);
|
|
|
|
if tc_scope.has_caught() {
|
|
|
|
let e = tc_scope.exception().unwrap();
|
|
|
|
resolver.reject(tc_scope, e);
|
|
|
|
}
|
2021-12-15 13:22:36 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
let asserted_module_type =
|
|
|
|
get_asserted_module_type_from_assertions(&assertions);
|
2021-12-15 13:22:36 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
let resolver_handle = v8::Global::new(scope, resolver);
|
|
|
|
{
|
|
|
|
let state_rc = JsRuntime::state(scope);
|
|
|
|
let module_map_rc = JsRuntime::module_map(scope);
|
2021-05-19 14:53:43 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
debug!(
|
|
|
|
"dyn_import specifier {} referrer {} ",
|
|
|
|
specifier_str, referrer_name_str
|
|
|
|
);
|
|
|
|
ModuleMap::load_dynamic_import(
|
|
|
|
module_map_rc,
|
|
|
|
&specifier_str,
|
|
|
|
&referrer_name_str,
|
|
|
|
asserted_module_type,
|
|
|
|
resolver_handle,
|
|
|
|
);
|
|
|
|
state_rc.borrow_mut().notify_new_dynamic_import();
|
|
|
|
}
|
2020-05-29 17:41:39 -04:00
|
|
|
}
|
2021-02-21 10:22:06 -05:00
|
|
|
// Map errors from module resolution (not JS errors from module execution) to
|
|
|
|
// ones rethrown from this scope, so they include the call stack of the
|
|
|
|
// dynamic import site. Error objects without any stack frames are assumed to
|
|
|
|
// be module resolution errors, other exception values are left as they are.
|
2022-09-06 08:35:04 -04:00
|
|
|
let builder = v8::FunctionBuilder::new(catch_dynamic_import_promise_error);
|
|
|
|
|
|
|
|
let map_err =
|
|
|
|
v8::FunctionBuilder::<v8::Function>::build(builder, scope).unwrap();
|
|
|
|
|
2021-02-21 10:22:06 -05:00
|
|
|
let promise = promise.catch(scope, map_err).unwrap();
|
|
|
|
|
2023-03-08 06:44:54 -05:00
|
|
|
if is_ext_module {
|
2023-03-06 11:37:46 -05:00
|
|
|
let message = v8::String::new_external_onebyte_static(
|
|
|
|
scope,
|
2023-03-08 06:44:54 -05:00
|
|
|
b"Cannot load extension module from external code",
|
2023-03-06 11:37:46 -05:00
|
|
|
)
|
|
|
|
.unwrap();
|
2023-02-07 14:22:46 -05:00
|
|
|
let exception = v8::Exception::type_error(scope, message);
|
|
|
|
resolver.reject(scope, exception);
|
|
|
|
}
|
|
|
|
|
2022-10-14 19:53:13 -04:00
|
|
|
Some(promise)
|
2020-01-06 10:24:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub extern "C" fn host_initialize_import_meta_object_callback(
|
|
|
|
context: v8::Local<v8::Context>,
|
|
|
|
module: v8::Local<v8::Module>,
|
|
|
|
meta: v8::Local<v8::Object>,
|
|
|
|
) {
|
2022-06-25 18:13:24 -04:00
|
|
|
// SAFETY: `CallbackScope` can be safely constructed from `Local<Context>`
|
2020-06-20 07:18:08 -04:00
|
|
|
let scope = &mut unsafe { v8::CallbackScope::new(context) };
|
2021-05-19 14:53:43 -04:00
|
|
|
let module_map_rc = JsRuntime::module_map(scope);
|
|
|
|
let module_map = module_map_rc.borrow();
|
2020-01-06 10:24:44 -05:00
|
|
|
|
2020-11-21 10:23:35 -05:00
|
|
|
let module_global = v8::Global::new(scope, module);
|
2021-05-19 14:53:43 -04:00
|
|
|
let info = module_map
|
2020-11-21 10:23:35 -05:00
|
|
|
.get_info(&module_global)
|
|
|
|
.expect("Module not found");
|
2020-01-06 10:24:44 -05:00
|
|
|
|
2023-03-06 11:37:46 -05:00
|
|
|
let url_key = v8::String::new_external_onebyte_static(scope, b"url").unwrap();
|
2023-04-04 08:46:31 -04:00
|
|
|
let url_val = info.name.v8(scope);
|
2020-06-20 07:18:08 -04:00
|
|
|
meta.create_data_property(scope, url_key.into(), url_val.into());
|
|
|
|
|
2023-03-06 11:37:46 -05:00
|
|
|
let main_key =
|
|
|
|
v8::String::new_external_onebyte_static(scope, b"main").unwrap();
|
2020-06-20 07:18:08 -04:00
|
|
|
let main_val = v8::Boolean::new(scope, info.main);
|
|
|
|
meta.create_data_property(scope, main_key.into(), main_val.into());
|
2022-07-18 14:05:26 -04:00
|
|
|
|
|
|
|
let builder =
|
|
|
|
v8::FunctionBuilder::new(import_meta_resolve).data(url_val.into());
|
|
|
|
let val = v8::FunctionBuilder::<v8::Function>::build(builder, scope).unwrap();
|
2023-03-06 11:37:46 -05:00
|
|
|
let resolve_key =
|
|
|
|
v8::String::new_external_onebyte_static(scope, b"resolve").unwrap();
|
2022-07-18 14:05:26 -04:00
|
|
|
meta.set(scope, resolve_key.into(), val.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn import_meta_resolve(
|
|
|
|
scope: &mut v8::HandleScope,
|
|
|
|
args: v8::FunctionCallbackArguments,
|
|
|
|
mut rv: v8::ReturnValue,
|
|
|
|
) {
|
|
|
|
if args.length() > 1 {
|
|
|
|
return throw_type_error(scope, "Invalid arguments");
|
|
|
|
}
|
|
|
|
|
|
|
|
let maybe_arg_str = args.get(0).to_string(scope);
|
|
|
|
if maybe_arg_str.is_none() {
|
|
|
|
return throw_type_error(scope, "Invalid arguments");
|
|
|
|
}
|
|
|
|
let specifier = maybe_arg_str.unwrap();
|
|
|
|
let referrer = {
|
2022-10-20 15:01:49 -04:00
|
|
|
let url_prop = args.data();
|
2022-07-18 14:05:26 -04:00
|
|
|
url_prop.to_rust_string_lossy(scope)
|
|
|
|
};
|
|
|
|
let module_map_rc = JsRuntime::module_map(scope);
|
2023-02-08 12:13:33 -05:00
|
|
|
let (loader, snapshot_loaded_and_not_snapshotting) = {
|
2022-07-18 14:05:26 -04:00
|
|
|
let module_map = module_map_rc.borrow();
|
2023-02-08 12:13:33 -05:00
|
|
|
(
|
|
|
|
module_map.loader.clone(),
|
|
|
|
module_map.snapshot_loaded_and_not_snapshotting,
|
|
|
|
)
|
2022-07-18 14:05:26 -04:00
|
|
|
};
|
2022-11-11 12:20:13 -05:00
|
|
|
let specifier_str = specifier.to_rust_string_lossy(scope);
|
|
|
|
|
|
|
|
if specifier_str.starts_with("npm:") {
|
|
|
|
throw_type_error(scope, "\"npm:\" specifiers are currently not supported in import.meta.resolve()");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-08 12:13:33 -05:00
|
|
|
match resolve_helper(
|
|
|
|
snapshot_loaded_and_not_snapshotting,
|
|
|
|
loader,
|
|
|
|
&specifier_str,
|
|
|
|
&referrer,
|
|
|
|
ResolutionKind::DynamicImport,
|
|
|
|
) {
|
2022-07-18 14:05:26 -04:00
|
|
|
Ok(resolved) => {
|
|
|
|
let resolved_val = serde_v8::to_v8(scope, resolved.as_str()).unwrap();
|
|
|
|
rv.set(resolved_val);
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
throw_type_error(scope, &err.to_string());
|
|
|
|
}
|
|
|
|
};
|
2020-01-06 10:24:44 -05:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:35:04 -04:00
|
|
|
fn empty_fn(
|
|
|
|
_scope: &mut v8::HandleScope,
|
|
|
|
_args: v8::FunctionCallbackArguments,
|
|
|
|
_rv: v8::ReturnValue,
|
|
|
|
) {
|
|
|
|
//Do Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
//It creates a reference to an empty function which can be mantained after the snapshots
|
|
|
|
pub fn create_empty_fn<'s>(
|
|
|
|
scope: &mut v8::HandleScope<'s>,
|
|
|
|
) -> Option<v8::Local<'s, v8::Function>> {
|
|
|
|
let empty_fn = v8::FunctionTemplate::new(scope, empty_fn);
|
|
|
|
empty_fn.get_function(scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn catch_dynamic_import_promise_error(
|
|
|
|
scope: &mut v8::HandleScope,
|
|
|
|
args: v8::FunctionCallbackArguments,
|
|
|
|
_rv: v8::ReturnValue,
|
|
|
|
) {
|
|
|
|
let arg = args.get(0);
|
|
|
|
if is_instance_of_error(scope, arg) {
|
|
|
|
let e: crate::error::NativeJsError = serde_v8::from_v8(scope, arg).unwrap();
|
|
|
|
let name = e.name.unwrap_or_else(|| "Error".to_string());
|
2023-04-12 07:45:36 -04:00
|
|
|
let msg = v8::Exception::create_message(scope, arg);
|
|
|
|
if msg.get_stack_trace(scope).unwrap().get_frame_count() == 0 {
|
2022-09-06 08:35:04 -04:00
|
|
|
let arg: v8::Local<v8::Object> = arg.try_into().unwrap();
|
2023-03-06 11:37:46 -05:00
|
|
|
let message_key =
|
|
|
|
v8::String::new_external_onebyte_static(scope, b"message").unwrap();
|
2022-09-06 08:35:04 -04:00
|
|
|
let message = arg.get(scope, message_key.into()).unwrap();
|
2023-04-12 07:45:36 -04:00
|
|
|
let mut message: v8::Local<v8::String> = message.try_into().unwrap();
|
|
|
|
if let Some(stack_frame) = JsStackFrame::from_v8_message(scope, msg) {
|
|
|
|
if let Some(location) = stack_frame.maybe_format_location() {
|
|
|
|
let str =
|
|
|
|
format!("{} at {location}", message.to_rust_string_lossy(scope));
|
|
|
|
message = v8::String::new(scope, &str).unwrap();
|
2022-09-06 08:35:04 -04:00
|
|
|
}
|
2023-04-12 07:45:36 -04:00
|
|
|
}
|
|
|
|
let exception = match name.as_str() {
|
|
|
|
"RangeError" => v8::Exception::range_error(scope, message),
|
|
|
|
"TypeError" => v8::Exception::type_error(scope, message),
|
|
|
|
"SyntaxError" => v8::Exception::syntax_error(scope, message),
|
|
|
|
"ReferenceError" => v8::Exception::reference_error(scope, message),
|
|
|
|
_ => v8::Exception::error(scope, message),
|
2022-09-06 08:35:04 -04:00
|
|
|
};
|
2023-03-06 11:37:46 -05:00
|
|
|
let code_key =
|
|
|
|
v8::String::new_external_onebyte_static(scope, b"code").unwrap();
|
|
|
|
let code_value =
|
|
|
|
v8::String::new_external_onebyte_static(scope, b"ERR_MODULE_NOT_FOUND")
|
|
|
|
.unwrap();
|
2022-09-06 08:35:04 -04:00
|
|
|
let exception_obj = exception.to_object(scope).unwrap();
|
|
|
|
exception_obj.set(scope, code_key.into(), code_value.into());
|
|
|
|
scope.throw_exception(exception);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
scope.throw_exception(arg);
|
|
|
|
}
|
|
|
|
|
2020-01-16 17:25:57 -05:00
|
|
|
pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) {
|
2021-11-27 18:46:12 -05:00
|
|
|
use v8::PromiseRejectEvent::*;
|
|
|
|
|
2022-06-25 18:13:24 -04:00
|
|
|
// SAFETY: `CallbackScope` can be safely constructed from `&PromiseRejectMessage`
|
2020-06-20 07:18:08 -04:00
|
|
|
let scope = &mut unsafe { v8::CallbackScope::new(&message) };
|
2020-01-16 17:25:57 -05:00
|
|
|
|
2023-01-14 22:39:00 -05:00
|
|
|
let context_state_rc = JsRealm::state_from_scope(scope);
|
2023-01-16 11:36:42 -05:00
|
|
|
let mut context_state = context_state_rc.borrow_mut();
|
|
|
|
|
|
|
|
if let Some(js_promise_reject_cb) = context_state.js_promise_reject_cb.clone()
|
|
|
|
{
|
|
|
|
drop(context_state);
|
2020-01-16 17:25:57 -05:00
|
|
|
|
2021-11-27 18:46:12 -05:00
|
|
|
let tc_scope = &mut v8::TryCatch::new(scope);
|
|
|
|
let undefined: v8::Local<v8::Value> = v8::undefined(tc_scope).into();
|
|
|
|
let type_ = v8::Integer::new(tc_scope, message.get_event() as i32);
|
|
|
|
let promise = message.get_promise();
|
|
|
|
|
|
|
|
let reason = match message.get_event() {
|
|
|
|
PromiseRejectWithNoHandler
|
|
|
|
| PromiseRejectAfterResolved
|
|
|
|
| PromiseResolveAfterResolved => message.get_value().unwrap_or(undefined),
|
|
|
|
PromiseHandlerAddedAfterReject => undefined,
|
|
|
|
};
|
2020-01-06 10:24:44 -05:00
|
|
|
|
2022-07-20 14:28:19 -04:00
|
|
|
let promise_global = v8::Global::new(tc_scope, promise);
|
2021-11-27 18:46:12 -05:00
|
|
|
let args = &[type_.into(), promise.into(), reason];
|
2022-07-20 14:28:19 -04:00
|
|
|
let maybe_has_unhandled_rejection_handler = js_promise_reject_cb
|
2021-11-27 18:46:12 -05:00
|
|
|
.open(tc_scope)
|
|
|
|
.call(tc_scope, undefined, args);
|
|
|
|
|
2022-07-20 14:28:19 -04:00
|
|
|
let has_unhandled_rejection_handler =
|
|
|
|
if let Some(value) = maybe_has_unhandled_rejection_handler {
|
|
|
|
value.is_true()
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
|
|
|
if has_unhandled_rejection_handler {
|
2023-01-14 22:39:00 -05:00
|
|
|
let state_rc = JsRuntime::state(tc_scope);
|
2022-07-20 14:28:19 -04:00
|
|
|
let mut state = state_rc.borrow_mut();
|
|
|
|
if let Some(pending_mod_evaluate) = state.pending_mod_evaluate.as_mut() {
|
|
|
|
if !pending_mod_evaluate.has_evaluated {
|
|
|
|
pending_mod_evaluate
|
|
|
|
.handled_promise_rejections
|
2022-07-24 19:10:56 -04:00
|
|
|
.push(promise_global);
|
2022-07-20 14:28:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-27 18:46:12 -05:00
|
|
|
} else {
|
|
|
|
let promise = message.get_promise();
|
|
|
|
let promise_global = v8::Global::new(scope, promise);
|
|
|
|
match message.get_event() {
|
|
|
|
PromiseRejectWithNoHandler => {
|
|
|
|
let error = message.get_value().unwrap();
|
|
|
|
let error_global = v8::Global::new(scope, error);
|
2023-01-16 11:36:42 -05:00
|
|
|
context_state
|
2023-01-16 10:19:04 -05:00
|
|
|
.pending_promise_rejections
|
2021-11-27 18:46:12 -05:00
|
|
|
.insert(promise_global, error_global);
|
|
|
|
}
|
|
|
|
PromiseHandlerAddedAfterReject => {
|
2023-01-16 11:36:42 -05:00
|
|
|
context_state
|
|
|
|
.pending_promise_rejections
|
|
|
|
.remove(&promise_global);
|
2021-11-27 18:46:12 -05:00
|
|
|
}
|
|
|
|
PromiseRejectAfterResolved => {}
|
|
|
|
PromiseResolveAfterResolved => {
|
|
|
|
// Should not warn. See #1272
|
|
|
|
}
|
2020-01-06 10:24:44 -05:00
|
|
|
}
|
2022-10-20 22:54:22 -04:00
|
|
|
}
|
2020-01-06 10:24:44 -05:00
|
|
|
}
|
|
|
|
|
2021-06-26 20:27:50 -04:00
|
|
|
/// This binding should be used if there's a custom console implementation
|
|
|
|
/// available. Using it will make sure that proper stack frames are displayed
|
|
|
|
/// in the inspector console.
|
|
|
|
///
|
|
|
|
/// Each method on console object should be bound to this function, eg:
|
|
|
|
/// ```ignore
|
|
|
|
/// function wrapConsole(consoleFromDeno, consoleFromV8) {
|
|
|
|
/// const callConsole = core.callConsole;
|
|
|
|
///
|
|
|
|
/// for (const key of Object.keys(consoleFromV8)) {
|
|
|
|
/// if (consoleFromDeno.hasOwnProperty(key)) {
|
|
|
|
/// consoleFromDeno[key] = callConsole.bind(
|
|
|
|
/// consoleFromDeno,
|
|
|
|
/// consoleFromV8[key],
|
|
|
|
/// consoleFromDeno[key],
|
|
|
|
/// );
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Inspired by:
|
|
|
|
/// https://github.com/nodejs/node/blob/1317252dfe8824fd9cfee125d2aaa94004db2f3b/src/inspector_js_api.cc#L194-L222
|
|
|
|
fn call_console(
|
|
|
|
scope: &mut v8::HandleScope,
|
|
|
|
args: v8::FunctionCallbackArguments,
|
|
|
|
_rv: v8::ReturnValue,
|
|
|
|
) {
|
2021-12-03 05:55:14 -05:00
|
|
|
if args.length() < 2
|
|
|
|
|| !args.get(0).is_function()
|
|
|
|
|| !args.get(1).is_function()
|
|
|
|
{
|
|
|
|
return throw_type_error(scope, "Invalid arguments");
|
|
|
|
}
|
2021-06-26 20:27:50 -04:00
|
|
|
|
|
|
|
let mut call_args = vec![];
|
|
|
|
for i in 2..args.length() {
|
|
|
|
call_args.push(args.get(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
let receiver = args.this();
|
|
|
|
let inspector_console_method =
|
|
|
|
v8::Local::<v8::Function>::try_from(args.get(0)).unwrap();
|
|
|
|
let deno_console_method =
|
|
|
|
v8::Local::<v8::Function>::try_from(args.get(1)).unwrap();
|
|
|
|
|
|
|
|
inspector_console_method.call(scope, receiver.into(), &call_args);
|
|
|
|
deno_console_method.call(scope, receiver.into(), &call_args);
|
|
|
|
}
|
|
|
|
|
2021-05-19 14:53:43 -04:00
|
|
|
/// Called by V8 during `JsRuntime::instantiate_module`.
|
|
|
|
///
|
|
|
|
/// This function borrows `ModuleMap` from the isolate slot,
|
|
|
|
/// so it is crucial to ensure there are no existing borrows
|
|
|
|
/// of `ModuleMap` when `JsRuntime::instantiate_module` is called.
|
2020-01-16 17:25:57 -05:00
|
|
|
pub fn module_resolve_callback<'s>(
|
|
|
|
context: v8::Local<'s, v8::Context>,
|
|
|
|
specifier: v8::Local<'s, v8::String>,
|
2021-12-15 13:22:36 -05:00
|
|
|
import_assertions: v8::Local<'s, v8::FixedArray>,
|
2020-01-16 17:25:57 -05:00
|
|
|
referrer: v8::Local<'s, v8::Module>,
|
|
|
|
) -> Option<v8::Local<'s, v8::Module>> {
|
2022-06-25 18:13:24 -04:00
|
|
|
// SAFETY: `CallbackScope` can be safely constructed from `Local<Context>`
|
2020-06-20 07:18:08 -04:00
|
|
|
let scope = &mut unsafe { v8::CallbackScope::new(context) };
|
2020-01-06 10:24:44 -05:00
|
|
|
|
2021-05-19 14:53:43 -04:00
|
|
|
let module_map_rc = JsRuntime::module_map(scope);
|
|
|
|
let module_map = module_map_rc.borrow();
|
2020-01-06 10:24:44 -05:00
|
|
|
|
2020-11-21 10:23:35 -05:00
|
|
|
let referrer_global = v8::Global::new(scope, referrer);
|
2021-05-19 14:53:43 -04:00
|
|
|
|
|
|
|
let referrer_info = module_map
|
2020-11-21 10:23:35 -05:00
|
|
|
.get_info(&referrer_global)
|
|
|
|
.expect("ModuleInfo not found");
|
2023-04-04 08:46:31 -04:00
|
|
|
let referrer_name = referrer_info.name.as_str();
|
2020-01-06 10:24:44 -05:00
|
|
|
|
|
|
|
let specifier_str = specifier.to_rust_string_lossy(scope);
|
|
|
|
|
2021-12-15 13:22:36 -05:00
|
|
|
let assertions = parse_import_assertions(
|
|
|
|
scope,
|
|
|
|
import_assertions,
|
|
|
|
ImportAssertionsKind::StaticImport,
|
|
|
|
);
|
|
|
|
let maybe_module = module_map.resolve_callback(
|
|
|
|
scope,
|
|
|
|
&specifier_str,
|
2023-04-04 08:46:31 -04:00
|
|
|
referrer_name,
|
2021-12-15 13:22:36 -05:00
|
|
|
assertions,
|
|
|
|
);
|
2021-05-19 14:53:43 -04:00
|
|
|
if let Some(module) = maybe_module {
|
|
|
|
return Some(module);
|
2020-01-06 10:24:44 -05:00
|
|
|
}
|
|
|
|
|
2020-11-21 10:23:35 -05:00
|
|
|
let msg = format!(
|
2023-01-27 10:43:16 -05:00
|
|
|
r#"Cannot resolve module "{specifier_str}" from "{referrer_name}""#
|
2020-11-21 10:23:35 -05:00
|
|
|
);
|
|
|
|
throw_type_error(scope, msg);
|
2020-01-16 17:25:57 -05:00
|
|
|
None
|
2020-01-06 10:24:44 -05:00
|
|
|
}
|
2020-03-30 19:01:19 -04:00
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
pub fn throw_type_error(scope: &mut v8::HandleScope, message: impl AsRef<str>) {
|
2020-07-18 16:32:11 -04:00
|
|
|
let message = v8::String::new(scope, message.as_ref()).unwrap();
|
|
|
|
let exception = v8::Exception::type_error(scope, message);
|
|
|
|
scope.throw_exception(exception);
|
|
|
|
}
|