Skip to main content

gasm/
sys.rs

1//! Raw ABI. On wasm32 these are the real imports (see spec/ABI.md). On other
2//! targets they are backed by [`crate::native`], an in-process stub host used
3//! for native parity tests and benchmarks.
4//!
5//! GENERATED from spec/abi.json by scripts/gen-abi.mjs. Do not edit by hand.
6
7#![allow(clippy::missing_safety_doc)]
8
9#[cfg(target_arch = "wasm32")]
10mod imports {
11    #[link(wasm_import_module = "gasm")]
12    unsafe extern "C" {
13        /// Write a line to the runner's log.
14        pub fn log(msg: *const u8, msg_len: u32);
15        /// Monotonic time in milliseconds (virtual, frame-derived in headless runs).
16        pub fn time_ms() -> f64;
17        /// Rate (Hz) at which the runner calls gasm_frame(). Default 60; 1-1000.
18        pub fn set_frame_rate(hz: f64);
19        /// Present RGBA8 pixels (bytes R,G,B,A), stride bytes per row, w,h <= 4096. Copied before returning; letterboxed by the runner.
20        pub fn video_present(rgba: *const u8, width: u32, height: u32, stride: u32);
21        /// Format for audio_push: 8-192 kHz, 1 or 2 channels. Default 44100/2.
22        pub fn audio_config(sample_rate: u32, channels: u32);
23        /// Queue frames x channels interleaved f32 samples in [-1, 1]; the runner resamples.
24        pub fn audio_push(samples: *const f32, frames: u32);
25        /// Bitmask of GASM_BTN_* held on virtual pad player (0..3), stable within a frame.
26        pub fn input_pad(player: u32) -> u32;
27        /// UTF-8 text typed since the previous frame (backspace = \b, enter = \n), stable within a frame. Returns its length (copied only if length <= cap; cap = 0 queries), or -1 if the runner has no keyboard.
28        pub fn text_input(dst: *mut u8, cap: u32) -> i32;
29        /// GASM_INPUT_* flags: KEYS_RAW (the runner stops mapping the keyboard to pads; gamepads still map), POINTER_HIDDEN (hide the system cursor over the game), POINTER_LOCKED (capture the pointer for relative motion; best effort, the browser needs a click).
30        pub fn input_mode(flags: u32);
31        /// Held keys as a bitset indexed by GASM_KEY_* (bit k of byte k/8), stable within a frame. Copies min(len, GASM_KEY_STATE_BYTES) bytes; returns GASM_KEY_STATE_BYTES, or -1 if the runner has no keyboard.
32        pub fn key_state(dst: *mut u8, len: u32) -> i32;
33        /// Key presses and releases since the previous frame, in order: 4 bytes each (u16 GASM_KEY_* code, u8 1 = down / 0 = up, u8 0). Returns the byte length (copied only if <= cap; cap = 0 queries), or -1 if the runner has no keyboard.
34        pub fn key_events(dst: *mut u8, cap: u32) -> i32;
35        /// Mouse/touch state for this frame as GASM_POINTER_BYTES bytes (see ABI.md: position in drawable and frame pixels, relative motion, wheel, buttons held/pressed/released, flags). Copied only if cap is large enough; returns GASM_POINTER_BYTES, or -1 if the runner has no pointer.
36        pub fn pointer(dst: *mut u8, cap: u32) -> i32;
37        /// Raw gamepad/joystick in slot 0-3 (connection order) as GASM_GAMEPAD_BYTES bytes: u32 flags (GASM_GAMEPAD_CONNECTED, GASM_GAMEPAD_STANDARD), u32 button count, u32 axis count, f32 buttons[32] (0-1), f32 axes[16] (-1..1). Standard mapping: W3C button and axis order. Copied only if cap is large enough; returns GASM_GAMEPAD_BYTES, or -1 if slot > 3 or the runner has no gamepad support.
38        pub fn gamepad(slot: u32, dst: *mut u8, cap: u32) -> i32;
39        /// Device name of the gamepad in slot: its length (copied only if <= cap), or -1 if the slot is empty.
40        pub fn gamepad_name(slot: u32, dst: *mut u8, cap: u32) -> i32;
41        /// Size in bytes of asset name, or -1 if it does not exist.
42        pub fn asset_size(name: *const u8, name_len: u32) -> i32;
43        /// Copy up to cap bytes of asset name into dst. Bytes copied, or -1 if missing.
44        pub fn asset_read(name: *const u8, name_len: u32, dst: *mut u8, cap: u32) -> i32;
45        /// Copy up to len bytes of asset name starting at offset (streaming). Bytes copied (0 at the end), or -1 if missing.
46        pub fn asset_read_at(name: *const u8, name_len: u32, offset: u32, dst: *mut u8, len: u32) -> i32;
47        /// Number of assets.
48        pub fn asset_count() -> u32;
49        /// Name of asset index (0 .. asset_count-1, sorted by UTF-8 bytes; folder entries as named on disk). Its length (copied only if length <= cap; cap = 0 queries), or -1 if index is out of range.
50        pub fn asset_name(index: u32, dst: *mut u8, cap: u32) -> i32;
51        /// Launch parameter value length, or -1 if unset. Copied only if length <= cap (cap = 0 queries the length).
52        pub fn param(name: *const u8, name_len: u32, dst: *mut u8, cap: u32) -> i32;
53    }
54
55    #[link(wasm_import_module = "gasm:gfx")]
56    unsafe extern "C" {
57        /// Current drawable width in pixels.
58        #[link_name = "width"]
59        pub fn gfx_width() -> u32;
60        /// Current drawable height in pixels.
61        #[link_name = "height"]
62        pub fn gfx_height() -> u32;
63        /// Compile WGSL source.
64        #[link_name = "create_shader"]
65        pub fn gfx_create_shader(wgsl: *const u8, wgsl_len: u32) -> u32;
66        /// size: non-zero multiple of 4; usage: GASM_BUF_* (WebGPU GPUBufferUsage bits; COPY_DST always added).
67        #[link_name = "create_buffer"]
68        pub fn gfx_create_buffer(size: u32, usage: u32) -> u32;
69        /// GPURenderPipelineDescriptor as JSON. layout: "auto" (default) or an array of bind group layout handles.
70        #[link_name = "create_pipeline"]
71        pub fn gfx_create_pipeline(json: *const u8, json_len: u32) -> u32;
72        /// {"layout":L,"entries":[...]} or {"pipeline":P,"group":G,"entries":[...]}; entries {"binding":B,"buffer":H,"offset":O,"size":S}, {"binding":B,"texture":T} or {"binding":B,"sampler":S}.
73        #[link_name = "create_bind_group"]
74        pub fn gfx_create_bind_group(json: *const u8, json_len: u32) -> u32;
75        /// GPUBindGroupLayoutDescriptor subset: {"entries":[{"binding":B,"visibility":GASM_STAGE_*,"buffer":{"type":"uniform"|"read-only-storage","hasDynamicOffset":bool,"minBindingSize":N} | "texture":{"sampleType":"float"} | "sampler":{"type":"filtering"}}]}.
76        #[link_name = "create_bind_group_layout"]
77        pub fn gfx_create_bind_group_layout(json: *const u8, json_len: u32) -> u32;
78        /// 2D texture: {"size":[w,h],"format":"rgba8unorm"|"rgba8unorm-srgb","mipLevelCount":n}, w,h 1-8192. Usage is TEXTURE_BINDING | COPY_DST.
79        #[link_name = "create_texture"]
80        pub fn gfx_create_texture(json: *const u8, json_len: u32) -> u32;
81        /// Upload a tightly packed RGBA8 region (len = width*height*4) of mip level mip at (x, y). Queued like write_buffer.
82        #[link_name = "write_texture"]
83        pub fn gfx_write_texture(texture: u32, mip: u32, x: u32, y: u32, width: u32, height: u32, data: *const u8, len: u32);
84        /// GPUSamplerDescriptor subset: addressModeU/V, magFilter, minFilter, mipmapFilter, lodMinClamp, lodMaxClamp, maxAnisotropy (1-16).
85        #[link_name = "create_sampler"]
86        pub fn gfx_create_sampler(json: *const u8, json_len: u32) -> u32;
87        /// Queue a write, applied before the frame's draws. offset/len: multiples of 4.
88        #[link_name = "write_buffer"]
89        pub fn gfx_write_buffer(buffer: u32, offset: u32, data: *const u8, len: u32);
90        /// Start the frame (clears color and depth). 1 = will be shown, 0 = discarded (the guest may skip draws).
91        #[link_name = "begin_frame"]
92        pub fn gfx_begin_frame(r: f32, g: f32, b: f32, a: f32) -> u32;
93        #[link_name = "set_pipeline"]
94        pub fn gfx_set_pipeline(pipeline: u32);
95        #[link_name = "set_bind_group"]
96        pub fn gfx_set_bind_group(index: u32, bind_group: u32);
97        /// set_bind_group with count dynamic offsets (multiples of 256), one per dynamic-offset entry of the layout, in binding order.
98        #[link_name = "set_bind_group_offsets"]
99        pub fn gfx_set_bind_group_offsets(index: u32, bind_group: u32, offsets: *const u32, count: u32);
100        /// Viewport in drawable pixels (clamped to the drawable), depth range 0-1. Reset to the whole drawable by begin_frame.
101        #[link_name = "set_viewport"]
102        pub fn gfx_set_viewport(x: f32, y: f32, width: f32, height: f32, min_depth: f32, max_depth: f32);
103        /// Scissor rectangle in drawable pixels (clamped to the drawable). Reset to the whole drawable by begin_frame.
104        #[link_name = "set_scissor_rect"]
105        pub fn gfx_set_scissor_rect(x: u32, y: u32, width: u32, height: u32);
106        #[link_name = "set_vertex_buffer"]
107        pub fn gfx_set_vertex_buffer(slot: u32, buffer: u32, offset: u32);
108        /// format: GASM_INDEX_U16 or GASM_INDEX_U32.
109        #[link_name = "set_index_buffer"]
110        pub fn gfx_set_index_buffer(buffer: u32, format: u32, offset: u32);
111        #[link_name = "draw"]
112        pub fn gfx_draw(vertex_count: u32, instance_count: u32, first_vertex: u32, first_instance: u32);
113        #[link_name = "draw_indexed"]
114        pub fn gfx_draw_indexed(index_count: u32, instance_count: u32, first_index: u32, base_vertex: i32, first_instance: u32);
115        /// Submit and present.
116        #[link_name = "end_frame"]
117        pub fn gfx_end_frame();
118    }
119
120    #[link(wasm_import_module = "gasm:net")]
121    unsafe extern "C" {
122        /// Open a ws:// or wss:// URL. Handle > 0, or -1 if denied/invalid.
123        #[link_name = "open"]
124        pub fn net_open(url: *const u8, url_len: u32) -> i32;
125        /// GASM_NET_CONNECTING / OPEN / CLOSED / ERROR.
126        #[link_name = "state"]
127        pub fn net_state(conn: i32) -> u32;
128        /// Send one message (len > 0). 0, or -1 if not open.
129        #[link_name = "send"]
130        pub fn net_send(conn: i32, data: *const u8, len: u32) -> i32;
131        /// Next message's length (copied only if <= cap, else it stays queued), 0 if none, -1 if closed and drained.
132        #[link_name = "recv"]
133        pub fn net_recv(conn: i32, dst: *mut u8, cap: u32) -> i32;
134        #[link_name = "close"]
135        pub fn net_close(conn: i32);
136    }
137
138    #[link(wasm_import_module = "gasm:storage")]
139    unsafe extern "C" {
140        /// Value length, or -1 if missing. Copied only if length <= cap.
141        #[link_name = "get"]
142        pub fn storage_get(key: *const u8, key_len: u32, dst: *mut u8, cap: u32) -> i32;
143        /// 0, or -1 on invalid key, too large, quota exceeded or I/O error.
144        #[link_name = "set"]
145        pub fn storage_set(key: *const u8, key_len: u32, data: *const u8, len: u32) -> i32;
146        /// 0 if deleted, -1 if it did not exist.
147        #[link_name = "delete"]
148        pub fn storage_delete(key: *const u8, key_len: u32) -> i32;
149        /// Number of keys in the namespace.
150        #[link_name = "count"]
151        pub fn storage_count() -> u32;
152        /// Key index (0 .. count-1, sorted): its length (copied only if <= cap; cap = 0 queries), or -1 if out of range.
153        #[link_name = "key"]
154        pub fn storage_key(index: u32, dst: *mut u8, cap: u32) -> i32;
155    }
156
157    #[link(wasm_import_module = "wasi_snapshot_preview1")]
158    unsafe extern "C" {
159        /// End the game; 0 is a normal exit.
160        pub fn proc_exit(code: i32) -> !;
161    }
162}
163
164#[cfg(target_arch = "wasm32")]
165pub use imports::*;
166
167#[cfg(not(target_arch = "wasm32"))]
168pub use crate::native::abi::*;
169
170/// Physical keys (W3C KeyboardEvent.code names; layout-independent). The third column is the W3C name.
171pub mod keys {
172    pub const ESCAPE: u32 = 1;
173    pub const F1: u32 = 2;
174    pub const F2: u32 = 3;
175    pub const F3: u32 = 4;
176    pub const F4: u32 = 5;
177    pub const F5: u32 = 6;
178    pub const F6: u32 = 7;
179    pub const F7: u32 = 8;
180    pub const F8: u32 = 9;
181    pub const F9: u32 = 10;
182    pub const F10: u32 = 11;
183    pub const F11: u32 = 12;
184    pub const F12: u32 = 13;
185    pub const BACKQUOTE: u32 = 14;
186    pub const DIGIT0: u32 = 15;
187    pub const DIGIT1: u32 = 16;
188    pub const DIGIT2: u32 = 17;
189    pub const DIGIT3: u32 = 18;
190    pub const DIGIT4: u32 = 19;
191    pub const DIGIT5: u32 = 20;
192    pub const DIGIT6: u32 = 21;
193    pub const DIGIT7: u32 = 22;
194    pub const DIGIT8: u32 = 23;
195    pub const DIGIT9: u32 = 24;
196    pub const MINUS: u32 = 25;
197    pub const EQUAL: u32 = 26;
198    pub const BACKSPACE: u32 = 27;
199    pub const TAB: u32 = 28;
200    pub const KEY_A: u32 = 29;
201    pub const KEY_B: u32 = 30;
202    pub const KEY_C: u32 = 31;
203    pub const KEY_D: u32 = 32;
204    pub const KEY_E: u32 = 33;
205    pub const KEY_F: u32 = 34;
206    pub const KEY_G: u32 = 35;
207    pub const KEY_H: u32 = 36;
208    pub const KEY_I: u32 = 37;
209    pub const KEY_J: u32 = 38;
210    pub const KEY_K: u32 = 39;
211    pub const KEY_L: u32 = 40;
212    pub const KEY_M: u32 = 41;
213    pub const KEY_N: u32 = 42;
214    pub const KEY_O: u32 = 43;
215    pub const KEY_P: u32 = 44;
216    pub const KEY_Q: u32 = 45;
217    pub const KEY_R: u32 = 46;
218    pub const KEY_S: u32 = 47;
219    pub const KEY_T: u32 = 48;
220    pub const KEY_U: u32 = 49;
221    pub const KEY_V: u32 = 50;
222    pub const KEY_W: u32 = 51;
223    pub const KEY_X: u32 = 52;
224    pub const KEY_Y: u32 = 53;
225    pub const KEY_Z: u32 = 54;
226    pub const BRACKET_LEFT: u32 = 55;
227    pub const BRACKET_RIGHT: u32 = 56;
228    pub const BACKSLASH: u32 = 57;
229    pub const CAPS_LOCK: u32 = 58;
230    pub const SEMICOLON: u32 = 59;
231    pub const QUOTE: u32 = 60;
232    pub const ENTER: u32 = 61;
233    pub const SHIFT_LEFT: u32 = 62;
234    pub const INTL_BACKSLASH: u32 = 63;
235    pub const COMMA: u32 = 64;
236    pub const PERIOD: u32 = 65;
237    pub const SLASH: u32 = 66;
238    pub const SHIFT_RIGHT: u32 = 67;
239    pub const CONTROL_LEFT: u32 = 68;
240    pub const META_LEFT: u32 = 69;
241    pub const ALT_LEFT: u32 = 70;
242    pub const SPACE: u32 = 71;
243    pub const ALT_RIGHT: u32 = 72;
244    pub const META_RIGHT: u32 = 73;
245    pub const CONTEXT_MENU: u32 = 74;
246    pub const CONTROL_RIGHT: u32 = 75;
247    pub const PRINT_SCREEN: u32 = 76;
248    pub const SCROLL_LOCK: u32 = 77;
249    pub const PAUSE: u32 = 78;
250    pub const INSERT: u32 = 79;
251    pub const HOME: u32 = 80;
252    pub const PAGE_UP: u32 = 81;
253    pub const DELETE: u32 = 82;
254    pub const END: u32 = 83;
255    pub const PAGE_DOWN: u32 = 84;
256    pub const ARROW_UP: u32 = 85;
257    pub const ARROW_LEFT: u32 = 86;
258    pub const ARROW_DOWN: u32 = 87;
259    pub const ARROW_RIGHT: u32 = 88;
260    pub const NUM_LOCK: u32 = 89;
261    pub const NUMPAD_DIVIDE: u32 = 90;
262    pub const NUMPAD_MULTIPLY: u32 = 91;
263    pub const NUMPAD_SUBTRACT: u32 = 92;
264    pub const NUMPAD_ADD: u32 = 93;
265    pub const NUMPAD_ENTER: u32 = 94;
266    pub const NUMPAD_DECIMAL: u32 = 95;
267    pub const NUMPAD0: u32 = 96;
268    pub const NUMPAD1: u32 = 97;
269    pub const NUMPAD2: u32 = 98;
270    pub const NUMPAD3: u32 = 99;
271    pub const NUMPAD4: u32 = 100;
272    pub const NUMPAD5: u32 = 101;
273    pub const NUMPAD6: u32 = 102;
274    pub const NUMPAD7: u32 = 103;
275    pub const NUMPAD8: u32 = 104;
276    pub const NUMPAD9: u32 = 105;
277    pub const NUMPAD_EQUAL: u32 = 106;
278    pub const NUMPAD_COMMA: u32 = 107;
279    pub const INTL_RO: u32 = 108;
280    pub const INTL_YEN: u32 = 109;
281    pub const F13: u32 = 110;
282    pub const F14: u32 = 111;
283    pub const F15: u32 = 112;
284    pub const F16: u32 = 113;
285    pub const F17: u32 = 114;
286    pub const F18: u32 = 115;
287    pub const F19: u32 = 116;
288    pub const F20: u32 = 117;
289    pub const F21: u32 = 118;
290    pub const F22: u32 = 119;
291    pub const F23: u32 = 120;
292    pub const F24: u32 = 121;
293    /// W3C names by code (index 0 is unused).
294    pub const NAMES: [&str; 122] = [
295        "", "Escape", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "Backquote", "Digit0", "Digit1", "Digit2", "Digit3", "Digit4", "Digit5", "Digit6", "Digit7", "Digit8", "Digit9", "Minus", "Equal", "Backspace", "Tab", "KeyA", "KeyB", "KeyC", "KeyD", "KeyE", "KeyF", "KeyG", "KeyH", "KeyI", "KeyJ", "KeyK", "KeyL", "KeyM", "KeyN", "KeyO", "KeyP", "KeyQ", "KeyR", "KeyS", "KeyT", "KeyU", "KeyV", "KeyW", "KeyX", "KeyY", "KeyZ", "BracketLeft", "BracketRight", "Backslash", "CapsLock", "Semicolon", "Quote", "Enter", "ShiftLeft", "IntlBackslash", "Comma", "Period", "Slash", "ShiftRight", "ControlLeft", "MetaLeft", "AltLeft", "Space", "AltRight", "MetaRight", "ContextMenu", "ControlRight", "PrintScreen", "ScrollLock", "Pause", "Insert", "Home", "PageUp", "Delete", "End", "PageDown", "ArrowUp", "ArrowLeft", "ArrowDown", "ArrowRight", "NumLock", "NumpadDivide", "NumpadMultiply", "NumpadSubtract", "NumpadAdd", "NumpadEnter", "NumpadDecimal", "Numpad0", "Numpad1", "Numpad2", "Numpad3", "Numpad4", "Numpad5", "Numpad6", "Numpad7", "Numpad8", "Numpad9", "NumpadEqual", "NumpadComma", "IntlRo", "IntlYen", "F13", "F14", "F15", "F16", "F17", "F18", "F19", "F20", "F21", "F22", "F23", "F24",
296    ];
297}