Skip to content

Commit

Permalink
feat(ops): support SMI in return types (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Jul 25, 2023
1 parent 39edde8 commit 482ffcf
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 9 deletions.
3 changes: 2 additions & 1 deletion ops/op2/dispatch_fast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,8 @@ fn map_retval_to_v8_fastcall_type(
Arg::Numeric(NumericArg::u32)
| Arg::Numeric(NumericArg::u16)
| Arg::Numeric(NumericArg::u8) => V8FastCallType::U32,
Arg::Numeric(NumericArg::i32)
Arg::Numeric(NumericArg::__SMI__)
| Arg::Numeric(NumericArg::i32)
| Arg::Numeric(NumericArg::i16)
| Arg::Numeric(NumericArg::i8) => V8FastCallType::I32,
Arg::Numeric(NumericArg::u64) | Arg::Numeric(NumericArg::usize) => {
Expand Down
9 changes: 6 additions & 3 deletions ops/op2/dispatch_slow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,8 @@ pub fn return_value_infallible(
}
Arg::Numeric(NumericArg::i8)
| Arg::Numeric(NumericArg::i16)
| Arg::Numeric(NumericArg::i32) => {
| Arg::Numeric(NumericArg::i32)
| Arg::Numeric(NumericArg::__SMI__) => {
quote!(#retval.set_int32(#result as i32);)
}
Arg::Numeric(NumericArg::i64 | NumericArg::isize) => {
Expand Down Expand Up @@ -644,8 +645,10 @@ pub fn return_value_v8_value(
Arg::Numeric(NumericArg::bool) => {
quote!(Ok(#deno_core::v8::Boolean::new(#result).into()))
}
Arg::Numeric(NumericArg::i8 | NumericArg::i16 | NumericArg::i32) => {
quote!(Ok(#deno_core::v8::Integer::new(#scope, #result).into()))
Arg::Numeric(
NumericArg::i8 | NumericArg::i16 | NumericArg::i32 | NumericArg::__SMI__,
) => {
quote!(Ok(#deno_core::v8::Integer::new(#scope, #result as i32).into()))
}
Arg::Numeric(NumericArg::u8 | NumericArg::u16 | NumericArg::u32) => {
quote!(Ok(#deno_core::v8::Integer::new_from_unsigned(#scope, #result).into()))
Expand Down
4 changes: 4 additions & 0 deletions ops/op2/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,10 @@ mod tests {
fn op_resource(#[smi] rid: ResourceId, #[buffer] buffer: &[u8]);
(Numeric(__SMI__), Buffer(Slice(Ref, u8))) -> Infallible(Void)
);
test!(
#[smi] fn op_resource2(#[smi] rid: ResourceId) -> Result<ResourceId, Error>;
(Numeric(__SMI__)) -> Result(Numeric(__SMI__))
);
test!(
fn op_option_numeric_result(state: &mut OpState) -> Result<Option<u32>, AnyError>;
(Ref(Mut, OpState)) -> Result(OptionNumeric(u32))
Expand Down
4 changes: 3 additions & 1 deletion ops/op2/test_cases/async/async_arg_return.out
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ impl op_async {
opctx,
promise_id,
result,
|scope, result| { Ok(deno_core::v8::Integer::new(scope, result).into()) },
|scope, result| {
Ok(deno_core::v8::Integer::new(scope, result as i32).into())
},
) {
rv.set_int32(result as i32);
}
Expand Down
4 changes: 3 additions & 1 deletion ops/op2/test_cases/async/async_arg_return_result.out
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ impl op_async {
opctx,
promise_id,
result,
|scope, result| { Ok(deno_core::v8::Integer::new(scope, result).into()) },
|scope, result| {
Ok(deno_core::v8::Integer::new(scope, result as i32).into())
},
) {
match result {
Ok(result) => {
Expand Down
4 changes: 3 additions & 1 deletion ops/op2/test_cases/async/async_opstate.out
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ impl op_async_opstate {
opctx,
promise_id,
result,
|scope, result| { Ok(deno_core::v8::Integer::new(scope, result).into()) },
|scope, result| {
Ok(deno_core::v8::Integer::new(scope, result as i32).into())
},
) {
match result {
Ok(result) => {
Expand Down
4 changes: 3 additions & 1 deletion ops/op2/test_cases/async/async_result.out
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ impl op_async {
opctx,
promise_id,
result,
|scope, result| { Ok(deno_core::v8::Integer::new(scope, result).into()) },
|scope, result| {
Ok(deno_core::v8::Integer::new(scope, result as i32).into())
},
) {
match result {
Ok(result) => {
Expand Down
4 changes: 3 additions & 1 deletion ops/op2/test_cases/async/async_result_impl.out
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ impl op_async_result_impl {
opctx,
promise_id,
result,
|scope, result| { Ok(deno_core::v8::Integer::new(scope, result).into()) },
|scope, result| {
Ok(deno_core::v8::Integer::new(scope, result as i32).into())
},
) {
match result {
Ok(result) => {
Expand Down
87 changes: 87 additions & 0 deletions ops/op2/test_cases/async/async_result_smi.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#[allow(non_camel_case_types)]
pub struct op_async {
_unconstructable: ::std::marker::PhantomData<()>,
}
impl deno_core::_ops::Op for op_async {
const NAME: &'static str = stringify!(op_async);
const DECL: deno_core::_ops::OpDecl = deno_core::_ops::OpDecl::new_internal(
stringify!(op_async),
true,
false,
false,
2usize as u8,
Self::v8_fn_ptr as _,
None,
);
}
impl op_async {
pub const fn name() -> &'static str {
stringify!(op_async)
}
#[deprecated(note = "Use the const op::DECL instead")]
pub const fn decl() -> deno_core::_ops::OpDecl {
<Self as deno_core::_ops::Op>::DECL
}
extern "C" fn v8_fn_ptr(info: *const deno_core::v8::FunctionCallbackInfo) {
let mut scope = unsafe { deno_core::v8::CallbackScope::new(&*info) };
let mut rv = deno_core::v8::ReturnValue::from_function_callback_info(unsafe {
&*info
});
let args = deno_core::v8::FunctionCallbackArguments::from_function_callback_info(unsafe {
&*info
});
let opctx = unsafe {
&*(deno_core::v8::Local::<deno_core::v8::External>::cast(args.data()).value()
as *const deno_core::_ops::OpCtx)
};
let result = {
let arg0 = args.get(1usize as i32);
let Some(arg0) = deno_core::_ops::to_i32_option(&arg0) else {
let mut scope = unsafe { deno_core::v8::CallbackScope::new(&*info) };
let msg = deno_core::v8::String::new_from_one_byte(
&mut scope,
"expected i32".as_bytes(),
deno_core::v8::NewStringType::Normal,
)
.unwrap();
let exc = deno_core::v8::Exception::type_error(&mut scope, msg);
scope.throw_exception(exc);
return;
};
let arg0 = arg0 as _;
Self::call(arg0)
};
let promise_id = deno_core::_ops::to_i32_option(&args.get(0))
.unwrap_or_default();
if let Some(result)
= deno_core::_ops::map_async_op_fallible(
opctx,
promise_id,
result,
|scope, result| {
Ok(deno_core::v8::Integer::new(scope, result as i32).into())
},
) {
match result {
Ok(result) => {
rv.set_int32(result as i32);
}
Err(err) => {
let err = err.into();
let opstate = ::std::cell::RefCell::borrow(&*opctx.state);
let exception = deno_core::error::to_v8_error(
&mut scope,
opstate.get_error_class_fn,
&err,
);
scope.throw_exception(exception);
return;
}
};
}
}
#[inline(always)]
pub async fn call(rid: ResourceId) -> std::io::Result<ResourceId> {
Ok(rid as _)
}
}
11 changes: 11 additions & 0 deletions ops/op2/test_cases/async/async_result_smi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
#![deny(warnings)]
deno_ops_compile_test_runner::prelude!();

pub type ResourceId = i16;

#[op2(async)]
#[smi]
pub async fn op_async(#[smi] rid: ResourceId) -> std::io::Result<ResourceId> {
Ok(rid as _)
}
90 changes: 90 additions & 0 deletions ops/op2/test_cases/sync/smi.out
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,93 @@ impl op_add {
id as u32 + extra as u32
}
}

#[allow(non_camel_case_types)]
struct op_subtract {
_unconstructable: ::std::marker::PhantomData<()>,
}
impl deno_core::_ops::Op for op_subtract {
const NAME: &'static str = stringify!(op_subtract);
const DECL: deno_core::_ops::OpDecl = deno_core::_ops::OpDecl::new_internal(
stringify!(op_subtract),
false,
false,
false,
2usize as u8,
Self::v8_fn_ptr as _,
Some({
use deno_core::v8::fast_api::Type;
use deno_core::v8::fast_api::CType;
deno_core::v8::fast_api::FastFunction::new_with_bigint(
&[Type::V8Value, Type::Int32, Type::Int32],
CType::Int32,
Self::v8_fn_ptr_fast as *const ::std::ffi::c_void,
)
}),
);
}
impl op_subtract {
pub const fn name() -> &'static str {
stringify!(op_subtract)
}
#[deprecated(note = "Use the const op::DECL instead")]
pub const fn decl() -> deno_core::_ops::OpDecl {
<Self as deno_core::_ops::Op>::DECL
}
fn v8_fn_ptr_fast(
_: deno_core::v8::Local<deno_core::v8::Object>,
arg0: i32,
arg1: i32,
) -> i32 {
let result = {
let arg0 = arg0 as _;
let arg1 = arg1 as _;
Self::call(arg0, arg1)
};
result
}
extern "C" fn v8_fn_ptr(info: *const deno_core::v8::FunctionCallbackInfo) {
let mut rv = deno_core::v8::ReturnValue::from_function_callback_info(unsafe {
&*info
});
let args = deno_core::v8::FunctionCallbackArguments::from_function_callback_info(unsafe {
&*info
});
let result = {
let arg0 = args.get(0usize as i32);
let Some(arg0) = deno_core::_ops::to_i32_option(&arg0) else {
let mut scope = unsafe { deno_core::v8::CallbackScope::new(&*info) };
let msg = deno_core::v8::String::new_from_one_byte(
&mut scope,
"expected i32".as_bytes(),
deno_core::v8::NewStringType::Normal,
)
.unwrap();
let exc = deno_core::v8::Exception::type_error(&mut scope, msg);
scope.throw_exception(exc);
return;
};
let arg0 = arg0 as _;
let arg1 = args.get(1usize as i32);
let Some(arg1) = deno_core::_ops::to_i32_option(&arg1) else {
let mut scope = unsafe { deno_core::v8::CallbackScope::new(&*info) };
let msg = deno_core::v8::String::new_from_one_byte(
&mut scope,
"expected i32".as_bytes(),
deno_core::v8::NewStringType::Normal,
)
.unwrap();
let exc = deno_core::v8::Exception::type_error(&mut scope, msg);
scope.throw_exception(exc);
return;
};
let arg1 = arg1 as _;
Self::call(arg0, arg1)
};
rv.set_int32(result as i32);
}
#[inline(always)]
fn call(id: StubId, extra: i32) -> StubId {
id - extra
}
}
8 changes: 8 additions & 0 deletions ops/op2/test_cases/sync/smi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ pub type ResourceId = i16;
fn op_add(#[smi] id: ResourceId, extra: u16) -> u32 {
id as u32 + extra as u32
}

pub type StubId = i32;

#[op2(fast)]
#[smi]
fn op_subtract(#[smi] id: StubId, extra: i32) -> StubId {
id - extra
}

0 comments on commit 482ffcf

Please sign in to comment.