diff --git a/ext/ffi/00_ffi.js b/ext/ffi/00_ffi.js index 25eba02336adfd..c7fdd0e8c58458 100644 --- a/ext/ffi/00_ffi.js +++ b/ext/ffi/00_ffi.js @@ -4,9 +4,7 @@ ((window) => { const core = window.Deno.core; const __bootstrap = window.__bootstrap; - const { - ArrayBuffer, - } = window.__bootstrap.primordials; + class DynamicLibrary { #rid; symbols = {}; @@ -15,40 +13,15 @@ this.#rid = core.opSync("op_ffi_load", { path, symbols }); for (const symbol in symbols) { - const isNonBlocking = symbols[symbol].nonblocking; - - this.symbols[symbol] = (...args) => { - const parameters = []; - const buffers = []; - - for (const arg of args) { - if ( - arg?.buffer instanceof ArrayBuffer && - arg.byteLength !== undefined - ) { - parameters.push(buffers.length); - buffers.push(arg); - } else { - parameters.push(arg); - } - } - - if (isNonBlocking) { - return core.opAsync("op_ffi_call_nonblocking", { - rid: this.#rid, - symbol, - parameters, - buffers, - }); - } else { - return core.opSync("op_ffi_call", { + this.symbols[symbol] = symbols[symbol].nonblocking + ? (...parameters) => + core.opAsync("op_ffi_call_nonblocking", { rid: this.#rid, symbol, parameters, - buffers, - }); - } - }; + }) + : (...parameters) => + core.opSync("op_ffi_call", { rid: this.#rid, symbol, parameters }); } } diff --git a/ext/ffi/lib.rs b/ext/ffi/lib.rs index b5505fb0cf8b7e..d2ab14803fa9fe 100644 --- a/ext/ffi/lib.rs +++ b/ext/ffi/lib.rs @@ -12,7 +12,6 @@ use deno_core::Extension; use deno_core::OpState; use deno_core::Resource; use deno_core::ResourceId; -use deno_core::ZeroCopyBuf; use dlopen::raw::Library; use libffi::middle::Arg; use serde::Deserialize; @@ -133,7 +132,6 @@ enum NativeType { ISize, F32, F64, - Buffer, } impl From for libffi::middle::Type { @@ -152,7 +150,6 @@ impl From for libffi::middle::Type { NativeType::ISize => libffi::middle::Type::isize(), NativeType::F32 => libffi::middle::Type::f32(), NativeType::F64 => libffi::middle::Type::f64(), - NativeType::Buffer => libffi::middle::Type::pointer(), } } } @@ -172,7 +169,6 @@ union NativeValue { isize_value: isize, f32_value: f32, f64_value: f64, - buffer: *const u8, } impl NativeValue { @@ -215,14 +211,9 @@ impl NativeValue { NativeType::F64 => Self { f64_value: value_as_f64(value), }, - NativeType::Buffer => unreachable!(), } } - fn buffer(ptr: *const u8) -> Self { - Self { buffer: ptr } - } - unsafe fn as_arg(&self, native_type: NativeType) -> Arg { match native_type { NativeType::Void => Arg::new(&self.void_value), @@ -238,7 +229,6 @@ impl NativeValue { NativeType::ISize => Arg::new(&self.isize_value), NativeType::F32 => Arg::new(&self.f32_value), NativeType::F64 => Arg::new(&self.f64_value), - NativeType::Buffer => Arg::new(&self.buffer), } } } @@ -268,7 +258,6 @@ fn value_as_f64(value: Value) -> f64 { } #[derive(Deserialize, Debug)] -#[serde(rename_all = "camelCase")] struct ForeignFunction { parameters: Vec, result: NativeType, @@ -384,32 +373,20 @@ where Ok(state.resource_table.add(resource)) } -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] #[serde(rename_all = "camelCase")] struct FfiCallArgs { rid: ResourceId, symbol: String, parameters: Vec, - buffers: Vec, } fn ffi_call(args: FfiCallArgs, symbol: &Symbol) -> Result { - let buffers: Vec<&[u8]> = - args.buffers.iter().map(|buffer| &buffer[..]).collect(); - let native_values = symbol .parameter_types .iter() .zip(args.parameters.into_iter()) - .map(|(&native_type, value)| { - if let NativeType::Buffer = native_type { - let idx: usize = value_as_uint(value); - let ptr = buffers[idx].as_ptr(); - NativeValue::buffer(ptr) - } else { - NativeValue::new(native_type, value) - } - }) + .map(|(&native_type, value)| NativeValue::new(native_type, value)) .collect::>(); let call_args = symbol @@ -461,7 +438,6 @@ fn ffi_call(args: FfiCallArgs, symbol: &Symbol) -> Result { NativeType::F64 => { json!(unsafe { symbol.cif.call::(symbol.ptr, &call_args) }) } - NativeType::Buffer => unreachable!(), }) } diff --git a/test_ffi/src/lib.rs b/test_ffi/src/lib.rs index cc6063ca3442a5..c91d05e0554a8f 100644 --- a/test_ffi/src/lib.rs +++ b/test_ffi/src/lib.rs @@ -1,5 +1,3 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. - use std::thread::sleep; use std::time::Duration; @@ -8,13 +6,6 @@ pub extern "C" fn print_something() { println!("something"); } -#[allow(clippy::not_unsafe_ptr_arg_deref)] -#[no_mangle] -pub extern "C" fn print_buffer(ptr: *const u8, len: usize) { - let buf = unsafe { std::slice::from_raw_parts(ptr, len) }; - println!("{:?}", buf); -} - #[no_mangle] pub extern "C" fn add_u32(a: u32, b: u32) -> u32 { a + b @@ -60,10 +51,3 @@ pub extern "C" fn sleep_blocking(ms: u64) { let duration = Duration::from_millis(ms); sleep(duration); } - -#[allow(clippy::not_unsafe_ptr_arg_deref)] -#[no_mangle] -pub extern "C" fn nonblocking_buffer(ptr: *const u8, len: usize) { - let buf = unsafe { std::slice::from_raw_parts(ptr, len) }; - assert_eq!(buf, vec![1, 2, 3, 4, 5, 6, 7, 8]); -} diff --git a/test_ffi/tests/integration_tests.rs b/test_ffi/tests/integration_tests.rs index 0ae395da86972a..0b2eae854cd9ab 100644 --- a/test_ffi/tests/integration_tests.rs +++ b/test_ffi/tests/integration_tests.rs @@ -38,7 +38,6 @@ fn basic() { assert!(output.status.success()); let expected = "\ something\n\ - [1, 2, 3, 4, 5, 6, 7, 8]\n\ 579\n\ 579\n\ 579\n\ diff --git a/test_ffi/tests/test.js b/test_ffi/tests/test.js index fc354139daeddc..09c5f74178105d 100644 --- a/test_ffi/tests/test.js +++ b/test_ffi/tests/test.js @@ -12,7 +12,6 @@ const libPath = `${targetDir}/${libPrefix}test_ffi.${libSuffix}`; const resourcesPre = Deno.resources(); const dylib = Deno.dlopen(libPath, { "print_something": { parameters: [], result: "void" }, - "print_buffer": { parameters: ["buffer", "usize"], result: "void" }, "add_u32": { parameters: ["u32", "u32"], result: "u32" }, "add_i32": { parameters: ["i32", "i32"], result: "i32" }, "add_u64": { parameters: ["u64", "u64"], result: "u64" }, @@ -22,16 +21,9 @@ const dylib = Deno.dlopen(libPath, { "add_f32": { parameters: ["f32", "f32"], result: "f32" }, "add_f64": { parameters: ["f64", "f64"], result: "f64" }, "sleep_blocking": { parameters: ["u64"], result: "void", nonblocking: true }, - "nonblocking_buffer": { - parameters: ["buffer", "usize"], - result: "void", - nonblocking: true, - }, }); dylib.symbols.print_something(); -const buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); -dylib.symbols.print_buffer(buffer, buffer.length); console.log(dylib.symbols.add_u32(123, 456)); console.log(dylib.symbols.add_i32(123, 456)); console.log(dylib.symbols.add_u64(123, 456)); @@ -42,30 +34,6 @@ console.log(dylib.symbols.add_f32(123.123, 456.789)); console.log(dylib.symbols.add_f64(123.123, 456.789)); // Test non blocking calls - -function deferred() { - let methods; - const promise = new Promise((resolve, reject) => { - methods = { - async resolve(value) { - await value; - resolve(value); - }, - reject(reason) { - reject(reason); - }, - }; - }); - return Object.assign(promise, methods); -} - -const promise = deferred(); -const buffer2 = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); -dylib.symbols.nonblocking_buffer(buffer2, buffer2.length).then(() => { - promise.resolve(); -}); -await promise; - const start = performance.now(); dylib.symbols.sleep_blocking(100).then(() => { console.log("After");