2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-01-06 10:24:44 -05:00
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
use crate::error::AnyError;
|
2021-06-28 21:03:02 -04:00
|
|
|
use crate::modules::ModuleMap;
|
2021-06-06 12:32:06 -04:00
|
|
|
use crate::resolve_url_or_path;
|
2020-09-06 15:44:29 -04:00
|
|
|
use crate::JsRuntime;
|
2020-09-05 20:34:02 -04:00
|
|
|
use crate::Op;
|
|
|
|
use crate::OpId;
|
2021-03-31 10:37:38 -04:00
|
|
|
use crate::OpPayload;
|
2021-10-04 06:34:53 -04:00
|
|
|
use crate::OpResult;
|
2020-09-10 09:57:45 -04:00
|
|
|
use crate::OpTable;
|
2021-03-31 10:37:38 -04:00
|
|
|
use crate::PromiseId;
|
2020-05-28 13:36:43 -04:00
|
|
|
use crate::ZeroCopyBuf;
|
2021-05-19 14:53:43 -04:00
|
|
|
use log::debug;
|
2020-01-06 10:24:44 -05:00
|
|
|
use rusty_v8 as v8;
|
2021-06-26 05:17:05 -04:00
|
|
|
use serde::Deserialize;
|
2021-03-26 12:34:25 -04:00
|
|
|
use serde::Serialize;
|
|
|
|
use serde_v8::to_v8;
|
2021-07-03 17:33:36 -04:00
|
|
|
use std::cell::RefCell;
|
2020-01-06 10:24:44 -05:00
|
|
|
use std::convert::TryFrom;
|
2021-02-21 10:22:06 -05:00
|
|
|
use std::convert::TryInto;
|
2020-01-06 10:24:44 -05:00
|
|
|
use std::option::Option;
|
2020-03-19 10:42:07 -04:00
|
|
|
use url::Url;
|
2021-07-06 13:42:52 -04:00
|
|
|
use v8::HandleScope;
|
|
|
|
use v8::Local;
|
2020-09-05 20:34:02 -04:00
|
|
|
use v8::MapFnTo;
|
2021-07-06 13:42:52 -04:00
|
|
|
use v8::SharedArrayBuffer;
|
2021-08-09 04:39:00 -04:00
|
|
|
use v8::ValueDeserializerHelper;
|
|
|
|
use v8::ValueSerializerHelper;
|
2020-01-06 10:24:44 -05:00
|
|
|
|
2021-03-26 12:34:25 -04:00
|
|
|
lazy_static::lazy_static! {
|
2020-01-06 14:07:35 -05:00
|
|
|
pub static ref EXTERNAL_REFERENCES: v8::ExternalReferences =
|
|
|
|
v8::ExternalReferences::new(&[
|
|
|
|
v8::ExternalReference {
|
2021-10-04 06:34:53 -04:00
|
|
|
function: opcall_async.map_fn_to()
|
|
|
|
},
|
|
|
|
v8::ExternalReference {
|
|
|
|
function: opcall_sync.map_fn_to()
|
2020-01-16 17:25:57 -05:00
|
|
|
},
|
2020-03-19 10:45:28 -04:00
|
|
|
v8::ExternalReference {
|
|
|
|
function: set_macrotask_callback.map_fn_to()
|
|
|
|
},
|
2020-01-16 17:25:57 -05:00
|
|
|
v8::ExternalReference {
|
|
|
|
function: eval_context.map_fn_to()
|
|
|
|
},
|
2020-01-06 14:07:35 -05:00
|
|
|
v8::ExternalReference {
|
2020-01-16 17:25:57 -05:00
|
|
|
function: queue_microtask.map_fn_to()
|
2020-01-06 14:07:35 -05:00
|
|
|
},
|
2021-06-26 05:17:05 -04:00
|
|
|
v8::ExternalReference {
|
|
|
|
function: create_host_object.map_fn_to()
|
|
|
|
},
|
2020-03-15 10:31:55 -04:00
|
|
|
v8::ExternalReference {
|
|
|
|
function: encode.map_fn_to()
|
|
|
|
},
|
|
|
|
v8::ExternalReference {
|
|
|
|
function: decode.map_fn_to()
|
|
|
|
},
|
2021-02-16 08:20:21 -05:00
|
|
|
v8::ExternalReference {
|
|
|
|
function: serialize.map_fn_to()
|
|
|
|
},
|
|
|
|
v8::ExternalReference {
|
|
|
|
function: deserialize.map_fn_to()
|
|
|
|
},
|
2020-03-30 19:01:19 -04:00
|
|
|
v8::ExternalReference {
|
2020-09-05 20:34:02 -04:00
|
|
|
function: get_promise_details.map_fn_to()
|
2020-09-08 10:06:26 -04:00
|
|
|
},
|
|
|
|
v8::ExternalReference {
|
|
|
|
function: get_proxy_details.map_fn_to()
|
|
|
|
},
|
2021-10-01 14:25:33 -04:00
|
|
|
v8::ExternalReference {
|
|
|
|
function: is_proxy.map_fn_to()
|
|
|
|
},
|
2021-03-23 10:33:06 -04:00
|
|
|
v8::ExternalReference {
|
2021-04-12 04:47:44 -04:00
|
|
|
function: memory_usage.map_fn_to(),
|
2021-03-23 10:33:06 -04:00
|
|
|
},
|
2021-06-26 20:27:50 -04:00
|
|
|
v8::ExternalReference {
|
|
|
|
function: call_console.map_fn_to(),
|
|
|
|
},
|
2021-07-03 17:33:36 -04:00
|
|
|
v8::ExternalReference {
|
|
|
|
function: set_wasm_streaming_callback.map_fn_to()
|
|
|
|
}
|
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> {
|
2020-05-11 17:48:36 -04:00
|
|
|
let source_map_url = v8::String::new(s, "").unwrap();
|
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> {
|
2020-05-11 17:48:36 -04:00
|
|
|
let source_map_url = v8::String::new(s, "").unwrap();
|
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
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-01-21 14:24:31 -05:00
|
|
|
pub fn initialize_context<'s>(
|
2020-06-20 07:18:08 -04:00
|
|
|
scope: &mut v8::HandleScope<'s, ()>,
|
2020-01-21 14:24:31 -05:00
|
|
|
) -> v8::Local<'s, v8::Context> {
|
2020-06-20 07:18:08 -04:00
|
|
|
let scope = &mut v8::EscapableHandleScope::new(scope);
|
2020-01-06 14:07:35 -05:00
|
|
|
|
2020-01-21 14:24:31 -05:00
|
|
|
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
|
|
|
|
2021-02-15 06:53:33 -05:00
|
|
|
// global.Deno = { core: {} };
|
2020-06-20 07:18:08 -04:00
|
|
|
let deno_key = v8::String::new(scope, "Deno").unwrap();
|
2020-01-21 14:24:31 -05:00
|
|
|
let deno_val = v8::Object::new(scope);
|
2020-06-20 07:18:08 -04:00
|
|
|
global.set(scope, deno_key.into(), deno_val.into());
|
|
|
|
let core_key = v8::String::new(scope, "core").unwrap();
|
|
|
|
let core_val = v8::Object::new(scope);
|
|
|
|
deno_val.set(scope, core_key.into(), core_val.into());
|
|
|
|
|
2021-02-15 06:53:33 -05:00
|
|
|
// Bind functions to Deno.core.*
|
2021-10-04 06:34:53 -04:00
|
|
|
set_func(scope, core_val, "opcallSync", opcall_sync);
|
|
|
|
set_func(scope, core_val, "opcallAsync", opcall_async);
|
2021-02-15 06:53:33 -05:00
|
|
|
set_func(
|
2020-06-20 07:18:08 -04:00
|
|
|
scope,
|
2021-02-15 06:53:33 -05:00
|
|
|
core_val,
|
|
|
|
"setMacrotaskCallback",
|
|
|
|
set_macrotask_callback,
|
2020-09-08 10:06:26 -04:00
|
|
|
);
|
2021-02-15 06:53:33 -05:00
|
|
|
set_func(scope, core_val, "evalContext", eval_context);
|
|
|
|
set_func(scope, core_val, "encode", encode);
|
|
|
|
set_func(scope, core_val, "decode", decode);
|
2021-02-16 08:20:21 -05:00
|
|
|
set_func(scope, core_val, "serialize", serialize);
|
|
|
|
set_func(scope, core_val, "deserialize", deserialize);
|
2021-02-15 06:53:33 -05:00
|
|
|
set_func(scope, core_val, "getPromiseDetails", get_promise_details);
|
|
|
|
set_func(scope, core_val, "getProxyDetails", get_proxy_details);
|
2021-10-01 14:25:33 -04:00
|
|
|
set_func(scope, core_val, "isProxy", is_proxy);
|
2021-04-12 04:47:44 -04:00
|
|
|
set_func(scope, core_val, "memoryUsage", memory_usage);
|
2021-06-26 20:27:50 -04:00
|
|
|
set_func(scope, core_val, "callConsole", call_console);
|
2021-06-26 05:17:05 -04:00
|
|
|
set_func(scope, core_val, "createHostObject", create_host_object);
|
2021-07-03 17:33:36 -04:00
|
|
|
set_func(
|
|
|
|
scope,
|
|
|
|
core_val,
|
|
|
|
"setWasmStreamingCallback",
|
|
|
|
set_wasm_streaming_callback,
|
|
|
|
);
|
2020-01-06 14:07:35 -05:00
|
|
|
// Direct bindings on `window`.
|
2021-02-15 06:53:33 -05:00
|
|
|
set_func(scope, global, "queueMicrotask", queue_microtask);
|
2020-01-06 14:07:35 -05:00
|
|
|
|
2020-01-21 14:24:31 -05:00
|
|
|
scope.escape(context)
|
2020-01-06 14:07:35 -05:00
|
|
|
}
|
|
|
|
|
2021-02-15 06:53:33 -05:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn set_func(
|
|
|
|
scope: &mut v8::HandleScope<'_>,
|
|
|
|
obj: v8::Local<v8::Object>,
|
|
|
|
name: &'static str,
|
|
|
|
callback: impl v8::MapFnTo<v8::FunctionCallback>,
|
|
|
|
) {
|
|
|
|
let key = v8::String::new(scope, name).unwrap();
|
|
|
|
let tmpl = v8::FunctionTemplate::new(scope, callback);
|
|
|
|
let val = tmpl.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());
|
|
|
|
}
|
|
|
|
|
2020-01-06 10:24:44 -05:00
|
|
|
pub extern "C" fn host_import_module_dynamically_callback(
|
|
|
|
context: v8::Local<v8::Context>,
|
|
|
|
referrer: v8::Local<v8::ScriptOrModule>,
|
|
|
|
specifier: v8::Local<v8::String>,
|
2021-02-05 16:25:02 -05:00
|
|
|
_import_assertions: v8::Local<v8::FixedArray>,
|
2020-01-06 10:24:44 -05:00
|
|
|
) -> *mut v8::Promise {
|
2020-06-20 07:18:08 -04:00
|
|
|
let scope = &mut unsafe { v8::CallbackScope::new(context) };
|
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);
|
|
|
|
let referrer_name = referrer.get_resource_name();
|
|
|
|
let referrer_name_str = referrer_name
|
|
|
|
.to_string(scope)
|
|
|
|
.unwrap()
|
|
|
|
.to_rust_string_lossy(scope);
|
|
|
|
|
|
|
|
// TODO(ry) I'm not sure what HostDefinedOptions is for or if we're ever going
|
|
|
|
// to use it. For now we check that it is not used. This check may need to be
|
|
|
|
// changed in the future.
|
|
|
|
let host_defined_options = referrer.get_host_defined_options();
|
|
|
|
assert_eq!(host_defined_options.length(), 0);
|
|
|
|
|
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);
|
|
|
|
|
2020-07-18 16:32:11 -04:00
|
|
|
let resolver_handle = v8::Global::new(scope, resolver);
|
2020-05-29 17:41:39 -04:00
|
|
|
{
|
2020-09-06 15:44:29 -04:00
|
|
|
let state_rc = JsRuntime::state(scope);
|
2021-05-19 14:53:43 -04:00
|
|
|
let module_map_rc = JsRuntime::module_map(scope);
|
|
|
|
|
|
|
|
debug!(
|
|
|
|
"dyn_import specifier {} referrer {} ",
|
|
|
|
specifier_str, referrer_name_str
|
|
|
|
);
|
2021-06-28 21:03:02 -04:00
|
|
|
ModuleMap::load_dynamic_import(
|
|
|
|
module_map_rc,
|
2021-05-19 14:53:43 -04:00
|
|
|
&specifier_str,
|
|
|
|
&referrer_name_str,
|
|
|
|
resolver_handle,
|
|
|
|
);
|
|
|
|
state_rc.borrow_mut().notify_new_dynamic_import();
|
2020-05-29 17:41:39 -04:00
|
|
|
}
|
2020-01-06 10:24:44 -05: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.
|
|
|
|
let map_err = |scope: &mut v8::HandleScope,
|
|
|
|
args: v8::FunctionCallbackArguments,
|
|
|
|
_rv: v8::ReturnValue| {
|
|
|
|
let arg = args.get(0);
|
|
|
|
if arg.is_native_error() {
|
|
|
|
let message = v8::Exception::create_message(scope, arg);
|
|
|
|
if message.get_stack_trace(scope).unwrap().get_frame_count() == 0 {
|
2021-06-17 15:56:30 -04:00
|
|
|
let arg: v8::Local<v8::Object> = arg.try_into().unwrap();
|
2021-02-21 10:22:06 -05:00
|
|
|
let message_key = v8::String::new(scope, "message").unwrap();
|
|
|
|
let message = arg.get(scope, message_key.into()).unwrap();
|
|
|
|
let exception =
|
|
|
|
v8::Exception::type_error(scope, message.try_into().unwrap());
|
|
|
|
scope.throw_exception(exception);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
scope.throw_exception(arg);
|
|
|
|
};
|
|
|
|
let map_err = v8::FunctionTemplate::new(scope, map_err);
|
|
|
|
let map_err = map_err.get_function(scope).unwrap();
|
|
|
|
let promise = promise.catch(scope, map_err).unwrap();
|
|
|
|
|
2020-06-20 07:18:08 -04:00
|
|
|
&*promise as *const _ as *mut _
|
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>,
|
|
|
|
) {
|
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
|
|
|
|
2020-06-20 07:18:08 -04:00
|
|
|
let url_key = v8::String::new(scope, "url").unwrap();
|
|
|
|
let url_val = v8::String::new(scope, &info.name).unwrap();
|
|
|
|
meta.create_data_property(scope, url_key.into(), url_val.into());
|
|
|
|
|
|
|
|
let main_key = v8::String::new(scope, "main").unwrap();
|
|
|
|
let main_val = v8::Boolean::new(scope, info.main);
|
|
|
|
meta.create_data_property(scope, main_key.into(), main_val.into());
|
2020-01-06 10:24:44 -05:00
|
|
|
}
|
|
|
|
|
2020-01-16 17:25:57 -05:00
|
|
|
pub extern "C" fn promise_reject_callback(message: v8::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
|
|
|
|
2020-09-06 15:44:29 -04:00
|
|
|
let state_rc = JsRuntime::state(scope);
|
2020-05-29 17:41:39 -04:00
|
|
|
let mut state = state_rc.borrow_mut();
|
2020-01-16 17:25:57 -05:00
|
|
|
|
|
|
|
let promise = message.get_promise();
|
2020-11-11 17:11:40 -05:00
|
|
|
let promise_global = v8::Global::new(scope, promise);
|
2020-01-06 10:24:44 -05:00
|
|
|
|
2020-01-16 17:25:57 -05:00
|
|
|
match message.get_event() {
|
2020-01-06 10:24:44 -05:00
|
|
|
v8::PromiseRejectEvent::PromiseRejectWithNoHandler => {
|
2020-10-18 13:07:11 -04:00
|
|
|
let error = message.get_value().unwrap();
|
2020-07-18 16:32:11 -04:00
|
|
|
let error_global = v8::Global::new(scope, error);
|
2020-05-29 17:41:39 -04:00
|
|
|
state
|
2020-01-25 08:31:42 -05:00
|
|
|
.pending_promise_exceptions
|
2020-11-11 17:11:40 -05:00
|
|
|
.insert(promise_global, error_global);
|
2020-01-06 10:24:44 -05:00
|
|
|
}
|
|
|
|
v8::PromiseRejectEvent::PromiseHandlerAddedAfterReject => {
|
2020-11-11 17:11:40 -05:00
|
|
|
state.pending_promise_exceptions.remove(&promise_global);
|
2020-01-06 10:24:44 -05:00
|
|
|
}
|
|
|
|
v8::PromiseRejectEvent::PromiseRejectAfterResolved => {}
|
|
|
|
v8::PromiseRejectEvent::PromiseResolveAfterResolved => {
|
|
|
|
// Should not warn. See #1272
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-04 06:34:53 -04:00
|
|
|
fn opcall_sync<'s>(
|
2020-09-05 20:34:02 -04:00
|
|
|
scope: &mut v8::HandleScope<'s>,
|
2020-01-16 17:25:57 -05:00
|
|
|
args: v8::FunctionCallbackArguments,
|
|
|
|
mut rv: v8::ReturnValue,
|
|
|
|
) {
|
2020-09-06 15:44:29 -04:00
|
|
|
let state_rc = JsRuntime::state(scope);
|
2021-10-10 11:20:30 -04:00
|
|
|
let state = state_rc.borrow_mut();
|
2020-09-05 20:34:02 -04:00
|
|
|
|
|
|
|
let op_id = match v8::Local::<v8::Integer>::try_from(args.get(0))
|
2021-03-31 10:37:38 -04:00
|
|
|
.map(|l| l.value() as OpId)
|
2020-09-14 12:48:57 -04:00
|
|
|
.map_err(AnyError::from)
|
2020-09-05 20:34:02 -04:00
|
|
|
{
|
|
|
|
Ok(op_id) => op_id,
|
2020-04-22 14:24:49 -04:00
|
|
|
Err(err) => {
|
2021-03-31 10:37:38 -04:00
|
|
|
throw_type_error(scope, format!("invalid op id: {}", err));
|
2020-04-22 14:24:49 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2020-01-11 04:49:16 -05:00
|
|
|
|
2021-04-23 11:50:45 -04:00
|
|
|
// opcall(0) returns obj of all ops, handle as special case
|
2021-03-31 10:37:38 -04:00
|
|
|
if op_id == 0 {
|
|
|
|
// TODO: Serialize as HashMap when serde_v8 supports maps ...
|
|
|
|
let ops = OpTable::op_entries(state.op_state.clone());
|
|
|
|
rv.set(to_v8(scope, ops).unwrap());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-04 06:34:53 -04:00
|
|
|
// Deserializable args (may be structured args or ZeroCopyBuf)
|
|
|
|
let a = args.get(1);
|
|
|
|
let b = args.get(2);
|
|
|
|
|
|
|
|
let payload = OpPayload {
|
|
|
|
scope,
|
|
|
|
a,
|
|
|
|
b,
|
2021-10-10 11:20:30 -04:00
|
|
|
op_id,
|
2021-10-04 06:34:53 -04:00
|
|
|
promise_id: 0,
|
|
|
|
};
|
|
|
|
let op = OpTable::route_op(op_id, state.op_state.clone(), payload);
|
|
|
|
match op {
|
|
|
|
Op::Sync(result) => {
|
2021-10-10 11:20:30 -04:00
|
|
|
state.op_state.borrow_mut().tracker.track_sync(op_id);
|
2021-10-04 06:34:53 -04:00
|
|
|
rv.set(result.to_v8(scope).unwrap());
|
|
|
|
}
|
|
|
|
Op::NotFound => {
|
|
|
|
throw_type_error(scope, format!("Unknown op id: {}", op_id));
|
|
|
|
}
|
|
|
|
// Async ops (ref or unref)
|
|
|
|
_ => {
|
|
|
|
throw_type_error(
|
|
|
|
scope,
|
|
|
|
format!("Can not call an async op [{}] with opSync()", op_id),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn opcall_async<'s>(
|
|
|
|
scope: &mut v8::HandleScope<'s>,
|
|
|
|
args: v8::FunctionCallbackArguments,
|
|
|
|
mut rv: v8::ReturnValue,
|
|
|
|
) {
|
|
|
|
let state_rc = JsRuntime::state(scope);
|
|
|
|
let mut state = state_rc.borrow_mut();
|
|
|
|
|
|
|
|
let op_id = match v8::Local::<v8::Integer>::try_from(args.get(0))
|
|
|
|
.map(|l| l.value() as OpId)
|
|
|
|
.map_err(AnyError::from)
|
|
|
|
{
|
|
|
|
Ok(op_id) => op_id,
|
|
|
|
Err(err) => {
|
|
|
|
throw_type_error(scope, format!("invalid op id: {}", err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-31 10:37:38 -04:00
|
|
|
// PromiseId
|
|
|
|
let arg1 = args.get(1);
|
2021-10-04 06:34:53 -04:00
|
|
|
let promise_id = v8::Local::<v8::Integer>::try_from(arg1)
|
|
|
|
.map(|l| l.value() as PromiseId)
|
|
|
|
.map_err(AnyError::from);
|
|
|
|
// Fail if promise id invalid (not an int)
|
2021-03-31 10:37:38 -04:00
|
|
|
let promise_id: PromiseId = match promise_id {
|
|
|
|
Ok(promise_id) => promise_id,
|
|
|
|
Err(err) => {
|
|
|
|
throw_type_error(scope, format!("invalid promise id: {}", err));
|
2020-09-05 20:34:02 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2020-01-06 10:24:44 -05:00
|
|
|
|
2021-05-06 13:32:03 -04:00
|
|
|
// Deserializable args (may be structured args or ZeroCopyBuf)
|
|
|
|
let a = args.get(2);
|
|
|
|
let b = args.get(3);
|
2021-03-31 10:37:38 -04:00
|
|
|
|
2021-05-06 13:32:03 -04:00
|
|
|
let payload = OpPayload {
|
|
|
|
scope,
|
|
|
|
a,
|
|
|
|
b,
|
2021-10-10 11:20:30 -04:00
|
|
|
op_id,
|
2021-05-06 13:32:03 -04:00
|
|
|
promise_id,
|
2021-03-31 10:37:38 -04:00
|
|
|
};
|
2021-05-06 13:32:03 -04:00
|
|
|
let op = OpTable::route_op(op_id, state.op_state.clone(), payload);
|
2021-03-31 10:37:38 -04:00
|
|
|
match op {
|
2021-10-04 06:34:53 -04:00
|
|
|
Op::Sync(result) => match result {
|
|
|
|
OpResult::Ok(_) => throw_type_error(
|
|
|
|
scope,
|
|
|
|
format!("Can not call a sync op [{}] with opAsync()", op_id),
|
|
|
|
),
|
|
|
|
OpResult::Err(_) => rv.set(result.to_v8(scope).unwrap()),
|
|
|
|
},
|
2020-09-05 20:34:02 -04:00
|
|
|
Op::Async(fut) => {
|
2021-10-10 11:20:30 -04:00
|
|
|
state.op_state.borrow_mut().tracker.track_async(op_id);
|
2021-04-11 01:05:43 -04:00
|
|
|
state.pending_ops.push(fut);
|
2021-02-23 07:08:50 -05:00
|
|
|
state.have_unpolled_ops = true;
|
2020-09-05 20:34:02 -04:00
|
|
|
}
|
|
|
|
Op::AsyncUnref(fut) => {
|
2021-10-10 11:20:30 -04:00
|
|
|
state.op_state.borrow_mut().tracker.track_unref(op_id);
|
2021-04-11 01:05:43 -04:00
|
|
|
state.pending_unref_ops.push(fut);
|
2021-02-23 07:08:50 -05:00
|
|
|
state.have_unpolled_ops = true;
|
2020-09-05 20:34:02 -04:00
|
|
|
}
|
|
|
|
Op::NotFound => {
|
2021-03-31 10:37:38 -04:00
|
|
|
throw_type_error(scope, format!("Unknown op id: {}", op_id));
|
2020-01-11 04:49:16 -05:00
|
|
|
}
|
2020-01-06 10:24:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 10:45:28 -04:00
|
|
|
fn set_macrotask_callback(
|
2020-06-20 07:18:08 -04:00
|
|
|
scope: &mut v8::HandleScope,
|
2020-03-19 10:45:28 -04:00
|
|
|
args: v8::FunctionCallbackArguments,
|
|
|
|
_rv: v8::ReturnValue,
|
|
|
|
) {
|
2020-09-06 15:44:29 -04:00
|
|
|
let state_rc = JsRuntime::state(scope);
|
2020-05-29 17:41:39 -04:00
|
|
|
let mut state = state_rc.borrow_mut();
|
2020-03-19 10:45:28 -04:00
|
|
|
|
2020-07-18 16:32:11 -04:00
|
|
|
let cb = match v8::Local::<v8::Function>::try_from(args.get(0)) {
|
|
|
|
Ok(cb) => cb,
|
|
|
|
Err(err) => return throw_type_error(scope, err.to_string()),
|
|
|
|
};
|
|
|
|
|
|
|
|
let slot = match &mut state.js_macrotask_cb {
|
|
|
|
slot @ None => slot,
|
|
|
|
_ => {
|
|
|
|
return throw_type_error(
|
|
|
|
scope,
|
|
|
|
"Deno.core.setMacrotaskCallback() already called",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2020-03-19 10:45:28 -04:00
|
|
|
|
2020-07-18 16:32:11 -04:00
|
|
|
slot.replace(v8::Global::new(scope, cb));
|
2020-03-19 10:45:28 -04:00
|
|
|
}
|
|
|
|
|
2020-01-16 17:25:57 -05:00
|
|
|
fn eval_context(
|
2020-06-20 07:18:08 -04:00
|
|
|
scope: &mut v8::HandleScope,
|
2020-01-16 17:25:57 -05:00
|
|
|
args: v8::FunctionCallbackArguments,
|
|
|
|
mut rv: v8::ReturnValue,
|
|
|
|
) {
|
|
|
|
let source = match v8::Local::<v8::String>::try_from(args.get(0)) {
|
2020-01-06 10:24:44 -05:00
|
|
|
Ok(s) => s,
|
|
|
|
Err(_) => {
|
2021-06-06 12:32:06 -04:00
|
|
|
throw_type_error(scope, "Missing first argument");
|
2020-01-06 10:24:44 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-06 12:32:06 -04:00
|
|
|
let url = match v8::Local::<v8::String>::try_from(args.get(1)) {
|
|
|
|
Ok(s) => match resolve_url_or_path(&s.to_rust_string_lossy(scope)) {
|
|
|
|
Ok(s) => Some(s),
|
|
|
|
Err(err) => {
|
|
|
|
throw_type_error(scope, &format!("Invalid specifier: {}", err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(_) => None,
|
|
|
|
};
|
2020-03-19 10:42:07 -04:00
|
|
|
|
2021-03-25 22:36:46 -04:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct Output<'s>(Option<serde_v8::Value<'s>>, Option<ErrInfo<'s>>);
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct ErrInfo<'s> {
|
|
|
|
thrown: serde_v8::Value<'s>,
|
|
|
|
is_native_error: bool,
|
|
|
|
is_compile_error: bool,
|
|
|
|
}
|
|
|
|
|
2020-06-20 07:18:08 -04:00
|
|
|
let tc_scope = &mut v8::TryCatch::new(scope);
|
2021-02-17 13:47:18 -05:00
|
|
|
let name = v8::String::new(
|
|
|
|
tc_scope,
|
|
|
|
url.as_ref().map_or(crate::DUMMY_SPECIFIER, Url::as_str),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-06-20 07:18:08 -04:00
|
|
|
let origin = script_origin(tc_scope, name);
|
|
|
|
let maybe_script = v8::Script::compile(tc_scope, source, Some(&origin));
|
2020-01-06 10:24:44 -05:00
|
|
|
|
|
|
|
if maybe_script.is_none() {
|
2020-06-20 07:18:08 -04:00
|
|
|
assert!(tc_scope.has_caught());
|
|
|
|
let exception = tc_scope.exception().unwrap();
|
2021-03-25 22:36:46 -04:00
|
|
|
let output = Output(
|
|
|
|
None,
|
|
|
|
Some(ErrInfo {
|
|
|
|
thrown: exception.into(),
|
|
|
|
is_native_error: exception.is_native_error(),
|
|
|
|
is_compile_error: true,
|
|
|
|
}),
|
2020-01-06 10:24:44 -05:00
|
|
|
);
|
2021-03-25 22:36:46 -04:00
|
|
|
rv.set(to_v8(tc_scope, output).unwrap());
|
2020-01-06 10:24:44 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-20 07:18:08 -04:00
|
|
|
let result = maybe_script.unwrap().run(tc_scope);
|
2020-01-06 10:24:44 -05:00
|
|
|
|
|
|
|
if result.is_none() {
|
2020-06-20 07:18:08 -04:00
|
|
|
assert!(tc_scope.has_caught());
|
|
|
|
let exception = tc_scope.exception().unwrap();
|
2021-03-25 22:36:46 -04:00
|
|
|
let output = Output(
|
|
|
|
None,
|
|
|
|
Some(ErrInfo {
|
|
|
|
thrown: exception.into(),
|
|
|
|
is_native_error: exception.is_native_error(),
|
|
|
|
is_compile_error: false,
|
|
|
|
}),
|
2020-01-06 10:24:44 -05:00
|
|
|
);
|
2021-03-25 22:36:46 -04:00
|
|
|
rv.set(to_v8(tc_scope, output).unwrap());
|
2020-01-06 10:24:44 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-25 22:36:46 -04:00
|
|
|
let output = Output(Some(result.unwrap().into()), None);
|
|
|
|
rv.set(to_v8(tc_scope, output).unwrap());
|
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,
|
|
|
|
) {
|
|
|
|
assert!(args.length() >= 2);
|
|
|
|
|
|
|
|
assert!(args.get(0).is_function());
|
|
|
|
assert!(args.get(1).is_function());
|
|
|
|
|
|
|
|
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-07-03 17:33:36 -04:00
|
|
|
fn set_wasm_streaming_callback(
|
|
|
|
scope: &mut v8::HandleScope,
|
|
|
|
args: v8::FunctionCallbackArguments,
|
|
|
|
_rv: v8::ReturnValue,
|
|
|
|
) {
|
refactor(core): Turn the `wasm_streaming_feed` binding into ops (#11985)
Async WebAssembly compilation was implemented by adding two
bindings: `set_wasm_streaming_callback`, which registered a callback to
be called whenever a streaming wasm compilation was started, and
`wasm_streaming_feed`, which let the JS callback modify the state of the
v8 wasm compiler.
`set_wasm_streaming_callback` cannot currently be implemented as
anything other than a binding, but `wasm_streaming_feed` does not really
need to use anything specific to bindings, and could indeed be
implemented as one or more ops. This PR does that, resulting in a
simplification of the relevant code.
There are three operations on the state of the v8 wasm compiler that
`wasm_streaming_feed` allowed: feeding new bytes into the compiler,
letting it know that there are no more bytes coming from the network,
and aborting the compilation. This PR provides `op_wasm_streaming_feed`
to feed new bytes into the compiler, and `op_wasm_streaming_abort` to
abort the compilation. It doesn't provide an op to let v8 know that the
response is finished, but closing the resource with `Deno.core.close()`
will achieve that.
2021-09-13 08:27:54 -04:00
|
|
|
use crate::ops_builtin::WasmStreamingResource;
|
|
|
|
|
2021-07-03 17:33:36 -04:00
|
|
|
let state_rc = JsRuntime::state(scope);
|
|
|
|
let mut state = state_rc.borrow_mut();
|
|
|
|
|
|
|
|
let cb = match v8::Local::<v8::Function>::try_from(args.get(0)) {
|
|
|
|
Ok(cb) => cb,
|
|
|
|
Err(err) => return throw_type_error(scope, err.to_string()),
|
|
|
|
};
|
|
|
|
|
|
|
|
// The callback to pass to the v8 API has to be a unit type, so it can't
|
|
|
|
// borrow or move any local variables. Therefore, we're storing the JS
|
|
|
|
// callback in a JsRuntimeState slot.
|
|
|
|
if let slot @ None = &mut state.js_wasm_streaming_cb {
|
|
|
|
slot.replace(v8::Global::new(scope, cb));
|
|
|
|
} else {
|
|
|
|
return throw_type_error(
|
|
|
|
scope,
|
|
|
|
"Deno.core.setWasmStreamingCallback() already called",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
scope.set_wasm_streaming_callback(|scope, arg, wasm_streaming| {
|
|
|
|
let (cb_handle, streaming_rid) = {
|
|
|
|
let state_rc = JsRuntime::state(scope);
|
|
|
|
let state = state_rc.borrow();
|
|
|
|
let cb_handle = state.js_wasm_streaming_cb.as_ref().unwrap().clone();
|
|
|
|
let streaming_rid = state
|
|
|
|
.op_state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
|
|
|
.add(WasmStreamingResource(RefCell::new(wasm_streaming)));
|
|
|
|
(cb_handle, streaming_rid)
|
|
|
|
};
|
|
|
|
|
|
|
|
let undefined = v8::undefined(scope);
|
|
|
|
let rid = serde_v8::to_v8(scope, streaming_rid).unwrap();
|
|
|
|
cb_handle
|
|
|
|
.get(scope)
|
|
|
|
.call(scope, undefined.into(), &[arg, rid]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-15 10:31:55 -04:00
|
|
|
fn encode(
|
2020-06-20 07:18:08 -04:00
|
|
|
scope: &mut v8::HandleScope,
|
2020-03-15 10:31:55 -04:00
|
|
|
args: v8::FunctionCallbackArguments,
|
|
|
|
mut rv: v8::ReturnValue,
|
|
|
|
) {
|
|
|
|
let text = match v8::Local::<v8::String>::try_from(args.get(0)) {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(_) => {
|
2021-02-16 08:20:21 -05:00
|
|
|
throw_type_error(scope, "Invalid argument");
|
2020-03-15 10:31:55 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let text_str = text.to_rust_string_lossy(scope);
|
2021-07-08 11:57:57 -04:00
|
|
|
let zbuf: ZeroCopyBuf = text_str.into_bytes().into();
|
2020-03-19 16:31:56 -04:00
|
|
|
|
2021-04-30 21:09:58 -04:00
|
|
|
rv.set(to_v8(scope, zbuf).unwrap())
|
2020-03-15 10:31:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn decode(
|
2020-06-20 07:18:08 -04:00
|
|
|
scope: &mut v8::HandleScope,
|
2020-03-15 10:31:55 -04:00
|
|
|
args: v8::FunctionCallbackArguments,
|
|
|
|
mut rv: v8::ReturnValue,
|
|
|
|
) {
|
2021-04-30 21:09:58 -04:00
|
|
|
let zero_copy: ZeroCopyBuf = match serde_v8::from_v8(scope, args.get(0)) {
|
|
|
|
Ok(zbuf) => zbuf,
|
2020-04-22 14:24:49 -04:00
|
|
|
Err(_) => {
|
2021-02-16 08:20:21 -05:00
|
|
|
throw_type_error(scope, "Invalid argument");
|
2020-03-15 10:31:55 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2021-04-18 08:39:26 -04:00
|
|
|
let buf = &zero_copy;
|
2020-04-22 14:24:49 -04:00
|
|
|
|
2021-01-05 13:50:40 -05:00
|
|
|
// Strip BOM
|
|
|
|
let buf =
|
|
|
|
if buf.len() >= 3 && buf[0] == 0xef && buf[1] == 0xbb && buf[2] == 0xbf {
|
|
|
|
&buf[3..]
|
|
|
|
} else {
|
|
|
|
buf
|
|
|
|
};
|
|
|
|
|
2020-09-09 14:52:11 -04:00
|
|
|
// If `String::new_from_utf8()` returns `None`, this means that the
|
|
|
|
// length of the decoded string would be longer than what V8 can
|
|
|
|
// handle. In this case we return `RangeError`.
|
|
|
|
//
|
|
|
|
// For more details see:
|
|
|
|
// - https://encoding.spec.whatwg.org/#dom-textdecoder-decode
|
|
|
|
// - https://github.com/denoland/deno/issues/6649
|
|
|
|
// - https://github.com/v8/v8/blob/d68fb4733e39525f9ff0a9222107c02c28096e2a/include/v8.h#L3277-L3278
|
2021-07-30 09:03:41 -04:00
|
|
|
match v8::String::new_from_utf8(scope, buf, v8::NewStringType::Normal) {
|
2020-09-09 14:52:11 -04:00
|
|
|
Some(text) => rv.set(text.into()),
|
|
|
|
None => {
|
|
|
|
let msg = v8::String::new(scope, "string too long").unwrap();
|
|
|
|
let exception = v8::Exception::range_error(scope, msg);
|
|
|
|
scope.throw_exception(exception);
|
|
|
|
}
|
|
|
|
};
|
2020-03-15 10:31:55 -04:00
|
|
|
}
|
|
|
|
|
2021-06-26 05:17:05 -04:00
|
|
|
struct SerializeDeserialize<'a> {
|
|
|
|
host_objects: Option<v8::Local<'a, v8::Array>>,
|
|
|
|
}
|
2021-02-16 08:20:21 -05:00
|
|
|
|
2021-06-26 05:17:05 -04:00
|
|
|
impl<'a> v8::ValueSerializerImpl for SerializeDeserialize<'a> {
|
2021-02-16 08:20:21 -05:00
|
|
|
#[allow(unused_variables)]
|
|
|
|
fn throw_data_clone_error<'s>(
|
|
|
|
&mut self,
|
|
|
|
scope: &mut v8::HandleScope<'s>,
|
|
|
|
message: v8::Local<'s, v8::String>,
|
|
|
|
) {
|
|
|
|
let error = v8::Exception::error(scope, message);
|
|
|
|
scope.throw_exception(error);
|
|
|
|
}
|
2021-06-26 05:17:05 -04:00
|
|
|
|
2021-07-06 13:42:52 -04:00
|
|
|
fn get_shared_array_buffer_id<'s>(
|
|
|
|
&mut self,
|
|
|
|
scope: &mut HandleScope<'s>,
|
|
|
|
shared_array_buffer: Local<'s, SharedArrayBuffer>,
|
|
|
|
) -> Option<u32> {
|
|
|
|
let state_rc = JsRuntime::state(scope);
|
|
|
|
let state = state_rc.borrow_mut();
|
|
|
|
if let Some(shared_array_buffer_store) = &state.shared_array_buffer_store {
|
|
|
|
let backing_store = shared_array_buffer.get_backing_store();
|
|
|
|
let id = shared_array_buffer_store.insert(backing_store);
|
|
|
|
Some(id)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-29 04:47:24 -04:00
|
|
|
fn get_wasm_module_transfer_id(
|
|
|
|
&mut self,
|
|
|
|
scope: &mut HandleScope<'_>,
|
|
|
|
module: Local<v8::WasmModuleObject>,
|
|
|
|
) -> Option<u32> {
|
|
|
|
let state_rc = JsRuntime::state(scope);
|
|
|
|
let state = state_rc.borrow_mut();
|
|
|
|
if let Some(compiled_wasm_module_store) = &state.compiled_wasm_module_store
|
|
|
|
{
|
|
|
|
let compiled_wasm_module = module.get_compiled_module();
|
|
|
|
let id = compiled_wasm_module_store.insert(compiled_wasm_module);
|
|
|
|
Some(id)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-26 05:17:05 -04:00
|
|
|
fn write_host_object<'s>(
|
|
|
|
&mut self,
|
|
|
|
scope: &mut v8::HandleScope<'s>,
|
|
|
|
object: v8::Local<'s, v8::Object>,
|
|
|
|
value_serializer: &mut dyn v8::ValueSerializerHelper,
|
|
|
|
) -> Option<bool> {
|
|
|
|
if let Some(host_objects) = self.host_objects {
|
|
|
|
for i in 0..host_objects.length() {
|
|
|
|
let value = host_objects.get_index(scope, i).unwrap();
|
|
|
|
if value == object {
|
|
|
|
value_serializer.write_uint32(i);
|
|
|
|
return Some(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let message = v8::String::new(scope, "Unsupported object type").unwrap();
|
|
|
|
self.throw_data_clone_error(scope, message);
|
|
|
|
None
|
|
|
|
}
|
2021-02-16 08:20:21 -05:00
|
|
|
}
|
|
|
|
|
2021-06-26 05:17:05 -04:00
|
|
|
impl<'a> v8::ValueDeserializerImpl for SerializeDeserialize<'a> {
|
2021-07-06 13:42:52 -04:00
|
|
|
fn get_shared_array_buffer_from_id<'s>(
|
|
|
|
&mut self,
|
|
|
|
scope: &mut HandleScope<'s>,
|
|
|
|
transfer_id: u32,
|
|
|
|
) -> Option<Local<'s, SharedArrayBuffer>> {
|
|
|
|
let state_rc = JsRuntime::state(scope);
|
|
|
|
let state = state_rc.borrow_mut();
|
|
|
|
if let Some(shared_array_buffer_store) = &state.shared_array_buffer_store {
|
|
|
|
let backing_store = shared_array_buffer_store.take(transfer_id)?;
|
|
|
|
let shared_array_buffer =
|
|
|
|
v8::SharedArrayBuffer::with_backing_store(scope, &backing_store);
|
|
|
|
Some(shared_array_buffer)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-29 04:47:24 -04:00
|
|
|
fn get_wasm_module_from_id<'s>(
|
|
|
|
&mut self,
|
|
|
|
scope: &mut HandleScope<'s>,
|
|
|
|
clone_id: u32,
|
|
|
|
) -> Option<Local<'s, v8::WasmModuleObject>> {
|
|
|
|
let state_rc = JsRuntime::state(scope);
|
|
|
|
let state = state_rc.borrow_mut();
|
|
|
|
if let Some(compiled_wasm_module_store) = &state.compiled_wasm_module_store
|
|
|
|
{
|
|
|
|
let compiled_module = compiled_wasm_module_store.take(clone_id)?;
|
|
|
|
v8::WasmModuleObject::from_compiled_module(scope, &compiled_module)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-26 05:17:05 -04:00
|
|
|
fn read_host_object<'s>(
|
|
|
|
&mut self,
|
|
|
|
scope: &mut v8::HandleScope<'s>,
|
|
|
|
value_deserializer: &mut dyn v8::ValueDeserializerHelper,
|
|
|
|
) -> Option<v8::Local<'s, v8::Object>> {
|
|
|
|
if let Some(host_objects) = self.host_objects {
|
|
|
|
let mut i = 0;
|
|
|
|
if !value_deserializer.read_uint32(&mut i) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let maybe_value = host_objects.get_index(scope, i);
|
|
|
|
if let Some(value) = maybe_value {
|
|
|
|
return value.to_object(scope);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let message =
|
|
|
|
v8::String::new(scope, "Failed to deserialize host object").unwrap();
|
|
|
|
let error = v8::Exception::error(scope, message);
|
|
|
|
scope.throw_exception(error);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2021-02-16 08:20:21 -05:00
|
|
|
|
|
|
|
fn serialize(
|
|
|
|
scope: &mut v8::HandleScope,
|
|
|
|
args: v8::FunctionCallbackArguments,
|
|
|
|
mut rv: v8::ReturnValue,
|
|
|
|
) {
|
2021-06-26 05:17:05 -04:00
|
|
|
let value = args.get(0);
|
|
|
|
|
|
|
|
let options: Option<SerializeDeserializeOptions> =
|
|
|
|
match serde_v8::from_v8(scope, args.get(1)) {
|
|
|
|
Ok(opts) => opts,
|
|
|
|
Err(_) => {
|
|
|
|
throw_type_error(scope, "Invalid argument 2");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-08-25 07:48:53 -04:00
|
|
|
let options = options.unwrap_or(SerializeDeserializeOptions {
|
|
|
|
host_objects: None,
|
|
|
|
transfered_array_buffers: None,
|
|
|
|
});
|
2021-06-26 05:17:05 -04:00
|
|
|
|
|
|
|
let host_objects = match options.host_objects {
|
|
|
|
Some(value) => match v8::Local::<v8::Array>::try_from(value.v8_value) {
|
|
|
|
Ok(host_objects) => Some(host_objects),
|
|
|
|
Err(_) => {
|
|
|
|
throw_type_error(scope, "host_objects not an array");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
2021-08-25 07:48:53 -04:00
|
|
|
let transfered_array_buffers = match options.transfered_array_buffers {
|
|
|
|
Some(value) => match v8::Local::<v8::Array>::try_from(value.v8_value) {
|
|
|
|
Ok(transfered_array_buffers) => Some(transfered_array_buffers),
|
|
|
|
Err(_) => {
|
|
|
|
throw_type_error(scope, "transfered_array_buffers not an array");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
2021-06-26 05:17:05 -04:00
|
|
|
let serialize_deserialize = Box::new(SerializeDeserialize { host_objects });
|
2021-02-16 08:20:21 -05:00
|
|
|
let mut value_serializer =
|
|
|
|
v8::ValueSerializer::new(scope, serialize_deserialize);
|
2021-08-25 07:48:53 -04:00
|
|
|
|
2021-08-09 04:39:00 -04:00
|
|
|
value_serializer.write_header();
|
2021-08-25 07:48:53 -04:00
|
|
|
|
|
|
|
if let Some(transfered_array_buffers) = transfered_array_buffers {
|
|
|
|
let state_rc = JsRuntime::state(scope);
|
|
|
|
let state = state_rc.borrow_mut();
|
|
|
|
for i in 0..transfered_array_buffers.length() {
|
|
|
|
let i = v8::Number::new(scope, i as f64).into();
|
|
|
|
let buf = transfered_array_buffers.get(scope, i).unwrap();
|
|
|
|
let buf = match v8::Local::<v8::ArrayBuffer>::try_from(buf) {
|
|
|
|
Ok(buf) => buf,
|
|
|
|
Err(_) => {
|
|
|
|
throw_type_error(
|
|
|
|
scope,
|
|
|
|
"item in transfered_array_buffers not an ArrayBuffer",
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if let Some(shared_array_buffer_store) = &state.shared_array_buffer_store
|
|
|
|
{
|
|
|
|
// TODO(lucacasonato): we need to check here that the buffer is not
|
|
|
|
// already detached. We can not do that because V8 does not provide
|
|
|
|
// a way to check if a buffer is already detached.
|
|
|
|
if !buf.is_detachable() {
|
|
|
|
throw_type_error(
|
|
|
|
scope,
|
|
|
|
"item in transfered_array_buffers is not transferable",
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let backing_store = buf.get_backing_store();
|
|
|
|
buf.detach();
|
|
|
|
let id = shared_array_buffer_store.insert(backing_store);
|
|
|
|
value_serializer.transfer_array_buffer(id, buf);
|
|
|
|
let id = v8::Number::new(scope, id as f64).into();
|
|
|
|
transfered_array_buffers.set(scope, i, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-26 05:17:05 -04:00
|
|
|
match value_serializer.write_value(scope.get_current_context(), value) {
|
2021-02-16 08:20:21 -05:00
|
|
|
Some(true) => {
|
|
|
|
let vector = value_serializer.release();
|
2021-04-30 21:09:58 -04:00
|
|
|
let zbuf: ZeroCopyBuf = vector.into();
|
|
|
|
rv.set(to_v8(scope, zbuf).unwrap());
|
2021-02-16 08:20:21 -05:00
|
|
|
}
|
|
|
|
_ => {
|
2021-06-26 05:17:05 -04:00
|
|
|
throw_type_error(scope, "Failed to serialize response");
|
2021-02-16 08:20:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 07:48:53 -04:00
|
|
|
#[derive(Deserialize)]
|
2021-06-26 05:17:05 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct SerializeDeserializeOptions<'a> {
|
|
|
|
host_objects: Option<serde_v8::Value<'a>>,
|
2021-08-25 07:48:53 -04:00
|
|
|
transfered_array_buffers: Option<serde_v8::Value<'a>>,
|
2021-06-26 05:17:05 -04:00
|
|
|
}
|
|
|
|
|
2021-02-16 08:20:21 -05:00
|
|
|
fn deserialize(
|
|
|
|
scope: &mut v8::HandleScope,
|
|
|
|
args: v8::FunctionCallbackArguments,
|
|
|
|
mut rv: v8::ReturnValue,
|
|
|
|
) {
|
2021-04-30 21:09:58 -04:00
|
|
|
let zero_copy: ZeroCopyBuf = match serde_v8::from_v8(scope, args.get(0)) {
|
|
|
|
Ok(zbuf) => zbuf,
|
2021-02-16 08:20:21 -05:00
|
|
|
Err(_) => {
|
2021-06-26 05:17:05 -04:00
|
|
|
throw_type_error(scope, "Invalid argument 1");
|
2021-02-16 08:20:21 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-26 05:17:05 -04:00
|
|
|
let options: Option<SerializeDeserializeOptions> =
|
|
|
|
match serde_v8::from_v8(scope, args.get(1)) {
|
|
|
|
Ok(opts) => opts,
|
|
|
|
Err(_) => {
|
|
|
|
throw_type_error(scope, "Invalid argument 2");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-08-25 07:48:53 -04:00
|
|
|
let options = options.unwrap_or(SerializeDeserializeOptions {
|
|
|
|
host_objects: None,
|
|
|
|
transfered_array_buffers: None,
|
|
|
|
});
|
2021-06-26 05:17:05 -04:00
|
|
|
|
|
|
|
let host_objects = match options.host_objects {
|
|
|
|
Some(value) => match v8::Local::<v8::Array>::try_from(value.v8_value) {
|
|
|
|
Ok(host_objects) => Some(host_objects),
|
|
|
|
Err(_) => {
|
|
|
|
throw_type_error(scope, "host_objects not an array");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
2021-08-25 07:48:53 -04:00
|
|
|
let transfered_array_buffers = match options.transfered_array_buffers {
|
|
|
|
Some(value) => match v8::Local::<v8::Array>::try_from(value.v8_value) {
|
|
|
|
Ok(transfered_array_buffers) => Some(transfered_array_buffers),
|
|
|
|
Err(_) => {
|
|
|
|
throw_type_error(scope, "transfered_array_buffers not an array");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
2021-06-26 05:17:05 -04:00
|
|
|
let serialize_deserialize = Box::new(SerializeDeserialize { host_objects });
|
2021-02-16 08:20:21 -05:00
|
|
|
let mut value_deserializer =
|
2021-06-26 05:17:05 -04:00
|
|
|
v8::ValueDeserializer::new(scope, serialize_deserialize, &zero_copy);
|
2021-08-25 07:48:53 -04:00
|
|
|
|
2021-08-09 04:39:00 -04:00
|
|
|
let parsed_header = value_deserializer
|
|
|
|
.read_header(scope.get_current_context())
|
|
|
|
.unwrap_or_default();
|
|
|
|
if !parsed_header {
|
|
|
|
let msg = v8::String::new(scope, "could not deserialize value").unwrap();
|
|
|
|
let exception = v8::Exception::range_error(scope, msg);
|
|
|
|
scope.throw_exception(exception);
|
|
|
|
return;
|
|
|
|
}
|
2021-08-25 07:48:53 -04:00
|
|
|
|
|
|
|
if let Some(transfered_array_buffers) = transfered_array_buffers {
|
|
|
|
let state_rc = JsRuntime::state(scope);
|
|
|
|
let state = state_rc.borrow_mut();
|
|
|
|
if let Some(shared_array_buffer_store) = &state.shared_array_buffer_store {
|
|
|
|
for i in 0..transfered_array_buffers.length() {
|
|
|
|
let i = v8::Number::new(scope, i as f64).into();
|
|
|
|
let id_val = transfered_array_buffers.get(scope, i).unwrap();
|
|
|
|
let id = match id_val.number_value(scope) {
|
|
|
|
Some(id) => id as u32,
|
|
|
|
None => {
|
|
|
|
throw_type_error(
|
|
|
|
scope,
|
|
|
|
"item in transfered_array_buffers not number",
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if let Some(backing_store) = shared_array_buffer_store.take(id) {
|
|
|
|
let array_buffer =
|
|
|
|
v8::ArrayBuffer::with_backing_store(scope, &backing_store);
|
|
|
|
value_deserializer.transfer_array_buffer(id, array_buffer);
|
|
|
|
transfered_array_buffers.set(scope, id_val, array_buffer.into());
|
|
|
|
} else {
|
|
|
|
throw_type_error(
|
|
|
|
scope,
|
|
|
|
"transfered array buffer not present in shared_array_buffer_store",
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 08:20:21 -05:00
|
|
|
let value = value_deserializer.read_value(scope.get_current_context());
|
|
|
|
|
|
|
|
match value {
|
|
|
|
Some(deserialized) => rv.set(deserialized),
|
|
|
|
None => {
|
2021-05-11 15:09:09 -04:00
|
|
|
let msg = v8::String::new(scope, "could not deserialize value").unwrap();
|
2021-02-16 08:20:21 -05:00
|
|
|
let exception = v8::Exception::range_error(scope, msg);
|
|
|
|
scope.throw_exception(exception);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-01-16 17:25:57 -05:00
|
|
|
fn queue_microtask(
|
2020-06-20 07:18:08 -04:00
|
|
|
scope: &mut v8::HandleScope,
|
2020-01-16 17:25:57 -05:00
|
|
|
args: v8::FunctionCallbackArguments,
|
|
|
|
_rv: v8::ReturnValue,
|
|
|
|
) {
|
|
|
|
match v8::Local::<v8::Function>::try_from(args.get(0)) {
|
2020-06-20 07:18:08 -04:00
|
|
|
Ok(f) => scope.enqueue_microtask(f),
|
2020-01-06 10:24:44 -05:00
|
|
|
Err(_) => {
|
2021-02-16 08:20:21 -05:00
|
|
|
throw_type_error(scope, "Invalid argument");
|
2020-01-06 10:24:44 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-26 05:17:05 -04:00
|
|
|
fn create_host_object(
|
|
|
|
scope: &mut v8::HandleScope,
|
|
|
|
_args: v8::FunctionCallbackArguments,
|
|
|
|
mut rv: v8::ReturnValue,
|
|
|
|
) {
|
|
|
|
let template = v8::ObjectTemplate::new(scope);
|
|
|
|
template.set_internal_field_count(1);
|
|
|
|
if let Some(obj) = template.new_instance(scope) {
|
|
|
|
rv.set(obj.into());
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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-02-05 16:25:02 -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>> {
|
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");
|
|
|
|
let referrer_name = referrer_info.name.to_string();
|
2020-01-06 10:24:44 -05:00
|
|
|
|
|
|
|
let specifier_str = specifier.to_rust_string_lossy(scope);
|
|
|
|
|
2021-05-19 14:53:43 -04:00
|
|
|
let maybe_module =
|
|
|
|
module_map.resolve_callback(scope, &specifier_str, &referrer_name);
|
|
|
|
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!(
|
|
|
|
r#"Cannot resolve module "{}" from "{}""#,
|
|
|
|
specifier_str, referrer_name
|
|
|
|
);
|
|
|
|
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
|
|
|
|
|
|
|
// Returns promise details or throw TypeError, if argument passed isn't a Promise.
|
2020-06-20 07:18:08 -04:00
|
|
|
// Promise details is a js_two elements array.
|
2020-03-30 19:01:19 -04:00
|
|
|
// promise_details = [State, Result]
|
|
|
|
// State = enum { Pending = 0, Fulfilled = 1, Rejected = 2}
|
|
|
|
// Result = PromiseResult<T> | PromiseError
|
|
|
|
fn get_promise_details(
|
2020-06-20 07:18:08 -04:00
|
|
|
scope: &mut v8::HandleScope,
|
2020-03-30 19:01:19 -04:00
|
|
|
args: v8::FunctionCallbackArguments,
|
|
|
|
mut rv: v8::ReturnValue,
|
|
|
|
) {
|
2020-04-22 14:24:49 -04:00
|
|
|
let promise = match v8::Local::<v8::Promise>::try_from(args.get(0)) {
|
2020-03-30 19:01:19 -04:00
|
|
|
Ok(val) => val,
|
|
|
|
Err(_) => {
|
2021-02-16 08:20:21 -05:00
|
|
|
throw_type_error(scope, "Invalid argument");
|
2020-03-30 19:01:19 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-25 22:36:46 -04:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct PromiseDetails<'s>(u32, Option<serde_v8::Value<'s>>);
|
2020-03-30 19:01:19 -04:00
|
|
|
|
|
|
|
match promise.state() {
|
|
|
|
v8::PromiseState::Pending => {
|
2021-03-25 22:36:46 -04:00
|
|
|
rv.set(to_v8(scope, PromiseDetails(0, None)).unwrap());
|
2020-03-30 19:01:19 -04:00
|
|
|
}
|
|
|
|
v8::PromiseState::Fulfilled => {
|
2020-06-20 07:18:08 -04:00
|
|
|
let promise_result = promise.result(scope);
|
2021-03-25 22:36:46 -04:00
|
|
|
rv.set(
|
|
|
|
to_v8(scope, PromiseDetails(1, Some(promise_result.into()))).unwrap(),
|
|
|
|
);
|
2020-03-30 19:01:19 -04:00
|
|
|
}
|
|
|
|
v8::PromiseState::Rejected => {
|
2020-06-20 07:18:08 -04:00
|
|
|
let promise_result = promise.result(scope);
|
2021-03-25 22:36:46 -04:00
|
|
|
rv.set(
|
|
|
|
to_v8(scope, PromiseDetails(2, Some(promise_result.into()))).unwrap(),
|
|
|
|
);
|
2020-03-30 19:01:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-18 16:32:11 -04:00
|
|
|
|
2020-09-08 10:06:26 -04:00
|
|
|
// Based on https://github.com/nodejs/node/blob/1e470510ff74391d7d4ec382909ea8960d2d2fbc/src/node_util.cc
|
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
fn get_proxy_details(
|
|
|
|
scope: &mut v8::HandleScope,
|
|
|
|
args: v8::FunctionCallbackArguments,
|
|
|
|
mut rv: v8::ReturnValue,
|
|
|
|
) {
|
|
|
|
// Return undefined if it's not a proxy.
|
|
|
|
let proxy = match v8::Local::<v8::Proxy>::try_from(args.get(0)) {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(_) => {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let target = proxy.get_target(scope);
|
|
|
|
let handler = proxy.get_handler(scope);
|
2021-03-25 22:36:46 -04:00
|
|
|
let p: (serde_v8::Value, serde_v8::Value) = (target.into(), handler.into());
|
|
|
|
rv.set(to_v8(scope, p).unwrap());
|
2020-09-08 10:06:26 -04:00
|
|
|
}
|
|
|
|
|
2021-10-01 14:25:33 -04:00
|
|
|
fn is_proxy(
|
|
|
|
scope: &mut v8::HandleScope,
|
|
|
|
args: v8::FunctionCallbackArguments,
|
|
|
|
mut rv: v8::ReturnValue,
|
|
|
|
) {
|
|
|
|
rv.set(v8::Boolean::new(scope, args.get(0).is_proxy()).into())
|
|
|
|
}
|
|
|
|
|
2020-11-27 14:47:35 -05:00
|
|
|
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);
|
|
|
|
}
|
2021-03-23 10:33:06 -04:00
|
|
|
|
2021-04-12 04:47:44 -04:00
|
|
|
fn memory_usage(
|
2021-03-23 10:33:06 -04:00
|
|
|
scope: &mut v8::HandleScope,
|
|
|
|
_args: v8::FunctionCallbackArguments,
|
|
|
|
mut rv: v8::ReturnValue,
|
|
|
|
) {
|
2021-04-12 04:47:44 -04:00
|
|
|
let stats = get_memory_usage(scope);
|
2021-03-26 09:09:22 -04:00
|
|
|
rv.set(to_v8(scope, stats).unwrap());
|
2021-03-23 10:33:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// HeapStats stores values from a isolate.get_heap_statistics() call
|
2021-03-26 09:09:22 -04:00
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-04-12 04:47:44 -04:00
|
|
|
struct MemoryUsage {
|
|
|
|
rss: usize,
|
|
|
|
heap_total: usize,
|
|
|
|
heap_used: usize,
|
|
|
|
external: usize,
|
|
|
|
// TODO: track ArrayBuffers, would require using a custom allocator to track
|
|
|
|
// but it's otherwise a subset of external so can be indirectly tracked
|
|
|
|
// array_buffers: usize,
|
2021-03-23 10:33:06 -04:00
|
|
|
}
|
2021-04-12 04:47:44 -04:00
|
|
|
fn get_memory_usage(isolate: &mut v8::Isolate) -> MemoryUsage {
|
2021-03-23 10:33:06 -04:00
|
|
|
let mut s = v8::HeapStatistics::default();
|
|
|
|
isolate.get_heap_statistics(&mut s);
|
|
|
|
|
2021-04-12 04:47:44 -04:00
|
|
|
MemoryUsage {
|
|
|
|
rss: s.total_physical_size(),
|
|
|
|
heap_total: s.total_heap_size(),
|
|
|
|
heap_used: s.used_heap_size(),
|
|
|
|
external: s.external_memory(),
|
2021-03-23 10:33:06 -04:00
|
|
|
}
|
|
|
|
}
|