@tishlang/tish-format 1.0.12 → 2.0.1
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/Cargo.toml +51 -0
- package/LICENSE +13 -0
- package/bin/tish-format +0 -0
- package/crates/js_to_tish/Cargo.toml +11 -0
- package/crates/js_to_tish/README.md +18 -0
- package/crates/js_to_tish/src/error.rs +55 -0
- package/crates/js_to_tish/src/lib.rs +11 -0
- package/crates/js_to_tish/src/span_util.rs +35 -0
- package/crates/js_to_tish/src/transform/expr.rs +611 -0
- package/crates/js_to_tish/src/transform/stmt.rs +503 -0
- package/crates/js_to_tish/src/transform.rs +60 -0
- package/crates/tish/Cargo.toml +62 -0
- package/crates/tish/build.rs +21 -0
- package/crates/tish/src/cargo_native_registry.rs +32 -0
- package/crates/tish/src/cli_help.rs +576 -0
- package/crates/tish/src/main.rs +853 -0
- package/crates/tish/src/repl_completion.rs +199 -0
- package/crates/tish/tests/cargo_example_compile.rs +67 -0
- package/crates/tish/tests/error_source_location.rs +36 -0
- package/crates/tish/tests/fixtures/cargo_example_project/Cargo.toml +3 -0
- package/crates/tish/tests/fixtures/cargo_example_project/crates/demo-shim/Cargo.toml +11 -0
- package/crates/tish/tests/fixtures/cargo_example_project/crates/demo-shim/src/lib.rs +12 -0
- package/crates/tish/tests/fixtures/cargo_example_project/package.json +10 -0
- package/crates/tish/tests/fixtures/cargo_example_project/src/main.tish +3 -0
- package/crates/tish/tests/fixtures/runtime_error_location.tish +5 -0
- package/crates/tish/tests/fixtures/trycatch_runtime_errors.tish +15 -0
- package/crates/tish/tests/fixtures/tty_capability.tish +9 -0
- package/crates/tish/tests/integration_test.rs +1406 -0
- package/crates/tish/tests/run_optimize_stdout_parity.rs +50 -0
- package/crates/tish/tests/shortcircuit.rs +65 -0
- package/crates/tish/tests/trycatch_runtime_errors.rs +45 -0
- package/crates/tish/tests/tty_capability.rs +43 -0
- package/crates/tish_ast/Cargo.toml +9 -0
- package/crates/tish_ast/src/ast.rs +649 -0
- package/crates/tish_ast/src/lib.rs +5 -0
- package/crates/tish_build_utils/Cargo.toml +11 -0
- package/crates/tish_build_utils/src/lib.rs +577 -0
- package/crates/tish_builtins/Cargo.toml +22 -0
- package/crates/tish_builtins/src/array.rs +803 -0
- package/crates/tish_builtins/src/collections.rs +481 -0
- package/crates/tish_builtins/src/construct.rs +199 -0
- package/crates/tish_builtins/src/date.rs +538 -0
- package/crates/tish_builtins/src/globals.rs +293 -0
- package/crates/tish_builtins/src/helpers.rs +35 -0
- package/crates/tish_builtins/src/iterator.rs +129 -0
- package/crates/tish_builtins/src/lib.rs +21 -0
- package/crates/tish_builtins/src/math.rs +89 -0
- package/crates/tish_builtins/src/number.rs +96 -0
- package/crates/tish_builtins/src/object.rs +36 -0
- package/crates/tish_builtins/src/string.rs +646 -0
- package/crates/tish_builtins/src/symbol.rs +83 -0
- package/crates/tish_builtins/src/typedarrays.rs +298 -0
- package/crates/tish_bytecode/Cargo.toml +17 -0
- package/crates/tish_bytecode/src/chunk.rs +164 -0
- package/crates/tish_bytecode/src/compiler.rs +2604 -0
- package/crates/tish_bytecode/src/encoding.rs +102 -0
- package/crates/tish_bytecode/src/lib.rs +20 -0
- package/crates/tish_bytecode/src/opcode.rs +185 -0
- package/crates/tish_bytecode/src/peephole.rs +189 -0
- package/crates/tish_bytecode/src/serialize.rs +193 -0
- package/crates/tish_bytecode/tests/break_continue_bytecode.rs +44 -0
- package/crates/tish_bytecode/tests/constant_folding.rs +84 -0
- package/crates/tish_bytecode/tests/sort_optimization.rs +31 -0
- package/crates/tish_compile/Cargo.toml +27 -0
- package/crates/tish_compile/src/check.rs +774 -0
- package/crates/tish_compile/src/codegen.rs +7317 -0
- package/crates/tish_compile/src/infer.rs +1681 -0
- package/crates/tish_compile/src/lib.rs +206 -0
- package/crates/tish_compile/src/resolve.rs +1951 -0
- package/crates/tish_compile/src/types.rs +605 -0
- package/crates/tish_compile_js/Cargo.toml +18 -0
- package/crates/tish_compile_js/examples/jsx_vdom_smoke.tish +8 -0
- package/crates/tish_compile_js/src/codegen.rs +938 -0
- package/crates/tish_compile_js/src/error.rs +20 -0
- package/crates/tish_compile_js/src/lib.rs +26 -0
- package/crates/tish_compile_js/src/tests_jsx.rs +414 -0
- package/crates/tish_compiler_wasm/Cargo.toml +21 -0
- package/crates/tish_compiler_wasm/src/lib.rs +57 -0
- package/crates/tish_compiler_wasm/src/resolve_virtual.rs +473 -0
- package/crates/tish_core/Cargo.toml +32 -0
- package/crates/tish_core/src/console_style.rs +170 -0
- package/crates/tish_core/src/json.rs +430 -0
- package/crates/tish_core/src/lib.rs +20 -0
- package/crates/tish_core/src/macros.rs +36 -0
- package/crates/tish_core/src/shape.rs +85 -0
- package/crates/tish_core/src/uri.rs +118 -0
- package/crates/tish_core/src/value.rs +1350 -0
- package/crates/tish_core/src/vmref.rs +183 -0
- package/crates/tish_cranelift/Cargo.toml +19 -0
- package/crates/tish_cranelift/src/lib.rs +43 -0
- package/crates/tish_cranelift/src/link.rs +130 -0
- package/crates/tish_cranelift/src/lower.rs +85 -0
- package/crates/tish_cranelift_runtime/Cargo.toml +26 -0
- package/crates/tish_cranelift_runtime/src/lib.rs +45 -0
- package/crates/tish_eval/Cargo.toml +51 -0
- package/crates/tish_eval/src/eval.rs +4265 -0
- package/crates/tish_eval/src/http.rs +191 -0
- package/crates/tish_eval/src/lib.rs +99 -0
- package/crates/tish_eval/src/natives.rs +551 -0
- package/crates/tish_eval/src/promise.rs +179 -0
- package/crates/tish_eval/src/regex.rs +299 -0
- package/crates/tish_eval/src/timers.rs +120 -0
- package/crates/tish_eval/src/value.rs +336 -0
- package/crates/tish_eval/src/value_convert.rs +117 -0
- package/crates/tish_ffi/Cargo.toml +26 -0
- package/crates/tish_ffi/src/lib.rs +518 -0
- package/crates/tish_ffi/tests/fixtures/testmod/Cargo.toml +18 -0
- package/crates/tish_ffi/tests/fixtures/testmod/src/lib.rs +46 -0
- package/crates/tish_ffi/tests/loader.rs +65 -0
- package/crates/tish_fmt/Cargo.toml +16 -0
- package/crates/tish_fmt/src/bin/tish-fmt.rs +41 -0
- package/crates/tish_fmt/src/lib.rs +2157 -0
- package/crates/tish_jsx_web/Cargo.toml +9 -0
- package/crates/tish_jsx_web/README.md +5 -0
- package/crates/tish_jsx_web/src/lib.rs +2 -0
- package/crates/tish_lexer/Cargo.toml +9 -0
- package/crates/tish_lexer/src/lib.rs +1104 -0
- package/crates/tish_lexer/src/token.rs +170 -0
- package/crates/tish_lint/Cargo.toml +18 -0
- package/crates/tish_lint/src/bin/tish-lint.rs +195 -0
- package/crates/tish_lint/src/lib.rs +281 -0
- package/crates/tish_llvm/Cargo.toml +13 -0
- package/crates/tish_llvm/src/lib.rs +115 -0
- package/crates/tish_lsp/Cargo.toml +25 -0
- package/crates/tish_lsp/README.md +26 -0
- package/crates/tish_lsp/src/builtin_goto.rs +362 -0
- package/crates/tish_lsp/src/import_goto.rs +564 -0
- package/crates/tish_lsp/src/main.rs +1459 -0
- package/crates/tish_native/Cargo.toml +16 -0
- package/crates/tish_native/src/build.rs +481 -0
- package/crates/tish_native/src/config.rs +48 -0
- package/crates/tish_native/src/lib.rs +416 -0
- package/crates/tish_opt/Cargo.toml +13 -0
- package/crates/tish_opt/src/lib.rs +1046 -0
- package/crates/tish_parser/Cargo.toml +11 -0
- package/crates/tish_parser/src/lib.rs +386 -0
- package/crates/tish_parser/src/parser.rs +2726 -0
- package/crates/tish_pg/Cargo.toml +34 -0
- package/crates/tish_pg/README.md +38 -0
- package/crates/tish_pg/src/error.rs +52 -0
- package/crates/tish_pg/src/lib.rs +955 -0
- package/crates/tish_resolve/Cargo.toml +13 -0
- package/crates/tish_resolve/src/lib.rs +3601 -0
- package/crates/tish_resolve/src/pos.rs +141 -0
- package/crates/tish_runtime/Cargo.toml +100 -0
- package/crates/tish_runtime/src/http.rs +1347 -0
- package/crates/tish_runtime/src/http_fetch.rs +492 -0
- package/crates/tish_runtime/src/http_hyper.rs +441 -0
- package/crates/tish_runtime/src/http_prefork.rs +189 -0
- package/crates/tish_runtime/src/lib.rs +1447 -0
- package/crates/tish_runtime/src/native_promise.rs +15 -0
- package/crates/tish_runtime/src/promise.rs +558 -0
- package/crates/tish_runtime/src/promise_io.rs +38 -0
- package/crates/tish_runtime/src/timers.rs +172 -0
- package/crates/tish_runtime/src/tty.rs +226 -0
- package/crates/tish_runtime/src/ws.rs +778 -0
- package/crates/tish_runtime/tests/fetch_readable_stream.rs +102 -0
- package/crates/tish_ui/Cargo.toml +17 -0
- package/crates/tish_ui/src/jsx.rs +692 -0
- package/crates/tish_ui/src/lib.rs +20 -0
- package/crates/tish_ui/src/runtime/hooks.rs +573 -0
- package/crates/tish_ui/src/runtime/mod.rs +183 -0
- package/crates/tish_vm/Cargo.toml +60 -0
- package/crates/tish_vm/src/jit.rs +1050 -0
- package/crates/tish_vm/src/lib.rs +41 -0
- package/crates/tish_vm/src/vm.rs +3536 -0
- package/crates/tish_vm/tests/concurrent_shared_state.rs +140 -0
- package/crates/tish_vm/tests/fixtures/or_string_cmd.tish +2 -0
- package/crates/tish_vm/tests/lexical_scope_declare.rs +34 -0
- package/crates/tish_vm/tests/peephole_jump_chain_logical_or.rs +150 -0
- package/crates/tish_wasm/Cargo.toml +15 -0
- package/crates/tish_wasm/src/lib.rs +428 -0
- package/crates/tish_wasm_runtime/Cargo.toml +37 -0
- package/crates/tish_wasm_runtime/src/gpu.rs +429 -0
- package/crates/tish_wasm_runtime/src/lib.rs +42 -0
- package/crates/tishlang_cargo_bindgen/Cargo.toml +26 -0
- package/crates/tishlang_cargo_bindgen/src/classify.rs +261 -0
- package/crates/tishlang_cargo_bindgen/src/discover.rs +125 -0
- package/crates/tishlang_cargo_bindgen/src/infer.rs +382 -0
- package/crates/tishlang_cargo_bindgen/src/lib.rs +349 -0
- package/crates/tishlang_cargo_bindgen/src/main.rs +167 -0
- package/crates/tishlang_cargo_bindgen/src/metadata.rs +117 -0
- package/justfile +276 -0
- package/package.json +2 -2
- package/platform/darwin-arm64/tish-fmt +0 -0
- package/platform/darwin-x64/tish-fmt +0 -0
- package/platform/linux-arm64/tish-fmt +0 -0
- package/platform/linux-x64/tish-fmt +0 -0
- package/platform/win32-x64/tish-fmt.exe +0 -0
|
@@ -0,0 +1,429 @@
|
|
|
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)` / `u32a(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
|
+
fn ffi_u32a() -> Value {
|
|
288
|
+
Value::native(|args: &[Value]| {
|
|
289
|
+
let arr = match args.first() {
|
|
290
|
+
Some(Value::Array(a)) => a.clone(),
|
|
291
|
+
_ => return Value::Null,
|
|
292
|
+
};
|
|
293
|
+
let b = arr.borrow();
|
|
294
|
+
let ta = js_sys::Uint32Array::new_with_length(b.len() as u32);
|
|
295
|
+
for (i, v) in b.iter().enumerate() {
|
|
296
|
+
ta.set_index(i as u32, v.as_number().unwrap_or(0.0) as u32);
|
|
297
|
+
}
|
|
298
|
+
wrap(ta.into())
|
|
299
|
+
})
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// ---------------------------------------------------------------------------
|
|
303
|
+
// requestAnimationFrame render loop
|
|
304
|
+
// ---------------------------------------------------------------------------
|
|
305
|
+
|
|
306
|
+
thread_local! {
|
|
307
|
+
static RAF_CALLBACK: RefCell<Option<Value>> = const { RefCell::new(None) };
|
|
308
|
+
static RAF_CLOSURE: RefCell<Option<Closure<dyn FnMut(f64)>>> = const { RefCell::new(None) };
|
|
309
|
+
// True while a frame is pending, so repeated request_animation_frame calls
|
|
310
|
+
// within one frame don't compound into runaway scheduling.
|
|
311
|
+
static RAF_SCHEDULED: Cell<bool> = const { Cell::new(false) };
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/// Browser-driven per-frame entry: invoke the stored tish callback, then
|
|
315
|
+
/// re-arm for the next frame. We re-schedule from Rust (rather than requiring
|
|
316
|
+
/// the tish callback to call `request_animation_frame` again each frame) so the
|
|
317
|
+
/// loop runs continuously as long as a callback is registered. `cancel`-ing the
|
|
318
|
+
/// loop = clearing `RAF_CALLBACK`. The `value_call` runs the frame closure to
|
|
319
|
+
/// completion; all WebGPU recording happens synchronously inside.
|
|
320
|
+
fn tick(ts: f64) {
|
|
321
|
+
let cb = RAF_CALLBACK.with(|c| c.borrow().clone());
|
|
322
|
+
// This frame's pending schedule is now consumed.
|
|
323
|
+
RAF_SCHEDULED.with(|f| f.set(false));
|
|
324
|
+
if let Some(cb) = cb {
|
|
325
|
+
if matches!(cb, Value::Function(_)) {
|
|
326
|
+
value_call(&cb, &[Value::Number(ts)]);
|
|
327
|
+
}
|
|
328
|
+
// Re-arm only if the callback is still registered (allows a future
|
|
329
|
+
// cancel by clearing RAF_CALLBACK).
|
|
330
|
+
if RAF_CALLBACK.with(|c| c.borrow().is_some()) {
|
|
331
|
+
schedule_raf();
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
fn schedule_raf() {
|
|
337
|
+
// Coalesce: at most one rAF in flight at a time.
|
|
338
|
+
if RAF_SCHEDULED.with(|f| f.get()) {
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
RAF_SCHEDULED.with(|f| f.set(true));
|
|
342
|
+
RAF_CLOSURE.with(|slot| {
|
|
343
|
+
let mut s = slot.borrow_mut();
|
|
344
|
+
if s.is_none() {
|
|
345
|
+
*s = Some(Closure::wrap(Box::new(|ts: f64| tick(ts)) as Box<dyn FnMut(f64)>));
|
|
346
|
+
}
|
|
347
|
+
let g = js_sys::global();
|
|
348
|
+
if let Ok(raf) = js_sys::Reflect::get(&g, &JsValue::from_str("requestAnimationFrame")) {
|
|
349
|
+
if let Ok(raf_fn) = raf.dyn_into::<js_sys::Function>() {
|
|
350
|
+
let _ = raf_fn.call1(&g, s.as_ref().unwrap().as_ref().unchecked_ref());
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
fn ffi_request_animation_frame() -> Value {
|
|
357
|
+
Value::native(|args: &[Value]| {
|
|
358
|
+
if let Some(cb) = args.first() {
|
|
359
|
+
RAF_CALLBACK.with(|c| *c.borrow_mut() = Some(cb.clone()));
|
|
360
|
+
}
|
|
361
|
+
schedule_raf();
|
|
362
|
+
Value::Null
|
|
363
|
+
})
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// ---------------------------------------------------------------------------
|
|
367
|
+
// Install + entry point
|
|
368
|
+
// ---------------------------------------------------------------------------
|
|
369
|
+
|
|
370
|
+
fn install_ffi(vm: &mut Vm) {
|
|
371
|
+
vm.set_global("js_global".into(), ffi_js_global());
|
|
372
|
+
vm.set_global("js_get".into(), ffi_js_get());
|
|
373
|
+
vm.set_global("js_set".into(), ffi_js_set());
|
|
374
|
+
vm.set_global("js_call".into(), ffi_js_call());
|
|
375
|
+
vm.set_global("js_new".into(), ffi_js_new());
|
|
376
|
+
vm.set_global("js_typeof".into(), ffi_js_typeof());
|
|
377
|
+
vm.set_global("f32a".into(), ffi_f32a());
|
|
378
|
+
vm.set_global("u16a".into(), ffi_u16a());
|
|
379
|
+
vm.set_global("u8a".into(), ffi_u8a());
|
|
380
|
+
vm.set_global("u32a".into(), ffi_u32a());
|
|
381
|
+
vm.set_global("request_animation_frame".into(), ffi_request_animation_frame());
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
#[wasm_bindgen]
|
|
385
|
+
extern "C" {
|
|
386
|
+
#[wasm_bindgen(js_namespace = console, js_name = error)]
|
|
387
|
+
fn console_error(s: &str);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
fn set_panic_hook() {
|
|
391
|
+
use std::sync::Once;
|
|
392
|
+
static HOOK: Once = Once::new();
|
|
393
|
+
HOOK.call_once(|| {
|
|
394
|
+
std::panic::set_hook(Box::new(|info| {
|
|
395
|
+
console_error(&format!("tish wasm panic: {}", info));
|
|
396
|
+
}));
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/// Browser entry: run a tish bytecode `chunk` with the JS-interop FFI installed
|
|
401
|
+
/// and the host environment object (device/queue/context/format/canvas/assets,
|
|
402
|
+
/// built by the page's async startup glue) exposed as the `host` global.
|
|
403
|
+
///
|
|
404
|
+
/// Returns after top-level tish runs; the `requestAnimationFrame` loop keeps
|
|
405
|
+
/// the captured globals alive via the stored callback, so the VM state persists
|
|
406
|
+
/// across frames even though this call returns.
|
|
407
|
+
/// Invoke the registered frame callback exactly once, without re-scheduling.
|
|
408
|
+
/// For driving frames deterministically from JS when `requestAnimationFrame` is
|
|
409
|
+
/// throttled (e.g. a hidden/offscreen preview tab) — verification & debugging.
|
|
410
|
+
#[wasm_bindgen]
|
|
411
|
+
pub fn tick_once(ts: f64) {
|
|
412
|
+
let cb = RAF_CALLBACK.with(|c| c.borrow().clone());
|
|
413
|
+
if let Some(cb) = cb {
|
|
414
|
+
if matches!(cb, Value::Function(_)) {
|
|
415
|
+
value_call(&cb, &[Value::Number(ts)]);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
#[wasm_bindgen]
|
|
421
|
+
pub fn start(chunk: Vec<u8>, env: JsValue) -> Result<(), JsValue> {
|
|
422
|
+
set_panic_hook();
|
|
423
|
+
let chunk = deserialize(&chunk).map_err(|e| JsValue::from_str(&e))?;
|
|
424
|
+
let mut vm = Vm::new();
|
|
425
|
+
install_ffi(&mut vm);
|
|
426
|
+
vm.set_global("host".into(), wrap(env));
|
|
427
|
+
vm.run(&chunk).map_err(|e| JsValue::from_str(&e))?;
|
|
428
|
+
Ok(())
|
|
429
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
//! Tish VM as a WebAssembly module.
|
|
2
|
+
//!
|
|
3
|
+
//! Two targets:
|
|
4
|
+
//! - **Browser** (wasm32-unknown-unknown): use `--features browser`, wasm-bindgen, console output
|
|
5
|
+
//! - **WASI/Wasmtime** (wasm32-wasip1): optional `timers` / `http` / … via Cargo features; `compile_to_wasi`
|
|
6
|
+
//! merges CLI capability flags with imports and always enables `timers` when globals use `setTimeout`.
|
|
7
|
+
|
|
8
|
+
use tishlang_bytecode::deserialize;
|
|
9
|
+
use tishlang_vm::Vm;
|
|
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
|
+
|
|
16
|
+
/// Run serialized Tish bytecode (WASI/Wasmtime or native).
|
|
17
|
+
///
|
|
18
|
+
/// `chunk` is the output of `tishlang_bytecode::serialize(chunk)`.
|
|
19
|
+
/// Uses println! for output (WASI fd_write when built for wasm32-wasi).
|
|
20
|
+
#[cfg(not(feature = "browser"))]
|
|
21
|
+
pub fn run_wasi(chunk: &[u8]) -> Result<(), String> {
|
|
22
|
+
let chunk = deserialize(chunk)?;
|
|
23
|
+
let mut vm = Vm::new();
|
|
24
|
+
vm.run(&chunk)?;
|
|
25
|
+
Ok(())
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/// Run serialized Tish bytecode in the browser.
|
|
29
|
+
///
|
|
30
|
+
/// `chunk` is the output of `tishlang_bytecode::serialize(chunk)`.
|
|
31
|
+
/// Errors are returned as a JsValue (string).
|
|
32
|
+
#[cfg(feature = "browser")]
|
|
33
|
+
use wasm_bindgen::prelude::*;
|
|
34
|
+
|
|
35
|
+
#[cfg(feature = "browser")]
|
|
36
|
+
#[wasm_bindgen]
|
|
37
|
+
pub fn run(chunk: Vec<u8>) -> Result<(), JsValue> {
|
|
38
|
+
let chunk = deserialize(&chunk).map_err(|e| JsValue::from_str(&e))?;
|
|
39
|
+
let mut vm = Vm::new();
|
|
40
|
+
vm.run(&chunk).map_err(|e| JsValue::from_str(&e))?;
|
|
41
|
+
Ok(())
|
|
42
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "tishlang_cargo_bindgen"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
edition = "2021"
|
|
5
|
+
description = "Generate Rust glue for Tish cargo: imports (bindgen-style, pre-commit friendly)"
|
|
6
|
+
license-file = { workspace = true }
|
|
7
|
+
repository = { workspace = true }
|
|
8
|
+
|
|
9
|
+
[[bin]]
|
|
10
|
+
name = "tishlang-cargo-bindgen"
|
|
11
|
+
path = "src/main.rs"
|
|
12
|
+
|
|
13
|
+
[lib]
|
|
14
|
+
name = "tishlang_cargo_bindgen"
|
|
15
|
+
path = "src/lib.rs"
|
|
16
|
+
|
|
17
|
+
[dependencies]
|
|
18
|
+
cargo_metadata = "0.19"
|
|
19
|
+
clap = { version = "4.6", features = ["derive", "color"] }
|
|
20
|
+
pathdiff = "0.2"
|
|
21
|
+
proc-macro2 = { version = "1.0", features = ["span-locations"] }
|
|
22
|
+
serde_json = "1"
|
|
23
|
+
syn = { version = "2.0", features = ["full", "extra-traits"] }
|
|
24
|
+
tempfile = "3"
|
|
25
|
+
toml = "0.8"
|
|
26
|
+
walkdir = "2"
|