@tishlang/tish 1.10.0 → 1.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/tish +0 -0
- package/crates/tish/Cargo.toml +1 -1
- package/crates/tish/src/cli_help.rs +9 -1
- package/crates/tish/src/main.rs +55 -3
- package/crates/tish_build_utils/src/lib.rs +33 -10
- package/crates/tish_builtins/src/math.rs +7 -0
- package/crates/tish_builtins/src/string.rs +26 -0
- package/crates/tish_compile/src/codegen.rs +207 -26
- package/crates/tish_compile/src/lib.rs +19 -9
- package/crates/tish_compile/src/resolve.rs +185 -3
- package/crates/tish_core/src/value.rs +21 -0
- package/crates/tish_cranelift/src/link.rs +1 -1
- package/crates/tish_fmt/src/lib.rs +1201 -132
- package/crates/tish_native/src/build.rs +88 -19
- package/crates/tish_native/src/config.rs +48 -0
- package/crates/tish_native/src/lib.rs +48 -5
- package/crates/tish_runtime/src/lib.rs +9 -1
- package/crates/tish_runtime/src/timers.rs +12 -7
- package/crates/tish_ui/src/lib.rs +5 -4
- package/crates/tish_ui/src/runtime/hooks.rs +119 -23
- package/crates/tish_ui/src/runtime/mod.rs +7 -26
- package/crates/tish_vm/src/vm.rs +40 -0
- package/crates/tish_wasm/src/lib.rs +27 -0
- package/crates/tish_wasm_runtime/Cargo.toml +6 -0
- package/crates/tish_wasm_runtime/src/gpu.rs +413 -0
- package/crates/tish_wasm_runtime/src/lib.rs +5 -0
- package/package.json +1 -1
- package/platform/darwin-arm64/tish +0 -0
- package/platform/darwin-x64/tish +0 -0
- package/platform/linux-arm64/tish +0 -0
- package/platform/linux-x64/tish +0 -0
- package/platform/win32-x64/tish.exe +0 -0
|
@@ -2,14 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
mod hooks;
|
|
4
4
|
|
|
5
|
-
use std::cell::RefCell;
|
|
6
5
|
use std::sync::Arc;
|
|
7
6
|
|
|
8
7
|
pub use hooks::{
|
|
9
|
-
alloc_root_id, current_root_id, drop_host_for_root, install_host_for_root,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
alloc_root_id, current_root_id, drop_host_for_root, install_host_for_root,
|
|
9
|
+
install_thread_local_host, native_create_root,
|
|
10
|
+
native_use_effect, native_use_layout_effect, native_use_memo, native_use_ref,
|
|
11
|
+
native_use_state, run_with_current_root,
|
|
12
|
+
schedule_flush,
|
|
13
|
+
unregister_root, unregister_root_hooks_and_effects, with_host_for_root,
|
|
14
|
+
with_thread_local_host, HookState, RootId, LEGACY_ROOT_ID,
|
|
13
15
|
};
|
|
14
16
|
|
|
15
17
|
use tishlang_core::{ObjectMap, Value, VmRef};
|
|
@@ -157,27 +159,6 @@ impl Host for HeadlessHost {
|
|
|
157
159
|
}
|
|
158
160
|
}
|
|
159
161
|
|
|
160
|
-
thread_local! {
|
|
161
|
-
static ACTIVE_HOST: RefCell<Option<Box<dyn Host>>> = RefCell::new(None);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/// Install the thread-local host used by [`schedule_flush`] / `createRoot`.
|
|
165
|
-
pub fn install_thread_local_host(host: Box<dyn Host>) {
|
|
166
|
-
ACTIVE_HOST.with(|c| {
|
|
167
|
-
*c.borrow_mut() = Some(host);
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
pub fn with_thread_local_host<R>(f: impl FnOnce(&mut dyn Host) -> R) -> Option<R> {
|
|
172
|
-
ACTIVE_HOST.with(|c| {
|
|
173
|
-
let mut opt = c.borrow_mut();
|
|
174
|
-
match opt.as_deref_mut() {
|
|
175
|
-
Some(host) => Some(f(host)),
|
|
176
|
-
None => None,
|
|
177
|
-
}
|
|
178
|
-
})
|
|
179
|
-
}
|
|
180
|
-
|
|
181
162
|
/// Tag registry hook for future host-specific intrinsic mapping (HTML tag → component kind).
|
|
182
163
|
#[derive(Default)]
|
|
183
164
|
pub struct TagRegistry;
|
package/crates/tish_vm/src/vm.rs
CHANGED
|
@@ -455,6 +455,46 @@ fn init_globals(enabled: &HashSet<String>) -> ObjectMap {
|
|
|
455
455
|
"trunc".into(),
|
|
456
456
|
Value::native(|args: &[Value]| math_builtins::trunc(args)),
|
|
457
457
|
);
|
|
458
|
+
// Trig/hypot not covered by `math_builtins`; needed by the 3D engine's
|
|
459
|
+
// camera + character-controller math (atan2/hypot) on the wasm VM, where
|
|
460
|
+
// (unlike `--target js`) there is no host `Math` to fall through to.
|
|
461
|
+
math.insert(
|
|
462
|
+
"atan2".into(),
|
|
463
|
+
Value::native(|args: &[Value]| {
|
|
464
|
+
let y = args.first().and_then(|v| v.as_number()).unwrap_or(f64::NAN);
|
|
465
|
+
let x = args.get(1).and_then(|v| v.as_number()).unwrap_or(f64::NAN);
|
|
466
|
+
Value::Number(y.atan2(x))
|
|
467
|
+
}),
|
|
468
|
+
);
|
|
469
|
+
math.insert(
|
|
470
|
+
"atan".into(),
|
|
471
|
+
Value::native(|args: &[Value]| {
|
|
472
|
+
let n = args.first().and_then(|v| v.as_number()).unwrap_or(f64::NAN);
|
|
473
|
+
Value::Number(n.atan())
|
|
474
|
+
}),
|
|
475
|
+
);
|
|
476
|
+
math.insert(
|
|
477
|
+
"asin".into(),
|
|
478
|
+
Value::native(|args: &[Value]| {
|
|
479
|
+
let n = args.first().and_then(|v| v.as_number()).unwrap_or(f64::NAN);
|
|
480
|
+
Value::Number(n.asin())
|
|
481
|
+
}),
|
|
482
|
+
);
|
|
483
|
+
math.insert(
|
|
484
|
+
"acos".into(),
|
|
485
|
+
Value::native(|args: &[Value]| {
|
|
486
|
+
let n = args.first().and_then(|v| v.as_number()).unwrap_or(f64::NAN);
|
|
487
|
+
Value::Number(n.acos())
|
|
488
|
+
}),
|
|
489
|
+
);
|
|
490
|
+
math.insert(
|
|
491
|
+
"hypot".into(),
|
|
492
|
+
Value::native(|args: &[Value]| {
|
|
493
|
+
let nums: Vec<f64> = args.iter().filter_map(|v| v.as_number()).collect();
|
|
494
|
+
let sum_sq: f64 = nums.iter().map(|n| n * n).sum();
|
|
495
|
+
Value::Number(sum_sq.sqrt())
|
|
496
|
+
}),
|
|
497
|
+
);
|
|
458
498
|
math.insert("PI".into(), Value::Number(std::f64::consts::PI));
|
|
459
499
|
math.insert("E".into(), Value::Number(std::f64::consts::E));
|
|
460
500
|
g.insert("Math".into(), value_object_from_map(math));
|
|
@@ -231,6 +231,33 @@ pub fn compile_to_wasm(
|
|
|
231
231
|
emit_wasm_from_chunk(&chunk, output_path)
|
|
232
232
|
}
|
|
233
233
|
|
|
234
|
+
/// Compile a Tish project to a raw serialized bytecode chunk.
|
|
235
|
+
///
|
|
236
|
+
/// Writes a single `{output}` file of the exact bytes that the wasm/WASI runtime entry points
|
|
237
|
+
/// (`start` / `run`) deserialize directly — the same chunk `--target wasm` embeds as base64 in
|
|
238
|
+
/// its generated HTML loader, but written raw with no VM binary, JS glue, or HTML wrapper. Lets a
|
|
239
|
+
/// host that already ships the VM runtime (e.g. a bundler) consume the bytecode without the
|
|
240
|
+
/// throwaway standalone build.
|
|
241
|
+
pub fn compile_to_bytecode(
|
|
242
|
+
entry_path: &Path,
|
|
243
|
+
project_root: Option<&Path>,
|
|
244
|
+
output_path: &Path,
|
|
245
|
+
optimize: bool,
|
|
246
|
+
) -> Result<(), WasmError> {
|
|
247
|
+
let (chunk, _) = resolve_and_compile_to_chunk(entry_path, project_root, optimize)?;
|
|
248
|
+
let bytes = serialize(&chunk);
|
|
249
|
+
if let Some(parent) = output_path.parent().filter(|p| !p.as_os_str().is_empty()) {
|
|
250
|
+
std::fs::create_dir_all(parent).map_err(|e| WasmError {
|
|
251
|
+
message: format!("Cannot create output directory: {}", e),
|
|
252
|
+
})?;
|
|
253
|
+
}
|
|
254
|
+
std::fs::write(output_path, &bytes).map_err(|e| WasmError {
|
|
255
|
+
message: format!("Cannot write {}: {}", output_path.display(), e),
|
|
256
|
+
})?;
|
|
257
|
+
println!("Built: {} ({} bytes)", output_path.display(), bytes.len());
|
|
258
|
+
Ok(())
|
|
259
|
+
}
|
|
260
|
+
|
|
234
261
|
/// Compile a Tish project for Wasmtime/WASI.
|
|
235
262
|
///
|
|
236
263
|
/// Produces a single `{output}.wasm` with embedded bytecode. Run with:
|
|
@@ -12,6 +12,10 @@ crate-type = ["cdylib", "rlib"]
|
|
|
12
12
|
[features]
|
|
13
13
|
# For wasm32-unknown-unknown (browser): wasm-bindgen, console output
|
|
14
14
|
browser = ["dep:wasm-bindgen", "tishlang_vm/wasm"]
|
|
15
|
+
# Browser WebGPU / JS-interop FFI + requestAnimationFrame render loop (the
|
|
16
|
+
# `start(chunk, env)` entry). Reflection-based bridge over js-sys; no web-sys
|
|
17
|
+
# WebGPU bindings needed since the WebGPU command API is synchronous.
|
|
18
|
+
gpu = ["browser", "dep:js-sys"]
|
|
15
19
|
# Built-in modules for WASI (wasm32-wasip1): align with `tishlang_cranelift_runtime` / CLI caps
|
|
16
20
|
fs = ["tishlang_vm/fs"]
|
|
17
21
|
process = ["tishlang_vm/process"]
|
|
@@ -24,7 +28,9 @@ ws = ["tishlang_vm/ws"]
|
|
|
24
28
|
[dependencies]
|
|
25
29
|
tishlang_bytecode = { path = "../tish_bytecode", version = ">=0.1" }
|
|
26
30
|
tishlang_vm = { path = "../tish_vm", version = ">=0.1" }
|
|
31
|
+
tishlang_core = { path = "../tish_core", version = ">=0.1" }
|
|
27
32
|
wasm-bindgen = { version = "0.2", optional = true }
|
|
33
|
+
js-sys = { version = "0.3", optional = true }
|
|
28
34
|
|
|
29
35
|
# rand_core → getrandom 0.4 needs wasm_js on wasm32-unknown-unknown (browser VM build).
|
|
30
36
|
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
//! Browser WebGPU / JS-interop FFI for the tish bytecode VM.
|
|
2
|
+
//!
|
|
3
|
+
//! Lets a tish program compiled to wasm drive the browser's WebGPU (and any
|
|
4
|
+
//! Web API) without hand-binding each call. The bridge is a tiny set of
|
|
5
|
+
//! reflection-based primitives exposed as VM globals:
|
|
6
|
+
//!
|
|
7
|
+
//! - `js_global(name)` — read a JS global (e.g. `navigator`, `GPUBufferUsage`)
|
|
8
|
+
//! - `js_get(handle, key)` / `js_set(handle, key, val)`
|
|
9
|
+
//! - `js_call(handle, method, argsArray)` — call a method (the whole WebGPU
|
|
10
|
+
//! command API is synchronous, so this covers it)
|
|
11
|
+
//! - `js_new(ctorNameOrHandle, argsArray)`
|
|
12
|
+
//! - `js_typeof(handle)` — debugging
|
|
13
|
+
//! - `f32a(arr)` / `u16a(arr)` / `u8a(arr)` — tish `number[]` → real typed array
|
|
14
|
+
//! - `request_animation_frame(cb)` — drive a render loop
|
|
15
|
+
//!
|
|
16
|
+
//! GPU/JS objects (device, queue, context, buffers, pipelines, textures,
|
|
17
|
+
//! ImageBitmaps, the host env object …) round-trip through the VM as opaque
|
|
18
|
+
//! [`JsHandle`] values. Async startup (requestAdapter/requestDevice/fetch/
|
|
19
|
+
//! createImageBitmap) is done in JS glue; the ready handles are handed to the
|
|
20
|
+
//! VM via the `host` global by [`start`].
|
|
21
|
+
|
|
22
|
+
use std::any::Any;
|
|
23
|
+
use std::cell::{Cell, RefCell};
|
|
24
|
+
use std::sync::Arc;
|
|
25
|
+
|
|
26
|
+
use tishlang_bytecode::deserialize;
|
|
27
|
+
use tishlang_core::{value_call, NativeFn, TishOpaque, Value};
|
|
28
|
+
use tishlang_vm::Vm;
|
|
29
|
+
use wasm_bindgen::prelude::*;
|
|
30
|
+
use wasm_bindgen::JsCast;
|
|
31
|
+
|
|
32
|
+
// ---------------------------------------------------------------------------
|
|
33
|
+
// Opaque JS handle
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
|
|
36
|
+
/// Opaque tish value wrapping a browser `JsValue`. `JsValue` is `!Send`, which
|
|
37
|
+
/// is why `TishOpaque`'s `Send + Sync` bound is gated off in the browser
|
|
38
|
+
/// (`!send-values`) build — see `tish_core/src/value.rs`.
|
|
39
|
+
struct JsHandle(JsValue);
|
|
40
|
+
|
|
41
|
+
impl TishOpaque for JsHandle {
|
|
42
|
+
fn type_name(&self) -> &'static str {
|
|
43
|
+
"JsHandle"
|
|
44
|
+
}
|
|
45
|
+
fn get_method(&self, _name: &str) -> Option<NativeFn> {
|
|
46
|
+
None
|
|
47
|
+
}
|
|
48
|
+
fn as_any(&self) -> &dyn Any {
|
|
49
|
+
self
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
fn wrap(v: JsValue) -> Value {
|
|
54
|
+
Value::Opaque(Arc::new(JsHandle(v)))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
fn unwrap_handle(v: &Value) -> Option<JsValue> {
|
|
58
|
+
match v {
|
|
59
|
+
Value::Opaque(o) => o.as_any().downcast_ref::<JsHandle>().map(|h| h.0.clone()),
|
|
60
|
+
_ => None,
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
// Marshalling tish Value <-> JsValue
|
|
66
|
+
// ---------------------------------------------------------------------------
|
|
67
|
+
|
|
68
|
+
/// Convert a tish value to a JS value. Objects/arrays recurse; an opaque
|
|
69
|
+
/// `JsHandle` is spliced in **by reference** (so descriptors can embed live GPU
|
|
70
|
+
/// handles, e.g. `beginRenderPass({ colorAttachments:[{ view:<handle> }] })`).
|
|
71
|
+
/// Functions/promises/symbols are not marshalled (return `null`).
|
|
72
|
+
fn value_to_js(v: &Value) -> JsValue {
|
|
73
|
+
match v {
|
|
74
|
+
Value::Number(n) => JsValue::from_f64(*n),
|
|
75
|
+
Value::String(s) => JsValue::from_str(s.as_ref()),
|
|
76
|
+
Value::Bool(b) => JsValue::from_bool(*b),
|
|
77
|
+
Value::Null => JsValue::NULL,
|
|
78
|
+
Value::Opaque(o) => match o.as_any().downcast_ref::<JsHandle>() {
|
|
79
|
+
Some(h) => h.0.clone(),
|
|
80
|
+
None => JsValue::NULL,
|
|
81
|
+
},
|
|
82
|
+
Value::Array(arr) => {
|
|
83
|
+
let out = js_sys::Array::new();
|
|
84
|
+
for item in arr.borrow().iter() {
|
|
85
|
+
out.push(&value_to_js(item));
|
|
86
|
+
}
|
|
87
|
+
out.into()
|
|
88
|
+
}
|
|
89
|
+
Value::Object(obj) => {
|
|
90
|
+
let out = js_sys::Object::new();
|
|
91
|
+
let b = obj.borrow();
|
|
92
|
+
for (k, val) in b.strings.iter() {
|
|
93
|
+
let _ = js_sys::Reflect::set(
|
|
94
|
+
&out,
|
|
95
|
+
&JsValue::from_str(k.as_ref()),
|
|
96
|
+
&value_to_js(val),
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
out.into()
|
|
100
|
+
}
|
|
101
|
+
_ => JsValue::NULL,
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/// Convert a JS value back to tish. Primitives map directly; everything else
|
|
106
|
+
/// (objects, functions, typed arrays, GPU objects) becomes an opaque handle —
|
|
107
|
+
/// we deliberately do **not** expand JS containers into tish arrays/objects
|
|
108
|
+
/// (that would re-introduce boxing and lose typed-array identity).
|
|
109
|
+
fn js_to_value(v: JsValue) -> Value {
|
|
110
|
+
if v.is_null() || v.is_undefined() {
|
|
111
|
+
Value::Null
|
|
112
|
+
} else if let Some(n) = v.as_f64() {
|
|
113
|
+
Value::Number(n)
|
|
114
|
+
} else if let Some(b) = v.as_bool() {
|
|
115
|
+
Value::Bool(b)
|
|
116
|
+
} else if let Some(s) = v.as_string() {
|
|
117
|
+
Value::String(s.into())
|
|
118
|
+
} else {
|
|
119
|
+
wrap(v)
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// ---------------------------------------------------------------------------
|
|
124
|
+
// FFI builtins
|
|
125
|
+
// ---------------------------------------------------------------------------
|
|
126
|
+
|
|
127
|
+
fn ffi_js_global() -> Value {
|
|
128
|
+
Value::native(|args: &[Value]| {
|
|
129
|
+
let name = match args.first() {
|
|
130
|
+
Some(Value::String(s)) => s.clone(),
|
|
131
|
+
_ => return Value::Null,
|
|
132
|
+
};
|
|
133
|
+
match js_sys::Reflect::get(&js_sys::global(), &JsValue::from_str(name.as_ref())) {
|
|
134
|
+
Ok(v) => js_to_value(v),
|
|
135
|
+
Err(_) => Value::Null,
|
|
136
|
+
}
|
|
137
|
+
})
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
fn ffi_js_get() -> Value {
|
|
141
|
+
Value::native(|args: &[Value]| {
|
|
142
|
+
let obj = match args.first().and_then(unwrap_handle) {
|
|
143
|
+
Some(o) => o,
|
|
144
|
+
None => return Value::Null,
|
|
145
|
+
};
|
|
146
|
+
let key = args.get(1).map(value_to_js).unwrap_or(JsValue::NULL);
|
|
147
|
+
match js_sys::Reflect::get(&obj, &key) {
|
|
148
|
+
Ok(v) => js_to_value(v),
|
|
149
|
+
Err(_) => Value::Null,
|
|
150
|
+
}
|
|
151
|
+
})
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
fn ffi_js_set() -> Value {
|
|
155
|
+
Value::native(|args: &[Value]| {
|
|
156
|
+
let obj = match args.first().and_then(unwrap_handle) {
|
|
157
|
+
Some(o) => o,
|
|
158
|
+
None => return Value::Null,
|
|
159
|
+
};
|
|
160
|
+
let key = args.get(1).map(value_to_js).unwrap_or(JsValue::NULL);
|
|
161
|
+
let val = args.get(2).map(value_to_js).unwrap_or(JsValue::NULL);
|
|
162
|
+
let _ = js_sys::Reflect::set(&obj, &key, &val);
|
|
163
|
+
Value::Null
|
|
164
|
+
})
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
fn ffi_js_call() -> Value {
|
|
168
|
+
Value::native(|args: &[Value]| {
|
|
169
|
+
let obj = match args.first().and_then(unwrap_handle) {
|
|
170
|
+
Some(o) => o,
|
|
171
|
+
None => return Value::Null,
|
|
172
|
+
};
|
|
173
|
+
let method = match args.get(1) {
|
|
174
|
+
Some(Value::String(s)) => s.clone(),
|
|
175
|
+
_ => return Value::Null,
|
|
176
|
+
};
|
|
177
|
+
let func = match js_sys::Reflect::get(&obj, &JsValue::from_str(method.as_ref())) {
|
|
178
|
+
Ok(f) => match f.dyn_into::<js_sys::Function>() {
|
|
179
|
+
Ok(f) => f,
|
|
180
|
+
Err(_) => return Value::Null,
|
|
181
|
+
},
|
|
182
|
+
Err(_) => return Value::Null,
|
|
183
|
+
};
|
|
184
|
+
let js_args = js_sys::Array::new();
|
|
185
|
+
if let Some(Value::Array(a)) = args.get(2) {
|
|
186
|
+
for item in a.borrow().iter() {
|
|
187
|
+
js_args.push(&value_to_js(item));
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
match js_sys::Reflect::apply(&func, &obj, &js_args) {
|
|
191
|
+
Ok(v) => js_to_value(v),
|
|
192
|
+
Err(_) => Value::Null,
|
|
193
|
+
}
|
|
194
|
+
})
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
fn ffi_js_new() -> Value {
|
|
198
|
+
Value::native(|args: &[Value]| {
|
|
199
|
+
let ctor: JsValue = match args.first() {
|
|
200
|
+
Some(Value::String(s)) => {
|
|
201
|
+
match js_sys::Reflect::get(&js_sys::global(), &JsValue::from_str(s.as_ref())) {
|
|
202
|
+
Ok(v) => v,
|
|
203
|
+
Err(_) => return Value::Null,
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
Some(v @ Value::Opaque(_)) => match unwrap_handle(v) {
|
|
207
|
+
Some(h) => h,
|
|
208
|
+
None => return Value::Null,
|
|
209
|
+
},
|
|
210
|
+
_ => return Value::Null,
|
|
211
|
+
};
|
|
212
|
+
let ctor_fn = match ctor.dyn_into::<js_sys::Function>() {
|
|
213
|
+
Ok(f) => f,
|
|
214
|
+
Err(_) => return Value::Null,
|
|
215
|
+
};
|
|
216
|
+
let js_args = js_sys::Array::new();
|
|
217
|
+
if let Some(Value::Array(a)) = args.get(1) {
|
|
218
|
+
for item in a.borrow().iter() {
|
|
219
|
+
js_args.push(&value_to_js(item));
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
match js_sys::Reflect::construct(&ctor_fn, &js_args) {
|
|
223
|
+
Ok(v) => js_to_value(v),
|
|
224
|
+
Err(_) => Value::Null,
|
|
225
|
+
}
|
|
226
|
+
})
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
fn ffi_js_typeof() -> Value {
|
|
230
|
+
Value::native(|args: &[Value]| {
|
|
231
|
+
let v = args.first().map(value_to_js).unwrap_or(JsValue::NULL);
|
|
232
|
+
match v.js_typeof().as_string() {
|
|
233
|
+
Some(s) => Value::String(s.into()),
|
|
234
|
+
None => Value::Null,
|
|
235
|
+
}
|
|
236
|
+
})
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/// `f32a(numberArray)` -> opaque `Float32Array` handle (one-shot copy). Use for
|
|
240
|
+
/// per-frame uniform/transform staging. Large static buffers should instead be
|
|
241
|
+
/// materialised host-side and passed in opaque (never boxed into a tish array).
|
|
242
|
+
fn ffi_f32a() -> Value {
|
|
243
|
+
Value::native(|args: &[Value]| {
|
|
244
|
+
let arr = match args.first() {
|
|
245
|
+
Some(Value::Array(a)) => a.clone(),
|
|
246
|
+
_ => return Value::Null,
|
|
247
|
+
};
|
|
248
|
+
let b = arr.borrow();
|
|
249
|
+
let ta = js_sys::Float32Array::new_with_length(b.len() as u32);
|
|
250
|
+
for (i, v) in b.iter().enumerate() {
|
|
251
|
+
ta.set_index(i as u32, v.as_number().unwrap_or(0.0) as f32);
|
|
252
|
+
}
|
|
253
|
+
wrap(ta.into())
|
|
254
|
+
})
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
fn ffi_u16a() -> Value {
|
|
258
|
+
Value::native(|args: &[Value]| {
|
|
259
|
+
let arr = match args.first() {
|
|
260
|
+
Some(Value::Array(a)) => a.clone(),
|
|
261
|
+
_ => return Value::Null,
|
|
262
|
+
};
|
|
263
|
+
let b = arr.borrow();
|
|
264
|
+
let ta = js_sys::Uint16Array::new_with_length(b.len() as u32);
|
|
265
|
+
for (i, v) in b.iter().enumerate() {
|
|
266
|
+
ta.set_index(i as u32, v.as_number().unwrap_or(0.0) as u16);
|
|
267
|
+
}
|
|
268
|
+
wrap(ta.into())
|
|
269
|
+
})
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
fn ffi_u8a() -> Value {
|
|
273
|
+
Value::native(|args: &[Value]| {
|
|
274
|
+
let arr = match args.first() {
|
|
275
|
+
Some(Value::Array(a)) => a.clone(),
|
|
276
|
+
_ => return Value::Null,
|
|
277
|
+
};
|
|
278
|
+
let b = arr.borrow();
|
|
279
|
+
let ta = js_sys::Uint8Array::new_with_length(b.len() as u32);
|
|
280
|
+
for (i, v) in b.iter().enumerate() {
|
|
281
|
+
ta.set_index(i as u32, v.as_number().unwrap_or(0.0) as u8);
|
|
282
|
+
}
|
|
283
|
+
wrap(ta.into())
|
|
284
|
+
})
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// ---------------------------------------------------------------------------
|
|
288
|
+
// requestAnimationFrame render loop
|
|
289
|
+
// ---------------------------------------------------------------------------
|
|
290
|
+
|
|
291
|
+
thread_local! {
|
|
292
|
+
static RAF_CALLBACK: RefCell<Option<Value>> = const { RefCell::new(None) };
|
|
293
|
+
static RAF_CLOSURE: RefCell<Option<Closure<dyn FnMut(f64)>>> = const { RefCell::new(None) };
|
|
294
|
+
// True while a frame is pending, so repeated request_animation_frame calls
|
|
295
|
+
// within one frame don't compound into runaway scheduling.
|
|
296
|
+
static RAF_SCHEDULED: Cell<bool> = const { Cell::new(false) };
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/// Browser-driven per-frame entry: invoke the stored tish callback, then
|
|
300
|
+
/// re-arm for the next frame. We re-schedule from Rust (rather than requiring
|
|
301
|
+
/// the tish callback to call `request_animation_frame` again each frame) so the
|
|
302
|
+
/// loop runs continuously as long as a callback is registered. `cancel`-ing the
|
|
303
|
+
/// loop = clearing `RAF_CALLBACK`. The `value_call` runs the frame closure to
|
|
304
|
+
/// completion; all WebGPU recording happens synchronously inside.
|
|
305
|
+
fn tick(ts: f64) {
|
|
306
|
+
let cb = RAF_CALLBACK.with(|c| c.borrow().clone());
|
|
307
|
+
// This frame's pending schedule is now consumed.
|
|
308
|
+
RAF_SCHEDULED.with(|f| f.set(false));
|
|
309
|
+
if let Some(cb) = cb {
|
|
310
|
+
if matches!(cb, Value::Function(_)) {
|
|
311
|
+
value_call(&cb, &[Value::Number(ts)]);
|
|
312
|
+
}
|
|
313
|
+
// Re-arm only if the callback is still registered (allows a future
|
|
314
|
+
// cancel by clearing RAF_CALLBACK).
|
|
315
|
+
if RAF_CALLBACK.with(|c| c.borrow().is_some()) {
|
|
316
|
+
schedule_raf();
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
fn schedule_raf() {
|
|
322
|
+
// Coalesce: at most one rAF in flight at a time.
|
|
323
|
+
if RAF_SCHEDULED.with(|f| f.get()) {
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
RAF_SCHEDULED.with(|f| f.set(true));
|
|
327
|
+
RAF_CLOSURE.with(|slot| {
|
|
328
|
+
let mut s = slot.borrow_mut();
|
|
329
|
+
if s.is_none() {
|
|
330
|
+
*s = Some(Closure::wrap(Box::new(|ts: f64| tick(ts)) as Box<dyn FnMut(f64)>));
|
|
331
|
+
}
|
|
332
|
+
let g = js_sys::global();
|
|
333
|
+
if let Ok(raf) = js_sys::Reflect::get(&g, &JsValue::from_str("requestAnimationFrame")) {
|
|
334
|
+
if let Ok(raf_fn) = raf.dyn_into::<js_sys::Function>() {
|
|
335
|
+
let _ = raf_fn.call1(&g, s.as_ref().unwrap().as_ref().unchecked_ref());
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
fn ffi_request_animation_frame() -> Value {
|
|
342
|
+
Value::native(|args: &[Value]| {
|
|
343
|
+
if let Some(cb) = args.first() {
|
|
344
|
+
RAF_CALLBACK.with(|c| *c.borrow_mut() = Some(cb.clone()));
|
|
345
|
+
}
|
|
346
|
+
schedule_raf();
|
|
347
|
+
Value::Null
|
|
348
|
+
})
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// ---------------------------------------------------------------------------
|
|
352
|
+
// Install + entry point
|
|
353
|
+
// ---------------------------------------------------------------------------
|
|
354
|
+
|
|
355
|
+
fn install_ffi(vm: &mut Vm) {
|
|
356
|
+
vm.set_global("js_global".into(), ffi_js_global());
|
|
357
|
+
vm.set_global("js_get".into(), ffi_js_get());
|
|
358
|
+
vm.set_global("js_set".into(), ffi_js_set());
|
|
359
|
+
vm.set_global("js_call".into(), ffi_js_call());
|
|
360
|
+
vm.set_global("js_new".into(), ffi_js_new());
|
|
361
|
+
vm.set_global("js_typeof".into(), ffi_js_typeof());
|
|
362
|
+
vm.set_global("f32a".into(), ffi_f32a());
|
|
363
|
+
vm.set_global("u16a".into(), ffi_u16a());
|
|
364
|
+
vm.set_global("u8a".into(), ffi_u8a());
|
|
365
|
+
vm.set_global("request_animation_frame".into(), ffi_request_animation_frame());
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
#[wasm_bindgen]
|
|
369
|
+
extern "C" {
|
|
370
|
+
#[wasm_bindgen(js_namespace = console, js_name = error)]
|
|
371
|
+
fn console_error(s: &str);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
fn set_panic_hook() {
|
|
375
|
+
use std::sync::Once;
|
|
376
|
+
static HOOK: Once = Once::new();
|
|
377
|
+
HOOK.call_once(|| {
|
|
378
|
+
std::panic::set_hook(Box::new(|info| {
|
|
379
|
+
console_error(&format!("tish wasm panic: {}", info));
|
|
380
|
+
}));
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/// Browser entry: run a tish bytecode `chunk` with the JS-interop FFI installed
|
|
385
|
+
/// and the host environment object (device/queue/context/format/canvas/assets,
|
|
386
|
+
/// built by the page's async startup glue) exposed as the `host` global.
|
|
387
|
+
///
|
|
388
|
+
/// Returns after top-level tish runs; the `requestAnimationFrame` loop keeps
|
|
389
|
+
/// the captured globals alive via the stored callback, so the VM state persists
|
|
390
|
+
/// across frames even though this call returns.
|
|
391
|
+
/// Invoke the registered frame callback exactly once, without re-scheduling.
|
|
392
|
+
/// For driving frames deterministically from JS when `requestAnimationFrame` is
|
|
393
|
+
/// throttled (e.g. a hidden/offscreen preview tab) — verification & debugging.
|
|
394
|
+
#[wasm_bindgen]
|
|
395
|
+
pub fn tick_once(ts: f64) {
|
|
396
|
+
let cb = RAF_CALLBACK.with(|c| c.borrow().clone());
|
|
397
|
+
if let Some(cb) = cb {
|
|
398
|
+
if matches!(cb, Value::Function(_)) {
|
|
399
|
+
value_call(&cb, &[Value::Number(ts)]);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
#[wasm_bindgen]
|
|
405
|
+
pub fn start(chunk: Vec<u8>, env: JsValue) -> Result<(), JsValue> {
|
|
406
|
+
set_panic_hook();
|
|
407
|
+
let chunk = deserialize(&chunk).map_err(|e| JsValue::from_str(&e))?;
|
|
408
|
+
let mut vm = Vm::new();
|
|
409
|
+
install_ffi(&mut vm);
|
|
410
|
+
vm.set_global("host".into(), wrap(env));
|
|
411
|
+
vm.run(&chunk).map_err(|e| JsValue::from_str(&e))?;
|
|
412
|
+
Ok(())
|
|
413
|
+
}
|
|
@@ -8,6 +8,11 @@
|
|
|
8
8
|
use tishlang_bytecode::deserialize;
|
|
9
9
|
use tishlang_vm::Vm;
|
|
10
10
|
|
|
11
|
+
/// Browser WebGPU / JS-interop FFI + requestAnimationFrame render loop.
|
|
12
|
+
/// Adds the `start(chunk, env)` wasm-bindgen entry used by the engine.
|
|
13
|
+
#[cfg(feature = "gpu")]
|
|
14
|
+
pub mod gpu;
|
|
15
|
+
|
|
11
16
|
/// Run serialized Tish bytecode (WASI/Wasmtime or native).
|
|
12
17
|
///
|
|
13
18
|
/// `chunk` is the output of `tishlang_bytecode::serialize(chunk)`.
|
package/package.json
CHANGED
|
Binary file
|
package/platform/darwin-x64/tish
CHANGED
|
Binary file
|
|
Binary file
|
package/platform/linux-x64/tish
CHANGED
|
Binary file
|
|
Binary file
|