tishlang_wasm_runtime 3.0.0

Tish VM compiled to WebAssembly (browser + Wasmtime/WASI)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Browser WebGPU / JS-interop FFI for the tish bytecode VM.
//!
//! Lets a tish program compiled to wasm drive the browser's WebGPU (and any
//! Web API) without hand-binding each call. The bridge is a tiny set of
//! reflection-based primitives exposed as VM globals:
//!
//! - `js_global(name)` — read a JS global (e.g. `navigator`, `GPUBufferUsage`)
//! - `js_get(handle, key)` / `js_set(handle, key, val)`
//! - `js_call(handle, method, argsArray)` — call a method (the whole WebGPU
//!   command API is synchronous, so this covers it)
//! - `js_new(ctorNameOrHandle, argsArray)`
//! - `js_typeof(handle)` — debugging
//! - `f32a(arr)` / `u16a(arr)` / `u8a(arr)` / `u32a(arr)` — tish `number[]` → real typed array
//! - `request_animation_frame(cb)` — drive a render loop
//!
//! GPU/JS objects (device, queue, context, buffers, pipelines, textures,
//! ImageBitmaps, the host env object …) round-trip through the VM as opaque
//! [`JsHandle`] values. Async startup (requestAdapter/requestDevice/fetch/
//! createImageBitmap) is done in JS glue; the ready handles are handed to the
//! VM via the `host` global by [`start`].

use std::any::Any;
use std::cell::{Cell, RefCell};
use std::sync::Arc;

use tishlang_bytecode::deserialize;
use tishlang_core::{value_call, NativeFn, TishOpaque, Value};
use tishlang_vm::Vm;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;

// ---------------------------------------------------------------------------
// Opaque JS handle
// ---------------------------------------------------------------------------

/// Opaque tish value wrapping a browser `JsValue`. `JsValue` is `!Send`, which
/// is why `TishOpaque`'s `Send + Sync` bound is gated off in the browser
/// (`!send-values`) build — see `tish_core/src/value.rs`.
struct JsHandle(JsValue);

impl TishOpaque for JsHandle {
    fn type_name(&self) -> &'static str {
        "JsHandle"
    }
    fn get_method(&self, _name: &str) -> Option<NativeFn> {
        None
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
}

fn wrap(v: JsValue) -> Value {
    Value::Opaque(Arc::new(JsHandle(v)))
}

fn unwrap_handle(v: &Value) -> Option<JsValue> {
    match v {
        Value::Opaque(o) => o.as_any().downcast_ref::<JsHandle>().map(|h| h.0.clone()),
        _ => None,
    }
}

// ---------------------------------------------------------------------------
// Marshalling  tish Value <-> JsValue
// ---------------------------------------------------------------------------

/// Convert a tish value to a JS value. Objects/arrays recurse; an opaque
/// `JsHandle` is spliced in **by reference** (so descriptors can embed live GPU
/// handles, e.g. `beginRenderPass({ colorAttachments:[{ view:<handle> }] })`).
/// Functions/promises/symbols are not marshalled (return `null`).
fn value_to_js(v: &Value) -> JsValue {
    match v {
        Value::Number(n) => JsValue::from_f64(*n),
        Value::String(s) => JsValue::from_str(s.as_ref()),
        Value::Bool(b) => JsValue::from_bool(*b),
        Value::Null => JsValue::NULL,
        Value::Opaque(o) => match o.as_any().downcast_ref::<JsHandle>() {
            Some(h) => h.0.clone(),
            None => JsValue::NULL,
        },
        Value::Array(arr) => {
            let out = js_sys::Array::new();
            for item in arr.borrow().iter() {
                out.push(&value_to_js(item));
            }
            out.into()
        }
        Value::Object(obj) => {
            let out = js_sys::Object::new();
            let b = obj.borrow();
            for (k, val) in b.strings.iter() {
                let _ = js_sys::Reflect::set(
                    &out,
                    &JsValue::from_str(k.as_ref()),
                    &value_to_js(val),
                );
            }
            out.into()
        }
        _ => JsValue::NULL,
    }
}

/// Convert a JS value back to tish. Primitives map directly; everything else
/// (objects, functions, typed arrays, GPU objects) becomes an opaque handle —
/// we deliberately do **not** expand JS containers into tish arrays/objects
/// (that would re-introduce boxing and lose typed-array identity).
fn js_to_value(v: JsValue) -> Value {
    if v.is_null() || v.is_undefined() {
        Value::Null
    } else if let Some(n) = v.as_f64() {
        Value::Number(n)
    } else if let Some(b) = v.as_bool() {
        Value::Bool(b)
    } else if let Some(s) = v.as_string() {
        Value::String(s.into())
    } else {
        wrap(v)
    }
}

// ---------------------------------------------------------------------------
// FFI builtins
// ---------------------------------------------------------------------------

fn ffi_js_global() -> Value {
    Value::native(|args: &[Value]| {
        let name = match args.first() {
            Some(Value::String(s)) => s.clone(),
            _ => return Value::Null,
        };
        match js_sys::Reflect::get(&js_sys::global(), &JsValue::from_str(name.as_ref())) {
            Ok(v) => js_to_value(v),
            Err(_) => Value::Null,
        }
    })
}

fn ffi_js_get() -> Value {
    Value::native(|args: &[Value]| {
        let obj = match args.first().and_then(unwrap_handle) {
            Some(o) => o,
            None => return Value::Null,
        };
        let key = args.get(1).map(value_to_js).unwrap_or(JsValue::NULL);
        match js_sys::Reflect::get(&obj, &key) {
            Ok(v) => js_to_value(v),
            Err(_) => Value::Null,
        }
    })
}

fn ffi_js_set() -> Value {
    Value::native(|args: &[Value]| {
        let obj = match args.first().and_then(unwrap_handle) {
            Some(o) => o,
            None => return Value::Null,
        };
        let key = args.get(1).map(value_to_js).unwrap_or(JsValue::NULL);
        let val = args.get(2).map(value_to_js).unwrap_or(JsValue::NULL);
        let _ = js_sys::Reflect::set(&obj, &key, &val);
        Value::Null
    })
}

fn ffi_js_call() -> Value {
    Value::native(|args: &[Value]| {
        let obj = match args.first().and_then(unwrap_handle) {
            Some(o) => o,
            None => return Value::Null,
        };
        let method = match args.get(1) {
            Some(Value::String(s)) => s.clone(),
            _ => return Value::Null,
        };
        let func = match js_sys::Reflect::get(&obj, &JsValue::from_str(method.as_ref())) {
            Ok(f) => match f.dyn_into::<js_sys::Function>() {
                Ok(f) => f,
                Err(_) => return Value::Null,
            },
            Err(_) => return Value::Null,
        };
        let js_args = js_sys::Array::new();
        if let Some(Value::Array(a)) = args.get(2) {
            for item in a.borrow().iter() {
                js_args.push(&value_to_js(item));
            }
        }
        match js_sys::Reflect::apply(&func, &obj, &js_args) {
            Ok(v) => js_to_value(v),
            Err(_) => Value::Null,
        }
    })
}

fn ffi_js_new() -> Value {
    Value::native(|args: &[Value]| {
        let ctor: JsValue = match args.first() {
            Some(Value::String(s)) => {
                match js_sys::Reflect::get(&js_sys::global(), &JsValue::from_str(s.as_ref())) {
                    Ok(v) => v,
                    Err(_) => return Value::Null,
                }
            }
            Some(v @ Value::Opaque(_)) => match unwrap_handle(v) {
                Some(h) => h,
                None => return Value::Null,
            },
            _ => return Value::Null,
        };
        let ctor_fn = match ctor.dyn_into::<js_sys::Function>() {
            Ok(f) => f,
            Err(_) => return Value::Null,
        };
        let js_args = js_sys::Array::new();
        if let Some(Value::Array(a)) = args.get(1) {
            for item in a.borrow().iter() {
                js_args.push(&value_to_js(item));
            }
        }
        match js_sys::Reflect::construct(&ctor_fn, &js_args) {
            Ok(v) => js_to_value(v),
            Err(_) => Value::Null,
        }
    })
}

fn ffi_js_typeof() -> Value {
    Value::native(|args: &[Value]| {
        let v = args.first().map(value_to_js).unwrap_or(JsValue::NULL);
        match v.js_typeof().as_string() {
            Some(s) => Value::String(s.into()),
            None => Value::Null,
        }
    })
}

/// `f32a(numberArray)` -> opaque `Float32Array` handle (one-shot copy). Use for
/// per-frame uniform/transform staging. Large static buffers should instead be
/// materialised host-side and passed in opaque (never boxed into a tish array).
fn ffi_f32a() -> Value {
    Value::native(|args: &[Value]| {
        let arr = match args.first() {
            Some(Value::Array(a)) => a.clone(),
            _ => return Value::Null,
        };
        let b = arr.borrow();
        let ta = js_sys::Float32Array::new_with_length(b.len() as u32);
        for (i, v) in b.iter().enumerate() {
            ta.set_index(i as u32, v.as_number().unwrap_or(0.0) as f32);
        }
        wrap(ta.into())
    })
}

fn ffi_u16a() -> Value {
    Value::native(|args: &[Value]| {
        let arr = match args.first() {
            Some(Value::Array(a)) => a.clone(),
            _ => return Value::Null,
        };
        let b = arr.borrow();
        let ta = js_sys::Uint16Array::new_with_length(b.len() as u32);
        for (i, v) in b.iter().enumerate() {
            ta.set_index(i as u32, v.as_number().unwrap_or(0.0) as u16);
        }
        wrap(ta.into())
    })
}

fn ffi_u8a() -> Value {
    Value::native(|args: &[Value]| {
        let arr = match args.first() {
            Some(Value::Array(a)) => a.clone(),
            _ => return Value::Null,
        };
        let b = arr.borrow();
        let ta = js_sys::Uint8Array::new_with_length(b.len() as u32);
        for (i, v) in b.iter().enumerate() {
            ta.set_index(i as u32, v.as_number().unwrap_or(0.0) as u8);
        }
        wrap(ta.into())
    })
}

fn ffi_u32a() -> Value {
    Value::native(|args: &[Value]| {
        let arr = match args.first() {
            Some(Value::Array(a)) => a.clone(),
            _ => return Value::Null,
        };
        let b = arr.borrow();
        let ta = js_sys::Uint32Array::new_with_length(b.len() as u32);
        for (i, v) in b.iter().enumerate() {
            ta.set_index(i as u32, v.as_number().unwrap_or(0.0) as u32);
        }
        wrap(ta.into())
    })
}

// ---------------------------------------------------------------------------
// requestAnimationFrame render loop
// ---------------------------------------------------------------------------

thread_local! {
    static RAF_CALLBACK: RefCell<Option<Value>> = const { RefCell::new(None) };
    static RAF_CLOSURE: RefCell<Option<Closure<dyn FnMut(f64)>>> = const { RefCell::new(None) };
    // True while a frame is pending, so repeated request_animation_frame calls
    // within one frame don't compound into runaway scheduling.
    static RAF_SCHEDULED: Cell<bool> = const { Cell::new(false) };
}

/// Browser-driven per-frame entry: invoke the stored tish callback, then
/// re-arm for the next frame. We re-schedule from Rust (rather than requiring
/// the tish callback to call `request_animation_frame` again each frame) so the
/// loop runs continuously as long as a callback is registered. `cancel`-ing the
/// loop = clearing `RAF_CALLBACK`. The `value_call` runs the frame closure to
/// completion; all WebGPU recording happens synchronously inside.
fn tick(ts: f64) {
    let cb = RAF_CALLBACK.with(|c| c.borrow().clone());
    // This frame's pending schedule is now consumed.
    RAF_SCHEDULED.with(|f| f.set(false));
    if let Some(cb) = cb {
        if matches!(cb, Value::Function(_)) {
            value_call(&cb, &[Value::Number(ts)]);
        }
        // Re-arm only if the callback is still registered (allows a future
        // cancel by clearing RAF_CALLBACK).
        if RAF_CALLBACK.with(|c| c.borrow().is_some()) {
            schedule_raf();
        }
    }
}

fn schedule_raf() {
    // Coalesce: at most one rAF in flight at a time.
    if RAF_SCHEDULED.with(|f| f.get()) {
        return;
    }
    RAF_SCHEDULED.with(|f| f.set(true));
    RAF_CLOSURE.with(|slot| {
        let mut s = slot.borrow_mut();
        if s.is_none() {
            *s = Some(Closure::wrap(Box::new(|ts: f64| tick(ts)) as Box<dyn FnMut(f64)>));
        }
        let g = js_sys::global();
        if let Ok(raf) = js_sys::Reflect::get(&g, &JsValue::from_str("requestAnimationFrame")) {
            if let Ok(raf_fn) = raf.dyn_into::<js_sys::Function>() {
                let _ = raf_fn.call1(&g, s.as_ref().unwrap().as_ref().unchecked_ref());
            }
        }
    });
}

fn ffi_request_animation_frame() -> Value {
    Value::native(|args: &[Value]| {
        if let Some(cb) = args.first() {
            RAF_CALLBACK.with(|c| *c.borrow_mut() = Some(cb.clone()));
        }
        schedule_raf();
        Value::Null
    })
}

// ---------------------------------------------------------------------------
// Install + entry point
// ---------------------------------------------------------------------------

fn install_ffi(vm: &mut Vm) {
    vm.set_global("js_global".into(), ffi_js_global());
    vm.set_global("js_get".into(), ffi_js_get());
    vm.set_global("js_set".into(), ffi_js_set());
    vm.set_global("js_call".into(), ffi_js_call());
    vm.set_global("js_new".into(), ffi_js_new());
    vm.set_global("js_typeof".into(), ffi_js_typeof());
    vm.set_global("f32a".into(), ffi_f32a());
    vm.set_global("u16a".into(), ffi_u16a());
    vm.set_global("u8a".into(), ffi_u8a());
    vm.set_global("u32a".into(), ffi_u32a());
    vm.set_global("request_animation_frame".into(), ffi_request_animation_frame());
}

#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(js_namespace = console, js_name = error)]
    fn console_error(s: &str);
}

fn set_panic_hook() {
    use std::sync::Once;
    static HOOK: Once = Once::new();
    HOOK.call_once(|| {
        std::panic::set_hook(Box::new(|info| {
            console_error(&format!("tish wasm panic: {}", info));
        }));
    });
}

/// Browser entry: run a tish bytecode `chunk` with the JS-interop FFI installed
/// and the host environment object (device/queue/context/format/canvas/assets,
/// built by the page's async startup glue) exposed as the `host` global.
///
/// Returns after top-level tish runs; the `requestAnimationFrame` loop keeps
/// the captured globals alive via the stored callback, so the VM state persists
/// across frames even though this call returns.
/// Invoke the registered frame callback exactly once, without re-scheduling.
/// For driving frames deterministically from JS when `requestAnimationFrame` is
/// throttled (e.g. a hidden/offscreen preview tab) — verification & debugging.
#[wasm_bindgen]
pub fn tick_once(ts: f64) {
    let cb = RAF_CALLBACK.with(|c| c.borrow().clone());
    if let Some(cb) = cb {
        if matches!(cb, Value::Function(_)) {
            value_call(&cb, &[Value::Number(ts)]);
        }
    }
}

#[wasm_bindgen]
pub fn start(chunk: Vec<u8>, env: JsValue) -> Result<(), JsValue> {
    set_panic_hook();
    let chunk = deserialize(&chunk).map_err(|e| JsValue::from_str(&e))?;
    let mut vm = Vm::new();
    install_ffi(&mut vm);
    vm.set_global("host".into(), wrap(env));
    vm.run(&chunk).map_err(|e| JsValue::from_str(&e))?;
    Ok(())
}