Skip to content

Commit efb124f

Browse files
committed
feat(ops): support f32 slice parameters and f32/f64 buffer returns
1 parent 0694f17 commit efb124f

File tree

7 files changed

+88
-13
lines changed

7 files changed

+88
-13
lines changed

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ proc-macro2 = "1"
7373
quote = "1"
7474
syn = { version = "2", features = ["full", "extra-traits"] }
7575

76+
[patch.crates-io]
77+
v8 = { git = "https://github.com/denoland/rusty_v8.git", rev = "refs/pull/1858/head" }
78+
7679
[profile.dev.package.v8]
7780
# v8 miscompiles at opt-level=0
7881
opt-level = 1

core/runtime/ops.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,8 @@ mod tests {
622622
op_buffer_ptr,
623623
op_buffer_slice_32,
624624
op_buffer_ptr_32,
625+
op_buffer_slice_f32,
626+
op_buffer_ptr_f32,
625627
op_buffer_slice_f64,
626628
op_buffer_ptr_f64,
627629
op_buffer_slice_unsafe_callback,
@@ -675,6 +677,8 @@ mod tests {
675677
op_create_buf_i16,
676678
op_create_buf_i32,
677679
op_create_buf_i64,
680+
op_create_buf_f32,
681+
op_create_buf_f64,
678682
],
679683
state = |state| {
680684
state.put(1234u32);
@@ -1587,12 +1591,45 @@ mod tests {
15871591
}
15881592
}
15891593

1594+
#[op2(fast)]
1595+
pub fn op_buffer_slice_f32(
1596+
#[buffer] input: &[f32],
1597+
#[number] inlen: usize,
1598+
#[buffer] output: &mut [f32],
1599+
#[number] outlen: usize,
1600+
) {
1601+
assert_eq!(inlen, input.len());
1602+
assert_eq!(outlen, output.len());
1603+
if inlen > 0 && outlen > 0 {
1604+
output[0] = input[0];
1605+
}
1606+
}
1607+
1608+
#[op2(fast)]
1609+
pub fn op_buffer_ptr_f32(
1610+
#[buffer] input: *const f32,
1611+
#[number] inlen: usize,
1612+
#[buffer] output: *mut f32,
1613+
#[number] outlen: usize,
1614+
) {
1615+
if inlen > 0 && outlen > 0 {
1616+
// SAFETY: for test
1617+
unsafe { std::ptr::write(output, std::ptr::read(input)) }
1618+
}
1619+
}
1620+
15901621
#[tokio::test]
15911622
pub async fn test_op_buffer_slice() -> Result<(), Box<dyn std::error::Error>>
15921623
{
15931624
for (op, op_ptr, arr, size) in [
15941625
("op_buffer_slice", "op_buffer_ptr", "Uint8Array", 1),
15951626
("op_buffer_slice_32", "op_buffer_ptr_32", "Uint32Array", 4),
1627+
(
1628+
"op_buffer_slice_f32",
1629+
"op_buffer_ptr_f32",
1630+
"Float32Array",
1631+
4,
1632+
),
15961633
(
15971634
"op_buffer_slice_f64",
15981635
"op_buffer_ptr_f64",
@@ -2534,7 +2571,7 @@ mod tests {
25342571
#[op2]
25352572
#[buffer]
25362573
fn [< op_create_buf_ $size >] () -> Vec<$size> {
2537-
vec![1, 2, 3, 4]
2574+
vec![1 as _, 2 as _, 3 as _, 4 as _]
25382575
}
25392576
}
25402577
};
@@ -2547,6 +2584,8 @@ mod tests {
25472584
op_create_buf!(i16);
25482585
op_create_buf!(i32);
25492586
op_create_buf!(i64);
2587+
op_create_buf!(f32);
2588+
op_create_buf!(f64);
25502589

25512590
#[test]
25522591
fn return_buffers() -> Result<(), Box<dyn std::error::Error>> {
@@ -2574,6 +2613,8 @@ mod tests {
25742613
test("i16")?;
25752614
test("i32")?;
25762615
test("i64")?;
2616+
test("f32")?;
2617+
test("f64")?;
25772618
Ok(())
25782619
}
25792620
}

core/runtime/ops_rust_to_v8.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ typedarray!(i8, Int8Array);
420420
typedarray!(i16, Int16Array);
421421
typedarray!(i32, Int32Array);
422422
typedarray!(i64, BigInt64Array);
423+
typedarray!(f32, Float32Array);
424+
typedarray!(f64, Float64Array);
423425

424426
//
425427
// Serde

ops/op2/dispatch_fast.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ pub(crate) enum V8FastCallType {
193193
AnyArray,
194194
Uint8Array,
195195
Uint32Array,
196+
Float32Array,
196197
Float64Array,
197198
SeqOneByteString,
198199
CallbackOptions,
@@ -220,6 +221,7 @@ impl V8FastCallType {
220221
V8FastCallType::V8Value
221222
| V8FastCallType::Uint8Array
222223
| V8FastCallType::Uint32Array
224+
| V8FastCallType::Float32Array
223225
| V8FastCallType::Float64Array => {
224226
quote!(deno_core::v8::Local<deno_core::v8::Value>)
225227
}
@@ -261,6 +263,7 @@ impl V8FastCallType {
261263
V8FastCallType::AnyArray => quote!(CType::V8Value.as_info()),
262264
V8FastCallType::Uint8Array => quote!(CType::V8Value.as_info()),
263265
V8FastCallType::Uint32Array => quote!(CType::V8Value.as_info()),
266+
V8FastCallType::Float32Array => quote!(CType::V8Value.as_info()),
264267
V8FastCallType::Float64Array => quote!(CType::V8Value.as_info()),
265268
V8FastCallType::SeqOneByteString => {
266269
quote!(CType::SeqOneByteString.as_info())
@@ -909,6 +912,14 @@ fn map_arg_to_v8_fastcall_type(
909912
_,
910913
BufferSource::TypedArray,
911914
) => V8FastCallType::Uint32Array,
915+
Arg::Buffer(
916+
BufferType::Slice(.., NumericArg::f32)
917+
| BufferType::Ptr(.., NumericArg::f32)
918+
| BufferType::Vec(.., NumericArg::f32)
919+
| BufferType::BoxSlice(.., NumericArg::f32),
920+
_,
921+
BufferSource::TypedArray,
922+
) => V8FastCallType::Float32Array,
912923
Arg::Buffer(
913924
BufferType::Slice(.., NumericArg::f64)
914925
| BufferType::Ptr(.., NumericArg::f64)

ops/op2/dispatch_shared.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,33 +87,35 @@ pub fn v8slice_to_buffer(
8787
}
8888
BufferType::Slice(
8989
RefType::Ref,
90-
NumericArg::u8 | NumericArg::u32 | NumericArg::f64,
90+
NumericArg::u8 | NumericArg::u32 | NumericArg::f32 | NumericArg::f64,
9191
) => {
9292
quote!(let #arg_ident = #v8slice.as_ref();)
9393
}
9494
BufferType::Slice(
9595
RefType::Mut,
96-
NumericArg::u8 | NumericArg::u32 | NumericArg::f64,
96+
NumericArg::u8 | NumericArg::u32 | NumericArg::f32 | NumericArg::f64,
9797
) => {
9898
quote!(let #arg_ident = #v8slice.as_mut();)
9999
}
100100
BufferType::Ptr(
101101
RefType::Ref,
102-
NumericArg::u8 | NumericArg::u32 | NumericArg::f64,
102+
NumericArg::u8 | NumericArg::u32 | NumericArg::f32 | NumericArg::f64,
103103
) => {
104104
quote!(let #arg_ident = if #v8slice.len() == 0 { std::ptr::null() } else { #v8slice.as_ref().as_ptr() };)
105105
}
106106
BufferType::Ptr(
107107
RefType::Mut,
108-
NumericArg::u8 | NumericArg::u32 | NumericArg::f64,
108+
NumericArg::u8 | NumericArg::u32 | NumericArg::f32 | NumericArg::f64,
109109
) => {
110110
quote!(let #arg_ident = if #v8slice.len() == 0 { std::ptr::null_mut() } else { #v8slice.as_mut().as_mut_ptr() };)
111111
}
112-
BufferType::Vec(NumericArg::u8 | NumericArg::u32 | NumericArg::f64) => {
112+
BufferType::Vec(
113+
NumericArg::u8 | NumericArg::u32 | NumericArg::f32 | NumericArg::f64,
114+
) => {
113115
quote!(let #arg_ident = #v8slice.to_vec();)
114116
}
115117
BufferType::BoxSlice(
116-
NumericArg::u8 | NumericArg::u32 | NumericArg::f64,
118+
NumericArg::u8 | NumericArg::u32 | NumericArg::f32 | NumericArg::f64,
117119
) => {
118120
quote!(let #arg_ident = #v8slice.to_boxed_slice();)
119121
}
@@ -137,18 +139,23 @@ pub fn byte_slice_to_buffer(
137139
let res = match buffer {
138140
BufferType::Slice(
139141
_,
140-
NumericArg::u8 | NumericArg::u32 | NumericArg::f64,
142+
NumericArg::u8 | NumericArg::u32 | NumericArg::f32 | NumericArg::f64,
141143
) => {
142144
quote!(let #arg_ident = #buf;)
143145
}
144-
BufferType::Ptr(_, NumericArg::u8 | NumericArg::u32 | NumericArg::f64) => {
146+
BufferType::Ptr(
147+
_,
148+
NumericArg::u8 | NumericArg::u32 | NumericArg::f32 | NumericArg::f64,
149+
) => {
145150
quote!(let #arg_ident = if #buf.len() == 0 { ::std::ptr::null_mut() } else { #buf.as_mut_ptr() as _ };)
146151
}
147-
BufferType::Vec(NumericArg::u8 | NumericArg::u32 | NumericArg::f64) => {
152+
BufferType::Vec(
153+
NumericArg::u8 | NumericArg::u32 | NumericArg::f32 | NumericArg::f64,
154+
) => {
148155
quote!(let #arg_ident = #buf.to_vec();)
149156
}
150157
BufferType::BoxSlice(
151-
NumericArg::u8 | NumericArg::u32 | NumericArg::f64,
158+
NumericArg::u8 | NumericArg::u32 | NumericArg::f32 | NumericArg::f64,
152159
) => {
153160
quote!(let #arg_ident = #buf.to_vec().into_boxed_slice();)
154161
}

serde_v8/magic/v8slice.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ impl V8Sliceable for u32 {
4444
}
4545
}
4646

47+
impl V8Sliceable for f32 {
48+
type V8 = v8::Float32Array;
49+
fn new_buf<'s, 'i>(
50+
scope: &mut v8::PinScope<'s, 'i>,
51+
buf: v8::Local<v8::ArrayBuffer>,
52+
byte_offset: usize,
53+
length: usize,
54+
) -> Option<v8::Local<'s, Self::V8>> {
55+
v8::Float32Array::new(scope, buf, byte_offset, length)
56+
}
57+
}
58+
4759
impl V8Sliceable for f64 {
4860
type V8 = v8::Float64Array;
4961
fn new_buf<'s, 'i>(

0 commit comments

Comments
 (0)