beetlebug▾
/

ztcl::wasm call

Calls an exported function and gives its result.

ztcl::wasm call INSTANCE FUNCTION ?-limit UNITS? ?-raw? ?--? ?ARG ...?

Description

Calls the export FUNCTION of INSTANCE with the ARG values, one for each parameter. It gives the result, a list for more than one result, or an empty string for none. The options come after FUNCTION, and the first word that is not an option is the first ARG. A word of a dash and a letter that is no number, such as -bogus, is an unknown option and an error. A negative number, -inf, and -nan are arguments.

Arguments

ArgumentTypeDefaultDescription
INSTANCEhandlerequiredAn instance handle.
FUNCTIONstringrequiredThe name of an exported function.
ARGwasm valuenoneA value for a parameter.

Options

OptionTypeDefaultDescription
-limitintegernoneGives the most compute units for the call.
-rawflagnoneReads and gives each value as the unsigned integer of its bits.
--flagnoneEnds the options.

-limit UNITS

Gives the most compute units for the call.

The call stops after UNITS compute units with the error wasm trap: compute limit reached and the code ZTCL WASM LIMIT, which catch can catch. The units that the call used leave the limit of the interpreter.

% # (func (export "spin")) loops forever
> set sm [ztcl::wasm new [binary decode hex 0061736d0100000001040160000003020100070801047370696e00000a0901070003400c000b0b]]
> set sw [ztcl::wasm instantiate $sm]
> list [catch {ztcl::wasm call $sw spin -limit 1000} msg] $msg
1 {wasm trap: compute limit reached}

% ztcl::wasm call $w add -limit 5 1 2
3
% ztcl::wasm call $w add -limit 0 1 2
wasm trap: compute limit reached
-errorcode ZTCL WASM LIMIT

% ztcl::wasm call $w add -limit -1 1 2
the limit must be 0 or more
-errorcode ZTCL WASM LIMIT

% ztcl::wasm call $w add -limit x 1 2
expected integer but got "x"
-errorcode TCL VALUE NUMBER

% ztcl::wasm call $w add -limit
missing value for option "-limit"
-errorcode NONE

-raw

Reads and gives each value as the unsigned integer of its bits.

Each ARG and each result is the unsigned integer of the bits of the value: an f64 is its 64 bits, and an i32 of -1 is 4294967295. It gives the exact bits of a NaN.

% set mu [ztcl::wasm new [binary decode hex 0061736d01000000010f036000027f7f6000017c60017c017c0304030001020714030374776f00000468616c660001036e656700020a1a030600410141020b0b0044000000000000e03f0b050020009a0b]]
> set uw [ztcl::wasm instantiate $mu]
> list [ztcl::wasm call $uw half] [ztcl::wasm call $uw half -raw]
0.5 4602678819172646912

% set dw [ztcl::wasm instantiate $mem]
> ztcl::wasm call $dw div -raw -1 1
4294967295

A flag has no value to check.

--

Ends the options.

The next argument is the first ARG, also when it starts with -.

% ztcl::wasm call $w add -- -1 -2
-3

A flag has no value to check.

Returns

The result, a list of the results, or an empty string.

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]] .

% ztcl::wasm call $w add 40 2
42

% ztcl::wasm call $w add 4294967295 1
0

% set dw [ztcl::wasm instantiate $mem]
> ztcl::wasm call $dw div 7 2
3

% # (func (export "two") (result i32 i32)) gives 1 and 2
> set mu [ztcl::wasm new [binary decode hex 0061736d01000000010f036000027f7f6000017c60017c017c0304030001020714030374776f00000468616c660001036e656700020a1a030600410141020b0b0044000000000000e03f0b050020009a0b]]
> ztcl::wasm call [ztcl::wasm instantiate $mu] two
1 2

Errors

% set dw [ztcl::wasm instantiate $mem]
> ztcl::wasm call $dw div 7 0
wasm trap: integer divide by zero
-errorcode ZTCL WASM TRAP {integer divide by zero}

% ztcl::wasm call $w add 1
the function "add" has 2 parameters and got 1 arguments
-errorcode ZTCL WASM ARGS

% ztcl::wasm call $w add -bogus 1 2
bad option "-bogus": must be -limit, -raw, or --
-errorcode TCL LOOKUP INDEX option

% ztcl::wasm call $w add 4294967296 1
expected i32 but got "4294967296"
-errorcode ZTCL WASM VALUE

% ztcl::wasm call $w add x 1
expected integer but got "x"
-errorcode TCL VALUE NUMBER

% ztcl::wasm call $w nope
the instance has no export "nope"
-errorcode ZTCL WASM EXPORT

% ztcl::wasm call $w -raw add 1 2
the instance has no export "-raw"
-errorcode ZTCL WASM EXPORT

% ztcl::wasm call $m add 1 2
can not find wasm instance "wasmmod0"
-errorcode ZTCL WASM HANDLE

% ztcl::wasm call $w
wrong # args: should be "ztcl::wasm call instance function ?-limit units? ?-raw? ?--? ?arg ...?"
-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.

See also