diff --git a/Cargo.lock b/Cargo.lock index d775bb0bf3..40c405d805 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3280,9 +3280,9 @@ dependencies = [ [[package]] name = "serde_v8" -version = "0.9.0" +version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88bf7bf03d60f6c5098d2d1867404ff50695435f638c34e4a95b8b3631cb4900" +checksum = "f5ce7662cda194ff443bddf146c952e83075889590838cd41df768fba7d152d0" dependencies = [ "rusty_v8", "serde", diff --git a/cli/tests/unit/ffi_test.ts b/cli/tests/unit/ffi_test.ts new file mode 100644 index 0000000000..ab25df699e --- /dev/null +++ b/cli/tests/unit/ffi_test.ts @@ -0,0 +1,25 @@ +// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. + +import { assertThrows, unitTest } from "./test_util.ts"; + +unitTest(function dlopenInvalidArguments() { + const filename = "/usr/lib/libc.so.6"; + assertThrows(() => { + // @ts-expect-error: ForeignFunction cannot be null + Deno.dlopen(filename, { malloc: null }); + }, TypeError); + assertThrows(() => { + Deno.dlopen(filename, { + // @ts-expect-error: invalid NativeType + malloc: { parameters: ["a"], result: "b" }, + }); + }, TypeError); + assertThrows(() => { + // @ts-expect-error: DynamicLibrary symbols cannot be null + Deno.dlopen(filename, null); + }, TypeError); + assertThrows(() => { + // @ts-expect-error: require 2 arguments + Deno.dlopen(filename); + }, TypeError); +}); diff --git a/core/Cargo.toml b/core/Cargo.toml index 7ddb7d8147..78fbf18368 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -25,7 +25,7 @@ pin-project = "1.0.7" rusty_v8 = "0.26.0" serde = { version = "1.0.126", features = ["derive"] } serde_json = { version = "1.0.64", features = ["preserve_order"] } -serde_v8 = { version = "0.9.0" } +serde_v8 = { version = "0.9.3" } url = { version = "2.2.2", features = ["serde"] } [[example]] diff --git a/ext/ffi/lib.rs b/ext/ffi/lib.rs index cfff59953e..673f834722 100644 --- a/ext/ffi/lib.rs +++ b/ext/ffi/lib.rs @@ -75,12 +75,13 @@ impl DynamicLibraryResource { ) -> Result<(), AnyError> { let fn_ptr = unsafe { self.lib.symbol::<*const c_void>(&symbol) }?; let ptr = libffi::middle::CodePtr::from_ptr(fn_ptr as _); - let parameter_types = - foreign_fn.parameters.into_iter().map(NativeType::from); - let result_type = NativeType::from(foreign_fn.result); let cif = libffi::middle::Cif::new( - parameter_types.clone().map(libffi::middle::Type::from), - result_type.into(), + foreign_fn + .parameters + .clone() + .into_iter() + .map(libffi::middle::Type::from), + foreign_fn.result.into(), ); self.symbols.insert( @@ -88,8 +89,8 @@ impl DynamicLibraryResource { Symbol { cif, ptr, - parameter_types: parameter_types.collect(), - result_type, + parameter_types: foreign_fn.parameters, + result_type: foreign_fn.result, }, ); @@ -153,27 +154,6 @@ impl From for libffi::middle::Type { } } -impl From for NativeType { - fn from(string: String) -> Self { - match string.as_str() { - "void" => NativeType::Void, - "u8" => NativeType::U8, - "i8" => NativeType::I8, - "u16" => NativeType::U16, - "i16" => NativeType::I16, - "u32" => NativeType::U32, - "i32" => NativeType::I32, - "u64" => NativeType::U64, - "i64" => NativeType::I64, - "usize" => NativeType::USize, - "isize" => NativeType::ISize, - "f32" => NativeType::F32, - "f64" => NativeType::F64, - _ => unimplemented!(), - } - } -} - #[repr(C)] union NativeValue { void_value: (), @@ -279,8 +259,8 @@ fn value_as_f64(value: Value) -> f64 { #[derive(Deserialize, Debug)] struct ForeignFunction { - parameters: Vec, - result: String, + parameters: Vec, + result: NativeType, } #[derive(Deserialize, Debug)]