diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/bun.js/api/bun.zig | 10 | ||||
-rw-r--r-- | src/bun.js/api/bun/socket.zig | 2 | ||||
-rw-r--r-- | src/bun.js/api/bun/subprocess.zig | 2 | ||||
-rw-r--r-- | src/bun.js/bindings/FFI.zig | 2 | ||||
-rw-r--r-- | src/bun.js/bindings/bindings.cpp | 6 | ||||
-rw-r--r-- | src/bun.js/bindings/bindings.zig | 33 | ||||
-rw-r--r-- | src/bun.js/bindings/headers-cpp.h | 2 | ||||
-rw-r--r-- | src/bun.js/bindings/headers.h | 3 | ||||
-rw-r--r-- | src/bun.js/bindings/headers.zig | 1 | ||||
-rw-r--r-- | src/bun.js/node/node_fs.zig | 8 | ||||
-rw-r--r-- | src/bun.js/node/node_os.zig | 4 |
11 files changed, 55 insertions, 18 deletions
diff --git a/src/bun.js/api/bun.zig b/src/bun.js/api/bun.zig index e1e84c2d4..52b457e54 100644 --- a/src/bun.js/api/bun.zig +++ b/src/bun.js/api/bun.zig @@ -2435,7 +2435,7 @@ pub const Timer = struct { // We don't deal with nesting levels directly // but we do set the minimum timeout to be 1ms for repeating timers const interval: i32 = @maximum( - countdown.toInt32(), + countdown.coerce(i32, globalThis), if (repeat) @as(i32, 1) else 0, ); @@ -3477,19 +3477,19 @@ pub const JSZlib = struct { if (options_val_) |options_val| { if (options_val.isObject()) { if (options_val.get(globalThis, "windowBits")) |window| { - opts.windowBits = window.toInt32(); + opts.windowBits = window.coerce(i32, globalThis); } if (options_val.get(globalThis, "level")) |level| { - opts.level = level.toInt32(); + opts.level = level.coerce(i32, globalThis); } if (options_val.get(globalThis, "memLevel")) |memLevel| { - opts.memLevel = memLevel.toInt32(); + opts.memLevel = memLevel.coerce(i32, globalThis); } if (options_val.get(globalThis, "strategy")) |strategy| { - opts.strategy = strategy.toInt32(); + opts.strategy = strategy.coerce(i32, globalThis); } } } diff --git a/src/bun.js/api/bun/socket.zig b/src/bun.js/api/bun/socket.zig index 8851ff47b..77b4a266f 100644 --- a/src/bun.js/api/bun/socket.zig +++ b/src/bun.js/api/bun/socket.zig @@ -1111,7 +1111,7 @@ fn NewSocket(comptime ssl: bool) type { globalObject.throw("Expected 1 argument, got 0", .{}); return .zero; } - const t = args.ptr[0].toInt32(); + const t = args.ptr[0].coerce(i32, globalObject); if (t < 0) { globalObject.throw("Timeout must be a positive integer", .{}); return .zero; diff --git a/src/bun.js/api/bun/subprocess.zig b/src/bun.js/api/bun/subprocess.zig index 3cc534e18..4e2a0cc94 100644 --- a/src/bun.js/api/bun/subprocess.zig +++ b/src/bun.js/api/bun/subprocess.zig @@ -238,7 +238,7 @@ pub const Subprocess = struct { var sig: i32 = 1; if (arguments.len > 0) { - sig = arguments.ptr[0].toInt32(); + sig = arguments.ptr[0].coerce(i32, globalThis); } if (!(sig > -1 and sig < std.math.maxInt(u8))) { diff --git a/src/bun.js/bindings/FFI.zig b/src/bun.js/bindings/FFI.zig index 802615007..e9d55c9f3 100644 --- a/src/bun.js/bindings/FFI.zig +++ b/src/bun.js/bindings/FFI.zig @@ -120,7 +120,7 @@ pub inline fn JSVALUE_TO_FLOAT(arg_val: EncodedJSValue) f32 { } pub inline fn JSVALUE_TO_DOUBLE(arg_val: EncodedJSValue) f64 { var val = arg_val; - val.asInt64 -= @as(c_longlong, 1) << @intCast(@import("std").math.Log2Int(c_longlong), 49); + val.asInt64 -= comptime @as(c_longlong, 1) << @intCast(@import("std").math.Log2Int(c_longlong), 49); return val.asDouble; } pub inline fn JSVALUE_TO_BOOL(arg_val: EncodedJSValue) @"bool" { diff --git a/src/bun.js/bindings/bindings.cpp b/src/bun.js/bindings/bindings.cpp index 3500c32e5..65c2c458f 100644 --- a/src/bun.js/bindings/bindings.cpp +++ b/src/bun.js/bindings/bindings.cpp @@ -2087,6 +2087,12 @@ int32_t JSC__JSValue__toInt32(JSC__JSValue JSValue0) return JSC::JSValue::decode(JSValue0).asInt32(); } +int32_t JSC__JSValue__coerceToInt32(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1) +{ + JSC::JSValue value = JSC::JSValue::decode(JSValue0); + return value.toInt32(arg1); +} + JSC__JSValue JSC__JSValue__getErrorsProperty(JSC__JSValue JSValue0, JSC__JSGlobalObject* global) { JSC::JSObject* obj = JSC::JSValue::decode(JSValue0).getObject(); diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig index 9c4135c42..b4bf44895 100644 --- a/src/bun.js/bindings/bindings.zig +++ b/src/bun.js/bindings/bindings.zig @@ -2718,6 +2718,30 @@ pub const JSValue = enum(JSValueReprInt) { } }; + pub fn coerceToInt32(this: JSValue, globalThis: *JSC.JSGlobalObject) i32 { + return cppFn("coerceToInt32", .{ this, globalThis }); + } + + pub fn coerce(this: JSValue, comptime T: type, globalThis: *JSC.JSGlobalObject) T { + return switch (T) { + ZigString => this.getZigString(globalThis), + i32 => { + if (this.isInt32()) { + return this.asInt32(); + } + + if (this.isNumber()) { + return @truncate(i32, @floatToInt(i64, this.asDouble())); + } + + return this.coerceToInt32(globalThis); + }, + else => @compileError("Unsupported coercion type"), + }; + } + + /// This does not call [Symbol.toPrimitive] or [Symbol.toStringTag]. + /// This is only safe when you don't want to do conversions across non-primitive types. pub fn to(this: JSValue, comptime T: type) T { return switch (comptime T) { u32 => toU32(this), @@ -3292,7 +3316,7 @@ pub const JSValue = enum(JSValueReprInt) { pub fn getTruthy(this: JSValue, global: *JSGlobalObject, comptime property: []const u8) ?JSValue { if (get(this, global, property)) |prop| { - if (@enumToInt(prop) == 0 or prop.isUndefinedOrNull()) return null; + if (prop.isEmptyOrUndefinedOrNull()) return null; return prop; } @@ -3408,6 +3432,11 @@ pub const JSValue = enum(JSValueReprInt) { return @truncate(i32, @floatToInt(i64, asDouble(this))); } + if (comptime bun.Environment.allow_assert) { + std.debug.assert(!this.isString()); // use coerce() instead + std.debug.assert(!this.isCell()); // use coerce() instead + } + return cppFn("toInt32", .{ this, }); @@ -3496,7 +3525,7 @@ pub const JSValue = enum(JSValueReprInt) { return this.asNullableVoid().?; } - pub const Extern = [_][]const u8{ "fastGet_", "getStaticProperty", "createUninitializedUint8Array", "fromInt64NoTruncate", "fromUInt64NoTruncate", "toUInt64NoTruncate", "asPromise", "toInt64", "_then", "put", "makeWithNameAndPrototype", "parseJSON", "symbolKeyFor", "symbolFor", "getSymbolDescription", "createInternalPromise", "asInternalPromise", "asArrayBuffer_", "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", "toError", "toZigException", "isException", "toWTFString", "hasProperty", "getPropertyNames", "getDirect", "putDirect", "getIfExists", "asString", "asObject", "asNumber", "isError", "jsNull", "jsUndefined", "jsTDZValue", "jsBoolean", "jsDoubleNumber", "jsNumberFromDouble", "jsNumberFromChar", "jsNumberFromU16", "jsNumberFromInt64", "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{ "coerceToInt32", "fastGet_", "getStaticProperty", "createUninitializedUint8Array", "fromInt64NoTruncate", "fromUInt64NoTruncate", "toUInt64NoTruncate", "asPromise", "toInt64", "_then", "put", "makeWithNameAndPrototype", "parseJSON", "symbolKeyFor", "symbolFor", "getSymbolDescription", "createInternalPromise", "asInternalPromise", "asArrayBuffer_", "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", "toError", "toZigException", "isException", "toWTFString", "hasProperty", "getPropertyNames", "getDirect", "putDirect", "getIfExists", "asString", "asObject", "asNumber", "isError", "jsNull", "jsUndefined", "jsTDZValue", "jsBoolean", "jsDoubleNumber", "jsNumberFromDouble", "jsNumberFromChar", "jsNumberFromU16", "jsNumberFromInt64", "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; diff --git a/src/bun.js/bindings/headers-cpp.h b/src/bun.js/bindings/headers-cpp.h index 559c44d1f..60276f76f 100644 --- a/src/bun.js/bindings/headers-cpp.h +++ b/src/bun.js/bindings/headers-cpp.h @@ -1,4 +1,4 @@ -//-- AUTOGENERATED FILE -- 1666999550 +//-- AUTOGENERATED FILE -- 1667784809 // clang-format off #pragma once diff --git a/src/bun.js/bindings/headers.h b/src/bun.js/bindings/headers.h index 10052533a..8450e7390 100644 --- a/src/bun.js/bindings/headers.h +++ b/src/bun.js/bindings/headers.h @@ -1,5 +1,5 @@ // clang-format off -//-- AUTOGENERATED FILE -- 1666999550 +//-- AUTOGENERATED FILE -- 1667784809 #pragma once #include <stddef.h> @@ -478,6 +478,7 @@ CPP_DECL bJSC__JSObject JSC__JSValue__asObject(JSC__JSValue JSValue0); CPP_DECL JSC__JSPromise* JSC__JSValue__asPromise(JSC__JSValue JSValue0); CPP_DECL JSC__JSPromise* JSC__JSValue__asPromise(JSC__JSValue JSValue0); CPP_DECL JSC__JSString* JSC__JSValue__asString(JSC__JSValue JSValue0); +CPP_DECL int32_t JSC__JSValue__coerceToInt32(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1); CPP_DECL JSC__JSValue JSC__JSValue__createEmptyObject(JSC__JSGlobalObject* arg0, size_t arg1); CPP_DECL JSC__JSValue JSC__JSValue__createInternalPromise(JSC__JSGlobalObject* arg0); CPP_DECL JSC__JSValue JSC__JSValue__createObject2(JSC__JSGlobalObject* arg0, const ZigString* arg1, const ZigString* arg2, JSC__JSValue JSValue3, JSC__JSValue JSValue4); diff --git a/src/bun.js/bindings/headers.zig b/src/bun.js/bindings/headers.zig index 242056ecb..e0909ba8c 100644 --- a/src/bun.js/bindings/headers.zig +++ b/src/bun.js/bindings/headers.zig @@ -271,6 +271,7 @@ pub extern fn JSC__JSValue__asNumber(JSValue0: JSC__JSValue) f64; pub extern fn JSC__JSValue__asObject(JSValue0: JSC__JSValue) bJSC__JSObject; pub extern fn JSC__JSValue__asPromise(JSValue0: JSC__JSValue) [*c]JSC__JSPromise; pub extern fn JSC__JSValue__asString(JSValue0: JSC__JSValue) [*c]JSC__JSString; +pub extern fn JSC__JSValue__coerceToInt32(JSValue0: JSC__JSValue, arg1: ?*JSC__JSGlobalObject) i32; pub extern fn JSC__JSValue__createEmptyObject(arg0: ?*JSC__JSGlobalObject, arg1: usize) JSC__JSValue; pub extern fn JSC__JSValue__createInternalPromise(arg0: ?*JSC__JSGlobalObject) JSC__JSValue; pub extern fn JSC__JSValue__createObject2(arg0: ?*JSC__JSGlobalObject, arg1: [*c]const ZigString, arg2: [*c]const ZigString, JSValue3: JSC__JSValue, JSValue4: JSC__JSValue) JSC__JSValue; diff --git a/src/bun.js/node/node_fs.zig b/src/bun.js/node/node_fs.zig index ef0f2385b..6862b53de 100644 --- a/src/bun.js/node/node_fs.zig +++ b/src/bun.js/node/node_fs.zig @@ -1424,7 +1424,7 @@ const Arguments = struct { } if (current.getIfPropertyExists(ctx.ptr(), "position")) |num| { - const position: i32 = if (num.isEmptyOrUndefinedOrNull()) -1 else num.toInt32(); + const position: i32 = if (num.isEmptyOrUndefinedOrNull()) -1 else num.coerce(i32, ctx); if (position > -1) { args.position = @intCast(ReadPosition, position); } @@ -1905,11 +1905,11 @@ const Arguments = struct { } if (arg.getIfPropertyExists(ctx.ptr(), "start")) |start| { - stream.start = start.toInt32(); + stream.start = start.coerce(i32, ctx); } if (arg.getIfPropertyExists(ctx.ptr(), "end")) |end| { - stream.end = end.toInt32(); + stream.end = end.coerce(i32, ctx); } if (arg.getIfPropertyExists(ctx.ptr(), "highWaterMark")) |highwaterMark| { @@ -2128,7 +2128,7 @@ const Arguments = struct { if (arguments.next()) |arg| { arguments.eat(); if (arg.isNumber()) { - mode = arg.toInt32(); + mode = arg.coerce(i32, ctx); } } diff --git a/src/bun.js/node/node_os.zig b/src/bun.js/node/node_os.zig index 55b2479c7..cf8a72564 100644 --- a/src/bun.js/node/node_os.zig +++ b/src/bun.js/node/node_os.zig @@ -189,8 +189,8 @@ pub const Os = struct { return JSC.JSValue.jsUndefined(); } - const pid = if (arguments.len == 2) arguments[0].toInt32() else 0; - const priority = if (arguments.len == 2) arguments[1].toInt32() else arguments[0].toInt32(); + const pid = if (arguments.len == 2) arguments[0].coerce(i32, globalThis) else 0; + const priority = if (arguments.len == 2) arguments[1].coerce(i32, globalThis) else arguments[0].coerce(i32, globalThis); if (priority < -20 or priority > 19) { const err = JSC.toTypeError( |