@holoscript/core 7.0.0 → 8.0.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/README.md +2 -2
- package/bin/holoscript.cjs +3 -0
- package/dist/GLTFPipeline-276GE64O.js +16 -0
- package/dist/GLTFPipeline-M7WSSJED.cjs +37 -0
- package/dist/HoloScriptPlusParser-7FBIFUFC.js +693 -0
- package/dist/HoloScriptPlusParser-A2W7OCN2.cjs +698 -0
- package/dist/HoloScriptPlusParser-F2Z56FJR.js +10 -0
- package/dist/HoloScriptPlusParser-WKHRIH6T.cjs +27 -0
- package/dist/HoloScriptRuntime-3URVTKPJ.js +10 -0
- package/dist/HoloScriptRuntime-SXEAEU2N.cjs +19 -0
- package/dist/USDZExporter-3FWJ25JI.cjs +1037 -0
- package/dist/USDZExporter-D2HQLQYK.js +1035 -0
- package/dist/agents/index.cjs +19 -0
- package/dist/agents/index.d.ts +100 -0
- package/dist/agents/index.js +17 -0
- package/dist/chunk-23J755AR.js +1046 -0
- package/dist/chunk-253GBQZY.cjs +627 -0
- package/dist/chunk-25IQCHTO.cjs +781 -0
- package/dist/chunk-2BSMSUCE.js +1941 -0
- package/dist/chunk-2M2DPHKB.js +762 -0
- package/dist/chunk-2YSNA2F2.cjs +1274 -0
- package/dist/chunk-3AO425MZ.cjs +886 -0
- package/dist/chunk-3FOKASKO.cjs +348 -0
- package/dist/chunk-3HYYMEXI.cjs +56578 -0
- package/dist/chunk-3NFFOWGG.js +4287 -0
- package/dist/chunk-3WZEJHUQ.cjs +116 -0
- package/dist/chunk-4C57PR42.js +468 -0
- package/dist/chunk-4N4SXXWF.js +769 -0
- package/dist/chunk-4PDEPEQW.cjs +41 -0
- package/dist/chunk-4R6SBXDB.js +45 -0
- package/dist/chunk-4V2E7U7Q.cjs +2015 -0
- package/dist/chunk-5FSKKMR7.cjs +2092 -0
- package/dist/chunk-5GLCTO44.js +161 -0
- package/dist/chunk-5JEU52DW.js +382 -0
- package/dist/chunk-5S7RKFGR.cjs +446 -0
- package/dist/chunk-5WUNN6ZN.cjs +428 -0
- package/dist/chunk-62FBK3B7.js +70 -0
- package/dist/chunk-6CTRH7HT.cjs +1985 -0
- package/dist/chunk-6DPSFNJB.cjs +847 -0
- package/dist/chunk-6EX3I5P4.cjs +130 -0
- package/dist/chunk-73FELZVJ.cjs +611 -0
- package/dist/chunk-7CUEMLDZ.cjs +6445 -0
- package/dist/chunk-7HIASF7S.cjs +814 -0
- package/dist/chunk-A6KINLZJ.js +236 -0
- package/dist/chunk-AIA52I7T.cjs +543 -0
- package/dist/chunk-ANXZFVY2.js +113 -0
- package/dist/chunk-AYBXB26F.js +2176 -0
- package/dist/chunk-B4TFTDG4.js +449 -0
- package/dist/chunk-BE4UJCQT.js +623 -0
- package/dist/chunk-BRQJKJGT.cjs +20 -0
- package/dist/chunk-C2A5DUMF.cjs +689 -0
- package/dist/chunk-C37BHB7N.cjs +636 -0
- package/dist/chunk-CBJY23VL.js +126 -0
- package/dist/chunk-CIWWXYHZ.cjs +50 -0
- package/dist/chunk-CXFDRBQU.cjs +672 -0
- package/dist/chunk-D54CNIAJ.cjs +413 -0
- package/dist/chunk-DAJXW4KX.js +725 -0
- package/dist/chunk-DTGBOSEM.cjs +319 -0
- package/dist/chunk-E7JP3BS3.cjs +61 -0
- package/dist/chunk-EKBKLK7P.js +1169 -0
- package/dist/chunk-EKYL4PE3.cjs +404 -0
- package/dist/chunk-EUDQTK7N.cjs +4310 -0
- package/dist/chunk-EXKSFGSX.cjs +274 -0
- package/dist/chunk-EXUP2BUM.cjs +1173 -0
- package/dist/chunk-F43YAA34.js +623 -0
- package/dist/chunk-FDSGHPAK.js +130 -0
- package/dist/chunk-FEWUHQNM.cjs +144 -0
- package/dist/chunk-FGGJQRLZ.js +270 -0
- package/dist/chunk-FHP6LXJS.js +415 -0
- package/dist/chunk-FJPADH77.cjs +666 -0
- package/dist/chunk-FYSPZT4V.cjs +169 -0
- package/dist/chunk-GIOYPNTH.js +5575 -0
- package/dist/chunk-GJNCFRJU.cjs +125 -0
- package/dist/chunk-GMP623FG.cjs +1508 -0
- package/dist/chunk-GQHQOPXZ.cjs +5580 -0
- package/dist/chunk-GQT4TYKY.cjs +4381 -0
- package/dist/chunk-GTU2Z2WW.cjs +262 -0
- package/dist/chunk-GYZE55MO.js +2007 -0
- package/dist/chunk-GZG4KA4E.js +529 -0
- package/dist/chunk-H652NHX5.js +600 -0
- package/dist/chunk-HCCQLAA4.cjs +74 -0
- package/dist/chunk-HINXWAOD.cjs +1049 -0
- package/dist/chunk-HYNTS5CP.js +422 -0
- package/dist/chunk-HZSPDCXV.cjs +457 -0
- package/dist/chunk-HZTDET3N.js +1399 -0
- package/dist/chunk-I6XZ5DZF.cjs +488 -0
- package/dist/chunk-ILRBGFVD.cjs +72 -0
- package/dist/chunk-IPWSXCCN.js +1183 -0
- package/dist/chunk-IUTY5WQ2.cjs +424 -0
- package/dist/chunk-IVSZDECV.js +882 -0
- package/dist/chunk-IVY74O6C.js +247 -0
- package/dist/chunk-IZM5VQZD.js +714 -0
- package/dist/chunk-J32ZJZJH.js +554 -0
- package/dist/chunk-J6M4FPK5.js +421 -0
- package/dist/chunk-JC5YBPSF.cjs +984 -0
- package/dist/chunk-JCLBKPMX.js +3813 -0
- package/dist/chunk-JD7CIZ33.js +1949 -0
- package/dist/chunk-JIYN45TV.cjs +2525 -0
- package/dist/chunk-JJBE3TIH.cjs +1189 -0
- package/dist/chunk-JX7YL6ZE.js +2252 -0
- package/dist/chunk-K4VVR7CN.cjs +2433 -0
- package/dist/chunk-KEFNV2WM.cjs +2257 -0
- package/dist/chunk-KH4CNH66.cjs +424 -0
- package/dist/chunk-KJLB4OPV.js +5731 -0
- package/dist/chunk-KKFZ3B6J.cjs +557 -0
- package/dist/chunk-KKP7JKN4.cjs +6002 -0
- package/dist/chunk-KPJL34Y7.cjs +186 -0
- package/dist/chunk-KQCN6AKJ.cjs +3042 -0
- package/dist/chunk-KVRSTFNH.cjs +72 -0
- package/dist/chunk-LICPB5HZ.cjs +1409 -0
- package/dist/chunk-LM2VU56C.cjs +37 -0
- package/dist/chunk-M56JSP4A.js +400 -0
- package/dist/chunk-MBOOR7X4.js +18 -0
- package/dist/chunk-MGE5Z4GS.js +608 -0
- package/dist/chunk-MXFUTKCO.js +56 -0
- package/dist/chunk-MYY4PMWX.cjs +264 -0
- package/dist/chunk-N75IH2HY.js +258 -0
- package/dist/chunk-NBMHU6SW.js +669 -0
- package/dist/chunk-NG77ATEW.cjs +778 -0
- package/dist/chunk-NGKREZ36.js +6384 -0
- package/dist/chunk-NHWSN2SD.cjs +718 -0
- package/dist/chunk-NV4VMVXU.js +421 -0
- package/dist/chunk-NW5G5FBQ.js +215 -0
- package/dist/chunk-NXZFOPVD.cjs +5737 -0
- package/dist/chunk-OBIYPYYS.cjs +1993 -0
- package/dist/chunk-OIQYIIO3.cjs +473 -0
- package/dist/chunk-OL46QLBJ.js +61 -0
- package/dist/chunk-OMXII44L.js +5386 -0
- package/dist/chunk-OR4Q2BGL.cjs +759 -0
- package/dist/chunk-OSYNRVOV.cjs +243 -0
- package/dist/chunk-PGANYCAG.cjs +220 -0
- package/dist/chunk-PHENW4MQ.js +65 -0
- package/dist/chunk-PHVRG4I2.js +844 -0
- package/dist/chunk-Q56TT7IL.js +483 -0
- package/dist/chunk-QPOQ7VPI.js +1491 -0
- package/dist/chunk-QPSKHBJP.js +5963 -0
- package/dist/chunk-QXH7QDSZ.js +1989 -0
- package/dist/chunk-R75BSDH7.cjs +50 -0
- package/dist/chunk-RARB3MBR.js +207 -0
- package/dist/chunk-RVBHAXFG.js +56160 -0
- package/dist/chunk-S3MDHNKB.cjs +490 -0
- package/dist/chunk-S4W6Z2MN.js +2426 -0
- package/dist/chunk-S6YWHCKV.cjs +410 -0
- package/dist/chunk-SIEXCILW.js +229 -0
- package/dist/chunk-SJBGLS25.cjs +2196 -0
- package/dist/chunk-SLU6UPJI.cjs +410 -0
- package/dist/chunk-SMIWS6DE.cjs +212 -0
- package/dist/chunk-T2QCTZMO.js +811 -0
- package/dist/chunk-T5J7ZKP3.cjs +4885 -0
- package/dist/chunk-TDCNJHWO.js +341 -0
- package/dist/chunk-TG7O4WBC.js +406 -0
- package/dist/chunk-THCK6ECW.js +2085 -0
- package/dist/chunk-TJHVSBP2.js +439 -0
- package/dist/chunk-TVHSPJ4N.cjs +606 -0
- package/dist/chunk-U44IKGG7.cjs +5390 -0
- package/dist/chunk-UJXISJBN.js +487 -0
- package/dist/chunk-UM6C3CGK.js +4375 -0
- package/dist/chunk-UMABK6SI.js +686 -0
- package/dist/chunk-UOIGFQXC.cjs +9750 -0
- package/dist/chunk-URG2SJLK.js +2521 -0
- package/dist/chunk-V6MKC2FZ.js +979 -0
- package/dist/chunk-VECQYRXL.js +474 -0
- package/dist/chunk-VJ3L7CHT.cjs +169 -0
- package/dist/chunk-VMLKSV26.js +1216 -0
- package/dist/chunk-VVKVQZI6.js +55 -0
- package/dist/chunk-W6RW64BB.cjs +1219 -0
- package/dist/chunk-W7VDOASB.cjs +639 -0
- package/dist/chunk-WDT3JTGM.cjs +483 -0
- package/dist/chunk-WJIHFLMJ.js +404 -0
- package/dist/chunk-WKX5AMOY.js +309 -0
- package/dist/chunk-WNNE3PO3.js +34 -0
- package/dist/chunk-WRKUDAZJ.js +3038 -0
- package/dist/chunk-WRLUBRKS.cjs +1943 -0
- package/dist/chunk-WRNEH5MO.js +3169 -0
- package/dist/chunk-WRWEX5ZF.js +658 -0
- package/dist/chunk-X3GJPRVK.cjs +3246 -0
- package/dist/chunk-XHIV6326.cjs +793 -0
- package/dist/chunk-XIVIE43A.cjs +729 -0
- package/dist/chunk-XJAN7CZG.cjs +432 -0
- package/dist/chunk-XKTPFXGA.js +164 -0
- package/dist/chunk-XL6INWKT.js +121 -0
- package/dist/chunk-XSFNHP2E.js +632 -0
- package/dist/chunk-XT6JY5W5.js +9684 -0
- package/dist/chunk-XUILI4KB.cjs +430 -0
- package/dist/chunk-XUUVDHIF.cjs +3962 -0
- package/dist/chunk-YF2I3MV3.js +46 -0
- package/dist/chunk-YIMN575O.js +4847 -0
- package/dist/chunk-YIVFJNYV.js +176 -0
- package/dist/chunk-YKGPCZEE.cjs +237 -0
- package/dist/chunk-YN6V7R3L.cjs +327 -0
- package/dist/chunk-YQELFNJV.js +39 -0
- package/dist/chunk-YYXH45E2.js +426 -0
- package/dist/chunk-ZBQXLH2L.js +756 -0
- package/dist/chunk-ZDSYWBIE.cjs +69 -0
- package/dist/chunk-ZU7NIRT6.js +325 -0
- package/dist/chunk-ZZ7RO2UQ.js +1243 -0
- package/dist/chunk-ZZEI7MV4.js +778 -0
- package/dist/cli/holoscript-runner.cjs +2255 -0
- package/dist/cli/holoscript-runner.js +2230 -0
- package/dist/codebase/index.cjs +336 -0
- package/dist/codebase/index.js +331 -0
- package/dist/compiler/agent-inference.cjs +31 -0
- package/dist/compiler/agent-inference.js +8 -0
- package/dist/compiler/android-xr.cjs +24 -0
- package/dist/compiler/android-xr.js +14 -0
- package/dist/compiler/android.cjs +24 -0
- package/dist/compiler/android.js +14 -0
- package/dist/compiler/ar.cjs +23 -0
- package/dist/compiler/ar.js +11 -0
- package/dist/compiler/babylon.cjs +21 -0
- package/dist/compiler/babylon.js +15 -0
- package/dist/compiler/business-quest.cjs +43 -0
- package/dist/compiler/business-quest.js +4 -0
- package/dist/compiler/coco.cjs +27 -0
- package/dist/compiler/coco.js +4 -0
- package/dist/compiler/context.cjs +25 -0
- package/dist/compiler/context.d.ts +28 -0
- package/dist/compiler/context.js +8 -0
- package/dist/compiler/domain-block-utils.cjs +605 -0
- package/dist/compiler/domain-block-utils.js +11 -0
- package/dist/compiler/dtdl.cjs +27 -0
- package/dist/compiler/dtdl.js +11 -0
- package/dist/compiler/gltf-pipeline.cjs +39 -0
- package/dist/compiler/gltf-pipeline.js +4 -0
- package/dist/compiler/godot.cjs +18 -0
- package/dist/compiler/godot.js +12 -0
- package/dist/compiler/incremental.cjs +34 -0
- package/dist/compiler/incremental.js +9 -0
- package/dist/compiler/index.cjs +5860 -0
- package/dist/compiler/index.d.ts +322 -21
- package/dist/compiler/index.js +5525 -0
- package/dist/compiler/ios.cjs +23 -0
- package/dist/compiler/ios.js +13 -0
- package/dist/compiler/llm-provider-capabilities.cjs +25 -0
- package/dist/compiler/llm-provider-capabilities.d.ts +43 -0
- package/dist/compiler/llm-provider-capabilities.js +8 -0
- package/dist/compiler/multi-layer.cjs +24 -0
- package/dist/compiler/multi-layer.js +15 -0
- package/dist/compiler/nodetoy.cjs +23 -0
- package/dist/compiler/nodetoy.js +4 -0
- package/dist/compiler/openxr.cjs +19 -0
- package/dist/compiler/openxr.js +13 -0
- package/dist/compiler/playcanvas.cjs +19 -0
- package/dist/compiler/playcanvas.js +13 -0
- package/dist/compiler/r3f.cjs +38 -0
- package/dist/compiler/r3f.d.ts +22 -0
- package/dist/compiler/r3f.js +16 -0
- package/dist/compiler/remotion.cjs +23 -0
- package/dist/compiler/remotion.js +4 -0
- package/dist/compiler/reproducibility.cjs +39 -0
- package/dist/compiler/reproducibility.js +4 -0
- package/dist/compiler/sdf.cjs +24 -0
- package/dist/compiler/sdf.js +12 -0
- package/dist/compiler/semantic-scene.cjs +31 -0
- package/dist/compiler/semantic-scene.js +4 -0
- package/dist/compiler/state.cjs +21 -0
- package/dist/compiler/state.js +11 -0
- package/dist/compiler/trait-composition.cjs +30 -0
- package/dist/compiler/trait-composition.js +9 -0
- package/dist/compiler/unity.cjs +19 -0
- package/dist/compiler/unity.js +13 -0
- package/dist/compiler/unreal.cjs +23 -0
- package/dist/compiler/unreal.js +13 -0
- package/dist/compiler/urdf.cjs +52 -0
- package/dist/compiler/urdf.js +12 -0
- package/dist/compiler/usd-physics.cjs +26 -0
- package/dist/compiler/usd-physics.js +9 -0
- package/dist/compiler/visionos.cjs +19 -0
- package/dist/compiler/visionos.js +13 -0
- package/dist/compiler/vrchat.cjs +22 -0
- package/dist/compiler/vrchat.js +12 -0
- package/dist/compiler/vrr.cjs +24 -0
- package/dist/compiler/vrr.js +12 -0
- package/dist/compiler/wasm.cjs +36 -0
- package/dist/compiler/wasm.js +12 -0
- package/dist/compiler/webgpu.cjs +18 -0
- package/dist/compiler/webgpu.js +12 -0
- package/dist/coordinators/index.cjs +1266 -0
- package/dist/coordinators/index.d.ts +72 -1
- package/dist/coordinators/index.js +1259 -0
- package/dist/debugger.cjs +24 -0
- package/dist/debugger.js +11 -0
- package/dist/entries/interop.cjs +64 -0
- package/dist/entries/interop.js +7 -0
- package/dist/entries/scripting.cjs +55 -0
- package/dist/entries/scripting.js +10 -0
- package/dist/hololand/index.cjs +146 -0
- package/dist/hololand/index.d.ts +70 -0
- package/dist/hololand/index.js +5 -0
- package/dist/index.cjs +43233 -0
- package/dist/index.d.ts +976 -21
- package/dist/index.js +39995 -0
- package/dist/math/tropical-spmv.cjs +45 -0
- package/dist/math/tropical-spmv.js +4 -0
- package/dist/math/vec3.cjs +57 -0
- package/dist/math/vec3.js +4 -0
- package/dist/ml-dsa-FSKBTE3W.cjs +1265 -0
- package/dist/ml-dsa-Z7JDKW4L.js +1265 -0
- package/dist/paper-0c-spike/index.cjs +150 -0
- package/dist/paper-0c-spike/index.js +141 -0
- package/dist/parser.cjs +61 -0
- package/dist/parser.d.ts +11 -0
- package/dist/parser.js +12 -0
- package/dist/playwright-3AT3AKTV.cjs +98673 -0
- package/dist/playwright-VZPDVUEQ.js +98660 -0
- package/dist/reconstruction/index.cjs +153 -0
- package/dist/reconstruction/index.d.ts +29 -2
- package/dist/reconstruction/index.js +8 -0
- package/dist/runtime.cjs +316 -0
- package/dist/runtime.d.ts +61 -0
- package/dist/runtime.js +31 -0
- package/dist/self-improvement/index.cjs +563 -0
- package/dist/self-improvement/index.js +545 -0
- package/dist/storage/index.cjs +38 -0
- package/dist/storage/index.js +5 -0
- package/dist/testing.cjs +25 -0
- package/dist/testing.d.ts +70 -0
- package/dist/testing.js +4 -0
- package/dist/traitDocs/traitDocs.cjs +33 -0
- package/dist/traitDocs/traitDocs.js +7 -0
- package/dist/traits/botanical-lotus.cjs +41 -0
- package/dist/traits/botanical-lotus.d.ts +172 -0
- package/dist/traits/botanical-lotus.js +4 -0
- package/dist/traits/engines/index.cjs +520 -0
- package/dist/traits/engines/index.js +380 -0
- package/dist/traits/index.cjs +24409 -0
- package/dist/traits/index.d.ts +171 -35
- package/dist/traits/index.js +21634 -0
- package/dist/traits/simulation-solver-factory.cjs +13 -0
- package/dist/traits/simulation-solver-factory.d.ts +23 -0
- package/dist/traits/simulation-solver-factory.js +4 -0
- package/dist/traits/webcam-gaze.cjs +35 -0
- package/dist/traits/webcam-gaze.d.ts +17 -0
- package/dist/traits/webcam-gaze.js +4 -0
- package/dist/type-checker.cjs +19 -0
- package/dist/type-checker.js +6 -0
- package/dist/world/index.cjs +947 -0
- package/dist/world/index.d.ts +147 -0
- package/dist/world/index.js +939 -0
- package/dist/world-model/index.cjs +1446 -0
- package/dist/world-model/index.d.ts +119 -0
- package/dist/world-model/index.js +1414 -0
- package/dist/wot/index.cjs +29 -0
- package/dist/wot/index.js +4 -0
- package/package.json +93 -49
|
@@ -0,0 +1,1189 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkXUUVDHIF_cjs = require('./chunk-XUUVDHIF.cjs');
|
|
4
|
+
var chunkKKP7JKN4_cjs = require('./chunk-KKP7JKN4.cjs');
|
|
5
|
+
var chunkHCCQLAA4_cjs = require('./chunk-HCCQLAA4.cjs');
|
|
6
|
+
var ans = require('@holoscript/core-types/ans');
|
|
7
|
+
|
|
8
|
+
// src/compiler/WASMCompiler.ts
|
|
9
|
+
var WASMCompiler_exports = {};
|
|
10
|
+
chunkHCCQLAA4_cjs.__export(WASMCompiler_exports, {
|
|
11
|
+
WASMCompiler: () => exports.WASMCompiler,
|
|
12
|
+
compileASTToWASM: () => compileASTToWASM,
|
|
13
|
+
compileToWASM: () => compileToWASM,
|
|
14
|
+
createWASMCompiler: () => createWASMCompiler,
|
|
15
|
+
default: () => exports.WASMCompiler_default
|
|
16
|
+
});
|
|
17
|
+
function createWASMCompiler(options) {
|
|
18
|
+
return new exports.WASMCompiler(options);
|
|
19
|
+
}
|
|
20
|
+
function compileToWASM(composition, options) {
|
|
21
|
+
const compiler = new exports.WASMCompiler(options);
|
|
22
|
+
return compiler.compile(composition, chunkKKP7JKN4_cjs.createTestCompilerToken());
|
|
23
|
+
}
|
|
24
|
+
function compileASTToWASM(ast, options) {
|
|
25
|
+
const compiler = new exports.WASMCompiler(options);
|
|
26
|
+
return compiler.compileAST(ast);
|
|
27
|
+
}
|
|
28
|
+
var WASM_WAT_LIMITATIONS, WASM_BINARY_LIMITATIONS; exports.WASMCompiler = void 0; exports.WASMCompiler_default = void 0;
|
|
29
|
+
var init_WASMCompiler = chunkHCCQLAA4_cjs.__esm({
|
|
30
|
+
"src/compiler/WASMCompiler.ts"() {
|
|
31
|
+
chunkKKP7JKN4_cjs.init_CompilerBase();
|
|
32
|
+
chunkXUUVDHIF_cjs.init_DomainBlockCompilerMixin();
|
|
33
|
+
WASM_WAT_LIMITATIONS = [
|
|
34
|
+
"Emits WebAssembly Text Format scaffolding only.",
|
|
35
|
+
'Does not return instantiable bytes unless format is set to "wasm".',
|
|
36
|
+
"Lowers trait/lifecycle/event hooks to deterministic host event calls, not arbitrary user code."
|
|
37
|
+
];
|
|
38
|
+
WASM_BINARY_LIMITATIONS = [
|
|
39
|
+
"Returns an instantiable WebAssembly binary for the compiler-supported runtime subset.",
|
|
40
|
+
"Lowers trait/lifecycle/event hooks to deterministic host event calls, not arbitrary user code.",
|
|
41
|
+
"Does not run a general-purpose WAT parser for unsupported custom WAT extensions."
|
|
42
|
+
];
|
|
43
|
+
exports.WASMCompiler = class extends chunkKKP7JKN4_cjs.CompilerBase {
|
|
44
|
+
constructor(options = {}) {
|
|
45
|
+
super();
|
|
46
|
+
this.compilerName = "WASMCompiler";
|
|
47
|
+
this.lines = [];
|
|
48
|
+
this.indentLevel = 0;
|
|
49
|
+
this.stateVars = [];
|
|
50
|
+
this.objects = [];
|
|
51
|
+
this.functions = /* @__PURE__ */ new Map();
|
|
52
|
+
this.stringPool = /* @__PURE__ */ new Map();
|
|
53
|
+
this.nextStringOffset = 0;
|
|
54
|
+
this.imports = [];
|
|
55
|
+
this.exports = [];
|
|
56
|
+
// Memory layout
|
|
57
|
+
this.memoryLayout = {
|
|
58
|
+
stateOffset: 0,
|
|
59
|
+
stateSize: 0,
|
|
60
|
+
objectsOffset: 0,
|
|
61
|
+
objectsSize: 0,
|
|
62
|
+
eventsOffset: 0,
|
|
63
|
+
eventsSize: 1024,
|
|
64
|
+
// Fixed event queue size
|
|
65
|
+
stringsOffset: 0,
|
|
66
|
+
stringsSize: 0,
|
|
67
|
+
totalSize: 0
|
|
68
|
+
};
|
|
69
|
+
const { generateDocs, docsOptions, provenanceHash, ...wasmRest } = options;
|
|
70
|
+
this.docGenerationOptions = { generateDocs, docsOptions, provenanceHash };
|
|
71
|
+
const w = wasmRest;
|
|
72
|
+
this.options = {
|
|
73
|
+
format: w.format || "wat",
|
|
74
|
+
debug: w.debug ?? false,
|
|
75
|
+
memoryPages: w.memoryPages || 16,
|
|
76
|
+
// 1MB default
|
|
77
|
+
simd: w.simd ?? false,
|
|
78
|
+
threads: w.threads ?? false,
|
|
79
|
+
generateBindings: w.generateBindings ?? true,
|
|
80
|
+
moduleName: w.moduleName || "holoscript"
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
getRequiredCapability() {
|
|
84
|
+
return ans.ANSCapabilityPath.WASM;
|
|
85
|
+
}
|
|
86
|
+
// ===========================================================================
|
|
87
|
+
// MAIN COMPILATION
|
|
88
|
+
// ===========================================================================
|
|
89
|
+
/**
|
|
90
|
+
* Compile HoloComposition AST to WASM
|
|
91
|
+
*/
|
|
92
|
+
compile(composition, agentToken, outputPath) {
|
|
93
|
+
this.validateCompilerAccess(agentToken, outputPath);
|
|
94
|
+
this.reset();
|
|
95
|
+
this.analyzeState(composition.state);
|
|
96
|
+
this.analyzeObjects(composition.objects || []);
|
|
97
|
+
this.calculateMemoryLayout();
|
|
98
|
+
this.emitModule(composition);
|
|
99
|
+
const domainBlocks = composition.domainBlocks ?? [];
|
|
100
|
+
if (domainBlocks.length > 0) {
|
|
101
|
+
this.emit("");
|
|
102
|
+
this.emit(" ;; === v4.2 Domain Blocks ===");
|
|
103
|
+
chunkXUUVDHIF_cjs.compileDomainBlocks(
|
|
104
|
+
domainBlocks,
|
|
105
|
+
{
|
|
106
|
+
material: (block) => {
|
|
107
|
+
const mat = chunkXUUVDHIF_cjs.compileMaterialBlock(block);
|
|
108
|
+
this.emit(
|
|
109
|
+
` ;; Material: "${this.escapeStringValue(mat.name, "TypeScript")}" type=${mat.type}`
|
|
110
|
+
);
|
|
111
|
+
this.emit(
|
|
112
|
+
` ;; baseColor=${mat.baseColor || "none"} roughness=${mat.roughness ?? 0.5} metallic=${mat.metallic ?? 0}`
|
|
113
|
+
);
|
|
114
|
+
return "";
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
(block) => {
|
|
118
|
+
this.emit(
|
|
119
|
+
` ;; Domain block: ${block.domain}/${block.keyword} "${this.escapeStringValue(block.name, "TypeScript")}"`
|
|
120
|
+
);
|
|
121
|
+
return "";
|
|
122
|
+
}
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
const bindings = this.options.generateBindings ? this.generateBindings(composition) : "";
|
|
126
|
+
const wasm = this.options.format === "wasm" ? this.emitWASMBytes(this.lowerCompositionObjectUpdates(composition.objects || [])) : void 0;
|
|
127
|
+
const result = {
|
|
128
|
+
artifactKind: wasm ? "wasm-binary" : "wat-scaffold",
|
|
129
|
+
format: this.options.format,
|
|
130
|
+
wat: this.lines.join("\n"),
|
|
131
|
+
...wasm ? { wasm } : {},
|
|
132
|
+
bindings,
|
|
133
|
+
memoryLayout: this.memoryLayout,
|
|
134
|
+
exports: this.exports,
|
|
135
|
+
imports: this.imports,
|
|
136
|
+
limitations: wasm ? [...WASM_BINARY_LIMITATIONS] : [...WASM_WAT_LIMITATIONS]
|
|
137
|
+
};
|
|
138
|
+
return this.withTripleOutputIfRequested(composition, result, this.docGenerationOptions);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Compile HSPlusAST to WASM
|
|
142
|
+
*/
|
|
143
|
+
compileAST(ast) {
|
|
144
|
+
this.reset();
|
|
145
|
+
this.analyzeHSPlusState(ast);
|
|
146
|
+
this.analyzeHSPlusObjects(ast.root);
|
|
147
|
+
this.calculateMemoryLayout();
|
|
148
|
+
this.emitModuleFromAST(ast);
|
|
149
|
+
const bindings = this.options.generateBindings ? this.generateBindingsFromAST(ast) : "";
|
|
150
|
+
const wasm = this.options.format === "wasm" ? this.emitWASMBytes(this.lowerASTObjectUpdates(ast.root.children || [])) : void 0;
|
|
151
|
+
return {
|
|
152
|
+
artifactKind: wasm ? "wasm-binary" : "wat-scaffold",
|
|
153
|
+
format: this.options.format,
|
|
154
|
+
wat: this.lines.join("\n"),
|
|
155
|
+
...wasm ? { wasm } : {},
|
|
156
|
+
bindings,
|
|
157
|
+
memoryLayout: this.memoryLayout,
|
|
158
|
+
exports: this.exports,
|
|
159
|
+
imports: this.imports,
|
|
160
|
+
limitations: wasm ? [...WASM_BINARY_LIMITATIONS] : [...WASM_WAT_LIMITATIONS]
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
reset() {
|
|
164
|
+
this.lines = [];
|
|
165
|
+
this.indentLevel = 0;
|
|
166
|
+
this.stateVars = [];
|
|
167
|
+
this.objects = [];
|
|
168
|
+
this.functions.clear();
|
|
169
|
+
this.stringPool.clear();
|
|
170
|
+
this.nextStringOffset = 0;
|
|
171
|
+
this.imports = [];
|
|
172
|
+
this.exports = [];
|
|
173
|
+
}
|
|
174
|
+
// ===========================================================================
|
|
175
|
+
// ANALYSIS
|
|
176
|
+
// ===========================================================================
|
|
177
|
+
analyzeState(state) {
|
|
178
|
+
if (!state) return;
|
|
179
|
+
let offset = 0;
|
|
180
|
+
const stateAny = state;
|
|
181
|
+
if (stateAny.properties) {
|
|
182
|
+
for (const prop of stateAny.properties) {
|
|
183
|
+
const type = this.inferWASMType(prop.value);
|
|
184
|
+
const size = this.getTypeSize(type);
|
|
185
|
+
this.stateVars.push({
|
|
186
|
+
name: prop.key,
|
|
187
|
+
type,
|
|
188
|
+
offset,
|
|
189
|
+
size,
|
|
190
|
+
initialValue: this.valueToWASM(prop.value, type)
|
|
191
|
+
});
|
|
192
|
+
offset += size;
|
|
193
|
+
}
|
|
194
|
+
} else if (stateAny.declarations) {
|
|
195
|
+
for (const [name, value] of Object.entries(stateAny.declarations)) {
|
|
196
|
+
const type = this.inferWASMType(value);
|
|
197
|
+
const size = this.getTypeSize(type);
|
|
198
|
+
this.stateVars.push({
|
|
199
|
+
name,
|
|
200
|
+
type,
|
|
201
|
+
offset,
|
|
202
|
+
size,
|
|
203
|
+
initialValue: this.valueToWASM(value, type)
|
|
204
|
+
});
|
|
205
|
+
offset += size;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
this.memoryLayout.stateSize = offset;
|
|
209
|
+
}
|
|
210
|
+
analyzeHSPlusState(ast) {
|
|
211
|
+
const stateDirective = ast.root.directives?.find((d) => d.type === "state");
|
|
212
|
+
if (!stateDirective || stateDirective.type !== "state") return;
|
|
213
|
+
let offset = 0;
|
|
214
|
+
const body = stateDirective.body;
|
|
215
|
+
for (const [name, value] of Object.entries(body)) {
|
|
216
|
+
const type = this.inferWASMType(value);
|
|
217
|
+
const size = this.getTypeSize(type);
|
|
218
|
+
this.stateVars.push({
|
|
219
|
+
name,
|
|
220
|
+
type,
|
|
221
|
+
offset,
|
|
222
|
+
size,
|
|
223
|
+
initialValue: this.valueToWASM(value, type)
|
|
224
|
+
});
|
|
225
|
+
offset += size;
|
|
226
|
+
}
|
|
227
|
+
this.memoryLayout.stateSize = offset;
|
|
228
|
+
}
|
|
229
|
+
analyzeObjects(objects) {
|
|
230
|
+
let offset = 0;
|
|
231
|
+
for (let i = 0; i < objects.length; i++) {
|
|
232
|
+
const obj = objects[i];
|
|
233
|
+
const instance = {
|
|
234
|
+
id: obj.name,
|
|
235
|
+
typeIndex: i,
|
|
236
|
+
offset,
|
|
237
|
+
properties: /* @__PURE__ */ new Map()
|
|
238
|
+
};
|
|
239
|
+
let propOffset = 12;
|
|
240
|
+
for (const prop of obj.properties || []) {
|
|
241
|
+
const type = this.inferWASMType(prop.value);
|
|
242
|
+
const size = this.getTypeSize(type);
|
|
243
|
+
instance.properties.set(prop.key, { offset: propOffset, type });
|
|
244
|
+
propOffset += size;
|
|
245
|
+
}
|
|
246
|
+
propOffset = Math.ceil(propOffset / 8) * 8;
|
|
247
|
+
this.objects.push(instance);
|
|
248
|
+
offset += propOffset;
|
|
249
|
+
}
|
|
250
|
+
this.memoryLayout.objectsSize = offset;
|
|
251
|
+
}
|
|
252
|
+
analyzeHSPlusObjects(root) {
|
|
253
|
+
const children = root.children || [];
|
|
254
|
+
let offset = 0;
|
|
255
|
+
for (let i = 0; i < children.length; i++) {
|
|
256
|
+
const node = children[i];
|
|
257
|
+
const instance = {
|
|
258
|
+
id: node.id || `obj_${i}`,
|
|
259
|
+
typeIndex: i,
|
|
260
|
+
offset,
|
|
261
|
+
properties: /* @__PURE__ */ new Map()
|
|
262
|
+
};
|
|
263
|
+
let propOffset = 12;
|
|
264
|
+
if (node.properties) {
|
|
265
|
+
for (const [key, value] of Object.entries(node.properties)) {
|
|
266
|
+
const type = this.inferWASMType(value);
|
|
267
|
+
const size = this.getTypeSize(type);
|
|
268
|
+
instance.properties.set(key, { offset: propOffset, type });
|
|
269
|
+
propOffset += size;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
propOffset = Math.ceil(propOffset / 8) * 8;
|
|
273
|
+
this.objects.push(instance);
|
|
274
|
+
offset += propOffset;
|
|
275
|
+
}
|
|
276
|
+
this.memoryLayout.objectsSize = offset;
|
|
277
|
+
}
|
|
278
|
+
calculateMemoryLayout() {
|
|
279
|
+
this.memoryLayout.stateOffset = 0;
|
|
280
|
+
this.memoryLayout.objectsOffset = this.memoryLayout.stateSize;
|
|
281
|
+
this.memoryLayout.eventsOffset = this.memoryLayout.objectsOffset + this.memoryLayout.objectsSize;
|
|
282
|
+
this.memoryLayout.stringsOffset = this.memoryLayout.eventsOffset + this.memoryLayout.eventsSize;
|
|
283
|
+
this.memoryLayout.totalSize = this.memoryLayout.stringsOffset + this.memoryLayout.stringsSize;
|
|
284
|
+
}
|
|
285
|
+
// ===========================================================================
|
|
286
|
+
// WAT GENERATION
|
|
287
|
+
// ===========================================================================
|
|
288
|
+
emitModule(composition) {
|
|
289
|
+
this.emit("(module");
|
|
290
|
+
this.indent();
|
|
291
|
+
if (this.options.debug) {
|
|
292
|
+
this.emit(`(; Module: ${this.options.moduleName} ;)`);
|
|
293
|
+
}
|
|
294
|
+
this.emitImports();
|
|
295
|
+
this.emitMemory();
|
|
296
|
+
this.emitGlobals();
|
|
297
|
+
this.emitFunctionTypes();
|
|
298
|
+
this.emitInitFunction();
|
|
299
|
+
this.emitStateAccessors();
|
|
300
|
+
this.emitObjectFunctions();
|
|
301
|
+
this.emitEventFunctions();
|
|
302
|
+
this.emitUpdateFunction(composition);
|
|
303
|
+
this.emitDataSections();
|
|
304
|
+
this.emitExports();
|
|
305
|
+
this.dedent();
|
|
306
|
+
this.emit(")");
|
|
307
|
+
}
|
|
308
|
+
emitModuleFromAST(ast) {
|
|
309
|
+
this.emit("(module");
|
|
310
|
+
this.indent();
|
|
311
|
+
if (this.options.debug) {
|
|
312
|
+
this.emit(`(; Module: ${this.options.moduleName} (from AST) ;)`);
|
|
313
|
+
}
|
|
314
|
+
this.emitImports();
|
|
315
|
+
this.emitMemory();
|
|
316
|
+
this.emitGlobals();
|
|
317
|
+
this.emitFunctionTypes();
|
|
318
|
+
this.emitInitFunction();
|
|
319
|
+
this.emitStateAccessors();
|
|
320
|
+
this.emitObjectFunctions();
|
|
321
|
+
this.emitEventFunctions();
|
|
322
|
+
this.emitUpdateFunctionFromAST(ast);
|
|
323
|
+
this.emitDataSections();
|
|
324
|
+
this.emitExports();
|
|
325
|
+
this.dedent();
|
|
326
|
+
this.emit(")");
|
|
327
|
+
}
|
|
328
|
+
emitImports() {
|
|
329
|
+
this.emit(";; Imports from host");
|
|
330
|
+
this.emit('(import "env" "log_i32" (func $log_i32 (param i32)))');
|
|
331
|
+
this.emit('(import "env" "log_f32" (func $log_f32 (param f32)))');
|
|
332
|
+
this.emit('(import "env" "log_str" (func $log_str (param i32 i32)))');
|
|
333
|
+
this.imports.push(
|
|
334
|
+
{ module: "env", name: "log_i32", kind: "function", signature: "(i32) -> void" },
|
|
335
|
+
{ module: "env", name: "log_f32", kind: "function", signature: "(f32) -> void" },
|
|
336
|
+
{ module: "env", name: "log_str", kind: "function", signature: "(i32, i32) -> void" }
|
|
337
|
+
);
|
|
338
|
+
this.emit('(import "env" "emit_event" (func $emit_event (param i32 i32)))');
|
|
339
|
+
this.imports.push({
|
|
340
|
+
module: "env",
|
|
341
|
+
name: "emit_event",
|
|
342
|
+
kind: "function",
|
|
343
|
+
signature: "(event_id: i32, payload_ptr: i32) -> void"
|
|
344
|
+
});
|
|
345
|
+
this.emit('(import "env" "get_time" (func $get_time (result f64)))');
|
|
346
|
+
this.imports.push({
|
|
347
|
+
module: "env",
|
|
348
|
+
name: "get_time",
|
|
349
|
+
kind: "function",
|
|
350
|
+
signature: "() -> f64"
|
|
351
|
+
});
|
|
352
|
+
this.emit("");
|
|
353
|
+
}
|
|
354
|
+
emitMemory() {
|
|
355
|
+
const pages = this.options.memoryPages;
|
|
356
|
+
this.emit(`;; Memory: ${pages} pages (${pages * 64}KB)`);
|
|
357
|
+
if (this.options.threads) {
|
|
358
|
+
this.emit(`(memory (export "memory") ${pages} ${pages * 2} shared)`);
|
|
359
|
+
} else {
|
|
360
|
+
this.emit(`(memory (export "memory") ${pages})`);
|
|
361
|
+
}
|
|
362
|
+
this.exports.push({ name: "memory", kind: "memory" });
|
|
363
|
+
this.emit("");
|
|
364
|
+
}
|
|
365
|
+
emitGlobals() {
|
|
366
|
+
this.emit(";; Globals");
|
|
367
|
+
this.emit("(global $frame_count (mut i32) (i32.const 0))");
|
|
368
|
+
this.emit("(global $delta_time (mut f32) (f32.const 0.0))");
|
|
369
|
+
this.emit("(global $last_time (mut f64) (f64.const 0.0))");
|
|
370
|
+
this.emit(`(global $object_count i32 (i32.const ${this.objects.length}))`);
|
|
371
|
+
this.emit("");
|
|
372
|
+
}
|
|
373
|
+
emitFunctionTypes() {
|
|
374
|
+
this.emit(";; Function types");
|
|
375
|
+
this.emit("(type $void_void (func))");
|
|
376
|
+
this.emit("(type $i32_void (func (param i32)))");
|
|
377
|
+
this.emit("(type $f32_void (func (param f32)))");
|
|
378
|
+
this.emit("(type $void_i32 (func (result i32)))");
|
|
379
|
+
this.emit("(type $void_f32 (func (result f32)))");
|
|
380
|
+
this.emit("(type $i32_i32 (func (param i32) (result i32)))");
|
|
381
|
+
this.emit("(type $i32_f32 (func (param i32) (result f32)))");
|
|
382
|
+
this.emit("(type $i32_i32_void (func (param i32 i32)))");
|
|
383
|
+
this.emit("(type $update_fn (func (param f32)))");
|
|
384
|
+
this.emit("");
|
|
385
|
+
}
|
|
386
|
+
emitInitFunction() {
|
|
387
|
+
this.emit(";; Initialize module");
|
|
388
|
+
this.emit('(func $init (export "init")');
|
|
389
|
+
this.indent();
|
|
390
|
+
for (const v of this.stateVars) {
|
|
391
|
+
const offset = this.memoryLayout.stateOffset + v.offset;
|
|
392
|
+
this.emit(` ;; ${this.escapeStringValue(v.name, "TypeScript")}`);
|
|
393
|
+
switch (v.type) {
|
|
394
|
+
case "i32":
|
|
395
|
+
this.emit(` (i32.store (i32.const ${offset}) (i32.const ${v.initialValue}))`);
|
|
396
|
+
break;
|
|
397
|
+
case "i64":
|
|
398
|
+
this.emit(` (i64.store (i32.const ${offset}) (i64.const ${v.initialValue}))`);
|
|
399
|
+
break;
|
|
400
|
+
case "f32":
|
|
401
|
+
this.emit(` (f32.store (i32.const ${offset}) (f32.const ${v.initialValue}))`);
|
|
402
|
+
break;
|
|
403
|
+
case "f64":
|
|
404
|
+
this.emit(` (f64.store (i32.const ${offset}) (f64.const ${v.initialValue}))`);
|
|
405
|
+
break;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
for (const obj of this.objects) {
|
|
409
|
+
const baseOffset = this.memoryLayout.objectsOffset + obj.offset;
|
|
410
|
+
const safeName = this.sanitizeName(obj.id);
|
|
411
|
+
this.emit(` ;; Object: ${safeName}`);
|
|
412
|
+
this.emit(` (i32.store (i32.const ${baseOffset}) (i32.const ${obj.typeIndex})) ;; type`);
|
|
413
|
+
this.emit(` (i32.store (i32.const ${baseOffset + 4}) (i32.const 1)) ;; active`);
|
|
414
|
+
this.emit(` (i32.store (i32.const ${baseOffset + 8}) (i32.const -1)) ;; parent`);
|
|
415
|
+
}
|
|
416
|
+
this.dedent();
|
|
417
|
+
this.emit(")");
|
|
418
|
+
this.emit("");
|
|
419
|
+
this.exports.push({ name: "init", kind: "function", signature: "() -> void" });
|
|
420
|
+
}
|
|
421
|
+
emitStateAccessors() {
|
|
422
|
+
this.emit(";; State accessors");
|
|
423
|
+
for (const v of this.stateVars) {
|
|
424
|
+
const offset = this.memoryLayout.stateOffset + v.offset;
|
|
425
|
+
const safeName = this.sanitizeName(v.name);
|
|
426
|
+
this.emit(`(func $get_${safeName} (export "get_${safeName}") (result ${v.type})`);
|
|
427
|
+
this.emit(` (${v.type}.load (i32.const ${offset}))`);
|
|
428
|
+
this.emit(")");
|
|
429
|
+
this.emit(`(func $set_${safeName} (export "set_${safeName}") (param $value ${v.type})`);
|
|
430
|
+
this.emit(` (${v.type}.store (i32.const ${offset}) (local.get $value))`);
|
|
431
|
+
this.emit(")");
|
|
432
|
+
this.exports.push({
|
|
433
|
+
name: `get_${safeName}`,
|
|
434
|
+
kind: "function",
|
|
435
|
+
signature: `() -> ${v.type}`
|
|
436
|
+
});
|
|
437
|
+
this.exports.push({
|
|
438
|
+
name: `set_${safeName}`,
|
|
439
|
+
kind: "function",
|
|
440
|
+
signature: `(${v.type}) -> void`
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
this.emit('(func $get_frame_count (export "get_frame_count") (result i32)');
|
|
444
|
+
this.emit(" (global.get $frame_count)");
|
|
445
|
+
this.emit(")");
|
|
446
|
+
this.exports.push({
|
|
447
|
+
name: "get_frame_count",
|
|
448
|
+
kind: "function",
|
|
449
|
+
signature: "() -> i32"
|
|
450
|
+
});
|
|
451
|
+
this.emit("");
|
|
452
|
+
}
|
|
453
|
+
emitObjectFunctions() {
|
|
454
|
+
this.emit(";; Object functions");
|
|
455
|
+
this.emit('(func $get_object_count (export "get_object_count") (result i32)');
|
|
456
|
+
this.emit(" (global.get $object_count)");
|
|
457
|
+
this.emit(")");
|
|
458
|
+
this.emit('(func $is_object_active (export "is_object_active") (param $idx i32) (result i32)');
|
|
459
|
+
this.indent();
|
|
460
|
+
this.emit("(i32.load");
|
|
461
|
+
this.emit(" (i32.add");
|
|
462
|
+
this.emit(` (i32.const ${this.memoryLayout.objectsOffset})`);
|
|
463
|
+
this.emit(" (i32.add");
|
|
464
|
+
this.emit(" (i32.mul (local.get $idx) (i32.const 64)) ;; obj size");
|
|
465
|
+
this.emit(" (i32.const 4) ;; active offset");
|
|
466
|
+
this.emit(" )");
|
|
467
|
+
this.emit(" )");
|
|
468
|
+
this.emit(")");
|
|
469
|
+
this.dedent();
|
|
470
|
+
this.emit(")");
|
|
471
|
+
this.emit(
|
|
472
|
+
'(func $set_object_active (export "set_object_active") (param $idx i32) (param $active i32)'
|
|
473
|
+
);
|
|
474
|
+
this.indent();
|
|
475
|
+
this.emit("(i32.store");
|
|
476
|
+
this.emit(" (i32.add");
|
|
477
|
+
this.emit(` (i32.const ${this.memoryLayout.objectsOffset})`);
|
|
478
|
+
this.emit(" (i32.add");
|
|
479
|
+
this.emit(" (i32.mul (local.get $idx) (i32.const 64))");
|
|
480
|
+
this.emit(" (i32.const 4)");
|
|
481
|
+
this.emit(" )");
|
|
482
|
+
this.emit(" )");
|
|
483
|
+
this.emit(" (local.get $active)");
|
|
484
|
+
this.emit(")");
|
|
485
|
+
this.dedent();
|
|
486
|
+
this.emit(")");
|
|
487
|
+
this.exports.push(
|
|
488
|
+
{ name: "get_object_count", kind: "function", signature: "() -> i32" },
|
|
489
|
+
{ name: "is_object_active", kind: "function", signature: "(i32) -> i32" },
|
|
490
|
+
{ name: "set_object_active", kind: "function", signature: "(i32, i32) -> void" }
|
|
491
|
+
);
|
|
492
|
+
this.emit("");
|
|
493
|
+
}
|
|
494
|
+
emitEventFunctions() {
|
|
495
|
+
this.emit(";; Event functions");
|
|
496
|
+
this.emit("(func $dispatch_event (param $event_id i32) (param $payload i32)");
|
|
497
|
+
this.emit(" (call $emit_event (local.get $event_id) (local.get $payload))");
|
|
498
|
+
this.emit(")");
|
|
499
|
+
this.emit("");
|
|
500
|
+
}
|
|
501
|
+
emitUpdateFunction(composition) {
|
|
502
|
+
const objectUpdates = this.lowerCompositionObjectUpdates(composition.objects || []);
|
|
503
|
+
this.emit(";; Main update function");
|
|
504
|
+
this.emit('(func $update (export "update") (param $dt f32)');
|
|
505
|
+
this.indent();
|
|
506
|
+
this.emit("(global.set $delta_time (local.get $dt))");
|
|
507
|
+
this.emit("(global.set $frame_count");
|
|
508
|
+
this.emit(" (i32.add (global.get $frame_count) (i32.const 1))");
|
|
509
|
+
this.emit(")");
|
|
510
|
+
for (let i = 0; i < this.objects.length; i++) {
|
|
511
|
+
const obj = this.objects[i];
|
|
512
|
+
const safeObjName = this.sanitizeName(obj.id);
|
|
513
|
+
this.emit(` ;; Update ${safeObjName}`);
|
|
514
|
+
this.emit(` (if (call $is_object_active (i32.const ${i}))`);
|
|
515
|
+
this.emit(" (then");
|
|
516
|
+
this.emit(` (call $update_object_${i} (local.get $dt))`);
|
|
517
|
+
this.emit(" )");
|
|
518
|
+
this.emit(" )");
|
|
519
|
+
}
|
|
520
|
+
this.dedent();
|
|
521
|
+
this.emit(")");
|
|
522
|
+
this.emit("");
|
|
523
|
+
for (let i = 0; i < this.objects.length; i++) {
|
|
524
|
+
const obj = this.objects[i];
|
|
525
|
+
const safeName = this.sanitizeName(obj.id);
|
|
526
|
+
this.emit(`(func $update_object_${i} (param $dt f32)`);
|
|
527
|
+
this.emit(` ;; Update logic for ${safeName}`);
|
|
528
|
+
this.emitLoweredObjectUpdateBody(objectUpdates[i] || [], i);
|
|
529
|
+
this.emit(")");
|
|
530
|
+
}
|
|
531
|
+
this.exports.push({ name: "update", kind: "function", signature: "(f32) -> void" });
|
|
532
|
+
this.emit("");
|
|
533
|
+
}
|
|
534
|
+
emitUpdateFunctionFromAST(ast) {
|
|
535
|
+
const objectUpdates = this.lowerASTObjectUpdates(ast.root.children || []);
|
|
536
|
+
this.emit(";; Main update function");
|
|
537
|
+
this.emit('(func $update (export "update") (param $dt f32)');
|
|
538
|
+
this.indent();
|
|
539
|
+
this.emit("(global.set $delta_time (local.get $dt))");
|
|
540
|
+
this.emit("(global.set $frame_count");
|
|
541
|
+
this.emit(" (i32.add (global.get $frame_count) (i32.const 1))");
|
|
542
|
+
this.emit(")");
|
|
543
|
+
for (let i = 0; i < this.objects.length; i++) {
|
|
544
|
+
const obj = this.objects[i];
|
|
545
|
+
const safeObjName = this.sanitizeName(obj.id);
|
|
546
|
+
this.emit(` ;; Update ${safeObjName}`);
|
|
547
|
+
this.emit(` (if (call $is_object_active (i32.const ${i}))`);
|
|
548
|
+
this.emit(" (then");
|
|
549
|
+
this.emit(` (call $update_object_${i} (local.get $dt))`);
|
|
550
|
+
this.emit(" )");
|
|
551
|
+
this.emit(" )");
|
|
552
|
+
}
|
|
553
|
+
this.dedent();
|
|
554
|
+
this.emit(")");
|
|
555
|
+
this.emit("");
|
|
556
|
+
for (let i = 0; i < this.objects.length; i++) {
|
|
557
|
+
const obj = this.objects[i];
|
|
558
|
+
const safeName = this.sanitizeName(obj.id);
|
|
559
|
+
this.emit(`(func $update_object_${i} (param $dt f32)`);
|
|
560
|
+
this.emit(` ;; Update logic for ${safeName}`);
|
|
561
|
+
this.emitLoweredObjectUpdateBody(objectUpdates[i] || [], i);
|
|
562
|
+
this.emit(")");
|
|
563
|
+
}
|
|
564
|
+
this.exports.push({ name: "update", kind: "function", signature: "(f32) -> void" });
|
|
565
|
+
this.emit("");
|
|
566
|
+
}
|
|
567
|
+
emitLoweredObjectUpdateBody(updates, objectIndex) {
|
|
568
|
+
if (updates.length === 0) {
|
|
569
|
+
this.emit(" ;; No WASM-lowerable trait/lifecycle/event hooks for this object");
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
for (const update of updates) {
|
|
573
|
+
this.emit(` ;; ${this.escapeStringValue(update.label, "TypeScript")}`);
|
|
574
|
+
this.emit(` (call $emit_event (i32.const ${update.eventId}) (i32.const ${objectIndex}))`);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
lowerCompositionObjectUpdates(objects) {
|
|
578
|
+
return objects.map((obj) => {
|
|
579
|
+
const events = [];
|
|
580
|
+
for (const trait of obj.traits || []) {
|
|
581
|
+
events.push(this.createLoweredUpdate(`trait:${trait.name}`));
|
|
582
|
+
}
|
|
583
|
+
for (const eventHandler of this.getUnknownArray(obj, "eventHandlers")) {
|
|
584
|
+
const event = this.getUnknownString(eventHandler, "event");
|
|
585
|
+
if (event) events.push(this.createLoweredUpdate(`event:${event}`));
|
|
586
|
+
}
|
|
587
|
+
for (const directive of this.getUnknownArray(obj, "directives")) {
|
|
588
|
+
const directiveType = this.getUnknownString(directive, "type");
|
|
589
|
+
const hook = this.getUnknownString(directive, "hook") || this.getUnknownString(directive, "name");
|
|
590
|
+
if (directiveType === "lifecycle" && hook) {
|
|
591
|
+
events.push(this.createLoweredUpdate(`lifecycle:${hook}`));
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
return events;
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
lowerASTObjectUpdates(nodes) {
|
|
598
|
+
return nodes.map((node) => {
|
|
599
|
+
const events = [];
|
|
600
|
+
for (const directive of node.directives || []) {
|
|
601
|
+
const directiveType = this.getUnknownString(directive, "type");
|
|
602
|
+
const name = this.getUnknownString(directive, "name") || this.getUnknownString(directive, "hook");
|
|
603
|
+
if (directiveType === "trait" && name) {
|
|
604
|
+
events.push(this.createLoweredUpdate(`trait:${name}`));
|
|
605
|
+
} else if (directiveType === "lifecycle" && name) {
|
|
606
|
+
events.push(this.createLoweredUpdate(`lifecycle:${name}`));
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
for (const [key] of Object.entries(node.properties || {})) {
|
|
610
|
+
if (key.startsWith("on_")) events.push(this.createLoweredUpdate(`event:${key}`));
|
|
611
|
+
}
|
|
612
|
+
return events;
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
createLoweredUpdate(label) {
|
|
616
|
+
return {
|
|
617
|
+
label,
|
|
618
|
+
eventId: this.stableEventId(label)
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
stableEventId(label) {
|
|
622
|
+
let hash = 2166136261;
|
|
623
|
+
for (let i = 0; i < label.length; i++) {
|
|
624
|
+
hash ^= label.charCodeAt(i);
|
|
625
|
+
hash = Math.imul(hash, 16777619);
|
|
626
|
+
}
|
|
627
|
+
return hash >>> 1;
|
|
628
|
+
}
|
|
629
|
+
getUnknownArray(source, key) {
|
|
630
|
+
if (!source || typeof source !== "object") return [];
|
|
631
|
+
const value = source[key];
|
|
632
|
+
return Array.isArray(value) ? value : [];
|
|
633
|
+
}
|
|
634
|
+
getUnknownString(source, key) {
|
|
635
|
+
if (!source || typeof source !== "object") return void 0;
|
|
636
|
+
const value = source[key];
|
|
637
|
+
return typeof value === "string" ? value : void 0;
|
|
638
|
+
}
|
|
639
|
+
emitDataSections() {
|
|
640
|
+
if (this.stringPool.size === 0) return;
|
|
641
|
+
this.emit(";; Data sections");
|
|
642
|
+
for (const [str, offset] of this.stringPool) {
|
|
643
|
+
const escaped = this.escapeWATString(str);
|
|
644
|
+
this.emit(`(data (i32.const ${this.memoryLayout.stringsOffset + offset}) "${escaped}")`);
|
|
645
|
+
}
|
|
646
|
+
this.emit("");
|
|
647
|
+
}
|
|
648
|
+
emitExports() {
|
|
649
|
+
}
|
|
650
|
+
// ===========================================================================
|
|
651
|
+
// JS BINDINGS GENERATION
|
|
652
|
+
// ===========================================================================
|
|
653
|
+
generateBindings(_composition) {
|
|
654
|
+
const lines = [];
|
|
655
|
+
lines.push("/**");
|
|
656
|
+
lines.push(` * JavaScript bindings for ${this.options.moduleName}`);
|
|
657
|
+
lines.push(" * Auto-generated by HoloScript WASMCompiler");
|
|
658
|
+
lines.push(" */");
|
|
659
|
+
lines.push("");
|
|
660
|
+
lines.push("export class HoloScriptWASM {");
|
|
661
|
+
lines.push(" private instance: WebAssembly.Instance | null = null;");
|
|
662
|
+
lines.push(" private memory: WebAssembly.Memory | null = null;");
|
|
663
|
+
lines.push(
|
|
664
|
+
" private eventHandlers: Map<string, Set<(payload: unknown) => void>> = new Map();"
|
|
665
|
+
);
|
|
666
|
+
lines.push("");
|
|
667
|
+
lines.push(" private static EVENT_IDS: Record<string, number> = {");
|
|
668
|
+
lines.push(" 'update': 0,");
|
|
669
|
+
lines.push(" 'state_change': 1,");
|
|
670
|
+
lines.push(" 'object_activated': 2,");
|
|
671
|
+
lines.push(" 'object_deactivated': 3,");
|
|
672
|
+
lines.push(" };");
|
|
673
|
+
lines.push("");
|
|
674
|
+
lines.push(" async load(wasmSource: ArrayBuffer | Response): Promise<void> {");
|
|
675
|
+
lines.push(" const importObject = {");
|
|
676
|
+
lines.push(" env: {");
|
|
677
|
+
lines.push(' log_i32: (v: number) => console.log("[WASM i32]", v),');
|
|
678
|
+
lines.push(' log_f32: (v: number) => console.log("[WASM f32]", v),');
|
|
679
|
+
lines.push(" log_str: (ptr: number, len: number) => {");
|
|
680
|
+
lines.push(" const bytes = new Uint8Array(this.memory!.buffer, ptr, len);");
|
|
681
|
+
lines.push(' console.log("[WASM str]", new TextDecoder().decode(bytes));');
|
|
682
|
+
lines.push(" },");
|
|
683
|
+
lines.push(" emit_event: (eventId: number, payloadPtr: number) => {");
|
|
684
|
+
lines.push(" this.handleEvent(eventId, payloadPtr);");
|
|
685
|
+
lines.push(" },");
|
|
686
|
+
lines.push(" get_time: () => performance.now(),");
|
|
687
|
+
lines.push(" },");
|
|
688
|
+
lines.push(" };");
|
|
689
|
+
lines.push("");
|
|
690
|
+
lines.push(" const result = await WebAssembly.instantiate(wasmSource, importObject);");
|
|
691
|
+
lines.push(" this.instance = result.instance;");
|
|
692
|
+
lines.push(" this.memory = this.instance.exports.memory as WebAssembly.Memory;");
|
|
693
|
+
lines.push(" (this.instance.exports.init as Function)();");
|
|
694
|
+
lines.push(" }");
|
|
695
|
+
lines.push("");
|
|
696
|
+
lines.push(" // State accessors");
|
|
697
|
+
for (const v of this.stateVars) {
|
|
698
|
+
const safeName = this.sanitizeName(v.name);
|
|
699
|
+
const jsType = this.wasmTypeToJS(v.type);
|
|
700
|
+
lines.push(` get ${safeName}(): ${jsType} {`);
|
|
701
|
+
lines.push(` return (this.instance!.exports.get_${safeName} as Function)();`);
|
|
702
|
+
lines.push(" }");
|
|
703
|
+
lines.push(` set ${safeName}(value: ${jsType}) {`);
|
|
704
|
+
lines.push(` (this.instance!.exports.set_${safeName} as Function)(value);`);
|
|
705
|
+
lines.push(" }");
|
|
706
|
+
}
|
|
707
|
+
lines.push("");
|
|
708
|
+
lines.push(" update(dt: number): void {");
|
|
709
|
+
lines.push(" (this.instance!.exports.update as Function)(dt);");
|
|
710
|
+
lines.push(" }");
|
|
711
|
+
lines.push("");
|
|
712
|
+
lines.push(" on(event: string, handler: (payload: unknown) => void): () => void {");
|
|
713
|
+
lines.push(" if (!this.eventHandlers.has(event)) {");
|
|
714
|
+
lines.push(" this.eventHandlers.set(event, new Set());");
|
|
715
|
+
lines.push(" }");
|
|
716
|
+
lines.push(" this.eventHandlers.get(event)!.add(handler);");
|
|
717
|
+
lines.push(" return () => this.eventHandlers.get(event)?.delete(handler);");
|
|
718
|
+
lines.push(" }");
|
|
719
|
+
lines.push("");
|
|
720
|
+
lines.push(" private handleEvent(eventId: number, payloadPtr: number): void {");
|
|
721
|
+
lines.push(" const eventName = Object.entries(HoloScriptWASM.EVENT_IDS)");
|
|
722
|
+
lines.push(" .find(([_, id]) => id === eventId)?.[0];");
|
|
723
|
+
lines.push(" if (!eventName) return;");
|
|
724
|
+
lines.push(" const handlers = this.eventHandlers.get(eventName);");
|
|
725
|
+
lines.push(" if (handlers) {");
|
|
726
|
+
lines.push(" const payload = this.decodePayload(payloadPtr);");
|
|
727
|
+
lines.push(" handlers.forEach(h => h(payload));");
|
|
728
|
+
lines.push(" }");
|
|
729
|
+
lines.push(" }");
|
|
730
|
+
lines.push("");
|
|
731
|
+
lines.push(" private decodePayload(ptr: number): unknown {");
|
|
732
|
+
lines.push(" if (!this.instance || ptr === 0) return {};");
|
|
733
|
+
lines.push(" const memory = this.instance.exports.memory as WebAssembly.Memory;");
|
|
734
|
+
lines.push(" const view = new DataView(memory.buffer);");
|
|
735
|
+
lines.push(" // Read payload header: [type:u8][length:u32]");
|
|
736
|
+
lines.push(" const payloadType = view.getUint8(ptr);");
|
|
737
|
+
lines.push(" const length = view.getUint32(ptr + 1, true);");
|
|
738
|
+
lines.push(" const dataPtr = ptr + 5;");
|
|
739
|
+
lines.push(" switch (payloadType) {");
|
|
740
|
+
lines.push(" case 0: return null; // Null");
|
|
741
|
+
lines.push(" case 1: return view.getFloat64(dataPtr, true); // Number");
|
|
742
|
+
lines.push(" case 2: return view.getUint8(dataPtr) !== 0; // Boolean");
|
|
743
|
+
lines.push(" case 3: { // String");
|
|
744
|
+
lines.push(" const bytes = new Uint8Array(memory.buffer, dataPtr, length);");
|
|
745
|
+
lines.push(" return new TextDecoder().decode(bytes);");
|
|
746
|
+
lines.push(" }");
|
|
747
|
+
lines.push(" case 4: { // Object (JSON)");
|
|
748
|
+
lines.push(" const bytes = new Uint8Array(memory.buffer, dataPtr, length);");
|
|
749
|
+
lines.push(" return JSON.parse(new TextDecoder().decode(bytes));");
|
|
750
|
+
lines.push(" }");
|
|
751
|
+
lines.push(" case 5: { // Float32Array");
|
|
752
|
+
lines.push(" return new Float32Array(memory.buffer, dataPtr, length / 4);");
|
|
753
|
+
lines.push(" }");
|
|
754
|
+
lines.push(" default: return { ptr, type: payloadType, length };");
|
|
755
|
+
lines.push(" }");
|
|
756
|
+
lines.push(" }");
|
|
757
|
+
lines.push("");
|
|
758
|
+
lines.push(" getObjectCount(): number {");
|
|
759
|
+
lines.push(" return (this.instance!.exports.get_object_count as Function)();");
|
|
760
|
+
lines.push(" }");
|
|
761
|
+
lines.push("");
|
|
762
|
+
lines.push(" isObjectActive(index: number): boolean {");
|
|
763
|
+
lines.push(" return (this.instance!.exports.is_object_active as Function)(index) !== 0;");
|
|
764
|
+
lines.push(" }");
|
|
765
|
+
lines.push("");
|
|
766
|
+
lines.push(" setObjectActive(index: number, active: boolean): void {");
|
|
767
|
+
lines.push(
|
|
768
|
+
" (this.instance!.exports.set_object_active as Function)(index, active ? 1 : 0);"
|
|
769
|
+
);
|
|
770
|
+
lines.push(" }");
|
|
771
|
+
lines.push("");
|
|
772
|
+
lines.push(" getMemoryLayout(): object {");
|
|
773
|
+
lines.push(` return ${JSON.stringify(this.memoryLayout)};`);
|
|
774
|
+
lines.push(" }");
|
|
775
|
+
lines.push("}");
|
|
776
|
+
return lines.join("\n");
|
|
777
|
+
}
|
|
778
|
+
generateBindingsFromAST(ast) {
|
|
779
|
+
const minimalComposition = {
|
|
780
|
+
type: "Composition",
|
|
781
|
+
name: ast.root.id || "scene",
|
|
782
|
+
objects: [],
|
|
783
|
+
templates: [],
|
|
784
|
+
spatialGroups: [],
|
|
785
|
+
lights: [],
|
|
786
|
+
imports: [],
|
|
787
|
+
timelines: [],
|
|
788
|
+
audio: [],
|
|
789
|
+
zones: [],
|
|
790
|
+
transitions: [],
|
|
791
|
+
conditionals: [],
|
|
792
|
+
iterators: [],
|
|
793
|
+
npcs: [],
|
|
794
|
+
quests: [],
|
|
795
|
+
abilities: [],
|
|
796
|
+
dialogues: [],
|
|
797
|
+
stateMachines: [],
|
|
798
|
+
achievements: [],
|
|
799
|
+
talentTrees: [],
|
|
800
|
+
shapes: []
|
|
801
|
+
};
|
|
802
|
+
return this.generateBindings(minimalComposition);
|
|
803
|
+
}
|
|
804
|
+
// ===========================================================================
|
|
805
|
+
// WASM BINARY GENERATION
|
|
806
|
+
// ===========================================================================
|
|
807
|
+
emitWASMBytes(objectUpdates) {
|
|
808
|
+
const types = [];
|
|
809
|
+
const typeIndexes = /* @__PURE__ */ new Map();
|
|
810
|
+
const typeIndex = (params, results = []) => {
|
|
811
|
+
const key = `${params.join(",")}->${results.join(",")}`;
|
|
812
|
+
const existing = typeIndexes.get(key);
|
|
813
|
+
if (existing !== void 0) return existing;
|
|
814
|
+
const next = types.length;
|
|
815
|
+
types.push({ params, results });
|
|
816
|
+
typeIndexes.set(key, next);
|
|
817
|
+
return next;
|
|
818
|
+
};
|
|
819
|
+
const imports = [
|
|
820
|
+
{ module: "env", name: "log_i32", typeIndex: typeIndex(["i32"]) },
|
|
821
|
+
{ module: "env", name: "log_f32", typeIndex: typeIndex(["f32"]) },
|
|
822
|
+
{ module: "env", name: "log_str", typeIndex: typeIndex(["i32", "i32"]) },
|
|
823
|
+
{ module: "env", name: "emit_event", typeIndex: typeIndex(["i32", "i32"]) },
|
|
824
|
+
{ module: "env", name: "get_time", typeIndex: typeIndex([], ["f64"]) }
|
|
825
|
+
];
|
|
826
|
+
const functions = [];
|
|
827
|
+
const functionIndexes = /* @__PURE__ */ new Map();
|
|
828
|
+
const addFunction = (name, params, results, body) => {
|
|
829
|
+
functionIndexes.set(name, imports.length + functions.length);
|
|
830
|
+
functions.push({ name, typeIndex: typeIndex(params, results), body });
|
|
831
|
+
};
|
|
832
|
+
addFunction("init", [], [], () => this.binaryInitBody());
|
|
833
|
+
for (const stateVar of this.stateVars) {
|
|
834
|
+
const safeName = this.sanitizeName(stateVar.name);
|
|
835
|
+
addFunction(
|
|
836
|
+
`get_${safeName}`,
|
|
837
|
+
[],
|
|
838
|
+
[stateVar.type],
|
|
839
|
+
() => this.binaryStateGetterBody(stateVar)
|
|
840
|
+
);
|
|
841
|
+
addFunction(
|
|
842
|
+
`set_${safeName}`,
|
|
843
|
+
[stateVar.type],
|
|
844
|
+
[],
|
|
845
|
+
() => this.binaryStateSetterBody(stateVar)
|
|
846
|
+
);
|
|
847
|
+
}
|
|
848
|
+
addFunction("get_frame_count", [], ["i32"], () => [0, 35, ...this.encodeULEB(0), 11]);
|
|
849
|
+
addFunction("get_object_count", [], ["i32"], () => [0, 35, ...this.encodeULEB(3), 11]);
|
|
850
|
+
addFunction("is_object_active", ["i32"], ["i32"], () => this.binaryIsObjectActiveBody());
|
|
851
|
+
addFunction("set_object_active", ["i32", "i32"], [], () => this.binarySetObjectActiveBody());
|
|
852
|
+
addFunction(
|
|
853
|
+
"update",
|
|
854
|
+
["f32"],
|
|
855
|
+
[],
|
|
856
|
+
() => this.binaryUpdateBody(functionIndexes, objectUpdates.length)
|
|
857
|
+
);
|
|
858
|
+
for (let i = 0; i < this.objects.length; i++) {
|
|
859
|
+
addFunction(
|
|
860
|
+
`update_object_${i}`,
|
|
861
|
+
["f32"],
|
|
862
|
+
[],
|
|
863
|
+
() => this.binaryUpdateObjectBody(objectUpdates[i] || [], i)
|
|
864
|
+
);
|
|
865
|
+
}
|
|
866
|
+
const bytes = [
|
|
867
|
+
0,
|
|
868
|
+
97,
|
|
869
|
+
115,
|
|
870
|
+
109,
|
|
871
|
+
1,
|
|
872
|
+
0,
|
|
873
|
+
0,
|
|
874
|
+
0,
|
|
875
|
+
...this.binarySection(1, this.binaryTypeSection(types)),
|
|
876
|
+
...this.binarySection(2, this.binaryImportSection(imports)),
|
|
877
|
+
...this.binarySection(3, this.binaryFunctionSection(functions)),
|
|
878
|
+
...this.binarySection(5, this.binaryMemorySection()),
|
|
879
|
+
...this.binarySection(6, this.binaryGlobalSection()),
|
|
880
|
+
...this.binarySection(7, this.binaryExportSection(functions, imports.length)),
|
|
881
|
+
...this.binarySection(10, this.binaryCodeSection(functions))
|
|
882
|
+
];
|
|
883
|
+
return new Uint8Array(bytes);
|
|
884
|
+
}
|
|
885
|
+
binaryTypeSection(types) {
|
|
886
|
+
return this.binaryVector(
|
|
887
|
+
types.map((type) => [
|
|
888
|
+
96,
|
|
889
|
+
...this.binaryVector(type.params.map((param) => [this.wasmBinaryValueType(param)])),
|
|
890
|
+
...this.binaryVector(type.results.map((result) => [this.wasmBinaryValueType(result)]))
|
|
891
|
+
])
|
|
892
|
+
);
|
|
893
|
+
}
|
|
894
|
+
binaryImportSection(imports) {
|
|
895
|
+
return this.binaryVector(
|
|
896
|
+
imports.map((entry) => [
|
|
897
|
+
...this.encodeString(entry.module),
|
|
898
|
+
...this.encodeString(entry.name),
|
|
899
|
+
0,
|
|
900
|
+
...this.encodeULEB(entry.typeIndex)
|
|
901
|
+
])
|
|
902
|
+
);
|
|
903
|
+
}
|
|
904
|
+
binaryFunctionSection(functions) {
|
|
905
|
+
return this.binaryVector(functions.map((fn) => this.encodeULEB(fn.typeIndex)));
|
|
906
|
+
}
|
|
907
|
+
binaryMemorySection() {
|
|
908
|
+
if (this.options.threads) {
|
|
909
|
+
return this.binaryVector([
|
|
910
|
+
[
|
|
911
|
+
3,
|
|
912
|
+
...this.encodeULEB(this.options.memoryPages),
|
|
913
|
+
...this.encodeULEB(this.options.memoryPages * 2)
|
|
914
|
+
]
|
|
915
|
+
]);
|
|
916
|
+
}
|
|
917
|
+
return this.binaryVector([[0, ...this.encodeULEB(this.options.memoryPages)]]);
|
|
918
|
+
}
|
|
919
|
+
binaryGlobalSection() {
|
|
920
|
+
return this.binaryVector([
|
|
921
|
+
[127, 1, 65, ...this.encodeSLEB(0), 11],
|
|
922
|
+
[125, 1, 67, ...this.f32Bytes(0), 11],
|
|
923
|
+
[124, 1, 68, ...this.f64Bytes(0), 11],
|
|
924
|
+
[127, 0, 65, ...this.encodeSLEB(this.objects.length), 11]
|
|
925
|
+
]);
|
|
926
|
+
}
|
|
927
|
+
binaryExportSection(functions, importedFunctionCount) {
|
|
928
|
+
const exports$1 = [
|
|
929
|
+
[...this.encodeString("memory"), 2, ...this.encodeULEB(0)],
|
|
930
|
+
...functions.filter((fn) => !fn.name.startsWith("update_object_")).map((fn, index) => [
|
|
931
|
+
...this.encodeString(fn.name),
|
|
932
|
+
0,
|
|
933
|
+
...this.encodeULEB(importedFunctionCount + index)
|
|
934
|
+
])
|
|
935
|
+
];
|
|
936
|
+
return this.binaryVector(exports$1);
|
|
937
|
+
}
|
|
938
|
+
binaryCodeSection(functions) {
|
|
939
|
+
return this.binaryVector(
|
|
940
|
+
functions.map((fn) => {
|
|
941
|
+
const body = fn.body();
|
|
942
|
+
return [...this.encodeULEB(body.length), ...body];
|
|
943
|
+
})
|
|
944
|
+
);
|
|
945
|
+
}
|
|
946
|
+
binaryInitBody() {
|
|
947
|
+
const body = [0];
|
|
948
|
+
for (const stateVar of this.stateVars) {
|
|
949
|
+
const offset = this.memoryLayout.stateOffset + stateVar.offset;
|
|
950
|
+
body.push(65, ...this.encodeSLEB(offset));
|
|
951
|
+
this.pushWASMConst(body, stateVar.type, stateVar.initialValue);
|
|
952
|
+
this.pushWASMStore(body, stateVar.type);
|
|
953
|
+
}
|
|
954
|
+
for (const obj of this.objects) {
|
|
955
|
+
const baseOffset = this.memoryLayout.objectsOffset + obj.offset;
|
|
956
|
+
this.pushI32StoreConst(body, baseOffset, obj.typeIndex);
|
|
957
|
+
this.pushI32StoreConst(body, baseOffset + 4, 1);
|
|
958
|
+
this.pushI32StoreConst(body, baseOffset + 8, -1);
|
|
959
|
+
}
|
|
960
|
+
body.push(11);
|
|
961
|
+
return body;
|
|
962
|
+
}
|
|
963
|
+
binaryStateGetterBody(stateVar) {
|
|
964
|
+
const offset = this.memoryLayout.stateOffset + stateVar.offset;
|
|
965
|
+
const body = [0, 65, ...this.encodeSLEB(offset)];
|
|
966
|
+
this.pushWASMLoad(body, stateVar.type);
|
|
967
|
+
body.push(11);
|
|
968
|
+
return body;
|
|
969
|
+
}
|
|
970
|
+
binaryStateSetterBody(stateVar) {
|
|
971
|
+
const offset = this.memoryLayout.stateOffset + stateVar.offset;
|
|
972
|
+
const body = [0, 65, ...this.encodeSLEB(offset), 32, 0];
|
|
973
|
+
this.pushWASMStore(body, stateVar.type);
|
|
974
|
+
body.push(11);
|
|
975
|
+
return body;
|
|
976
|
+
}
|
|
977
|
+
binaryIsObjectActiveBody() {
|
|
978
|
+
const body = [0];
|
|
979
|
+
this.pushObjectActiveAddress(body, 0);
|
|
980
|
+
body.push(40, 2, 0, 11);
|
|
981
|
+
return body;
|
|
982
|
+
}
|
|
983
|
+
binarySetObjectActiveBody() {
|
|
984
|
+
const body = [0];
|
|
985
|
+
this.pushObjectActiveAddress(body, 0);
|
|
986
|
+
body.push(32, 1, 54, 2, 0, 11);
|
|
987
|
+
return body;
|
|
988
|
+
}
|
|
989
|
+
binaryUpdateBody(functionIndexes, loweredObjectCount) {
|
|
990
|
+
const body = [0];
|
|
991
|
+
body.push(32, 0, 36, ...this.encodeULEB(1));
|
|
992
|
+
body.push(35, ...this.encodeULEB(0), 65, ...this.encodeSLEB(1), 106);
|
|
993
|
+
body.push(36, ...this.encodeULEB(0));
|
|
994
|
+
const isActiveIndex = functionIndexes.get("is_object_active");
|
|
995
|
+
if (isActiveIndex === void 0) throw new Error("missing is_object_active function index");
|
|
996
|
+
for (let i = 0; i < loweredObjectCount; i++) {
|
|
997
|
+
const updateObjectIndex = functionIndexes.get(`update_object_${i}`);
|
|
998
|
+
if (updateObjectIndex === void 0) continue;
|
|
999
|
+
body.push(65, ...this.encodeSLEB(i), 16, ...this.encodeULEB(isActiveIndex));
|
|
1000
|
+
body.push(4, 64);
|
|
1001
|
+
body.push(32, 0, 16, ...this.encodeULEB(updateObjectIndex));
|
|
1002
|
+
body.push(11);
|
|
1003
|
+
}
|
|
1004
|
+
body.push(11);
|
|
1005
|
+
return body;
|
|
1006
|
+
}
|
|
1007
|
+
binaryUpdateObjectBody(updates, objectIndex) {
|
|
1008
|
+
const body = [0];
|
|
1009
|
+
for (const update of updates) {
|
|
1010
|
+
body.push(
|
|
1011
|
+
65,
|
|
1012
|
+
...this.encodeSLEB(update.eventId),
|
|
1013
|
+
65,
|
|
1014
|
+
...this.encodeSLEB(objectIndex),
|
|
1015
|
+
16,
|
|
1016
|
+
...this.encodeULEB(3)
|
|
1017
|
+
);
|
|
1018
|
+
}
|
|
1019
|
+
body.push(11);
|
|
1020
|
+
return body;
|
|
1021
|
+
}
|
|
1022
|
+
pushObjectActiveAddress(body, localIndex) {
|
|
1023
|
+
body.push(65, ...this.encodeSLEB(this.memoryLayout.objectsOffset));
|
|
1024
|
+
body.push(32, ...this.encodeULEB(localIndex));
|
|
1025
|
+
body.push(65, ...this.encodeSLEB(64), 108);
|
|
1026
|
+
body.push(65, ...this.encodeSLEB(4), 106);
|
|
1027
|
+
body.push(106);
|
|
1028
|
+
}
|
|
1029
|
+
pushI32StoreConst(body, offset, value) {
|
|
1030
|
+
body.push(65, ...this.encodeSLEB(offset), 65, ...this.encodeSLEB(value), 54, 2, 0);
|
|
1031
|
+
}
|
|
1032
|
+
pushWASMConst(body, type, value) {
|
|
1033
|
+
switch (type) {
|
|
1034
|
+
case "i32":
|
|
1035
|
+
body.push(65, ...this.encodeSLEB(Number(value)));
|
|
1036
|
+
break;
|
|
1037
|
+
case "i64":
|
|
1038
|
+
body.push(66, ...this.encodeSLEB(value));
|
|
1039
|
+
break;
|
|
1040
|
+
case "f32":
|
|
1041
|
+
body.push(67, ...this.f32Bytes(Number(value)));
|
|
1042
|
+
break;
|
|
1043
|
+
case "f64":
|
|
1044
|
+
body.push(68, ...this.f64Bytes(Number(value)));
|
|
1045
|
+
break;
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
pushWASMLoad(body, type) {
|
|
1049
|
+
const opcode = { i32: 40, i64: 41, f32: 42, f64: 43 }[type];
|
|
1050
|
+
const align = type === "i64" || type === "f64" ? 3 : 2;
|
|
1051
|
+
body.push(opcode, align, 0);
|
|
1052
|
+
}
|
|
1053
|
+
pushWASMStore(body, type) {
|
|
1054
|
+
const opcode = { i32: 54, i64: 55, f32: 56, f64: 57 }[type];
|
|
1055
|
+
const align = type === "i64" || type === "f64" ? 3 : 2;
|
|
1056
|
+
body.push(opcode, align, 0);
|
|
1057
|
+
}
|
|
1058
|
+
binarySection(id, payload) {
|
|
1059
|
+
return [id, ...this.encodeULEB(payload.length), ...payload];
|
|
1060
|
+
}
|
|
1061
|
+
binaryVector(items) {
|
|
1062
|
+
return [...this.encodeULEB(items.length), ...items.flat()];
|
|
1063
|
+
}
|
|
1064
|
+
encodeString(value) {
|
|
1065
|
+
const bytes = Array.from(new TextEncoder().encode(value));
|
|
1066
|
+
return [...this.encodeULEB(bytes.length), ...bytes];
|
|
1067
|
+
}
|
|
1068
|
+
encodeULEB(value) {
|
|
1069
|
+
const bytes = [];
|
|
1070
|
+
let next = value >>> 0;
|
|
1071
|
+
do {
|
|
1072
|
+
let byte = next & 127;
|
|
1073
|
+
next >>>= 7;
|
|
1074
|
+
if (next !== 0) byte |= 128;
|
|
1075
|
+
bytes.push(byte);
|
|
1076
|
+
} while (next !== 0);
|
|
1077
|
+
return bytes;
|
|
1078
|
+
}
|
|
1079
|
+
encodeSLEB(value) {
|
|
1080
|
+
const bytes = [];
|
|
1081
|
+
let next = BigInt(value);
|
|
1082
|
+
let more = true;
|
|
1083
|
+
while (more) {
|
|
1084
|
+
let byte = Number(next & 0x7fn);
|
|
1085
|
+
next >>= 7n;
|
|
1086
|
+
const signBitSet = (byte & 64) !== 0;
|
|
1087
|
+
more = !(next === 0n && !signBitSet || next === -1n && signBitSet);
|
|
1088
|
+
if (more) byte |= 128;
|
|
1089
|
+
bytes.push(byte);
|
|
1090
|
+
}
|
|
1091
|
+
return bytes;
|
|
1092
|
+
}
|
|
1093
|
+
f32Bytes(value) {
|
|
1094
|
+
const bytes = new Uint8Array(4);
|
|
1095
|
+
new DataView(bytes.buffer).setFloat32(0, value, true);
|
|
1096
|
+
return Array.from(bytes);
|
|
1097
|
+
}
|
|
1098
|
+
f64Bytes(value) {
|
|
1099
|
+
const bytes = new Uint8Array(8);
|
|
1100
|
+
new DataView(bytes.buffer).setFloat64(0, value, true);
|
|
1101
|
+
return Array.from(bytes);
|
|
1102
|
+
}
|
|
1103
|
+
wasmBinaryValueType(type) {
|
|
1104
|
+
switch (type) {
|
|
1105
|
+
case "i32":
|
|
1106
|
+
return 127;
|
|
1107
|
+
case "i64":
|
|
1108
|
+
return 126;
|
|
1109
|
+
case "f32":
|
|
1110
|
+
return 125;
|
|
1111
|
+
case "f64":
|
|
1112
|
+
return 124;
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
// ===========================================================================
|
|
1116
|
+
// HELPERS
|
|
1117
|
+
// ===========================================================================
|
|
1118
|
+
emit(line) {
|
|
1119
|
+
const indent = " ".repeat(this.indentLevel);
|
|
1120
|
+
this.lines.push(indent + line);
|
|
1121
|
+
}
|
|
1122
|
+
indent() {
|
|
1123
|
+
this.indentLevel++;
|
|
1124
|
+
}
|
|
1125
|
+
dedent() {
|
|
1126
|
+
this.indentLevel = Math.max(0, this.indentLevel - 1);
|
|
1127
|
+
}
|
|
1128
|
+
inferWASMType(value) {
|
|
1129
|
+
if (typeof value === "boolean") return "i32";
|
|
1130
|
+
if (typeof value === "number") {
|
|
1131
|
+
return Number.isInteger(value) ? "i32" : "f32";
|
|
1132
|
+
}
|
|
1133
|
+
if (typeof value === "bigint") return "i64";
|
|
1134
|
+
if (typeof value === "string") return "i32";
|
|
1135
|
+
if (Array.isArray(value)) return "i32";
|
|
1136
|
+
return "i32";
|
|
1137
|
+
}
|
|
1138
|
+
getTypeSize(type) {
|
|
1139
|
+
switch (type) {
|
|
1140
|
+
case "i32":
|
|
1141
|
+
case "f32":
|
|
1142
|
+
return 4;
|
|
1143
|
+
case "i64":
|
|
1144
|
+
case "f64":
|
|
1145
|
+
return 8;
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
valueToWASM(value, _type) {
|
|
1149
|
+
if (typeof value === "boolean") return value ? 1 : 0;
|
|
1150
|
+
if (typeof value === "number") return value;
|
|
1151
|
+
if (typeof value === "bigint") return value;
|
|
1152
|
+
if (typeof value === "string") {
|
|
1153
|
+
if (!this.stringPool.has(value)) {
|
|
1154
|
+
this.stringPool.set(value, this.nextStringOffset);
|
|
1155
|
+
this.nextStringOffset += value.length + 1;
|
|
1156
|
+
}
|
|
1157
|
+
return this.stringPool.get(value);
|
|
1158
|
+
}
|
|
1159
|
+
return 0;
|
|
1160
|
+
}
|
|
1161
|
+
wasmTypeToJS(type) {
|
|
1162
|
+
switch (type) {
|
|
1163
|
+
case "i32":
|
|
1164
|
+
case "f32":
|
|
1165
|
+
return "number";
|
|
1166
|
+
case "i64":
|
|
1167
|
+
return "bigint";
|
|
1168
|
+
case "f64":
|
|
1169
|
+
return "number";
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
sanitizeName(name) {
|
|
1173
|
+
return name.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
1174
|
+
}
|
|
1175
|
+
escapeWATString(str) {
|
|
1176
|
+
return str.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t");
|
|
1177
|
+
}
|
|
1178
|
+
};
|
|
1179
|
+
exports.WASMCompiler_default = exports.WASMCompiler;
|
|
1180
|
+
}
|
|
1181
|
+
});
|
|
1182
|
+
|
|
1183
|
+
exports.WASMCompiler_exports = WASMCompiler_exports;
|
|
1184
|
+
exports.compileASTToWASM = compileASTToWASM;
|
|
1185
|
+
exports.compileToWASM = compileToWASM;
|
|
1186
|
+
exports.createWASMCompiler = createWASMCompiler;
|
|
1187
|
+
exports.init_WASMCompiler = init_WASMCompiler;
|
|
1188
|
+
//# sourceMappingURL=chunk-JJBE3TIH.cjs.map
|
|
1189
|
+
//# sourceMappingURL=chunk-JJBE3TIH.cjs.map
|