diff options
author | 2022-08-28 21:19:53 +0800 | |
---|---|---|
committer | 2022-08-28 06:19:53 -0700 | |
commit | ce90e0c372f3e92b2a163d24018132aa2ce2ee6b (patch) | |
tree | c9c89bfed4f50838aa84361fe858afa79ca9259a | |
parent | 296fb41e920736041c6c1dec38f1d8c355a402a1 (diff) | |
download | bun-ce90e0c372f3e92b2a163d24018132aa2ce2ee6b.tar.gz bun-ce90e0c372f3e92b2a163d24018132aa2ce2ee6b.tar.zst bun-ce90e0c372f3e92b2a163d24018132aa2ce2ee6b.zip |
support pass null as NULL ptr (#1160)
-rw-r--r-- | src/bun.js/api/FFI.h | 6 | ||||
-rw-r--r-- | src/bun.js/ffi.exports.js | 2 | ||||
-rw-r--r-- | test/bun.js/ffi-test.c | 1 | ||||
-rw-r--r-- | test/bun.js/ffi.test.fixture.callback.c | 6 | ||||
-rw-r--r-- | test/bun.js/ffi.test.fixture.receiver.c | 6 | ||||
-rw-r--r-- | test/bun.js/ffi.test.js | 7 |
6 files changed, 27 insertions, 1 deletions
diff --git a/src/bun.js/api/FFI.h b/src/bun.js/api/FFI.h index d7776dd23..bbe13bb5a 100644 --- a/src/bun.js/api/FFI.h +++ b/src/bun.js/api/FFI.h @@ -148,11 +148,17 @@ static bool JSVALUE_IS_NUMBER(EncodedJSValue val) { static void* JSVALUE_TO_PTR(EncodedJSValue val) { // must be a double + if (val.asInt64 == TagValueNull) + return 0; return (void*)(val.asInt64 - DoubleEncodeOffset); } static EncodedJSValue PTR_TO_JSVALUE(void* ptr) { EncodedJSValue val; + if (ptr == 0) { + val.asInt64 = TagValueNull; + return val; + } val.asInt64 = (int64_t)ptr + DoubleEncodeOffset; return val; } diff --git a/src/bun.js/ffi.exports.js b/src/bun.js/ffi.exports.js index f149cc0a9..52dd6d0d4 100644 --- a/src/bun.js/ffi.exports.js +++ b/src/bun.js/ffi.exports.js @@ -175,7 +175,7 @@ ffiWrappers[FFIType.cstring] = ffiWrappers[FFIType.pointer] = function pointer( ) { if (typeof val === "number") return val; if (!val) { - return 0; + return null; } if (ArrayBuffer.isView(val) || val instanceof ArrayBuffer) { diff --git a/test/bun.js/ffi-test.c b/test/bun.js/ffi-test.c index e508b2c11..0fe227385 100644 --- a/test/bun.js/ffi-test.c +++ b/test/bun.js/ffi-test.c @@ -118,6 +118,7 @@ void *getDeallocatorBuffer() { } int getDeallocatorCalledCount() { return deallocatorCalled; } +bool is_null(int32_t *ptr) { return ptr == NULL; } bool does_pointer_equal_42_as_int32_t(int32_t *ptr); bool does_pointer_equal_42_as_int32_t(int32_t *ptr) { return *ptr == 42; } diff --git a/test/bun.js/ffi.test.fixture.callback.c b/test/bun.js/ffi.test.fixture.callback.c index bd912dde2..f6e74f8ba 100644 --- a/test/bun.js/ffi.test.fixture.callback.c +++ b/test/bun.js/ffi.test.fixture.callback.c @@ -149,11 +149,17 @@ static bool JSVALUE_IS_NUMBER(EncodedJSValue val) { static void* JSVALUE_TO_PTR(EncodedJSValue val) { // must be a double + if (val.asInt64 == TagValueNull) + return 0; return (void*)(val.asInt64 - DoubleEncodeOffset); } static EncodedJSValue PTR_TO_JSVALUE(void* ptr) { EncodedJSValue val; + if (ptr == 0) { + val.asInt64 = TagValueNull; + return val; + } val.asInt64 = (int64_t)ptr + DoubleEncodeOffset; return val; } diff --git a/test/bun.js/ffi.test.fixture.receiver.c b/test/bun.js/ffi.test.fixture.receiver.c index 6e1795919..3e2939a26 100644 --- a/test/bun.js/ffi.test.fixture.receiver.c +++ b/test/bun.js/ffi.test.fixture.receiver.c @@ -150,11 +150,17 @@ static bool JSVALUE_IS_NUMBER(EncodedJSValue val) { static void* JSVALUE_TO_PTR(EncodedJSValue val) { // must be a double + if (val.asInt64 == TagValueNull) + return 0; return (void*)(val.asInt64 - DoubleEncodeOffset); } static EncodedJSValue PTR_TO_JSVALUE(void* ptr) { EncodedJSValue val; + if (ptr == 0) { + val.asInt64 = TagValueNull; + return val; + } val.asInt64 = (int64_t)ptr + DoubleEncodeOffset; return val; } diff --git a/test/bun.js/ffi.test.js b/test/bun.js/ffi.test.js index 424670b37..4e8ab42a7 100644 --- a/test/bun.js/ffi.test.js +++ b/test/bun.js/ffi.test.js @@ -206,6 +206,11 @@ function getTypes(fast) { args: ["uint32_t", "uint32_t"], }, + is_null: { + returns: "bool", + args: ["ptr"], + }, + does_pointer_equal_42_as_int32_t: { returns: "bool", args: ["ptr"], @@ -339,6 +344,7 @@ function ffiRunner(fast) { identity_ptr, add_uint32_t, add_uint64_t, + is_null, does_pointer_equal_42_as_int32_t, ptr_should_point_to_42_as_int32_t, cb_identity_true, @@ -460,6 +466,7 @@ function ffiRunner(fast) { expect(add_uint16_t(1, 1)).toBe(2); expect(add_uint32_t(1, 1)).toBe(2); Bun.gc(true); + expect(is_null(null)).toBe(true); const cptr = ptr_should_point_to_42_as_int32_t(); expect(cptr != 0).toBe(true); expect(typeof cptr === "number").toBe(true); |