1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

refactor(core): remove ops from Deno.core.ops that are disabled (#18793)

This commit changes how "disabled" ops behave. Instead of using "void"
functions under the hood, they now explicitly throw errors saying
that a given op doesn't exist.
This commit is contained in:
Bartek Iwańczuk 2023-04-21 15:22:45 +02:00 committed by GitHub
parent 4e944dea1d
commit 1d447cb7c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 30 deletions

View file

@ -160,30 +160,41 @@ pub(crate) fn initialize_context<'s>(
if matches!(snapshot_options, SnapshotOptions::Load) {
// Only register ops that have `force_registration` flag set to true,
// the remaining ones should already be in the snapshot.
for op_ctx in op_ctxs
.iter()
.filter(|op_ctx| op_ctx.decl.force_registration)
{
add_op_to_deno_core_ops(scope, ops_obj, op_ctx);
// the remaining ones should already be in the snapshot. Ignore ops that
// are disabled.
for op_ctx in op_ctxs {
if op_ctx.decl.enabled {
if op_ctx.decl.force_registration {
add_op_to_deno_core_ops(scope, ops_obj, op_ctx);
}
} else {
delete_op_from_deno_core_ops(scope, ops_obj, op_ctx)
}
}
} else if matches!(snapshot_options, SnapshotOptions::CreateFromExisting) {
// Register all ops, probing for which ones are already registered.
// Register all enabled ops, probing for which ones are already registered.
for op_ctx in op_ctxs {
let key = v8::String::new_external_onebyte_static(
scope,
op_ctx.decl.name.as_bytes(),
)
.unwrap();
if ops_obj.get(scope, key.into()).is_some() {
continue;
if op_ctx.decl.enabled {
if ops_obj.get(scope, key.into()).is_some() {
continue;
}
add_op_to_deno_core_ops(scope, ops_obj, op_ctx);
} else {
delete_op_from_deno_core_ops(scope, ops_obj, op_ctx)
}
add_op_to_deno_core_ops(scope, ops_obj, op_ctx);
}
} else {
// In other cases register all ops unconditionally.
// In other cases register all ops enabled unconditionally.
for op_ctx in op_ctxs {
add_op_to_deno_core_ops(scope, ops_obj, op_ctx);
if op_ctx.decl.enabled {
add_op_to_deno_core_ops(scope, ops_obj, op_ctx);
}
}
}
@ -203,6 +214,17 @@ fn set_func(
obj.set(scope, key.into(), val.into());
}
fn delete_op_from_deno_core_ops(
scope: &mut v8::HandleScope<'_>,
obj: v8::Local<v8::Object>,
op_ctx: &OpCtx,
) {
let key =
v8::String::new_external_onebyte_static(scope, op_ctx.decl.name.as_bytes())
.unwrap();
obj.delete(scope, key.into());
}
fn add_op_to_deno_core_ops(
scope: &mut v8::HandleScope<'_>,
obj: v8::Local<v8::Object>,

View file

@ -15,8 +15,6 @@ use crate::modules::ModuleId;
use crate::modules::ModuleLoadId;
use crate::modules::ModuleLoader;
use crate::modules::ModuleMap;
use crate::op_void_async;
use crate::op_void_sync;
use crate::ops::*;
use crate::realm::ContextState;
use crate::realm::JsRealm;
@ -773,16 +771,6 @@ impl JsRuntime {
name: d.name,
..macroware(d)
})
.map(|op| match op.enabled {
true => op,
false => OpDecl {
v8_fn_ptr: match op.is_async {
true => op_void_async::v8_fn_ptr as _,
false => op_void_sync::v8_fn_ptr as _,
},
..op
},
})
.collect()
}
@ -4223,11 +4211,12 @@ Deno.core.opAsync("op_async_serialize_object_with_numbers_as_keys", {
extensions: vec![test_ext::init_ops()],
..Default::default()
});
let r = runtime
let err = runtime
.execute_script_static("test.js", "Deno.core.ops.op_foo()")
.unwrap();
let scope = &mut runtime.handle_scope();
assert!(r.open(scope).is_undefined());
.unwrap_err();
assert!(err
.to_string()
.contains("TypeError: Deno.core.ops.op_foo is not a function"));
}
#[test]
@ -4327,7 +4316,7 @@ Deno.core.opAsync("op_async_serialize_object_with_numbers_as_keys", {
if (Deno.core.ops.op_foo() !== 42) {
throw new Error("Exptected op_foo() === 42");
}
if (Deno.core.ops.op_bar() !== undefined) {
if (typeof Deno.core.ops.op_bar !== "undefined") {
throw new Error("Expected op_bar to be disabled")
}
"#,

View file

@ -57,7 +57,10 @@ deno_core::extension!(
deno_os_worker,
ops_fn = deno_ops,
middleware = |op| match op.name {
"op_exit" | "op_set_exit_code" => op.disable(),
"op_exit" | "op_set_exit_code" => deno_core::OpDecl {
v8_fn_ptr: deno_core::op_void_sync::v8_fn_ptr as _,
..op
},
_ => op,
},
customizer = |ext: &mut deno_core::ExtensionBuilder| {