beetlebug▾
/

ztcl::wasm package

Loads a Tcl C package built for wasm32-wasi and runs its init function.

ztcl::wasm package FILENAME ?PREFIX? ?-wasi DICT?

Description

Loads a Tcl package in C that was built for wasm32-wasi with the stub library libztclstubs.a and the exports ztcl_cext_*, and runs its init function Prefix_Init. The commands of the package are commands of the interpreter, and the package runs in the limits of the interpreter. It needs the capability file. The package of SQLite (tclsqlite3.c) loads this way.

Arguments

ArgumentTypeDefaultDescription
FILENAMEpathrequiredThe path of the .wasm file of the package.
PREFIXstringnoneThe prefix of the init function, such as Sqlite3.

Options

OptionTypeDefaultDescription
-wasidictnoneGives the package the folders and the other values of WASI.

-wasi DICT

Gives the package the folders and the other values of WASI.

The keys of the -wasi option of instantiate. With no -wasi, the package gets the imports of WASI with no folder. The options are checked before the file is read. A -wasi with no value is a wrong count of arguments.

% set f [open add.wasm wb]
> puts -nonewline $f [binary decode hex 0061736d0100000001070160027f7f017f030201000707010361646400000a09010700200020016a0b]
> close $f
> catch {ztcl::wasm package add.wasm Add -wasi {dirs {. .}}} msg
> set msg
the module "add.wasm" has no export "ztcl_cext_reply" of a C package
% ztcl::wasm package x.wasm Add -wasi {bogus 1}
bad WASI option "bogus": must be args, dirs, env, or stdin
-errorcode TCL LOOKUP INDEX option

% ztcl::wasm package x.wasm Add -wasi
wrong # args: should be "ztcl::wasm package fileName ?prefix? ?-wasi dict?"
-errorcode TCL WRONGARGS

% ztcl::wasm package x.wasm Add -bogus 1
bad option "-bogus": must be -wasi
-errorcode TCL LOOKUP INDEX option

Returns

The result of the init function.

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

% set f [open add.wasm wb]
> puts -nonewline $f [binary decode hex 0061736d0100000001070160027f7f017f030201000707010361646400000a09010700200020016a0b]
> close $f
> catch {ztcl::wasm package add.wasm Add} msg
> set msg
the module "add.wasm" has no export "ztcl_cext_reply" of a C package

Errors

% ztcl::wasm package x.wasm
couldn't read file "x.wasm": no such file or directory
-errorcode POSIX ENOENT {no such file or directory}

% ztcl::wasm package
wrong # args: should be "ztcl::wasm package fileName ?prefix? ?-wasi dict?"
-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