@overmvp/kumi 0.0.0-stage → 0.3.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.
Files changed (319) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +67 -2
  3. package/animation/color-transition.ts +196 -0
  4. package/animation/cycle.ts +151 -0
  5. package/animation/ease.ts +29 -0
  6. package/animation/index.ts +13 -0
  7. package/animation/package.json +12 -0
  8. package/animation/presets.ts +376 -0
  9. package/animation/spec.ts +197 -0
  10. package/builder/Cargo.lock +192 -0
  11. package/builder/Cargo.toml +7 -0
  12. package/builder/css-v1.mjs +387 -0
  13. package/builder/css-v1.test.mjs +361 -0
  14. package/builder/generated-output.mjs +61 -0
  15. package/builder/generated-output.test.mjs +132 -0
  16. package/builder/jsx-form.mjs +93 -0
  17. package/builder/jsx-form.test.mjs +48 -0
  18. package/builder/jsx-lists.mjs +209 -0
  19. package/builder/link-addon-package.mjs +127 -0
  20. package/builder/lua-assets.mjs +74 -0
  21. package/builder/lua-builtin-values.mjs +57 -0
  22. package/builder/lua-builtin-values.test.mjs +155 -0
  23. package/builder/lua-loop-closures.mjs +183 -0
  24. package/builder/lua-loop-closures.test.mjs +89 -0
  25. package/builder/lua-truthiness.mjs +61 -0
  26. package/builder/lua-truthiness.test.mjs +113 -0
  27. package/builder/module-declarations.mjs +594 -0
  28. package/builder/module-declarations.test.mjs +435 -0
  29. package/builder/native-kernels.mjs +60 -0
  30. package/builder/native-lua.d.ts +57 -0
  31. package/builder/native-routes.mjs +51 -0
  32. package/builder/no-static-ref-read.mjs +93 -0
  33. package/builder/no-static-ref-read.test.mjs +181 -0
  34. package/builder/secure-snippet.mjs +317 -0
  35. package/builder/secure-snippet.test.mjs +101 -0
  36. package/builder/shared-lua-runtime.mjs +150 -0
  37. package/builder/src/addon-link.lua +29 -0
  38. package/builder/src/arrays.lua +260 -0
  39. package/builder/src/assets.rs +177 -0
  40. package/builder/src/classes.lua +75 -0
  41. package/builder/src/collections.lua +190 -0
  42. package/builder/src/compiler.rs +2336 -0
  43. package/builder/src/config.rs +42 -0
  44. package/builder/src/entries.rs +166 -0
  45. package/builder/src/errors.lua +15 -0
  46. package/builder/src/lua_data.rs +231 -0
  47. package/builder/src/main.rs +381 -0
  48. package/builder/src/native_ir.rs +65 -0
  49. package/builder/src/native_signal.rs +83 -0
  50. package/builder/src/numbers.lua +76 -0
  51. package/builder/src/project-front.mjs +3682 -0
  52. package/builder/src/records.lua +75 -0
  53. package/builder/src/routes.rs +124 -0
  54. package/builder/src/setup-store.lua +18 -0
  55. package/builder/src/signal-kernel.fingerprint +1 -0
  56. package/builder/src/strings.lua +103 -0
  57. package/builder/src/ts-lower.mjs +68 -0
  58. package/builder/src/wow-reads.lua +22 -0
  59. package/builder/src/wow_client.rs +253 -0
  60. package/builder/src/wow_types.rs +564 -0
  61. package/builder/style-modules.mjs +93 -0
  62. package/builder/style-states.mjs +100 -0
  63. package/builder/style-states.test.mjs +103 -0
  64. package/builder/vite-assets.mjs +53 -0
  65. package/builder/vite-project.mjs +10 -0
  66. package/config.ts +59 -0
  67. package/eslint.mjs +74 -0
  68. package/form/index.ts +526 -0
  69. package/form/package.json +13 -0
  70. package/motion/index.ts +2 -0
  71. package/motion/package.json +10 -0
  72. package/package.json +26 -4
  73. package/router/index.ts +50 -0
  74. package/router/navigation.ts +22 -0
  75. package/router/package.json +12 -0
  76. package/router/router-view.ts +134 -0
  77. package/router/routes.ts +64 -0
  78. package/scripts/addon-build.mjs +364 -0
  79. package/scripts/addon-project.mjs +78 -0
  80. package/scripts/kumi-cli.mjs +105 -0
  81. package/scripts/link-addons.mjs +61 -0
  82. package/src/bindings.ts +35 -0
  83. package/src/context.ts +15 -0
  84. package/src/css-tokens.ts +239 -0
  85. package/src/css-v1.ts +68 -0
  86. package/src/cvars.ts +91 -0
  87. package/src/element-node.ts +32 -0
  88. package/src/elements.ts +505 -0
  89. package/src/feature-scope.ts +82 -0
  90. package/src/flex-layout.ts +807 -0
  91. package/src/frame-scheduler.ts +336 -0
  92. package/src/grid-tracks.ts +165 -0
  93. package/src/image-imports.d.ts +24 -0
  94. package/src/images.ts +79 -0
  95. package/src/index.ts +29 -0
  96. package/src/jsx-dev-runtime.ts +2 -0
  97. package/src/jsx-runtime.ts +29 -0
  98. package/src/jsx.d.ts +12 -0
  99. package/src/keyed-list.ts +366 -0
  100. package/src/media-query.ts +144 -0
  101. package/src/module-ui.ts +147 -0
  102. package/src/module.ts +311 -0
  103. package/src/node-selectors.ts +114 -0
  104. package/src/owned-collections.ts +52 -0
  105. package/src/providers.ts +241 -0
  106. package/src/reactive-scope.ts +34 -0
  107. package/src/reactivity.ts +275 -0
  108. package/src/result.ts +64 -0
  109. package/src/runtime.ts +52 -0
  110. package/src/scheduler.ts +185 -0
  111. package/src/signal-kernel.ts +63 -0
  112. package/src/style-classes.ts +51 -0
  113. package/src/style-states.ts +134 -0
  114. package/src/style-variables.ts +53 -0
  115. package/src/text-children.ts +42 -0
  116. package/src/theme.ts +50 -0
  117. package/src/transition.ts +18 -0
  118. package/state/index.ts +9 -0
  119. package/state/package.json +12 -0
  120. package/state/setup-store-persistence.ts +206 -0
  121. package/state/setup-store-view.ts +10 -0
  122. package/state/setup-store.ts +342 -0
  123. package/state/store-bindings.ts +25 -0
  124. package/state/store.ts +84 -0
  125. package/styles/_layers.scss +14 -0
  126. package/templates/addon/.agents/skills/kumi-build-debug/SKILL.md +36 -0
  127. package/templates/addon/.agents/skills/kumi-feature/SKILL.md +50 -0
  128. package/templates/addon/.agents/skills/kumi-plugin/SKILL.md +36 -0
  129. package/templates/addon/.agents/skills/kumi-store/SKILL.md +47 -0
  130. package/templates/addon/.agents/skills/kumi-ui/SKILL.md +73 -0
  131. package/templates/addon/.agents/skills/kumi-wow-api/SKILL.md +30 -0
  132. package/templates/addon/.claude/skills/kumi-build-debug/SKILL.md +36 -0
  133. package/templates/addon/.claude/skills/kumi-feature/SKILL.md +50 -0
  134. package/templates/addon/.claude/skills/kumi-plugin/SKILL.md +36 -0
  135. package/templates/addon/.claude/skills/kumi-store/SKILL.md +47 -0
  136. package/templates/addon/.claude/skills/kumi-ui/SKILL.md +73 -0
  137. package/templates/addon/.claude/skills/kumi-wow-api/SKILL.md +30 -0
  138. package/templates/addon/AGENTS.md +34 -0
  139. package/templates/addon/Assets/Art/Corner10.tga +0 -0
  140. package/templates/addon/Assets/Art/Corner11.tga +0 -0
  141. package/templates/addon/Assets/Art/Corner12.tga +0 -0
  142. package/templates/addon/Assets/Art/Corner13.tga +0 -0
  143. package/templates/addon/Assets/Art/Corner14.tga +0 -0
  144. package/templates/addon/Assets/Art/Corner15.tga +0 -0
  145. package/templates/addon/Assets/Art/Corner16.tga +0 -0
  146. package/templates/addon/Assets/Art/Corner2.tga +0 -0
  147. package/templates/addon/Assets/Art/Corner3.tga +0 -0
  148. package/templates/addon/Assets/Art/Corner4.tga +0 -0
  149. package/templates/addon/Assets/Art/Corner5.tga +0 -0
  150. package/templates/addon/Assets/Art/Corner6.tga +0 -0
  151. package/templates/addon/Assets/Art/Corner7.tga +0 -0
  152. package/templates/addon/Assets/Art/Corner8.tga +0 -0
  153. package/templates/addon/Assets/Art/Corner9.tga +0 -0
  154. package/templates/addon/Assets/Art/Dot.tga +0 -0
  155. package/templates/addon/Assets/Art/Gear.tga +0 -0
  156. package/templates/addon/Assets/Art/Grip.tga +0 -0
  157. package/templates/addon/Assets/Art/Outline10.tga +0 -0
  158. package/templates/addon/Assets/Art/Outline12.tga +0 -0
  159. package/templates/addon/Assets/Art/Outline13.tga +0 -0
  160. package/templates/addon/Assets/Art/Outline14.tga +0 -0
  161. package/templates/addon/Assets/Art/Outline15.tga +0 -0
  162. package/templates/addon/Assets/Art/Outline2.tga +0 -0
  163. package/templates/addon/Assets/Art/Outline3.tga +0 -0
  164. package/templates/addon/Assets/Art/Outline5.tga +0 -0
  165. package/templates/addon/Assets/Art/Outline7.tga +0 -0
  166. package/templates/addon/Assets/Art/Outline9.tga +0 -0
  167. package/templates/addon/Assets/Art/Round11.tga +0 -0
  168. package/templates/addon/Assets/Art/Round11Border.tga +0 -0
  169. package/templates/addon/Assets/Art/Round4.tga +0 -0
  170. package/templates/addon/Assets/Art/Round4Border.tga +0 -0
  171. package/templates/addon/Assets/Art/Round6.tga +0 -0
  172. package/templates/addon/Assets/Art/Round6Border.tga +0 -0
  173. package/templates/addon/Assets/Art/Round8.tga +0 -0
  174. package/templates/addon/Assets/Art/Round8Border.tga +0 -0
  175. package/templates/addon/Assets/Art/Search.tga +0 -0
  176. package/templates/addon/CLAUDE.md +1 -0
  177. package/templates/addon/README.md +49 -0
  178. package/templates/addon/eslint.config.mjs +6 -0
  179. package/templates/addon/gitignore +9 -0
  180. package/templates/addon/kumi.config.ts +17 -0
  181. package/templates/addon/package.json +20 -0
  182. package/templates/addon/src/app/app.module.ts +18 -0
  183. package/templates/addon/src/app/commands.store.ts +11 -0
  184. package/templates/addon/src/app/layout/AppWindow.module.scss +92 -0
  185. package/templates/addon/src/app/layout/AppWindow.tsx +32 -0
  186. package/templates/addon/src/app/main.ts +5 -0
  187. package/templates/addon/src/app/routes/(window)/home/page.tsx +8 -0
  188. package/templates/addon/src/app/routes/(window)/plugins/page.tsx +38 -0
  189. package/templates/addon/src/app/routes/(window)/settings/page.tsx +8 -0
  190. package/templates/addon/src/app/theme.ts +6 -0
  191. package/templates/addon/src/modules/greeting/api/player.api.ts +29 -0
  192. package/templates/addon/src/modules/greeting/greeting.module.ts +18 -0
  193. package/templates/addon/src/modules/greeting/model/greeting.store.ts +81 -0
  194. package/templates/addon/src/modules/greeting/services/greeting.service.ts +38 -0
  195. package/templates/addon/src/modules/greeting/ui/Greeting.module.scss +105 -0
  196. package/templates/addon/src/modules/greeting/ui/GreetingCard.tsx +29 -0
  197. package/templates/addon/src/modules/greeting/widgets/GreetingHud.tsx +54 -0
  198. package/templates/addon/src/modules/greeting/widgets/GreetingOverview.tsx +18 -0
  199. package/templates/addon/src/modules/greeting/widgets/GreetingSettings.tsx +56 -0
  200. package/templates/addon/src/modules/playtime/api/clock.api.ts +6 -0
  201. package/templates/addon/src/modules/playtime/model/playtime.store.ts +19 -0
  202. package/templates/addon/src/modules/playtime/playtime.module.ts +18 -0
  203. package/templates/addon/src/modules/playtime/services/playtime.service.ts +18 -0
  204. package/templates/addon/src/modules/playtime/ui/Playtime.module.scss +25 -0
  205. package/templates/addon/src/modules/playtime/ui/PlaytimeBadge.tsx +10 -0
  206. package/templates/addon/src/modules/playtime/widgets/PlaytimePage.tsx +16 -0
  207. package/templates/addon/src/shared/api/native-art.ts +28 -0
  208. package/templates/addon/src/shared/assets/close.png +0 -0
  209. package/templates/addon/src/shared/lib/format.ts +20 -0
  210. package/templates/addon/src/shared/styles/_tokens.scss +12 -0
  211. package/templates/addon/src/shared/ui/Button.tsx +29 -0
  212. package/templates/addon/src/shared/ui/Choice.tsx +34 -0
  213. package/templates/addon/src/shared/ui/Controls.module.scss +93 -0
  214. package/templates/addon/src/shared/ui/Glyph.tsx +18 -0
  215. package/templates/addon/src/shared/ui/Slider.tsx +25 -0
  216. package/templates/addon/src/shared/ui/Toggle.tsx +29 -0
  217. package/templates/addon/src/shared/ui/focus-ref.ts +7 -0
  218. package/templates/addon/src/shared/ui/range-skin.tsx +15 -0
  219. package/templates/addon/tsconfig.json +63 -0
  220. package/translate/index.ts +14 -0
  221. package/translate/package.json +9 -0
  222. package/utils/colors.ts +76 -0
  223. package/utils/format.ts +37 -0
  224. package/utils/index.ts +4 -0
  225. package/utils/number.ts +17 -0
  226. package/utils/package.json +7 -0
  227. package/utils/text.ts +6 -0
  228. package/validate/index.ts +119 -0
  229. package/validate/package.json +9 -0
  230. package/wow/absolute-position.ts +142 -0
  231. package/wow/action-bar-layout.ts +108 -0
  232. package/wow/action-button.ts +219 -0
  233. package/wow/action-catalog.ts +98 -0
  234. package/wow/addon-manifest.ts +36 -0
  235. package/wow/animation.ts +428 -0
  236. package/wow/app-ui.ts +221 -0
  237. package/wow/aura-button.ts +76 -0
  238. package/wow/aura-container.ts +591 -0
  239. package/wow/aura-countdown.ts +201 -0
  240. package/wow/aura-slot.ts +153 -0
  241. package/wow/bar-shade.ts +93 -0
  242. package/wow/borrowed-slot.ts +216 -0
  243. package/wow/button-drag.ts +48 -0
  244. package/wow/cast-clock.ts +153 -0
  245. package/wow/cast-text.ts +83 -0
  246. package/wow/class-colors.ts +126 -0
  247. package/wow/clock.ts +7 -0
  248. package/wow/color-picker.ts +128 -0
  249. package/wow/commands.ts +17 -0
  250. package/wow/contained-frame.ts +128 -0
  251. package/wow/contract.ts +42 -0
  252. package/wow/cooldown-timing.ts +24 -0
  253. package/wow/css-box.ts +144 -0
  254. package/wow/css-color.ts +12 -0
  255. package/wow/css-style.ts +109 -0
  256. package/wow/css-surfaces.ts +603 -0
  257. package/wow/css-text.ts +399 -0
  258. package/wow/cursor.ts +45 -0
  259. package/wow/cvar-schema.ts +32 -0
  260. package/wow/cvars.ts +98 -0
  261. package/wow/dropdown-art.ts +39 -0
  262. package/wow/element-click.ts +20 -0
  263. package/wow/element-event-coordinator.ts +219 -0
  264. package/wow/element-events.ts +317 -0
  265. package/wow/event-dispatcher.ts +151 -0
  266. package/wow/events.ts +50 -0
  267. package/wow/font-cache.ts +92 -0
  268. package/wow/frame-access.ts +53 -0
  269. package/wow/frame-layer.ts +5 -0
  270. package/wow/frame-timing.ts +13 -0
  271. package/wow/frames.ts +236 -0
  272. package/wow/heal-prediction.ts +305 -0
  273. package/wow/hooks.ts +133 -0
  274. package/wow/image-layers.ts +55 -0
  275. package/wow/index.ts +111 -0
  276. package/wow/layout-view.ts +663 -0
  277. package/wow/locale-font.ts +15 -0
  278. package/wow/lua-string.ts +18 -0
  279. package/wow/lua-table.ts +18 -0
  280. package/wow/macro-book.ts +84 -0
  281. package/wow/map-position.ts +27 -0
  282. package/wow/media-query.ts +65 -0
  283. package/wow/module-events.ts +11 -0
  284. package/wow/native-hooks.ts +168 -0
  285. package/wow/native-placement.ts +88 -0
  286. package/wow/native.ts +153 -0
  287. package/wow/number-format.ts +8 -0
  288. package/wow/number-input.ts +10 -0
  289. package/wow/panel-host.ts +97 -0
  290. package/wow/pixel.ts +35 -0
  291. package/wow/plugins.ts +246 -0
  292. package/wow/protect.ts +48 -0
  293. package/wow/protected-layout.ts +133 -0
  294. package/wow/protected-view.ts +103 -0
  295. package/wow/range-art.ts +81 -0
  296. package/wow/readable-api.ts +120 -0
  297. package/wow/reparent-frame.ts +99 -0
  298. package/wow/rounded-texture.ts +183 -0
  299. package/wow/saved-variables.ts +109 -0
  300. package/wow/scroll-view.ts +64 -0
  301. package/wow/secret-ref.ts +14 -0
  302. package/wow/secure-bindings.ts +89 -0
  303. package/wow/secure-clicks.ts +432 -0
  304. package/wow/secure-snippet.ts +235 -0
  305. package/wow/secure-state.ts +130 -0
  306. package/wow/secure-target-visibility.ts +89 -0
  307. package/wow/secure-transaction.ts +331 -0
  308. package/wow/special-window.ts +269 -0
  309. package/wow/spell-cooldown.ts +36 -0
  310. package/wow/start-addon.ts +122 -0
  311. package/wow/state-paint.ts +256 -0
  312. package/wow/transition.ts +86 -0
  313. package/wow/unit-button.ts +39 -0
  314. package/wow/unit-cast.ts +92 -0
  315. package/wow/unit-health-text.ts +47 -0
  316. package/wow/view-nodes.ts +304 -0
  317. package/wow/view.ts +1864 -0
  318. package/wow/visible-frame-job.ts +34 -0
  319. package/wow/width-reserve.ts +108 -0
@@ -0,0 +1,101 @@
1
+ import assert from "node:assert/strict";
2
+ import { fileURLToPath } from "node:url";
3
+ import test from "node:test";
4
+ import ts from "typescript";
5
+ import { transformSecureSnippets } from "./secure-snippet.mjs";
6
+
7
+ const normalize = path => path.replaceAll("\\", "/");
8
+ const path = normalize(fileURLToPath(new URL("./secure-snippet.fixture.ts", import.meta.url)));
9
+ const declarations = normalize(fileURLToPath(new URL("./secure-snippet.fixture.d.ts", import.meta.url)));
10
+ const prelude = 'import { secureSnippet as compile } from "@kumi/wow";\n';
11
+
12
+ function transform(body) {
13
+ const source = prelude + body;
14
+ const files = new Map([[path, source], [declarations, `
15
+ declare module "@kumi/wow" {
16
+ export interface Handle {
17
+ GetFrameRef(key: string): Handle | undefined;
18
+ SetAttribute(key: string, value: string): void;
19
+ Hide(): void;
20
+ }
21
+ export function secureSnippet(body: (frame: Handle, button: string, down: boolean, control: Handle) => unknown,
22
+ options?: { event?: string; refs?: Record<string, object> }): object;
23
+ }
24
+ `]]);
25
+ const options = { target: ts.ScriptTarget.ES2020, strict: true, noEmit: true, skipLibCheck: true };
26
+ const host = ts.createCompilerHost(options);
27
+ const get = host.getSourceFile;
28
+ host.getSourceFile = (name, language, ...rest) => {
29
+ const input = files.get(normalize(name));
30
+ return input === undefined ? get(name, language, ...rest) : ts.createSourceFile(name, input, language, true);
31
+ };
32
+ return transformSecureSnippets(ts, path, source, { program: ts.createProgram([...files.keys()], options, host) });
33
+ }
34
+
35
+ function bodies(output) {
36
+ return [...output.matchAll(/compile\(("(?:[^"\\]|\\.)*") as never/g)].map(([, body]) => JSON.parse(body));
37
+ }
38
+
39
+ test("emits standalone Lua using client arguments and preserving multiple returns", () => {
40
+ const output = transform(`declare const popup: object;
41
+ const snippet = compile((_buttonFrame, clicked, pressed, header) => {
42
+ const target = header.GetFrameRef("popup");
43
+ if (target !== undefined && clicked === "RightButton" && pressed) {
44
+ target.Hide();
45
+ return false;
46
+ }
47
+ return [undefined, true];
48
+ }, { refs: { popup } });
49
+ `);
50
+ assert.deepEqual(bodies(output), [[
51
+ 'local local1_target = control:GetFrameRef("popup")',
52
+ 'if (((local1_target ~= nil) and (button == "RightButton")) and down) then',
53
+ ' local1_target:Hide()',
54
+ ' return false',
55
+ 'end',
56
+ 'return nil, true',
57
+ ].join("\n")]);
58
+ assert.match(output, /refs: \{ popup \}/);
59
+ assert.doesNotMatch(output, /__TS__|=>/);
60
+ });
61
+
62
+ test("emits a local loop and Lua byte escapes for control characters", () => {
63
+ const output = transform(`compile(frame => {
64
+ for (let index = 1; index < 3; index++) {
65
+ frame.SetAttribute("slot-" + index, "a\\n\\x01");
66
+ }
67
+ });`);
68
+ assert.deepEqual(bodies(output), [[
69
+ 'do',
70
+ ' local local1_index = 1',
71
+ ' while (local1_index < 3) do',
72
+ ' do',
73
+ ' self:SetAttribute(("slot-" .. local1_index), "a\\010\\001")',
74
+ ' end',
75
+ ' local1_index = local1_index + 1',
76
+ ' end',
77
+ 'end',
78
+ ].join("\n")]);
79
+ });
80
+
81
+ test("reports unsupported bindings and expressions at their original locations", async t => {
82
+ const cases = [
83
+ ["captured local", 'const outside = false;\ncompile(() => outside);', /:3:\d+: Secure snippet cannot capture outside/],
84
+ ["native dynamic execution", 'compile(frame => frame.Run("return true"));', /:2:\d+: Expected an allowed restricted function/],
85
+ ["unknown global", 'compile(() => loadstring("return true"));', /:2:\d+: Expected an allowed restricted function/],
86
+ ["undeclared WoW global", 'compile(() => GetMouseFocus());', /:2:\d+: Expected an allowed restricted function/],
87
+ ["undeclared identifier", 'compile(() => foo);', /:2:\d+: Secure snippet cannot capture foo/],
88
+ ["undeclared library function", 'compile(() => format("x"));', /:2:\d+: Expected an allowed restricted function/],
89
+ ["dynamic ref", 'compile((frame, key) => frame.GetFrameRef(key));', /:2:\d+: GetFrameRef requires a literal key declared in refs/],
90
+ ["ref receiver", 'compile(frame => frame.GetFrameRef("popup"), { refs: { popup: {} } });', /:2:\d+: Read declared frame references through the control parameter/],
91
+ ["optional chaining", 'compile(frame => frame.GetFrameRef("popup")?.Hide());', /:2:\d+: Optional calls are unsupported/],
92
+ ["spread", 'compile(frame => frame.SetAttribute(...["key", "value"]));', /:2:\d+: Unsupported secure expression SpreadElement/],
93
+ ["script attributes", 'compile(frame => frame.SetAttribute("_onclick", "bad"));', /:2:\d+: Secure handlers cannot replace scripts/],
94
+ ["marker alias", 'const indirect = compile;', /:2:\d+: Use secureSnippet as a direct call/],
95
+ ["handwritten text", 'compile("return false" as never);', /:2:\d+: Pass a synchronous inline arrow/],
96
+ ["type error", 'compile(frame => frame.SetAttribute("field", 42));', /:2:\d+: Argument of type 'number'/],
97
+ ];
98
+ for (const [name, input, error] of cases) {
99
+ await t.test(name, () => assert.throws(() => transform(input), error));
100
+ }
101
+ });
@@ -0,0 +1,150 @@
1
+ import { createHash } from "node:crypto";
2
+
3
+ export const sharedLuaKey = "lualib_bundle";
4
+ export const sharedLuaPath = "KumiGenerated/SharedRuntime.lua";
5
+ const expectedCompiler = "1.37.1";
6
+ const schema = 1;
7
+ const standardGlobals = new Set([
8
+ "_G", "_VERSION", "coroutine", "debug", "error", "getmetatable", "ipairs", "math", "next", "pairs",
9
+ "rawget", "rawset", "select", "setmetatable", "string", "table", "tonumber", "tostring", "type", "unpack", "xpcall",
10
+ ]);
11
+ const globalMembers = new Set([
12
+ "pcall", "coroutine", "getmetatable", "rawget", "rawset", "__TS__sourcemap", "__TS__originalTraceback",
13
+ ]);
14
+
15
+ function parseChunk(parse, text) {
16
+ return parse(text, { luaVersion: "5.1", scope: true, ranges: true, comments: false });
17
+ }
18
+
19
+ function visit(node, callback, parent, depth = 0) {
20
+ callback(node, parent, depth);
21
+ const nested = depth + (node.type === "FunctionDeclaration" ? 1 : 0);
22
+ for (const [property, value] of Object.entries(node)) {
23
+ if (property === "globals") continue;
24
+ if (Array.isArray(value)) {
25
+ for (const child of value) if (child?.type) visit(child, callback, node, nested);
26
+ } else if (value?.type) visit(value, callback, node, nested);
27
+ }
28
+ }
29
+
30
+ function checkFactoryContext(chunk) {
31
+ for (const binding of chunk.globals) {
32
+ if (!standardGlobals.has(binding.name)) throw new Error(`Shared ${sharedLuaKey} reads unbound ${binding.name}`);
33
+ }
34
+ visit(chunk, (node, parent, depth) => {
35
+ if (node.type === "VarargLiteral" && depth === 0) throw new Error(`Shared ${sharedLuaKey} reads consumer varargs`);
36
+ if (node.type === "Identifier" && node.name === "_G" && node.isLocal === false &&
37
+ (parent?.type !== "MemberExpression" || parent.base !== node || !globalMembers.has(parent.identifier.name))) {
38
+ throw new Error(`Shared ${sharedLuaKey} reads a non-portable global table member`);
39
+ }
40
+ });
41
+ }
42
+
43
+ function listExports(body) {
44
+ const returned = body.at(-1);
45
+ const table = returned?.type === "ReturnStatement" && returned.arguments.length === 1 && returned.arguments[0];
46
+ if (table?.type !== "TableConstructorExpression") throw new Error(`Shared ${sharedLuaKey} must return a named export table`);
47
+ const bindings = new Map();
48
+ for (const field of table.fields) {
49
+ const name = field.type === "TableKeyString" ? field.key.name : undefined;
50
+ if (!name || field.value.type !== "Identifier" || field.value.isLocal !== true ||
51
+ (bindings.has(name) && bindings.get(name) !== field.value.name)) {
52
+ throw new Error(`Invalid shared ${sharedLuaKey} export ${name ?? "<unnamed>"}`);
53
+ }
54
+ // TSTL 1.37.1 repeats the identical ParseFloat and ParseInt export bindings.
55
+ bindings.set(name, field.value.name);
56
+ }
57
+ if (bindings.size === 0) throw new Error(`Shared ${sharedLuaKey} has no exports`);
58
+ return [...bindings.keys()].sort();
59
+ }
60
+
61
+ export function createSharedLuaRuntime({ parse, library, compiler, target }) {
62
+ if (compiler !== expectedCompiler || target !== "5.1") {
63
+ throw new Error(`Shared Lua requires TSTL ${expectedCompiler} / Lua 5.1; got TSTL ${compiler} / Lua ${target}. Update the pin and rebuild every bundle together.`);
64
+ }
65
+ const normalized = library.replaceAll("\r\n", "\n").trimEnd();
66
+ // TSTL 1.37.1 guards stack collection but not Error stringification. WoW has no debug table.
67
+ const callerLookup = 'local caller = debug.getinfo(3, "f")';
68
+ const callerIndex = normalized.indexOf(callerLookup);
69
+ const factory = normalized.replace(callerLookup,
70
+ `if debug == nil then return description end\n ${callerLookup}`);
71
+ const chunk = parseChunk(parse, factory);
72
+ checkFactoryContext(chunk);
73
+ const exports = listExports(chunk.body);
74
+ if (exports.includes("Error") && (callerIndex === -1 || normalized.indexOf(callerLookup, callerIndex + callerLookup.length) !== -1)) {
75
+ throw new Error("Pinned TSTL Error stringifier has changed");
76
+ }
77
+ const manifest = { schema, compiler, target, modules: [{ key: sharedLuaKey, exports, hash: sha256(factory) }] };
78
+ const stamp = `kumi-shared/${schema};tstl=${compiler};lua=${target};sha256=${sha256(JSON.stringify(manifest))}`;
79
+ const quotedStamp = JSON.stringify(stamp);
80
+ const source = `local _, addonTable = ...
81
+ local function libraryFactory()
82
+ ${factory}
83
+ end
84
+ local libraryExports
85
+ local function sharedRequire(key)
86
+ if key ~= "${sharedLuaKey}" then error("Unknown shared Lua module " .. tostring(key), 2) end
87
+ if libraryExports == nil then libraryExports = libraryFactory() end
88
+ return libraryExports
89
+ end
90
+ assert(type(addonTable) == "table", "Shared Lua requires the core addon table")
91
+ assert(addonTable.KumiSharedLua == nil, "Duplicate shared Lua registry")
92
+ addonTable.KumiSharedLua = {
93
+ Stamp = ${quotedStamp},
94
+ Require = sharedRequire,
95
+ }
96
+ `;
97
+ return { manifest, stamp, source };
98
+ }
99
+
100
+ function sha256(value) { return createHash("sha256").update(value).digest("hex"); }
101
+
102
+ function sharedField(field) {
103
+ return field.type === "TableKey" && /^(["'])lualib_bundle\1$/.test(field.key.raw ?? "");
104
+ }
105
+
106
+ export function externalizeSharedLuaBundle(lua, { parse, runtime }) {
107
+ const tree = parseChunk(parse, lua);
108
+ const registry = tree.body.find(node => node.type === "AssignmentStatement" && node.variables[0]?.name === "____modules")?.init[0];
109
+ if (registry?.type !== "TableConstructorExpression") throw new Error("TSTL Lua module registry is missing");
110
+ const shared = registry.fields.filter(sharedField);
111
+ if (shared.length > 1) throw new Error(`Duplicate bundled ${sharedLuaKey}`);
112
+ const [entry] = shared;
113
+ if (!entry) return lua;
114
+ if (entry.value.type !== "FunctionDeclaration") throw new Error(`Invalid bundled ${sharedLuaKey} loader`);
115
+ const available = new Set(runtime.manifest.modules[0].exports);
116
+ for (const name of listExports(entry.value.body)) {
117
+ if (!available.has(name)) throw new Error(`Shared ${sharedLuaKey} is missing ${name}`);
118
+ }
119
+ const [start, end] = entry.range;
120
+ const [, tableEnd] = registry.range;
121
+ const separator = /^\s*[,;]/.exec(lua.slice(end, tableEnd));
122
+ // The preceding field may retain its trailing comma.
123
+ const stop = separator ? end + separator[0].length : end;
124
+ return lua.slice(0, start) + lua.slice(start, stop).replace(/[^\n]/g, "") + lua.slice(stop);
125
+ }
126
+
127
+ export function bindSharedLuaConsumer({ stamp }) {
128
+ return `local __kumiSharedLua = App.KumiSharedLua
129
+ local __kumiSharedStamp = ${JSON.stringify(stamp)}
130
+ local __kumiActualStamp = type(__kumiSharedLua) == "table" and __kumiSharedLua.Stamp
131
+ if __kumiActualStamp ~= __kumiSharedStamp then
132
+ error("Lua runtime mismatch for " .. addonName .. ": expected " .. __kumiSharedStamp .. ", got " .. tostring(__kumiActualStamp or "missing"), 0)
133
+ end
134
+ local __kumiFallbackRequire = require
135
+ local function require(key)
136
+ if key == "${sharedLuaKey}" then return __kumiSharedLua.Require(key) end
137
+ if __kumiFallbackRequire then return __kumiFallbackRequire(key) end
138
+ error("Unknown Lua module " .. tostring(key), 2)
139
+ end
140
+ `;
141
+ }
142
+
143
+ export function verifySharedLuaConsumer(lua, { parse, stamp }) {
144
+ let stamps = 0;
145
+ visit(parseChunk(parse, lua), node => {
146
+ if (sharedField(node)) throw new Error(`Emitted Lua duplicates shared ${sharedLuaKey}`);
147
+ if (node.type === "StringLiteral" && node.raw === JSON.stringify(stamp)) stamps++;
148
+ });
149
+ if (stamps !== 1) throw new Error(`Emitted Lua expects exactly one shared runtime stamp, found ${stamps}`);
150
+ }
@@ -0,0 +1,29 @@
1
+ if type(addonApp) ~= "table" then error("A native WoW addon requires its shared addon table", 0) end
2
+ local nativeRegistry = rawget(addonApp, "KumiNativeLua")
3
+ if nativeRegistry == nil then
4
+ nativeRegistry = { abi = 2, build = nativeBuild, modules = {}, classPrototypes = setmetatable({}, { __mode = "kv" }) }
5
+ rawset(addonApp, "KumiNativeLua", nativeRegistry)
6
+ elseif type(nativeRegistry) ~= "table" or nativeRegistry.abi ~= 2 or nativeRegistry.build ~= nativeBuild then
7
+ error("Native Kumi runtime mismatch for " .. tostring(addonName), 0)
8
+ end
9
+
10
+ local sharedModules = nativeRegistry.modules
11
+ local function cachedNativeModule(key, fingerprint)
12
+ local entry = sharedModules[key]
13
+ if entry == nil then return end
14
+ if entry.fingerprint ~= fingerprint then error("Native Kumi module mismatch for " .. key, 0) end
15
+ if entry.value == nil then error("Circular native Kumi initialization of " .. key, 0) end
16
+ return entry.value
17
+ end
18
+
19
+ local function initializeNativeModule(key, fingerprint, initialize, ...)
20
+ local entry = { fingerprint = fingerprint }
21
+ sharedModules[key] = entry
22
+ local ok, value = pcall(initialize, ...)
23
+ if not ok then
24
+ sharedModules[key] = nil
25
+ error(value, 0)
26
+ end
27
+ entry.value = value
28
+ return value
29
+ end
@@ -0,0 +1,260 @@
1
+ local arrays = {}
2
+
3
+ local function isLuaArray(value)
4
+ return type(value) == "table" and (value[1] ~= nil or next(value) == nil)
5
+ end
6
+
7
+ local function arrayOffset(offset, length, default)
8
+ if offset == nil then return default end
9
+ if offset ~= offset then return 0 end
10
+ if offset < 0 then return math.max(0, length + math.ceil(offset)) end
11
+ return math.min(length, math.floor(offset))
12
+ end
13
+
14
+ local function arrayIncludes(array, expected, offset)
15
+ local length = #array
16
+ local start = offset ~= nil and arrayOffset(offset, length, 0) or 0
17
+ for index = start + 1, length do
18
+ local item = array[index]
19
+ if rawequal(item, expected) or item ~= item and expected ~= expected then return true end
20
+ end
21
+ return false
22
+ end
23
+
24
+ local function arrayIndexOf(array, expected, offset)
25
+ local length = #array
26
+ local start = offset ~= nil and arrayOffset(offset, length, 0) or 0
27
+ for index = start + 1, length do
28
+ if rawequal(array[index], expected) then return index - 1 end
29
+ end
30
+ return -1
31
+ end
32
+
33
+ local function arraySlice(array, start, stop)
34
+ local length = #array
35
+ local result = {}
36
+ local first = arrayOffset(start, length, 0)
37
+ for index = first + 1, arrayOffset(stop, length, length) do result[index - first] = array[index] end
38
+ return result
39
+ end
40
+
41
+ local function arrayFromLength(length, map)
42
+ local result = {}
43
+ if length ~= length or length <= 0 then return result end
44
+ if length >= 4294967296 then error("Invalid array length", 2) end
45
+ for index = 1, math.floor(length) do result[index] = map(nil, index - 1) end
46
+ return result
47
+ end
48
+
49
+ local function arrayReduce(array, reduce, initial)
50
+ local result = initial
51
+ for index = 1, #array do result = reduce(result, array[index], index - 1, array) end
52
+ return result
53
+ end
54
+
55
+ local function arrayJoin(array, separator)
56
+ local parts = {}
57
+ for index = 1, #array do parts[index] = tostring(array[index]) end
58
+ return table.concat(parts, separator or ",")
59
+ end
60
+
61
+ local function arrayShift(array)
62
+ return table.remove(array, 1)
63
+ end
64
+
65
+ local function arrayReverse(array)
66
+ local length = #array
67
+ for left = 1, math.floor(length / 2) do
68
+ local right = length - left + 1
69
+ array[left], array[right] = array[right], array[left]
70
+ end
71
+ return array
72
+ end
73
+
74
+ local function arrayTakeTail(array, offset)
75
+ local length = #array
76
+ local start = arrayOffset(offset, length, 0)
77
+ local tail = {}
78
+ for index = start + 1, length do tail[#tail + 1] = array[index] end
79
+ for index = length, start + 1, -1 do array[index] = nil end
80
+ return tail
81
+ end
82
+
83
+ local function arraySplice(array, offset, count)
84
+ local removed = {}
85
+ if count == nil or not (count > 0) then return removed end
86
+ local length = #array
87
+ local first = arrayOffset(offset, length, 0)
88
+ local removedCount = math.min(length - first, math.floor(count))
89
+ if removedCount == 0 then return removed end
90
+ for index = 1, removedCount do removed[index] = array[first + index] end
91
+ for index = first + 1, length - removedCount do array[index] = array[index + removedCount] end
92
+ for index = length, length - removedCount + 1, -1 do array[index] = nil end
93
+ return removed
94
+ end
95
+
96
+ local function arraySpliceInsert(array, offset, count, ...)
97
+ local length = #array
98
+ local first = arrayOffset(offset, length, 0)
99
+ local removedCount = count ~= nil and count > 0 and math.min(length - first, math.floor(count)) or 0
100
+ local removed = {}
101
+ for index = 1, removedCount do removed[index] = array[first + index] end
102
+ local insertedCount = select("#", ...)
103
+ local shift = insertedCount - removedCount
104
+ if shift > 0 then
105
+ for index = length, first + removedCount + 1, -1 do array[index + shift] = array[index] end
106
+ elseif shift < 0 then
107
+ for index = first + removedCount + 1, length do array[index + shift] = array[index] end
108
+ for index = length, length + shift + 1, -1 do array[index] = nil end
109
+ end
110
+ for index = 1, insertedCount do array[first + index] = select(index, ...) end
111
+ return removed
112
+ end
113
+
114
+ local function arraySort(array, compare, usesSelf)
115
+ if compare == nil then table.sort(array)
116
+ elseif usesSelf then table.sort(array, function(left, right) return compare(nil, left, right) < 0 end)
117
+ else table.sort(array, function(left, right) return compare(left, right) < 0 end) end
118
+ return array
119
+ end
120
+
121
+ local function arrayEvery(array, test)
122
+ for index = 1, #array do
123
+ if not test(array[index], index - 1, array) then return false end
124
+ end
125
+ return true
126
+ end
127
+
128
+ local function arraySome(array, test)
129
+ for index = 1, #array do
130
+ if test(array[index], index - 1, array) then return true end
131
+ end
132
+ return false
133
+ end
134
+
135
+ local function arrayMap(array, map)
136
+ local result = {}
137
+ for index = 1, #array do result[index] = map(array[index], index - 1, array) end
138
+ return result
139
+ end
140
+
141
+ local function arrayFilter(array, test)
142
+ local result = {}
143
+ local write = 1
144
+ for index = 1, #array do
145
+ local item = array[index]
146
+ if test(item, index - 1, array) then
147
+ result[write] = item
148
+ write = write + 1
149
+ end
150
+ end
151
+ return result
152
+ end
153
+
154
+ local function arrayFindIndex(array, test)
155
+ for index = 1, #array do
156
+ if test(array[index], index - 1, array) then return index - 1 end
157
+ end
158
+ return -1
159
+ end
160
+
161
+ local function arrayFind(array, test)
162
+ for index = 1, #array do
163
+ local value = array[index]
164
+ if test(value, index - 1, array) then return value end
165
+ end
166
+ end
167
+
168
+ local function arrayAppend(target, source)
169
+ local offset = #target
170
+ for index = 1, #source do target[offset + index] = source[index] end
171
+ end
172
+
173
+ local function arrayConcat(first, ...)
174
+ local result = arraySlice(first)
175
+ for index = 1, select("#", ...) do arrayAppend(result, (select(index, ...))) end
176
+ return result
177
+ end
178
+
179
+ local function arrayPushAll(target, source)
180
+ arrayAppend(target, source)
181
+ return #target
182
+ end
183
+
184
+ local function arrayPushValues(target, ...)
185
+ local offset, count = #target, select("#", ...)
186
+ for index = 1, count do
187
+ local value = select(index, ...)
188
+ if value == nil then error("Cannot append nil to a dense Kumi array", 2) end
189
+ target[offset + index] = value
190
+ end
191
+ return offset + count
192
+ end
193
+
194
+ local function arrayPushSlots(array, ...)
195
+ local offset, count = #array, select("#", ...)
196
+ for index = 1, count do array[offset + index] = select(index, ...) end
197
+ return offset + count
198
+ end
199
+
200
+ local function arrayUnshift(target, ...)
201
+ local length, count = #target, select("#", ...)
202
+ if count == 0 then return length end
203
+ for index = length, 1, -1 do target[index + count] = target[index] end
204
+ for index = 1, count do
205
+ local value = select(index, ...)
206
+ if value == nil then error("Cannot prepend nil to a dense Kumi array", 2) end
207
+ target[index] = value
208
+ end
209
+ return length + count
210
+ end
211
+
212
+ local function arrayFlatMap(array, map)
213
+ local result = {}
214
+ local next = 1
215
+ for index = 1, #array do
216
+ local value = map(array[index], index - 1, array)
217
+ if isLuaArray(value) then
218
+ for entry = 1, #value do
219
+ result[next] = value[entry]
220
+ next = next + 1
221
+ end
222
+ else
223
+ result[next] = value
224
+ next = next + 1
225
+ end
226
+ end
227
+ return result
228
+ end
229
+ function arrays.push(array, value)
230
+ if value == nil then error("Cannot append nil to a dense Kumi array", 2) end
231
+ local next = #array + 1
232
+ array[next] = value
233
+ return next
234
+ end
235
+
236
+ local function arrayForEach(array, effect)
237
+ for index = 1, #array do effect(array[index], index - 1, array) end
238
+ end
239
+ function arrays.pop(array)
240
+ local last = #array
241
+ if last == 0 then return end
242
+ local value = array[last]
243
+ array[last] = nil
244
+ return value
245
+ end
246
+
247
+ function arrays.write(array, offset, value)
248
+ if type(offset) ~= "number" or offset < 0 or offset > #array or offset % 1 ~= 0 or value == nil then
249
+ error("Cannot create a hole in a dense Kumi array", 2)
250
+ end
251
+ array[offset + 1] = value
252
+ end
253
+
254
+ function arrays.trim(array, length)
255
+ local previous = #array
256
+ if type(length) ~= "number" or length < 0 or length > previous or length % 1 ~= 0 then
257
+ error("Dense Kumi arrays can only shrink by changing length", 2)
258
+ end
259
+ for index = previous, length + 1, -1 do array[index] = nil end
260
+ end