ztcl::wasm new
Decodes and validates the bytes of a module, and gives a module handle.
ztcl::wasm new BYTES
Description
Decodes the byte string BYTES, as from binary decode or a file read in binary, validates the module, and gives a module handle. A module that is not well formed gives ZTCL WASM MALFORMED, and a module that does not validate gives ZTCL WASM INVALID, each with the offset of the byte.
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
| BYTES | byte string | required | The bytes of a module. |
Returns
A module 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 wasmmod* $m
1
% ztcl::wasm exports [ztcl::wasm new [binary decode hex 0061736d01000000]]
Errors
% ztcl::wasm new xyz
malformed module: magic header not detected at byte 0
-errorcode ZTCL WASM MALFORMED
% ztcl::wasm new [binary decode hex 0061736d02000000]
malformed module: unknown binary version at byte 4
-errorcode ZTCL WASM MALFORMED
% ztcl::wasm new [binary decode hex 0061736d0100000001]
malformed module: unexpected end at byte 9
-errorcode ZTCL WASM MALFORMED
% ztcl::wasm new [binary decode hex 0061736d010000000105016000017f030201000a040102000b]
invalid module: type mismatch at byte 24
-errorcode ZTCL WASM INVALID
% ztcl::wasm new
wrong # args: should be "ztcl::wasm new bytes"
-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.