bytecodealliance / jco

JavaScript toolchain for working with WebAssembly Components
https://bytecodealliance.github.io/jco/
Apache License 2.0
640 stars 63 forks source link

preview2-shim: `TypeError: Cannot read properties of null (reading 'startsWith')` on browser #497

Closed kateinoigakukun closed 2 months ago

kateinoigakukun commented 2 months ago

Minimum repro

fn main() {
    use std::fs;
    let metadata = fs::metadata(".");
    println!("{:?}", metadata);
}
$ rustc check.rs --target wasm32-wasip1 -o check.core.wasm
$ npx jco new --wasi-command ./check.core.wasm -o check.wasm
$ npx jco transpile check.wasm -o component
<html>
  <script type="importmap">
    {
      "imports": {
        "@bytecodealliance/preview2-shim/filesystem": "https://cdn.jsdelivr.net/npm/@bytecodealliance/preview2-shim@0.16.5/lib/browser/filesystem.js",
        "@bytecodealliance/preview2-shim/io": "https://cdn.jsdelivr.net/npm/@bytecodealliance/preview2-shim@0.16.5/lib/browser/io.js",
        "@bytecodealliance/preview2-shim/cli": "https://cdn.jsdelivr.net/npm/@bytecodealliance/preview2-shim@0.16.5/lib/browser/cli.js"
      }
    }
  </script>
  <script type="module">
    import { run } from './component/check.js';
    run.run();
  </script>
</html>

Then we get the following:

TypeError: Cannot read properties of null (reading 'startsWith')
    at getChildEntry (filesystem.js:33:17)
    at Descriptor.statAt (filesystem.js:201:19)
    at trampoline13 (check.js:1063:57)
    at wit-component:shim.indirect-wasi:filesystem/types@0.2.0-rc-2023-11-10-[method]descriptor.stat-at (wit-component:shim-f0b2f95a:0x11f)
    at wit-component:adapter:wasi_snapshot_preview1._ZN22wasi_snapshot_preview18bindings4wasi10filesystem5types10Descriptor7stat_at17hba52cb8364bf76aaE (check.core2.wasm:0x291e)
    at wit-component:adapter:wasi_snapshot_preview1.path_filestat_get (check.core2.wasm:0x21f8)
    at wit-component:shim.adapt-wasi_snapshot_preview1-path_filestat_get (wit-component:shim-f0b2f95a:0x1a1)
    at wasi::lib_generated::path_filestat_get::h0652677c656d456f (lib_generated.rs:1739)
    at std::sys::pal::wasi::fd::WasiFd::path_filestat_get::h13c43d2132dd560f (fd.rs:214)
    at std::sys::pal::wasi::fs::metadata_at::h6829004f9b6a5cf2 (fs.rs:651)
    at std::sys::pal::wasi::fs::stat::hdff972b2d4208143 (fs.rs:642)
    at check.core.wasm._ZN3std2fs8metadata17h0cddf98eb593bfdbE (check.core.wasm:0x630)

I guess the problem here is that cwd variable is initialized as null but I'm not sure what it should be on browser.

guybedford commented 2 months ago

Was this resolved in https://github.com/bytecodealliance/jco/pull/496?

kateinoigakukun commented 2 months ago

It's a separate issue :cry:

guybedford commented 2 months ago

The browser implementation is very much an experimental placeholder for now - do feel free to make any changes you'd like to this implementation further. Perhaps we just need to decide on a cwd.