Skip to content

Commit

Permalink
back to the non-promise version & port some webpack tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed Jun 12, 2024
1 parent 761ff8e commit 959442e
Show file tree
Hide file tree
Showing 63 changed files with 486 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ type AsyncModule = (
body: (
handleAsyncDependencies: (
deps: Dep[]
) => Exports[] | Promise<() => Exports[]>
) => Promise<void>,
) => Exports[] | Promise<() => Exports[]>,
asyncResult: (err?: any) => void
) => void,
hasAwait: boolean
) => void;

Expand Down
48 changes: 30 additions & 18 deletions crates/turbopack-ecmascript-runtime/js/src/shared/runtime-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,20 @@ const turbopackQueues = Symbol("turbopack queues");
const turbopackExports = Symbol("turbopack exports");
const turbopackError = Symbol("turbopack error");

const enum QueueStatus {
Unknown = -1,
Unresolved = 0,
Resolved = 1,
}

type AsyncQueueFn = (() => void) & { queueCount: number };
type AsyncQueue = AsyncQueueFn[] & { resolved: boolean };
type AsyncQueue = AsyncQueueFn[] & {
status: QueueStatus;
};

function resolveQueue(queue?: AsyncQueue) {
if (queue && !queue.resolved) {
queue.resolved = true;
if (queue && queue.status !== QueueStatus.Resolved) {
queue.status = QueueStatus.Resolved;
queue.forEach((fn) => fn.queueCount--);
queue.forEach((fn) => (fn.queueCount-- ? fn.queueCount++ : fn()));
}
Expand All @@ -359,7 +367,9 @@ function wrapDeps(deps: Dep[]): AsyncModuleExt[] {
if (dep !== null && typeof dep === "object") {
if (isAsyncModuleExt(dep)) return dep;
if (isPromise(dep)) {
const queue: AsyncQueue = Object.assign([], { resolved: false });
const queue: AsyncQueue = Object.assign([], {
status: QueueStatus.Unresolved,
});

const obj: AsyncModuleExt = {
[turbopackExports]: {},
Expand Down Expand Up @@ -393,12 +403,13 @@ function asyncModule(
body: (
handleAsyncDependencies: (
deps: Dep[]
) => Exports[] | Promise<() => Exports[]>
) => Promise<void>,
) => Exports[] | Promise<() => Exports[]>,
asyncResult: (err?: any) => void
) => void,
hasAwait: boolean
) {
const queue: AsyncQueue | undefined = hasAwait
? Object.assign([], { resolved: true })
? Object.assign([], { status: QueueStatus.Unknown })
: undefined;

const depQueues: Set<AsyncQueue> = new Set();
Expand Down Expand Up @@ -447,7 +458,7 @@ function asyncModule(
function fnQueue(q: AsyncQueue) {
if (q !== queue && !depQueues.has(q)) {
depQueues.add(q);
if (q && !q.resolved) {
if (q && q.status === QueueStatus.Unresolved) {
fn.queueCount++;
q.push(fn);
}
Expand All @@ -459,19 +470,20 @@ function asyncModule(
return fn.queueCount ? promise : getResult();
}

Promise.resolve(body(handleAsyncDependencies)).then(
() => {
resolve(promise[turbopackExports]);
resolveQueue(queue);
},
(err) => {
function asyncResult(err?: any) {
if (err) {
reject((promise[turbopackError] = err));
resolveQueue(queue);
} else {
resolve(promise[turbopackExports]);
}
);

if (queue) {
queue.resolved = false;
resolveQueue(queue);
}

body(handleAsyncDependencies, asyncResult);

if (queue && queue.status === QueueStatus.Unknown) {
queue.status = QueueStatus.Unresolved;
}
}

Expand Down
9 changes: 7 additions & 2 deletions crates/turbopack-ecmascript/src/chunk/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,18 @@ impl EcmascriptChunkItemContent {

if this.options.async_module.is_some() {
code += "__turbopack_async_module__(async (__turbopack_handle_async_dependencies__, \
__turbopack_async_result__) => {\n";
__turbopack_async_result__) => { try {\n";
}

code.push_source(&this.inner_code, this.source_map);

if let Some(opts) = &this.options.async_module {
write!(code, "}}, {});", opts.has_top_level_await)?;
write!(
code,
"__turbopack_async_result__();\n}} catch(e) {{ __turbopack_async_result__(e); }} \
}}, {});",
opts.has_top_level_await
)?;
}

if this.options.this {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const magicAsyncModule = require("./magic")
const magicAsyncModule = require("./magic");

describe("complex wasm", () => {
it("should be possible to use imported memory", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { fibonacci } from "./fibonacci.wasm";
export { add, factorial, fibonacci };

export function factorialJavascript(i) {
if (i < 1) return 1;
return i * factorialJavascript(i - 1);
if (i < 1) return 1;
return i * factorialJavascript(i - 1);
}

export function fibonacciJavascript(i) {
if (i < 2) return 1;
return fibonacciJavascript(i - 1) + fibonacciJavascript(i - 2);
if (i < 2) return 1;
return fibonacciJavascript(i - 1) + fibonacciJavascript(i - 2);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./unknown.js";

await 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
it("should handle re-export from async modules correctly", async () => {
await import("./test.js");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./async-unknown.js";
export { a } from "./async-unknown.js";
export default "default";
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as ns from "./reexport-async-unknown.js?ns";
import { a, b, c } from "./reexport-async-unknown.js?named";
import value from "./reexport-async-unknown.js?default";

function nsObj(m) {
Object.defineProperty(m, Symbol.toStringTag, { value: "Module" });
return m;
}

expect(ns).toEqual(
nsObj({
default: "default",
a: "a",
b: "b",
c: "c",
})
);

expect(a).toBe("a");
expect(b).toBe("b");
expect(c).toBe("c");

expect(value).toBe("default");
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const o = {
a: "a",
b: "b",
c: "c",
};

module.exports = Object(o);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import x from "./shared";

export default x + " world";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import x from "./shared";

export default x + " world";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
it("should allow to import an async module twice", async () => {
const result = await require("./main");
expect(result.default).toBe("hello world, hello world");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import a from "./a";
import b from "./b";

export default a + ", " + b;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
await 1;
await 1;
export default "hello";
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import i, { foo } from "./won't-run-tla";

it("should have value imported from won't-run-tla", async () => {
expect(i).toBe(42);
expect(foo).toBe(undefined);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global.someNonExistentVariable && (await "test");
const foo = global.otherSomeNonExistentVariable && (await 43);
export default 42;
export { foo };
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { report } from "../tick";
import "./async";

report("a");
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { report } from "../tick";

report("async before");
await 0;
report("async middle");
await 0;
report("async after");
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { report } from "../tick";
import "./d";

report("async2 before");
await 0;
report("async2 middle");
await 0;
report("async2 after");
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { report } from "../tick";
import "./async";

report("b");
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { report } from "../tick";
import "./b";

report("c");
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { report } from "../tick";
import "./c";
import "./a";

report("d");
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { report } from "../tick";

report("e");
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { report } from "../tick";
import "./e";
import "./async2";

report("f");
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { report } from "../tick";
import "./async";
import "./b";

report("a");
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { report } from "../tick";

report("async before");
await 0;
report("async middle");
await 0;
report("async after");
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { report } from "../tick";
import "./c";

report("b");
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { report } from "../tick";
import "./async";

report("c");
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { report } from "../tick";
import "./c";

report("d");
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { report } from "../tick";
import "./a";
import "./d";

report("async before");
await 0;
report("async middle");
await 0;
report("async after");
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { report } from "../tick";
import "./b";
import "./a";

report("a before");
await 0;
report("a after");
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { report } from "../tick";

report("b");
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { report } from "../tick";
import "./b";

report("a before");
await 0;
report("a after");
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { report } from "../tick";
import "./c";

report("b before");
await 0;
report("b after");
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { report } from "../tick";
import "./a";

report("c before");
await 0;
report("c after");
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { report } from "../tick";
import "./x";
import "./y";

report("index");
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { report } from "../tick";
import "./a";

report("x");
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { report } from "../tick";
import "./b";

report("y");
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { report } from "../tick";
import "./async";

report("a");
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { report } from "../tick";

report("async before");
await 0;
report("async middle");
await 0;
report("async after");
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { report } from "../tick";
import "./async";

report("b");
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { report } from "../tick";
import "./a";
import "./b";
import "./x";

report("index");
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { report } from "../tick";
import "./a";

report("x");
Loading

0 comments on commit 959442e

Please sign in to comment.