Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ops): Support for *const c_void and *mut c_void externals #146

Merged
merged 6 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions core/benches/ops/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::borrow::Cow;
use bencher::*;
use deno_core::error::generic_error;
use deno_core::*;
use std::ffi::c_void;

deno_core::extension!(
testing,
Expand All @@ -20,6 +21,9 @@ deno_core::extension!(
op_local_nofast,
op_global,
op_global_scope,
op_make_external,
op_external,
op_external_nofast,
],
state = |state| {
state.put(1234u32);
Expand Down Expand Up @@ -85,6 +89,17 @@ pub fn op_global_scope(
) {
}

#[op2(fast)]
pub fn op_make_external() -> *const c_void {
std::ptr::null()
}

#[op2(fast)]
pub fn op_external(_input: *const c_void) {}

#[op2(nofast)]
pub fn op_external_nofast(_input: *const c_void) {}

fn bench_op(
b: &mut Bencher,
count: usize,
Expand Down Expand Up @@ -124,6 +139,8 @@ const LARGE_STRING_1000 = '*'.repeat(1000);
const LARGE_STRING_UTF8_1000000 = '\u1000'.repeat(1000000);
const LARGE_STRING_UTF8_1000 = '\u1000'.repeat(1000);
const {{ {op}: op }} = Deno.core.ensureFastOps();
const {{ op_make_external }} = Deno.core.ensureFastOps();
const EXTERNAL = op_make_external();
function {op}({args}) {{
op({args});
}}
Expand Down Expand Up @@ -349,6 +366,20 @@ fn bench_op_v8_global_scope(b: &mut Bencher) {
bench_op(b, BENCH_COUNT, "op_global_scope", 1, "op_global_scope('this is a reasonably long string that we would like to get the length of!');");
}

fn bench_op_external(b: &mut Bencher) {
bench_op(b, BENCH_COUNT, "op_external", 1, "op_external(EXTERNAL)");
}

fn bench_op_external_nofast(b: &mut Bencher) {
bench_op(
b,
BENCH_COUNT,
"op_external_nofast",
1,
"op_external_nofast(EXTERNAL)",
);
}

benchmark_group!(
benches,
baseline,
Expand All @@ -375,6 +406,8 @@ benchmark_group!(
bench_op_v8_local_nofast,
bench_op_v8_global,
bench_op_v8_global_scope,
bench_op_external,
bench_op_external_nofast,
);

benchmark_main!(benches);
60 changes: 59 additions & 1 deletion core/runtime/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use futures::future::Either;
use futures::future::Future;
use futures::future::FutureExt;
use futures::task::noop_waker_ref;
use libc::c_void;
use serde::Deserialize;
use serde::Serialize;
use serde_v8::from_v8;
Expand All @@ -17,6 +16,7 @@ use serde_v8::JsBuffer;
use serde_v8::V8Slice;
use std::borrow::Cow;
use std::cell::RefCell;
use std::ffi::c_void;
use std::future::ready;
use std::mem::MaybeUninit;
use std::option::Option;
Expand Down Expand Up @@ -319,6 +319,16 @@ pub fn to_f64_option(number: &v8::Value) -> Option<f64> {
None
}

pub fn to_external_option(external: &v8::Value) -> Option<*mut c_void> {
if external.is_external() {
// SAFETY: We know this is an external
let external: &v8::External = unsafe { std::mem::transmute(external) };
Some(external.value())
} else {
None
}
}

/// Expands `inbuf` to `outbuf`, assuming that `outbuf` has at least 2x `input_length`.
#[inline(always)]
unsafe fn latin1_to_utf8(
Expand Down Expand Up @@ -740,6 +750,8 @@ mod tests {
op_buffer_slice_unsafe_callback,
op_buffer_copy,
op_buffer_bytesmut,
op_external_make,
op_external_process,

op_async_void,
op_async_number,
Expand All @@ -754,6 +766,7 @@ mod tests {
op_async_buffer,
op_async_buffer_vec,
op_async_buffer_impl,
op_async_external,
],
state = |state| {
state.put(1234u32);
Expand Down Expand Up @@ -1675,6 +1688,31 @@ mod tests {
Ok(())
}

static STRING: &str = "hello world";

#[op2(core, fast)]
fn op_external_make() -> *const std::ffi::c_void {
STRING.as_ptr() as _
}

#[op2(core, fast)]
fn op_external_process(
input: *const std::ffi::c_void,
) -> *const std::ffi::c_void {
assert_eq!(input, STRING.as_ptr() as _);
input
}

#[tokio::test]
pub async fn test_external() -> Result<(), Box<dyn std::error::Error>> {
run_test2(
10000,
"op_external_make, op_external_process",
"op_external_process(op_external_make())",
)?;
Ok(())
}

#[op2(async, core)]
async fn op_async_void() {}

Expand Down Expand Up @@ -1882,4 +1920,24 @@ mod tests {
.await?;
Ok(())
}

#[op2(async, core)]
async fn op_async_external(
input: *const std::ffi::c_void,
) -> *const std::ffi::c_void {
assert_eq!(input, STRING.as_ptr() as _);
input
}

#[tokio::test]
pub async fn test_op_async_external() -> Result<(), Box<dyn std::error::Error>>
{
run_async_test(
2,
"op_external_make, op_async_external",
"await op_async_external(op_external_make())",
)
.await?;
Ok(())
}
}
56 changes: 56 additions & 0 deletions ops/op2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,34 @@ Safe.
<tr>
<td>

```text
*const std::ffi::c_void
```

</td><td>
</td><td>
External
</td><td>

</td></tr>
<tr>
<td>

```text
*mut std::ffi::c_void
```

</td><td>
</td><td>
External
</td><td>

</td></tr>
<tr>
<td>

```text
&OpState
```
Expand Down Expand Up @@ -964,6 +992,34 @@ ArrayBuffer
<tr>
<td>

```text
*const std::ffi::c_void
```

</td><td>
</td><td>
External
</td><td>

</td></tr>
<tr>
<td>

```text
*mut std::ffi::c_void
```

</td><td>
</td><td>
External
</td><td>

</td></tr>
<tr>
<td>

```text
v8::Local<v8::Value>
```
Expand Down
2 changes: 2 additions & 0 deletions ops/op2/dispatch_fast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ fn map_arg_to_v8_fastcall_type(
Arg::String(Strings::CowStr) => V8FastCallType::SeqOneByteString,
// Cow byte strings can be fast and don't require copying
Arg::String(Strings::CowByte) => V8FastCallType::SeqOneByteString,
Arg::External(..) => V8FastCallType::Pointer,
_ => return Err(V8MappingError::NoMapping("a fast argument", arg.clone())),
};
Ok(Some(rv))
Expand Down Expand Up @@ -508,6 +509,7 @@ fn map_retval_to_v8_fastcall_type(
| Arg::OptionV8Local(_)
| Arg::OptionV8Ref(..) => return Ok(None),
Arg::Buffer(..) => return Ok(None),
Arg::External(..) => V8FastCallType::Pointer,
_ => {
return Err(V8MappingError::NoMapping(
"a fast return value",
Expand Down
11 changes: 11 additions & 0 deletions ops/op2/dispatch_slow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::dispatch_shared::v8_to_arg;
use super::generator_state::GeneratorState;
use super::signature::Arg;
use super::signature::Buffer;
use super::signature::External;
use super::signature::NumericArg;
use super::signature::ParsedSignature;
use super::signature::RefType;
Expand Down Expand Up @@ -297,6 +298,9 @@ pub fn from_arg(
Arg::Buffer(buffer) => {
from_arg_buffer(generator_state, &arg_ident, buffer)?
}
Arg::External(External::Ptr(_)) => {
from_arg_numeric(generator_state, &arg_ident, "external")?
}
Arg::Ref(_, Special::HandleScope) => {
*needs_scope = true;
quote!(let #arg_ident = &mut #scope;)
Expand Down Expand Up @@ -615,6 +619,10 @@ pub fn return_value_infallible(
*needs_scope = true;
quote! { #retval.set(#deno_core::_ops::ToV8Value::to_v8_value(#result, &mut #scope)); }
}
Arg::External(External::Ptr(_)) => {
*needs_scope = true;
quote! { #retval.set(#deno_core::v8::External::new(&mut #scope, #result as _).into()) }
}
arg if arg.is_option() => {
// We support all optional types by generating the infallible version in a branch
let some = return_value_infallible(
Expand Down Expand Up @@ -669,6 +677,9 @@ pub fn return_value_v8_value(
) => {
quote!(Ok(#deno_core::_ops::ToV8Value::to_v8_value(#result, #scope)))
}
Arg::External(External::Ptr(_)) => {
quote!(Ok(#deno_core::v8::External::new(#scope, #result as _).into()))
}
_ => {
return Err(V8MappingError::NoMapping(
"a v8 return value",
Expand Down
Loading