diff --git a/README.md b/README.md index c18cd89..d8bdda8 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,10 @@ Notes }; ``` +- If the argument type of Rust is f32, the calculation result may be different. + Number in Java Script is float64, when data is passed to Rust, it becomes float32, so the number may change. + e.g: `1.3 + 1.5` will be `2.799999952316284` + ### CLI The `deno_bindgen` CLI tool provides the following flags: diff --git a/deno_bindgen_macro/src/derive_fn.rs b/deno_bindgen_macro/src/derive_fn.rs index 8873802..65d5686 100644 --- a/deno_bindgen_macro/src/derive_fn.rs +++ b/deno_bindgen_macro/src/derive_fn.rs @@ -38,6 +38,8 @@ pub fn process_function( "u64" => Type::U64, "usize" => Type::Usize, "isize" => Type::Isize, + "f32" => Type::F32, + "f64" => Type::F64, _ => { metadata.type_defs.get(&ident).expect(&format!( "Type definition not found for `{}` identifier", diff --git a/example/bindings/bindings.ts b/example/bindings/bindings.ts index 94c1ad5..b37f0c6 100644 --- a/example/bindings/bindings.ts +++ b/example/bindings/bindings.ts @@ -48,6 +48,8 @@ const opts = { const _lib = await prepare(opts, { add: { parameters: ["i32", "i32"], result: "i32", nonblocking: false }, add2: { parameters: ["pointer", "usize"], result: "i32", nonblocking: false }, + add3: { parameters: ["f32", "f32"], result: "f32", nonblocking: false }, + add4: { parameters: ["f64", "f64"], result: "f64", nonblocking: false }, sleep: { parameters: ["u64"], result: "void", nonblocking: true }, test_buf: { parameters: ["buffer", "usize"], @@ -175,6 +177,16 @@ export function add2(a0: Input) { const result = rawResult return result } +export function add3(a0: number, a1: number) { + let rawResult = _lib.symbols.add3(a0, a1) + const result = rawResult + return result +} +export function add4(a0: number, a1: number) { + let rawResult = _lib.symbols.add4(a0, a1) + const result = rawResult + return result +} export function sleep(a0: bigint) { let rawResult = _lib.symbols.sleep(a0) const result = rawResult diff --git a/example/bindings_test.ts b/example/bindings_test.ts index e57f091..39d4abc 100644 --- a/example/bindings_test.ts +++ b/example/bindings_test.ts @@ -1,6 +1,8 @@ import { add, add2, + add3, + add4, OptionStruct, sleep, test_buf, @@ -40,6 +42,23 @@ Deno.test({ }, }); +Deno.test({ + name: "add#test3", + fn: () => { + // If the argument type of Rust is f32, the calculation result may be different. + // Number in Java Script is float64, when data is passed to Rust, it becomes float32, so the number may change. + // e.g: `1.3 + 1.5` will be `2.799999952316284` + assertEquals(add3(1.5, 1.5), 3.0); + }, +}); + +Deno.test({ + name: "add#test4", + fn: () => { + assertEquals(add4(1.5, 1.3), 2.8); + }, +}); + Deno.test({ name: "test_mixed#test", fn: () => { diff --git a/example/src/lib.rs b/example/src/lib.rs index d4da328..22c2a9a 100644 --- a/example/src/lib.rs +++ b/example/src/lib.rs @@ -5,7 +5,7 @@ use std::collections::HashMap; #[deno_bindgen] fn add(a: i32, b: i32) -> i32 { a + b -} +} // Test Structs #[deno_bindgen] @@ -24,6 +24,16 @@ fn add2(input: Input) -> i32 { input.a + input.b } +#[deno_bindgen] +fn add3(a: f32, b: f32) -> f32 { + a + b +} + +#[deno_bindgen] +fn add4(a: f64, b: f64) -> f64 { + a + b +} + // Test mixed types #[deno_bindgen] fn test_mixed(a: isize, b: Input) -> i32 { @@ -84,7 +94,7 @@ enum PlainEnum { // Test mut buffer #[deno_bindgen] fn test_mut_buf(buf: &mut [u8]) { - buf[0] = 69; + buf[0] = 69; } // Test mut buffer prevent return @@ -96,7 +106,7 @@ fn test_mut_buf(buf: &mut [u8]) { // Test mut buffer musn't outlive symbol call // #[deno_bindgen] // fn test_mut_buf_outlive(_: &'static mut [u8]) { -// +// // } #[deno_bindgen] @@ -106,13 +116,13 @@ struct TestLifetimes<'l> { #[deno_bindgen] enum TestLifetimeEnums<'a> { - Text { _text: &'a str } + Text { _text: &'a str }, } #[deno_bindgen] struct TestLifetimeWrap<'a> { #[serde(borrow)] - _a: TestLifetimeEnums<'a> + _a: TestLifetimeEnums<'a>, } #[deno_bindgen] @@ -124,10 +134,9 @@ fn test_lifetime<'l>(s: TestLifetimes<'l>) -> usize { #[serde(tag = "key", content = "value")] pub enum TagAndContent { A { b: i32 }, - C { d: i32 } + C { d: i32 }, } - #[deno_bindgen] fn test_tag_and_content(arg: TagAndContent) -> i32 { if let TagAndContent::A { b } = arg { @@ -175,18 +184,12 @@ fn test_manual_ptr_async() -> *const u8 { #[deno_bindgen] fn test_output() -> Input { - Input { - a: 1, - b: 2 - } + Input { a: 1, b: 2 } } #[deno_bindgen(non_blocking)] fn test_output_async() -> Input { - Input { - a: 3, - b: 4 - } + Input { a: 3, b: 4 } } #[deno_bindgen] @@ -206,7 +209,7 @@ fn test_reserved_field() -> TestReservedField { #[deno_bindgen] fn test_str_ret() -> String { String::from("🦕") -} +} #[deno_bindgen] pub struct WithRecord { @@ -217,7 +220,5 @@ pub struct WithRecord { fn test_hashmap() -> WithRecord { let mut map = HashMap::new(); map.insert("key".to_string(), "value".to_string()); - WithRecord { - my_map: map, - } + WithRecord { my_map: map } }