WebAssembly / spec

WebAssembly specification, reference interpreter, and test suite.
https://webassembly.github.io/spec/
Other
3.13k stars 445 forks source link

Add grammar to execute raw WAT in Text Format #1581

Open guest271314 opened 1 year ago

guest271314 commented 1 year ago

Currently the Text Format specification does not define a means to call a WASM executable from within the WAT file, see https://github.com/bytecodealliance/wasmtime/issues/3715.

Reading the specification(s) https://github.com/WebAssembly/spec/blob/4c249c5a575e2b0e252e747af261bbb82f448dd4/interpreter/README.md, https://webassembly.github.io/spec/core/text/conventions.html#grammar this sounds reasonable

file.wat

;;#!wasmtime
(module
;; ...
)

Prospective specification text:

Comment ;; at position 0 and 1 in WAT followed by #! 1 invokes the following until end of line as WAT executable, optionally followed by arguments to WAT executable where lines 1-N are interpreted as WAT for WASM executable to execute.

The above change should not affect existing WAT usage, where it is highly unlikely WAT files in existence contain that precise sequence of characters at positions 0-3 in the file.

I found this limitation when using wasmtime to execute WASM as a Native Messaging host.

This is the current workaround I'm using to use WAT in a Bash sheel script, call wasmtime (which does not exit when the parent exits) using WAT Text Format in the same file, then terminate wasmtime in a separate script 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

Pros, cons, discussion. I cannot champion this because W3C banned me. So either one of you champion this or it doesn't happen.

1. The #! magic, details about the shebang/hash-bang mechanism on various Unix flavours

bjorn3 commented 1 year ago

I would rather specify the following:

If the WAT file starts with a # followed by an ! everything from the start of the file until the first newline shall be interpreted as comment.

;;#! is not something that any OS will interpret. It can only be interpreted once a program that supports reading WAT files has already started at which point I don't think there should be a possibility to run another program instead. UNIX systems only interpret #! at the start of the file without anything before it to determine which program to run.