Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ proc-macro2 = "1"
quote = "1"
syn = { version = "2", features = ["full", "extra-traits"] }

[patch.crates-io]
v8 = { git = "https://github.com/denoland/rusty_v8.git", rev = "refs/pull/1858/head" }

[profile.dev.package.v8]
# v8 miscompiles at opt-level=0
opt-level = 1
Expand Down
43 changes: 42 additions & 1 deletion core/runtime/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,8 @@ mod tests {
op_buffer_ptr,
op_buffer_slice_32,
op_buffer_ptr_32,
op_buffer_slice_f32,
op_buffer_ptr_f32,
op_buffer_slice_f64,
op_buffer_ptr_f64,
op_buffer_slice_unsafe_callback,
Expand Down Expand Up @@ -675,6 +677,8 @@ mod tests {
op_create_buf_i16,
op_create_buf_i32,
op_create_buf_i64,
op_create_buf_f32,
op_create_buf_f64,
],
state = |state| {
state.put(1234u32);
Expand Down Expand Up @@ -1587,12 +1591,45 @@ mod tests {
}
}

#[op2(fast)]
pub fn op_buffer_slice_f32(
#[buffer] input: &[f32],
#[number] inlen: usize,
#[buffer] output: &mut [f32],
#[number] outlen: usize,
) {
assert_eq!(inlen, input.len());
assert_eq!(outlen, output.len());
if inlen > 0 && outlen > 0 {
output[0] = input[0];
}
}

#[op2(fast)]
pub fn op_buffer_ptr_f32(
#[buffer] input: *const f32,
#[number] inlen: usize,
#[buffer] output: *mut f32,
#[number] outlen: usize,
) {
if inlen > 0 && outlen > 0 {
// SAFETY: for test
unsafe { std::ptr::write(output, std::ptr::read(input)) }
}
}

#[tokio::test]
pub async fn test_op_buffer_slice() -> Result<(), Box<dyn std::error::Error>>
{
for (op, op_ptr, arr, size) in [
("op_buffer_slice", "op_buffer_ptr", "Uint8Array", 1),
("op_buffer_slice_32", "op_buffer_ptr_32", "Uint32Array", 4),
(
"op_buffer_slice_f32",
"op_buffer_ptr_f32",
"Float32Array",
4,
),
(
"op_buffer_slice_f64",
"op_buffer_ptr_f64",
Expand Down Expand Up @@ -2534,7 +2571,7 @@ mod tests {
#[op2]
#[buffer]
fn [< op_create_buf_ $size >] () -> Vec<$size> {
vec![1, 2, 3, 4]
vec![1 as _, 2 as _, 3 as _, 4 as _]
}
}
};
Expand All @@ -2547,6 +2584,8 @@ mod tests {
op_create_buf!(i16);
op_create_buf!(i32);
op_create_buf!(i64);
op_create_buf!(f32);
op_create_buf!(f64);

#[test]
fn return_buffers() -> Result<(), Box<dyn std::error::Error>> {
Expand Down Expand Up @@ -2574,6 +2613,8 @@ mod tests {
test("i16")?;
test("i32")?;
test("i64")?;
test("f32")?;
test("f64")?;
Ok(())
}
}
2 changes: 2 additions & 0 deletions core/runtime/ops_rust_to_v8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,8 @@ typedarray!(i8, Int8Array);
typedarray!(i16, Int16Array);
typedarray!(i32, Int32Array);
typedarray!(i64, BigInt64Array);
typedarray!(f32, Float32Array);
typedarray!(f64, Float64Array);

//
// Serde
Expand Down
11 changes: 11 additions & 0 deletions ops/op2/dispatch_fast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ pub(crate) enum V8FastCallType {
AnyArray,
Uint8Array,
Uint32Array,
Float32Array,
Float64Array,
SeqOneByteString,
CallbackOptions,
Expand Down Expand Up @@ -220,6 +221,7 @@ impl V8FastCallType {
V8FastCallType::V8Value
| V8FastCallType::Uint8Array
| V8FastCallType::Uint32Array
| V8FastCallType::Float32Array
| V8FastCallType::Float64Array => {
quote!(deno_core::v8::Local<deno_core::v8::Value>)
}
Expand Down Expand Up @@ -261,6 +263,7 @@ impl V8FastCallType {
V8FastCallType::AnyArray => quote!(CType::V8Value.as_info()),
V8FastCallType::Uint8Array => quote!(CType::V8Value.as_info()),
V8FastCallType::Uint32Array => quote!(CType::V8Value.as_info()),
V8FastCallType::Float32Array => quote!(CType::V8Value.as_info()),
V8FastCallType::Float64Array => quote!(CType::V8Value.as_info()),
V8FastCallType::SeqOneByteString => {
quote!(CType::SeqOneByteString.as_info())
Expand Down Expand Up @@ -909,6 +912,14 @@ fn map_arg_to_v8_fastcall_type(
_,
BufferSource::TypedArray,
) => V8FastCallType::Uint32Array,
Arg::Buffer(
BufferType::Slice(.., NumericArg::f32)
| BufferType::Ptr(.., NumericArg::f32)
| BufferType::Vec(.., NumericArg::f32)
| BufferType::BoxSlice(.., NumericArg::f32),
_,
BufferSource::TypedArray,
) => V8FastCallType::Float32Array,
Arg::Buffer(
BufferType::Slice(.., NumericArg::f64)
| BufferType::Ptr(.., NumericArg::f64)
Expand Down
27 changes: 17 additions & 10 deletions ops/op2/dispatch_shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,33 +87,35 @@ pub fn v8slice_to_buffer(
}
BufferType::Slice(
RefType::Ref,
NumericArg::u8 | NumericArg::u32 | NumericArg::f64,
NumericArg::u8 | NumericArg::u32 | NumericArg::f32 | NumericArg::f64,
) => {
quote!(let #arg_ident = #v8slice.as_ref();)
}
BufferType::Slice(
RefType::Mut,
NumericArg::u8 | NumericArg::u32 | NumericArg::f64,
NumericArg::u8 | NumericArg::u32 | NumericArg::f32 | NumericArg::f64,
) => {
quote!(let #arg_ident = #v8slice.as_mut();)
}
BufferType::Ptr(
RefType::Ref,
NumericArg::u8 | NumericArg::u32 | NumericArg::f64,
NumericArg::u8 | NumericArg::u32 | NumericArg::f32 | NumericArg::f64,
) => {
quote!(let #arg_ident = if #v8slice.len() == 0 { std::ptr::null() } else { #v8slice.as_ref().as_ptr() };)
}
BufferType::Ptr(
RefType::Mut,
NumericArg::u8 | NumericArg::u32 | NumericArg::f64,
NumericArg::u8 | NumericArg::u32 | NumericArg::f32 | NumericArg::f64,
) => {
quote!(let #arg_ident = if #v8slice.len() == 0 { std::ptr::null_mut() } else { #v8slice.as_mut().as_mut_ptr() };)
}
BufferType::Vec(NumericArg::u8 | NumericArg::u32 | NumericArg::f64) => {
BufferType::Vec(
NumericArg::u8 | NumericArg::u32 | NumericArg::f32 | NumericArg::f64,
) => {
quote!(let #arg_ident = #v8slice.to_vec();)
}
BufferType::BoxSlice(
NumericArg::u8 | NumericArg::u32 | NumericArg::f64,
NumericArg::u8 | NumericArg::u32 | NumericArg::f32 | NumericArg::f64,
) => {
quote!(let #arg_ident = #v8slice.to_boxed_slice();)
}
Expand All @@ -137,18 +139,23 @@ pub fn byte_slice_to_buffer(
let res = match buffer {
BufferType::Slice(
_,
NumericArg::u8 | NumericArg::u32 | NumericArg::f64,
NumericArg::u8 | NumericArg::u32 | NumericArg::f32 | NumericArg::f64,
) => {
quote!(let #arg_ident = #buf;)
}
BufferType::Ptr(_, NumericArg::u8 | NumericArg::u32 | NumericArg::f64) => {
BufferType::Ptr(
_,
NumericArg::u8 | NumericArg::u32 | NumericArg::f32 | NumericArg::f64,
) => {
quote!(let #arg_ident = if #buf.len() == 0 { ::std::ptr::null_mut() } else { #buf.as_mut_ptr() as _ };)
}
BufferType::Vec(NumericArg::u8 | NumericArg::u32 | NumericArg::f64) => {
BufferType::Vec(
NumericArg::u8 | NumericArg::u32 | NumericArg::f32 | NumericArg::f64,
) => {
quote!(let #arg_ident = #buf.to_vec();)
}
BufferType::BoxSlice(
NumericArg::u8 | NumericArg::u32 | NumericArg::f64,
NumericArg::u8 | NumericArg::u32 | NumericArg::f32 | NumericArg::f64,
) => {
quote!(let #arg_ident = #buf.to_vec().into_boxed_slice();)
}
Expand Down
12 changes: 12 additions & 0 deletions serde_v8/magic/v8slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ impl V8Sliceable for u32 {
}
}

impl V8Sliceable for f32 {
type V8 = v8::Float32Array;
fn new_buf<'s, 'i>(
scope: &mut v8::PinScope<'s, 'i>,
buf: v8::Local<v8::ArrayBuffer>,
byte_offset: usize,
length: usize,
) -> Option<v8::Local<'s, Self::V8>> {
v8::Float32Array::new(scope, buf, byte_offset, length)
}
}

impl V8Sliceable for f64 {
type V8 = v8::Float64Array;
fn new_buf<'s, 'i>(
Expand Down
Loading