beetlebug▾
/

ztcl::wasm global

Reads or sets an exported global.

ztcl::wasm global INSTANCE NAME ?VALUE?

Description

Gives the value of the global that INSTANCE exports as NAME. With VALUE it sets a mutable global and gives the new value as the type of the global gives it.

Arguments

ArgumentTypeDefaultDescription
INSTANCEhandlerequiredAn instance handle.
NAMEstringrequiredThe name of an exported global.
VALUEwasm valuenoneThe new value.

Returns

The value of the global.

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 gw [ztcl::wasm instantiate $mem]
> list [ztcl::wasm global $gw g] [ztcl::wasm global $gw g 11] [ztcl::wasm global $gw k]
5 11 7

% set gw [ztcl::wasm instantiate $mem]
> ztcl::wasm global $gw g 4294967295
-1

Errors

% set gw [ztcl::wasm instantiate $mem]
> ztcl::wasm global $gw k 1
the global "k" is immutable
-errorcode ZTCL WASM GLOBAL

% set gw [ztcl::wasm instantiate $mem]
> ztcl::wasm global $gw div
the export "div" is not a global
-errorcode ZTCL WASM EXPORT

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

% set gw [ztcl::wasm instantiate $mem]
> ztcl::wasm global $gw g x
expected integer but got "x"
-errorcode TCL VALUE NUMBER

% ztcl::wasm global $w
wrong # args: should be "ztcl::wasm global instance name ?value?"
-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