beetlebug▾
/

The ABI

The interface between an app and ztk is the WebAssembly import module tk. tk.wit defines it: the handles, the enums, and each function. tools/wasm-tk-gen.py writes this page, the glue of the host, and the import layer of each language from that file.

An app is a core WebAssembly module with one linear memory. No Tcl text goes from the app to the host: a widget is a handle, an option is a case of an enum, and a number is a number. The function call is for a Tk command that the interface does not have. It gets a list of words, and no word goes through the Tcl parser.

Exports of the app

Export What it does
memory The linear memory.
app_init() Builds the widgets. The host calls it one time, after the instantiation.
app_event(callback: i32) Runs a callback of the app. The app chooses the number when it gives the callback to on_command, bind, bind_key, bind_sequence, item_bind, or after.
_initialize() Optional. The host calls it before app_init, for the setup of the runtime of the language.

The core form of the types

Type of tk.wit Core form
s32, bool, an enum, a handle One i32. An enum is the number of its case.
color One i32: 0xRRGGBBAA. Alpha 0 is the empty color.
f64 One f64.
string parameter Two i32: a pointer and a byte length. The text is UTF-8.
list<f64> parameter Two i32: a pointer and a count of f64 values.
list<string> parameter Two i32: a pointer and a byte length. For each word: its byte length as a 32-bit little-endian number, then its UTF-8 bytes.
string result Two more i32 parameters: a pointer and a capacity in bytes. The i32 result is the full byte length. The host writes at most the capacity, and keeps the full text for result.
list<f64> result Two more i32 parameters: a pointer and a capacity as a count. The i32 result is the full count. The host writes at most the capacity.

Rules

  1. A handle is a number from the host. The app has no path name of a widget. destroy frees the handle of a widget and of each widget in it. An item handle is valid for its canvas.
  2. The host does not enter the module two times. An event that fires while the module runs (for example from update) waits in a queue of the host until the module returns.
  3. The event_* functions give the fields of the event of the callback that runs.
  4. The host writes only into the buffer of a result. The module owns its memory, so the interface needs no alloc export.
  5. An error of Tk in a function writes a line to the standard error of the host. The function then gives 0 or an empty value.
  6. set_id and item_set_id give a widget and an item a name. A check script finds them with these names: [w NAME] is the path of a widget, and [item CANVAS NAME] is an item.

Enums

The number of a case is its place in the list, from 0.

Enum Cases
opt text, width, height, background, foreground, fill, outline, anchor, state, justify, relief, orient, borderwidth, highlightthickness, padx, pady, from, to, resolution, value, show, wraplength, dash, textvariable, variable, length, takefocus
anchor center, n, ne, e, se, s, sw, w, nw
state normal, disabled, hidden, readonly, active
justify left, center, right
relief flat, raised, sunken, groove, ridge, solid
orient horizontal, vertical
side top, bottom, left, right
fill none, x, y, both
event-kind button-press, button-release, button-motion, motion, double-click, enter, leave, wheel, focus-in, focus-out, key-press
sticky (bits) n = 1, e = 2, s = 4, w = 8

Imports of the module tk

The interface has 91 functions.

Core import What it does
root() -> i32 The main window.
frame(parent: i32) -> i32
label(parent: i32) -> i32
button(parent: i32) -> i32
entry(parent: i32) -> i32
checkbutton(parent: i32) -> i32
radiobutton(parent: i32) -> i32
scale(parent: i32) -> i32
listbox(parent: i32) -> i32
text(parent: i32) -> i32
canvas(parent: i32) -> i32
toplevel(parent: i32) -> i32
scrollbar(parent: i32) -> i32
destroy(w: i32) Destroys a widget and frees its handle and the handles of the widgets in it.
set_id(w: i32, id_ptr: i32, id_len: i32) Gives a widget a name for check scripts and tools.
set_str(w: i32, o: i32, value_ptr: i32, value_len: i32)
set_int(w: i32, o: i32, value: i32) Sets a number, or the case of the enum of the option.
set_f64(w: i32, o: i32, value: f64)
set_bool(w: i32, o: i32, value: i32)
set_color(w: i32, o: i32, value: i32)
set_var(w: i32, o: i32, v: i32) Sets the option textvariable or variable.
set_font(w: i32, f: i32)
get_str(w: i32, o: i32, out_ptr: i32, out_cap: i32) -> i32
get_int(w: i32, o: i32) -> i32
get_f64(w: i32, o: i32) -> f64
get_bool(w: i32, o: i32) -> i32
on_command(w: i32, callback: i32) Sets the command of a button, a checkbutton, a radiobutton, or a scale.
bind(w: i32, e: i32, callback: i32)
bind_key(w: i32, key_ptr: i32, key_len: i32, callback: i32, stop: i32) Binds a key by its keysym, such as Return. With stop, no later binding of the key runs.
bind_sequence(w: i32, sequence_ptr: i32, sequence_len: i32, callback: i32, stop: i32) Binds a Tk event pattern, such as <Control-z>, for an event that event-kind does not have.
after(ms: i32, callback: i32) -> i32 Calls the callback one time after a count of milliseconds.
after_cancel(t: i32)
event_x() -> i32 The fields of the event of the callback that runs.
event_y() -> i32
event_root_x() -> i32
event_root_y() -> i32
event_button() -> i32
event_delta() -> i32
event_key(out_ptr: i32, out_cap: i32) -> i32 The keysym of a key event.
pack(w: i32, s: i32, f: i32, expand: i32, padx: i32, pady: i32)
grid(w: i32, row: i32, column: i32, rowspan: i32, columnspan: i32, sticky: i32, padx: i32, pady: i32)
grid_columnconfigure(parent: i32, index: i32, weight: i32)
grid_rowconfigure(parent: i32, index: i32, weight: i32)
place(w: i32, x: i32, y: i32, width: i32, height: i32) Places a widget at a point of its parent. A width or a height of 0 keeps the natural size.
focus(w: i32)
update() Handles the pending events and draws.
window_title(w: i32, title_ptr: i32, title_len: i32)
window_geometry(w: i32, width: i32, height: i32)
entry_get(w: i32, out_ptr: i32, out_cap: i32) -> i32
entry_set(w: i32, value_ptr: i32, value_len: i32)
entry_insert(w: i32, index: i32, value_ptr: i32, value_len: i32)
entry_delete(w: i32, first: i32, last: i32)
entry_select(w: i32, first: i32, last: i32)
entry_cursor(w: i32, index: i32)
listbox_insert(w: i32, index: i32, value_ptr: i32, value_len: i32)
listbox_delete(w: i32, first: i32, last: i32)
listbox_selection(w: i32) -> i32 The first selected index, or -1.
listbox_size(w: i32) -> i32
text_insert(w: i32, value_ptr: i32, value_len: i32) Adds text at the end.
text_get(w: i32, out_ptr: i32, out_cap: i32) -> i32
text_delete(w: i32) Deletes all the text.
scale_get(w: i32) -> f64
scale_set(w: i32, value: f64)
scrollbar_attach(bar: i32, target: i32, vertical: i32) Connects a scrollbar and the view of a widget.
variable_new() -> i32
variable_set(v: i32, value_ptr: i32, value_len: i32)
variable_get(v: i32, out_ptr: i32, out_cap: i32) -> i32
variable_free(v: i32)
font_new(family_ptr: i32, family_len: i32, size: i32, bold: i32) -> i32
font_free(f: i32)
canvas_rect(c: i32, x1: f64, y1: f64, x2: f64, y2: f64) -> i32
canvas_oval(c: i32, x1: f64, y1: f64, x2: f64, y2: f64) -> i32
canvas_line(c: i32, points_ptr: i32, points_count: i32) -> i32
canvas_text(c: i32, x: f64, y: f64, value_ptr: i32, value_len: i32) -> i32
canvas_find_closest(c: i32, x: f64, y: f64) -> i32 The top item that is nearest to a point, or 0.
item_set_id(c: i32, i: i32, id_ptr: i32, id_len: i32) Gives an item a name for check scripts and tools.
item_coords_set(c: i32, i: i32, points_ptr: i32, points_count: i32)
item_coords_get(c: i32, i: i32, out_ptr: i32, out_cap: i32) -> i32
item_move(c: i32, i: i32, dx: f64, dy: f64)
item_set_str(c: i32, i: i32, o: i32, value_ptr: i32, value_len: i32)
item_set_int(c: i32, i: i32, o: i32, value: i32)
item_set_f64(c: i32, i: i32, o: i32, value: f64)
item_set_color(c: i32, i: i32, o: i32, value: i32)
item_get_str(c: i32, i: i32, o: i32, out_ptr: i32, out_cap: i32) -> i32
item_delete(c: i32, i: i32)
item_raise(c: i32, i: i32)
item_lower(c: i32, i: i32)
item_bind(c: i32, i: i32, e: i32, callback: i32)
call(words_ptr: i32, words_len: i32, out_ptr: i32, out_cap: i32) -> i32 Runs a Tk command from a list of words. No word goes through the Tcl parser.
result(out_ptr: i32, out_cap: i32) -> i32 The full text of the last string result.
log(message_ptr: i32, message_len: i32) Writes a line to the standard error of the host.