diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2022-03-22 16:39:58 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-22 16:39:58 +0100 |
commit | f81334d5bd1df723b1543dd906bd5672808bc75f (patch) | |
tree | f0cfe6da3c6078170a714d52272243ca647e119d /core/runtime.rs | |
parent | c9817c335c23a5b85d6262b0e42abef19075c975 (diff) |
feat(core): disableable extensions & ops (#14063)
Streamlines a common middleware pattern and provides foundations for avoiding variably sized v8::ExternalReferences & enabling fully monomorphic op callpaths
Diffstat (limited to 'core/runtime.rs')
-rw-r--r-- | core/runtime.rs | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/core/runtime.rs b/core/runtime.rs index 8c781bf24..c2c2f4b92 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -14,6 +14,8 @@ use crate::modules::ModuleLoadId; use crate::modules::ModuleLoader; use crate::modules::ModuleMap; use crate::modules::NoopModuleLoader; +use crate::op_void_async; +use crate::op_void_sync; use crate::ops::*; use crate::Extension; use crate::OpMiddlewareFn; @@ -472,7 +474,7 @@ impl JsRuntime { // macroware wraps an opfn in all the middleware let macroware = move |d| middleware.iter().fold(d, |d, m| m(d)); - // Flatten ops & apply middlware + // Flatten ops, apply middlware & override disabled ops extensions .iter_mut() .filter_map(|e| e.init_ops()) @@ -481,6 +483,16 @@ 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(), + false => op_void_sync::v8_fn_ptr(), + }, + ..op + }, + }) .collect() } @@ -3019,4 +3031,25 @@ assertEquals(1, notify_return_value); let scope = &mut runtime.handle_scope(); assert_eq!(r.open(scope).integer_value(scope), Some(10)); } + + #[test] + fn test_op_disabled() { + #[op] + fn op_foo() -> Result<i64, anyhow::Error> { + Ok(42) + } + + let ext = Extension::builder() + .ops(vec![op_foo::decl().disable()]) + .build(); + let mut runtime = JsRuntime::new(RuntimeOptions { + extensions: vec![ext], + ..Default::default() + }); + let r = runtime + .execute_script("test.js", "Deno.core.opSync('op_foo')") + .unwrap(); + let scope = &mut runtime.handle_scope(); + assert!(r.open(scope).is_undefined()); + } } |