diff options
-rw-r--r-- | integration/bunjs-only-snippets/sleep.js | 10 | ||||
-rw-r--r-- | src/javascript/jsc/javascript.zig | 19 |
2 files changed, 29 insertions, 0 deletions
diff --git a/integration/bunjs-only-snippets/sleep.js b/integration/bunjs-only-snippets/sleep.js new file mode 100644 index 000000000..9a62201c5 --- /dev/null +++ b/integration/bunjs-only-snippets/sleep.js @@ -0,0 +1,10 @@ +const interval = 0.5; +const now = performance.now(); +console.time("Slept"); +Bun.sleep(interval); +const elapsed = performance.now() - now; +if (elapsed < interval) { + throw new Error("Didn't sleep"); +} + +console.timeEnd("Slept"); diff --git a/src/javascript/jsc/javascript.zig b/src/javascript/jsc/javascript.zig index 1d353169c..ba2161c9d 100644 --- a/src/javascript/jsc/javascript.zig +++ b/src/javascript/jsc/javascript.zig @@ -455,6 +455,22 @@ pub const Bun = struct { } } + pub fn sleep( + this: void, + ctx: js.JSContextRef, + function: js.JSObjectRef, + thisObject: js.JSObjectRef, + arguments: []const js.JSValueRef, + exception: js.ExceptionRef, + ) js.JSValueRef { + if (js.JSValueIsNumber(ctx, arguments[0])) { + const ms = JSValue.fromRef(arguments[0]).asNumber(); + if (ms > 0 and std.math.isFinite(ms)) std.time.sleep(@floatToInt(u64, @floor(@floatCast(f128, ms) * std.time.ns_per_ms))); + } + + return js.JSValueMakeUndefined(ctx); + } + var public_path_temp_str: [std.fs.MAX_PATH_BYTES]u8 = undefined; pub fn getPublicPathJS( @@ -493,6 +509,9 @@ pub const Bun = struct { .rfn = Router.match, .ts = Router.match_type_definition, }, + .sleep = .{ + .rfn = sleep, + }, .fetch = .{ .rfn = Fetch.call, .ts = d.ts{}, |