Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support f32, f64 in parameters of function #97

Merged
merged 1 commit into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions deno_bindgen_macro/src/derive_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 12 additions & 0 deletions example/bindings/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions example/bindings_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
add,
add2,
add3,
add4,
OptionStruct,
sleep,
test_buf,
Expand Down Expand Up @@ -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: () => {
Expand Down
39 changes: 20 additions & 19 deletions example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::collections::HashMap;
#[deno_bindgen]
fn add(a: i32, b: i32) -> i32 {
a + b
}
}

// Test Structs
#[deno_bindgen]
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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 {
Expand Down Expand Up @@ -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]
Expand All @@ -206,7 +209,7 @@ fn test_reserved_field() -> TestReservedField {
#[deno_bindgen]
fn test_str_ret() -> String {
String::from("🦕")
}
}

#[deno_bindgen]
pub struct WithRecord {
Expand All @@ -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 }
}