Cheng Shao pushed to branch wip/wasm-dyld-pie at Glasgow Haskell Compiler / GHC

Commits:

1 changed file:

Changes:

  • utils/jsffi/dyld.mjs
    ... ... @@ -318,11 +318,7 @@ class DyLDHost {
    318 318
       // Deduped absolute paths of directories where we lookup .so files
    
    319 319
       #rpaths = new Set();
    
    320 320
     
    
    321
    -  constructor() {
    
    322
    -    // Inherited pipe file descriptors from GHC
    
    323
    -    const out_fd = Number.parseInt(process.argv[4]),
    
    324
    -      in_fd = Number.parseInt(process.argv[5]);
    
    325
    -
    
    321
    +  constructor({ out_fd, in_fd }) {
    
    326 322
         this.readStream = stream.Readable.toWeb(
    
    327 323
           fs.createReadStream(undefined, { fd: in_fd })
    
    328 324
         );
    
    ... ... @@ -494,7 +490,7 @@ export class DyLDRPC {
    494 490
     
    
    495 491
     // Actual implementation of endpoints used by DyLDRPC
    
    496 492
     class DyLDRPCServer {
    
    497
    -  #dyldHost = new DyLDHost();
    
    493
    +  #dyldHost;
    
    498 494
       #server;
    
    499 495
       #wss;
    
    500 496
     
    
    ... ... @@ -504,9 +500,13 @@ class DyLDRPCServer {
    504 500
         dyldPath,
    
    505 501
         libdir,
    
    506 502
         ghciSoPath,
    
    503
    +    out_fd,
    
    504
    +    in_fd,
    
    507 505
         args,
    
    508 506
         redirectWasiConsole,
    
    509 507
       }) {
    
    508
    +    this.#dyldHost = new DyLDHost({ out_fd, in_fd });
    
    509
    +
    
    510 510
         this.#server = http.createServer(async (req, res) => {
    
    511 511
           const origin = originFromServerAddress(await this.listening);
    
    512 512
     
    
    ... ... @@ -1252,17 +1252,9 @@ export async function main({ rpc, libdir, ghciSoPath, args }) {
    1252 1252
       }
    
    1253 1253
     }
    
    1254 1254
     
    
    1255
    -(async () => {
    
    1256
    -  if (!isNode) {
    
    1257
    -    return;
    
    1258
    -  }
    
    1259
    -
    
    1260
    -  const libdir = process.argv[2];
    
    1261
    -  const ghciSoPath = process.argv[3];
    
    1262
    -  const args = process.argv.slice(6);
    
    1263
    -
    
    1255
    +export async function nodeMain({ libdir, ghciSoPath, out_fd, in_fd, args }) {
    
    1264 1256
       if (!process.env.GHCI_BROWSER) {
    
    1265
    -    const rpc = new DyLDHost();
    
    1257
    +    const rpc = new DyLDHost({ out_fd, in_fd });
    
    1266 1258
         await main({
    
    1267 1259
           rpc,
    
    1268 1260
           libdir,
    
    ... ... @@ -1284,6 +1276,8 @@ export async function main({ rpc, libdir, ghciSoPath, args }) {
    1284 1276
         dyldPath: import.meta.filename,
    
    1285 1277
         libdir,
    
    1286 1278
         ghciSoPath,
    
    1279
    +    out_fd,
    
    1280
    +    in_fd,
    
    1287 1281
         args,
    
    1288 1282
         redirectWasiConsole:
    
    1289 1283
           process.env.GHCI_BROWSER_PUPPETEER_LAUNCH_OPTS ||
    
    ... ... @@ -1374,4 +1368,22 @@ export async function main({ rpc, libdir, ghciSoPath, args }) {
    1374 1368
       console.log(
    
    1375 1369
         `Open ${origin}/main.html or import ${origin}/main.js to boot ghci`
    
    1376 1370
       );
    
    1377
    -})();
    1371
    +}
    
    1372
    +
    
    1373
    +function isNodeMain() {
    
    1374
    +  if (!globalThis?.process?.versions?.node) {
    
    1375
    +    return false;
    
    1376
    +  }
    
    1377
    +
    
    1378
    +  return import.meta.filename === process.argv[1];
    
    1379
    +}
    
    1380
    +
    
    1381
    +if (isNodeMain()) {
    
    1382
    +  const libdir = process.argv[2];
    
    1383
    +  const ghciSoPath = process.argv[3];
    
    1384
    +  const out_fd = Number.parseInt(process.argv[4]),
    
    1385
    +    in_fd = Number.parseInt(process.argv[5]);
    
    1386
    +  const args = process.argv.slice(6);
    
    1387
    +
    
    1388
    +  nodeMain({ libdir, ghciSoPath, out_fd, in_fd, args });
    
    1389
    +}