aboutsummaryrefslogtreecommitdiff
path: root/integration/bunjs-only-snippets
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-05-02 06:26:03 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-05-02 06:26:03 -0700
commit21ab47d9fe1085b7a1a959df810386367ebd5c23 (patch)
tree99773c7fc26cc253111fb13ccccaffbf2f28fa2f /integration/bunjs-only-snippets
parentd70e92e79f3940f80bee186a6c7c3c8ef23aed00 (diff)
downloadbun-21ab47d9fe1085b7a1a959df810386367ebd5c23.tar.gz
bun-21ab47d9fe1085b7a1a959df810386367ebd5c23.tar.zst
bun-21ab47d9fe1085b7a1a959df810386367ebd5c23.zip
[bun:ffi] Support `i64` and `u64`
Diffstat (limited to 'integration/bunjs-only-snippets')
-rw-r--r--integration/bunjs-only-snippets/ffi.test.fixture.callback.c34
-rw-r--r--integration/bunjs-only-snippets/ffi.test.fixture.receiver.c34
-rw-r--r--integration/bunjs-only-snippets/ffi.test.js113
3 files changed, 80 insertions, 101 deletions
diff --git a/integration/bunjs-only-snippets/ffi.test.fixture.callback.c b/integration/bunjs-only-snippets/ffi.test.fixture.callback.c
index a0da35f62..bc31d8b1c 100644
--- a/integration/bunjs-only-snippets/ffi.test.fixture.callback.c
+++ b/integration/bunjs-only-snippets/ffi.test.fixture.callback.c
@@ -38,28 +38,6 @@ typedef _Bool bool;
#define true 1
#define false 0
-#ifdef USES_FLOAT
-// https://git.musl-libc.org/cgit/musl/tree/src/math/trunc.c
-double trunc(double x);
-double trunc(double x)
-{
- union {double f; uint64_t i;} u = {x};
- int e = (int)(u.i >> 52 & 0x7ff) - 0x3ff + 12;
- uint64_t m;
-
- if (e >= 52 + 12)
- return x;
- if (e < 12)
- e = 1;
- m = -1ULL >> e;
- if ((u.i & m) == 0)
- return x;
- x + 0x1p120f;
- u.i &= ~m;
- return u.f;
-}
-#endif
-
// This value is 2^49, used to encode doubles such that the encoded value will
// begin with a 15-bit pattern within the range 0x0002..0xFFFC.
@@ -82,7 +60,6 @@ typedef void* JSCell;
typedef union EncodedJSValue {
int64_t asInt64;
#if USE_JSVALUE32_64
- double asDouble;
#elif USE_JSVALUE64
JSCell *ptr;
#endif
@@ -117,6 +94,11 @@ JSContext cachedJSContext;
void* cachedCallbackFunction;
#endif
+uint64_t JSVALUE_TO_UINT64(void* globalObject, EncodedJSValue value);
+int64_t JSVALUE_TO_INT64(EncodedJSValue value);
+
+EncodedJSValue UINT64_TO_JSVALUE(void* globalObject, uint64_t val);
+EncodedJSValue INT64_TO_JSVALUE(void* globalObject, int64_t val);
static EncodedJSValue INT32_TO_JSVALUE(int32_t val) __attribute__((__always_inline__));
static EncodedJSValue DOUBLE_TO_JSVALUE(double val) __attribute__((__always_inline__));
@@ -184,11 +166,9 @@ static bool JSVALUE_TO_BOOL(EncodedJSValue val) {
}
#define arg(i) ((EncodedJSValue*)args)[i]
+#ifndef IS_CALLBACK
void* JSFunctionCall(void* globalObject, void* callFrame);
-// int64_t JSFunctionCall(void* globalObject, void* callFrame) {
-// EncodedJSValue* args = (EncodedJSValue*)((unsigned char*)callFrame + Bun_FFI_PointerOffsetToArgumentsList);
-// }
-
+#endif
// --- Generated Code ---
diff --git a/integration/bunjs-only-snippets/ffi.test.fixture.receiver.c b/integration/bunjs-only-snippets/ffi.test.fixture.receiver.c
index 0f7047ab5..121f2d3a0 100644
--- a/integration/bunjs-only-snippets/ffi.test.fixture.receiver.c
+++ b/integration/bunjs-only-snippets/ffi.test.fixture.receiver.c
@@ -38,28 +38,6 @@ typedef _Bool bool;
#define true 1
#define false 0
-#ifdef USES_FLOAT
-// https://git.musl-libc.org/cgit/musl/tree/src/math/trunc.c
-double trunc(double x);
-double trunc(double x)
-{
- union {double f; uint64_t i;} u = {x};
- int e = (int)(u.i >> 52 & 0x7ff) - 0x3ff + 12;
- uint64_t m;
-
- if (e >= 52 + 12)
- return x;
- if (e < 12)
- e = 1;
- m = -1ULL >> e;
- if ((u.i & m) == 0)
- return x;
- x + 0x1p120f;
- u.i &= ~m;
- return u.f;
-}
-#endif
-
// This value is 2^49, used to encode doubles such that the encoded value will
// begin with a 15-bit pattern within the range 0x0002..0xFFFC.
@@ -82,7 +60,6 @@ typedef void* JSCell;
typedef union EncodedJSValue {
int64_t asInt64;
#if USE_JSVALUE32_64
- double asDouble;
#elif USE_JSVALUE64
JSCell *ptr;
#endif
@@ -117,6 +94,11 @@ JSContext cachedJSContext;
void* cachedCallbackFunction;
#endif
+uint64_t JSVALUE_TO_UINT64(void* globalObject, EncodedJSValue value);
+int64_t JSVALUE_TO_INT64(EncodedJSValue value);
+
+EncodedJSValue UINT64_TO_JSVALUE(void* globalObject, uint64_t val);
+EncodedJSValue INT64_TO_JSVALUE(void* globalObject, int64_t val);
static EncodedJSValue INT32_TO_JSVALUE(int32_t val) __attribute__((__always_inline__));
static EncodedJSValue DOUBLE_TO_JSVALUE(double val) __attribute__((__always_inline__));
@@ -184,11 +166,9 @@ static bool JSVALUE_TO_BOOL(EncodedJSValue val) {
}
#define arg(i) ((EncodedJSValue*)args)[i]
+#ifndef IS_CALLBACK
void* JSFunctionCall(void* globalObject, void* callFrame);
-// int64_t JSFunctionCall(void* globalObject, void* callFrame) {
-// EncodedJSValue* args = (EncodedJSValue*)((unsigned char*)callFrame + Bun_FFI_PointerOffsetToArgumentsList);
-// }
-
+#endif
// --- Generated Code ---
diff --git a/integration/bunjs-only-snippets/ffi.test.js b/integration/bunjs-only-snippets/ffi.test.js
index 597c0ef1f..b95bcfa8e 100644
--- a/integration/bunjs-only-snippets/ffi.test.js
+++ b/integration/bunjs-only-snippets/ffi.test.js
@@ -95,10 +95,10 @@ it("ffi run", () => {
return_type: "uint32_t",
args: [],
},
- // // returns_42_uint64_t: {
- // // return_type: "uint64_t",
- // // args: [],
- // // },
+ returns_42_uint64_t: {
+ return_type: "uint64_t",
+ args: [],
+ },
returns_neg_42_int16_t: {
return_type: "int16_t",
args: [],
@@ -107,10 +107,10 @@ it("ffi run", () => {
return_type: "int32_t",
args: [],
},
- // returns_neg_42_int64_t: {
- // return_type: "int64_t",
- // args: [],
- // },
+ returns_neg_42_int64_t: {
+ return_type: "int64_t",
+ args: [],
+ },
identity_char: {
return_type: "char",
@@ -140,10 +140,10 @@ it("ffi run", () => {
return_type: "int32_t",
args: ["int32_t"],
},
- // identity_int64_t: {
- // return_type: "int64_t",
- // args: ["int64_t"],
- // },
+ identity_int64_t: {
+ return_type: "int64_t",
+ args: ["int64_t"],
+ },
identity_uint8_t: {
return_type: "uint8_t",
args: ["uint8_t"],
@@ -156,10 +156,10 @@ it("ffi run", () => {
return_type: "uint32_t",
args: ["uint32_t"],
},
- // identity_uint64_t: {
- // return_type: "uint64_t",
- // args: ["uint64_t"],
- // },
+ identity_uint64_t: {
+ return_type: "uint64_t",
+ args: ["uint64_t"],
+ },
add_char: {
return_type: "char",
@@ -185,10 +185,10 @@ it("ffi run", () => {
return_type: "int32_t",
args: ["int32_t", "int32_t"],
},
- // add_int64_t: {
- // return_type: "int64_t",
- // args: ["int64_t", "int64_t"],
- // },
+ add_int64_t: {
+ return_type: "int64_t",
+ args: ["int64_t", "int64_t"],
+ },
add_uint8_t: {
return_type: "uint8_t",
args: ["uint8_t", "uint8_t"],
@@ -215,10 +215,10 @@ it("ffi run", () => {
return_type: "ptr",
args: ["ptr"],
},
- // add_uint64_t: {
- // return_type: "uint64_t",
- // args: ["uint64_t", "uint64_t"],
- // },
+ add_uint64_t: {
+ return_type: "uint64_t",
+ args: ["uint64_t", "uint64_t"],
+ },
cb_identity_true: {
return_type: "bool",
@@ -232,14 +232,14 @@ it("ffi run", () => {
return_type: "char",
args: ["ptr"],
},
- // cb_identity_42_float: {
- // return_type: "float",
- // args: ["ptr"],
- // },
- // cb_identity_42_double: {
- // return_type: "double",
- // args: ["ptr"],
- // },
+ cb_identity_42_float: {
+ return_type: "float",
+ args: ["ptr"],
+ },
+ cb_identity_42_double: {
+ return_type: "double",
+ args: ["ptr"],
+ },
cb_identity_42_uint8_t: {
return_type: "uint8_t",
args: ["ptr"],
@@ -256,10 +256,10 @@ it("ffi run", () => {
return_type: "uint32_t",
args: ["ptr"],
},
- // cb_identity_42_uint64_t: {
- // return_type: "uint64_t",
- // args: ["ptr"],
- // },
+ cb_identity_42_uint64_t: {
+ return_type: "uint64_t",
+ args: ["ptr"],
+ },
cb_identity_neg_42_int16_t: {
return_type: "int16_t",
args: ["ptr"],
@@ -268,10 +268,10 @@ it("ffi run", () => {
return_type: "int32_t",
args: ["ptr"],
},
- // cb_identity_neg_42_int64_t: {
- // return_type: "int64_t",
- // args: ["ptr"],
- // },
+ cb_identity_neg_42_int64_t: {
+ return_type: "int64_t",
+ args: ["ptr"],
+ },
return_a_function_ptr_to_function_that_returns_true: {
return_type: "ptr",
@@ -340,6 +340,7 @@ it("ffi run", () => {
expect(returns_true()).toBe(true);
expect(returns_false()).toBe(false);
expect(returns_42_char()).toBe(42);
+ expect(returns_42_uint64_t().valueOf()).toBe(42);
expect(Math.fround(returns_42_float())).toBe(Math.fround(42.41999804973602));
@@ -348,11 +349,11 @@ it("ffi run", () => {
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_42_uint64_t()).toBe(42);
expect(returns_neg_42_int16_t()).toBe(-42);
expect(returns_neg_42_int32_t()).toBe(-42);
expect(identity_int32_t(10)).toBe(10);
- // expect(returns_neg_42_int64_t()).toBe(-42);
+ expect(returns_neg_42_int64_t()).toBe(-42);
expect(identity_char(10)).toBe(10);
expect(identity_float(10.199999809265137)).toBe(10.199999809265137);
expect(identity_bool(true)).toBe(true);
@@ -360,18 +361,38 @@ it("ffi run", () => {
expect(identity_double(10.100000000000364)).toBe(10.100000000000364);
expect(identity_int8_t(10)).toBe(10);
expect(identity_int16_t(10)).toBe(10);
- console.log("here");
- // expect(identity_int64_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(identity_uint64_t(10)).toBe(10);
+ var bigArray = new BigUint64Array(8);
+ new Uint8Array(bigArray.buffer).fill(255);
+ var bigIntArray = new BigInt64Array(bigArray.buffer);
+
+ expect(identity_uint64_t(bigArray[0])).toBe(bigArray[0]);
+ expect(identity_uint64_t(bigArray[0] - BigInt(1))).toBe(
+ bigArray[0] - BigInt(1)
+ );
+ expect(add_uint64_t(BigInt(-1) * bigArray[0], bigArray[0])).toBe(0);
+ expect(add_uint64_t(BigInt(-1) * bigArray[0] + BigInt(10), bigArray[0])).toBe(
+ 10
+ );
+ expect(identity_uint64_t(0)).toBe(0);
+ expect(identity_uint64_t(100)).toBe(100);
+ expect(identity_uint64_t(BigInt(100))).toBe(100);
+ expect(identity_int64_t(bigIntArray[0])).toBe(bigIntArray[0]);
+ expect(identity_int64_t(bigIntArray[0] - BigInt(1))).toBe(
+ bigIntArray[0] - BigInt(1)
+ );
+
expect(add_char(1, 1)).toBe(2);
expect(add_float(2.4, 2.8)).toBe(Math.fround(5.2));
expect(add_double(4.2, 0.1)).toBe(4.3);
expect(add_int8_t(1, 1)).toBe(2);
expect(add_int16_t(1, 1)).toBe(2);
expect(add_int32_t(1, 1)).toBe(2);
- // expect(add_int64_t(1, 1)).toBe(2);
+ expect(add_int64_t(1, 1)).toBe(2);
expect(add_uint8_t(1, 1)).toBe(2);
expect(add_uint16_t(1, 1)).toBe(2);
expect(add_uint32_t(1, 1)).toBe(2);
@@ -391,7 +412,6 @@ it("ffi run", () => {
const second_ptr = ptr(new Buffer(8));
expect(identity_ptr(second_ptr)).toBe(second_ptr);
function identityBool() {
- console.log("hi");
return true;
}
globalThis.identityBool = identityBool;
@@ -408,7 +428,6 @@ it("ffi run", () => {
).toBe(true);
expect(cb_identity_true(first)).toBe(true);
- console.log("second");
expect(
cb_identity_false(