aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/bun.js/api/bun.zig28
-rw-r--r--test/bun.js/sleep.js2
-rw-r--r--test/bun.js/sleepSync.test.ts32
3 files changed, 57 insertions, 5 deletions
diff --git a/src/bun.js/api/bun.zig b/src/bun.js/api/bun.zig
index 1c991eece..5dc3abcf9 100644
--- a/src/bun.js/api/bun.zig
+++ b/src/bun.js/api/bun.zig
@@ -861,12 +861,32 @@ pub fn sleepSync(
arguments: []const js.JSValueRef,
_: js.ExceptionRef,
) js.JSValueRef {
- if (js.JSValueIsNumber(ctx, arguments[0])) {
- const seconds = JSValue.fromRef(arguments[0]).asNumber();
- if (seconds > 0 and std.math.isFinite(seconds)) std.time.sleep(@floatToInt(u64, seconds * 1000) * std.time.ns_per_ms);
+ // This function always returns undefined
+ const ret = js.JSValueMakeUndefined(ctx);
+
+ // Expect at least one argument. We allow more than one but ignore them; this
+ // is useful for supporting things like `[1, 2].map(sleepSync)`
+ if (arguments.len < 1) {
+ ctx.throwInvalidArguments("expected one argument, got {}", .{arguments.len});
+ return ret;
}
+ const arg = JSValue.fromRef(arguments[0]);
- return js.JSValueMakeUndefined(ctx);
+ // The argument must be a number
+ if (!arg.isNumber()) {
+ ctx.throwInvalidArguments("argument to sleepSync must be a number, got {}", .{arg.jsTypeLoose()});
+ return ret;
+ }
+
+ //NOTE: if argument is > max(i32) then it will be truncated
+ const milliseconds = arg.coerce(i32, ctx);
+ if (milliseconds < 0) {
+ ctx.throwInvalidArguments("argument to sleepSync must not be negative, got {}", .{milliseconds});
+ return ret;
+ }
+
+ std.time.sleep(@intCast(u64, milliseconds) * std.time.ns_per_ms);
+ return ret;
}
pub fn createNodeFS(
diff --git a/test/bun.js/sleep.js b/test/bun.js/sleep.js
index 080597424..1ec79e79d 100644
--- a/test/bun.js/sleep.js
+++ b/test/bun.js/sleep.js
@@ -1,4 +1,4 @@
-const interval = 0.01;
+const interval = 10;
const now = performance.now();
console.time("Slept");
Bun.sleepSync(interval);
diff --git a/test/bun.js/sleepSync.test.ts b/test/bun.js/sleepSync.test.ts
new file mode 100644
index 000000000..dd2e8818a
--- /dev/null
+++ b/test/bun.js/sleepSync.test.ts
@@ -0,0 +1,32 @@
+import { it, expect } from "bun:test";
+import { sleepSync } from "bun";
+
+it("sleepSync uses milliseconds", async () => {
+ const start = Date.now();
+ sleepSync(5);
+ const end = Date.now();
+ expect(end - start).toBeGreaterThanOrEqual(5);
+ expect(end - start).toBeLessThan(10);
+});
+
+it("sleepSync with no arguments throws", async () => {
+ expect(() => sleepSync()).toThrow();
+});
+
+it("sleepSync with non-numbers throws", async () => {
+ expect(() => sleepSync(true)).toThrow();
+ expect(() => sleepSync(false)).toThrow();
+ expect(() => sleepSync("hi")).toThrow();
+ expect(() => sleepSync({})).toThrow();
+ expect(() => sleepSync([])).toThrow();
+ expect(() => sleepSync(undefined)).toThrow();
+ expect(() => sleepSync(null)).toThrow();
+});
+
+it("sleepSync with negative number throws", async () => {
+ expect(() => sleepSync(-10)).toThrow();
+});
+
+it("can map with sleepSync", async () => {
+ [1, 2, 3].map(sleepSync);
+});