[bun:ffi] Support i64 and u64

This commit is contained in:
Jarred Sumner
2022-05-02 06:26:03 -07:00
parent d70e92e79f
commit 21ab47d9fe
11 changed files with 212 additions and 144 deletions

View File

@@ -37,28 +37,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.
@@ -81,7 +59,6 @@ typedef void* JSCell;
typedef union EncodedJSValue {
int64_t asInt64;
#if USE_JSVALUE32_64
double asDouble;
#elif USE_JSVALUE64
JSCell *ptr;
#endif
@@ -116,6 +93,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__));
@@ -183,11 +165,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 ---

View File

@@ -591,6 +591,21 @@ pub const FFI = struct {
extern fn pthread_jit_write_protect_np(enable: bool) callconv(.C) void;
const MyFunctionSStructWorkAround = struct {
JSVALUE_TO_INT64: fn (JSValue0: JSC.JSValue) callconv(.C) i64,
JSVALUE_TO_UINT64: fn (JSValue0: JSC.JSValue) callconv(.C) u64,
INT64_TO_JSVALUE: fn (arg0: [*c]JSC.JSGlobalObject, arg1: i64) callconv(.C) JSC.JSValue,
UINT64_TO_JSVALUE: fn (arg0: [*c]JSC.JSGlobalObject, arg1: u64) callconv(.C) JSC.JSValue,
};
const headers = @import("../bindings/headers.zig");
var workaround: MyFunctionSStructWorkAround = .{
.JSVALUE_TO_INT64 = headers.JSC__JSValue__toInt64,
.JSVALUE_TO_UINT64 = headers.JSC__JSValue__toUInt64NoTruncate,
.INT64_TO_JSVALUE = headers.JSC__JSValue__fromInt64NoTruncate,
.UINT64_TO_JSVALUE = headers.JSC__JSValue__fromUInt64NoTruncate,
};
const tcc_options = "-std=c11 -nostdlib -Wl,--export-all-symbols";
pub fn compile(
@@ -625,6 +640,7 @@ pub const FFI = struct {
"Bun_FFI_PointerOffsetToArgumentsList",
std.fmt.bufPrintZ(&symbol_buf, "{d}", .{Sizes.Bun_FFI_PointerOffsetToArgumentsList}) catch unreachable,
);
// TCC.tcc_define_symbol(
// state,
// "Bun_FFI_PointerOffsetToArgumentsCount",
@@ -647,7 +663,31 @@ pub const FFI = struct {
}
CompilerRT.inject(state);
_ = TCC.tcc_add_symbol(state, this.base_name, this.symbol_from_dynamic_library.?);
_ = TCC.tcc_add_symbol(
state,
"JSVALUE_TO_INT64",
workaround.JSVALUE_TO_INT64,
);
_ = TCC.tcc_add_symbol(
state,
"JSVALUE_TO_UINT64",
workaround.JSVALUE_TO_UINT64,
);
std.mem.doNotOptimizeAway(headers.JSC__JSValue__toUInt64NoTruncate);
std.mem.doNotOptimizeAway(headers.JSC__JSValue__toInt64);
std.mem.doNotOptimizeAway(headers.JSC__JSValue__fromInt64NoTruncate);
std.mem.doNotOptimizeAway(headers.JSC__JSValue__fromUInt64NoTruncate);
_ = TCC.tcc_add_symbol(
state,
"INT64_TO_JSVALUE",
workaround.INT64_TO_JSVALUE,
);
_ = TCC.tcc_add_symbol(
state,
"UINT64_TO_JSVALUE",
workaround.UINT64_TO_JSVALUE,
);
if (this.step == .failed) {
return;
}
@@ -1131,8 +1171,12 @@ pub const FFI = struct {
.char, .int8_t, .uint8_t, .int16_t, .uint16_t, .int32_t, .uint32_t => {
try writer.print("JSVALUE_TO_INT32({s})", .{self.symbol});
},
.int64_t => {},
.uint64_t => {},
.int64_t => {
try writer.print("JSVALUE_TO_INT64({s})", .{self.symbol});
},
.uint64_t => {
try writer.print("JSVALUE_TO_UINT64(globalObject, {s})", .{self.symbol});
},
.cstring, .ptr => {
try writer.print("JSVALUE_TO_PTR({s})", .{self.symbol});
},
@@ -1159,8 +1203,12 @@ pub const FFI = struct {
.char, .int8_t, .uint8_t, .int16_t, .uint16_t, .int32_t, .uint32_t => {
try writer.print("INT32_TO_JSVALUE({s})", .{self.symbol});
},
.int64_t => {},
.uint64_t => {},
.int64_t => {
try writer.print("INT64_TO_JSVALUE(globalObject, {s})", .{self.symbol});
},
.uint64_t => {
try writer.print("UINT64_TO_JSVALUE(globalObject, {s})", .{self.symbol});
},
.cstring, .ptr => {
try writer.print("PTR_TO_JSVALUE({s})", .{self.symbol});
},

View File

@@ -1898,6 +1898,37 @@ int64_t JSC__JSValue__toInt64(JSC__JSValue val)
return _val.asAnyInt();
}
JSC__JSValue JSC__JSValue__fromInt64NoTruncate(JSC__JSGlobalObject* globalObject, int64_t val)
{
return JSC::JSValue::encode(JSC::JSValue(JSC::JSBigInt::createFrom(globalObject, val)));
}
JSC__JSValue JSC__JSValue__fromUInt64NoTruncate(JSC__JSGlobalObject* globalObject, uint64_t val)
{
return JSC::JSValue::encode(JSC::JSValue(JSC::JSBigInt::createFrom(globalObject, val)));
}
uint64_t JSC__JSValue__toUInt64NoTruncate(JSC__JSValue val)
{
JSC::JSValue _val = JSC::JSValue::decode(val);
int64_t result = JSC::tryConvertToInt52(_val.asDouble());
if (result != JSC::JSValue::notInt52) {
if (result < 0)
return 0;
return static_cast<uint64_t>(result);
}
if (_val.isHeapBigInt()) {
if (auto* heapBigInt = _val.asHeapBigInt()) {
return heapBigInt->toBigUInt64(heapBigInt);
}
}
return static_cast<uint64_t>(_val.asAnyInt());
}
JSC__JSValue JSC__JSValue__createObject2(JSC__JSGlobalObject* globalObject, const ZigString* arg1,
const ZigString* arg2, JSC__JSValue JSValue3,
JSC__JSValue JSValue4)

View File

@@ -2567,6 +2567,18 @@ pub const JSValue = enum(u64) {
return null;
}
pub fn fromInt64NoTruncate(globalObject: *JSGlobalObject, i: i64) JSValue {
return cppFn("fromInt64NoTruncate", .{ globalObject, i });
}
pub fn fromUInt64NoTruncate(globalObject: *JSGlobalObject, i: u64) JSValue {
return cppFn("fromUInt64NoTruncate", .{ globalObject, i });
}
pub fn toUInt64NoTruncate(this: JSValue) u64 {
return cppFn("toUInt64NoTruncate", .{
this,
});
}
pub inline fn getZigString(this: JSValue, global: *JSGlobalObject) ZigString {
var str = ZigString.init("");
this.toZigString(&str, global);
@@ -2828,7 +2840,7 @@ pub const JSValue = enum(u64) {
return @intToPtr(*anyopaque, @enumToInt(this));
}
pub const Extern = [_][]const u8{ "asPromise", "toInt64", "_then", "put", "makeWithNameAndPrototype", "parseJSON", "symbolKeyFor", "symbolFor", "getSymbolDescription", "createInternalPromise", "asInternalPromise", "asArrayBuffer_", "getReadableStreamState", "getWritableStreamState", "fromEntries", "createTypeError", "createRangeError", "createObject2", "getIfPropertyExistsImpl", "jsType", "jsonStringify", "kind_", "isTerminationException", "isSameValue", "getLengthOfArray", "toZigString", "createStringArray", "createEmptyObject", "putRecord", "asPromise", "isClass", "getNameProperty", "getClassName", "getErrorsProperty", "toInt32", "toBoolean", "isInt32", "isIterable", "forEach", "isAggregateError", "toZigException", "isException", "toWTFString", "hasProperty", "getPropertyNames", "getDirect", "putDirect", "getIfExists", "asString", "asObject", "asNumber", "isError", "jsNull", "jsUndefined", "jsTDZValue", "jsBoolean", "jsDoubleNumber", "jsNumberFromDouble", "jsNumberFromChar", "jsNumberFromU16", "jsNumberFromInt32", "jsNumberFromInt64", "jsNumberFromUint64", "isBoolean", "isAnyInt", "isUInt32AsAnyInt", "isInt32AsAnyInt", "isNumber", "isString", "isBigInt", "isHeapBigInt", "isBigInt32", "isSymbol", "isPrimitive", "isGetterSetter", "isCustomGetterSetter", "isObject", "isCell", "asCell", "toString", "toStringOrNull", "toPropertyKey", "toPropertyKeyValue", "toObject", "toString", "getPrototype", "getPropertyByPropertyName", "eqlValue", "eqlCell", "isCallable" };
pub const Extern = [_][]const u8{ "fromInt64NoTruncate", "fromUInt64NoTruncate", "toUInt64NoTruncate", "asPromise", "toInt64", "_then", "put", "makeWithNameAndPrototype", "parseJSON", "symbolKeyFor", "symbolFor", "getSymbolDescription", "createInternalPromise", "asInternalPromise", "asArrayBuffer_", "getReadableStreamState", "getWritableStreamState", "fromEntries", "createTypeError", "createRangeError", "createObject2", "getIfPropertyExistsImpl", "jsType", "jsonStringify", "kind_", "isTerminationException", "isSameValue", "getLengthOfArray", "toZigString", "createStringArray", "createEmptyObject", "putRecord", "asPromise", "isClass", "getNameProperty", "getClassName", "getErrorsProperty", "toInt32", "toBoolean", "isInt32", "isIterable", "forEach", "isAggregateError", "toZigException", "isException", "toWTFString", "hasProperty", "getPropertyNames", "getDirect", "putDirect", "getIfExists", "asString", "asObject", "asNumber", "isError", "jsNull", "jsUndefined", "jsTDZValue", "jsBoolean", "jsDoubleNumber", "jsNumberFromDouble", "jsNumberFromChar", "jsNumberFromU16", "jsNumberFromInt32", "jsNumberFromInt64", "jsNumberFromUint64", "isBoolean", "isAnyInt", "isUInt32AsAnyInt", "isInt32AsAnyInt", "isNumber", "isString", "isBigInt", "isHeapBigInt", "isBigInt32", "isSymbol", "isPrimitive", "isGetterSetter", "isCustomGetterSetter", "isObject", "isCell", "asCell", "toString", "toStringOrNull", "toPropertyKey", "toPropertyKeyValue", "toObject", "toString", "getPrototype", "getPropertyByPropertyName", "eqlValue", "eqlCell", "isCallable" };
};
extern "c" fn Microtask__run(*Microtask, *JSGlobalObject) void;

View File

@@ -1,4 +1,4 @@
//-- AUTOGENERATED FILE -- 1651480881
//-- AUTOGENERATED FILE -- 1651495723
// clang-format off
#pragma once
@@ -218,7 +218,7 @@ extern "C" const size_t WTF__StringView_object_align_ = alignof(WTF::StringView)
#ifndef INCLUDED__ZigGlobalObject_h_
#define INCLUDED__ZigGlobalObject_h_
#include "ZigGlobalObject.h"
#include ""ZigGlobalObject.h""
#endif
extern "C" const size_t Zig__GlobalObject_object_size_ = sizeof(Zig::GlobalObject);
@@ -242,9 +242,7 @@ extern "C" const size_t Bun__Writable_object_align_ = alignof(Bun__Writable);
#ifndef INCLUDED_Path_h
#define INCLUDED_Path_h
// #include "Path.h"
#define Bun__Path void*
#define Bun__Timer void*
#include "Path.h"
#endif
extern "C" const size_t Bun__Path_object_size_ = sizeof(Bun__Path);
@@ -252,7 +250,7 @@ extern "C" const size_t Bun__Path_object_align_ = alignof(Bun__Path);
#ifndef INCLUDED__ZigConsoleClient_h_
#define INCLUDED__ZigConsoleClient_h_
#include "ZigConsoleClient.h"
#include ""ZigConsoleClient.h""
#endif
extern "C" const size_t Zig__ConsoleClient_object_size_ = sizeof(Zig::ConsoleClient);
@@ -260,7 +258,7 @@ extern "C" const size_t Zig__ConsoleClient_object_align_ = alignof(Zig::ConsoleC
#ifndef INCLUDED_
#define INCLUDED_
// #include ""
#include ""
#endif
extern "C" const size_t Bun__Timer_object_size_ = sizeof(Bun__Timer);

View File

@@ -1,5 +1,5 @@
// clang-format: off
//-- AUTOGENERATED FILE -- 1651480881
//-- AUTOGENERATED FILE -- 1651495723
#pragma once
#include <stddef.h>
@@ -55,7 +55,7 @@ typedef struct bJSC__PropertyName {
} bJSC__PropertyName;
typedef char* bJSC__PropertyName_buf;
typedef struct bJSC__JSGlobalObject {
unsigned char bytes[2400];
unsigned char bytes[2312];
} bJSC__JSGlobalObject;
typedef char* bJSC__JSGlobalObject_buf;
typedef struct bJSC__JSCell {
@@ -87,7 +87,7 @@ typedef struct bJSC__Exception {
} bJSC__Exception;
typedef char* bJSC__Exception_buf;
typedef struct bJSC__VM {
unsigned char bytes[48824];
unsigned char bytes[52168];
} bJSC__VM;
typedef char* bJSC__VM_buf;
typedef struct bJSC__JSString {
@@ -99,7 +99,7 @@ typedef struct bJSC__SourceOrigin {
} bJSC__SourceOrigin;
typedef char* bJSC__SourceOrigin_buf;
typedef struct bWTF__ExternalStringImpl {
unsigned char bytes[32];
unsigned char bytes[40];
} bWTF__ExternalStringImpl;
typedef char* bWTF__ExternalStringImpl_buf;
typedef struct bJSC__JSInternalPromise {
@@ -528,6 +528,8 @@ CPP_DECL bool JSC__JSValue__eqlCell(JSC__JSValue JSValue0, JSC__JSCell* arg1);
CPP_DECL bool JSC__JSValue__eqlValue(JSC__JSValue JSValue0, JSC__JSValue JSValue1);
CPP_DECL void JSC__JSValue__forEach(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, void* arg2, void (*ArgFn3)(JSC__VM* arg0, JSC__JSGlobalObject* arg1, void* arg2, JSC__JSValue JSValue3));
CPP_DECL JSC__JSValue JSC__JSValue__fromEntries(JSC__JSGlobalObject* arg0, ZigString* arg1, ZigString* arg2, size_t arg3, bool arg4);
CPP_DECL JSC__JSValue JSC__JSValue__fromInt64NoTruncate(JSC__JSGlobalObject* arg0, int64_t arg1);
CPP_DECL JSC__JSValue JSC__JSValue__fromUInt64NoTruncate(JSC__JSGlobalObject* arg0, uint64_t arg1);
CPP_DECL void JSC__JSValue__getClassName(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, ZigString* arg2);
CPP_DECL JSC__JSValue JSC__JSValue__getErrorsProperty(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1);
CPP_DECL JSC__JSValue JSC__JSValue__getIfPropertyExistsImpl(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, const unsigned char* arg2, uint32_t arg3);
@@ -589,6 +591,7 @@ CPP_DECL JSC__JSValue JSC__JSValue__toPropertyKeyValue(JSC__JSValue JSValue0, JS
CPP_DECL JSC__JSString* JSC__JSValue__toString(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1);
CPP_DECL JSC__JSString* JSC__JSValue__toString(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1);
CPP_DECL JSC__JSString* JSC__JSValue__toStringOrNull(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1);
CPP_DECL uint64_t JSC__JSValue__toUInt64NoTruncate(JSC__JSValue JSValue0);
CPP_DECL bWTF__String JSC__JSValue__toWTFString(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1);
CPP_DECL void JSC__JSValue__toZigException(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, ZigException* arg2);
CPP_DECL void JSC__JSValue__toZigString(JSC__JSValue JSValue0, ZigString* arg1, JSC__JSGlobalObject* arg2);

View File

@@ -324,6 +324,8 @@ pub extern fn JSC__JSValue__eqlCell(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSCel
pub extern fn JSC__JSValue__eqlValue(JSValue0: JSC__JSValue, JSValue1: JSC__JSValue) bool;
pub extern fn JSC__JSValue__forEach(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject, arg2: ?*anyopaque, ArgFn3: ?fn ([*c]JSC__VM, [*c]JSC__JSGlobalObject, ?*anyopaque, JSC__JSValue) callconv(.C) void) void;
pub extern fn JSC__JSValue__fromEntries(arg0: [*c]JSC__JSGlobalObject, arg1: [*c]ZigString, arg2: [*c]ZigString, arg3: usize, arg4: bool) JSC__JSValue;
pub extern fn JSC__JSValue__fromInt64NoTruncate(arg0: [*c]JSC__JSGlobalObject, arg1: i64) JSC__JSValue;
pub extern fn JSC__JSValue__fromUInt64NoTruncate(arg0: [*c]JSC__JSGlobalObject, arg1: u64) JSC__JSValue;
pub extern fn JSC__JSValue__getClassName(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject, arg2: [*c]ZigString) void;
pub extern fn JSC__JSValue__getErrorsProperty(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject) JSC__JSValue;
pub extern fn JSC__JSValue__getIfPropertyExistsImpl(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject, arg2: [*c]const u8, arg3: u32) JSC__JSValue;
@@ -384,6 +386,7 @@ pub extern fn JSC__JSValue__toPropertyKey(JSValue0: JSC__JSValue, arg1: [*c]JSC_
pub extern fn JSC__JSValue__toPropertyKeyValue(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject) JSC__JSValue;
pub extern fn JSC__JSValue__toString(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject) [*c]JSC__JSString;
pub extern fn JSC__JSValue__toStringOrNull(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject) [*c]JSC__JSString;
pub extern fn JSC__JSValue__toUInt64NoTruncate(JSValue0: JSC__JSValue) u64;
pub extern fn JSC__JSValue__toWTFString(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject) bWTF__String;
pub extern fn JSC__JSValue__toZigException(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject, arg2: [*c]ZigException) void;
pub extern fn JSC__JSValue__toZigString(JSValue0: JSC__JSValue, arg1: [*c]ZigString, arg2: [*c]JSC__JSGlobalObject) void;

View File

@@ -84,6 +84,20 @@ ffiWrappers[FFIType.int64_t] = function int64(val) {
return val;
};
ffiWrappers[FFIType.uint64_t] = function int64(val) {
if (typeof val === "bigint") {
if (val < Number.MAX_VALUE && val > 0) {
return Number(val).valueOf();
}
}
if (!val) {
return 0;
}
return val;
};
ffiWrappers[FFIType.uint16_t] = function uint64(val) {
if (typeof val === "bigint") {
if (val < Number.MAX_VALUE) {