Skip to content
Snippets Groups Projects
Commit 53770d00 authored by Cheng Shao's avatar Cheng Shao
Browse files

wasm: fix post-link.mjs for browser

The wasm ghci browser mode needs to run dyld.mjs in the browser which
imports post-link.mjs. This script makes post-link.mjs runnable in the
browser by deferring node-specific module imports to their actual use
sites.

(cherry picked from commit efcebed6)
(cherry picked from commit 2b6ab4cc)
parent c3aa0aaa
No related branches found
No related tags found
No related merge requests found
...@@ -7,10 +7,6 @@ ...@@ -7,10 +7,6 @@
// foo.js", as well as an exported postLink function that takes a // foo.js", as well as an exported postLink function that takes a
// WebAssembly.Module object and returns the ESM module content. // WebAssembly.Module object and returns the ESM module content.
import fs from "node:fs/promises";
import path from "node:path";
import util from "node:util";
// Each record in the ghc_wasm_jsffi custom section are 3 // Each record in the ghc_wasm_jsffi custom section are 3
// NUL-terminated strings: name, binder, body. We try to parse the // NUL-terminated strings: name, binder, body. We try to parse the
// body as an expression and fallback to statements, and return the // body as an expression and fallback to statements, and return the
...@@ -94,6 +90,9 @@ export function parseSections(mod) { ...@@ -94,6 +90,9 @@ export function parseSections(mod) {
// immediately invokes it by passing the right magic variables. // immediately invokes it by passing the right magic variables.
export async function postLink(mod) { export async function postLink(mod) {
const fs = (await import("node:fs/promises")).default;
const path = (await import("node:path")).default;
let src = ( let src = (
await fs.readFile(path.join(import.meta.dirname, "prelude.mjs"), { await fs.readFile(path.join(import.meta.dirname, "prelude.mjs"), {
encoding: "utf-8", encoding: "utf-8",
...@@ -116,10 +115,17 @@ export async function postLink(mod) { ...@@ -116,10 +115,17 @@ export async function postLink(mod) {
} }
function isMain() { function isMain() {
if (!globalThis?.process?.versions?.node) {
return false;
}
return import.meta.filename === process.argv[1]; return import.meta.filename === process.argv[1];
} }
async function main() { async function main() {
const fs = (await import("node:fs/promises")).default;
const util = (await import("node:util")).default;
const { input, output } = util.parseArgs({ const { input, output } = util.parseArgs({
options: { options: {
input: { input: {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment