ztcl::wasm instantiate
Links the imports of a module, runs its start function, and gives an instance handle.
ztcl::wasm instantiate MODULE ?-imports DICT? ?-link DICT? ?-wasi DICT? ?-command NAME? ?-limit UNITS?
Description
Makes an instance of MODULE with its own memory, tables, and globals. It links each import, runs the start function of the module, and gives the instance handle. An import with no value is an error with the code ZTCL WASM LINK. A module that imports from wasi_snapshot_preview1 and exports _initialize, a WASI reactor, runs _initialize after the start function.
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
| MODULE | handle | required | A module handle from new or load. |
Options
| Option | Type | Default | Description |
|---|---|---|---|
-imports | dict of dicts | none | Links imports to Tcl commands. |
-link | dict of instance handles | none | Links imports to the exports of other instances. |
-wasi | dict | none | Gives the module the imports of WASI preview 1. |
-command | command name | none | Makes a command that calls the exports of the instance. |
-limit | integer | none | Gives the most compute units for the start function. |
-imports DICT
Links imports to Tcl commands.
For each import module, a dict of the import names and Tcl command prefixes. A call of the import calls the prefix with the arguments as Tcl values, and the result of the command is the result of the import. The Tcl code can call the instance again. A prefix is looked up at the call, and an error of the command is an error of call with its message and code.
% # (import "env" "log" (func (param i32))) and (func (export "run")) that calls it with 7
> set lm [ztcl::wasm new [binary decode hex 0061736d0100000001080260017f00600000020b0103656e76036c6f670000030201010707010372756e00010a08010600410710000b]]
> proc log {x} { puts "log $x" }
> ztcl::wasm call [ztcl::wasm instantiate $lm -imports {env {log log}}] run
log 7% set lm [ztcl::wasm new [binary decode hex 0061736d0100000001080260017f00600000020b0103656e76036c6f670000030201010707010372756e00010a08010600410710000b]]
> proc bad {x} { error boom {} {MY ERROR} }
> ztcl::wasm call [ztcl::wasm instantiate $lm -imports {env {log bad}}] run
boom
-errorcode MY ERROR
% ztcl::wasm instantiate $m -imports env
missing value to go with key
-errorcode TCL VALUE DICTIONARY-link DICT
Links imports to the exports of other instances.
For each import module, an instance handle. An import gets the export of the same name from that instance: a function, a table, a memory, or a global.
% # (import "env" "add" (func (param i32 i32) (result i32))) and (func (export "inc") (param i32) (result i32)) that adds 1
> set um [ztcl::wasm new [binary decode hex 0061736d01000000010c0260027f7f017f60017f017f020b0103656e760361646400000302010107070103696e6300010a0a0108002000410110000b]]
> ztcl::wasm call [ztcl::wasm instantiate $um -link [list env $w]] inc 41
42% set um [ztcl::wasm new [binary decode hex 0061736d01000000010c0260027f7f017f60017f017f020b0103656e760361646400000302010107070103696e6300010a0a0108002000410110000b]]
> ztcl::wasm instantiate $um -link {env nowasm}
can not find wasm instance "nowasm"
-errorcode ZTCL WASM HANDLE-wasi DICT
Gives the module the imports of WASI preview 1.
The keys are args, env, dirs, and stdin, as the options of run of the same names. A call of _start that calls proc_exit is an error with the code ZTCL WASM EXIT CODE.
% # (import "wasi_snapshot_preview1" "proc_exit") and a "_start" that exits with 3
> set em [ztcl::wasm new [binary decode hex 0061736d0100000001080260017f0060000002240116776173695f736e617073686f745f70726576696577310970726f635f657869740000030201010503010001071302066d656d6f72790200065f737461727400010a08010600410310000b]]
> set ew [ztcl::wasm instantiate $em -wasi {}]
> list [catch {ztcl::wasm call $ew _start} msg o] $msg [dict get $o -errorcode]
1 {wasm exit 3} {ZTCL WASM EXIT 3}% ztcl::wasm instantiate $m -wasi {bogus 1}
bad WASI option "bogus": must be args, dirs, env, or stdin
-errorcode TCL LOOKUP INDEX option-command NAME
Makes a command that calls the exports of the instance.
Makes the command NAME FUNCTION ?ARG ...?, the same as ztcl::wasm call INSTANCE FUNCTION ?ARG ...?. A command of that name is replaced. After delete of the instance the command stays, and a call of it is an error.
% ztcl::wasm instantiate $m -command adder
> adder add 3 4
7% ztcl::wasm instantiate $m -command adder
> adder
wrong # args: should be "adder func ?arg ...?"
-errorcode TCL WRONGARGS-limit UNITS
Gives the most compute units for the start function.
The start function stops with ZTCL WASM LIMIT after UNITS compute units.
% string match wasm* [ztcl::wasm instantiate $m -limit 5]
1% # a start function that loops forever
> ztcl::wasm instantiate [ztcl::wasm new [binary decode hex 0061736d01000000010401600000030201000801000a0901070003400c000b0b]] -limit 50
wasm trap: compute limit reached
-errorcode ZTCL WASM LIMIT
% ztcl::wasm instantiate $m -limit
wrong # args: should be "ztcl::wasm instantiate module ?-imports dict? ?-link dict? ?-wasi dict? ?-command name? ?-limit units?"
-errorcode TCL WRONGARGSReturns
An instance handle.
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]]
.
% string match wasm* $w
1
% # (global (export "g") (mut i32) (i32.const 0)) and a start function that sets it to 9
> set sm [ztcl::wasm new [binary decode hex 0061736d01000000010401600000030201000606017f0141000b070501016703000801000a08010600410924000b]]
> ztcl::wasm global [ztcl::wasm instantiate $sm] g
9
Errors
% # (import "env" "log" (func (param i32)))
> ztcl::wasm instantiate [ztcl::wasm new [binary decode hex 0061736d0100000001080260017f00600000020b0103656e76036c6f670000030201010707010372756e00010a08010600410710000b]]
missing import "env" "log"
-errorcode ZTCL WASM LINK
% ztcl::wasm instantiate $w
can not find wasm module "wasm0"
-errorcode ZTCL WASM HANDLE
% ztcl::wasm instantiate $m -bogus 1
bad option "-bogus": must be -command, -imports, -limit, -link, or -wasi
-errorcode TCL LOOKUP INDEX option
% ztcl::wasm instantiate
wrong # args: should be "ztcl::wasm instantiate module ?-imports dict? ?-link dict? ?-wasi dict? ?-command name? ?-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.