mirror of
https://github.com/denoland/deno.git
synced 2024-11-24 15:19:26 -05:00
refactor(core): Move Deno.core bindings to ops (#14793)
This commit is contained in:
parent
cfb6067f9b
commit
9385a91312
17 changed files with 941 additions and 1260 deletions
6
.github/workflows/ci.yml
vendored
6
.github/workflows/ci.yml
vendored
|
@ -235,7 +235,7 @@ jobs:
|
|||
~/.cargo/registry/index
|
||||
~/.cargo/registry/cache
|
||||
~/.cargo/git/db
|
||||
key: 14-cargo-home-${{ matrix.os }}-${{ hashFiles('Cargo.lock') }}
|
||||
key: 15-cargo-home-${{ matrix.os }}-${{ hashFiles('Cargo.lock') }}
|
||||
|
||||
# In main branch, always creates fresh cache
|
||||
- name: Cache build output (main)
|
||||
|
@ -251,7 +251,7 @@ jobs:
|
|||
!./target/*/*.zip
|
||||
!./target/*/*.tar.gz
|
||||
key: |
|
||||
14-cargo-target-${{ matrix.os }}-${{ matrix.profile }}-${{ github.sha }}
|
||||
15-cargo-target-${{ matrix.os }}-${{ matrix.profile }}-${{ github.sha }}
|
||||
|
||||
# Restore cache from the latest 'main' branch build.
|
||||
- name: Cache build output (PR)
|
||||
|
@ -267,7 +267,7 @@ jobs:
|
|||
!./target/*/*.tar.gz
|
||||
key: never_saved
|
||||
restore-keys: |
|
||||
14-cargo-target-${{ matrix.os }}-${{ matrix.profile }}-
|
||||
15-cargo-target-${{ matrix.os }}-${{ matrix.profile }}-
|
||||
|
||||
# Don't save cache after building PRs or branches other than 'main'.
|
||||
- name: Skip save cache (PR)
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
[WILDCARD]error: Uncaught Error: foo
|
||||
Warning Couldn't format source line: Column 31 is out of bounds (source may have changed at runtime)
|
||||
at file:///[WILDCARD]/eval_context_conflicting_source.ts:1:31
|
||||
at [WILDCARD]
|
||||
at file:///[WILDCARD]/eval_context_throw_with_conflicting_source.ts:[WILDCARD]
|
||||
|
|
|
@ -170,9 +170,6 @@
|
|||
// Create copy of isNaN
|
||||
primordials[isNaN.name] = isNaN;
|
||||
|
||||
// Create copy of queueMicrotask
|
||||
primordials["queueMicrotask"] = queueMicrotask;
|
||||
|
||||
// Create copies of URI handling functions
|
||||
[
|
||||
decodeURI,
|
||||
|
@ -280,6 +277,7 @@
|
|||
ArrayPrototypeForEach,
|
||||
FunctionPrototypeCall,
|
||||
Map,
|
||||
ObjectDefineProperty,
|
||||
ObjectFreeze,
|
||||
ObjectSetPrototypeOf,
|
||||
Promise,
|
||||
|
@ -456,6 +454,20 @@
|
|||
.then(a, b)
|
||||
);
|
||||
|
||||
// Create getter and setter for `queueMicrotask`, it hasn't been bound yet.
|
||||
let queueMicrotask = undefined;
|
||||
ObjectDefineProperty(primordials, "queueMicrotask", {
|
||||
get() {
|
||||
return queueMicrotask;
|
||||
},
|
||||
});
|
||||
primordials.setQueueMicrotask = (value) => {
|
||||
if (queueMicrotask !== undefined) {
|
||||
throw new Error("queueMicrotask is already defined");
|
||||
}
|
||||
queueMicrotask = value;
|
||||
};
|
||||
|
||||
ObjectSetPrototypeOf(primordials, null);
|
||||
ObjectFreeze(primordials);
|
||||
|
||||
|
|
|
@ -24,12 +24,10 @@
|
|||
StringPrototypeSlice,
|
||||
ObjectAssign,
|
||||
SymbolFor,
|
||||
setQueueMicrotask,
|
||||
} = window.__bootstrap.primordials;
|
||||
const ops = window.Deno.core.ops;
|
||||
|
||||
// Available on start due to bindings.
|
||||
const { refOp_, unrefOp_ } = window.Deno.core;
|
||||
|
||||
const errorMap = {};
|
||||
// Builtin v8 / JS errors
|
||||
registerErrorClass("Error", Error);
|
||||
|
@ -176,53 +174,33 @@
|
|||
if (!hasPromise(promiseId)) {
|
||||
return;
|
||||
}
|
||||
refOp_(promiseId);
|
||||
opSync("op_ref_op", promiseId);
|
||||
}
|
||||
|
||||
function unrefOp(promiseId) {
|
||||
if (!hasPromise(promiseId)) {
|
||||
return;
|
||||
}
|
||||
unrefOp_(promiseId);
|
||||
opSync("op_unref_op", promiseId);
|
||||
}
|
||||
|
||||
function resources() {
|
||||
return ObjectFromEntries(opSync("op_resources"));
|
||||
}
|
||||
|
||||
function read(rid, buf) {
|
||||
return opAsync("op_read", rid, buf);
|
||||
}
|
||||
|
||||
function write(rid, buf) {
|
||||
return opAsync("op_write", rid, buf);
|
||||
}
|
||||
|
||||
function shutdown(rid) {
|
||||
return opAsync("op_shutdown", rid);
|
||||
}
|
||||
|
||||
function close(rid) {
|
||||
opSync("op_close", rid);
|
||||
}
|
||||
|
||||
function tryClose(rid) {
|
||||
opSync("op_try_close", rid);
|
||||
}
|
||||
|
||||
function print(str, isErr = false) {
|
||||
opSync("op_print", str, isErr);
|
||||
}
|
||||
|
||||
function metrics() {
|
||||
const [aggregate, perOps] = opSync("op_metrics");
|
||||
aggregate.ops = ObjectFromEntries(ArrayPrototypeMap(
|
||||
core.opNames(),
|
||||
core.opSync("op_op_names"),
|
||||
(opName, opId) => [opName, perOps[opId]],
|
||||
));
|
||||
return aggregate;
|
||||
}
|
||||
|
||||
function queueMicrotask(...args) {
|
||||
return opSync("op_queue_microtask", ...args);
|
||||
}
|
||||
|
||||
// Some "extensions" rely on "BadResource" and "Interrupted" errors in the
|
||||
// JS code (eg. "deno_net") so they are provided in "Deno.core" but later
|
||||
// reexported on "Deno.errors"
|
||||
|
@ -246,12 +224,6 @@
|
|||
const core = ObjectAssign(globalThis.Deno.core, {
|
||||
opAsync,
|
||||
opSync,
|
||||
close,
|
||||
tryClose,
|
||||
read,
|
||||
write,
|
||||
shutdown,
|
||||
print,
|
||||
resources,
|
||||
metrics,
|
||||
registerErrorBuilder,
|
||||
|
@ -266,8 +238,41 @@
|
|||
opCallTraces,
|
||||
refOp,
|
||||
unrefOp,
|
||||
close: opSync.bind(null, "op_close"),
|
||||
tryClose: opSync.bind(null, "op_try_close"),
|
||||
read: opAsync.bind(null, "op_read"),
|
||||
write: opAsync.bind(null, "op_write"),
|
||||
shutdown: opAsync.bind(null, "op_shutdown"),
|
||||
print: opSync.bind(null, "op_print"),
|
||||
setMacrotaskCallback: opSync.bind(null, "op_set_macrotask_callback"),
|
||||
setNextTickCallback: opSync.bind(null, "op_set_next_tick_callback"),
|
||||
runMicrotasks: opSync.bind(null, "op_run_microtasks"),
|
||||
hasTickScheduled: opSync.bind(null, "op_has_tick_scheduled"),
|
||||
setHasTickScheduled: opSync.bind(null, "op_set_has_tick_scheduled"),
|
||||
evalContext: opSync.bind(null, "op_eval_context"),
|
||||
createHostObject: opSync.bind(null, "op_create_host_object"),
|
||||
encode: opSync.bind(null, "op_encode"),
|
||||
decode: opSync.bind(null, "op_decode"),
|
||||
serialize: opSync.bind(null, "op_serialize"),
|
||||
deserialize: opSync.bind(null, "op_deserialize"),
|
||||
getPromiseDetails: opSync.bind(null, "op_get_promise_details"),
|
||||
getProxyDetails: opSync.bind(null, "op_get_proxy_details"),
|
||||
isProxy: opSync.bind(null, "op_is_proxy"),
|
||||
memoryUsage: opSync.bind(null, "op_memory_usage"),
|
||||
setWasmStreamingCallback: opSync.bind(
|
||||
null,
|
||||
"op_set_wasm_streaming_callback",
|
||||
),
|
||||
abortWasmStreaming: opSync.bind(null, "op_abort_wasm_streaming"),
|
||||
destructureError: opSync.bind(null, "op_destructure_error"),
|
||||
terminate: opSync.bind(null, "op_terminate"),
|
||||
opNames: opSync.bind(null, "op_op_names"),
|
||||
});
|
||||
|
||||
ObjectAssign(globalThis.__bootstrap, { core });
|
||||
ObjectAssign(globalThis.Deno, { core });
|
||||
|
||||
// Direct bindings on `globalThis`
|
||||
ObjectAssign(globalThis, { queueMicrotask });
|
||||
setQueueMicrotask(queueMicrotask);
|
||||
})(globalThis);
|
||||
|
|
|
@ -201,7 +201,7 @@
|
|||
if (fileName && lineNumber != null && columnNumber != null) {
|
||||
return patchCallSite(
|
||||
callSite,
|
||||
core.applySourceMap({
|
||||
core.opSync("op_apply_source_map", {
|
||||
fileName,
|
||||
lineNumber,
|
||||
columnNumber,
|
||||
|
|
1159
core/bindings.rs
1159
core/bindings.rs
File diff suppressed because it is too large
Load diff
|
@ -33,25 +33,31 @@ function main() {
|
|||
108, 100
|
||||
];
|
||||
|
||||
const empty = Deno.core.encode("");
|
||||
const empty = Deno.core.opSync("op_encode", "");
|
||||
if (empty.length !== 0) throw new Error("assert");
|
||||
|
||||
assertArrayEquals(Array.from(Deno.core.encode("𝓽𝓮𝔁𝓽")), fixture1);
|
||||
assertArrayEquals(
|
||||
Array.from(Deno.core.encode("Hello \udc12\ud834 World")),
|
||||
Array.from(Deno.core.opSync("op_encode", "𝓽𝓮𝔁𝓽")),
|
||||
fixture1,
|
||||
);
|
||||
assertArrayEquals(
|
||||
Array.from(Deno.core.opSync("op_encode", "Hello \udc12\ud834 World")),
|
||||
fixture2,
|
||||
);
|
||||
|
||||
const emptyBuf = Deno.core.decode(new Uint8Array(0));
|
||||
const emptyBuf = Deno.core.opSync("op_decode", new Uint8Array(0));
|
||||
if (emptyBuf !== "") throw new Error("assert");
|
||||
|
||||
assert(Deno.core.decode(new Uint8Array(fixture1)) === "𝓽𝓮𝔁𝓽");
|
||||
assert(Deno.core.decode(new Uint8Array(fixture2)) === "Hello <20><> World");
|
||||
assert(Deno.core.opSync("op_decode", new Uint8Array(fixture1)) === "𝓽𝓮𝔁𝓽");
|
||||
assert(
|
||||
Deno.core.opSync("op_decode", new Uint8Array(fixture2)) ===
|
||||
"Hello <20><> World",
|
||||
);
|
||||
|
||||
// See https://github.com/denoland/deno/issues/6649
|
||||
let thrown = false;
|
||||
try {
|
||||
Deno.core.decode(new Uint8Array(2 ** 29));
|
||||
Deno.core.opSync("op_decode", new Uint8Array(2 ** 29));
|
||||
} catch (e) {
|
||||
thrown = true;
|
||||
assert(e instanceof RangeError);
|
||||
|
|
|
@ -13,6 +13,7 @@ mod modules;
|
|||
mod normalize_path;
|
||||
mod ops;
|
||||
mod ops_builtin;
|
||||
mod ops_builtin_v8;
|
||||
mod ops_metrics;
|
||||
mod resources;
|
||||
mod runtime;
|
||||
|
|
|
@ -108,7 +108,7 @@ impl OpResult {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct OpError {
|
||||
#[serde(rename = "$err_class_name")]
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
||||
use crate::error::format_file_name;
|
||||
use crate::error::type_error;
|
||||
use crate::include_js_files;
|
||||
|
@ -36,7 +37,9 @@ pub(crate) fn init_builtins() -> Extension {
|
|||
op_shutdown::decl(),
|
||||
op_metrics::decl(),
|
||||
op_format_file_name::decl(),
|
||||
op_is_proxy::decl(),
|
||||
])
|
||||
.ops(crate::ops_builtin_v8::init_builtins_v8())
|
||||
.build()
|
||||
}
|
||||
|
||||
|
@ -181,3 +184,8 @@ async fn op_shutdown(
|
|||
fn op_format_file_name(file_name: String) -> String {
|
||||
format_file_name(&file_name)
|
||||
}
|
||||
|
||||
#[op]
|
||||
fn op_is_proxy(value: serde_v8::Value) -> bool {
|
||||
value.v8_value.is_proxy()
|
||||
}
|
||||
|
|
788
core/ops_builtin_v8.rs
Normal file
788
core/ops_builtin_v8.rs
Normal file
|
@ -0,0 +1,788 @@
|
|||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
||||
use crate::bindings::script_origin;
|
||||
use crate::error::is_instance_of_error;
|
||||
use crate::error::range_error;
|
||||
use crate::error::type_error;
|
||||
use crate::error::JsError;
|
||||
use crate::ops_builtin::WasmStreamingResource;
|
||||
use crate::resolve_url_or_path;
|
||||
use crate::serde_v8::from_v8;
|
||||
use crate::source_map::apply_source_map as apply_source_map_;
|
||||
use crate::JsRuntime;
|
||||
use crate::OpDecl;
|
||||
use crate::ZeroCopyBuf;
|
||||
use anyhow::Error;
|
||||
use deno_ops::op;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use std::cell::RefCell;
|
||||
use v8::ValueDeserializerHelper;
|
||||
use v8::ValueSerializerHelper;
|
||||
|
||||
pub(crate) fn init_builtins_v8() -> Vec<OpDecl> {
|
||||
vec![
|
||||
op_ref_op::decl(),
|
||||
op_unref_op::decl(),
|
||||
op_set_macrotask_callback::decl(),
|
||||
op_set_next_tick_callback::decl(),
|
||||
op_set_promise_reject_callback::decl(),
|
||||
op_set_uncaught_exception_callback::decl(),
|
||||
op_run_microtasks::decl(),
|
||||
op_has_tick_scheduled::decl(),
|
||||
op_set_has_tick_scheduled::decl(),
|
||||
op_eval_context::decl(),
|
||||
op_queue_microtask::decl(),
|
||||
op_create_host_object::decl(),
|
||||
op_encode::decl(),
|
||||
op_decode::decl(),
|
||||
op_serialize::decl(),
|
||||
op_deserialize::decl(),
|
||||
op_get_promise_details::decl(),
|
||||
op_get_proxy_details::decl(),
|
||||
op_memory_usage::decl(),
|
||||
op_set_wasm_streaming_callback::decl(),
|
||||
op_abort_wasm_streaming::decl(),
|
||||
op_destructure_error::decl(),
|
||||
op_terminate::decl(),
|
||||
op_op_names::decl(),
|
||||
op_apply_source_map::decl(),
|
||||
op_set_format_exception_callback::decl(),
|
||||
]
|
||||
}
|
||||
|
||||
fn to_v8_fn(
|
||||
scope: &mut v8::HandleScope,
|
||||
value: serde_v8::Value,
|
||||
) -> Result<v8::Global<v8::Function>, Error> {
|
||||
v8::Local::<v8::Function>::try_from(value.v8_value)
|
||||
.map(|cb| v8::Global::new(scope, cb))
|
||||
.map_err(|err| type_error(err.to_string()))
|
||||
}
|
||||
|
||||
#[op(v8)]
|
||||
fn op_ref_op(scope: &mut v8::HandleScope, promise_id: i32) {
|
||||
let state_rc = JsRuntime::state(scope);
|
||||
state_rc.borrow_mut().unrefed_ops.remove(&promise_id);
|
||||
}
|
||||
|
||||
#[op(v8)]
|
||||
fn op_unref_op(scope: &mut v8::HandleScope, promise_id: i32) {
|
||||
let state_rc = JsRuntime::state(scope);
|
||||
state_rc.borrow_mut().unrefed_ops.insert(promise_id);
|
||||
}
|
||||
|
||||
#[op(v8)]
|
||||
fn op_set_macrotask_callback(
|
||||
scope: &mut v8::HandleScope,
|
||||
cb: serde_v8::Value,
|
||||
) -> Result<(), Error> {
|
||||
let cb = to_v8_fn(scope, cb)?;
|
||||
let state_rc = JsRuntime::state(scope);
|
||||
state_rc.borrow_mut().js_macrotask_cbs.push(cb);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[op(v8)]
|
||||
fn op_set_next_tick_callback(
|
||||
scope: &mut v8::HandleScope,
|
||||
cb: serde_v8::Value,
|
||||
) -> Result<(), Error> {
|
||||
let cb = to_v8_fn(scope, cb)?;
|
||||
let state_rc = JsRuntime::state(scope);
|
||||
state_rc.borrow_mut().js_nexttick_cbs.push(cb);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[op(v8)]
|
||||
fn op_set_promise_reject_callback<'a>(
|
||||
scope: &mut v8::HandleScope<'a>,
|
||||
cb: serde_v8::Value,
|
||||
) -> Result<Option<serde_v8::Value<'a>>, Error> {
|
||||
let cb = to_v8_fn(scope, cb)?;
|
||||
let state_rc = JsRuntime::state(scope);
|
||||
let old = state_rc.borrow_mut().js_promise_reject_cb.replace(cb);
|
||||
let old = old.map(|v| v8::Local::new(scope, v));
|
||||
Ok(old.map(|v| from_v8(scope, v.into()).unwrap()))
|
||||
}
|
||||
|
||||
#[op(v8)]
|
||||
fn op_set_uncaught_exception_callback<'a>(
|
||||
scope: &mut v8::HandleScope<'a>,
|
||||
cb: serde_v8::Value,
|
||||
) -> Result<Option<serde_v8::Value<'a>>, Error> {
|
||||
let cb = to_v8_fn(scope, cb)?;
|
||||
let state_rc = JsRuntime::state(scope);
|
||||
let old = state_rc.borrow_mut().js_uncaught_exception_cb.replace(cb);
|
||||
let old = old.map(|v| v8::Local::new(scope, v));
|
||||
Ok(old.map(|v| from_v8(scope, v.into()).unwrap()))
|
||||
}
|
||||
|
||||
#[op(v8)]
|
||||
fn op_run_microtasks(scope: &mut v8::HandleScope) {
|
||||
scope.perform_microtask_checkpoint();
|
||||
}
|
||||
|
||||
#[op(v8)]
|
||||
fn op_has_tick_scheduled(scope: &mut v8::HandleScope) -> bool {
|
||||
let state_rc = JsRuntime::state(scope);
|
||||
let state = state_rc.borrow();
|
||||
state.has_tick_scheduled
|
||||
}
|
||||
|
||||
#[op(v8)]
|
||||
fn op_set_has_tick_scheduled(scope: &mut v8::HandleScope, v: bool) {
|
||||
let state_rc = JsRuntime::state(scope);
|
||||
state_rc.borrow_mut().has_tick_scheduled = v;
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct EvalContextError<'s> {
|
||||
thrown: serde_v8::Value<'s>,
|
||||
is_native_error: bool,
|
||||
is_compile_error: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct EvalContextResult<'s>(
|
||||
Option<serde_v8::Value<'s>>,
|
||||
Option<EvalContextError<'s>>,
|
||||
);
|
||||
|
||||
#[op(v8)]
|
||||
fn op_eval_context<'a>(
|
||||
scope: &mut v8::HandleScope<'a>,
|
||||
source: serde_v8::Value<'a>,
|
||||
specifier: Option<String>,
|
||||
) -> Result<EvalContextResult<'a>, Error> {
|
||||
let tc_scope = &mut v8::TryCatch::new(scope);
|
||||
let source = v8::Local::<v8::String>::try_from(source.v8_value)
|
||||
.map_err(|_| type_error("Invalid source"))?;
|
||||
let specifier = match specifier {
|
||||
Some(s) => resolve_url_or_path(&s)?.to_string(),
|
||||
None => crate::DUMMY_SPECIFIER.to_string(),
|
||||
};
|
||||
let specifier = v8::String::new(tc_scope, &specifier).unwrap();
|
||||
let origin = script_origin(tc_scope, specifier);
|
||||
|
||||
let script = match v8::Script::compile(tc_scope, source, Some(&origin)) {
|
||||
Some(s) => s,
|
||||
None => {
|
||||
assert!(tc_scope.has_caught());
|
||||
let exception = tc_scope.exception().unwrap();
|
||||
return Ok(EvalContextResult(
|
||||
None,
|
||||
Some(EvalContextError {
|
||||
thrown: exception.into(),
|
||||
is_native_error: is_instance_of_error(tc_scope, exception),
|
||||
is_compile_error: true,
|
||||
}),
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
match script.run(tc_scope) {
|
||||
Some(result) => Ok(EvalContextResult(Some(result.into()), None)),
|
||||
None => {
|
||||
assert!(tc_scope.has_caught());
|
||||
let exception = tc_scope.exception().unwrap();
|
||||
Ok(EvalContextResult(
|
||||
None,
|
||||
Some(EvalContextError {
|
||||
thrown: exception.into(),
|
||||
is_native_error: is_instance_of_error(tc_scope, exception),
|
||||
is_compile_error: false,
|
||||
}),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[op(v8)]
|
||||
fn op_queue_microtask(
|
||||
scope: &mut v8::HandleScope,
|
||||
cb: serde_v8::Value,
|
||||
) -> Result<(), Error> {
|
||||
let cb = to_v8_fn(scope, cb)?;
|
||||
let cb = v8::Local::new(scope, cb);
|
||||
scope.enqueue_microtask(cb);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[op(v8)]
|
||||
fn op_create_host_object<'a>(
|
||||
scope: &mut v8::HandleScope<'a>,
|
||||
) -> serde_v8::Value<'a> {
|
||||
let template = v8::ObjectTemplate::new(scope);
|
||||
template.set_internal_field_count(1);
|
||||
let object = template.new_instance(scope).unwrap();
|
||||
from_v8(scope, object.into()).unwrap()
|
||||
}
|
||||
|
||||
#[op(v8)]
|
||||
fn op_encode<'a>(
|
||||
scope: &mut v8::HandleScope<'a>,
|
||||
text: serde_v8::Value<'a>,
|
||||
) -> Result<serde_v8::Value<'a>, Error> {
|
||||
let text = v8::Local::<v8::String>::try_from(text.v8_value)
|
||||
.map_err(|_| type_error("Invalid argument"))?;
|
||||
let text_str = serde_v8::to_utf8(text, scope);
|
||||
let bytes = text_str.into_bytes();
|
||||
let len = bytes.len();
|
||||
let backing_store =
|
||||
v8::ArrayBuffer::new_backing_store_from_vec(bytes).make_shared();
|
||||
let buffer = v8::ArrayBuffer::with_backing_store(scope, &backing_store);
|
||||
let u8array = v8::Uint8Array::new(scope, buffer, 0, len).unwrap();
|
||||
Ok((from_v8(scope, u8array.into()))?)
|
||||
}
|
||||
|
||||
#[op(v8)]
|
||||
fn op_decode<'a>(
|
||||
scope: &mut v8::HandleScope<'a>,
|
||||
zero_copy: ZeroCopyBuf,
|
||||
) -> Result<serde_v8::Value<'a>, Error> {
|
||||
let buf = &zero_copy;
|
||||
|
||||
// Strip BOM
|
||||
let buf =
|
||||
if buf.len() >= 3 && buf[0] == 0xef && buf[1] == 0xbb && buf[2] == 0xbf {
|
||||
&buf[3..]
|
||||
} else {
|
||||
buf
|
||||
};
|
||||
|
||||
// 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
|
||||
match v8::String::new_from_utf8(scope, buf, v8::NewStringType::Normal) {
|
||||
Some(text) => Ok(from_v8(scope, text.into())?),
|
||||
None => Err(range_error("string too long")),
|
||||
}
|
||||
}
|
||||
|
||||
struct SerializeDeserialize<'a> {
|
||||
host_objects: Option<v8::Local<'a, v8::Array>>,
|
||||
error_callback: Option<v8::Local<'a, v8::Function>>,
|
||||
}
|
||||
|
||||
impl<'a> v8::ValueSerializerImpl for SerializeDeserialize<'a> {
|
||||
#[allow(unused_variables)]
|
||||
fn throw_data_clone_error<'s>(
|
||||
&mut self,
|
||||
scope: &mut v8::HandleScope<'s>,
|
||||
message: v8::Local<'s, v8::String>,
|
||||
) {
|
||||
if let Some(cb) = self.error_callback {
|
||||
let scope = &mut v8::TryCatch::new(scope);
|
||||
let undefined = v8::undefined(scope).into();
|
||||
cb.call(scope, undefined, &[message.into()]);
|
||||
if scope.has_caught() || scope.has_terminated() {
|
||||
scope.rethrow();
|
||||
return;
|
||||
};
|
||||
}
|
||||
let error = v8::Exception::type_error(scope, message);
|
||||
scope.throw_exception(error);
|
||||
}
|
||||
|
||||
fn get_shared_array_buffer_id<'s>(
|
||||
&mut self,
|
||||
scope: &mut v8::HandleScope<'s>,
|
||||
shared_array_buffer: v8::Local<'s, v8::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
|
||||
}
|
||||
}
|
||||
|
||||
fn get_wasm_module_transfer_id(
|
||||
&mut self,
|
||||
scope: &mut v8::HandleScope<'_>,
|
||||
module: v8::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
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> v8::ValueDeserializerImpl for SerializeDeserialize<'a> {
|
||||
fn get_shared_array_buffer_from_id<'s>(
|
||||
&mut self,
|
||||
scope: &mut v8::HandleScope<'s>,
|
||||
transfer_id: u32,
|
||||
) -> Option<v8::Local<'s, v8::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
|
||||
}
|
||||
}
|
||||
|
||||
fn get_wasm_module_from_id<'s>(
|
||||
&mut self,
|
||||
scope: &mut v8::HandleScope<'s>,
|
||||
clone_id: u32,
|
||||
) -> Option<v8::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
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct SerializeDeserializeOptions<'a> {
|
||||
host_objects: Option<serde_v8::Value<'a>>,
|
||||
transferred_array_buffers: Option<serde_v8::Value<'a>>,
|
||||
}
|
||||
|
||||
#[op(v8)]
|
||||
fn op_serialize(
|
||||
scope: &mut v8::HandleScope,
|
||||
value: serde_v8::Value,
|
||||
options: Option<SerializeDeserializeOptions>,
|
||||
error_callback: Option<serde_v8::Value>,
|
||||
) -> Result<ZeroCopyBuf, Error> {
|
||||
let options = options.unwrap_or_default();
|
||||
let error_callback = match error_callback {
|
||||
Some(cb) => Some(
|
||||
v8::Local::<v8::Function>::try_from(cb.v8_value)
|
||||
.map_err(|_| type_error("Invalid error callback"))?,
|
||||
),
|
||||
None => None,
|
||||
};
|
||||
let host_objects = match options.host_objects {
|
||||
Some(value) => Some(
|
||||
v8::Local::<v8::Array>::try_from(value.v8_value)
|
||||
.map_err(|_| type_error("hostObjects not an array"))?,
|
||||
),
|
||||
None => None,
|
||||
};
|
||||
let transferred_array_buffers = match options.transferred_array_buffers {
|
||||
Some(value) => Some(
|
||||
v8::Local::<v8::Array>::try_from(value.v8_value)
|
||||
.map_err(|_| type_error("transferredArrayBuffers not an array"))?,
|
||||
),
|
||||
None => None,
|
||||
};
|
||||
|
||||
let serialize_deserialize = Box::new(SerializeDeserialize {
|
||||
host_objects,
|
||||
error_callback,
|
||||
});
|
||||
let mut value_serializer =
|
||||
v8::ValueSerializer::new(scope, serialize_deserialize);
|
||||
value_serializer.write_header();
|
||||
|
||||
if let Some(transferred_array_buffers) = transferred_array_buffers {
|
||||
let state_rc = JsRuntime::state(scope);
|
||||
let state = state_rc.borrow_mut();
|
||||
for i in 0..transferred_array_buffers.length() {
|
||||
let i = v8::Number::new(scope, i as f64).into();
|
||||
let buf = transferred_array_buffers.get(scope, i).unwrap();
|
||||
let buf = v8::Local::<v8::ArrayBuffer>::try_from(buf).map_err(|_| {
|
||||
type_error("item in transferredArrayBuffers not an ArrayBuffer")
|
||||
})?;
|
||||
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() {
|
||||
return Err(type_error(
|
||||
"item in transferredArrayBuffers is not transferable",
|
||||
));
|
||||
}
|
||||
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();
|
||||
transferred_array_buffers.set(scope, i, id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let scope = &mut v8::TryCatch::new(scope);
|
||||
let ret =
|
||||
value_serializer.write_value(scope.get_current_context(), value.v8_value);
|
||||
if scope.has_caught() || scope.has_terminated() {
|
||||
scope.rethrow();
|
||||
Err(type_error("unreachable"))
|
||||
} else if let Some(true) = ret {
|
||||
let vector = value_serializer.release();
|
||||
Ok(vector.into())
|
||||
} else {
|
||||
Err(type_error("Failed to serialize response"))
|
||||
}
|
||||
}
|
||||
|
||||
#[op(v8)]
|
||||
fn op_deserialize<'a>(
|
||||
scope: &mut v8::HandleScope<'a>,
|
||||
zero_copy: ZeroCopyBuf,
|
||||
options: Option<SerializeDeserializeOptions>,
|
||||
) -> Result<serde_v8::Value<'a>, Error> {
|
||||
let options = options.unwrap_or_default();
|
||||
let host_objects = match options.host_objects {
|
||||
Some(value) => Some(
|
||||
v8::Local::<v8::Array>::try_from(value.v8_value)
|
||||
.map_err(|_| type_error("hostObjects not an array"))?,
|
||||
),
|
||||
None => None,
|
||||
};
|
||||
let transferred_array_buffers = match options.transferred_array_buffers {
|
||||
Some(value) => Some(
|
||||
v8::Local::<v8::Array>::try_from(value.v8_value)
|
||||
.map_err(|_| type_error("transferredArrayBuffers not an array"))?,
|
||||
),
|
||||
None => None,
|
||||
};
|
||||
|
||||
let serialize_deserialize = Box::new(SerializeDeserialize {
|
||||
host_objects,
|
||||
error_callback: None,
|
||||
});
|
||||
let mut value_deserializer =
|
||||
v8::ValueDeserializer::new(scope, serialize_deserialize, &zero_copy);
|
||||
let parsed_header = value_deserializer
|
||||
.read_header(scope.get_current_context())
|
||||
.unwrap_or_default();
|
||||
if !parsed_header {
|
||||
return Err(range_error("could not deserialize value"));
|
||||
}
|
||||
|
||||
if let Some(transferred_array_buffers) = transferred_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..transferred_array_buffers.length() {
|
||||
let i = v8::Number::new(scope, i as f64).into();
|
||||
let id_val = transferred_array_buffers.get(scope, i).unwrap();
|
||||
let id = match id_val.number_value(scope) {
|
||||
Some(id) => id as u32,
|
||||
None => {
|
||||
return Err(type_error(
|
||||
"item in transferredArrayBuffers not number",
|
||||
))
|
||||
}
|
||||
};
|
||||
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);
|
||||
transferred_array_buffers.set(scope, id_val, array_buffer.into());
|
||||
} else {
|
||||
return Err(type_error(
|
||||
"transferred array buffer not present in shared_array_buffer_store",
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let value = value_deserializer.read_value(scope.get_current_context());
|
||||
match value {
|
||||
Some(deserialized) => Ok(deserialized.into()),
|
||||
None => Err(range_error("could not deserialize value")),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct PromiseDetails<'s>(u32, Option<serde_v8::Value<'s>>);
|
||||
|
||||
#[op(v8)]
|
||||
fn op_get_promise_details<'a>(
|
||||
scope: &mut v8::HandleScope<'a>,
|
||||
promise: serde_v8::Value<'a>,
|
||||
) -> Result<PromiseDetails<'a>, Error> {
|
||||
let promise = v8::Local::<v8::Promise>::try_from(promise.v8_value)
|
||||
.map_err(|_| type_error("Invalid argument"))?;
|
||||
match promise.state() {
|
||||
v8::PromiseState::Pending => Ok(PromiseDetails(0, None)),
|
||||
v8::PromiseState::Fulfilled => {
|
||||
Ok(PromiseDetails(1, Some(promise.result(scope).into())))
|
||||
}
|
||||
v8::PromiseState::Rejected => {
|
||||
Ok(PromiseDetails(2, Some(promise.result(scope).into())))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
#[op(v8)]
|
||||
fn op_get_proxy_details<'a>(
|
||||
scope: &mut v8::HandleScope<'a>,
|
||||
proxy: serde_v8::Value<'a>,
|
||||
) -> Option<(serde_v8::Value<'a>, serde_v8::Value<'a>)> {
|
||||
let proxy = match v8::Local::<v8::Proxy>::try_from(proxy.v8_value) {
|
||||
Ok(proxy) => proxy,
|
||||
Err(_) => return None,
|
||||
};
|
||||
let target = proxy.get_target(scope);
|
||||
let handler = proxy.get_handler(scope);
|
||||
Some((target.into(), handler.into()))
|
||||
}
|
||||
|
||||
// HeapStats stores values from a isolate.get_heap_statistics() call
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
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,
|
||||
}
|
||||
|
||||
#[op(v8)]
|
||||
fn op_memory_usage(scope: &mut v8::HandleScope) -> MemoryUsage {
|
||||
let mut s = v8::HeapStatistics::default();
|
||||
scope.get_heap_statistics(&mut s);
|
||||
MemoryUsage {
|
||||
rss: s.total_physical_size(),
|
||||
heap_total: s.total_heap_size(),
|
||||
heap_used: s.used_heap_size(),
|
||||
external: s.external_memory(),
|
||||
}
|
||||
}
|
||||
|
||||
#[op(v8)]
|
||||
fn op_set_wasm_streaming_callback(
|
||||
scope: &mut v8::HandleScope,
|
||||
cb: serde_v8::Value,
|
||||
) -> Result<(), Error> {
|
||||
let cb = to_v8_fn(scope, cb)?;
|
||||
let state_rc = JsRuntime::state(scope);
|
||||
let mut state = state_rc.borrow_mut();
|
||||
// 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 state.js_wasm_streaming_cb.is_some() {
|
||||
return Err(type_error("op_set_wasm_streaming_callback already called"));
|
||||
}
|
||||
state.js_wasm_streaming_cb = Some(cb);
|
||||
|
||||
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
|
||||
.open(scope)
|
||||
.call(scope, undefined.into(), &[arg, rid]);
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[op(v8)]
|
||||
fn op_abort_wasm_streaming(
|
||||
scope: &mut v8::HandleScope,
|
||||
rid: u32,
|
||||
error: serde_v8::Value,
|
||||
) -> Result<(), Error> {
|
||||
let wasm_streaming = {
|
||||
let state_rc = JsRuntime::state(scope);
|
||||
let state = state_rc.borrow();
|
||||
let wsr = state
|
||||
.op_state
|
||||
.borrow_mut()
|
||||
.resource_table
|
||||
.take::<WasmStreamingResource>(rid)?;
|
||||
wsr
|
||||
};
|
||||
|
||||
// At this point there are no clones of Rc<WasmStreamingResource> on the
|
||||
// resource table, and no one should own a reference because we're never
|
||||
// cloning them. So we can be sure `wasm_streaming` is the only reference.
|
||||
if let Ok(wsr) = std::rc::Rc::try_unwrap(wasm_streaming) {
|
||||
// NOTE: v8::WasmStreaming::abort can't be called while `state` is borrowed;
|
||||
// see https://github.com/denoland/deno/issues/13917
|
||||
wsr.0.into_inner().abort(Some(error.v8_value));
|
||||
} else {
|
||||
panic!("Couldn't consume WasmStreamingResource.");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[op(v8)]
|
||||
fn op_destructure_error(
|
||||
scope: &mut v8::HandleScope,
|
||||
error: serde_v8::Value,
|
||||
) -> JsError {
|
||||
JsError::from_v8_exception(scope, error.v8_value)
|
||||
}
|
||||
|
||||
#[op(v8)]
|
||||
fn op_terminate(scope: &mut v8::HandleScope, exception: serde_v8::Value) {
|
||||
let state_rc = JsRuntime::state(scope);
|
||||
let mut state = state_rc.borrow_mut();
|
||||
state.explicit_terminate_exception =
|
||||
Some(v8::Global::new(scope, exception.v8_value));
|
||||
scope.terminate_execution();
|
||||
}
|
||||
|
||||
#[op(v8)]
|
||||
fn op_op_names(scope: &mut v8::HandleScope) -> Vec<String> {
|
||||
let state_rc = JsRuntime::state(scope);
|
||||
let state = state_rc.borrow();
|
||||
state
|
||||
.op_ctxs
|
||||
.iter()
|
||||
.map(|o| o.decl.name.to_string())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct Location {
|
||||
file_name: String,
|
||||
line_number: u32,
|
||||
column_number: u32,
|
||||
}
|
||||
|
||||
#[op(v8)]
|
||||
fn op_apply_source_map(
|
||||
scope: &mut v8::HandleScope,
|
||||
location: Location,
|
||||
) -> Result<Location, Error> {
|
||||
let state_rc = JsRuntime::state(scope);
|
||||
let state = state_rc.borrow();
|
||||
if let Some(source_map_getter) = &state.source_map_getter {
|
||||
let mut location = location;
|
||||
let (f, l, c, _) = apply_source_map_(
|
||||
location.file_name,
|
||||
location.line_number.into(),
|
||||
location.column_number.into(),
|
||||
&mut Default::default(),
|
||||
source_map_getter.as_ref(),
|
||||
);
|
||||
location.file_name = f;
|
||||
location.line_number = l as u32;
|
||||
location.column_number = c as u32;
|
||||
Ok(location)
|
||||
} else {
|
||||
Ok(location)
|
||||
}
|
||||
}
|
||||
|
||||
/// Set a callback which formats exception messages as stored in
|
||||
/// `JsError::exception_message`. The callback is passed the error value and
|
||||
/// should return a string or `null`. If no callback is set or the callback
|
||||
/// returns `null`, the built-in default formatting will be used.
|
||||
#[op(v8)]
|
||||
fn op_set_format_exception_callback<'a>(
|
||||
scope: &mut v8::HandleScope<'a>,
|
||||
cb: serde_v8::Value<'a>,
|
||||
) -> Result<Option<serde_v8::Value<'a>>, Error> {
|
||||
let cb = to_v8_fn(scope, cb)?;
|
||||
let state_rc = JsRuntime::state(scope);
|
||||
let old = state_rc.borrow_mut().js_format_exception_cb.replace(cb);
|
||||
let old = old.map(|v| v8::Local::new(scope, v));
|
||||
Ok(old.map(|v| from_v8(scope, v.into()).unwrap()))
|
||||
}
|
|
@ -1990,6 +1990,9 @@ pub mod tests {
|
|||
.build();
|
||||
let mut runtime = JsRuntime::new(RuntimeOptions {
|
||||
extensions: vec![ext],
|
||||
get_error_class_fn: Some(&|error| {
|
||||
crate::error::get_custom_error_class(error).unwrap()
|
||||
}),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
|
@ -2068,8 +2071,8 @@ pub mod tests {
|
|||
.execute_script(
|
||||
"filename.js",
|
||||
r#"
|
||||
Deno.core.unrefOp(p1[promiseIdSymbol]);
|
||||
Deno.core.unrefOp(p2[promiseIdSymbol]);
|
||||
Deno.core.opSync("op_unref_op", p1[promiseIdSymbol]);
|
||||
Deno.core.opSync("op_unref_op", p2[promiseIdSymbol]);
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
@ -2084,8 +2087,8 @@ pub mod tests {
|
|||
.execute_script(
|
||||
"filename.js",
|
||||
r#"
|
||||
Deno.core.refOp(p1[promiseIdSymbol]);
|
||||
Deno.core.refOp(p2[promiseIdSymbol]);
|
||||
Deno.core.opSync("op_ref_op", p1[promiseIdSymbol]);
|
||||
Deno.core.opSync("op_ref_op", p2[promiseIdSymbol]);
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
@ -2790,10 +2793,9 @@ assertEquals(1, notify_return_value);
|
|||
"is_proxy.js",
|
||||
r#"
|
||||
(function () {
|
||||
const { isProxy } = Deno.core;
|
||||
const o = { a: 1, b: 2};
|
||||
const p = new Proxy(o, {});
|
||||
return isProxy(p) && !isProxy(o) && !isProxy(42);
|
||||
return Deno.core.opSync("op_is_proxy", p) && !Deno.core.opSync("op_is_proxy", o) && !Deno.core.opSync("op_is_proxy", 42);
|
||||
})()
|
||||
"#,
|
||||
)
|
||||
|
@ -2803,20 +2805,6 @@ assertEquals(1, notify_return_value);
|
|||
assert!(all_true.is_true());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_binding_names() {
|
||||
let mut runtime = JsRuntime::new(RuntimeOptions::default());
|
||||
let all_true: v8::Global<v8::Value> = runtime
|
||||
.execute_script(
|
||||
"binding_names.js",
|
||||
"Deno.core.encode.toString() === 'function encode() { [native code] }'",
|
||||
)
|
||||
.unwrap();
|
||||
let mut scope = runtime.handle_scope();
|
||||
let all_true = v8::Local::<v8::Value>::new(&mut scope, &all_true);
|
||||
assert!(all_true.is_true());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_async_opstate_borrow() {
|
||||
struct InnerState(u64);
|
||||
|
@ -2884,16 +2872,16 @@ assertEquals(1, notify_return_value);
|
|||
r#"
|
||||
(async function () {
|
||||
const results = [];
|
||||
Deno.core.setMacrotaskCallback(() => {
|
||||
Deno.core.opSync("op_set_macrotask_callback", () => {
|
||||
results.push("macrotask");
|
||||
return true;
|
||||
});
|
||||
Deno.core.setNextTickCallback(() => {
|
||||
Deno.core.opSync("op_set_next_tick_callback", () => {
|
||||
results.push("nextTick");
|
||||
Deno.core.setHasTickScheduled(false);
|
||||
Deno.core.opSync("op_set_has_tick_scheduled", false);
|
||||
});
|
||||
|
||||
Deno.core.setHasTickScheduled(true);
|
||||
Deno.core.opSync("op_set_has_tick_scheduled", true);
|
||||
await Deno.core.opAsync('op_async_sleep');
|
||||
if (results[0] != "nextTick") {
|
||||
throw new Error(`expected nextTick, got: ${results[0]}`);
|
||||
|
@ -2916,10 +2904,10 @@ assertEquals(1, notify_return_value);
|
|||
.execute_script(
|
||||
"multiple_macrotasks_and_nextticks.js",
|
||||
r#"
|
||||
Deno.core.setMacrotaskCallback(() => { return true; });
|
||||
Deno.core.setMacrotaskCallback(() => { return true; });
|
||||
Deno.core.setNextTickCallback(() => {});
|
||||
Deno.core.setNextTickCallback(() => {});
|
||||
Deno.core.opSync("op_set_macrotask_callback", () => { return true; });
|
||||
Deno.core.opSync("op_set_macrotask_callback", () => { return true; });
|
||||
Deno.core.opSync("op_set_next_tick_callback", () => {});
|
||||
Deno.core.opSync("op_set_next_tick_callback", () => {});
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
@ -2962,12 +2950,12 @@ assertEquals(1, notify_return_value);
|
|||
.execute_script(
|
||||
"has_tick_scheduled.js",
|
||||
r#"
|
||||
Deno.core.setMacrotaskCallback(() => {
|
||||
Deno.core.opSync("op_set_macrotask_callback", () => {
|
||||
Deno.core.opSync("op_macrotask");
|
||||
return true; // We're done.
|
||||
});
|
||||
Deno.core.setNextTickCallback(() => Deno.core.opSync("op_next_tick"));
|
||||
Deno.core.setHasTickScheduled(true);
|
||||
Deno.core.opSync("op_set_next_tick_callback", () => Deno.core.opSync("op_next_tick"));
|
||||
Deno.core.opSync("op_set_has_tick_scheduled", true);
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
@ -3103,7 +3091,7 @@ assertEquals(1, notify_return_value);
|
|||
"promise_reject_callback.js",
|
||||
r#"
|
||||
// Note: |promise| is not the promise created below, it's a child.
|
||||
Deno.core.setPromiseRejectCallback((type, promise, reason) => {
|
||||
Deno.core.opSync("op_set_promise_reject_callback", (type, promise, reason) => {
|
||||
if (type !== /* PromiseRejectWithNoHandler */ 0) {
|
||||
throw Error("unexpected type: " + type);
|
||||
}
|
||||
|
@ -3114,7 +3102,7 @@ assertEquals(1, notify_return_value);
|
|||
throw Error("promiseReject"); // Triggers uncaughtException handler.
|
||||
});
|
||||
|
||||
Deno.core.setUncaughtExceptionCallback((err) => {
|
||||
Deno.core.opSync("op_set_uncaught_exception_callback", (err) => {
|
||||
if (err.message !== "promiseReject") throw err;
|
||||
Deno.core.opSync("op_uncaught_exception");
|
||||
});
|
||||
|
@ -3133,13 +3121,13 @@ assertEquals(1, notify_return_value);
|
|||
"promise_reject_callback.js",
|
||||
r#"
|
||||
{
|
||||
const prev = Deno.core.setPromiseRejectCallback((...args) => {
|
||||
const prev = Deno.core.opSync("op_set_promise_reject_callback", (...args) => {
|
||||
prev(...args);
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
const prev = Deno.core.setUncaughtExceptionCallback((...args) => {
|
||||
const prev = Deno.core.opSync("op_set_uncaught_exception_callback", (...args) => {
|
||||
prev(...args);
|
||||
throw Error("fail");
|
||||
});
|
||||
|
|
|
@ -20,9 +20,15 @@ function assertArrayEquals(a1, a2) {
|
|||
function main() {
|
||||
const emptyString = "";
|
||||
const emptyStringSerialized = [255, 15, 34, 0];
|
||||
assertArrayEquals(Deno.core.serialize(emptyString), emptyStringSerialized);
|
||||
assertArrayEquals(
|
||||
Deno.core.opSync("op_serialize", emptyString),
|
||||
emptyStringSerialized,
|
||||
);
|
||||
assert(
|
||||
Deno.core.deserialize(new Uint8Array(emptyStringSerialized)) ===
|
||||
Deno.core.opSync(
|
||||
"op_deserialize",
|
||||
new Uint8Array(emptyStringSerialized),
|
||||
) ===
|
||||
emptyString,
|
||||
);
|
||||
|
||||
|
@ -33,12 +39,13 @@ function main() {
|
|||
34, 1, 97, 48, 95, 36, 0, 4,
|
||||
];
|
||||
assertArrayEquals(
|
||||
Deno.core.serialize(primitiveValueArray),
|
||||
Deno.core.opSync("op_serialize", primitiveValueArray),
|
||||
primitiveValueArraySerialized,
|
||||
);
|
||||
|
||||
assertArrayEquals(
|
||||
Deno.core.deserialize(
|
||||
Deno.core.opSync(
|
||||
"op_deserialize",
|
||||
new Uint8Array(primitiveValueArraySerialized),
|
||||
),
|
||||
primitiveValueArray,
|
||||
|
@ -56,11 +63,12 @@ function main() {
|
|||
];
|
||||
|
||||
assertArrayEquals(
|
||||
Deno.core.serialize(circularObject),
|
||||
Deno.core.opSync("op_serialize", circularObject),
|
||||
circularObjectSerialized,
|
||||
);
|
||||
|
||||
const deserializedCircularObject = Deno.core.deserialize(
|
||||
const deserializedCircularObject = Deno.core.opSync(
|
||||
"op_deserialize",
|
||||
new Uint8Array(circularObjectSerialized),
|
||||
);
|
||||
assert(deserializedCircularObject.test == deserializedCircularObject);
|
||||
|
|
|
@ -205,7 +205,7 @@
|
|||
const transferables = [];
|
||||
const hostObjects = [];
|
||||
const arrayBufferIdsInTransferables = [];
|
||||
const transferedArrayBuffers = [];
|
||||
const transferredArrayBuffers = [];
|
||||
|
||||
for (const transferable of messageData.transferables) {
|
||||
switch (transferable.kind) {
|
||||
|
@ -216,7 +216,7 @@
|
|||
break;
|
||||
}
|
||||
case "arrayBuffer": {
|
||||
ArrayPrototypePush(transferedArrayBuffers, transferable.data);
|
||||
ArrayPrototypePush(transferredArrayBuffers, transferable.data);
|
||||
const i = ArrayPrototypePush(transferables, null);
|
||||
ArrayPrototypePush(arrayBufferIdsInTransferables, i);
|
||||
break;
|
||||
|
@ -228,12 +228,12 @@
|
|||
|
||||
const data = core.deserialize(messageData.data, {
|
||||
hostObjects,
|
||||
transferedArrayBuffers,
|
||||
transferredArrayBuffers,
|
||||
});
|
||||
|
||||
for (const i in arrayBufferIdsInTransferables) {
|
||||
const id = arrayBufferIdsInTransferables[i];
|
||||
transferables[id] = transferedArrayBuffers[i];
|
||||
transferables[id] = transferredArrayBuffers[i];
|
||||
}
|
||||
|
||||
return [data, transferables];
|
||||
|
@ -247,12 +247,12 @@
|
|||
* @returns {globalThis.__bootstrap.messagePort.MessageData}
|
||||
*/
|
||||
function serializeJsMessageData(data, transferables) {
|
||||
const transferedArrayBuffers = ArrayPrototypeFilter(
|
||||
const transferredArrayBuffers = ArrayPrototypeFilter(
|
||||
transferables,
|
||||
(a) => ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, a),
|
||||
);
|
||||
|
||||
for (const arrayBuffer of transferedArrayBuffers) {
|
||||
for (const arrayBuffer of transferredArrayBuffers) {
|
||||
// This is hacky with both false positives and false negatives for
|
||||
// detecting detached array buffers. V8 needs to add a way to tell if a
|
||||
// buffer is detached or not.
|
||||
|
@ -270,7 +270,7 @@
|
|||
transferables,
|
||||
(a) => ObjectPrototypeIsPrototypeOf(MessagePortPrototype, a),
|
||||
),
|
||||
transferedArrayBuffers,
|
||||
transferredArrayBuffers,
|
||||
}, (err) => {
|
||||
throw new DOMException(err, "DataCloneError");
|
||||
});
|
||||
|
@ -299,7 +299,7 @@
|
|||
) {
|
||||
ArrayPrototypePush(serializedTransferables, {
|
||||
kind: "arrayBuffer",
|
||||
data: transferedArrayBuffers[arrayBufferI],
|
||||
data: transferredArrayBuffers[arrayBufferI],
|
||||
});
|
||||
arrayBufferI++;
|
||||
} else {
|
||||
|
|
2
ext/web/lib.deno_web.d.ts
vendored
2
ext/web/lib.deno_web.d.ts
vendored
|
@ -730,7 +730,7 @@ declare class MessageEvent<T = any> extends Event {
|
|||
*/
|
||||
readonly lastEventId: string;
|
||||
/**
|
||||
* Returns transfered ports.
|
||||
* Returns transferred ports.
|
||||
*/
|
||||
readonly ports: ReadonlyArray<MessagePort>;
|
||||
constructor(type: string, eventInitDict?: MessageEventInit);
|
||||
|
|
21
ops/lib.rs
21
ops/lib.rs
|
@ -6,7 +6,10 @@ use proc_macro_crate::crate_name;
|
|||
use proc_macro_crate::FoundCrate;
|
||||
use quote::quote;
|
||||
use quote::ToTokens;
|
||||
use syn::punctuated::Punctuated;
|
||||
use syn::token::Comma;
|
||||
use syn::FnArg;
|
||||
use syn::GenericParam;
|
||||
use syn::Ident;
|
||||
|
||||
// Identifier to the `deno_core` crate.
|
||||
|
@ -71,7 +74,7 @@ pub fn op(attr: TokenStream, item: TokenStream) -> TokenStream {
|
|||
let func = syn::parse::<syn::ItemFn>(item).expect("expected a function");
|
||||
let name = &func.sig.ident;
|
||||
let generics = &func.sig.generics;
|
||||
let type_params = &func.sig.generics.params;
|
||||
let type_params = exclude_lifetime_params(&func.sig.generics.params);
|
||||
let where_clause = &func.sig.generics.where_clause;
|
||||
|
||||
// Preserve the original func as op_foo::call()
|
||||
|
@ -152,7 +155,7 @@ fn codegen_v8_async(
|
|||
};
|
||||
let rust_i0 = if uses_opstate { 1 } else { 0 };
|
||||
let (arg_decls, args_tail) = codegen_args(core, f, rust_i0, 1);
|
||||
let type_params = &f.sig.generics.params;
|
||||
let type_params = exclude_lifetime_params(&f.sig.generics.params);
|
||||
|
||||
let result_wrapper = match is_result(&f.sig.output) {
|
||||
true => quote! {},
|
||||
|
@ -234,7 +237,7 @@ fn codegen_v8_sync(
|
|||
|
||||
let (arg_decls, args_tail) = codegen_args(core, f, rust_i0, 0);
|
||||
let ret = codegen_sync_ret(core, &f.sig.output);
|
||||
let type_params = &f.sig.generics.params;
|
||||
let type_params = exclude_lifetime_params(&f.sig.generics.params);
|
||||
|
||||
quote! {
|
||||
// SAFETY: #core guarantees args.data() is a v8 External pointing to an OpCtx for the isolates lifetime
|
||||
|
@ -379,9 +382,21 @@ fn is_rc_refcell_opstate(arg: &syn::FnArg) -> bool {
|
|||
|
||||
fn is_handle_scope(arg: &syn::FnArg) -> bool {
|
||||
tokens(arg).ends_with(": & mut v8 :: HandleScope")
|
||||
|| tokens(arg).ends_with(": & mut v8 :: HandleScope < 'a >")
|
||||
|| tokens(arg).ends_with(": & mut deno_core :: v8 :: HandleScope")
|
||||
|| tokens(arg).ends_with(": & mut deno_core :: v8 :: HandleScope < 'a >")
|
||||
}
|
||||
|
||||
fn tokens(x: impl ToTokens) -> String {
|
||||
x.to_token_stream().to_string()
|
||||
}
|
||||
|
||||
fn exclude_lifetime_params(
|
||||
generic_params: &Punctuated<GenericParam, Comma>,
|
||||
) -> Punctuated<GenericParam, Comma> {
|
||||
generic_params
|
||||
.iter()
|
||||
.filter(|t| !tokens(t).starts_with('\''))
|
||||
.cloned()
|
||||
.collect::<Punctuated<GenericParam, Comma>>()
|
||||
}
|
||||
|
|
|
@ -231,7 +231,7 @@ delete Object.prototype.__proto__;
|
|||
function runtimeStart(runtimeOptions, source) {
|
||||
core.setMacrotaskCallback(timers.handleTimerMacrotask);
|
||||
core.setWasmStreamingCallback(fetch.handleWasmStreaming);
|
||||
core.setFormatExceptionCallback(formatException);
|
||||
core.opSync("op_set_format_exception_callback", formatException);
|
||||
version.setVersions(
|
||||
runtimeOptions.denoVersion,
|
||||
runtimeOptions.v8Version,
|
||||
|
|
Loading…
Reference in a new issue