diff options
-rw-r--r-- | integration/bunjs-only-snippets/microtask.js | 76 | ||||
-rw-r--r-- | integration/bunjs-only-snippets/microtask.test.js | 80 | ||||
-rw-r--r-- | integration/bunjs-only-snippets/process-nexttick.test.js | 93 | ||||
-rw-r--r-- | shadow.js | 3 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/headers-cpp.h | 2 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/headers.h | 2 | ||||
-rw-r--r-- | src/javascript/jsc/javascript.zig | 42 |
7 files changed, 193 insertions, 105 deletions
diff --git a/integration/bunjs-only-snippets/microtask.js b/integration/bunjs-only-snippets/microtask.js deleted file mode 100644 index c5acfd578..000000000 --- a/integration/bunjs-only-snippets/microtask.js +++ /dev/null @@ -1,76 +0,0 @@ -// You can verify this test is correct by copy pasting this into a browser's console and checking it doesn't throw an error. -var run = 0; - -await new Promise((resolve, reject) => { - queueMicrotask(() => { - if (run++ != 0) { - reject(new Error("Microtask execution order is wrong: " + run)); - } - queueMicrotask(() => { - if (run++ != 3) { - reject(new Error("Microtask execution order is wrong: " + run)); - } - }); - }); - queueMicrotask(() => { - if (run++ != 1) { - reject(new Error("Microtask execution order is wrong: " + run)); - } - queueMicrotask(() => { - if (run++ != 4) { - reject(new Error("Microtask execution order is wrong: " + run)); - } - - queueMicrotask(() => { - if (run++ != 6) { - reject(new Error("Microtask execution order is wrong: " + run)); - } - }); - }); - }); - queueMicrotask(() => { - if (run++ != 2) { - reject(new Error("Microtask execution order is wrong: " + run)); - } - queueMicrotask(() => { - if (run++ != 5) { - reject(new Error("Microtask execution order is wrong: " + run)); - } - - queueMicrotask(() => { - if (run++ != 7) { - reject(new Error("Microtask execution order is wrong: " + run)); - } - resolve(true); - }); - }); - }); -}); - -{ - var passed = false; - try { - queueMicrotask(1234); - } catch (exception) { - passed = exception instanceof TypeError; - } - - if (!passed) - throw new Error( - "queueMicrotask should throw a TypeError if the argument is not a function" - ); -} - -{ - var passed = false; - try { - queueMicrotask(); - } catch (exception) { - passed = exception instanceof TypeError; - } - - if (!passed) - throw new Error( - "queueMicrotask should throw a TypeError if the argument is empty" - ); -} diff --git a/integration/bunjs-only-snippets/microtask.test.js b/integration/bunjs-only-snippets/microtask.test.js new file mode 100644 index 000000000..18956b1e5 --- /dev/null +++ b/integration/bunjs-only-snippets/microtask.test.js @@ -0,0 +1,80 @@ +import { it } from "bun:test"; + +it("queueMicrotask", async () => { + // You can verify this test is correct by copy pasting this into a browser's console and checking it doesn't throw an error. + var run = 0; + + await new Promise((resolve, reject) => { + queueMicrotask(() => { + if (run++ != 0) { + reject(new Error("Microtask execution order is wrong: " + run)); + } + queueMicrotask(() => { + if (run++ != 3) { + reject(new Error("Microtask execution order is wrong: " + run)); + } + }); + }); + queueMicrotask(() => { + if (run++ != 1) { + reject(new Error("Microtask execution order is wrong: " + run)); + } + queueMicrotask(() => { + if (run++ != 4) { + reject(new Error("Microtask execution order is wrong: " + run)); + } + + queueMicrotask(() => { + if (run++ != 6) { + reject(new Error("Microtask execution order is wrong: " + run)); + } + }); + }); + }); + queueMicrotask(() => { + if (run++ != 2) { + reject(new Error("Microtask execution order is wrong: " + run)); + } + queueMicrotask(() => { + if (run++ != 5) { + reject(new Error("Microtask execution order is wrong: " + run)); + } + + queueMicrotask(() => { + if (run++ != 7) { + reject(new Error("Microtask execution order is wrong: " + run)); + } + resolve(true); + }); + }); + }); + }); + + { + var passed = false; + try { + queueMicrotask(1234); + } catch (exception) { + passed = exception instanceof TypeError; + } + + if (!passed) + throw new Error( + "queueMicrotask should throw a TypeError if the argument is not a function" + ); + } + + { + var passed = false; + try { + queueMicrotask(); + } catch (exception) { + passed = exception instanceof TypeError; + } + + if (!passed) + throw new Error( + "queueMicrotask should throw a TypeError if the argument is empty" + ); + } +}); diff --git a/integration/bunjs-only-snippets/process-nexttick.test.js b/integration/bunjs-only-snippets/process-nexttick.test.js new file mode 100644 index 000000000..ac53399c0 --- /dev/null +++ b/integration/bunjs-only-snippets/process-nexttick.test.js @@ -0,0 +1,93 @@ +import { it } from "bun:test"; + +it("process.nextTick", async () => { + // You can verify this test is correct by copy pasting this into a browser's console and checking it doesn't throw an error. + var run = 0; + var queueMicrotask = process.nextTick; + + await new Promise((resolve, reject) => { + queueMicrotask(() => { + if (run++ != 0) { + reject(new Error("Microtask execution order is wrong: " + run)); + } + queueMicrotask(() => { + if (run++ != 3) { + reject(new Error("Microtask execution order is wrong: " + run)); + } + }); + }); + queueMicrotask(() => { + if (run++ != 1) { + reject(new Error("Microtask execution order is wrong: " + run)); + } + queueMicrotask(() => { + if (run++ != 4) { + reject(new Error("Microtask execution order is wrong: " + run)); + } + + queueMicrotask(() => { + if (run++ != 6) { + reject(new Error("Microtask execution order is wrong: " + run)); + } + }); + }); + }); + queueMicrotask(() => { + if (run++ != 2) { + reject(new Error("Microtask execution order is wrong: " + run)); + } + queueMicrotask(() => { + if (run++ != 5) { + reject(new Error("Microtask execution order is wrong: " + run)); + } + + queueMicrotask(() => { + if (run++ != 7) { + reject(new Error("Microtask execution order is wrong: " + run)); + } + resolve(true); + }); + }); + }); + }); + + { + var passed = false; + try { + queueMicrotask(1234); + } catch (exception) { + passed = exception instanceof TypeError; + } + + if (!passed) + throw new Error( + "queueMicrotask should throw a TypeError if the argument is not a function" + ); + } + + { + var passed = false; + try { + queueMicrotask(); + } catch (exception) { + passed = exception instanceof TypeError; + } + + if (!passed) + throw new Error( + "queueMicrotask should throw a TypeError if the argument is empty" + ); + } + + await new Promise((resolve, reject) => { + process.nextTick( + (first, second) => { + if (first !== 12345 || second !== "hello") + reject(new Error("process.nextTick called with wrong arguments")); + resolve(true); + }, + 12345, + "hello" + ); + }); +}); diff --git a/shadow.js b/shadow.js new file mode 100644 index 000000000..e4272b15a --- /dev/null +++ b/shadow.js @@ -0,0 +1,3 @@ +var realm = new ShadowRealm(); + +console.log(realm.evaluate("import('hi').then(a => a);")); diff --git a/src/javascript/jsc/bindings/headers-cpp.h b/src/javascript/jsc/bindings/headers-cpp.h index ee99ba75c..c40eb5c36 100644 --- a/src/javascript/jsc/bindings/headers-cpp.h +++ b/src/javascript/jsc/bindings/headers-cpp.h @@ -1,4 +1,4 @@ -//-- AUTOGENERATED FILE -- 1647318604 +//-- AUTOGENERATED FILE -- 1647432636 // clang-format off #pragma once diff --git a/src/javascript/jsc/bindings/headers.h b/src/javascript/jsc/bindings/headers.h index fcdf4ad5e..d4560f9ac 100644 --- a/src/javascript/jsc/bindings/headers.h +++ b/src/javascript/jsc/bindings/headers.h @@ -1,5 +1,5 @@ // clang-format: off -//-- AUTOGENERATED FILE -- 1647318604 +//-- AUTOGENERATED FILE -- 1647432636 #pragma once #include <stddef.h> diff --git a/src/javascript/jsc/javascript.zig b/src/javascript/jsc/javascript.zig index 9e9a8c9f8..1c6074180 100644 --- a/src/javascript/jsc/javascript.zig +++ b/src/javascript/jsc/javascript.zig @@ -1953,6 +1953,8 @@ pub const VirtualMachine = struct { _ = this.ready_tasks_count.fetchSub(add, .Monotonic); } } + + // TODO: fix this technical debt pub fn tick(this: *EventLoop) void { while (true) { this.tickConcurrent(); @@ -1965,6 +1967,7 @@ pub const VirtualMachine = struct { } } + // TODO: fix this technical debt pub fn waitForPromise(this: *EventLoop, promise: *JSC.JSInternalPromise) void { switch (promise.status(this.global.vm())) { JSC.JSPromise.Status.Pending => { @@ -3304,36 +3307,21 @@ pub const VirtualMachine = struct { writer.writeByteNTimes(' ', pad) catch unreachable; const top = exception.stack.frames()[0]; var remainder = std.mem.trim(u8, source.text, "\n"); - if (@intCast(usize, top.position.column_stop) > remainder.len or (top.position.column_stop - top.position.column_start) <= 0) { - writer.print( - comptime Output.prettyFmt( - "<r><d>{d} |<r> {s}\n", - allow_ansi_color, - ), - .{ source.line, remainder }, - ) catch unreachable; - } else { - const prefix = remainder[0..@intCast(usize, top.position.column_start)]; - const underline = remainder[@intCast(usize, top.position.column_start)..@intCast(usize, top.position.column_stop)]; - const suffix = remainder[@intCast(usize, top.position.column_stop)..]; - writer.print( - comptime Output.prettyFmt( - "<r><d>{d} |<r> {s}<red>{s}<r>{s}<r>\n<r>", - allow_ansi_color, - ), - .{ - source.line, - prefix, - underline, - suffix, - }, - ) catch unreachable; + writer.print( + comptime Output.prettyFmt( + "<r><d>{d} |<r> {s}\n", + allow_ansi_color, + ), + .{ source.line, remainder }, + ) catch unreachable; + + if (!top.position.isInvalid()) { var first_non_whitespace = @intCast(u32, top.position.column_start); while (first_non_whitespace < source.text.len and source.text[first_non_whitespace] == ' ') { first_non_whitespace += 1; } - const indent = @intCast(usize, pad) + " | ".len + first_non_whitespace + 1; + const indent = @intCast(usize, pad) + " | ".len + first_non_whitespace; writer.writeByteNTimes(' ', indent) catch unreachable; writer.print(comptime Output.prettyFmt( @@ -3514,10 +3502,10 @@ pub const EventListenerMixin = struct { if (promise.status(vm.global.vm()) == .Rejected) { onError(ctx, error.JSError, promise.result(vm.global.vm()), request_context) catch {}; return; - } else { - _ = promise.result(vm.global.vm()); } + _ = promise.result(vm.global.vm()); + vm.waitForTasks(); if (request_context.has_called_done) { |