aboutsummaryrefslogtreecommitdiff
path: root/integration/bunjs-only-snippets/ffi.test.js
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-04-29 05:02:01 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-04-29 05:02:01 -0700
commitf07463bdfdcdd312e0d2aaf2f73bb71646f5f8a3 (patch)
treef7820fa748d40bbad4620f26e59149623a412a9b /integration/bunjs-only-snippets/ffi.test.js
parent75374fdc32b547890b5d04f98ca253232e73bf4b (diff)
downloadbun-f07463bdfdcdd312e0d2aaf2f73bb71646f5f8a3.tar.gz
bun-f07463bdfdcdd312e0d2aaf2f73bb71646f5f8a3.tar.zst
bun-f07463bdfdcdd312e0d2aaf2f73bb71646f5f8a3.zip
[bun ffi] support `i32`, `i8`, `u8`, `u16`, `i16`, `u32`, `bool`
Diffstat (limited to 'integration/bunjs-only-snippets/ffi.test.js')
-rw-r--r--integration/bunjs-only-snippets/ffi.test.js302
1 files changed, 165 insertions, 137 deletions
diff --git a/integration/bunjs-only-snippets/ffi.test.js b/integration/bunjs-only-snippets/ffi.test.js
index 83a6c8950..1a18ae2cd 100644
--- a/integration/bunjs-only-snippets/ffi.test.js
+++ b/integration/bunjs-only-snippets/ffi.test.js
@@ -10,197 +10,225 @@ it("ffi print", () => {
});
it("ffi run", () => {
- const {
- symbols: {
- returns_true,
- returns_false,
- returns_42_char,
- returns_42_float,
- returns_42_double,
- returns_42_uint8_t,
- returns_neg_42_int8_t,
- returns_42_uint16_t,
- returns_42_uint32_t,
- returns_42_uint64_t,
- returns_neg_42_int16_t,
- returns_neg_42_int32_t,
- returns_neg_42_int64_t,
- identity_char,
- identity_float,
- identity_bool,
- identity_double,
- identity_int8_t,
- identity_int16_t,
- identity_int32_t,
- identity_int64_t,
- identity_uint8_t,
- identity_uint16_t,
- identity_uint32_t,
- identity_uint64_t,
- add_char,
- add_float,
- add_double,
- add_int8_t,
- add_int16_t,
- add_int32_t,
- add_int64_t,
- add_uint8_t,
- add_uint16_t,
- add_uint32_t,
- add_uint64_t,
- },
- close,
- } = Bun.dlopen("/tmp/bun-ffi-test.dylib", {
+ const types = {
returns_true: {
- returns: "bool",
- expects: [],
+ return_type: "bool",
+ params: [],
},
returns_false: {
- returns: "bool",
- expects: [],
+ return_type: "bool",
+ params: [],
},
returns_42_char: {
- returns: "char",
- expects: [],
- },
- returns_42_float: {
- returns: "float",
- expects: [],
- },
- returns_42_double: {
- returns: "double",
- expects: [],
- },
+ return_type: "char",
+ params: [],
+ },
+ // returns_42_float: {
+ // return_type: "float",
+ // params: [],
+ // },
+ // returns_42_double: {
+ // return_type: "double",
+ // params: [],
+ // },
returns_42_uint8_t: {
- returns: "uint8_t",
- expects: [],
+ return_type: "uint8_t",
+ params: [],
},
returns_neg_42_int8_t: {
- returns: "int8_t",
- expects: [],
+ return_type: "int8_t",
+ params: [],
},
returns_42_uint16_t: {
- returns: "uint16_t",
- expects: [],
+ return_type: "uint16_t",
+ params: [],
},
returns_42_uint32_t: {
- returns: "uint32_t",
- expects: [],
- },
- returns_42_uint64_t: {
- returns: "uint64_t",
- expects: [],
+ return_type: "uint32_t",
+ params: [],
},
+ // // returns_42_uint64_t: {
+ // // return_type: "uint64_t",
+ // // params: [],
+ // // },
returns_neg_42_int16_t: {
- returns: "int16_t",
- expects: [],
+ return_type: "int16_t",
+ params: [],
},
returns_neg_42_int32_t: {
- returns: "int32_t",
- expects: [],
- },
- returns_neg_42_int64_t: {
- returns: "int64_t",
- expects: [],
+ return_type: "int32_t",
+ params: [],
},
+ // returns_neg_42_int64_t: {
+ // return_type: "int64_t",
+ // params: [],
+ // },
identity_char: {
- returns: "char",
- expects: ["char"],
- },
- identity_float: {
- returns: "float",
- expects: ["float"],
+ return_type: "char",
+ params: ["char"],
},
+ // identity_float: {
+ // return_type: "float",
+ // params: ["float"],
+ // },
identity_bool: {
- returns: "bool",
- expects: ["bool"],
- },
- identity_double: {
- returns: "double",
- expects: ["double"],
+ return_type: "bool",
+ params: ["bool"],
},
+ // identity_double: {
+ // return_type: "double",
+ // params: ["double"],
+ // },
identity_int8_t: {
- returns: "int8_t",
- expects: ["int8_t"],
+ return_type: "int8_t",
+ params: ["int8_t"],
},
identity_int16_t: {
- returns: "int16_t",
- expects: ["int16_t"],
+ return_type: "int16_t",
+ params: ["int16_t"],
},
identity_int32_t: {
- returns: "int32_t",
- expects: ["int32_t"],
- },
- identity_int64_t: {
- returns: "int64_t",
- expects: ["int64_t"],
+ return_type: "int32_t",
+ params: ["int32_t"],
},
+ // identity_int64_t: {
+ // return_type: "int64_t",
+ // params: ["int64_t"],
+ // },
identity_uint8_t: {
- returns: "uint8_t",
- expects: ["uint8_t"],
+ return_type: "uint8_t",
+ params: ["uint8_t"],
},
identity_uint16_t: {
- returns: "uint16_t",
- expects: ["uint16_t"],
+ return_type: "uint16_t",
+ params: ["uint16_t"],
},
identity_uint32_t: {
- returns: "uint32_t",
- expects: ["uint32_t"],
- },
- identity_uint64_t: {
- returns: "uint64_t",
- expects: ["uint64_t"],
+ return_type: "uint32_t",
+ params: ["uint32_t"],
},
+ // identity_uint64_t: {
+ // return_type: "uint64_t",
+ // params: ["uint64_t"],
+ // },
add_char: {
- returns: "char",
- expects: ["char", "char"],
+ return_type: "char",
+ params: ["char", "char"],
},
add_float: {
- returns: "float",
- expects: ["float", "float"],
+ return_type: "float",
+ params: ["float", "float"],
},
add_double: {
- returns: "double",
- expects: ["double", "double"],
+ return_type: "double",
+ params: ["double", "double"],
},
add_int8_t: {
- returns: "int8_t",
- expects: ["int8_t", "int8_t"],
+ return_type: "int8_t",
+ params: ["int8_t", "int8_t"],
},
add_int16_t: {
- returns: "int16_t",
- expects: ["int16_t", "int16_t"],
+ return_type: "int16_t",
+ params: ["int16_t", "int16_t"],
},
add_int32_t: {
- returns: "int32_t",
- expects: ["int32_t", "int32_t"],
- },
- add_int64_t: {
- returns: "int64_t",
- expects: ["int64_t", "int64_t"],
+ return_type: "int32_t",
+ params: ["int32_t", "int32_t"],
},
+ // add_int64_t: {
+ // return_type: "int64_t",
+ // params: ["int64_t", "int64_t"],
+ // },
add_uint8_t: {
- returns: "uint8_t",
- expects: ["uint8_t", "uint8_t"],
+ return_type: "uint8_t",
+ params: ["uint8_t", "uint8_t"],
},
add_uint16_t: {
- returns: "uint16_t",
- expects: ["uint16_t", "uint16_t"],
+ return_type: "uint16_t",
+ params: ["uint16_t", "uint16_t"],
},
add_uint32_t: {
- returns: "uint32_t",
- expects: ["uint32_t", "uint32_t"],
- },
- add_uint64_t: {
- returns: "uint64_t",
- expects: ["uint64_t", "uint64_t"],
+ return_type: "uint32_t",
+ params: ["uint32_t", "uint32_t"],
+ },
+ // add_uint64_t: {
+ // return_type: "uint64_t",
+ // params: ["uint64_t", "uint64_t"],
+ // },
+ };
+ console.log(Bun.dlprint(types)[0]);
+ const {
+ symbols: {
+ returns_true,
+ returns_false,
+ returns_42_char,
+ returns_42_float,
+ returns_42_double,
+ returns_42_uint8_t,
+ returns_neg_42_int8_t,
+ returns_42_uint16_t,
+ returns_42_uint32_t,
+ returns_42_uint64_t,
+ returns_neg_42_int16_t,
+ returns_neg_42_int32_t,
+ returns_neg_42_int64_t,
+ identity_char,
+ identity_float,
+ identity_bool,
+ identity_double,
+ identity_int8_t,
+ identity_int16_t,
+ identity_int32_t,
+ identity_int64_t,
+ identity_uint8_t,
+ identity_uint16_t,
+ identity_uint32_t,
+ identity_uint64_t,
+ add_char,
+ add_float,
+ add_double,
+ add_int8_t,
+ add_int16_t,
+ add_int32_t,
+ add_int64_t,
+ add_uint8_t,
+ add_uint16_t,
+ add_uint32_t,
+ add_uint64_t,
},
- });
+ close,
+ } = Bun.dlopen("/tmp/bun-ffi-test.dylib", types);
+
+ expect(returns_true()).toBe(true);
+ expect(returns_false()).toBe(false);
+ expect(returns_42_char()).toBe(42);
+ // expect(returns_42_float()).toBe(42);
+ // expect(returns_42_double()).toBe(42);
+ expect(returns_42_uint8_t()).toBe(42);
+ expect(returns_neg_42_int8_t()).toBe(-42);
+ expect(returns_42_uint16_t()).toBe(42);
+ expect(returns_42_uint32_t()).toBe(42);
+ // expect(returns_42_uint64_t()).toBe(42);
+ expect(returns_neg_42_int16_t()).toBe(-42);
+ expect(returns_neg_42_int32_t()).toBe(-42);
+ // expect(returns_neg_42_int64_t()).toBe(-42);
+ expect(identity_char(10)).toBe(10);
+ // expect(identity_float(10.1)).toBe(10.1);
+ expect(identity_bool(true)).toBe(true);
+ expect(identity_bool(false)).toBe(false);
+ // expect(identity_double(10.1)).toBe(10.1);
+ expect(identity_int8_t(10)).toBe(10);
+ expect(identity_int16_t(10)).toBe(10);
+ expect(identity_int32_t(10)).toBe(10);
+ // expect(identity_int64_t(10)).toBe(10);
+ expect(identity_uint8_t(10)).toBe(10);
+ expect(identity_uint16_t(10)).toBe(10);
+ expect(identity_uint32_t(10)).toBe(10);
expect(add_char(1, 1)).toBe(2);
- expect(add_float(1.1, 1.1)).toBe(2.2);
- expect(add_double(1.1, 1.1)).toBe(2.2);
+ // expect(add_float(1.1, 1.1)).toBe(2.2);
+ // expect(add_double(1.1, 1.1)).toBe(2.2);
expect(add_int8_t(1, 1)).toBe(2);
expect(add_int16_t(1, 1)).toBe(2);
expect(add_int32_t(1, 1)).toBe(2);