mirror of
https://github.com/denoland/deno.git
synced 2024-11-25 15:29:32 -05:00
feat(ops): #[op(unstable)] (#14124)
This commit is contained in:
parent
79106d757f
commit
3cc14196e2
3 changed files with 63 additions and 1 deletions
|
@ -16,6 +16,7 @@ pub struct OpDecl {
|
||||||
pub v8_fn_ptr: OpFnRef,
|
pub v8_fn_ptr: OpFnRef,
|
||||||
pub enabled: bool,
|
pub enabled: bool,
|
||||||
pub is_async: bool, // TODO(@AaronO): enum sync/async/fast ?
|
pub is_async: bool, // TODO(@AaronO): enum sync/async/fast ?
|
||||||
|
pub is_unstable: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OpDecl {
|
impl OpDecl {
|
||||||
|
|
|
@ -3050,4 +3050,39 @@ assertEquals(1, notify_return_value);
|
||||||
let scope = &mut runtime.handle_scope();
|
let scope = &mut runtime.handle_scope();
|
||||||
assert!(r.open(scope).is_undefined());
|
assert!(r.open(scope).is_undefined());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_op_unstable_disabling() {
|
||||||
|
#[op]
|
||||||
|
fn op_foo() -> Result<i64, anyhow::Error> {
|
||||||
|
Ok(42)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[op(unstable)]
|
||||||
|
fn op_bar() -> Result<i64, anyhow::Error> {
|
||||||
|
Ok(42)
|
||||||
|
}
|
||||||
|
|
||||||
|
let ext = Extension::builder()
|
||||||
|
.ops(vec![op_foo::decl(), op_bar::decl()])
|
||||||
|
.middleware(|op| if op.is_unstable { op.disable() } else { op })
|
||||||
|
.build();
|
||||||
|
let mut runtime = JsRuntime::new(RuntimeOptions {
|
||||||
|
extensions: vec![ext],
|
||||||
|
..Default::default()
|
||||||
|
});
|
||||||
|
runtime
|
||||||
|
.execute_script(
|
||||||
|
"test.js",
|
||||||
|
r#"
|
||||||
|
if (Deno.core.opSync('op_foo') !== 42) {
|
||||||
|
throw new Error("Exptected op_foo() === 42");
|
||||||
|
}
|
||||||
|
if (Deno.core.opSync('op_bar') !== undefined) {
|
||||||
|
throw new Error("Expected op_bar to be disabled")
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
28
ops/lib.rs
28
ops/lib.rs
|
@ -34,8 +34,33 @@ fn core_import() -> TokenStream2 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct MacroArgs {
|
||||||
|
is_unstable: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl syn::parse::Parse for MacroArgs {
|
||||||
|
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||||
|
let vars =
|
||||||
|
syn::punctuated::Punctuated::<Ident, syn::Token![,]>::parse_terminated(
|
||||||
|
input,
|
||||||
|
)?;
|
||||||
|
let vars: Vec<_> = vars.iter().map(Ident::to_string).collect();
|
||||||
|
let vars: Vec<_> = vars.iter().map(String::as_str).collect();
|
||||||
|
match vars[..] {
|
||||||
|
["unstable"] => Ok(Self { is_unstable: true }),
|
||||||
|
[] => Ok(Self { is_unstable: false }),
|
||||||
|
_ => Err(syn::Error::new(
|
||||||
|
input.span(),
|
||||||
|
"Ops expect #[op] or #[op(unstable)]",
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn op(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
pub fn op(attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
|
let MacroArgs { is_unstable } = syn::parse_macro_input!(attr as MacroArgs);
|
||||||
let func = syn::parse::<syn::ItemFn>(item).expect("expected a function");
|
let func = syn::parse::<syn::ItemFn>(item).expect("expected a function");
|
||||||
let name = &func.sig.ident;
|
let name = &func.sig.ident;
|
||||||
let generics = &func.sig.generics;
|
let generics = &func.sig.generics;
|
||||||
|
@ -85,6 +110,7 @@ pub fn op(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
v8_fn_ptr: Self::v8_fn_ptr::<#type_params>(),
|
v8_fn_ptr: Self::v8_fn_ptr::<#type_params>(),
|
||||||
enabled: true,
|
enabled: true,
|
||||||
is_async: #is_async,
|
is_async: #is_async,
|
||||||
|
is_unstable: #is_unstable,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue