ztcl::wasm run
Runs a WASI command and gives its exit code.
ztcl::wasm run MODULE|FILENAME ?-args LIST? ?-env DICT? ?-dirs DICT? ?-stdin TEXT? ?-imports DICT? ?-limit UNITS?
Description
Instantiates a module handle or the module of a file with the imports of WASI preview 1, calls _start, and gives the exit code: the value of proc_exit, or 0 when _start returns. A plain wasm32-wasi program of C (zig cc -target wasm32-wasi), Zig, Rust, or TinyGo (-target=wasip1) runs.
Each import goes through the capabilities of the interpreter. The standard output and error go to puts -nonewline stdout and stderr, so to the channel or to the callback of the embedder. clock_time_get needs the capability clock, and random_get needs random; without it a call gives ENOTCAPABLE (76). A clock subscription of poll_oneoff is due at once, sockets and links give ENOTSUP, and each call of a WASI function uses one compute unit.
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
| MODULE|FILENAME | handle or path | required | A module handle, or the path of a .wasm file. |
Options
| Option | Type | Default | Description |
|---|---|---|---|
-args | list | an empty list | Gives the arguments of the program after its name. |
-env | dict | an empty dict | Gives the environment of the program. |
-dirs | dict of guest names and paths | none | Gives the program preopened folders. |
-stdin | string | an empty string | Gives the standard input of the program. |
-imports | dict of dicts | none | Links the imports that are not of WASI to Tcl commands. |
-limit | integer | none | Gives the most compute units for the program. |
-args LIST
Gives the arguments of the program after its name.
The program gets the name of the module as its first argument, then the items of LIST.
% # proc_exit with the count of arguments of args_sizes_get
> set am [ztcl::wasm new [binary decode hex 0061736d01000000010e0360017f0060027f7f017f600000024c0216776173695f736e617073686f745f70726576696577310970726f635f65786974000016776173695f736e617073686f745f70726576696577310e617267735f73697a65735f6765740001030201020503010001071302066d656d6f72790200065f737461727400020a120110004100410410011a410028020010000b]]
> list [ztcl::wasm run $am] [ztcl::wasm run $am -args {a b}]
1 3% ztcl::wasm run $m -args
wrong # args: should be "ztcl::wasm run module|fileName ?-args list? ?-env dict? ?-dirs dict? ?-stdin text? ?-imports dict? ?-limit units?"
-errorcode TCL WRONGARGS-env DICT
Gives the environment of the program.
The program sees only the names and values of DICT, and none of the environment of the host.
% # proc_exit with the count of variables of environ_sizes_get
> set vm [ztcl::wasm new [binary decode hex 0061736d01000000010e0360017f0060027f7f017f600000024f0216776173695f736e617073686f745f70726576696577310970726f635f65786974000016776173695f736e617073686f745f707265766965773111656e7669726f6e5f73697a65735f6765740001030201020503010001071302066d656d6f72790200065f737461727400020a120110004100410410011a410028020010000b]]
> list [ztcl::wasm run $vm] [ztcl::wasm run $vm -env {A 1 B 2}]
0 2% ztcl::wasm run $m -env A
missing value to go with key
-errorcode TCL VALUE DICTIONARY-dirs DICT
Gives the program preopened folders.
Each pair is a folder for the program: the guest name and a path of ztcl. It needs the capability file. The folder is inside the root folder of the interpreter, or a host path in host mode. A guest path stays inside its folder: .. above the folder gives ENOTCAPABLE.
% # proc_exit with the error number of fd_prestat_get of fd 3
> set pm [ztcl::wasm new [binary decode hex 0061736d01000000010e0360017f0060027f7f017f600000024c0216776173695f736e617073686f745f70726576696577310970726f635f65786974000016776173695f736e617073686f745f70726576696577310e66645f707265737461745f6765740001030201020503010001071302066d656d6f72790200065f737461727400020a0c010a0041034110100110000b]]
> list [ztcl::wasm run $pm] [ztcl::wasm run $pm -dirs {. .}]
8 0% ztcl::wasm run $m -dirs x
missing value to go with key
-errorcode TCL VALUE DICTIONARY-stdin TEXT
Gives the standard input of the program.
The program reads TEXT on its standard input, then the end of the file.
% # proc_exit with the count of bytes of one fd_read of 8 bytes
> set im [ztcl::wasm new [binary decode hex 0061736d0100000001100360017f0060047f7f7f7f017f60000002450216776173695f736e617073686f745f70726576696577310970726f635f65786974000016776173695f736e617073686f745f70726576696577310766645f726561640001030201020503010001071302066d656d6f72790200065f737461727400020a16011400410041004101410810011a410828020010000b0b0e010041000b081000000008000000]]
> list [ztcl::wasm run $im] [ztcl::wasm run $im -stdin hello]
0 5% ztcl::wasm run $m -stdin
wrong # args: should be "ztcl::wasm run module|fileName ?-args list? ?-env dict? ?-dirs dict? ?-stdin text? ?-imports dict? ?-limit units?"
-errorcode TCL WRONGARGS-imports DICT
Links the imports that are not of WASI to Tcl commands.
The same as the -imports option of instantiate.
% set lm [ztcl::wasm new [binary decode hex 0061736d0100000001080260017f00600000020b0103656e76036c6f670000030201010707010372756e00010a08010600410710000b]]
> catch {ztcl::wasm run $lm -imports {env {log puts}}} msg
> set msg
the instance has no export "_start"% ztcl::wasm run $m -imports env
missing value to go with key
-errorcode TCL VALUE DICTIONARY-limit UNITS
Gives the most compute units for the program.
The program stops after UNITS compute units with the error wasm trap: compute limit reached and the code ZTCL WASM LIMIT.
% # a "_start" that loops forever
> set sm [ztcl::wasm new [binary decode hex 0061736d01000000010401600000030201000503010001071302066d656d6f72790200065f737461727400000a0901070003400c000b0b]]
> list [catch {ztcl::wasm run $sm -limit 100} msg] $msg
1 {wasm trap: compute limit reached}% set sm [ztcl::wasm new [binary decode hex 0061736d01000000010401600000030201000503010001071302066d656d6f72790200065f737461727400000a0901070003400c000b0b]]
> ztcl::wasm run $sm -limit 100
wasm trap: compute limit reached
-errorcode ZTCL WASM LIMITReturns
The exit code.
Examples
Each example of this page runs after
package require ztcl::wasm
# (func (export "add") (param i32 i32) (result i32))
set m [ztcl::wasm new [binary decode hex 0061736d0100000001070160027f7f017f030201000707010361646400000a09010700200020016a0b]]
set w [ztcl::wasm instantiate $m]
# (memory (export "memory") 1 2), the bytes "hi" at 16, the globals "g" (mut i32 5) and "k" (i64 7), and (func (export "div") (param i32 i32) (result i32)) of i32.div_s
set mem [ztcl::wasm new [binary decode hex 0061736d0100000001070160027f7f017f03020100050401010102060b027f0141050b7e0042070b071804066d656d6f7279020001670300016b03010364697600000a09010700200020016d0b0b09010041100b03686900]]
.
% # fd_write of "hello\n" to stdout
> set hm [ztcl::wasm new [binary decode hex 0061736d01000000010c0260047f7f7f7f017f60000002230116776173695f736e617073686f745f70726576696577310866645f77726974650000030201010503010001071302066d656d6f72790200065f737461727400010a0f010d00410141004101411410001a0b0b19020041000b0808000000060000000041080b0668656c6c6f0a]]
> ztcl::wasm run $hm
hello
0
% # proc_exit with 3
> set f [open exit3.wasm wb]
> puts -nonewline $f [binary decode hex 0061736d0100000001080260017f0060000002240116776173695f736e617073686f745f70726576696577310970726f635f657869740000030201010503010001071302066d656d6f72790200065f737461727400010a08010600410310000b]
> close $f
> ztcl::wasm run exit3.wasm
3
% # proc_exit with the error number of clock_time_get
> ztcl::wasm run [ztcl::wasm new [binary decode hex 0061736d01000000010f0360017f0060037f7e7f017f600000024c0216776173695f736e617073686f745f70726576696577310970726f635f65786974000016776173695f736e617073686f745f70726576696577310e636c6f636b5f74696d655f6765740001030201020503010001071302066d656d6f72790200065f737461727400020a0e010c00410042014110100110000b]]
76
Errors
% ztcl::wasm run $m
the instance has no export "_start"
-errorcode ZTCL WASM EXPORT
% ztcl::wasm run nosuch.wasm
couldn't read file "nosuch.wasm": no such file or directory
-errorcode POSIX ENOENT {no such file or directory}
% ztcl::wasm run $m -bogus 1
bad option "-bogus": must be -args, -dirs, -env, -imports, -limit, or -stdin
-errorcode TCL LOOKUP INDEX option
% ztcl::wasm run
wrong # args: should be "ztcl::wasm run module|fileName ?-args list? ?-env dict? ?-dirs dict? ?-stdin text? ?-imports dict? ?-limit units?"
-errorcode TCL WRONGARGS
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1
- Compute cost
- One compute unit for each call and each loop pass of the wasm code.