aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/webcore.zig
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-07-11 19:14:34 -0700
committerGravatar GitHub <noreply@github.com> 2023-07-11 19:14:34 -0700
commitcbb88672f217a90db1aa1eb29cd92d5d9035b22b (patch)
tree43a00501f3cde495967e116f0b660777051551f8 /src/bun.js/webcore.zig
parent1f900cff453700b19bca2acadfe26da4468c1282 (diff)
parent34b0e7a2bbd8bf8097341cdb0075d0908283e834 (diff)
downloadbun-jarred/esm-conditions.tar.gz
bun-jarred/esm-conditions.tar.zst
bun-jarred/esm-conditions.zip
Merge branch 'main' into jarred/esm-conditionsjarred/esm-conditions
Diffstat (limited to '')
-rw-r--r--src/bun.js/webcore.zig33
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);
}