diff options
Diffstat (limited to 'src/bun.js/webcore.zig')
-rw-r--r-- | src/bun.js/webcore.zig | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/src/bun.js/webcore.zig b/src/bun.js/webcore.zig index e7357e4ca..8cb9ec80a 100644 --- a/src/bun.js/webcore.zig +++ b/src/bun.js/webcore.zig @@ -10,6 +10,7 @@ const std = @import("std"); const bun = @import("root").bun; const string = bun.string; pub const AbortSignal = @import("./bindings/bindings.zig").AbortSignal; +pub const JSValue = @import("./bindings/bindings.zig").JSValue; pub const Lifetime = enum { clone, @@ -365,7 +366,6 @@ pub const Prompt = struct { }; pub const Crypto = struct { - const UUID = @import("./uuid.zig"); const BoringSSL = @import("root").bun.BoringSSL; pub const Class = JSC.NewClass( void, @@ -374,6 +374,7 @@ pub const Crypto = struct { .getRandomValues = JSC.DOMCall("Crypto", @This(), "getRandomValues", JSC.JSValue, JSC.DOMEffect.top), .randomUUID = JSC.DOMCall("Crypto", @This(), "randomUUID", *JSC.JSString, JSC.DOMEffect.top), .timingSafeEqual = JSC.DOMCall("Crypto", @This(), "timingSafeEqual", JSC.JSValue, JSC.DOMEffect.top), + .randomInt = .{ .rfn = &JSC.wrapWithHasContainer(Crypto, "randomInt", false, false, false) }, .scryptSync = .{ .rfn = &JSC.wrapWithHasContainer(Crypto, "scryptSync", false, false, false) }, }, .{}, @@ -663,7 +664,7 @@ pub const Crypto = struct { ) callconv(.C) JSC.JSValue { var slice = array.slice(); randomData(globalThis, slice.ptr, slice.len); - return @intToEnum(JSC.JSValue, @bitCast(i64, @ptrToInt(array))); + return @enumFromInt(JSC.JSValue, @bitCast(i64, @intFromPtr(array))); } fn randomData( @@ -691,21 +692,37 @@ pub const Crypto = struct { _: []const JSC.JSValue, ) JSC.JSValue { var out: [36]u8 = undefined; - const uuid: UUID = .{ - .bytes = globalThis.bunVM().rareData().nextUUID(), - }; + const uuid = globalThis.bunVM().rareData().nextUUID(); + uuid.print(&out); return JSC.ZigString.init(&out).toValueGC(globalThis); } + pub fn randomInt(globalThis: *JSC.JSGlobalObject, min_value: ?JSValue, max_value: ?JSValue) JSValue { + _ = globalThis; + + var at_least: u52 = 0; + var at_most: u52 = std.math.maxInt(u52); + + if (min_value) |min| { + if (max_value) |max| { + if (min.isNumber()) at_least = min.to(u52); + if (max.isNumber()) at_most = max.to(u52); + } else { + if (min.isNumber()) at_most = min.to(u52); + } + } + + return JSValue.jsNumberFromUint64(std.crypto.random.intRangeAtMost(u52, at_least, at_most)); + } + pub fn randomUUIDWithoutTypeChecks( globalThis: *JSC.JSGlobalObject, _: *anyopaque, ) callconv(.C) JSC.JSValue { var out: [36]u8 = undefined; - const uuid: UUID = .{ - .bytes = globalThis.bunVM().rareData().nextUUID(), - }; + const uuid = globalThis.bunVM().rareData().nextUUID(); + uuid.print(&out); return JSC.ZigString.init(&out).toValueGC(globalThis); } |