Stream: git-wasmtime

Topic: wasmtime / issue #5614 How to use wasmtime to execute .wa...


view this post on Zulip Wasmtime GitHub notifications bot (Jan 21 2023 at 06:58):

guest271314 opened issue #5614:

I downloaded the WASI SDK and wasmtime. I then compiled working C Native Messaging host to WASM, with and without --sysroot option

$ /home/user/wasi-sdk-19.0/bin/clang --sysroot=/home/xubuntu/localscripts/wasi-sdk-19.0/share/wasi-sysroot /home/user/native-messaging-c/nm_c.c -o nm_wasm.wasm

then tried

#!/usr/bin/env -S wasmtime run /path/to/nm_wasm.wasm

I then used wasm2wat to try to run the module that way, first testing with hello demo

#!/usr/bin/env wasmtime
(module
    ;; Import the required fd_write WASI function which will write the given io vectors to stdout
    ;; The function signature for fd_write is:
    ;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written
    (import "wasi_unstable" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))

    (memory 1)
    (export "memory" (memory 0))

    ;; Write 'hello world\n' to memory at an offset of 8 bytes
    ;; Note the trailing newline which is required for the text to appear
    (data (i32.const 8) "hello world\n")

    (func $main (export "_start")
        ;; Creating a new io vector within linear memory
        (i32.store (i32.const 0) (i32.const 8))  ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string
        (i32.store (i32.const 4) (i32.const 12))  ;; iov.iov_len - The length of the 'hello world\n' string

        (call $fd_write
            (i32.const 1) ;; file_descriptor - 1 for stdout
            (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0
            (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one.
            (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written
        )
        drop ;; Discard the number of bytes written from the top of the stack
    )
)

and after the script exited I searched and located https://github.com/bytecodealliance/wasmtime/issues/3715 then tried

#!/usr/bin/sh

wasmtime run /dev/stdin <<__END__
(module
  (type $t0 (func (param i32 i32 i32) (result i32)))
  (type $t1 (func (param i32 i64 i32) (result i64)))
  (type $t2 (func (param i32) (result i32)))
  (type $t3 (func (param i32 i32) (result i32)))
  (type $t4 (func (param i32 i32 i32 i32) (result i32)))
  (type $t5 (func (param i32 i64 i32 i32) (result i32)))
  (type $t6 (func (param i32)))
  (type $t7 (func))
  (type $t8 (func (result i32)))
  (import "wasi_snapshot_preview1" "fd_close" (func $__imported_wasi_snapshot_preview1_fd_close (type $t2)))
  (import "wasi_snapshot_preview1" "fd_fdstat_get" (func $__imported_wasi_snapshot_preview1_fd_fdstat_get (type $t3)))
  (import "wasi_snapshot_preview1" "fd_read" (func $__imported_wasi_snapshot_preview1_fd_read (type $t4)))
  (import "wasi_snapshot_preview1" "fd_seek" (func $__imported_wasi_snapshot_preview1_fd_seek (type $t5)))
  (import "wasi_snapshot_preview1" "fd_write" (func $__imported_wasi_snapshot_preview1_fd_write (type $t4)))
  (import "wasi_snapshot_preview1" "proc_exit" (func $__imported_wasi_snapshot_preview1_proc_exit (type $t6)))
  (func $__wasm_call_ctors (type $t7))
  (func $_start (export "_start") (type $t7)
;; ...

to no avail. See https://github.com/WebAssembly/wasi-filesystem/discussions/82.

Native Messaging protocol is initiated from the browser. The host reads input from the browser as stdin, stdout from the host is sent to the client browser. See Native messaging protocol; and MDN's Native Messaging.

Notice how we can use shebang line in a .js file where # is not a comment and .py file.

Not sure how to solve this given lack of shebang support and inability to utilize /usr/bin/env with S option.

Asking for help to achieve the requirement.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 21 2023 at 07:00):

guest271314 edited issue #5614:

I downloaded the WASI SDK and wasmtime. I then compiled working C Native Messaging host to WASM, with and without --sysroot option

$ /home/user/wasi-sdk-19.0/bin/clang --sysroot=/home/user/wasi-sdk-19.0/share/wasi-sysroot /home/user/native-messaging-c/nm_c.c -o nm_wasm.wasm

then tried

#!/usr/bin/env -S wasmtime run /path/to/nm_wasm.wasm

I then used wasm2wat to try to run the module that way, first testing with hello demo

#!/usr/bin/env wasmtime
(module
    ;; Import the required fd_write WASI function which will write the given io vectors to stdout
    ;; The function signature for fd_write is:
    ;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written
    (import "wasi_unstable" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))

    (memory 1)
    (export "memory" (memory 0))

    ;; Write 'hello world\n' to memory at an offset of 8 bytes
    ;; Note the trailing newline which is required for the text to appear
    (data (i32.const 8) "hello world\n")

    (func $main (export "_start")
        ;; Creating a new io vector within linear memory
        (i32.store (i32.const 0) (i32.const 8))  ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string
        (i32.store (i32.const 4) (i32.const 12))  ;; iov.iov_len - The length of the 'hello world\n' string

        (call $fd_write
            (i32.const 1) ;; file_descriptor - 1 for stdout
            (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0
            (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one.
            (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written
        )
        drop ;; Discard the number of bytes written from the top of the stack
    )
)

and after the script exited I searched and located https://github.com/bytecodealliance/wasmtime/issues/3715 then tried

#!/usr/bin/sh

wasmtime run /dev/stdin <<__END__
(module
  (type $t0 (func (param i32 i32 i32) (result i32)))
  (type $t1 (func (param i32 i64 i32) (result i64)))
  (type $t2 (func (param i32) (result i32)))
  (type $t3 (func (param i32 i32) (result i32)))
  (type $t4 (func (param i32 i32 i32 i32) (result i32)))
  (type $t5 (func (param i32 i64 i32 i32) (result i32)))
  (type $t6 (func (param i32)))
  (type $t7 (func))
  (type $t8 (func (result i32)))
  (import "wasi_snapshot_preview1" "fd_close" (func $__imported_wasi_snapshot_preview1_fd_close (type $t2)))
  (import "wasi_snapshot_preview1" "fd_fdstat_get" (func $__imported_wasi_snapshot_preview1_fd_fdstat_get (type $t3)))
  (import "wasi_snapshot_preview1" "fd_read" (func $__imported_wasi_snapshot_preview1_fd_read (type $t4)))
  (import "wasi_snapshot_preview1" "fd_seek" (func $__imported_wasi_snapshot_preview1_fd_seek (type $t5)))
  (import "wasi_snapshot_preview1" "fd_write" (func $__imported_wasi_snapshot_preview1_fd_write (type $t4)))
  (import "wasi_snapshot_preview1" "proc_exit" (func $__imported_wasi_snapshot_preview1_proc_exit (type $t6)))
  (func $__wasm_call_ctors (type $t7))
  (func $_start (export "_start") (type $t7)
;; ...

to no avail. See https://github.com/WebAssembly/wasi-filesystem/discussions/82.

Native Messaging protocol is initiated from the browser. The host reads input from the browser as stdin, stdout from the host is sent to the client browser. See Native messaging protocol; and MDN's Native Messaging.

Notice how we can use shebang line in a .js file where # is not a comment and .py file.

Not sure how to solve this given lack of shebang support and inability to utilize /usr/bin/env with S option.

Asking for help to achieve the requirement.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 21 2023 at 07:03):

guest271314 edited issue #5614:

I downloaded the WASI SDK and wasmtime. I then compiled working C Native Messaging host to WASM, with and without --sysroot option

$ /home/user/wasi-sdk-19.0/bin/clang --sysroot=/home/user/wasi-sdk-19.0/share/wasi-sysroot /home/user/native-messaging-c/nm_c.c -o nm_wasm.wasm

then tried

#!/usr/bin/env -S wasmtime run /path/to/nm_wasm.wasm

and

$ wasmtime compile nm_c.wasm

nm_wasm.sh

#!/usr/bin/env wasmtime nm_c.cwasm

I then used wasm2wat to try to run the module that way, first testing with hello demo

#!/usr/bin/env wasmtime
(module
    ;; Import the required fd_write WASI function which will write the given io vectors to stdout
    ;; The function signature for fd_write is:
    ;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written
    (import "wasi_unstable" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))

    (memory 1)
    (export "memory" (memory 0))

    ;; Write 'hello world\n' to memory at an offset of 8 bytes
    ;; Note the trailing newline which is required for the text to appear
    (data (i32.const 8) "hello world\n")

    (func $main (export "_start")
        ;; Creating a new io vector within linear memory
        (i32.store (i32.const 0) (i32.const 8))  ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string
        (i32.store (i32.const 4) (i32.const 12))  ;; iov.iov_len - The length of the 'hello world\n' string

        (call $fd_write
            (i32.const 1) ;; file_descriptor - 1 for stdout
            (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0
            (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one.
            (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written
        )
        drop ;; Discard the number of bytes written from the top of the stack
    )
)

and after the script exited I searched and located https://github.com/bytecodealliance/wasmtime/issues/3715 then tried

#!/usr/bin/sh

wasmtime run /dev/stdin <<__END__
(module
  (type $t0 (func (param i32 i32 i32) (result i32)))
  (type $t1 (func (param i32 i64 i32) (result i64)))
  (type $t2 (func (param i32) (result i32)))
  (type $t3 (func (param i32 i32) (result i32)))
  (type $t4 (func (param i32 i32 i32 i32) (result i32)))
  (type $t5 (func (param i32 i64 i32 i32) (result i32)))
  (type $t6 (func (param i32)))
  (type $t7 (func))
  (type $t8 (func (result i32)))
  (import "wasi_snapshot_preview1" "fd_close" (func $__imported_wasi_snapshot_preview1_fd_close (type $t2)))
  (import "wasi_snapshot_preview1" "fd_fdstat_get" (func $__imported_wasi_snapshot_preview1_fd_fdstat_get (type $t3)))
  (import "wasi_snapshot_preview1" "fd_read" (func $__imported_wasi_snapshot_preview1_fd_read (type $t4)))
  (import "wasi_snapshot_preview1" "fd_seek" (func $__imported_wasi_snapshot_preview1_fd_seek (type $t5)))
  (import "wasi_snapshot_preview1" "fd_write" (func $__imported_wasi_snapshot_preview1_fd_write (type $t4)))
  (import "wasi_snapshot_preview1" "proc_exit" (func $__imported_wasi_snapshot_preview1_proc_exit (type $t6)))
  (func $__wasm_call_ctors (type $t7))
  (func $_start (export "_start") (type $t7)
;; ...

to no avail. See https://github.com/WebAssembly/wasi-filesystem/discussions/82.

Native Messaging protocol is initiated from the browser. The host reads input from the browser as stdin, stdout from the host is sent to the client browser. See Native messaging protocol; and MDN's Native Messaging.

Notice how we can use shebang line in a .js file where # is not a comment and .py file.

Not sure how to solve this given lack of shebang support and inability to utilize /usr/bin/env with S option.

Asking for help to achieve the requirement.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 21 2023 at 07:05):

guest271314 edited issue #5614:

I downloaded the WASI SDK and wasmtime. I then compiled working C Native Messaging host to WASM, with and without --sysroot option

$ /home/user/wasi-sdk-19.0/bin/clang --sysroot=/home/user/wasi-sdk-19.0/share/wasi-sysroot /home/user/native-messaging-c/nm_c.c -o nm_wasm.wasm

then tried

#!/usr/bin/env -S wasmtime run /path/to/nm_wasm.wasm

and

$ wasmtime compile nm_c.wasm

nm_wasm.sh

#!/usr/bin/env wasmtime nm_c.cwasm

I then used wasm2wat to try to run the module that way, first testing with hello demo

#!/usr/bin/env wasmtime
(module
    ;; Import the required fd_write WASI function which will write the given io vectors to stdout
    ;; The function signature for fd_write is:
    ;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written
    (import "wasi_unstable" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))

    (memory 1)
    (export "memory" (memory 0))

    ;; Write 'hello world\n' to memory at an offset of 8 bytes
    ;; Note the trailing newline which is required for the text to appear
    (data (i32.const 8) "hello world\n")

    (func $main (export "_start")
        ;; Creating a new io vector within linear memory
        (i32.store (i32.const 0) (i32.const 8))  ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string
        (i32.store (i32.const 4) (i32.const 12))  ;; iov.iov_len - The length of the 'hello world\n' string

        (call $fd_write
            (i32.const 1) ;; file_descriptor - 1 for stdout
            (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0
            (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one.
            (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written
        )
        drop ;; Discard the number of bytes written from the top of the stack
    )
)

and after the script exited I searched and located https://github.com/bytecodealliance/wasmtime/issues/3715 then tried

#!/usr/bin/sh

wasmtime run /dev/stdin <<__END__
(module
  (type $t0 (func (param i32 i32 i32) (result i32)))
  (type $t1 (func (param i32 i64 i32) (result i64)))
  (type $t2 (func (param i32) (result i32)))
  (type $t3 (func (param i32 i32) (result i32)))
  (type $t4 (func (param i32 i32 i32 i32) (result i32)))
  (type $t5 (func (param i32 i64 i32 i32) (result i32)))
  (type $t6 (func (param i32)))
  (type $t7 (func))
  (type $t8 (func (result i32)))
  (import "wasi_snapshot_preview1" "fd_close" (func $__imported_wasi_snapshot_preview1_fd_close (type $t2)))
  (import "wasi_snapshot_preview1" "fd_fdstat_get" (func $__imported_wasi_snapshot_preview1_fd_fdstat_get (type $t3)))
  (import "wasi_snapshot_preview1" "fd_read" (func $__imported_wasi_snapshot_preview1_fd_read (type $t4)))
  (import "wasi_snapshot_preview1" "fd_seek" (func $__imported_wasi_snapshot_preview1_fd_seek (type $t5)))
  (import "wasi_snapshot_preview1" "fd_write" (func $__imported_wasi_snapshot_preview1_fd_write (type $t4)))
  (import "wasi_snapshot_preview1" "proc_exit" (func $__imported_wasi_snapshot_preview1_proc_exit (type $t6)))
  (func $__wasm_call_ctors (type $t7))
  (func $_start (export "_start") (type $t7)
;; ...

to no avail. See https://github.com/WebAssembly/wasi-filesystem/discussions/82.

Native Messaging protocol is initiated from the browser. The host reads input from the browser as stdin, stdout from the host is sent to the client browser. See Native messaging protocol; and MDN's Native Messaging.

Notice how we can use shebang line in a .js file where # is not a comment and .py file. I use shebang line in QuickJS, Deno, Bun, Node.js - and Python Native Messaging hosts.

Not sure how to solve this given lack of shebang support and inability to utilize /usr/bin/env with S option.

Asking for help to achieve the requirement.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 21 2023 at 07:13):

guest271314 edited issue #5614:

I downloaded the WASI SDK and wasmtime. I then compiled working C Native Messaging host to WASM, with and without --sysroot option

$ /home/user/wasi-sdk-19.0/bin/clang --sysroot=/home/user/wasi-sdk-19.0/share/wasi-sysroot /home/user/native-messaging-c/nm_c.c -o nm_wasm.wasm

then tried

#!/usr/bin/env -S wasmtime run /path/to/nm_wasm.wasm

and

$ wasmtime compile nm_c.wasm

nm_wasm.sh

#!/usr/bin/env wasmtime nm_c.cwasm

I then used wasm2wat to try to run the module that way, first testing with hello demo

#!/usr/bin/env wasmtime
(module
    ;; Import the required fd_write WASI function which will write the given io vectors to stdout
    ;; The function signature for fd_write is:
    ;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written
    (import "wasi_unstable" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))

    (memory 1)
    (export "memory" (memory 0))

    ;; Write 'hello world\n' to memory at an offset of 8 bytes
    ;; Note the trailing newline which is required for the text to appear
    (data (i32.const 8) "hello world\n")

    (func $main (export "_start")
        ;; Creating a new io vector within linear memory
        (i32.store (i32.const 0) (i32.const 8))  ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string
        (i32.store (i32.const 4) (i32.const 12))  ;; iov.iov_len - The length of the 'hello world\n' string

        (call $fd_write
            (i32.const 1) ;; file_descriptor - 1 for stdout
            (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0
            (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one.
            (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written
        )
        drop ;; Discard the number of bytes written from the top of the stack
    )
)

and after the script exited I searched and located https://github.com/bytecodealliance/wasmtime/issues/3715 then tried

#!/usr/bin/sh

wasmtime run /dev/stdin <<__END__
(module
  (type $t0 (func (param i32 i32 i32) (result i32)))
  (type $t1 (func (param i32 i64 i32) (result i64)))
  (type $t2 (func (param i32) (result i32)))
  (type $t3 (func (param i32 i32) (result i32)))
  (type $t4 (func (param i32 i32 i32 i32) (result i32)))
  (type $t5 (func (param i32 i64 i32 i32) (result i32)))
  (type $t6 (func (param i32)))
  (type $t7 (func))
  (type $t8 (func (result i32)))
  (import "wasi_snapshot_preview1" "fd_close" (func $__imported_wasi_snapshot_preview1_fd_close (type $t2)))
  (import "wasi_snapshot_preview1" "fd_fdstat_get" (func $__imported_wasi_snapshot_preview1_fd_fdstat_get (type $t3)))
  (import "wasi_snapshot_preview1" "fd_read" (func $__imported_wasi_snapshot_preview1_fd_read (type $t4)))
  (import "wasi_snapshot_preview1" "fd_seek" (func $__imported_wasi_snapshot_preview1_fd_seek (type $t5)))
  (import "wasi_snapshot_preview1" "fd_write" (func $__imported_wasi_snapshot_preview1_fd_write (type $t4)))
  (import "wasi_snapshot_preview1" "proc_exit" (func $__imported_wasi_snapshot_preview1_proc_exit (type $t6)))
  (func $__wasm_call_ctors (type $t7))
  (func $_start (export "_start") (type $t7)
;; ...

to no avail. See https://github.com/WebAssembly/wasi-filesystem/discussions/82.

Native Messaging protocol is initiated from the browser. The host reads input from the browser as stdin, stdout from the host is sent to the client browser. See Native messaging protocol; and MDN's Native Messaging.

Notice how we can use shebang line in a .js file where # is not the beginning of a comment in JavaScript and .py file. I use shebang line in QuickJS, Deno, Bun, Node.js - and Python Native Messaging hosts.

Why can't wastime recognize that we intend to execute the code below the shebang line and not self-execute the same file starting at the beginning of the script - just skip to the next line then execute the script body?

Not sure how to solve this given lack of shebang support and inability to utilize /usr/bin/env with S option.

Asking for help to achieve the requirement.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 21 2023 at 07:15):

guest271314 edited issue #5614:

I downloaded the WASI SDK and wasmtime. I then compiled working C Native Messaging host to WASM, with and without --sysroot option

$ /home/user/wasi-sdk-19.0/bin/clang --sysroot=/home/user/wasi-sdk-19.0/share/wasi-sysroot /home/user/native-messaging-c/nm_c.c -o nm_wasm.wasm

then tried

#!/usr/bin/env -S wasmtime run /path/to/nm_wasm.wasm

and

$ wasmtime compile nm_c.wasm

nm_wasm.sh

#!/usr/bin/env wasmtime nm_c.cwasm

I then used wasm2wat to try to run the module that way, first testing with hello demo

#!/usr/bin/env wasmtime
(module
    ;; Import the required fd_write WASI function which will write the given io vectors to stdout
    ;; The function signature for fd_write is:
    ;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written
    (import "wasi_unstable" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))

    (memory 1)
    (export "memory" (memory 0))

    ;; Write 'hello world\n' to memory at an offset of 8 bytes
    ;; Note the trailing newline which is required for the text to appear
    (data (i32.const 8) "hello world\n")

    (func $main (export "_start")
        ;; Creating a new io vector within linear memory
        (i32.store (i32.const 0) (i32.const 8))  ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string
        (i32.store (i32.const 4) (i32.const 12))  ;; iov.iov_len - The length of the 'hello world\n' string

        (call $fd_write
            (i32.const 1) ;; file_descriptor - 1 for stdout
            (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0
            (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one.
            (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written
        )
        drop ;; Discard the number of bytes written from the top of the stack
    )
)
$ ./demo
Error: failed to run main module `./demo`

Caused by:
    0: if you're trying to run a precompiled module, pass --allow-precompiled
    1: expected `(`
            --> ./demo:1:1
             |
           1 | #!/usr/bin/env wasmtime
             | ^

and after the script exited I searched and located https://github.com/bytecodealliance/wasmtime/issues/3715 then tried

#!/usr/bin/sh

wasmtime run /dev/stdin <<__END__
(module
  (type $t0 (func (param i32 i32 i32) (result i32)))
  (type $t1 (func (param i32 i64 i32) (result i64)))
  (type $t2 (func (param i32) (result i32)))
  (type $t3 (func (param i32 i32) (result i32)))
  (type $t4 (func (param i32 i32 i32 i32) (result i32)))
  (type $t5 (func (param i32 i64 i32 i32) (result i32)))
  (type $t6 (func (param i32)))
  (type $t7 (func))
  (type $t8 (func (result i32)))
  (import "wasi_snapshot_preview1" "fd_close" (func $__imported_wasi_snapshot_preview1_fd_close (type $t2)))
  (import "wasi_snapshot_preview1" "fd_fdstat_get" (func $__imported_wasi_snapshot_preview1_fd_fdstat_get (type $t3)))
  (import "wasi_snapshot_preview1" "fd_read" (func $__imported_wasi_snapshot_preview1_fd_read (type $t4)))
  (import "wasi_snapshot_preview1" "fd_seek" (func $__imported_wasi_snapshot_preview1_fd_seek (type $t5)))
  (import "wasi_snapshot_preview1" "fd_write" (func $__imported_wasi_snapshot_preview1_fd_write (type $t4)))
  (import "wasi_snapshot_preview1" "proc_exit" (func $__imported_wasi_snapshot_preview1_proc_exit (type $t6)))
  (func $__wasm_call_ctors (type $t7))
  (func $_start (export "_start") (type $t7)
;; ...

to no avail. See https://github.com/WebAssembly/wasi-filesystem/discussions/82.

Native Messaging protocol is initiated from the browser. The host reads input from the browser as stdin, stdout from the host is sent to the client browser. See Native messaging protocol; and MDN's Native Messaging.

Notice how we can use shebang line in a .js file where # is not the beginning of a comment in JavaScript and .py file. I use shebang line in QuickJS, Deno, Bun, Node.js - and Python Native Messaging hosts.

Why can't wastime recognize that we intend to execute the code below the shebang line and not self-execute the same file starting at the beginning of the script - just skip to the next line then execute the script body?

Not sure how to solve this given lack of shebang support and inability to utilize /usr/bin/env with S option.

Asking for help to achieve the requirement.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 21 2023 at 07:16):

guest271314 edited issue #5614:

I downloaded the WASI SDK and wasmtime. I then compiled working C Native Messaging host https://github.com/guest271314/native-messaging-c to WASM, with and without --sysroot option

$ /home/user/wasi-sdk-19.0/bin/clang --sysroot=/home/user/wasi-sdk-19.0/share/wasi-sysroot /home/user/native-messaging-c/nm_c.c -o nm_wasm.wasm

then tried

#!/usr/bin/env -S wasmtime run /path/to/nm_wasm.wasm

and

$ wasmtime compile nm_c.wasm

nm_wasm.sh

#!/usr/bin/env wasmtime nm_c.cwasm

I then used wasm2wat to try to run the module that way, first testing with hello demo

#!/usr/bin/env wasmtime
(module
    ;; Import the required fd_write WASI function which will write the given io vectors to stdout
    ;; The function signature for fd_write is:
    ;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written
    (import "wasi_unstable" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))

    (memory 1)
    (export "memory" (memory 0))

    ;; Write 'hello world\n' to memory at an offset of 8 bytes
    ;; Note the trailing newline which is required for the text to appear
    (data (i32.const 8) "hello world\n")

    (func $main (export "_start")
        ;; Creating a new io vector within linear memory
        (i32.store (i32.const 0) (i32.const 8))  ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string
        (i32.store (i32.const 4) (i32.const 12))  ;; iov.iov_len - The length of the 'hello world\n' string

        (call $fd_write
            (i32.const 1) ;; file_descriptor - 1 for stdout
            (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0
            (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one.
            (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written
        )
        drop ;; Discard the number of bytes written from the top of the stack
    )
)
$ ./demo
Error: failed to run main module `./demo`

Caused by:
    0: if you're trying to run a precompiled module, pass --allow-precompiled
    1: expected `(`
            --> ./demo:1:1
             |
           1 | #!/usr/bin/env wasmtime
             | ^

and after the script exited I searched and located https://github.com/bytecodealliance/wasmtime/issues/3715 then tried

#!/usr/bin/sh

wasmtime run /dev/stdin <<__END__
(module
  (type $t0 (func (param i32 i32 i32) (result i32)))
  (type $t1 (func (param i32 i64 i32) (result i64)))
  (type $t2 (func (param i32) (result i32)))
  (type $t3 (func (param i32 i32) (result i32)))
  (type $t4 (func (param i32 i32 i32 i32) (result i32)))
  (type $t5 (func (param i32 i64 i32 i32) (result i32)))
  (type $t6 (func (param i32)))
  (type $t7 (func))
  (type $t8 (func (result i32)))
  (import "wasi_snapshot_preview1" "fd_close" (func $__imported_wasi_snapshot_preview1_fd_close (type $t2)))
  (import "wasi_snapshot_preview1" "fd_fdstat_get" (func $__imported_wasi_snapshot_preview1_fd_fdstat_get (type $t3)))
  (import "wasi_snapshot_preview1" "fd_read" (func $__imported_wasi_snapshot_preview1_fd_read (type $t4)))
  (import "wasi_snapshot_preview1" "fd_seek" (func $__imported_wasi_snapshot_preview1_fd_seek (type $t5)))
  (import "wasi_snapshot_preview1" "fd_write" (func $__imported_wasi_snapshot_preview1_fd_write (type $t4)))
  (import "wasi_snapshot_preview1" "proc_exit" (func $__imported_wasi_snapshot_preview1_proc_exit (type $t6)))
  (func $__wasm_call_ctors (type $t7))
  (func $_start (export "_start") (type $t7)
;; ...

to no avail. See https://github.com/WebAssembly/wasi-filesystem/discussions/82.

Native Messaging protocol is initiated from the browser. The host reads input from the browser as stdin, stdout from the host is sent to the client browser. See Native messaging protocol; and MDN's Native Messaging.

Notice how we can use shebang line in a .js file where # is not the beginning of a comment in JavaScript and .py file. I use shebang line in QuickJS, Deno, Bun, Node.js - and Python Native Messaging hosts.

Why can't wastime recognize that we intend to execute the code below the shebang line and not self-execute the same file starting at the beginning of the script - just skip to the next line then execute the script body?

Not sure how to solve this given lack of shebang support and inability to utilize /usr/bin/env with S option.

Asking for help to achieve the requirement.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 21 2023 at 07:20):

guest271314 edited issue #5614:

I downloaded the WASI SDK and wasmtime. I then compiled working C Native Messaging host https://github.com/guest271314/native-messaging-c to WASM, with and without --sysroot option

$ /home/user/wasi-sdk-19.0/bin/clang --sysroot=/home/user/wasi-sdk-19.0/share/wasi-sysroot /home/user/native-messaging-c/nm_c.c -o nm_wasm.wasm

then tried

#!/usr/bin/env -S wasmtime run /path/to/nm_wasm.wasm

and

$ wasmtime compile nm_c.wasm

nm_wasm.sh

#!/usr/bin/env wasmtime nm_c.cwasm

I then used wasm2wat to try to run the module that way, first testing with hello demo

#!/usr/bin/env wasmtime
(module
    ;; Import the required fd_write WASI function which will write the given io vectors to stdout
    ;; The function signature for fd_write is:
    ;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written
    (import "wasi_unstable" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))

    (memory 1)
    (export "memory" (memory 0))

    ;; Write 'hello world\n' to memory at an offset of 8 bytes
    ;; Note the trailing newline which is required for the text to appear
    (data (i32.const 8) "hello world\n")

    (func $main (export "_start")
        ;; Creating a new io vector within linear memory
        (i32.store (i32.const 0) (i32.const 8))  ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string
        (i32.store (i32.const 4) (i32.const 12))  ;; iov.iov_len - The length of the 'hello world\n' string

        (call $fd_write
            (i32.const 1) ;; file_descriptor - 1 for stdout
            (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0
            (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one.
            (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written
        )
        drop ;; Discard the number of bytes written from the top of the stack
    )
)
$ ./demo
Error: failed to run main module `./demo`

Caused by:
    0: if you're trying to run a precompiled module, pass --allow-precompiled
    1: expected `(`
            --> ./demo:1:1
             |
           1 | #!/usr/bin/env wasmtime
             | ^

and after the script exited I searched and located https://github.com/bytecodealliance/wasmtime/issues/3715 then tried

#!/usr/bin/sh

wasmtime run /dev/stdin <<__END__
(module
  (type $t0 (func (param i32 i32 i32) (result i32)))
  (type $t1 (func (param i32 i64 i32) (result i64)))
  (type $t2 (func (param i32) (result i32)))
  (type $t3 (func (param i32 i32) (result i32)))
  (type $t4 (func (param i32 i32 i32 i32) (result i32)))
  (type $t5 (func (param i32 i64 i32 i32) (result i32)))
  (type $t6 (func (param i32)))
  (type $t7 (func))
  (type $t8 (func (result i32)))
  (import "wasi_snapshot_preview1" "fd_close" (func $__imported_wasi_snapshot_preview1_fd_close (type $t2)))
  (import "wasi_snapshot_preview1" "fd_fdstat_get" (func $__imported_wasi_snapshot_preview1_fd_fdstat_get (type $t3)))
  (import "wasi_snapshot_preview1" "fd_read" (func $__imported_wasi_snapshot_preview1_fd_read (type $t4)))
  (import "wasi_snapshot_preview1" "fd_seek" (func $__imported_wasi_snapshot_preview1_fd_seek (type $t5)))
  (import "wasi_snapshot_preview1" "fd_write" (func $__imported_wasi_snapshot_preview1_fd_write (type $t4)))
  (import "wasi_snapshot_preview1" "proc_exit" (func $__imported_wasi_snapshot_preview1_proc_exit (type $t6)))
  (func $__wasm_call_ctors (type $t7))
  (func $_start (export "_start") (type $t7)
;; ...

to no avail. See https://github.com/WebAssembly/wasi-filesystem/discussions/82.

Native Messaging protocol is initiated from the browser. The host reads input from the browser as stdin, stdout from the host is sent to the client browser. See Native messaging protocol; and MDN's Native Messaging.

Notice how we can use shebang line in a .js file where # is not the beginning of a comment in JavaScript - and Python .py file. I use shebang line in QuickJS, Deno, Bun, Node.js - and Python Native Messaging hosts.

Why can't wasmtime have the option to recognize that we intend to execute the code below the shebang line and not self-execute the same file starting at the beginning of the script - just skip to the next line then execute the script body?

Not sure how to solve this given lack of shebang support and inability to utilize /usr/bin/env with S option.

Asking for help to achieve the requirement.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 21 2023 at 07:21):

guest271314 edited issue #5614:

I downloaded the WASI SDK and wasmtime. I then compiled working C Native Messaging host https://github.com/guest271314/native-messaging-c to WASM, with and without --sysroot option

$ /home/user/wasi-sdk-19.0/bin/clang --sysroot=/home/user/wasi-sdk-19.0/share/wasi-sysroot /home/user/native-messaging-c/nm_c.c -o nm_wasm.wasm

then tried

#!/usr/bin/env -S wasmtime run /path/to/nm_wasm.wasm

and

$ wasmtime compile nm_c.wasm

nm_wasm.sh

#!/usr/bin/env wasmtime nm_c.cwasm

I then used wasm2wat to try to run the module that way, first testing with hello demo

#!/usr/bin/env wasmtime
(module
    ;; Import the required fd_write WASI function which will write the given io vectors to stdout
    ;; The function signature for fd_write is:
    ;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written
    (import "wasi_unstable" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))

    (memory 1)
    (export "memory" (memory 0))

    ;; Write 'hello world\n' to memory at an offset of 8 bytes
    ;; Note the trailing newline which is required for the text to appear
    (data (i32.const 8) "hello world\n")

    (func $main (export "_start")
        ;; Creating a new io vector within linear memory
        (i32.store (i32.const 0) (i32.const 8))  ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string
        (i32.store (i32.const 4) (i32.const 12))  ;; iov.iov_len - The length of the 'hello world\n' string

        (call $fd_write
            (i32.const 1) ;; file_descriptor - 1 for stdout
            (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0
            (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one.
            (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written
        )
        drop ;; Discard the number of bytes written from the top of the stack
    )
)
$ ./demo
Error: failed to run main module `./demo`

Caused by:
    0: if you're trying to run a precompiled module, pass --allow-precompiled
    1: expected `(`
            --> ./demo:1:1
             |
           1 | #!/usr/bin/env wasmtime
             | ^

and after the script exited I searched and located https://github.com/bytecodealliance/wasmtime/issues/3715 then tried

#!/usr/bin/sh

wasmtime run /dev/stdin <<__END__
(module
  (type $t0 (func (param i32 i32 i32) (result i32)))
  (type $t1 (func (param i32 i64 i32) (result i64)))
  (type $t2 (func (param i32) (result i32)))
  (type $t3 (func (param i32 i32) (result i32)))
  (type $t4 (func (param i32 i32 i32 i32) (result i32)))
  (type $t5 (func (param i32 i64 i32 i32) (result i32)))
  (type $t6 (func (param i32)))
  (type $t7 (func))
  (type $t8 (func (result i32)))
  (import "wasi_snapshot_preview1" "fd_close" (func $__imported_wasi_snapshot_preview1_fd_close (type $t2)))
  (import "wasi_snapshot_preview1" "fd_fdstat_get" (func $__imported_wasi_snapshot_preview1_fd_fdstat_get (type $t3)))
  (import "wasi_snapshot_preview1" "fd_read" (func $__imported_wasi_snapshot_preview1_fd_read (type $t4)))
  (import "wasi_snapshot_preview1" "fd_seek" (func $__imported_wasi_snapshot_preview1_fd_seek (type $t5)))
  (import "wasi_snapshot_preview1" "fd_write" (func $__imported_wasi_snapshot_preview1_fd_write (type $t4)))
  (import "wasi_snapshot_preview1" "proc_exit" (func $__imported_wasi_snapshot_preview1_proc_exit (type $t6)))
  (func $__wasm_call_ctors (type $t7))
  (func $_start (export "_start") (type $t7)
;; ...

to no avail. See https://github.com/WebAssembly/wasi-filesystem/discussions/82.

Native Messaging protocol is initiated from the browser. The host reads input from the browser as stdin, stdout from the host is sent to the client browser. See Native messaging protocol; and MDN's Native Messaging.

Notice how we can use shebang line in a .js file where # is not the beginning of a comment in JavaScript - and Python .py file. I use shebang line in QuickJS, Deno, Bun, Node.js - and Python Native Messaging hosts.

Why can't wasmtime have the option to recognize that we intend to execute the code below the shebang line and not self-execute the same file starting at the beginning of the script - just skip to the next line then execute the script body?

Not sure how to solve this given lack of shebang support and inability to utilize /usr/bin/env with S option.

Asking for help to achieve the requirement. Thanks.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 21 2023 at 07:22):

guest271314 edited issue #5614:

I downloaded the WASI SDK and wasmtime. I then compiled working C Native Messaging host https://github.com/guest271314/native-messaging-c to WASM, with and without --sysroot option

$ /home/user/wasi-sdk-19.0/bin/clang --sysroot=/home/user/wasi-sdk-19.0/share/wasi-sysroot /home/user/native-messaging-c/nm_c.c -o nm_wasm.wasm

then tried

#!/usr/bin/env -S wasmtime run /path/to/nm_wasm.wasm

and

$ wasmtime compile nm_wasm.wasm

nm_wasm.sh

#!/usr/bin/env wasmtime nm_wasm.cwasm

I then used wasm2wat to try to run the module that way, first testing with hello demo

#!/usr/bin/env wasmtime
(module
    ;; Import the required fd_write WASI function which will write the given io vectors to stdout
    ;; The function signature for fd_write is:
    ;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written
    (import "wasi_unstable" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))

    (memory 1)
    (export "memory" (memory 0))

    ;; Write 'hello world\n' to memory at an offset of 8 bytes
    ;; Note the trailing newline which is required for the text to appear
    (data (i32.const 8) "hello world\n")

    (func $main (export "_start")
        ;; Creating a new io vector within linear memory
        (i32.store (i32.const 0) (i32.const 8))  ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string
        (i32.store (i32.const 4) (i32.const 12))  ;; iov.iov_len - The length of the 'hello world\n' string

        (call $fd_write
            (i32.const 1) ;; file_descriptor - 1 for stdout
            (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0
            (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one.
            (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written
        )
        drop ;; Discard the number of bytes written from the top of the stack
    )
)
$ ./demo
Error: failed to run main module `./demo`

Caused by:
    0: if you're trying to run a precompiled module, pass --allow-precompiled
    1: expected `(`
            --> ./demo:1:1
             |
           1 | #!/usr/bin/env wasmtime
             | ^

and after the script exited I searched and located https://github.com/bytecodealliance/wasmtime/issues/3715 then tried

#!/usr/bin/sh

wasmtime run /dev/stdin <<__END__
(module
  (type $t0 (func (param i32 i32 i32) (result i32)))
  (type $t1 (func (param i32 i64 i32) (result i64)))
  (type $t2 (func (param i32) (result i32)))
  (type $t3 (func (param i32 i32) (result i32)))
  (type $t4 (func (param i32 i32 i32 i32) (result i32)))
  (type $t5 (func (param i32 i64 i32 i32) (result i32)))
  (type $t6 (func (param i32)))
  (type $t7 (func))
  (type $t8 (func (result i32)))
  (import "wasi_snapshot_preview1" "fd_close" (func $__imported_wasi_snapshot_preview1_fd_close (type $t2)))
  (import "wasi_snapshot_preview1" "fd_fdstat_get" (func $__imported_wasi_snapshot_preview1_fd_fdstat_get (type $t3)))
  (import "wasi_snapshot_preview1" "fd_read" (func $__imported_wasi_snapshot_preview1_fd_read (type $t4)))
  (import "wasi_snapshot_preview1" "fd_seek" (func $__imported_wasi_snapshot_preview1_fd_seek (type $t5)))
  (import "wasi_snapshot_preview1" "fd_write" (func $__imported_wasi_snapshot_preview1_fd_write (type $t4)))
  (import "wasi_snapshot_preview1" "proc_exit" (func $__imported_wasi_snapshot_preview1_proc_exit (type $t6)))
  (func $__wasm_call_ctors (type $t7))
  (func $_start (export "_start") (type $t7)
;; ...

to no avail. See https://github.com/WebAssembly/wasi-filesystem/discussions/82.

Native Messaging protocol is initiated from the browser. The host reads input from the browser as stdin, stdout from the host is sent to the client browser. See Native messaging protocol; and MDN's Native Messaging.

Notice how we can use shebang line in a .js file where # is not the beginning of a comment in JavaScript - and Python .py file. I use shebang line in QuickJS, Deno, Bun, Node.js - and Python Native Messaging hosts.

Why can't wasmtime have the option to recognize that we intend to execute the code below the shebang line and not self-execute the same file starting at the beginning of the script - just skip to the next line then execute the script body?

Not sure how to solve this given lack of shebang support and inability to utilize /usr/bin/env with S option.

Asking for help to achieve the requirement. Thanks.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 21 2023 at 07:29):

guest271314 edited issue #5614:

I downloaded the WASI SDK and wasmtime. I then compiled working C Native Messaging host https://github.com/guest271314/native-messaging-c to WASM, with and without --sysroot option

$ /home/user/wasi-sdk-19.0/bin/clang --sysroot=/home/user/wasi-sdk-19.0/share/wasi-sysroot /home/user/native-messaging-c/nm_c.c -o nm_wasm.wasm

then tried

#!/usr/bin/env -S wasmtime run /path/to/nm_wasm.wasm

and

$ wasmtime compile nm_wasm.wasm

nm_wasm.sh

#!/usr/bin/env -S wasmtime nm_wasm.cwasm

I then used wasm2wat to try to run the module that way, first testing with hello demo

#!/usr/bin/env wasmtime
(module
    ;; Import the required fd_write WASI function which will write the given io vectors to stdout
    ;; The function signature for fd_write is:
    ;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written
    (import "wasi_unstable" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))

    (memory 1)
    (export "memory" (memory 0))

    ;; Write 'hello world\n' to memory at an offset of 8 bytes
    ;; Note the trailing newline which is required for the text to appear
    (data (i32.const 8) "hello world\n")

    (func $main (export "_start")
        ;; Creating a new io vector within linear memory
        (i32.store (i32.const 0) (i32.const 8))  ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string
        (i32.store (i32.const 4) (i32.const 12))  ;; iov.iov_len - The length of the 'hello world\n' string

        (call $fd_write
            (i32.const 1) ;; file_descriptor - 1 for stdout
            (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0
            (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one.
            (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written
        )
        drop ;; Discard the number of bytes written from the top of the stack
    )
)
$ ./demo
Error: failed to run main module `./demo`

Caused by:
    0: if you're trying to run a precompiled module, pass --allow-precompiled
    1: expected `(`
            --> ./demo:1:1
             |
           1 | #!/usr/bin/env wasmtime
             | ^

and after the script exited I searched and located https://github.com/bytecodealliance/wasmtime/issues/3715 then tried

#!/usr/bin/sh

wasmtime run /dev/stdin <<__END__
(module
  (type $t0 (func (param i32 i32 i32) (result i32)))
  (type $t1 (func (param i32 i64 i32) (result i64)))
  (type $t2 (func (param i32) (result i32)))
  (type $t3 (func (param i32 i32) (result i32)))
  (type $t4 (func (param i32 i32 i32 i32) (result i32)))
  (type $t5 (func (param i32 i64 i32 i32) (result i32)))
  (type $t6 (func (param i32)))
  (type $t7 (func))
  (type $t8 (func (result i32)))
  (import "wasi_snapshot_preview1" "fd_close" (func $__imported_wasi_snapshot_preview1_fd_close (type $t2)))
  (import "wasi_snapshot_preview1" "fd_fdstat_get" (func $__imported_wasi_snapshot_preview1_fd_fdstat_get (type $t3)))
  (import "wasi_snapshot_preview1" "fd_read" (func $__imported_wasi_snapshot_preview1_fd_read (type $t4)))
  (import "wasi_snapshot_preview1" "fd_seek" (func $__imported_wasi_snapshot_preview1_fd_seek (type $t5)))
  (import "wasi_snapshot_preview1" "fd_write" (func $__imported_wasi_snapshot_preview1_fd_write (type $t4)))
  (import "wasi_snapshot_preview1" "proc_exit" (func $__imported_wasi_snapshot_preview1_proc_exit (type $t6)))
  (func $__wasm_call_ctors (type $t7))
  (func $_start (export "_start") (type $t7)
;; ...

to no avail. See https://github.com/WebAssembly/wasi-filesystem/discussions/82.

Native Messaging protocol is initiated from the browser. The host reads input from the browser as stdin, stdout from the host is sent to the client browser. See Native messaging protocol; and MDN's Native Messaging.

Notice how we can use shebang line in a .js file where # is not the beginning of a comment in JavaScript - and Python .py file. I use shebang line in QuickJS, Deno, Bun, Node.js - and Python Native Messaging hosts.

Why can't wasmtime have the option to recognize that we intend to execute the code below the shebang line and not self-execute the same file starting at the beginning of the script - just skip to the next line then execute the script body?

Not sure how to solve this given lack of shebang support and inability to utilize /usr/bin/env with S option.

Asking for help to achieve the requirement. Thanks.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 21 2023 at 07:29):

guest271314 edited issue #5614:

I downloaded the WASI SDK and wasmtime. I then compiled working C Native Messaging host https://github.com/guest271314/native-messaging-c to WASM, with and without --sysroot option

$ /home/user/wasi-sdk-19.0/bin/clang --sysroot=/home/user/wasi-sdk-19.0/share/wasi-sysroot /home/user/native-messaging-c/nm_c.c -o nm_wasm.wasm

then tried

#!/usr/bin/env -S wasmtime run /path/to/nm_wasm.wasm

and

$ wasmtime compile nm_wasm.wasm

nm_wasm.sh

#!/usr/bin/env -S wasmtime nm_wasm.cwasm

I then used wasm2wat to try to run the module that way, first testing with hello demo

#!/usr/bin/env wasmtime
(module
    ;; Import the required fd_write WASI function which will write the given io vectors to stdout
    ;; The function signature for fd_write is:
    ;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written
    (import "wasi_unstable" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))

    (memory 1)
    (export "memory" (memory 0))

    ;; Write 'hello world\n' to memory at an offset of 8 bytes
    ;; Note the trailing newline which is required for the text to appear
    (data (i32.const 8) "hello world\n")

    (func $main (export "_start")
        ;; Creating a new io vector within linear memory
        (i32.store (i32.const 0) (i32.const 8))  ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string
        (i32.store (i32.const 4) (i32.const 12))  ;; iov.iov_len - The length of the 'hello world\n' string

        (call $fd_write
            (i32.const 1) ;; file_descriptor - 1 for stdout
            (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0
            (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one.
            (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written
        )
        drop ;; Discard the number of bytes written from the top of the stack
    )
)
$ ./demo
Error: failed to run main module `./demo`

Caused by:
    0: if you're trying to run a precompiled module, pass --allow-precompiled
    1: expected `(`
            --> ./demo:1:1
             |
           1 | #!/usr/bin/env wasmtime
             | ^

and after the script exited I searched and located https://github.com/bytecodealliance/wasmtime/issues/3715 then tried

#!/usr/bin/sh

wasmtime run /dev/stdin <<__END__
(module
  (type $t0 (func (param i32 i32 i32) (result i32)))
  (type $t1 (func (param i32 i64 i32) (result i64)))
  (type $t2 (func (param i32) (result i32)))
  (type $t3 (func (param i32 i32) (result i32)))
  (type $t4 (func (param i32 i32 i32 i32) (result i32)))
  (type $t5 (func (param i32 i64 i32 i32) (result i32)))
  (type $t6 (func (param i32)))
  (type $t7 (func))
  (type $t8 (func (result i32)))
  (import "wasi_snapshot_preview1" "fd_close" (func $__imported_wasi_snapshot_preview1_fd_close (type $t2)))
  (import "wasi_snapshot_preview1" "fd_fdstat_get" (func $__imported_wasi_snapshot_preview1_fd_fdstat_get (type $t3)))
  (import "wasi_snapshot_preview1" "fd_read" (func $__imported_wasi_snapshot_preview1_fd_read (type $t4)))
  (import "wasi_snapshot_preview1" "fd_seek" (func $__imported_wasi_snapshot_preview1_fd_seek (type $t5)))
  (import "wasi_snapshot_preview1" "fd_write" (func $__imported_wasi_snapshot_preview1_fd_write (type $t4)))
  (import "wasi_snapshot_preview1" "proc_exit" (func $__imported_wasi_snapshot_preview1_proc_exit (type $t6)))
  (func $__wasm_call_ctors (type $t7))
  (func $_start (export "_start") (type $t7)
;; ...

to no avail. See https://github.com/WebAssembly/wasi-filesystem/discussions/82.

Native Messaging protocol is initiated from the browser. The host reads input from the browser as stdin, stdout from the host is sent to the client browser. See Native messaging protocol; and MDN's Native Messaging.

Notice how we can use shebang line in a .js file where # is not the beginning of a comment in JavaScript - and Python .py file. I use shebang line in QuickJS, Deno, Bun, Node.js - and Python Native Messaging hosts.

Why can't wasmtime have the option to recognize that we intend to execute the code below the shebang line and not self-execute the same file starting at the beginning of the script - just skip to the next line then execute the script body?

Not sure how to solve this given lack of shebang support and inability to utilize /usr/bin/env with S option.

Asking for help to achieve the requirement. Thanks.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 21 2023 at 21:08):

guest271314 commented on issue #5614:

@tschneidereit Should I go ahead and close this issue?

view this post on Zulip Wasmtime GitHub notifications bot (Jan 21 2023 at 22:16):

tschneidereit commented on issue #5614:

@guest271314 if you think that shebang support is the only way to achieve your desired outcome here, then that might make sense. I don't have the bandwidth to look into the issue enough to know whether that's the case, I'm afraid

view this post on Zulip Wasmtime GitHub notifications bot (Jan 21 2023 at 22:29):

guest271314 commented on issue #5614:

W3C banned me so I can't propose anything given your current criteria. I am rather surprised that wasmtime doesn't support shebang. I kept searching after you locked the linked issue and found several issues concerning this limitation. I have went through all the hoops to download WASI SDK and wasmtime and am not limited. I think it makes sense for you folks to figure out a way to solve this internally.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 21 2023 at 22:32):

bjorn3 commented on issue #5614:

For reference I'm trying to help them over at https://github.com/guest271314/native-messaging-c/pull/2. Maybe it would make sense to close this issue and if the debugging over at there shows a concrete bug in wasmtime or a fetaure request that is reasonably likely to be accepted, a new issue could be opened focused on just this?

view this post on Zulip Wasmtime GitHub notifications bot (Jan 21 2023 at 22:43):

guest271314 commented on issue #5614:

@bjorn3 Sure. I just anticipated 19MB wasmtime and the breadth of WASI SDK to handle these cases.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 21 2023 at 22:43):

guest271314 closed issue #5614:

I downloaded the WASI SDK and wasmtime. I then compiled working C Native Messaging host https://github.com/guest271314/native-messaging-c to WASM, with and without --sysroot option

$ /home/user/wasi-sdk-19.0/bin/clang --sysroot=/home/user/wasi-sdk-19.0/share/wasi-sysroot /home/user/native-messaging-c/nm_c.c -o nm_wasm.wasm

then tried

#!/usr/bin/env -S wasmtime run /path/to/nm_wasm.wasm

and

$ wasmtime compile nm_wasm.wasm

nm_wasm.sh

#!/usr/bin/env -S wasmtime nm_wasm.cwasm

I then used wasm2wat to try to run the module that way, first testing with hello demo

#!/usr/bin/env wasmtime
(module
    ;; Import the required fd_write WASI function which will write the given io vectors to stdout
    ;; The function signature for fd_write is:
    ;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written
    (import "wasi_unstable" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))

    (memory 1)
    (export "memory" (memory 0))

    ;; Write 'hello world\n' to memory at an offset of 8 bytes
    ;; Note the trailing newline which is required for the text to appear
    (data (i32.const 8) "hello world\n")

    (func $main (export "_start")
        ;; Creating a new io vector within linear memory
        (i32.store (i32.const 0) (i32.const 8))  ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string
        (i32.store (i32.const 4) (i32.const 12))  ;; iov.iov_len - The length of the 'hello world\n' string

        (call $fd_write
            (i32.const 1) ;; file_descriptor - 1 for stdout
            (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0
            (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one.
            (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written
        )
        drop ;; Discard the number of bytes written from the top of the stack
    )
)
$ ./demo
Error: failed to run main module `./demo`

Caused by:
    0: if you're trying to run a precompiled module, pass --allow-precompiled
    1: expected `(`
            --> ./demo:1:1
             |
           1 | #!/usr/bin/env wasmtime
             | ^

and after the script exited I searched and located https://github.com/bytecodealliance/wasmtime/issues/3715 then tried

#!/usr/bin/sh

wasmtime run /dev/stdin <<__END__
(module
  (type $t0 (func (param i32 i32 i32) (result i32)))
  (type $t1 (func (param i32 i64 i32) (result i64)))
  (type $t2 (func (param i32) (result i32)))
  (type $t3 (func (param i32 i32) (result i32)))
  (type $t4 (func (param i32 i32 i32 i32) (result i32)))
  (type $t5 (func (param i32 i64 i32 i32) (result i32)))
  (type $t6 (func (param i32)))
  (type $t7 (func))
  (type $t8 (func (result i32)))
  (import "wasi_snapshot_preview1" "fd_close" (func $__imported_wasi_snapshot_preview1_fd_close (type $t2)))
  (import "wasi_snapshot_preview1" "fd_fdstat_get" (func $__imported_wasi_snapshot_preview1_fd_fdstat_get (type $t3)))
  (import "wasi_snapshot_preview1" "fd_read" (func $__imported_wasi_snapshot_preview1_fd_read (type $t4)))
  (import "wasi_snapshot_preview1" "fd_seek" (func $__imported_wasi_snapshot_preview1_fd_seek (type $t5)))
  (import "wasi_snapshot_preview1" "fd_write" (func $__imported_wasi_snapshot_preview1_fd_write (type $t4)))
  (import "wasi_snapshot_preview1" "proc_exit" (func $__imported_wasi_snapshot_preview1_proc_exit (type $t6)))
  (func $__wasm_call_ctors (type $t7))
  (func $_start (export "_start") (type $t7)
;; ...

to no avail. See https://github.com/WebAssembly/wasi-filesystem/discussions/82.

Native Messaging protocol is initiated from the browser. The host reads input from the browser as stdin, stdout from the host is sent to the client browser. See Native messaging protocol; and MDN's Native Messaging.

Notice how we can use shebang line in a .js file where # is not the beginning of a comment in JavaScript - and Python .py file. I use shebang line in QuickJS, Deno, Bun, Node.js - and Python Native Messaging hosts.

Why can't wasmtime have the option to recognize that we intend to execute the code below the shebang line and not self-execute the same file starting at the beginning of the script - just skip to the next line then execute the script body?

Not sure how to solve this given lack of shebang support and inability to utilize /usr/bin/env with S option.

Asking for help to achieve the requirement. Thanks.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 22 2023 at 01:12):

guest271314 commented on issue #5614:

@tschneidereit @bjorn3 @JoeStrout

Solved.

I think the install script

curl https://wasmtime.dev/install.sh -sSf | bash

sets wasmtime in PATH only for local terminal, not system wide. So when we use something like

#!/usr/bin/env -S wasmtime ...

env is not finding wasmtime, e.g.,

$ sudo su
# wasmtime --help
wasmtime: command not found

We need to use the full path to the wasmtime executable, e.g.,

#!/usr/bin/env bash
/home/user/.wasmtime/bin/wasmtime run nm_c.wasm
#!/usr/bin/env -S /home/user/.wasmtime/bin/wasmtime run nm_c.wasm

or more directly where we write the executable to the directory where we intent to use it

#!/wasmtime/bin/wasmtime run nm_c.wasm

For completeness I also tested using WAT in the shell script using "approach 1" from https://www.reddit.com/r/bash/comments/10i09se/comment/j5blsrw/, again, where I for this test just copied wasmtime from ~/.wasmtime/bin to the directory I am using the executable in

#!/bin/bash

script='
(module
;; ...
)
'
./wasmtime <(printf '%s' "$script")

view this post on Zulip Wasmtime GitHub notifications bot (Jan 22 2023 at 01:16):

guest271314 edited a comment on issue #5614:

@tschneidereit @bjorn3 @JoeStrout

Solved.

I think the install script

curl https://wasmtime.dev/install.sh -sSf | bash

sets wasmtime in PATH only for local terminal, not system wide. So when we use something like

#!/usr/bin/env -S wasmtime ...

env is not finding wasmtime, e.g.,

$ sudo su
# wasmtime --help
wasmtime: command not found

We need to use the full path to the wasmtime executable, e.g.,

#!/usr/bin/env bash
/home/user/.wasmtime/bin/wasmtime run nm_c.wasm

or more directly where we write the executable to the directory where we intent to use it

#!/usr/bin/env -S ./wasmtime run nm_c.wasm

For completeness I also tested using WAT in the shell script using "approach 1" from https://www.reddit.com/r/bash/comments/10i09se/comment/j5blsrw/, again, where I for this test just copied wasmtime from ~/.wasmtime/bin to the directory I am using the executable in

#!/bin/bash

script='
(module
;; ...
)
'
./wasmtime <(printf '%s' "$script")

view this post on Zulip Wasmtime GitHub notifications bot (Jan 22 2023 at 03:15):

guest271314 commented on issue #5614:

Note, the Bash process substitution approach does not kill wasmtime whrn the Native Messaging host exits.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 22 2023 at 14:08):

guest271314 edited a comment on issue #5614:

@tschneidereit @bjorn3 @JoeStrout

Solved.

I think the install script

curl https://wasmtime.dev/install.sh -sSf | bash

sets wasmtime in PATH only for local terminal, not system wide. So when we use something like

#!/usr/bin/env -S wasmtime ...

env is not finding wasmtime, e.g.,

$ sudo su
# wasmtime --help
wasmtime: command not found

We need to use the full path to the wasmtime executable, e.g.,

#!/usr/bin/env bash
/home/user/.wasmtime/bin/wasmtime run nm_c.wasm

or more directly where we write the executable to the directory where we intent to use it

#!wasmtime nm_c.wasm

or

#!wasmtime nm_c_wasm.wat

For completeness I also tested using WAT in the shell script using "approach 1" from https://www.reddit.com/r/bash/comments/10i09se/comment/j5blsrw/, again, where I for this test just copied wasmtime from ~/.wasmtime/bin to the directory I am using the executable in

#!/bin/bash

script='
(module
;; ...
)
'
./wasmtime <(printf '%s' "$script")

view this post on Zulip Wasmtime GitHub notifications bot (Jan 22 2023 at 23:19):

guest271314 commented on issue #5614:

The workaround so far for running wasmtime with WAT in the same file is using a separate script to terminate wasmtime executable when the Native Messaging host exits

nm_c_wat.sh

#!/bin/bash
# https://www.reddit.com/r/bash/comments/10i09se/comment/j5blsrw/

script='
(module
;; ...
)
'

./kill_wasmtime.sh &
./wasmtime <(printf '%s' "$script")

kill_wasmtime.sh

#!/bin/bash
while pgrep -f nm_c_wat.sh > /dev/null
do
  sleep 1
done
killall -9 wasmtime
exit 0

view this post on Zulip Wasmtime GitHub notifications bot (Jan 23 2023 at 01:01):

guest271314 commented on issue #5614:

@tschneidereit @JoeStrout See https://github.com/WebAssembly/spec/issues/1581


Last updated: Dec 23 2024 at 12:05 UTC