Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: top-level-await module execution #7946

Merged
merged 22 commits into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions cli/ops/worker_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,15 @@ fn run_worker_thread(
rt.block_on(load_future)
};

if let Err(e) = result {
let mut sender = worker.internal_channels.sender.clone();
let mut sender = worker.internal_channels.sender.clone();

// If sender is closed it means that worker has already been closed from
// within using "globalThis.close()"
if sender.is_closed() {
return;
}

if let Err(e) = result {
sender
.try_send(WorkerEvent::TerminalError(e))
.expect("Failed to post message to host");
Expand Down
10 changes: 10 additions & 0 deletions cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2410,6 +2410,16 @@ itest!(wasm_unreachable {
exit_code: 1,
});

itest!(top_level_await_order {
args: "run --allow-read top_level_await_order.js",
output: "top_level_await_order.out",
});

itest!(top_level_await_loop {
args: "run --allow-read top_level_await_loop.js",
output: "top_level_await_loop.out",
});

itest!(top_level_await {
args: "run --allow-read top_level_await.js",
output: "top_level_await.out",
Expand Down
3 changes: 3 additions & 0 deletions cli/tests/tla/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import order from "./order.js";

order.push("b");
7 changes: 7 additions & 0 deletions cli/tests/tla/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import order from "./order.js";

await new Promise((resolve) => {
setTimeout(resolve, 200);
});

order.push("a");
3 changes: 3 additions & 0 deletions cli/tests/tla/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import order from "./order.js";

order.push("c");
6 changes: 6 additions & 0 deletions cli/tests/tla/d.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import order from "./order.js";

const end = Date.now() + 500;
while (end < Date.now()) {}

order.push("d");
1 change: 1 addition & 0 deletions cli/tests/tla/order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default ["order"];
9 changes: 9 additions & 0 deletions cli/tests/tla/parent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import order from "./order.js";
import "./a.js";
import "./b.js";
import "./c.js";
import "./d.js";

order.push("parent");

export default order;
5 changes: 5 additions & 0 deletions cli/tests/tla2/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default class Foo {
constructor(message) {
this.message = message;
}
}
5 changes: 5 additions & 0 deletions cli/tests/tla2/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default class Bar {
constructor(message) {
this.message = message;
}
}
18 changes: 18 additions & 0 deletions cli/tests/top_level_await_loop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const importsDir = Deno.readDirSync(Deno.realPathSync("./tla2"));

const resolvedPaths = [];

for (const { name } of importsDir) {
const filePath = Deno.realPathSync(`./tla2/${name}`);
resolvedPaths.push(filePath);
}

resolvedPaths.sort();

for (const filePath of resolvedPaths) {
console.log("loading", filePath);
const mod = await import(`file://${filePath}`);
console.log("loaded", mod);
}

console.log("all loaded");
5 changes: 5 additions & 0 deletions cli/tests/top_level_await_loop.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
loading [WILDCARD]a.js
loaded Module { default: [Function: Foo], [Symbol(Symbol.toStringTag)]: "Module" }
loading [WILDCARD]b.js
loaded Module { default: [Function: Bar], [Symbol(Symbol.toStringTag)]: "Module" }
all loaded
21 changes: 21 additions & 0 deletions cli/tests/top_level_await_order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Ported from Node
// https://github.com/nodejs/node/blob/54746bb763ebea0dc7e99d88ff4b379bcd680964/test/es-module/test-esm-tla.mjs

const { default: order } = await import("./tla/parent.js");

console.log("order", JSON.stringify(order));

if (
!(
order[0] === "order" &&
order[1] === "b" &&
order[2] === "c" &&
order[3] === "d" &&
order[4] === "a" &&
order[5] === "parent"
)
) {
throw new Error("TLA wrong order");
}

console.log("TLA order correct");
2 changes: 2 additions & 0 deletions cli/tests/top_level_await_order.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
order ["order","b","c","d","a","parent"]
TLA order correct
2 changes: 1 addition & 1 deletion cli/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl Worker {
) -> Result<(), AnyError> {
let id = self.preload_module(module_specifier).await?;
self.wait_for_inspector_session();
self.js_runtime.mod_evaluate(id)
self.js_runtime.mod_evaluate(id).await
}

/// Returns a way to communicate with the Worker from other threads.
Expand Down
8 changes: 4 additions & 4 deletions core/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ mod tests {
let a_id_fut = runtime.load_module(&spec, None);
let a_id = futures::executor::block_on(a_id_fut).expect("Failed to load");

runtime.mod_evaluate(a_id).unwrap();
futures::executor::block_on(runtime.mod_evaluate(a_id)).unwrap();
let l = loads.lock().unwrap();
assert_eq!(
l.to_vec(),
Expand Down Expand Up @@ -734,7 +734,7 @@ mod tests {
let result = runtime.load_module(&spec, None).await;
assert!(result.is_ok());
let circular1_id = result.unwrap();
runtime.mod_evaluate(circular1_id).unwrap();
runtime.mod_evaluate(circular1_id).await.unwrap();

let l = loads.lock().unwrap();
assert_eq!(
Expand Down Expand Up @@ -811,7 +811,7 @@ mod tests {
println!(">> result {:?}", result);
assert!(result.is_ok());
let redirect1_id = result.unwrap();
runtime.mod_evaluate(redirect1_id).unwrap();
runtime.mod_evaluate(redirect1_id).await.unwrap();
let l = loads.lock().unwrap();
assert_eq!(
l.to_vec(),
Expand Down Expand Up @@ -961,7 +961,7 @@ mod tests {
let main_id =
futures::executor::block_on(main_id_fut).expect("Failed to load");

runtime.mod_evaluate(main_id).unwrap();
futures::executor::block_on(runtime.mod_evaluate(main_id)).unwrap();

let l = loads.lock().unwrap();
assert_eq!(
Expand Down
Loading