beetlebug▾
/

ztcl::wasm memory

Reads, writes, and grows the linear memory of an instance.

ztcl::wasm memory INSTANCE size
ztcl::wasm memory INSTANCE grow PAGES
ztcl::wasm memory INSTANCE read ADDRESS LENGTH
ztcl::wasm memory INSTANCE write ADDRESS BYTES ?-text?
ztcl::wasm memory INSTANCE string ADDRESS ?LENGTH?

Description

Works on the memory that INSTANCE exports as memory, or on its first memory. A page is 65,536 bytes, and a range outside the memory is an error with the code ZTCL WASM MEMORY. The name of the subcommand is checked before the memory is looked up, so an unknown subcommand is the same error for an instance with no memory.

Arguments

ArgumentTypeDefaultDescription
INSTANCEhandlerequiredAn instance handle.

Options

OptionTypeDefaultDescription
-textflagnoneWrites the UTF-8 bytes of a string.

-text

Writes the UTF-8 bytes of a string.

write writes the UTF-8 bytes of the string BYTES, so a character can be more than one byte.

% set mw [ztcl::wasm instantiate $mem]
> set n [ztcl::wasm memory $mw write 40 "hé" -text]
> list $n [binary encode hex [ztcl::wasm memory $mw read 40 3]]
3 68c3a9
% set mw [ztcl::wasm instantiate $mem]
> ztcl::wasm memory $mw write 0 ab -bogus
bad option "-bogus": must be -text
-errorcode TCL LOOKUP INDEX option

Returns

The size, the old size, the bytes, the count of bytes, or the text.

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 mw [ztcl::wasm instantiate $mem]
> list [ztcl::wasm memory $mw size] [ztcl::wasm memory $mw grow 1] [ztcl::wasm memory $mw grow 1] [ztcl::wasm memory $mw size]
1 1 -1 2

% set mw [ztcl::wasm instantiate $mem]
> list [ztcl::wasm memory $mw string 16] [ztcl::wasm memory $mw string 16 1] [binary encode hex [ztcl::wasm memory $mw read 16 4]]
hi h 68690000

% set mw [ztcl::wasm instantiate $mem]
> set n [ztcl::wasm memory $mw write 32 [binary decode hex 414243]]
> list $n [ztcl::wasm memory $mw string 32]
3 ABC

Errors

% set mw [ztcl::wasm instantiate $mem]
> ztcl::wasm memory $mw read 200000 4
the range 200000 to 200004 is outside the memory of 65536 bytes
-errorcode ZTCL WASM MEMORY

% set mw [ztcl::wasm instantiate $mem]
> ztcl::wasm memory $mw write 65535 ab
the range 65535 to 65537 is outside the memory of 65536 bytes
-errorcode ZTCL WASM MEMORY

% ztcl::wasm memory $w size
the instance has no memory
-errorcode ZTCL WASM MEMORY

% ztcl::wasm memory $w bogus
unknown subcommand "bogus": must be grow, read, size, string, or write
-errorcode TCL LOOKUP INDEX subcommand

% set mw [ztcl::wasm instantiate $mem]
> ztcl::wasm memory $mw read -1 4
the range -1 to 3 is outside the memory of 65536 bytes
-errorcode ZTCL WASM MEMORY

% ztcl::wasm memory $mem size
can not find wasm instance "wasmmod1"
-errorcode ZTCL WASM HANDLE

% set mw [ztcl::wasm instantiate $mem]
> ztcl::wasm memory $mw bogus
unknown subcommand "bogus": must be grow, read, size, string, or write
-errorcode TCL LOOKUP INDEX subcommand

% set mw [ztcl::wasm instantiate $mem]
> ztcl::wasm memory $mw s 16
unknown subcommand "s": must be grow, read, size, string, or write
-errorcode TCL LOOKUP INDEX subcommand

% ztcl::wasm memory $w
wrong # args: should be "ztcl::wasm memory instance size|read|write|string|grow ?arg ...?"
-errorcode TCL WRONGARGS

% set mw [ztcl::wasm instantiate $mem]
> ztcl::wasm memory $mw grow
wrong # args: should be "ztcl::wasm memory instance grow pages"
-errorcode TCL WRONGARGS

% set mw [ztcl::wasm instantiate $mem]
> ztcl::wasm memory $mw read 0 x
expected integer but got "x"
-errorcode TCL VALUE NUMBER

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