Skip to content

Commit be7e9cb

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

File tree

5 files changed

+84
-11
lines changed

5 files changed

+84
-11
lines changed

core/runtime/ops.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,8 @@ mod tests {
612612
op_buffer_ptr,
613613
op_buffer_slice_32,
614614
op_buffer_ptr_32,
615+
op_buffer_slice_f32,
616+
op_buffer_ptr_f32,
615617
op_buffer_slice_f64,
616618
op_buffer_ptr_f64,
617619
op_buffer_slice_unsafe_callback,
@@ -665,6 +667,8 @@ mod tests {
665667
op_create_buf_i16,
666668
op_create_buf_i32,
667669
op_create_buf_i64,
670+
op_create_buf_f32,
671+
op_create_buf_f64,
668672
],
669673
state = |state| {
670674
state.put(1234u32);
@@ -1550,12 +1554,45 @@ mod tests {
15501554
}
15511555
}
15521556

1557+
#[op2(fast)]
1558+
pub fn op_buffer_slice_f32(
1559+
#[buffer] input: &[f32],
1560+
#[number] inlen: usize,
1561+
#[buffer] output: &mut [f32],
1562+
#[number] outlen: usize,
1563+
) {
1564+
assert_eq!(inlen, input.len());
1565+
assert_eq!(outlen, output.len());
1566+
if inlen > 0 && outlen > 0 {
1567+
output[0] = input[0];
1568+
}
1569+
}
1570+
1571+
#[op2(fast)]
1572+
pub fn op_buffer_ptr_f32(
1573+
#[buffer] input: *const f32,
1574+
#[number] inlen: usize,
1575+
#[buffer] output: *mut f32,
1576+
#[number] outlen: usize,
1577+
) {
1578+
if inlen > 0 && outlen > 0 {
1579+
// SAFETY: for test
1580+
unsafe { std::ptr::write(output, std::ptr::read(input)) }
1581+
}
1582+
}
1583+
15531584
#[tokio::test]
15541585
pub async fn test_op_buffer_slice() -> Result<(), Box<dyn std::error::Error>>
15551586
{
15561587
for (op, op_ptr, arr, size) in [
15571588
("op_buffer_slice", "op_buffer_ptr", "Uint8Array", 1),
15581589
("op_buffer_slice_32", "op_buffer_ptr_32", "Uint32Array", 4),
1590+
(
1591+
"op_buffer_slice_f32",
1592+
"op_buffer_ptr_f32",
1593+
"Float32Array",
1594+
4,
1595+
),
15591596
(
15601597
"op_buffer_slice_f64",
15611598
"op_buffer_ptr_f64",
@@ -2497,7 +2534,7 @@ mod tests {
24972534
#[op2]
24982535
#[buffer]
24992536
fn [< op_create_buf_ $size >] () -> Vec<$size> {
2500-
vec![1, 2, 3, 4]
2537+
vec![1 as _, 2 as _, 3 as _, 4 as _]
25012538
}
25022539
}
25032540
};
@@ -2510,6 +2547,8 @@ mod tests {
25102547
op_create_buf!(i16);
25112548
op_create_buf!(i32);
25122549
op_create_buf!(i64);
2550+
op_create_buf!(f32);
2551+
op_create_buf!(f64);
25132552

25142553
#[test]
25152554
fn return_buffers() -> Result<(), Box<dyn std::error::Error>> {
@@ -2537,6 +2576,8 @@ mod tests {
25372576
test("i16")?;
25382577
test("i32")?;
25392578
test("i64")?;
2579+
test("f32")?;
2580+
test("f64")?;
25402581
Ok(())
25412582
}
25422583
}

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())
@@ -873,6 +876,14 @@ fn map_arg_to_v8_fastcall_type(
873876
_,
874877
BufferSource::TypedArray,
875878
) => V8FastCallType::Uint32Array,
879+
Arg::Buffer(
880+
BufferType::Slice(.., NumericArg::f32)
881+
| BufferType::Ptr(.., NumericArg::f32)
882+
| BufferType::Vec(.., NumericArg::f32)
883+
| BufferType::BoxSlice(.., NumericArg::f32),
884+
_,
885+
BufferSource::TypedArray,
886+
) => V8FastCallType::Float32Array,
876887
Arg::Buffer(
877888
BufferType::Slice(.., NumericArg::f64)
878889
| 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)