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

preopen's not really working #6

Closed
talentlessguy opened this issue Feb 28, 2025 · 2 comments · Fixed by #8
Closed

preopen's not really working #6

talentlessguy opened this issue Feb 28, 2025 · 2 comments · Fixed by #8

Comments

@talentlessguy
Copy link

talentlessguy commented Feb 28, 2025

Hey, first of all wanted to thank you for this amazing library! It works like a charm with both Node and Deno.

I am experimenting with WASI and I stumbled upon an issue that pre-open is not really working for me. I'm not sure if I'm doing it right, but preopening a current dir didn't help.

It works like fine with wasmtime:

wasmtime --dir=. solar.wasm Counter.sol --emit abi
{"contracts":{"Counter.sol:Counter":{"abi":[{"type":"function","name":"increment","inputs":[],"outputs":[],"stateMutability":"nonpayable"},{"type":"function","name":"number","inputs":[],"outputs":[{"name":"","type":"uint256","internalType":"uint256"}],"stateMutability":"view"},{"type":"function","name":"setNumber","inputs":[{"name":"newNumber","type":"uint256","internalType":"uint256"}],"outputs":[],"stateMutability":"nonpayable"}]}},"version":"0.1.1"}

but doing something similar in JS fails:

import { useAll, WASI } from "uwasi";
import fs from "node:fs/promises";
import process from "node:process";

export const setupSolar = async (args = []) => {
  const wasi = new WASI({
    args,
    preopens: {
      "./": import.meta.dirname,
    },
    features: [
      useAll({})],
  });
  const bytes = await fs.readFile("./solar.wasm");
  const { instance } = await WebAssembly.instantiate(bytes, {
    wasi_snapshot_preview1: wasi.wasiImport,
  });

  return wasi.start(instance);
};

await setupSolar(["-j1", "./Counter.sol", "--emit", "abi"]);

throws:

error: file ./Counter.sol not found


error: aborting due to 1 previous error

I've compiled the source library with the wasip1 target using this method: paradigmxyz/solar#211 (comment)

@andrewmd5
Copy link
Contributor

Currently there is no built-in native file system support. I just contributed a memory file system which you can use roughly like:

const fileSystem = new MemoryFileSystem({
    "/": "",
  });
 fileSystem.addFile("/Counter.sol", fileContents);
useMemoryFS({
        withFileSystem: fileSystem,
        withStdIo: {
          stdout: (str) => {
            if (ArrayBuffer.isView(str)) {
              str = textDecoder.decode(str);
            }
            if (StringBuilder.isMultiline(str)) {
              stdout.append(str);
            } else {
              stdout.appendLine(str);
            }
          },
          stderr: (str) => {
            if (ArrayBuffer.isView(str)) {
              str = textDecoder.decode(str);
            }
            if (StringBuilder.isMultiline(str)) {
              stderr.append(str);
            } else {
              stderr.appendLine(str);
            }
          },
        },
      }),

Full example here.

@talentlessguy
Copy link
Author

Where do I import MemoryFileSystem from?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants