aboutsummaryrefslogtreecommitdiff
path: root/src/javascript
diff options
context:
space:
mode:
Diffstat (limited to 'src/javascript')
-rw-r--r--src/javascript/jsc/api/bun.zig259
-rw-r--r--src/javascript/jsc/api/ffi.zig23
-rw-r--r--src/javascript/jsc/ffi.exports.js60
3 files changed, 205 insertions, 137 deletions
diff --git a/src/javascript/jsc/api/bun.zig b/src/javascript/jsc/api/bun.zig
index 632d85500..afb3c9a60 100644
--- a/src/javascript/jsc/api/bun.zig
+++ b/src/javascript/jsc/api/bun.zig
@@ -981,121 +981,150 @@ pub const Class = NewClass(
},
},
},
- .{ .match = .{
- .rfn = Router.match,
- .ts = Router.match_type_definition,
- }, .sleepSync = .{
- .rfn = sleepSync,
- }, .fetch = .{
- .rfn = Fetch.call,
- .ts = d.ts{},
- }, .getImportedStyles = .{
- .rfn = Bun.getImportedStyles,
- .ts = d.ts{
- .name = "getImportedStyles",
- .@"return" = "string[]",
- },
- }, .inspect = .{
- .rfn = Bun.inspect,
- .ts = d.ts{
- .name = "inspect",
- .@"return" = "string",
- },
- }, .getRouteFiles = .{
- .rfn = Bun.getRouteFiles,
- .ts = d.ts{
- .name = "getRouteFiles",
- .@"return" = "string[]",
- },
- }, ._Path = .{
- .rfn = Bun.newPath,
- .ts = d.ts{},
- }, .getRouteNames = .{
- .rfn = Bun.getRouteNames,
- .ts = d.ts{
- .name = "getRouteNames",
- .@"return" = "string[]",
- },
- }, .readFile = .{
- .rfn = Bun.readFileAsString,
- .ts = d.ts{
- .name = "readFile",
- .@"return" = "string",
- },
- }, .resolveSync = .{
- .rfn = Bun.resolveSync,
- .ts = d.ts{
- .name = "resolveSync",
- .@"return" = "string",
- },
- }, .resolve = .{
- .rfn = Bun.resolve,
- .ts = d.ts{
- .name = "resolve",
- .@"return" = "string",
- },
- }, .readFileBytes = .{
- .rfn = Bun.readFileAsBytes,
- .ts = d.ts{
- .name = "readFile",
- .@"return" = "Uint8Array",
- },
- }, .getPublicPath = .{
- .rfn = Bun.getPublicPathJS,
- .ts = d.ts{
- .name = "getPublicPath",
- .@"return" = "string",
- },
- }, .registerMacro = .{
- .rfn = Bun.registerMacro,
- .ts = d.ts{
- .name = "registerMacro",
- .@"return" = "undefined",
- },
- .enumerable = false,
- }, .fs = .{
- .rfn = Bun.createNodeFS,
- .ts = d.ts{},
- .enumerable = false,
- }, .jest = .{
- .rfn = @import("../test/jest.zig").Jest.call,
- .ts = d.ts{},
- .enumerable = false,
- }, .gc = .{
- .rfn = Bun.runGC,
- .ts = d.ts{},
- }, .allocUnsafe = .{
- .rfn = Bun.allocUnsafe,
- .ts = .{},
- }, .mmap = .{
- .rfn = Bun.mmapFile,
- .ts = .{},
- }, .generateHeapSnapshot = .{
- .rfn = Bun.generateHeapSnapshot,
- .ts = d.ts{},
- }, .shrink = .{
- .rfn = Bun.shrink,
- .ts = d.ts{},
- }, .openInEditor = .{
- .rfn = Bun.openInEditor,
- .ts = d.ts{},
- }, .readAllStdinSync = .{
- .rfn = Bun.readAllStdinSync,
- .ts = d.ts{},
- }, .serve = .{
- .rfn = Bun.serve,
- .ts = d.ts{},
- }, .file = .{
- .rfn = JSC.WebCore.Blob.constructFile,
- .ts = d.ts{},
- }, .write = .{
- .rfn = JSC.WebCore.Blob.writeFile,
- .ts = d.ts{},
- }, .sha = .{
- .rfn = JSC.wrapWithHasContainer(Crypto.SHA512_256, "hash", false, false, true),
- }, .nanoseconds = .{
- .rfn = nanoseconds,
- } },
+ .{
+ .match = .{
+ .rfn = Router.match,
+ .ts = Router.match_type_definition,
+ },
+ .sleepSync = .{
+ .rfn = sleepSync,
+ },
+ .fetch = .{
+ .rfn = Fetch.call,
+ .ts = d.ts{},
+ },
+ .getImportedStyles = .{
+ .rfn = Bun.getImportedStyles,
+ .ts = d.ts{
+ .name = "getImportedStyles",
+ .@"return" = "string[]",
+ },
+ },
+ .inspect = .{
+ .rfn = Bun.inspect,
+ .ts = d.ts{
+ .name = "inspect",
+ .@"return" = "string",
+ },
+ },
+ .getRouteFiles = .{
+ .rfn = Bun.getRouteFiles,
+ .ts = d.ts{
+ .name = "getRouteFiles",
+ .@"return" = "string[]",
+ },
+ },
+ ._Path = .{
+ .rfn = Bun.newPath,
+ .ts = d.ts{},
+ },
+ .getRouteNames = .{
+ .rfn = Bun.getRouteNames,
+ .ts = d.ts{
+ .name = "getRouteNames",
+ .@"return" = "string[]",
+ },
+ },
+ .readFile = .{
+ .rfn = Bun.readFileAsString,
+ .ts = d.ts{
+ .name = "readFile",
+ .@"return" = "string",
+ },
+ },
+ .resolveSync = .{
+ .rfn = Bun.resolveSync,
+ .ts = d.ts{
+ .name = "resolveSync",
+ .@"return" = "string",
+ },
+ },
+ .resolve = .{
+ .rfn = Bun.resolve,
+ .ts = d.ts{
+ .name = "resolve",
+ .@"return" = "string",
+ },
+ },
+ .readFileBytes = .{
+ .rfn = Bun.readFileAsBytes,
+ .ts = d.ts{
+ .name = "readFile",
+ .@"return" = "Uint8Array",
+ },
+ },
+ .getPublicPath = .{
+ .rfn = Bun.getPublicPathJS,
+ .ts = d.ts{
+ .name = "getPublicPath",
+ .@"return" = "string",
+ },
+ },
+ .registerMacro = .{
+ .rfn = Bun.registerMacro,
+ .ts = d.ts{
+ .name = "registerMacro",
+ .@"return" = "undefined",
+ },
+ .enumerable = false,
+ },
+ .fs = .{
+ .rfn = Bun.createNodeFS,
+ .ts = d.ts{},
+ .enumerable = false,
+ },
+ .jest = .{
+ .rfn = @import("../test/jest.zig").Jest.call,
+ .ts = d.ts{},
+ .enumerable = false,
+ },
+ .gc = .{
+ .rfn = Bun.runGC,
+ .ts = d.ts{},
+ },
+ .allocUnsafe = .{
+ .rfn = Bun.allocUnsafe,
+ .ts = .{},
+ },
+ .mmap = .{
+ .rfn = Bun.mmapFile,
+ .ts = .{},
+ },
+ .generateHeapSnapshot = .{
+ .rfn = Bun.generateHeapSnapshot,
+ .ts = d.ts{},
+ },
+ .shrink = .{
+ .rfn = Bun.shrink,
+ .ts = d.ts{},
+ },
+ .openInEditor = .{
+ .rfn = Bun.openInEditor,
+ .ts = d.ts{},
+ },
+ .readAllStdinSync = .{
+ .rfn = Bun.readAllStdinSync,
+ .ts = d.ts{},
+ },
+ .serve = .{
+ .rfn = Bun.serve,
+ .ts = d.ts{},
+ },
+ .file = .{
+ .rfn = JSC.WebCore.Blob.constructFile,
+ .ts = d.ts{},
+ },
+ .write = .{
+ .rfn = JSC.WebCore.Blob.writeFile,
+ .ts = d.ts{},
+ },
+ .sha = .{
+ .rfn = JSC.wrapWithHasContainer(Crypto.SHA512_256, "hash", false, false, true),
+ },
+ .nanoseconds = .{
+ .rfn = nanoseconds,
+ },
+ },
.{
.main = .{
.get = getMain,
diff --git a/src/javascript/jsc/api/ffi.zig b/src/javascript/jsc/api/ffi.zig
index c5ad6bf59..2e9173ee4 100644
--- a/src/javascript/jsc/api/ffi.zig
+++ b/src/javascript/jsc/api/ffi.zig
@@ -1118,6 +1118,9 @@ pub const FFI = struct {
cstring = 14,
+ i64_fast = 15,
+ u64_fast = 16,
+
/// Types that we can directly pass through as an `int64_t`
pub fn needsACastInC(this: ABIType) bool {
return switch (this) {
@@ -1160,6 +1163,8 @@ pub const FFI = struct {
.{ "pointer", ABIType.ptr },
.{ "void", ABIType.@"void" },
.{ "cstring", ABIType.@"cstring" },
+ .{ "i64_fast", ABIType.i64_fast },
+ .{ "u64_fast", ABIType.u64_fast },
};
pub const label = ComptimeStringMap(ABIType, map);
const EnumMapFormatter = struct {
@@ -1221,10 +1226,10 @@ 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 => {
+ .i64_fast, .int64_t => {
try writer.print("JSVALUE_TO_INT64({s})", .{self.symbol});
},
- .uint64_t => {
+ .u64_fast, .uint64_t => {
try writer.print("JSVALUE_TO_UINT64(globalObject, {s})", .{self.symbol});
},
.cstring, .ptr => {
@@ -1253,12 +1258,18 @@ 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 => {
+ .i64_fast => {
try writer.print("INT64_TO_JSVALUE(globalObject, {s})", .{self.symbol});
},
- .uint64_t => {
+ .int64_t => {
+ try writer.print("INT64_TO_JSVALUE_SLOW(globalObject, {s})", .{self.symbol});
+ },
+ .u64_fast => {
try writer.print("UINT64_TO_JSVALUE(globalObject, {s})", .{self.symbol});
},
+ .uint64_t => {
+ try writer.print("UINT64_TO_JSVALUE_SLOW(globalObject, {s})", .{self.symbol});
+ },
.cstring, .ptr => {
try writer.print("PTR_TO_JSVALUE({s})", .{self.symbol});
},
@@ -1300,8 +1311,8 @@ pub const FFI = struct {
.uint16_t => "uint16_t",
.int32_t => "int32_t",
.uint32_t => "uint32_t",
- .int64_t => "int64_t",
- .uint64_t => "uint64_t",
+ .i64_fast, .int64_t => "int64_t",
+ .u64_fast, .uint64_t => "uint64_t",
.double => "double",
.float => "float",
.char => "char",
diff --git a/src/javascript/jsc/ffi.exports.js b/src/javascript/jsc/ffi.exports.js
index fea92e0cd..ae11326e5 100644
--- a/src/javascript/jsc/ffi.exports.js
+++ b/src/javascript/jsc/ffi.exports.js
@@ -52,7 +52,7 @@ Object.defineProperty(globalThis, "__GlobalBunCString", {
configurable: false,
});
-const ffiWrappers = new Array(15);
+const ffiWrappers = new Array(16);
var char = (val) => val | 0;
ffiWrappers.fill(char);
ffiWrappers[FFIType.uint8_t] = function uint8(val) {
@@ -67,13 +67,14 @@ ffiWrappers[FFIType.uint16_t] = function uint16(val) {
ffiWrappers[FFIType.int32_t] = function int32(val) {
return val | 0;
};
+// we never want to return NaN
ffiWrappers[FFIType.uint32_t] = function uint32(val) {
- return val <= 0 ? 0 : val >= 0xffffffff ? 0xffffffff : val;
+ return val <= 0 ? 0 : val >= 0xffffffff ? 0xffffffff : +val || 0;
};
-ffiWrappers[FFIType.int64_t] = function int64(val) {
+ffiWrappers[FFIType.i64_fast] = function int64(val) {
if (typeof val === "bigint") {
if (val < BigInt(Number.MAX_VALUE)) {
- return Number(val).valueOf();
+ return Number(val).valueOf() || 0;
}
}
@@ -81,13 +82,13 @@ ffiWrappers[FFIType.int64_t] = function int64(val) {
return 0;
}
- return val;
+ return +val || 0;
};
-ffiWrappers[FFIType.uint64_t] = function int64(val) {
+ffiWrappers[FFIType.u64_fast] = function u64_fast(val) {
if (typeof val === "bigint") {
if (val < BigInt(Number.MAX_VALUE) && val > 0) {
- return Number(val).valueOf();
+ return Number(val).valueOf() || 0;
}
}
@@ -95,21 +96,48 @@ ffiWrappers[FFIType.uint64_t] = function int64(val) {
return 0;
}
- return val;
+ return +val || 0;
};
-ffiWrappers[FFIType.uint16_t] = function uint64(val) {
+ffiWrappers[FFIType.int64_t] = function int64(val) {
if (typeof val === "bigint") {
- if (val < BigInt(Number.MAX_VALUE)) {
- return Math.abs(Number(val).valueOf());
- }
+ return val;
}
- if (!val) {
- return 0;
+ if (typeof val === "number") {
+ return BigInt(val);
+ }
+
+ return BigInt(+val || 0);
+};
+
+ffiWrappers[FFIType.uint64_t] = function uint64(val) {
+ if (typeof val === "bigint") {
+ return val;
}
- return Math.abs(val);
+ if (typeof val === "number") {
+ return val <= 0 ? BigInt(0) : BigInt(val);
+ }
+
+ return BigInt(+val || 0);
+};
+
+ffiWrappers[FFIType.u64_fast] = function u64_fast(val) {
+ if (typeof val === "bigint") {
+ return val < BigInt(Number.MAX_VALUE)
+ ? val <= BigInt(0)
+ ? 0
+ : Number(val)
+ : val;
+ }
+
+ return typeof val === "number" ? (val <= 0 ? 0 : +val || 0) : +val || 0;
+};
+
+ffiWrappers[FFIType.uint16_t] = function uint16(val) {
+ const ret = (typeof val === "bigint" ? Number(val) : val) | 0;
+ return ret <= 0 ? 0 : ret > 0xffff ? 0xffff : ret;
};
ffiWrappers[FFIType.double] = function double(val) {
@@ -231,7 +259,7 @@ export function dlopen(path, options) {
);
} else {
// consistentcy
- result.native = result;
+ result.symbols[key].native = result.symbols[key];
}
}