@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.
- package/LICENSE +21 -0
- package/README.md +67 -2
- package/animation/color-transition.ts +196 -0
- package/animation/cycle.ts +151 -0
- package/animation/ease.ts +29 -0
- package/animation/index.ts +13 -0
- package/animation/package.json +12 -0
- package/animation/presets.ts +376 -0
- package/animation/spec.ts +197 -0
- package/builder/Cargo.lock +192 -0
- package/builder/Cargo.toml +7 -0
- package/builder/css-v1.mjs +387 -0
- package/builder/css-v1.test.mjs +361 -0
- package/builder/generated-output.mjs +61 -0
- package/builder/generated-output.test.mjs +132 -0
- package/builder/jsx-form.mjs +93 -0
- package/builder/jsx-form.test.mjs +48 -0
- package/builder/jsx-lists.mjs +209 -0
- package/builder/link-addon-package.mjs +127 -0
- package/builder/lua-assets.mjs +74 -0
- package/builder/lua-builtin-values.mjs +57 -0
- package/builder/lua-builtin-values.test.mjs +155 -0
- package/builder/lua-loop-closures.mjs +183 -0
- package/builder/lua-loop-closures.test.mjs +89 -0
- package/builder/lua-truthiness.mjs +61 -0
- package/builder/lua-truthiness.test.mjs +113 -0
- package/builder/module-declarations.mjs +594 -0
- package/builder/module-declarations.test.mjs +435 -0
- package/builder/native-kernels.mjs +60 -0
- package/builder/native-lua.d.ts +57 -0
- package/builder/native-routes.mjs +51 -0
- package/builder/no-static-ref-read.mjs +93 -0
- package/builder/no-static-ref-read.test.mjs +181 -0
- package/builder/secure-snippet.mjs +317 -0
- package/builder/secure-snippet.test.mjs +101 -0
- package/builder/shared-lua-runtime.mjs +150 -0
- package/builder/src/addon-link.lua +29 -0
- package/builder/src/arrays.lua +260 -0
- package/builder/src/assets.rs +177 -0
- package/builder/src/classes.lua +75 -0
- package/builder/src/collections.lua +190 -0
- package/builder/src/compiler.rs +2336 -0
- package/builder/src/config.rs +42 -0
- package/builder/src/entries.rs +166 -0
- package/builder/src/errors.lua +15 -0
- package/builder/src/lua_data.rs +231 -0
- package/builder/src/main.rs +381 -0
- package/builder/src/native_ir.rs +65 -0
- package/builder/src/native_signal.rs +83 -0
- package/builder/src/numbers.lua +76 -0
- package/builder/src/project-front.mjs +3682 -0
- package/builder/src/records.lua +75 -0
- package/builder/src/routes.rs +124 -0
- package/builder/src/setup-store.lua +18 -0
- package/builder/src/signal-kernel.fingerprint +1 -0
- package/builder/src/strings.lua +103 -0
- package/builder/src/ts-lower.mjs +68 -0
- package/builder/src/wow-reads.lua +22 -0
- package/builder/src/wow_client.rs +253 -0
- package/builder/src/wow_types.rs +564 -0
- package/builder/style-modules.mjs +93 -0
- package/builder/style-states.mjs +100 -0
- package/builder/style-states.test.mjs +103 -0
- package/builder/vite-assets.mjs +53 -0
- package/builder/vite-project.mjs +10 -0
- package/config.ts +59 -0
- package/eslint.mjs +74 -0
- package/form/index.ts +526 -0
- package/form/package.json +13 -0
- package/motion/index.ts +2 -0
- package/motion/package.json +10 -0
- package/package.json +26 -4
- package/router/index.ts +50 -0
- package/router/navigation.ts +22 -0
- package/router/package.json +12 -0
- package/router/router-view.ts +134 -0
- package/router/routes.ts +64 -0
- package/scripts/addon-build.mjs +364 -0
- package/scripts/addon-project.mjs +78 -0
- package/scripts/kumi-cli.mjs +105 -0
- package/scripts/link-addons.mjs +61 -0
- package/src/bindings.ts +35 -0
- package/src/context.ts +15 -0
- package/src/css-tokens.ts +239 -0
- package/src/css-v1.ts +68 -0
- package/src/cvars.ts +91 -0
- package/src/element-node.ts +32 -0
- package/src/elements.ts +505 -0
- package/src/feature-scope.ts +82 -0
- package/src/flex-layout.ts +807 -0
- package/src/frame-scheduler.ts +336 -0
- package/src/grid-tracks.ts +165 -0
- package/src/image-imports.d.ts +24 -0
- package/src/images.ts +79 -0
- package/src/index.ts +29 -0
- package/src/jsx-dev-runtime.ts +2 -0
- package/src/jsx-runtime.ts +29 -0
- package/src/jsx.d.ts +12 -0
- package/src/keyed-list.ts +366 -0
- package/src/media-query.ts +144 -0
- package/src/module-ui.ts +147 -0
- package/src/module.ts +311 -0
- package/src/node-selectors.ts +114 -0
- package/src/owned-collections.ts +52 -0
- package/src/providers.ts +241 -0
- package/src/reactive-scope.ts +34 -0
- package/src/reactivity.ts +275 -0
- package/src/result.ts +64 -0
- package/src/runtime.ts +52 -0
- package/src/scheduler.ts +185 -0
- package/src/signal-kernel.ts +63 -0
- package/src/style-classes.ts +51 -0
- package/src/style-states.ts +134 -0
- package/src/style-variables.ts +53 -0
- package/src/text-children.ts +42 -0
- package/src/theme.ts +50 -0
- package/src/transition.ts +18 -0
- package/state/index.ts +9 -0
- package/state/package.json +12 -0
- package/state/setup-store-persistence.ts +206 -0
- package/state/setup-store-view.ts +10 -0
- package/state/setup-store.ts +342 -0
- package/state/store-bindings.ts +25 -0
- package/state/store.ts +84 -0
- package/styles/_layers.scss +14 -0
- package/templates/addon/.agents/skills/kumi-build-debug/SKILL.md +36 -0
- package/templates/addon/.agents/skills/kumi-feature/SKILL.md +50 -0
- package/templates/addon/.agents/skills/kumi-plugin/SKILL.md +36 -0
- package/templates/addon/.agents/skills/kumi-store/SKILL.md +47 -0
- package/templates/addon/.agents/skills/kumi-ui/SKILL.md +73 -0
- package/templates/addon/.agents/skills/kumi-wow-api/SKILL.md +30 -0
- package/templates/addon/.claude/skills/kumi-build-debug/SKILL.md +36 -0
- package/templates/addon/.claude/skills/kumi-feature/SKILL.md +50 -0
- package/templates/addon/.claude/skills/kumi-plugin/SKILL.md +36 -0
- package/templates/addon/.claude/skills/kumi-store/SKILL.md +47 -0
- package/templates/addon/.claude/skills/kumi-ui/SKILL.md +73 -0
- package/templates/addon/.claude/skills/kumi-wow-api/SKILL.md +30 -0
- package/templates/addon/AGENTS.md +34 -0
- package/templates/addon/Assets/Art/Corner10.tga +0 -0
- package/templates/addon/Assets/Art/Corner11.tga +0 -0
- package/templates/addon/Assets/Art/Corner12.tga +0 -0
- package/templates/addon/Assets/Art/Corner13.tga +0 -0
- package/templates/addon/Assets/Art/Corner14.tga +0 -0
- package/templates/addon/Assets/Art/Corner15.tga +0 -0
- package/templates/addon/Assets/Art/Corner16.tga +0 -0
- package/templates/addon/Assets/Art/Corner2.tga +0 -0
- package/templates/addon/Assets/Art/Corner3.tga +0 -0
- package/templates/addon/Assets/Art/Corner4.tga +0 -0
- package/templates/addon/Assets/Art/Corner5.tga +0 -0
- package/templates/addon/Assets/Art/Corner6.tga +0 -0
- package/templates/addon/Assets/Art/Corner7.tga +0 -0
- package/templates/addon/Assets/Art/Corner8.tga +0 -0
- package/templates/addon/Assets/Art/Corner9.tga +0 -0
- package/templates/addon/Assets/Art/Dot.tga +0 -0
- package/templates/addon/Assets/Art/Gear.tga +0 -0
- package/templates/addon/Assets/Art/Grip.tga +0 -0
- package/templates/addon/Assets/Art/Outline10.tga +0 -0
- package/templates/addon/Assets/Art/Outline12.tga +0 -0
- package/templates/addon/Assets/Art/Outline13.tga +0 -0
- package/templates/addon/Assets/Art/Outline14.tga +0 -0
- package/templates/addon/Assets/Art/Outline15.tga +0 -0
- package/templates/addon/Assets/Art/Outline2.tga +0 -0
- package/templates/addon/Assets/Art/Outline3.tga +0 -0
- package/templates/addon/Assets/Art/Outline5.tga +0 -0
- package/templates/addon/Assets/Art/Outline7.tga +0 -0
- package/templates/addon/Assets/Art/Outline9.tga +0 -0
- package/templates/addon/Assets/Art/Round11.tga +0 -0
- package/templates/addon/Assets/Art/Round11Border.tga +0 -0
- package/templates/addon/Assets/Art/Round4.tga +0 -0
- package/templates/addon/Assets/Art/Round4Border.tga +0 -0
- package/templates/addon/Assets/Art/Round6.tga +0 -0
- package/templates/addon/Assets/Art/Round6Border.tga +0 -0
- package/templates/addon/Assets/Art/Round8.tga +0 -0
- package/templates/addon/Assets/Art/Round8Border.tga +0 -0
- package/templates/addon/Assets/Art/Search.tga +0 -0
- package/templates/addon/CLAUDE.md +1 -0
- package/templates/addon/README.md +49 -0
- package/templates/addon/eslint.config.mjs +6 -0
- package/templates/addon/gitignore +9 -0
- package/templates/addon/kumi.config.ts +17 -0
- package/templates/addon/package.json +20 -0
- package/templates/addon/src/app/app.module.ts +18 -0
- package/templates/addon/src/app/commands.store.ts +11 -0
- package/templates/addon/src/app/layout/AppWindow.module.scss +92 -0
- package/templates/addon/src/app/layout/AppWindow.tsx +32 -0
- package/templates/addon/src/app/main.ts +5 -0
- package/templates/addon/src/app/routes/(window)/home/page.tsx +8 -0
- package/templates/addon/src/app/routes/(window)/plugins/page.tsx +38 -0
- package/templates/addon/src/app/routes/(window)/settings/page.tsx +8 -0
- package/templates/addon/src/app/theme.ts +6 -0
- package/templates/addon/src/modules/greeting/api/player.api.ts +29 -0
- package/templates/addon/src/modules/greeting/greeting.module.ts +18 -0
- package/templates/addon/src/modules/greeting/model/greeting.store.ts +81 -0
- package/templates/addon/src/modules/greeting/services/greeting.service.ts +38 -0
- package/templates/addon/src/modules/greeting/ui/Greeting.module.scss +105 -0
- package/templates/addon/src/modules/greeting/ui/GreetingCard.tsx +29 -0
- package/templates/addon/src/modules/greeting/widgets/GreetingHud.tsx +54 -0
- package/templates/addon/src/modules/greeting/widgets/GreetingOverview.tsx +18 -0
- package/templates/addon/src/modules/greeting/widgets/GreetingSettings.tsx +56 -0
- package/templates/addon/src/modules/playtime/api/clock.api.ts +6 -0
- package/templates/addon/src/modules/playtime/model/playtime.store.ts +19 -0
- package/templates/addon/src/modules/playtime/playtime.module.ts +18 -0
- package/templates/addon/src/modules/playtime/services/playtime.service.ts +18 -0
- package/templates/addon/src/modules/playtime/ui/Playtime.module.scss +25 -0
- package/templates/addon/src/modules/playtime/ui/PlaytimeBadge.tsx +10 -0
- package/templates/addon/src/modules/playtime/widgets/PlaytimePage.tsx +16 -0
- package/templates/addon/src/shared/api/native-art.ts +28 -0
- package/templates/addon/src/shared/assets/close.png +0 -0
- package/templates/addon/src/shared/lib/format.ts +20 -0
- package/templates/addon/src/shared/styles/_tokens.scss +12 -0
- package/templates/addon/src/shared/ui/Button.tsx +29 -0
- package/templates/addon/src/shared/ui/Choice.tsx +34 -0
- package/templates/addon/src/shared/ui/Controls.module.scss +93 -0
- package/templates/addon/src/shared/ui/Glyph.tsx +18 -0
- package/templates/addon/src/shared/ui/Slider.tsx +25 -0
- package/templates/addon/src/shared/ui/Toggle.tsx +29 -0
- package/templates/addon/src/shared/ui/focus-ref.ts +7 -0
- package/templates/addon/src/shared/ui/range-skin.tsx +15 -0
- package/templates/addon/tsconfig.json +63 -0
- package/translate/index.ts +14 -0
- package/translate/package.json +9 -0
- package/utils/colors.ts +76 -0
- package/utils/format.ts +37 -0
- package/utils/index.ts +4 -0
- package/utils/number.ts +17 -0
- package/utils/package.json +7 -0
- package/utils/text.ts +6 -0
- package/validate/index.ts +119 -0
- package/validate/package.json +9 -0
- package/wow/absolute-position.ts +142 -0
- package/wow/action-bar-layout.ts +108 -0
- package/wow/action-button.ts +219 -0
- package/wow/action-catalog.ts +98 -0
- package/wow/addon-manifest.ts +36 -0
- package/wow/animation.ts +428 -0
- package/wow/app-ui.ts +221 -0
- package/wow/aura-button.ts +76 -0
- package/wow/aura-container.ts +591 -0
- package/wow/aura-countdown.ts +201 -0
- package/wow/aura-slot.ts +153 -0
- package/wow/bar-shade.ts +93 -0
- package/wow/borrowed-slot.ts +216 -0
- package/wow/button-drag.ts +48 -0
- package/wow/cast-clock.ts +153 -0
- package/wow/cast-text.ts +83 -0
- package/wow/class-colors.ts +126 -0
- package/wow/clock.ts +7 -0
- package/wow/color-picker.ts +128 -0
- package/wow/commands.ts +17 -0
- package/wow/contained-frame.ts +128 -0
- package/wow/contract.ts +42 -0
- package/wow/cooldown-timing.ts +24 -0
- package/wow/css-box.ts +144 -0
- package/wow/css-color.ts +12 -0
- package/wow/css-style.ts +109 -0
- package/wow/css-surfaces.ts +603 -0
- package/wow/css-text.ts +399 -0
- package/wow/cursor.ts +45 -0
- package/wow/cvar-schema.ts +32 -0
- package/wow/cvars.ts +98 -0
- package/wow/dropdown-art.ts +39 -0
- package/wow/element-click.ts +20 -0
- package/wow/element-event-coordinator.ts +219 -0
- package/wow/element-events.ts +317 -0
- package/wow/event-dispatcher.ts +151 -0
- package/wow/events.ts +50 -0
- package/wow/font-cache.ts +92 -0
- package/wow/frame-access.ts +53 -0
- package/wow/frame-layer.ts +5 -0
- package/wow/frame-timing.ts +13 -0
- package/wow/frames.ts +236 -0
- package/wow/heal-prediction.ts +305 -0
- package/wow/hooks.ts +133 -0
- package/wow/image-layers.ts +55 -0
- package/wow/index.ts +111 -0
- package/wow/layout-view.ts +663 -0
- package/wow/locale-font.ts +15 -0
- package/wow/lua-string.ts +18 -0
- package/wow/lua-table.ts +18 -0
- package/wow/macro-book.ts +84 -0
- package/wow/map-position.ts +27 -0
- package/wow/media-query.ts +65 -0
- package/wow/module-events.ts +11 -0
- package/wow/native-hooks.ts +168 -0
- package/wow/native-placement.ts +88 -0
- package/wow/native.ts +153 -0
- package/wow/number-format.ts +8 -0
- package/wow/number-input.ts +10 -0
- package/wow/panel-host.ts +97 -0
- package/wow/pixel.ts +35 -0
- package/wow/plugins.ts +246 -0
- package/wow/protect.ts +48 -0
- package/wow/protected-layout.ts +133 -0
- package/wow/protected-view.ts +103 -0
- package/wow/range-art.ts +81 -0
- package/wow/readable-api.ts +120 -0
- package/wow/reparent-frame.ts +99 -0
- package/wow/rounded-texture.ts +183 -0
- package/wow/saved-variables.ts +109 -0
- package/wow/scroll-view.ts +64 -0
- package/wow/secret-ref.ts +14 -0
- package/wow/secure-bindings.ts +89 -0
- package/wow/secure-clicks.ts +432 -0
- package/wow/secure-snippet.ts +235 -0
- package/wow/secure-state.ts +130 -0
- package/wow/secure-target-visibility.ts +89 -0
- package/wow/secure-transaction.ts +331 -0
- package/wow/special-window.ts +269 -0
- package/wow/spell-cooldown.ts +36 -0
- package/wow/start-addon.ts +122 -0
- package/wow/state-paint.ts +256 -0
- package/wow/transition.ts +86 -0
- package/wow/unit-button.ts +39 -0
- package/wow/unit-cast.ts +92 -0
- package/wow/unit-health-text.ts +47 -0
- package/wow/view-nodes.ts +304 -0
- package/wow/view.ts +1864 -0
- package/wow/visible-frame-job.ts +34 -0
- package/wow/width-reserve.ts +108 -0
|
@@ -0,0 +1,2336 @@
|
|
|
1
|
+
use std::collections::BTreeSet;
|
|
2
|
+
use std::fmt::Write;
|
|
3
|
+
use std::fs;
|
|
4
|
+
use std::io::Write as IoWrite;
|
|
5
|
+
use std::path::{Component, Path, PathBuf};
|
|
6
|
+
use std::process::{Command, Stdio};
|
|
7
|
+
|
|
8
|
+
type Compile<T> = Result<T, Box<dyn std::error::Error>>;
|
|
9
|
+
|
|
10
|
+
fn quote(value: &str) -> String {
|
|
11
|
+
let mut out = String::from("\"");
|
|
12
|
+
for ch in value.chars() {
|
|
13
|
+
match ch {
|
|
14
|
+
'"' => out.push_str("\\\""),
|
|
15
|
+
'\\' => out.push_str("\\\\"),
|
|
16
|
+
'\0'..='\u{001f}' | '\u{007f}' => {
|
|
17
|
+
let _ = write!(out, "\\{:03}", ch as u32);
|
|
18
|
+
}
|
|
19
|
+
_ => out.push(ch),
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
out.push('"');
|
|
23
|
+
out
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
fn decode(value: &str) -> Compile<String> {
|
|
27
|
+
if !value.len().is_multiple_of(2) || !value.bytes().all(|byte| byte.is_ascii_hexdigit()) {
|
|
28
|
+
return Err("Invalid string in compiler input".into());
|
|
29
|
+
}
|
|
30
|
+
let bytes: Result<Vec<_>, _> = (0..value.len())
|
|
31
|
+
.step_by(2)
|
|
32
|
+
.map(|index| u8::from_str_radix(&value[index..index + 2], 16))
|
|
33
|
+
.collect();
|
|
34
|
+
Ok(String::from_utf8(bytes?)?)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
fn identifier(name: &str) -> Compile<String> {
|
|
38
|
+
let mut bytes = name.bytes();
|
|
39
|
+
if !matches!(bytes.next(), Some(b'A'..=b'Z' | b'a'..=b'z' | b'_'))
|
|
40
|
+
|| !bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
|
|
41
|
+
{
|
|
42
|
+
return Err(format!("Invalid identifier in compiler input: {name:?}").into());
|
|
43
|
+
}
|
|
44
|
+
Ok(format!("v_{name}"))
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
fn parameters(names: &[&str], receiver: bool, indent: usize) -> Compile<(String, String)> {
|
|
48
|
+
let mut formal = Vec::new();
|
|
49
|
+
if receiver {
|
|
50
|
+
formal.push("self".into());
|
|
51
|
+
}
|
|
52
|
+
let mut prologue = String::new();
|
|
53
|
+
for (index, name) in names.iter().enumerate() {
|
|
54
|
+
let forwarded = name.strip_prefix("forwarded-rest:");
|
|
55
|
+
if let Some(rest) = forwarded.or_else(|| name.strip_prefix("rest:")) {
|
|
56
|
+
if index + 1 != names.len() {
|
|
57
|
+
return Err("Rest parameter must be last".into());
|
|
58
|
+
}
|
|
59
|
+
formal.push("...".into());
|
|
60
|
+
let pad = " ".repeat(indent);
|
|
61
|
+
let rest = identifier(rest)?;
|
|
62
|
+
writeln!(prologue, "{pad}local {rest} = {{...}}")?;
|
|
63
|
+
if forwarded.is_some() {
|
|
64
|
+
writeln!(prologue, "{pad}local {rest}_count = select(\"#\", ...)")?;
|
|
65
|
+
}
|
|
66
|
+
} else {
|
|
67
|
+
formal.push(identifier(name)?);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
Ok((formal.join(", "), prologue))
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
struct Emitter<'a> {
|
|
74
|
+
records: std::str::Lines<'a>,
|
|
75
|
+
collections: bool,
|
|
76
|
+
arrays: bool,
|
|
77
|
+
classes: bool,
|
|
78
|
+
types: bool,
|
|
79
|
+
numbers: bool,
|
|
80
|
+
wow_reads: bool,
|
|
81
|
+
store_views: bool,
|
|
82
|
+
strings: bool,
|
|
83
|
+
tables: bool,
|
|
84
|
+
errors: bool,
|
|
85
|
+
addon_args: bool,
|
|
86
|
+
addon_locale: bool,
|
|
87
|
+
fragments: bool,
|
|
88
|
+
addon_globals: BTreeSet<&'a str>,
|
|
89
|
+
loop_wrappers: Vec<bool>,
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
enum ObjectPart {
|
|
93
|
+
Field(String, String),
|
|
94
|
+
Spread { source: String, mode: SpreadMode },
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
enum SpreadMode {
|
|
98
|
+
Present,
|
|
99
|
+
Nullable,
|
|
100
|
+
Structural,
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
enum ArrayPart {
|
|
104
|
+
Value(String),
|
|
105
|
+
Spread(String),
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
impl<'a> Emitter<'a> {
|
|
109
|
+
fn record(&mut self) -> Compile<(&'a str, Vec<&'a str>)> {
|
|
110
|
+
let mut parts = self
|
|
111
|
+
.records
|
|
112
|
+
.next()
|
|
113
|
+
.ok_or("Unexpected end of compiler input")?
|
|
114
|
+
.split('\t');
|
|
115
|
+
let tag = parts.next().ok_or("Missing compiler instruction")?;
|
|
116
|
+
Ok((tag, parts.collect()))
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
fn expression(&mut self, indent: usize) -> Compile<String> {
|
|
120
|
+
let (tag, args) = self.record()?;
|
|
121
|
+
let pad = " ".repeat(indent);
|
|
122
|
+
match (tag, args.as_slice()) {
|
|
123
|
+
("literal", [value @ ("true" | "false" | "nil")]) => Ok((*value).into()),
|
|
124
|
+
("literal", ["nan"]) => Ok("(0/0)".into()),
|
|
125
|
+
("literal", ["-inf"]) => Ok("(-math.huge)".into()),
|
|
126
|
+
("error-value", []) => {
|
|
127
|
+
self.errors = true;
|
|
128
|
+
Ok(format!("createError({})", self.expression(indent)?))
|
|
129
|
+
}
|
|
130
|
+
(
|
|
131
|
+
"addon-global",
|
|
132
|
+
[name @ ("addonName" | "addonTable" | "addonApp" | "addonLocale" | "addonUi"
|
|
133
|
+
| "addonArgs")],
|
|
134
|
+
) => {
|
|
135
|
+
self.addon_args |= *name == "addonArgs";
|
|
136
|
+
self.addon_locale |= *name == "addonLocale";
|
|
137
|
+
if self.fragments {
|
|
138
|
+
self.addon_globals.insert(name);
|
|
139
|
+
}
|
|
140
|
+
Ok((*name).into())
|
|
141
|
+
}
|
|
142
|
+
("number", [value]) if value.parse::<f64>().is_ok_and(f64::is_finite) => {
|
|
143
|
+
Ok((*value).into())
|
|
144
|
+
}
|
|
145
|
+
("string", [value]) => Ok(quote(&decode(value)?)),
|
|
146
|
+
("template", [count, head]) => self.template(count.parse()?, head, indent),
|
|
147
|
+
("host-global", [name]) => {
|
|
148
|
+
identifier(name)?;
|
|
149
|
+
Ok((*name).into())
|
|
150
|
+
}
|
|
151
|
+
(
|
|
152
|
+
"global",
|
|
153
|
+
[name @ ("tostring" | "math.huge" | "math.floor" | "math.ceil" | "math.abs"
|
|
154
|
+
| "math.sqrt" | "math.atan2" | "math.pi" | "math.sin" | "math.cos"
|
|
155
|
+
| "math.tan" | "math.atan" | "math.random" | "math.exp" | "math.log"
|
|
156
|
+
| "math.pow" | "string.lower" | "string.upper")],
|
|
157
|
+
) => Ok((*name).into()),
|
|
158
|
+
("global", [name @ "setupStoreView"]) => {
|
|
159
|
+
self.store_views = true;
|
|
160
|
+
Ok((*name).into())
|
|
161
|
+
}
|
|
162
|
+
(
|
|
163
|
+
"global",
|
|
164
|
+
[name @ ("wowReadable" | "wowReadableSecond" | "wowReadableThird" | "wowOptionalReadable")],
|
|
165
|
+
) => {
|
|
166
|
+
self.wow_reads = true;
|
|
167
|
+
Ok((*name).into())
|
|
168
|
+
}
|
|
169
|
+
(
|
|
170
|
+
"global",
|
|
171
|
+
[name @ ("numberFinite" | "numberNaN" | "numberInteger" | "numberMin" | "numberMax"
|
|
172
|
+
| "numberMin2" | "numberMax2" | "numberValue" | "numberRound" | "numberFixed")],
|
|
173
|
+
) => {
|
|
174
|
+
self.numbers = true;
|
|
175
|
+
Ok((*name).into())
|
|
176
|
+
}
|
|
177
|
+
(
|
|
178
|
+
"global",
|
|
179
|
+
[name @ ("isLuaArray" | "arrayIncludes" | "arrayIndexOf" | "arrayJoin"
|
|
180
|
+
| "arraySlice" | "arrayTakeTail" | "arraySplice" | "arraySpliceInsert"
|
|
181
|
+
| "arraySort" | "arrayEvery" | "arraySome" | "arrayForEach" | "arrayMap"
|
|
182
|
+
| "arrayFilter" | "arrayFind" | "arrayFindIndex" | "arrayFlatMap"
|
|
183
|
+
| "arrayPushAll" | "arrayPushValues" | "arrayPushSlots" | "arrayFromLength"
|
|
184
|
+
| "arrayConcat" | "arrayUnshift" | "arrayReduce" | "arrayPop"
|
|
185
|
+
| "arrayShift" | "arrayReverse")],
|
|
186
|
+
) => {
|
|
187
|
+
self.arrays = true;
|
|
188
|
+
Ok(if *name == "arrayPop" {
|
|
189
|
+
"arrays.pop"
|
|
190
|
+
} else {
|
|
191
|
+
name
|
|
192
|
+
}
|
|
193
|
+
.into())
|
|
194
|
+
}
|
|
195
|
+
(
|
|
196
|
+
"global",
|
|
197
|
+
[name @ ("copyFields"
|
|
198
|
+
| "copyAllFields"
|
|
199
|
+
| "copyStructuralSources"
|
|
200
|
+
| "objectFromEntries"
|
|
201
|
+
| "tableHas")],
|
|
202
|
+
) => {
|
|
203
|
+
self.tables = true;
|
|
204
|
+
Ok((*name).into())
|
|
205
|
+
}
|
|
206
|
+
("global", ["constructClass"]) => {
|
|
207
|
+
self.classes = true;
|
|
208
|
+
Ok("constructClass".into())
|
|
209
|
+
}
|
|
210
|
+
(
|
|
211
|
+
"global",
|
|
212
|
+
[name @ ("byteCharAt" | "byteCharCodeAt" | "byteEndsWith" | "byteStartsWith"
|
|
213
|
+
| "byteIndexOf" | "byteIncludes" | "byteSlice" | "byteSplit" | "byteTrim"
|
|
214
|
+
| "byteSubstring" | "byteReplace" | "byteAt" | "byteTrimEnd" | "byteRepeat")],
|
|
215
|
+
) => {
|
|
216
|
+
self.strings = true;
|
|
217
|
+
Ok((*name).into())
|
|
218
|
+
}
|
|
219
|
+
("collection", [kind @ ("map" | "set" | "weakmap")]) => {
|
|
220
|
+
self.collections = true;
|
|
221
|
+
Ok(format!("collections.{kind}()"))
|
|
222
|
+
}
|
|
223
|
+
(
|
|
224
|
+
"collection-init",
|
|
225
|
+
[kind
|
|
226
|
+
@ ("copyMap" | "copySet" | "mapFromArray" | "setFromArray" | "setFromLuaArray")],
|
|
227
|
+
) => {
|
|
228
|
+
self.collections = true;
|
|
229
|
+
Ok(format!("collections.{kind}({})", self.expression(indent)?))
|
|
230
|
+
}
|
|
231
|
+
("collection-snapshot", [field @ ("key" | "value" | "entry")]) => {
|
|
232
|
+
self.collections = true;
|
|
233
|
+
Ok(format!(
|
|
234
|
+
"collections.snapshot({}, {field:?})",
|
|
235
|
+
self.expression(indent)?,
|
|
236
|
+
))
|
|
237
|
+
}
|
|
238
|
+
("map-collection", [kind @ ("map" | "set")]) => {
|
|
239
|
+
self.collections = true;
|
|
240
|
+
let source = self.expression(indent)?;
|
|
241
|
+
let mapper = self.expression(indent)?;
|
|
242
|
+
let pairs = *kind == "map";
|
|
243
|
+
Ok(format!(
|
|
244
|
+
"collections.mapToArray({source}, {mapper}, {pairs})"
|
|
245
|
+
))
|
|
246
|
+
}
|
|
247
|
+
("map-entry-iterator", []) => {
|
|
248
|
+
self.collections = true;
|
|
249
|
+
Ok(format!(
|
|
250
|
+
"collections.entryIterator({})",
|
|
251
|
+
self.expression(indent)?
|
|
252
|
+
))
|
|
253
|
+
}
|
|
254
|
+
("type-of", []) => {
|
|
255
|
+
self.types = true;
|
|
256
|
+
Ok(format!("valueType({})", self.expression(indent)?))
|
|
257
|
+
}
|
|
258
|
+
(
|
|
259
|
+
"primitive-type-test",
|
|
260
|
+
[op @ ("==" | "~="), kind @ ("boolean" | "number" | "string" | "nil")],
|
|
261
|
+
) => {
|
|
262
|
+
let value = self.expression(indent)?;
|
|
263
|
+
Ok(format!("(type(({value})) {op} {kind:?})"))
|
|
264
|
+
}
|
|
265
|
+
("byte-substring", [first]) => {
|
|
266
|
+
let start = first.parse::<i32>()?;
|
|
267
|
+
let value = self.expression(indent)?;
|
|
268
|
+
Ok(format!("(({value}):sub({start}))"))
|
|
269
|
+
}
|
|
270
|
+
("byte-substring", [first, last]) => {
|
|
271
|
+
let start = first.parse::<i32>()?;
|
|
272
|
+
let end = last.parse::<i32>()?;
|
|
273
|
+
let value = self.expression(indent)?;
|
|
274
|
+
Ok(format!("(({value}):sub({start}, {end}))"))
|
|
275
|
+
}
|
|
276
|
+
("instance-of", []) => {
|
|
277
|
+
self.classes = true;
|
|
278
|
+
Ok(format!(
|
|
279
|
+
"classInstance({}, {})",
|
|
280
|
+
self.expression(indent)?,
|
|
281
|
+
self.expression(indent)?
|
|
282
|
+
))
|
|
283
|
+
}
|
|
284
|
+
("ref", [name]) => identifier(name),
|
|
285
|
+
("self", []) => Ok("self".into()),
|
|
286
|
+
("property", [key]) => Ok(format!(
|
|
287
|
+
"({})[{}]",
|
|
288
|
+
self.expression(indent)?,
|
|
289
|
+
quote(&decode(key)?)
|
|
290
|
+
)),
|
|
291
|
+
("has-property", [key]) => Ok(format!(
|
|
292
|
+
"(({})[{}] ~= nil)",
|
|
293
|
+
self.expression(indent)?,
|
|
294
|
+
quote(&decode(key)?)
|
|
295
|
+
)),
|
|
296
|
+
("optional-property", [key]) => {
|
|
297
|
+
let local = self
|
|
298
|
+
.records
|
|
299
|
+
.clone()
|
|
300
|
+
.next()
|
|
301
|
+
.is_some_and(|record| record.starts_with("ref\t"));
|
|
302
|
+
let receiver = self.expression(indent)?;
|
|
303
|
+
let key = quote(&decode(key)?);
|
|
304
|
+
if local {
|
|
305
|
+
Ok(format!("({receiver} and {receiver}[{key}])"))
|
|
306
|
+
} else {
|
|
307
|
+
Ok(format!("(function() local target = {receiver}; if target ~= nil then return target[{key}] end; return nil end)()"))
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
("array-index", []) => {
|
|
311
|
+
let array = self.expression(indent)?;
|
|
312
|
+
let index = self.expression(indent)?;
|
|
313
|
+
Ok(format!("({array})[({index}) + 1]"))
|
|
314
|
+
}
|
|
315
|
+
("table-index", []) => {
|
|
316
|
+
let target = self.expression(indent)?;
|
|
317
|
+
let key = self.expression(indent)?;
|
|
318
|
+
Ok(format!("({target})[{key}]"))
|
|
319
|
+
}
|
|
320
|
+
("raw-table" | "optional-raw-table", [operation]) => {
|
|
321
|
+
let source = self.expression(indent)?;
|
|
322
|
+
let optional = tag == "optional-raw-table";
|
|
323
|
+
let target = if optional { "guardedTarget" } else { &source };
|
|
324
|
+
let value = match *operation {
|
|
325
|
+
"length" => format!("(#{target})"),
|
|
326
|
+
"isEmpty" => format!("(next({target}) == nil)"),
|
|
327
|
+
"get" | "has" => {
|
|
328
|
+
let key = self.expression(indent)?;
|
|
329
|
+
let value = format!("({target})[{key}]");
|
|
330
|
+
if *operation == "has" {
|
|
331
|
+
format!("({value} ~= nil)")
|
|
332
|
+
} else {
|
|
333
|
+
value
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
"set" | "add" | "delete" => {
|
|
337
|
+
let key = self.expression(indent)?;
|
|
338
|
+
let (value, result) = match *operation {
|
|
339
|
+
"set" => (self.expression(indent)?, "nil"),
|
|
340
|
+
"add" => ("true".into(), "nil"),
|
|
341
|
+
_ => ("nil".into(), "true"),
|
|
342
|
+
};
|
|
343
|
+
format!("(function() local target = {target}; local key = {key}; target[key] = {value}; return {result} end)()")
|
|
344
|
+
}
|
|
345
|
+
_ => return Err("Unknown Lua table operation".into()),
|
|
346
|
+
};
|
|
347
|
+
if optional {
|
|
348
|
+
Ok(format!("(function(guardedTarget) if guardedTarget ~= nil then return {value} end; return nil end)({source})"))
|
|
349
|
+
} else {
|
|
350
|
+
Ok(value)
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
("optional-index" | "optional-array-index", []) => {
|
|
354
|
+
let target = self.expression(indent)?;
|
|
355
|
+
let mut key = self.expression(indent)?;
|
|
356
|
+
if tag == "optional-array-index" {
|
|
357
|
+
key = format!("({key}) + 1");
|
|
358
|
+
}
|
|
359
|
+
Ok(format!("(function() local target = {target}; if target ~= nil then return target[{key}] end; return nil end)()"))
|
|
360
|
+
}
|
|
361
|
+
("object-keys", []) => {
|
|
362
|
+
self.tables = true;
|
|
363
|
+
Ok(format!("objectKeys({})", self.expression(indent)?))
|
|
364
|
+
}
|
|
365
|
+
("object-entries", []) => {
|
|
366
|
+
self.tables = true;
|
|
367
|
+
Ok(format!("objectEntries({})", self.expression(indent)?))
|
|
368
|
+
}
|
|
369
|
+
("object-values", []) => {
|
|
370
|
+
self.tables = true;
|
|
371
|
+
Ok(format!("objectValues({})", self.expression(indent)?))
|
|
372
|
+
}
|
|
373
|
+
("object-descriptor", []) => {
|
|
374
|
+
self.tables = true;
|
|
375
|
+
Ok(format!(
|
|
376
|
+
"objectDescriptor({}, {})",
|
|
377
|
+
self.expression(indent)?,
|
|
378
|
+
self.expression(indent)?
|
|
379
|
+
))
|
|
380
|
+
}
|
|
381
|
+
("array-length" | "string-length", []) => {
|
|
382
|
+
Ok(format!("(#({}))", self.expression(indent)?))
|
|
383
|
+
}
|
|
384
|
+
("optional-length", []) => Ok(format!(
|
|
385
|
+
"(function() local target = {}; if target ~= nil then return #target end; return nil end)()",
|
|
386
|
+
self.expression(indent)?
|
|
387
|
+
)),
|
|
388
|
+
("update-local", [operator @ ("+" | "-"), result @ ("old" | "new"), name]) => {
|
|
389
|
+
let name = identifier(name)?;
|
|
390
|
+
Ok(update_number("", &name, operator, result))
|
|
391
|
+
}
|
|
392
|
+
("update-property", [operator @ ("+" | "-"), result @ ("old" | "new"), key]) => {
|
|
393
|
+
let target = self.expression(indent)?;
|
|
394
|
+
let property = format!("target[{}]", quote(&decode(key)?));
|
|
395
|
+
Ok(update_number(
|
|
396
|
+
&format!("local target = {target}; "),
|
|
397
|
+
&property,
|
|
398
|
+
operator,
|
|
399
|
+
result,
|
|
400
|
+
))
|
|
401
|
+
}
|
|
402
|
+
("update-index", [operator @ ("+" | "-"), result @ ("old" | "new")]) => {
|
|
403
|
+
let target = self.expression(indent)?;
|
|
404
|
+
let key = self.expression(indent)?;
|
|
405
|
+
Ok(update_number(
|
|
406
|
+
&format!("local target = {target}; local key = {key}; "),
|
|
407
|
+
"target[key]",
|
|
408
|
+
operator,
|
|
409
|
+
result,
|
|
410
|
+
))
|
|
411
|
+
}
|
|
412
|
+
("assignment", []) => self.assignment(indent, None),
|
|
413
|
+
("nullish-assignment", []) => self.assignment(indent, Some("??")),
|
|
414
|
+
("logical-assignment", [mode @ ("or" | "and")]) => self.assignment(indent, Some(mode)),
|
|
415
|
+
("compound", [operator @ ("+" | "-" | "*" | "/" | "^" | "..")]) => {
|
|
416
|
+
self.assignment(indent, Some(operator))
|
|
417
|
+
}
|
|
418
|
+
(
|
|
419
|
+
"call"
|
|
420
|
+
| "method"
|
|
421
|
+
| "method-index"
|
|
422
|
+
| "optional-call"
|
|
423
|
+
| "optional-intrinsic"
|
|
424
|
+
| "optional-member-call"
|
|
425
|
+
| "optional-chain"
|
|
426
|
+
| "array-push"
|
|
427
|
+
| "array-pop",
|
|
428
|
+
_,
|
|
429
|
+
) => Ok(format!("({})", self.call(tag, &args, indent)?)),
|
|
430
|
+
("multi-array", []) => {
|
|
431
|
+
let (tag, arguments) = self.record()?;
|
|
432
|
+
if !matches!(tag, "call" | "method" | "method-index") {
|
|
433
|
+
return Err("Expected a native call to capture multiple values".into());
|
|
434
|
+
}
|
|
435
|
+
Ok(format!("{{ {} }}", self.call(tag, &arguments, indent)?))
|
|
436
|
+
}
|
|
437
|
+
("multi-slot", [index]) => {
|
|
438
|
+
let index: usize = index.parse()?;
|
|
439
|
+
let (tag, arguments) = self.record()?;
|
|
440
|
+
if !matches!(tag, "call" | "method" | "method-index") || index == 0 {
|
|
441
|
+
return Err("Expected a native call and a positive result index".into());
|
|
442
|
+
}
|
|
443
|
+
let source = self.call(tag, &arguments, indent)?;
|
|
444
|
+
if index == 1 {
|
|
445
|
+
Ok(format!("({source})"))
|
|
446
|
+
} else {
|
|
447
|
+
Ok(format!("(select({index}, {source}))"))
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
("object", [count]) => self.object(count.parse()?, indent),
|
|
451
|
+
("array", [count]) => self.array(count.parse()?, indent),
|
|
452
|
+
("array-copy", []) => Ok(format!("{{ unpack({}) }}", self.expression(indent)?)),
|
|
453
|
+
(
|
|
454
|
+
"arrow"
|
|
455
|
+
| "arrow-receiver"
|
|
456
|
+
| "object-method"
|
|
457
|
+
| "named-arrow"
|
|
458
|
+
| "named-object-method",
|
|
459
|
+
names,
|
|
460
|
+
) => {
|
|
461
|
+
let (name, names) = if tag.starts_with("named-") {
|
|
462
|
+
let [name, names @ ..] = names else {
|
|
463
|
+
return Err("Expected a named function binding".into());
|
|
464
|
+
};
|
|
465
|
+
(Some(identifier(name)?), names)
|
|
466
|
+
} else {
|
|
467
|
+
(None, names)
|
|
468
|
+
};
|
|
469
|
+
let (mut names, prologue) =
|
|
470
|
+
parameters(names, tag.ends_with("object-method"), indent + 1)?;
|
|
471
|
+
if tag == "arrow-receiver" {
|
|
472
|
+
names.insert_str(
|
|
473
|
+
0,
|
|
474
|
+
if names.is_empty() {
|
|
475
|
+
"ignoredReceiver"
|
|
476
|
+
} else {
|
|
477
|
+
"ignoredReceiver, "
|
|
478
|
+
},
|
|
479
|
+
);
|
|
480
|
+
}
|
|
481
|
+
let function = format!(
|
|
482
|
+
"function({names})\n{prologue}{}{pad}end",
|
|
483
|
+
self.block(indent + 1)?
|
|
484
|
+
);
|
|
485
|
+
Ok(match name {
|
|
486
|
+
Some(name) => format!(
|
|
487
|
+
"(function() local {name}; {name} = {function}; return {name} end)()"
|
|
488
|
+
),
|
|
489
|
+
None => function,
|
|
490
|
+
})
|
|
491
|
+
}
|
|
492
|
+
("select", []) => {
|
|
493
|
+
let condition = self.expression(indent)?;
|
|
494
|
+
let then_value = self.expression(indent)?;
|
|
495
|
+
let else_value = self.expression(indent)?;
|
|
496
|
+
Ok(format!("(function() if {condition} then return {then_value} else return {else_value} end end)()"))
|
|
497
|
+
}
|
|
498
|
+
// The frontend proved the first branch is never nil or false, so `and`/`or` keeps lazy evaluation.
|
|
499
|
+
("select-truthy", []) => {
|
|
500
|
+
let condition = self.expression(indent)?;
|
|
501
|
+
let then_value = self.expression(indent)?;
|
|
502
|
+
let else_value = self.expression(indent)?;
|
|
503
|
+
Ok(format!("(({condition}) and ({then_value}) or ({else_value}))"))
|
|
504
|
+
}
|
|
505
|
+
("nullish", []) => {
|
|
506
|
+
let left = self.expression(indent)?;
|
|
507
|
+
let right = self.expression(indent)?;
|
|
508
|
+
Ok(format!("(function() local value = {left}; if value == nil then return {right} end; return value end)()"))
|
|
509
|
+
}
|
|
510
|
+
("not", []) => Ok(format!("(not {})", self.expression(indent)?)),
|
|
511
|
+
("negate", []) => Ok(format!("(-({}))", self.expression(indent)?)),
|
|
512
|
+
("binary", ["%"]) => {
|
|
513
|
+
let left = self.expression(indent)?;
|
|
514
|
+
let right = self.expression(indent)?;
|
|
515
|
+
Ok(format!("math.fmod({left}, {right})"))
|
|
516
|
+
}
|
|
517
|
+
("lua-modulo", []) => {
|
|
518
|
+
let left = self.expression(indent)?;
|
|
519
|
+
let right = self.expression(indent)?;
|
|
520
|
+
Ok(format!("({left} % {right})"))
|
|
521
|
+
}
|
|
522
|
+
(
|
|
523
|
+
"binary",
|
|
524
|
+
[operator @ ("==" | "~=" | "+" | "-" | "*" | "^" | "/" | "<" | "<=" | ">" | ">="
|
|
525
|
+
| ".." | "and" | "or")],
|
|
526
|
+
) => {
|
|
527
|
+
let left = self.expression(indent)?;
|
|
528
|
+
let right = self.expression(indent)?;
|
|
529
|
+
Ok(format!("({left} {operator} {right})"))
|
|
530
|
+
}
|
|
531
|
+
_ => Err(format!("Unknown compiler expression '{tag}'").into()),
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
fn template(&mut self, count: usize, head: &str, indent: usize) -> Compile<String> {
|
|
536
|
+
let mut parts = vec![quote(&decode(head)?)];
|
|
537
|
+
for _ in 0..count {
|
|
538
|
+
let (tag, fields) = self.record()?;
|
|
539
|
+
let ("template-part", [mode @ ("string" | "scalar"), tail]) = (tag, fields.as_slice())
|
|
540
|
+
else {
|
|
541
|
+
return Err("Invalid template part".into());
|
|
542
|
+
};
|
|
543
|
+
let value = self.expression(indent)?;
|
|
544
|
+
parts.push(if *mode == "string" {
|
|
545
|
+
format!("({value})")
|
|
546
|
+
} else {
|
|
547
|
+
format!("tostring({value})")
|
|
548
|
+
});
|
|
549
|
+
parts.push(quote(&decode(tail)?));
|
|
550
|
+
}
|
|
551
|
+
Ok(format!("({})", parts.join(" .. ")))
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
fn array(&mut self, count: usize, indent: usize) -> Compile<String> {
|
|
555
|
+
let mut parts = Vec::with_capacity(count);
|
|
556
|
+
let mut mixed = false;
|
|
557
|
+
for index in 0..count {
|
|
558
|
+
if self.records.clone().next() == Some("spread-array") {
|
|
559
|
+
self.records.next();
|
|
560
|
+
parts.push(ArrayPart::Spread(self.expression(indent)?));
|
|
561
|
+
mixed |= index + 1 != count;
|
|
562
|
+
} else {
|
|
563
|
+
parts.push(ArrayPart::Value(self.expression(indent)?));
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
if !mixed {
|
|
567
|
+
let entries = parts
|
|
568
|
+
.into_iter()
|
|
569
|
+
.map(|part| match part {
|
|
570
|
+
ArrayPart::Value(value) => value,
|
|
571
|
+
ArrayPart::Spread(source) => format!("unpack({source})"),
|
|
572
|
+
})
|
|
573
|
+
.collect::<Vec<_>>();
|
|
574
|
+
return Ok(format!("{{ {} }}", entries.join(", ")));
|
|
575
|
+
}
|
|
576
|
+
self.arrays = true;
|
|
577
|
+
let mut out = String::from("(function() local values = {}; ");
|
|
578
|
+
for part in parts {
|
|
579
|
+
match part {
|
|
580
|
+
ArrayPart::Value(value) => write!(out, "values[#values + 1] = {value}; ")?,
|
|
581
|
+
ArrayPart::Spread(source) => write!(out, "arrayAppend(values, {source}); ")?,
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
out.push_str("return values end)()");
|
|
585
|
+
Ok(out)
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
fn object(&mut self, count: usize, indent: usize) -> Compile<String> {
|
|
589
|
+
let mut parts = Vec::with_capacity(count);
|
|
590
|
+
let mut spread = false;
|
|
591
|
+
for _ in 0..count {
|
|
592
|
+
let (tag, args) = self.record()?;
|
|
593
|
+
match (tag, args.as_slice()) {
|
|
594
|
+
("field", [key]) => parts.push(ObjectPart::Field(
|
|
595
|
+
quote(&decode(key)?),
|
|
596
|
+
self.expression(indent)?,
|
|
597
|
+
)),
|
|
598
|
+
("computed-field", []) => parts.push(ObjectPart::Field(
|
|
599
|
+
self.expression(indent)?,
|
|
600
|
+
self.expression(indent)?,
|
|
601
|
+
)),
|
|
602
|
+
("spread-fields", [mode @ ("present" | "nullable" | "structural")]) => {
|
|
603
|
+
spread = true;
|
|
604
|
+
parts.push(ObjectPart::Spread {
|
|
605
|
+
source: self.expression(indent)?,
|
|
606
|
+
mode: match *mode {
|
|
607
|
+
"present" => SpreadMode::Present,
|
|
608
|
+
"nullable" => SpreadMode::Nullable,
|
|
609
|
+
_ => SpreadMode::Structural,
|
|
610
|
+
},
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
_ => return Err("Expected object field".into()),
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
if !spread {
|
|
617
|
+
let fields = parts
|
|
618
|
+
.into_iter()
|
|
619
|
+
.map(|part| match part {
|
|
620
|
+
ObjectPart::Field(key, value) => format!("[{key}] = {value}"),
|
|
621
|
+
ObjectPart::Spread { .. } => unreachable!(),
|
|
622
|
+
})
|
|
623
|
+
.collect::<Vec<_>>();
|
|
624
|
+
return Ok(format!("{{ {} }}", fields.join(", ")));
|
|
625
|
+
}
|
|
626
|
+
self.tables = true;
|
|
627
|
+
let mut out = String::from("(function() local object = {}; ");
|
|
628
|
+
for part in parts {
|
|
629
|
+
match part {
|
|
630
|
+
ObjectPart::Field(key, value) => write!(out, "object[{key}] = {value}; ")?,
|
|
631
|
+
ObjectPart::Spread { source, mode } => match mode {
|
|
632
|
+
SpreadMode::Present => write!(out, "copyFields(object, {source}); ")?,
|
|
633
|
+
SpreadMode::Nullable => write!(out, "do local source = {source}; if source ~= nil then copyFields(object, source) end end; ")?,
|
|
634
|
+
SpreadMode::Structural => write!(out, "copyStructuralFields(object, {source}); ")?,
|
|
635
|
+
},
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
out.push_str("return object end)()");
|
|
639
|
+
Ok(out)
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
/// Lower an assignment or `++`/`--` whose value is discarded into a plain Lua statement.
|
|
643
|
+
fn effect_statement(&mut self, indent: usize) -> Compile<String> {
|
|
644
|
+
let (tag, args) = self.record()?;
|
|
645
|
+
match (tag, args.as_slice()) {
|
|
646
|
+
("update-local", [operator @ ("+" | "-"), "old" | "new", name]) => {
|
|
647
|
+
let name = identifier(name)?;
|
|
648
|
+
Ok(format!("{name} = {name} {operator} 1"))
|
|
649
|
+
}
|
|
650
|
+
("update-property", [operator @ ("+" | "-"), "old" | "new", key]) => {
|
|
651
|
+
let target = self.expression(indent)?;
|
|
652
|
+
let key = quote(&decode(key)?);
|
|
653
|
+
Ok(format!(
|
|
654
|
+
"do local target = {target}; target[{key}] = target[{key}] {operator} 1 end"
|
|
655
|
+
))
|
|
656
|
+
}
|
|
657
|
+
("update-index", [operator @ ("+" | "-"), "old" | "new"]) => {
|
|
658
|
+
let target = self.expression(indent)?;
|
|
659
|
+
let key = self.expression(indent)?;
|
|
660
|
+
Ok(format!("do local target = {target}; local key = {key}; target[key] = target[key] {operator} 1 end"))
|
|
661
|
+
}
|
|
662
|
+
("assignment", []) => self.assignment_statement(indent, None),
|
|
663
|
+
("nullish-assignment", []) => self.assignment_statement(indent, Some("??")),
|
|
664
|
+
("logical-assignment", [mode @ ("or" | "and")]) => {
|
|
665
|
+
self.assignment_statement(indent, Some(mode))
|
|
666
|
+
}
|
|
667
|
+
("compound", [operator @ ("+" | "-" | "*" | "/" | "^" | "..")]) => {
|
|
668
|
+
self.assignment_statement(indent, Some(operator))
|
|
669
|
+
}
|
|
670
|
+
_ => Err(format!("Unexpected discarded assignment: {tag}").into()),
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
fn assignment_target(&mut self, indent: usize) -> Compile<(String, String)> {
|
|
675
|
+
let (kind, fields) = self.record()?;
|
|
676
|
+
let mut setup = String::new();
|
|
677
|
+
let target = match (kind, fields.as_slice()) {
|
|
678
|
+
("target-local", [name]) => identifier(name)?,
|
|
679
|
+
("target-property", [key]) => {
|
|
680
|
+
write!(setup, "local target = {}; ", self.expression(indent)?)?;
|
|
681
|
+
format!("target[{}]", quote(&decode(key)?))
|
|
682
|
+
}
|
|
683
|
+
("target-index" | "target-array-index", []) => {
|
|
684
|
+
let target = self.expression(indent)?;
|
|
685
|
+
let key = self.expression(indent)?;
|
|
686
|
+
let key = if kind == "target-array-index" {
|
|
687
|
+
format!("({key}) + 1")
|
|
688
|
+
} else {
|
|
689
|
+
key
|
|
690
|
+
};
|
|
691
|
+
write!(setup, "local target = {target}; local key = {key}; ")?;
|
|
692
|
+
"target[key]".into()
|
|
693
|
+
}
|
|
694
|
+
_ => return Err("Invalid assignment target".into()),
|
|
695
|
+
};
|
|
696
|
+
Ok((setup, target))
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
fn assignment_statement(&mut self, indent: usize, compound: Option<&str>) -> Compile<String> {
|
|
700
|
+
let (setup, target) = self.assignment_target(indent)?;
|
|
701
|
+
let value = self.expression(indent)?;
|
|
702
|
+
let assign = match compound {
|
|
703
|
+
None => format!("{target} = {value}"),
|
|
704
|
+
Some("??") => {
|
|
705
|
+
format!("local current = {target}; if current == nil then {target} = {value} end")
|
|
706
|
+
}
|
|
707
|
+
Some("or") => {
|
|
708
|
+
format!("local current = {target}; if not current then {target} = {value} end")
|
|
709
|
+
}
|
|
710
|
+
Some("and") => {
|
|
711
|
+
format!("local current = {target}; if current then {target} = {value} end")
|
|
712
|
+
}
|
|
713
|
+
Some(operator) => {
|
|
714
|
+
format!("local current = {target}; {target} = current {operator} ({value})")
|
|
715
|
+
}
|
|
716
|
+
};
|
|
717
|
+
Ok(if setup.is_empty() && compound.is_none() {
|
|
718
|
+
assign
|
|
719
|
+
} else {
|
|
720
|
+
format!("do {setup}{assign} end")
|
|
721
|
+
})
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
fn assignment(&mut self, indent: usize, compound: Option<&str>) -> Compile<String> {
|
|
725
|
+
let (kind, fields) = self.record()?;
|
|
726
|
+
let mut setup = String::new();
|
|
727
|
+
let target = match (kind, fields.as_slice()) {
|
|
728
|
+
("target-local", [name]) => identifier(name)?,
|
|
729
|
+
("target-property", [key]) => {
|
|
730
|
+
write!(setup, "local target = {}; ", self.expression(indent)?)?;
|
|
731
|
+
format!("target[{}]", quote(&decode(key)?))
|
|
732
|
+
}
|
|
733
|
+
("target-index" | "target-array-index", []) => {
|
|
734
|
+
let target = self.expression(indent)?;
|
|
735
|
+
let key = self.expression(indent)?;
|
|
736
|
+
let key = if kind == "target-array-index" {
|
|
737
|
+
format!("({key}) + 1")
|
|
738
|
+
} else {
|
|
739
|
+
key
|
|
740
|
+
};
|
|
741
|
+
write!(setup, "local target = {target}; local key = {key}; ")?;
|
|
742
|
+
"target[key]".into()
|
|
743
|
+
}
|
|
744
|
+
_ => return Err("Invalid assignment target".into()),
|
|
745
|
+
};
|
|
746
|
+
let value = self.expression(indent)?;
|
|
747
|
+
let guard = match compound {
|
|
748
|
+
Some("??") => Some("current ~= nil"),
|
|
749
|
+
Some("or") => Some("current"),
|
|
750
|
+
Some("and") => Some("not current"),
|
|
751
|
+
_ => None,
|
|
752
|
+
};
|
|
753
|
+
let rhs = if let Some(guard) = guard {
|
|
754
|
+
write!(
|
|
755
|
+
setup,
|
|
756
|
+
"local current = {target}; if {guard} then return current end; "
|
|
757
|
+
)?;
|
|
758
|
+
value
|
|
759
|
+
} else if let Some(operator) = compound {
|
|
760
|
+
write!(setup, "local current = {target}; ")?;
|
|
761
|
+
format!("current {operator} ({value})")
|
|
762
|
+
} else {
|
|
763
|
+
value
|
|
764
|
+
};
|
|
765
|
+
Ok(format!(
|
|
766
|
+
"(function() {setup}local result = {rhs}; {target} = result; return result end)()"
|
|
767
|
+
))
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
fn call(&mut self, tag: &str, args: &[&str], indent: usize) -> Compile<String> {
|
|
771
|
+
if let ("optional-intrinsic", [count]) = (tag, args) {
|
|
772
|
+
let callback = self.expression(indent)?;
|
|
773
|
+
let target = self.expression(indent)?;
|
|
774
|
+
let mut arguments = vec!["value".into()];
|
|
775
|
+
arguments.extend(self.arguments(count.parse()?, indent)?);
|
|
776
|
+
return Ok(format!("(function() local value = {target}; if value ~= nil then return ({callback}({})) end; return nil end)()", arguments.join(", ")));
|
|
777
|
+
}
|
|
778
|
+
if let ("optional-chain", [steps]) = (tag, args) {
|
|
779
|
+
return self.optional_chain(steps.parse()?, indent);
|
|
780
|
+
}
|
|
781
|
+
if matches!((tag, args), ("array-push" | "array-pop", [])) {
|
|
782
|
+
self.arrays = true;
|
|
783
|
+
let array = self.expression(indent)?;
|
|
784
|
+
return if tag == "array-push" {
|
|
785
|
+
Ok(format!(
|
|
786
|
+
"arrays.push({array}, {})",
|
|
787
|
+
self.expression(indent)?
|
|
788
|
+
))
|
|
789
|
+
} else {
|
|
790
|
+
Ok(format!("arrays.pop({array})"))
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
if let (
|
|
794
|
+
"optional-member-call",
|
|
795
|
+
[count, key, convention @ ("self" | "free"), mode @ ("receiver" | "member" | "both")],
|
|
796
|
+
) = (tag, args)
|
|
797
|
+
{
|
|
798
|
+
let receiver = self.expression(indent)?;
|
|
799
|
+
let mut args = self.arguments(count.parse()?, indent)?;
|
|
800
|
+
if *convention == "self" {
|
|
801
|
+
args.insert(0, "target".into());
|
|
802
|
+
}
|
|
803
|
+
let guard_receiver = if *mode == "member" {
|
|
804
|
+
""
|
|
805
|
+
} else {
|
|
806
|
+
"if target == nil then return nil end; "
|
|
807
|
+
};
|
|
808
|
+
let guard_callback = if *mode == "receiver" {
|
|
809
|
+
""
|
|
810
|
+
} else {
|
|
811
|
+
"if callback == nil then return nil end; "
|
|
812
|
+
};
|
|
813
|
+
return Ok(format!("(function() local target = {receiver}; {guard_receiver}local callback = target[{}]; {guard_callback}return (callback({})) end)()", quote(&decode(key)?), args.join(", ")));
|
|
814
|
+
}
|
|
815
|
+
if let ("method-index", [count]) = (tag, args) {
|
|
816
|
+
let receiver = self.expression(indent)?;
|
|
817
|
+
let key = self.expression(indent)?;
|
|
818
|
+
let mut arguments = self.arguments(count.parse()?, indent)?;
|
|
819
|
+
arguments.insert(0, "target".into());
|
|
820
|
+
return Ok(format!(
|
|
821
|
+
"(function(target, key) return target[key]({}) end)({receiver}, {key})",
|
|
822
|
+
arguments.join(", ")
|
|
823
|
+
));
|
|
824
|
+
}
|
|
825
|
+
let (count, method) = match (tag, args) {
|
|
826
|
+
("call" | "optional-call", [count]) => (*count, None),
|
|
827
|
+
("method", [count, key]) => (*count, Some(decode(key)?)),
|
|
828
|
+
_ => return Err("Expected a call instruction".into()),
|
|
829
|
+
};
|
|
830
|
+
let receiver = self.expression(indent)?;
|
|
831
|
+
let arguments = self.arguments(count.parse()?, indent)?.join(", ");
|
|
832
|
+
if tag == "optional-call" {
|
|
833
|
+
Ok(format!("(function() local callback = {receiver}; if callback ~= nil then return (callback({arguments})) end; return nil end)()"))
|
|
834
|
+
} else if let Some(method) = method {
|
|
835
|
+
identifier(&method)?;
|
|
836
|
+
// WoW's typed native methods have identifier names. Reject Lua keywords.
|
|
837
|
+
if matches!(
|
|
838
|
+
method.as_str(),
|
|
839
|
+
"and"
|
|
840
|
+
| "break"
|
|
841
|
+
| "do"
|
|
842
|
+
| "else"
|
|
843
|
+
| "elseif"
|
|
844
|
+
| "end"
|
|
845
|
+
| "false"
|
|
846
|
+
| "for"
|
|
847
|
+
| "function"
|
|
848
|
+
| "if"
|
|
849
|
+
| "in"
|
|
850
|
+
| "local"
|
|
851
|
+
| "nil"
|
|
852
|
+
| "not"
|
|
853
|
+
| "or"
|
|
854
|
+
| "repeat"
|
|
855
|
+
| "return"
|
|
856
|
+
| "then"
|
|
857
|
+
| "true"
|
|
858
|
+
| "until"
|
|
859
|
+
| "while"
|
|
860
|
+
) {
|
|
861
|
+
return Err("Invalid native method name in compiler input".into());
|
|
862
|
+
}
|
|
863
|
+
Ok(format!("({receiver}):{method}({arguments})"))
|
|
864
|
+
} else {
|
|
865
|
+
Ok(format!("({receiver})({arguments})"))
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
fn optional_chain(&mut self, steps: usize, indent: usize) -> Compile<String> {
|
|
870
|
+
let target = self.expression(indent)?;
|
|
871
|
+
let mut out = format!("(function() local target = {target}; ");
|
|
872
|
+
for _ in 0..steps {
|
|
873
|
+
let (tag, args) = self.record()?;
|
|
874
|
+
if args.first() == Some(&"1") {
|
|
875
|
+
out.push_str("if target == nil then return nil end; ");
|
|
876
|
+
}
|
|
877
|
+
match (tag, args.as_slice()) {
|
|
878
|
+
("chain-field", ["0" | "1", key]) => {
|
|
879
|
+
write!(out, "target = target[{}]; ", quote(&decode(key)?))?;
|
|
880
|
+
}
|
|
881
|
+
("chain-length", ["0" | "1"]) => out.push_str("target = #target; "),
|
|
882
|
+
("chain-index", ["0" | "1", kind]) => {
|
|
883
|
+
let key = self.chain_index(kind, indent)?;
|
|
884
|
+
write!(out, "target = target[{key}]; ")?;
|
|
885
|
+
}
|
|
886
|
+
("chain-call", ["0" | "1", count]) => {
|
|
887
|
+
let args = self.arguments(count.parse()?, indent)?.join(", ");
|
|
888
|
+
write!(out, "target = (target({args})); ")?;
|
|
889
|
+
}
|
|
890
|
+
("chain-intrinsic", ["0" | "1", count]) => {
|
|
891
|
+
let callback = self.expression(indent)?;
|
|
892
|
+
let mut args = vec!["target".into()];
|
|
893
|
+
args.extend(self.arguments(count.parse()?, indent)?);
|
|
894
|
+
write!(out, "target = ({callback}({})); ", args.join(", "))?;
|
|
895
|
+
}
|
|
896
|
+
(
|
|
897
|
+
"chain-member" | "chain-index-call",
|
|
898
|
+
["0" | "1", guard @ ("0" | "1"), convention @ ("self" | "free"), count, key],
|
|
899
|
+
) => {
|
|
900
|
+
let key = if tag == "chain-member" {
|
|
901
|
+
quote(&decode(key)?)
|
|
902
|
+
} else {
|
|
903
|
+
self.chain_index(key, indent)?
|
|
904
|
+
};
|
|
905
|
+
write!(out, "local callback = target[{key}]; ")?;
|
|
906
|
+
if *guard == "1" {
|
|
907
|
+
out.push_str("if callback == nil then return nil end; ");
|
|
908
|
+
}
|
|
909
|
+
let mut args = self.arguments(count.parse()?, indent)?;
|
|
910
|
+
if *convention == "self" {
|
|
911
|
+
args.insert(0, "target".into());
|
|
912
|
+
}
|
|
913
|
+
write!(out, "target = (callback({})); ", args.join(", "))?;
|
|
914
|
+
}
|
|
915
|
+
_ => return Err("Invalid native optional chain step".into()),
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
out.push_str("return target end)()");
|
|
919
|
+
Ok(out)
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
fn chain_index(&mut self, kind: &str, indent: usize) -> Compile<String> {
|
|
923
|
+
let key = self.expression(indent)?;
|
|
924
|
+
match kind {
|
|
925
|
+
"array" => Ok(format!("({key}) + 1")),
|
|
926
|
+
"table" => Ok(key),
|
|
927
|
+
_ => Err("Invalid native chain index kind".into()),
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
fn arguments(&mut self, count: usize, indent: usize) -> Compile<Vec<String>> {
|
|
932
|
+
let mut arguments = Vec::new();
|
|
933
|
+
let mut packed = false;
|
|
934
|
+
for index in 0..count {
|
|
935
|
+
let next = self.records.clone().next().unwrap_or_default();
|
|
936
|
+
if matches!(next, "spread-array" | "spread-native") || next.starts_with("spread-rest\t")
|
|
937
|
+
{
|
|
938
|
+
packed |= index + 1 != count;
|
|
939
|
+
let (tag, fields) = self.record()?;
|
|
940
|
+
let spread = match (tag, fields.as_slice()) {
|
|
941
|
+
("spread-array", []) => format!("unpack({})", self.expression(indent)?),
|
|
942
|
+
("spread-native", []) => {
|
|
943
|
+
let (call, operands) = self.record()?;
|
|
944
|
+
if !matches!(call, "call" | "method" | "method-index") {
|
|
945
|
+
return Err("Expected a call for forwarded Lua results".into());
|
|
946
|
+
}
|
|
947
|
+
self.call(call, &operands, indent)?
|
|
948
|
+
}
|
|
949
|
+
("spread-rest", [binding]) => {
|
|
950
|
+
let name = identifier(binding)?;
|
|
951
|
+
format!("unpack({name}, 1, {name}_count)")
|
|
952
|
+
}
|
|
953
|
+
_ => return Err("Invalid spread instruction".into()),
|
|
954
|
+
};
|
|
955
|
+
arguments.push((spread, true));
|
|
956
|
+
} else {
|
|
957
|
+
arguments.push((self.expression(indent)?, false));
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
if !packed {
|
|
961
|
+
return Ok(arguments.into_iter().map(|(value, _)| value).collect());
|
|
962
|
+
}
|
|
963
|
+
let mut spread = String::from(
|
|
964
|
+
"(function() local slots, size = {}, 0; local function append(...) \
|
|
965
|
+
local count = select('#', ...); for index = 1, count do slots[size + index] = select(index, ...) end; \
|
|
966
|
+
size = size + count end; ",
|
|
967
|
+
);
|
|
968
|
+
for (value, expands) in arguments {
|
|
969
|
+
if expands {
|
|
970
|
+
write!(spread, "append({value}); ")?;
|
|
971
|
+
} else {
|
|
972
|
+
write!(spread, "append(({value})); ")?;
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
spread.push_str("return unpack(slots, 1, size) end)()");
|
|
976
|
+
Ok(vec![spread])
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
fn has_scalar_branch(&self) -> bool {
|
|
980
|
+
matches!(self.records.clone().next(), Some("nullish" | "select"))
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
fn scalar_value(
|
|
984
|
+
&mut self,
|
|
985
|
+
indent: usize,
|
|
986
|
+
out: &mut String,
|
|
987
|
+
target: Option<&str>,
|
|
988
|
+
) -> Compile<()> {
|
|
989
|
+
let pad = " ".repeat(indent);
|
|
990
|
+
let write_value = |out: &mut String, pad: &str, value: &str| match target {
|
|
991
|
+
Some(target) => writeln!(out, "{pad}{target} = {value}"),
|
|
992
|
+
None => writeln!(out, "{pad}do return {value} end"),
|
|
993
|
+
};
|
|
994
|
+
match self.records.clone().next() {
|
|
995
|
+
Some("nullish") => {
|
|
996
|
+
self.records.next();
|
|
997
|
+
let left = self.expression(indent + 1)?;
|
|
998
|
+
writeln!(out, "{pad}do")?;
|
|
999
|
+
writeln!(out, "{pad} local value = {left}")?;
|
|
1000
|
+
writeln!(out, "{pad} if value == nil then")?;
|
|
1001
|
+
self.scalar_value(indent + 2, out, target)?;
|
|
1002
|
+
writeln!(out, "{pad} else")?;
|
|
1003
|
+
write_value(out, &format!("{pad} "), "value")?;
|
|
1004
|
+
writeln!(out, "{pad} end")?;
|
|
1005
|
+
writeln!(out, "{pad}end")?;
|
|
1006
|
+
}
|
|
1007
|
+
Some("select") => {
|
|
1008
|
+
self.records.next();
|
|
1009
|
+
let condition = self.expression(indent)?;
|
|
1010
|
+
writeln!(out, "{pad}if {condition} then")?;
|
|
1011
|
+
self.scalar_value(indent + 1, out, target)?;
|
|
1012
|
+
writeln!(out, "{pad}else")?;
|
|
1013
|
+
self.scalar_value(indent + 1, out, target)?;
|
|
1014
|
+
writeln!(out, "{pad}end")?;
|
|
1015
|
+
}
|
|
1016
|
+
_ => write_value(out, &pad, &self.expression(indent)?)?,
|
|
1017
|
+
}
|
|
1018
|
+
Ok(())
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
fn block(&mut self, indent: usize) -> Compile<String> {
|
|
1022
|
+
let ("block", args) = self.record()? else {
|
|
1023
|
+
return Err("Expected statement block".into());
|
|
1024
|
+
};
|
|
1025
|
+
let [count] = args.as_slice() else {
|
|
1026
|
+
return Err("Invalid statement count".into());
|
|
1027
|
+
};
|
|
1028
|
+
let pad = " ".repeat(indent);
|
|
1029
|
+
let mut out = String::new();
|
|
1030
|
+
for _ in 0..count.parse::<usize>()? {
|
|
1031
|
+
let (tag, args) = self.record()?;
|
|
1032
|
+
match (tag, args.as_slice()) {
|
|
1033
|
+
("return", []) => {
|
|
1034
|
+
self.scalar_value(indent, &mut out, None)?;
|
|
1035
|
+
}
|
|
1036
|
+
("raise", []) => {
|
|
1037
|
+
writeln!(out, "{pad}error({}, 0)", self.expression(indent)?)?;
|
|
1038
|
+
}
|
|
1039
|
+
("try-exit", []) => writeln!(out, "{pad}do return true end")?,
|
|
1040
|
+
("try-return", []) => {
|
|
1041
|
+
writeln!(out, "{pad}do return true, {} end", self.expression(indent)?)?;
|
|
1042
|
+
}
|
|
1043
|
+
(
|
|
1044
|
+
"try",
|
|
1045
|
+
[catch @ ("0" | "1"), binding, finally @ ("0" | "1"), returns @ ("0" | "1" | "2"), parent @ ("0" | "1")],
|
|
1046
|
+
) => {
|
|
1047
|
+
let result_binding = if *returns == "2" { ", result" } else { "" };
|
|
1048
|
+
writeln!(
|
|
1049
|
+
out,
|
|
1050
|
+
"{pad}do\n{pad} local passed, failure{result_binding} = pcall(function()"
|
|
1051
|
+
)?;
|
|
1052
|
+
out.push_str(&self.block(indent + 2)?);
|
|
1053
|
+
writeln!(out, "{pad} end)")?;
|
|
1054
|
+
if *catch == "1" {
|
|
1055
|
+
writeln!(out, "{pad} if not passed then\n{pad} passed, failure{result_binding} = pcall(function()")?;
|
|
1056
|
+
if *binding != "-" {
|
|
1057
|
+
writeln!(
|
|
1058
|
+
out,
|
|
1059
|
+
"{pad} local {} = failure",
|
|
1060
|
+
identifier(binding)?
|
|
1061
|
+
)?;
|
|
1062
|
+
}
|
|
1063
|
+
out.push_str(&self.block(indent + 3)?);
|
|
1064
|
+
writeln!(out, "{pad} end)\n{pad} end")?;
|
|
1065
|
+
}
|
|
1066
|
+
if *finally == "1" {
|
|
1067
|
+
writeln!(out, "{pad} do")?;
|
|
1068
|
+
out.push_str(&self.block(indent + 2)?);
|
|
1069
|
+
writeln!(out, "{pad} end")?;
|
|
1070
|
+
}
|
|
1071
|
+
writeln!(out, "{pad} if not passed then error(failure, 0) end")?;
|
|
1072
|
+
if *returns != "0" {
|
|
1073
|
+
let result = match (*parent, *returns) {
|
|
1074
|
+
("1", "2") => "true, result",
|
|
1075
|
+
("1", _) => "true",
|
|
1076
|
+
(_, "2") => "result",
|
|
1077
|
+
_ => "nil",
|
|
1078
|
+
};
|
|
1079
|
+
writeln!(out, "{pad} if failure == true then return {result} end")?;
|
|
1080
|
+
}
|
|
1081
|
+
writeln!(out, "{pad}end")?;
|
|
1082
|
+
}
|
|
1083
|
+
("return-multi", [count]) => {
|
|
1084
|
+
let values = self.arguments(count.parse()?, indent)?.join(", ");
|
|
1085
|
+
if values.is_empty() {
|
|
1086
|
+
writeln!(out, "{pad}do return end")?;
|
|
1087
|
+
} else {
|
|
1088
|
+
writeln!(out, "{pad}do return {values} end")?;
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
("return-wow-reveal", [value]) => {
|
|
1092
|
+
let value = identifier(value)?;
|
|
1093
|
+
writeln!(out, "{pad}do")?;
|
|
1094
|
+
writeln!(out, "{pad} local secret = issecretvalue")?;
|
|
1095
|
+
writeln!(
|
|
1096
|
+
out,
|
|
1097
|
+
"{pad} if secret ~= nil and secret({value}) == true then return nil end"
|
|
1098
|
+
)?;
|
|
1099
|
+
writeln!(out, "{pad} return {value}\n{pad}end")?;
|
|
1100
|
+
}
|
|
1101
|
+
("return-native", []) => {
|
|
1102
|
+
let (tag, arguments) = self.record()?;
|
|
1103
|
+
if !matches!(tag, "call" | "method" | "method-index") {
|
|
1104
|
+
return Err("Expected a native call to forward multiple values".into());
|
|
1105
|
+
}
|
|
1106
|
+
let values = self.call(tag, &arguments, indent)?;
|
|
1107
|
+
writeln!(out, "{pad}do return {values} end")?;
|
|
1108
|
+
}
|
|
1109
|
+
("predeclare-locals", names) if (1..=190).contains(&names.len()) => {
|
|
1110
|
+
let names = names
|
|
1111
|
+
.iter()
|
|
1112
|
+
.map(|name| identifier(name))
|
|
1113
|
+
.collect::<Compile<Vec<_>>>()?;
|
|
1114
|
+
writeln!(out, "{pad}local {}", names.join(", "))?;
|
|
1115
|
+
}
|
|
1116
|
+
("local", [name]) if !self.has_scalar_branch() => writeln!(
|
|
1117
|
+
out,
|
|
1118
|
+
"{pad}local {} = {}",
|
|
1119
|
+
identifier(name)?,
|
|
1120
|
+
self.expression(indent)?
|
|
1121
|
+
)?,
|
|
1122
|
+
("local" | "local-initializer", [name]) => {
|
|
1123
|
+
let name = identifier(name)?;
|
|
1124
|
+
writeln!(out, "{pad}local {name}")?;
|
|
1125
|
+
self.scalar_value(indent, &mut out, Some(&name))?;
|
|
1126
|
+
}
|
|
1127
|
+
("object-local" | "object-binding", [rest, fields @ ..])
|
|
1128
|
+
if fields.len().is_multiple_of(2) && (!fields.is_empty() || *rest != "-") =>
|
|
1129
|
+
{
|
|
1130
|
+
let (pairs, _) = fields.as_chunks::<2>();
|
|
1131
|
+
let bindings = pairs
|
|
1132
|
+
.iter()
|
|
1133
|
+
.map(|field| {
|
|
1134
|
+
let name = field[0].strip_suffix(":default");
|
|
1135
|
+
Ok((
|
|
1136
|
+
identifier(name.unwrap_or(field[0]))?,
|
|
1137
|
+
quote(&decode(field[1])?),
|
|
1138
|
+
name.is_some(),
|
|
1139
|
+
))
|
|
1140
|
+
})
|
|
1141
|
+
.collect::<Compile<Vec<_>>>()?;
|
|
1142
|
+
let structural = rest.strip_suffix(":structural");
|
|
1143
|
+
let rest = (*rest != "-")
|
|
1144
|
+
.then(|| identifier(structural.unwrap_or(rest)))
|
|
1145
|
+
.transpose()?;
|
|
1146
|
+
let names = bindings
|
|
1147
|
+
.iter()
|
|
1148
|
+
.map(|(name, _, _)| name.as_str())
|
|
1149
|
+
.chain(rest.as_deref())
|
|
1150
|
+
.collect::<Vec<_>>();
|
|
1151
|
+
if tag == "object-local" {
|
|
1152
|
+
writeln!(out, "{pad}local {}", names.join(", "))?;
|
|
1153
|
+
}
|
|
1154
|
+
writeln!(out, "{pad}do")?;
|
|
1155
|
+
writeln!(
|
|
1156
|
+
out,
|
|
1157
|
+
"{pad} local source = {}",
|
|
1158
|
+
self.expression(indent + 1)?
|
|
1159
|
+
)?;
|
|
1160
|
+
for (name, key, default) in &bindings {
|
|
1161
|
+
writeln!(out, "{pad} {name} = source[{key}]")?;
|
|
1162
|
+
if *default {
|
|
1163
|
+
let fallback = self.expression(indent + 1)?;
|
|
1164
|
+
writeln!(
|
|
1165
|
+
out,
|
|
1166
|
+
"{pad} if {name} == nil then {name} = {fallback} end"
|
|
1167
|
+
)?;
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
if let Some(rest) = rest {
|
|
1171
|
+
self.tables = true;
|
|
1172
|
+
let skipped = bindings
|
|
1173
|
+
.iter()
|
|
1174
|
+
.map(|(_, key, _)| format!("[{key}] = true"))
|
|
1175
|
+
.collect::<Vec<_>>()
|
|
1176
|
+
.join(", ");
|
|
1177
|
+
let helper = if structural.is_some() {
|
|
1178
|
+
"restStructuralFields"
|
|
1179
|
+
} else {
|
|
1180
|
+
"restFields"
|
|
1181
|
+
};
|
|
1182
|
+
writeln!(out, "{pad} {rest} = {helper}(source, {{ {skipped} }})")?;
|
|
1183
|
+
}
|
|
1184
|
+
writeln!(out, "{pad}end")?;
|
|
1185
|
+
}
|
|
1186
|
+
("pattern-local" | "pattern-binding", names) if !names.is_empty() => {
|
|
1187
|
+
if tag == "pattern-local" {
|
|
1188
|
+
let locals = names
|
|
1189
|
+
.iter()
|
|
1190
|
+
.map(|name| identifier(name))
|
|
1191
|
+
.collect::<Compile<Vec<_>>>()?;
|
|
1192
|
+
writeln!(out, "{pad}local {}", locals.join(", "))?;
|
|
1193
|
+
}
|
|
1194
|
+
writeln!(
|
|
1195
|
+
out,
|
|
1196
|
+
"{pad}do\n{pad} local source = {}",
|
|
1197
|
+
self.expression(indent + 1)?
|
|
1198
|
+
)?;
|
|
1199
|
+
self.binding_pattern(indent + 1, &mut out)?;
|
|
1200
|
+
writeln!(out, "{pad}end")?;
|
|
1201
|
+
}
|
|
1202
|
+
("local-function" | "local-function-self", [name, params @ ..]) => {
|
|
1203
|
+
let name = identifier(name)?;
|
|
1204
|
+
let (params, prologue) =
|
|
1205
|
+
parameters(params, tag == "local-function-self", indent + 1)?;
|
|
1206
|
+
writeln!(out, "{pad}{name} = function({params})")?;
|
|
1207
|
+
out.push_str(&prologue);
|
|
1208
|
+
out.push_str(&self.block(indent + 1)?);
|
|
1209
|
+
writeln!(out, "{pad}end")?;
|
|
1210
|
+
}
|
|
1211
|
+
("readable-multi-local" | "readable-multi-binding", names) if !names.is_empty() => {
|
|
1212
|
+
let mut targets = Vec::new();
|
|
1213
|
+
let mut raw = Vec::new();
|
|
1214
|
+
let mut revealed = Vec::new();
|
|
1215
|
+
for (index, name) in names.iter().enumerate() {
|
|
1216
|
+
if *name == "-" {
|
|
1217
|
+
raw.push("_".into());
|
|
1218
|
+
} else {
|
|
1219
|
+
targets.push(identifier(name)?);
|
|
1220
|
+
raw.push(format!("raw{}", index + 1));
|
|
1221
|
+
revealed.push(format!("wowReadable({})", raw[index]));
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
if targets.is_empty() {
|
|
1225
|
+
return Err(
|
|
1226
|
+
"A readable Lua tuple requires at least one result binding".into()
|
|
1227
|
+
);
|
|
1228
|
+
}
|
|
1229
|
+
self.wow_reads = true;
|
|
1230
|
+
let (operation, operands) = self.record()?;
|
|
1231
|
+
let values = self.call(operation, &operands, indent + 1)?;
|
|
1232
|
+
let guard = if targets.len() == 1 {
|
|
1233
|
+
let offset = names.iter().position(|name| *name != "-").unwrap_or(0);
|
|
1234
|
+
["wowReadable", "wowReadableSecond", "wowReadableThird"].get(offset)
|
|
1235
|
+
} else {
|
|
1236
|
+
None
|
|
1237
|
+
};
|
|
1238
|
+
let targets = targets.join(", ");
|
|
1239
|
+
let revealed = revealed.join(", ");
|
|
1240
|
+
if let Some(guard) = guard {
|
|
1241
|
+
let local = if tag == "readable-multi-local" {
|
|
1242
|
+
"local "
|
|
1243
|
+
} else {
|
|
1244
|
+
""
|
|
1245
|
+
};
|
|
1246
|
+
writeln!(out, "{pad}{local}{targets} = {guard}({values})")?;
|
|
1247
|
+
} else {
|
|
1248
|
+
if tag == "readable-multi-local" {
|
|
1249
|
+
writeln!(out, "{pad}local {targets}")?;
|
|
1250
|
+
}
|
|
1251
|
+
let raw = raw.join(", ");
|
|
1252
|
+
writeln!(out, "{pad}do\n{pad} local {raw} = {values}\n{pad} {targets} = {revealed}\n{pad}end")?;
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
("multi-local" | "multi-binding", names) if !names.is_empty() => {
|
|
1256
|
+
let omitted = names.contains(&"-");
|
|
1257
|
+
let names = names
|
|
1258
|
+
.iter()
|
|
1259
|
+
.map(|name| {
|
|
1260
|
+
if *name == "-" {
|
|
1261
|
+
Ok("_".into())
|
|
1262
|
+
} else {
|
|
1263
|
+
identifier(name)
|
|
1264
|
+
}
|
|
1265
|
+
})
|
|
1266
|
+
.collect::<Compile<Vec<_>>>()?
|
|
1267
|
+
.join(", ");
|
|
1268
|
+
let (operation, operands) = self.record()?;
|
|
1269
|
+
let values = if let ("multi-list", [count]) = (operation, operands.as_slice()) {
|
|
1270
|
+
if *count == "0" {
|
|
1271
|
+
"nil".into()
|
|
1272
|
+
} else {
|
|
1273
|
+
self.arguments(count.parse()?, indent)?.join(", ")
|
|
1274
|
+
}
|
|
1275
|
+
} else {
|
|
1276
|
+
self.call(operation, &operands, indent)?
|
|
1277
|
+
};
|
|
1278
|
+
if tag == "multi-local" {
|
|
1279
|
+
writeln!(out, "{pad}local {names} = {values}")?;
|
|
1280
|
+
} else if omitted {
|
|
1281
|
+
writeln!(out, "{pad}do local _; {names} = {values} end")?;
|
|
1282
|
+
} else {
|
|
1283
|
+
writeln!(out, "{pad}{names} = {values}")?;
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
("tuple-local" | "tuple-binding", bindings) if !bindings.is_empty() => {
|
|
1287
|
+
let mut names = Vec::new();
|
|
1288
|
+
let mut positions = Vec::new();
|
|
1289
|
+
for (index, binding) in bindings.iter().enumerate() {
|
|
1290
|
+
if *binding == "-" {
|
|
1291
|
+
continue;
|
|
1292
|
+
}
|
|
1293
|
+
let (name, default) = match binding.strip_suffix(":default") {
|
|
1294
|
+
Some(name) => (identifier(name)?, true),
|
|
1295
|
+
None => (identifier(binding)?, false),
|
|
1296
|
+
};
|
|
1297
|
+
names.push(name.clone());
|
|
1298
|
+
positions.push((name, index + 1, default));
|
|
1299
|
+
}
|
|
1300
|
+
if tag == "tuple-local" {
|
|
1301
|
+
writeln!(out, "{pad}local {}", names.join(", "))?;
|
|
1302
|
+
}
|
|
1303
|
+
writeln!(out, "{pad}do")?;
|
|
1304
|
+
writeln!(
|
|
1305
|
+
out,
|
|
1306
|
+
"{pad} local tuple = {}",
|
|
1307
|
+
self.expression(indent + 1)?
|
|
1308
|
+
)?;
|
|
1309
|
+
for (name, index, default) in positions {
|
|
1310
|
+
writeln!(out, "{pad} {name} = tuple[{index}]")?;
|
|
1311
|
+
if default {
|
|
1312
|
+
let value = self.expression(indent + 1)?;
|
|
1313
|
+
writeln!(out, "{pad} if {name} == nil then {name} = {value} end")?;
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
writeln!(out, "{pad}end")?;
|
|
1317
|
+
}
|
|
1318
|
+
("assign", [name]) => {
|
|
1319
|
+
self.scalar_value(indent, &mut out, Some(&identifier(name)?))?;
|
|
1320
|
+
}
|
|
1321
|
+
("nil-assign", [name]) => {
|
|
1322
|
+
let name = identifier(name)?;
|
|
1323
|
+
let value = self.expression(indent + 1)?;
|
|
1324
|
+
writeln!(
|
|
1325
|
+
out,
|
|
1326
|
+
"{pad}if {name} == nil then\n{pad} {name} = {value}\n{pad}end"
|
|
1327
|
+
)?;
|
|
1328
|
+
}
|
|
1329
|
+
("write", [key]) => {
|
|
1330
|
+
let receiver = self.expression(indent + 1)?;
|
|
1331
|
+
let key = quote(&decode(key)?);
|
|
1332
|
+
if self.has_scalar_branch() {
|
|
1333
|
+
writeln!(out, "{pad}do")?;
|
|
1334
|
+
writeln!(out, "{pad} local target = {receiver}")?;
|
|
1335
|
+
self.scalar_value(indent + 1, &mut out, Some(&format!("target[{key}]")))?;
|
|
1336
|
+
writeln!(out, "{pad}end")?;
|
|
1337
|
+
} else {
|
|
1338
|
+
let value = self.expression(indent + 1)?;
|
|
1339
|
+
writeln!(
|
|
1340
|
+
out,
|
|
1341
|
+
"{pad}do local target = {receiver}; target[{key}] = {value} end"
|
|
1342
|
+
)?;
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
("table-write", []) => {
|
|
1346
|
+
let target = self.expression(indent + 1)?;
|
|
1347
|
+
let key = self.expression(indent + 1)?;
|
|
1348
|
+
let value = self.expression(indent + 1)?;
|
|
1349
|
+
writeln!(out, "{pad}do local target = {target}; local key = {key}; target[key] = {value} end")?;
|
|
1350
|
+
}
|
|
1351
|
+
("lua-array-write", []) => {
|
|
1352
|
+
let target = self.expression(indent + 1)?;
|
|
1353
|
+
let offset = self.expression(indent + 1)?;
|
|
1354
|
+
let value = self.expression(indent + 1)?;
|
|
1355
|
+
writeln!(out, "{pad}do local target = {target}; local index = ({offset}) + 1; target[index] = {value} end")?;
|
|
1356
|
+
}
|
|
1357
|
+
("effect", []) => {
|
|
1358
|
+
let (operation, operands) = self.record()?;
|
|
1359
|
+
writeln!(
|
|
1360
|
+
out,
|
|
1361
|
+
"{pad}do {} end",
|
|
1362
|
+
self.call(operation, &operands, indent + 1)?
|
|
1363
|
+
)?;
|
|
1364
|
+
}
|
|
1365
|
+
("effect-value", []) => {
|
|
1366
|
+
// An assignment or update used as a statement needs no closure for its result.
|
|
1367
|
+
let next = self
|
|
1368
|
+
.records
|
|
1369
|
+
.clone()
|
|
1370
|
+
.next()
|
|
1371
|
+
.and_then(|line| line.split('\t').next());
|
|
1372
|
+
let statement = match next {
|
|
1373
|
+
Some(
|
|
1374
|
+
"assignment" | "compound" | "nullish-assignment" | "logical-assignment"
|
|
1375
|
+
| "update-local" | "update-property" | "update-index",
|
|
1376
|
+
) => self.effect_statement(indent + 1)?,
|
|
1377
|
+
_ => format!("do local ignored = {} end", self.expression(indent + 1)?),
|
|
1378
|
+
};
|
|
1379
|
+
writeln!(out, "{pad}{statement}")?;
|
|
1380
|
+
}
|
|
1381
|
+
("array-write" | "array-trim", []) => {
|
|
1382
|
+
self.arrays = true;
|
|
1383
|
+
let array = self.expression(indent + 1)?;
|
|
1384
|
+
writeln!(out, "{pad}do")?;
|
|
1385
|
+
writeln!(out, "{pad} local target = {array}")?;
|
|
1386
|
+
if tag == "array-write" {
|
|
1387
|
+
let index = self.expression(indent + 1)?;
|
|
1388
|
+
writeln!(out, "{pad} local offset = {index}")?;
|
|
1389
|
+
let value = self.expression(indent + 1)?;
|
|
1390
|
+
writeln!(out, "{pad} arrays.write(target, offset, {value})")?;
|
|
1391
|
+
} else {
|
|
1392
|
+
let length = self.expression(indent + 1)?;
|
|
1393
|
+
writeln!(out, "{pad} arrays.trim(target, {length})")?;
|
|
1394
|
+
}
|
|
1395
|
+
writeln!(out, "{pad}end")?;
|
|
1396
|
+
}
|
|
1397
|
+
("for-array" | "for-array-or-set", [mode @ ("0" | "1"), name]) => {
|
|
1398
|
+
let name = identifier(name)?;
|
|
1399
|
+
let source = self.expression(indent)?;
|
|
1400
|
+
let iterator = if tag == "for-array-or-set" {
|
|
1401
|
+
self.collections = true;
|
|
1402
|
+
"collections.values"
|
|
1403
|
+
} else {
|
|
1404
|
+
"ipairs"
|
|
1405
|
+
};
|
|
1406
|
+
writeln!(out, "{pad}for _, {name} in {iterator}({source}) do")?;
|
|
1407
|
+
out.push_str(&self.loop_body(indent + 1, *mode == "1")?);
|
|
1408
|
+
writeln!(out, "{pad}end")?;
|
|
1409
|
+
}
|
|
1410
|
+
("for-native-values" | "for-raw-table", [mode @ ("0" | "1"), names @ ..])
|
|
1411
|
+
if !names.is_empty() =>
|
|
1412
|
+
{
|
|
1413
|
+
let names = names
|
|
1414
|
+
.iter()
|
|
1415
|
+
.enumerate()
|
|
1416
|
+
.map(|(index, name)| {
|
|
1417
|
+
if *name == "-" {
|
|
1418
|
+
Ok(format!("ignored{index}"))
|
|
1419
|
+
} else {
|
|
1420
|
+
identifier(name)
|
|
1421
|
+
}
|
|
1422
|
+
})
|
|
1423
|
+
.collect::<Compile<Vec<_>>>()?
|
|
1424
|
+
.join(", ");
|
|
1425
|
+
let iterator = if tag == "for-raw-table" {
|
|
1426
|
+
format!("pairs({})", self.expression(indent)?)
|
|
1427
|
+
} else if self.records.clone().next() == Some("iterator-call") {
|
|
1428
|
+
self.records.next();
|
|
1429
|
+
let (tag, arguments) = self.record()?;
|
|
1430
|
+
if !matches!(tag, "call" | "method" | "method-index") {
|
|
1431
|
+
return Err("Expected a native iterator call".into());
|
|
1432
|
+
}
|
|
1433
|
+
self.call(tag, &arguments, indent)?
|
|
1434
|
+
} else {
|
|
1435
|
+
self.expression(indent)?
|
|
1436
|
+
};
|
|
1437
|
+
writeln!(out, "{pad}for {names} in {iterator} do")?;
|
|
1438
|
+
out.push_str(&self.loop_body(indent + 1, *mode == "1")?);
|
|
1439
|
+
writeln!(out, "{pad}end")?;
|
|
1440
|
+
}
|
|
1441
|
+
("for-array-tuple", [mode @ ("0" | "1"), names @ ..]) => {
|
|
1442
|
+
let source = self.expression(indent)?;
|
|
1443
|
+
writeln!(out, "{pad}for _, tuple in ipairs({source}) do")?;
|
|
1444
|
+
for (index, name) in names.iter().enumerate() {
|
|
1445
|
+
if *name != "-" {
|
|
1446
|
+
let name = identifier(name)?;
|
|
1447
|
+
writeln!(out, "{pad} local {name} = tuple[{}]", index + 1)?;
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
out.push_str(&self.loop_body(indent + 1, *mode == "1")?);
|
|
1451
|
+
writeln!(out, "{pad}end")?;
|
|
1452
|
+
}
|
|
1453
|
+
("for-array-entries", [mode @ ("0" | "1"), index, item]) => {
|
|
1454
|
+
let item = if *item == "-" {
|
|
1455
|
+
"ignored_item".into()
|
|
1456
|
+
} else {
|
|
1457
|
+
identifier(item)?
|
|
1458
|
+
};
|
|
1459
|
+
let source = self.expression(indent)?;
|
|
1460
|
+
writeln!(out, "{pad}for index, {item} in ipairs({source}) do")?;
|
|
1461
|
+
if *index != "-" {
|
|
1462
|
+
writeln!(out, "{pad} local {} = index - 1", identifier(index)?)?;
|
|
1463
|
+
}
|
|
1464
|
+
out.push_str(&self.loop_body(indent + 1, *mode == "1")?);
|
|
1465
|
+
writeln!(out, "{pad}end")?;
|
|
1466
|
+
}
|
|
1467
|
+
(
|
|
1468
|
+
"for-collection" | "for-map-entries" | "for-map-entry-record",
|
|
1469
|
+
[mode @ ("0" | "1"), names @ ..],
|
|
1470
|
+
) if (1..=2).contains(&names.len()) => {
|
|
1471
|
+
self.collections = true;
|
|
1472
|
+
let names = names
|
|
1473
|
+
.iter()
|
|
1474
|
+
.enumerate()
|
|
1475
|
+
.map(|(index, name)| {
|
|
1476
|
+
if *name == "-" {
|
|
1477
|
+
Ok(format!("ignored{index}"))
|
|
1478
|
+
} else {
|
|
1479
|
+
identifier(name)
|
|
1480
|
+
}
|
|
1481
|
+
})
|
|
1482
|
+
.collect::<Compile<Vec<_>>>()?
|
|
1483
|
+
.join(", ");
|
|
1484
|
+
let source = self.expression(indent)?;
|
|
1485
|
+
let iterator = match tag {
|
|
1486
|
+
"for-map-entries" => "iterateEntries",
|
|
1487
|
+
"for-map-entry-record" => "iterateEntryRecords",
|
|
1488
|
+
_ => "iterate",
|
|
1489
|
+
};
|
|
1490
|
+
writeln!(
|
|
1491
|
+
out,
|
|
1492
|
+
"{pad}for _, {names} in collections.{iterator}({source}) do"
|
|
1493
|
+
)?;
|
|
1494
|
+
out.push_str(&self.loop_body(indent + 1, *mode == "1")?);
|
|
1495
|
+
writeln!(out, "{pad}end")?;
|
|
1496
|
+
}
|
|
1497
|
+
("for-existing", [mode @ ("0" | "1")]) => {
|
|
1498
|
+
let condition = self.expression(indent)?;
|
|
1499
|
+
writeln!(out, "{pad}while {condition} do")?;
|
|
1500
|
+
out.push_str(&self.loop_body(indent + 1, *mode == "1")?);
|
|
1501
|
+
out.push_str(&self.block(indent + 1)?);
|
|
1502
|
+
writeln!(out, "{pad}end")?;
|
|
1503
|
+
}
|
|
1504
|
+
("for-classic", [mode @ ("0" | "1"), name]) => {
|
|
1505
|
+
let name = identifier(name)?;
|
|
1506
|
+
let initial = self.expression(indent + 1)?;
|
|
1507
|
+
let condition = self.expression(indent + 2)?;
|
|
1508
|
+
writeln!(out, "{pad}do\n{pad} local {name}\n{pad} {name} = {initial}\n{pad} local carried = {name}")?;
|
|
1509
|
+
writeln!(
|
|
1510
|
+
out,
|
|
1511
|
+
"{pad} while true do\n{pad} local {name} = carried"
|
|
1512
|
+
)?;
|
|
1513
|
+
writeln!(out, "{pad} if not ({condition}) then break end")?;
|
|
1514
|
+
out.push_str(&self.loop_body(indent + 2, *mode == "1")?);
|
|
1515
|
+
writeln!(
|
|
1516
|
+
out,
|
|
1517
|
+
"{pad} do\n{pad} local {name} = {name}"
|
|
1518
|
+
)?;
|
|
1519
|
+
out.push_str(&self.block(indent + 3)?);
|
|
1520
|
+
writeln!(out, "{pad} carried = {name}\n{pad} end\n{pad} end\n{pad}end")?;
|
|
1521
|
+
}
|
|
1522
|
+
("break", []) => match self.loop_wrappers.last() {
|
|
1523
|
+
Some(true) => writeln!(out, "{pad}do loop_broken = true; break end")?,
|
|
1524
|
+
Some(false) => writeln!(out, "{pad}do break end")?,
|
|
1525
|
+
None => return Err("Break without a native loop".into()),
|
|
1526
|
+
},
|
|
1527
|
+
("continue", []) if self.loop_wrappers.last() == Some(&true) => {
|
|
1528
|
+
writeln!(out, "{pad}do break end")?;
|
|
1529
|
+
}
|
|
1530
|
+
("if", ["0" | "1"]) => {
|
|
1531
|
+
let condition = self.expression(indent)?;
|
|
1532
|
+
writeln!(out, "{pad}if {condition} then")?;
|
|
1533
|
+
out.push_str(&self.block(indent + 1)?);
|
|
1534
|
+
if args[0] == "1" {
|
|
1535
|
+
writeln!(out, "{pad}else")?;
|
|
1536
|
+
out.push_str(&self.block(indent + 1)?);
|
|
1537
|
+
}
|
|
1538
|
+
writeln!(out, "{pad}end")?;
|
|
1539
|
+
}
|
|
1540
|
+
("switch", [count, fallback @ ("0" | "1")]) => {
|
|
1541
|
+
let value = self.expression(indent + 1)?;
|
|
1542
|
+
let count = count.parse::<usize>()?;
|
|
1543
|
+
writeln!(out, "{pad}do\n{pad} local switch_value = {value}")?;
|
|
1544
|
+
if count == 0 {
|
|
1545
|
+
if *fallback == "1" {
|
|
1546
|
+
out.push_str(&self.block(indent + 1)?);
|
|
1547
|
+
}
|
|
1548
|
+
} else {
|
|
1549
|
+
for index in 0..count {
|
|
1550
|
+
let keyword = if index == 0 { "if" } else { "elseif" };
|
|
1551
|
+
let ("case", modes) = self.record()? else {
|
|
1552
|
+
return Err("Expected native switch case".into());
|
|
1553
|
+
};
|
|
1554
|
+
if modes.is_empty() {
|
|
1555
|
+
return Err("A native switch case requires a label".into());
|
|
1556
|
+
}
|
|
1557
|
+
let mut conditions = Vec::new();
|
|
1558
|
+
for mode in modes {
|
|
1559
|
+
let case = self.expression(indent + 1)?;
|
|
1560
|
+
let condition = match mode {
|
|
1561
|
+
"value" => format!("switch_value == {case}"),
|
|
1562
|
+
"identity" => format!("rawequal(switch_value, {case})"),
|
|
1563
|
+
_ => return Err("Invalid native switch comparison".into()),
|
|
1564
|
+
};
|
|
1565
|
+
conditions.push(condition);
|
|
1566
|
+
}
|
|
1567
|
+
let condition = conditions.join(" or ");
|
|
1568
|
+
writeln!(out, "{pad} {keyword} {condition} then")?;
|
|
1569
|
+
out.push_str(&self.block(indent + 2)?);
|
|
1570
|
+
}
|
|
1571
|
+
if *fallback == "1" {
|
|
1572
|
+
writeln!(out, "{pad} else")?;
|
|
1573
|
+
out.push_str(&self.block(indent + 2)?);
|
|
1574
|
+
}
|
|
1575
|
+
writeln!(out, "{pad} end")?;
|
|
1576
|
+
}
|
|
1577
|
+
writeln!(out, "{pad}end")?;
|
|
1578
|
+
}
|
|
1579
|
+
("scoped-block", []) => {
|
|
1580
|
+
writeln!(out, "{pad}do")?;
|
|
1581
|
+
out.push_str(&self.block(indent + 1)?);
|
|
1582
|
+
writeln!(out, "{pad}end")?;
|
|
1583
|
+
}
|
|
1584
|
+
("breakable-block", []) => {
|
|
1585
|
+
writeln!(out, "{pad}repeat")?;
|
|
1586
|
+
out.push_str(&self.loop_body(indent + 1, false)?);
|
|
1587
|
+
writeln!(out, "{pad}until true")?;
|
|
1588
|
+
}
|
|
1589
|
+
("while", [mode @ ("0" | "1")]) => {
|
|
1590
|
+
let condition = self.expression(indent)?;
|
|
1591
|
+
writeln!(out, "{pad}while {condition} do")?;
|
|
1592
|
+
out.push_str(&self.loop_body(indent + 1, *mode == "1")?);
|
|
1593
|
+
writeln!(out, "{pad}end")?;
|
|
1594
|
+
}
|
|
1595
|
+
("do-while", [mode @ ("0" | "1")]) => {
|
|
1596
|
+
writeln!(out, "{pad}repeat")?;
|
|
1597
|
+
out.push_str(&self.loop_body(indent + 1, *mode == "1")?);
|
|
1598
|
+
writeln!(out, "{pad}until not ({})", self.expression(indent)?)?;
|
|
1599
|
+
}
|
|
1600
|
+
_ => return Err(format!("Unknown compiler statement '{tag}'").into()),
|
|
1601
|
+
}
|
|
1602
|
+
}
|
|
1603
|
+
Ok(out)
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
fn loop_body(&mut self, indent: usize, wrapped: bool) -> Compile<String> {
|
|
1607
|
+
self.loop_wrappers.push(wrapped);
|
|
1608
|
+
let body = self.block(indent + usize::from(wrapped));
|
|
1609
|
+
self.loop_wrappers.pop();
|
|
1610
|
+
if !wrapped {
|
|
1611
|
+
return body;
|
|
1612
|
+
}
|
|
1613
|
+
let pad = " ".repeat(indent);
|
|
1614
|
+
Ok(format!("{pad}local loop_broken = false\n{pad}repeat\n{}{pad}until true\n{pad}if loop_broken then break end\n", body?))
|
|
1615
|
+
}
|
|
1616
|
+
|
|
1617
|
+
fn binding_pattern(&mut self, indent: usize, out: &mut String) -> Compile<()> {
|
|
1618
|
+
let ("pattern-fields", fields) = self.record()? else {
|
|
1619
|
+
return Err("Expected nested binding fields".into());
|
|
1620
|
+
};
|
|
1621
|
+
let [count] = fields.as_slice() else {
|
|
1622
|
+
return Err("Invalid binding field count".into());
|
|
1623
|
+
};
|
|
1624
|
+
let pad = " ".repeat(indent);
|
|
1625
|
+
for _ in 0..count.parse::<usize>()? {
|
|
1626
|
+
let (tag, fields) = self.record()?;
|
|
1627
|
+
let (key, name, default) = match (tag, fields.as_slice()) {
|
|
1628
|
+
("object-field" | "tuple-field", [key, name, default @ ("0" | "1")]) => {
|
|
1629
|
+
let key = if tag == "tuple-field" {
|
|
1630
|
+
key.parse::<usize>()?.to_string()
|
|
1631
|
+
} else {
|
|
1632
|
+
quote(&decode(key)?)
|
|
1633
|
+
};
|
|
1634
|
+
(key, *name, *default == "1")
|
|
1635
|
+
}
|
|
1636
|
+
("tuple-rest", [start, name]) => {
|
|
1637
|
+
self.arrays = true;
|
|
1638
|
+
writeln!(
|
|
1639
|
+
out,
|
|
1640
|
+
"{pad}{} = arraySlice(source, {})",
|
|
1641
|
+
identifier(name)?,
|
|
1642
|
+
start.parse::<usize>()?
|
|
1643
|
+
)?;
|
|
1644
|
+
continue;
|
|
1645
|
+
}
|
|
1646
|
+
_ => return Err("Invalid nested binding field".into()),
|
|
1647
|
+
};
|
|
1648
|
+
if name == "-" {
|
|
1649
|
+
writeln!(out, "{pad}do\n{pad} local source = source[{key}]")?;
|
|
1650
|
+
if default {
|
|
1651
|
+
writeln!(
|
|
1652
|
+
out,
|
|
1653
|
+
"{pad} if source == nil then source = {} end",
|
|
1654
|
+
self.expression(indent + 1)?
|
|
1655
|
+
)?;
|
|
1656
|
+
}
|
|
1657
|
+
self.binding_pattern(indent + 1, out)?;
|
|
1658
|
+
writeln!(out, "{pad}end")?;
|
|
1659
|
+
} else {
|
|
1660
|
+
let name = identifier(name)?;
|
|
1661
|
+
writeln!(out, "{pad}{name} = source[{key}]")?;
|
|
1662
|
+
if default {
|
|
1663
|
+
writeln!(
|
|
1664
|
+
out,
|
|
1665
|
+
"{pad}if {name} == nil then {name} = {} end",
|
|
1666
|
+
self.expression(indent)?
|
|
1667
|
+
)?;
|
|
1668
|
+
}
|
|
1669
|
+
}
|
|
1670
|
+
}
|
|
1671
|
+
Ok(())
|
|
1672
|
+
}
|
|
1673
|
+
|
|
1674
|
+
fn module(&mut self, id: usize, dependencies: &mut Vec<usize>) -> Compile<String> {
|
|
1675
|
+
let mut locals = Vec::new();
|
|
1676
|
+
let mut exports = String::new();
|
|
1677
|
+
let mut declarations = String::new();
|
|
1678
|
+
let mut functions = String::new();
|
|
1679
|
+
let mut initialization = String::new();
|
|
1680
|
+
loop {
|
|
1681
|
+
let (tag, args) = self.record()?;
|
|
1682
|
+
match (tag, args.as_slice()) {
|
|
1683
|
+
("end-module", []) => break,
|
|
1684
|
+
("import", [name, dependency, key]) => {
|
|
1685
|
+
let dependency = dependency.parse::<usize>()?;
|
|
1686
|
+
if dependency == 0 || dependency >= id {
|
|
1687
|
+
return Err(
|
|
1688
|
+
"A runtime dependency must be linked before its importer".into()
|
|
1689
|
+
);
|
|
1690
|
+
}
|
|
1691
|
+
let dependency = self.dependency_slot(dependencies, dependency);
|
|
1692
|
+
writeln!(
|
|
1693
|
+
declarations,
|
|
1694
|
+
"local {} = modules[{dependency}][{}]",
|
|
1695
|
+
identifier(name)?,
|
|
1696
|
+
quote(&decode(key)?)
|
|
1697
|
+
)?;
|
|
1698
|
+
}
|
|
1699
|
+
("import-module", [name, dependency]) => {
|
|
1700
|
+
let dependency = dependency.parse::<usize>()?;
|
|
1701
|
+
if dependency == 0 || dependency >= id {
|
|
1702
|
+
return Err("A namespace import must follow its dependency".into());
|
|
1703
|
+
}
|
|
1704
|
+
let dependency = self.dependency_slot(dependencies, dependency);
|
|
1705
|
+
writeln!(
|
|
1706
|
+
declarations,
|
|
1707
|
+
"local {} = modules[{dependency}]",
|
|
1708
|
+
identifier(name)?
|
|
1709
|
+
)?;
|
|
1710
|
+
}
|
|
1711
|
+
("re-export", [key, dependency, original]) => {
|
|
1712
|
+
let dependency = dependency.parse::<usize>()?;
|
|
1713
|
+
if dependency == 0 || dependency >= id {
|
|
1714
|
+
return Err(
|
|
1715
|
+
"A re-exported dependency must be linked before its importer".into(),
|
|
1716
|
+
);
|
|
1717
|
+
}
|
|
1718
|
+
let dependency = self.dependency_slot(dependencies, dependency);
|
|
1719
|
+
writeln!(
|
|
1720
|
+
exports,
|
|
1721
|
+
"module[{}] = modules[{dependency}][{}]",
|
|
1722
|
+
quote(&decode(key)?),
|
|
1723
|
+
quote(&decode(original)?)
|
|
1724
|
+
)?;
|
|
1725
|
+
}
|
|
1726
|
+
("export-binding", [key, name]) => {
|
|
1727
|
+
writeln!(
|
|
1728
|
+
exports,
|
|
1729
|
+
"module[{}] = {}",
|
|
1730
|
+
quote(&decode(key)?),
|
|
1731
|
+
identifier(name)?
|
|
1732
|
+
)?;
|
|
1733
|
+
}
|
|
1734
|
+
("module-locals", names) if !names.is_empty() => {
|
|
1735
|
+
for name in names {
|
|
1736
|
+
let name = identifier(name)?;
|
|
1737
|
+
if locals.len() >= 190 || locals.contains(&name) {
|
|
1738
|
+
return Err("Invalid or excessive Lua module locals".into());
|
|
1739
|
+
}
|
|
1740
|
+
locals.push(name);
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1743
|
+
("module-init", []) => initialization.push_str(&self.block(0)?),
|
|
1744
|
+
("function" | "function-self", [export, name, params @ ..]) => {
|
|
1745
|
+
let local = identifier(name)?;
|
|
1746
|
+
if locals.len() >= 190 || locals.contains(&local) {
|
|
1747
|
+
return Err("Invalid or excessive Lua module locals".into());
|
|
1748
|
+
}
|
|
1749
|
+
locals.push(local.clone());
|
|
1750
|
+
let (params, prologue) = parameters(params, tag == "function-self", 1)?;
|
|
1751
|
+
writeln!(functions, "{local} = function({params})")?;
|
|
1752
|
+
functions.push_str(&prologue);
|
|
1753
|
+
functions.push_str(&self.block(1)?);
|
|
1754
|
+
functions.push_str("end\n");
|
|
1755
|
+
if *export != "-" {
|
|
1756
|
+
writeln!(exports, "module[{}] = {local}", quote(&decode(export)?))?;
|
|
1757
|
+
}
|
|
1758
|
+
}
|
|
1759
|
+
("binding" | "class", [export, name]) => {
|
|
1760
|
+
let local = identifier(name)?;
|
|
1761
|
+
if locals.len() >= 190 || locals.contains(&local) {
|
|
1762
|
+
return Err("Invalid or excessive Lua module locals".into());
|
|
1763
|
+
}
|
|
1764
|
+
let value = if tag == "class" {
|
|
1765
|
+
self.class_body()?
|
|
1766
|
+
} else {
|
|
1767
|
+
self.expression(0)?
|
|
1768
|
+
};
|
|
1769
|
+
writeln!(initialization, "{local} = {value}")?;
|
|
1770
|
+
if *export != "-" {
|
|
1771
|
+
writeln!(exports, "module[{}] = {local}", quote(&decode(export)?))?;
|
|
1772
|
+
}
|
|
1773
|
+
locals.push(local);
|
|
1774
|
+
}
|
|
1775
|
+
_ => return Err(format!("Unknown module instruction '{tag}'").into()),
|
|
1776
|
+
}
|
|
1777
|
+
}
|
|
1778
|
+
let mut out = String::from("local module = {}\n");
|
|
1779
|
+
out.push_str(&declarations);
|
|
1780
|
+
for local in locals {
|
|
1781
|
+
writeln!(out, "local {local}")?;
|
|
1782
|
+
}
|
|
1783
|
+
out.push_str(&functions);
|
|
1784
|
+
out.push_str(&initialization);
|
|
1785
|
+
out.push_str(&exports);
|
|
1786
|
+
out.push_str("return module\n");
|
|
1787
|
+
Ok(out)
|
|
1788
|
+
}
|
|
1789
|
+
|
|
1790
|
+
fn dependency_slot(&self, dependencies: &mut Vec<usize>, dependency: usize) -> usize {
|
|
1791
|
+
if !self.fragments {
|
|
1792
|
+
return dependency;
|
|
1793
|
+
}
|
|
1794
|
+
if let Some(slot) = dependencies.iter().position(|&id| id == dependency) {
|
|
1795
|
+
return slot + 1;
|
|
1796
|
+
}
|
|
1797
|
+
dependencies.push(dependency);
|
|
1798
|
+
dependencies.len()
|
|
1799
|
+
}
|
|
1800
|
+
|
|
1801
|
+
fn class_body(&mut self) -> Compile<String> {
|
|
1802
|
+
self.classes = true;
|
|
1803
|
+
let ("class-body", members) = self.record()? else {
|
|
1804
|
+
return Err("Expected native class body".into());
|
|
1805
|
+
};
|
|
1806
|
+
let [name, count, derived @ ("0" | "1")] = members.as_slice() else {
|
|
1807
|
+
return Err("Invalid class member count".into());
|
|
1808
|
+
};
|
|
1809
|
+
let derived = *derived == "1";
|
|
1810
|
+
let mut out = String::from(
|
|
1811
|
+
"(function()\n local methods, getters, setters, statics = {}, {}, {}, {}\n",
|
|
1812
|
+
);
|
|
1813
|
+
if derived {
|
|
1814
|
+
let base = self.expression(1)?;
|
|
1815
|
+
writeln!(out, " local base = {base}\n local basePrototype = assert(classPrototypes[base], 'Expected a compiled Kumi base class')")?;
|
|
1816
|
+
}
|
|
1817
|
+
for _ in 0..count.parse::<usize>()? {
|
|
1818
|
+
let (kind, member) = self.record()?;
|
|
1819
|
+
let table = match kind {
|
|
1820
|
+
"method" | "free-method" => "methods",
|
|
1821
|
+
"static-method" | "free-static-method" => "statics",
|
|
1822
|
+
"getter" => "getters",
|
|
1823
|
+
"setter" => "setters",
|
|
1824
|
+
_ => return Err("Invalid native class member".into()),
|
|
1825
|
+
};
|
|
1826
|
+
let [key, params @ ..] = member.as_slice() else {
|
|
1827
|
+
return Err("Missing class member name".into());
|
|
1828
|
+
};
|
|
1829
|
+
let (params, prologue) = parameters(params, !kind.starts_with("free-"), 2)?;
|
|
1830
|
+
writeln!(
|
|
1831
|
+
out,
|
|
1832
|
+
" {table}[{}] = function({params})",
|
|
1833
|
+
quote(&decode(key)?)
|
|
1834
|
+
)?;
|
|
1835
|
+
out.push_str(&prologue);
|
|
1836
|
+
out.push_str(&self.block(2)?);
|
|
1837
|
+
out.push_str(" end\n");
|
|
1838
|
+
}
|
|
1839
|
+
let ("constructor", constructor) = self.record()? else {
|
|
1840
|
+
return Err("Expected native class constructor".into());
|
|
1841
|
+
};
|
|
1842
|
+
let [fields, params @ ..] = constructor.as_slice() else {
|
|
1843
|
+
return Err("Invalid native class constructor".into());
|
|
1844
|
+
};
|
|
1845
|
+
let (params, prologue) = parameters(params, true, 2)?;
|
|
1846
|
+
let base_prototype = if derived { ", basePrototype" } else { "" };
|
|
1847
|
+
writeln!(
|
|
1848
|
+
out,
|
|
1849
|
+
" local prototype = classPrototype(methods, getters, setters{base_prototype})"
|
|
1850
|
+
)?;
|
|
1851
|
+
writeln!(out, " local function construct({params})")?;
|
|
1852
|
+
out.push_str(&prologue);
|
|
1853
|
+
out.push_str(&self.block(2)?);
|
|
1854
|
+
out.push_str(" if self == nil then self = setmetatable({}, prototype) end\n");
|
|
1855
|
+
if derived {
|
|
1856
|
+
let (opcode, operands) = self.record()?;
|
|
1857
|
+
match (opcode, operands.as_slice()) {
|
|
1858
|
+
("super-forward", []) => {
|
|
1859
|
+
out.push_str(" basePrototype.kumiConstruct(self, ...)\n")
|
|
1860
|
+
}
|
|
1861
|
+
("super-init", [count]) => {
|
|
1862
|
+
let arguments = self.arguments(count.parse()?, 2)?.join(", ");
|
|
1863
|
+
let separator = if arguments.is_empty() { "" } else { ", " };
|
|
1864
|
+
writeln!(
|
|
1865
|
+
out,
|
|
1866
|
+
" basePrototype.kumiConstruct(self{separator}{arguments})"
|
|
1867
|
+
)?;
|
|
1868
|
+
}
|
|
1869
|
+
_ => return Err("Expected native base class initialization".into()),
|
|
1870
|
+
}
|
|
1871
|
+
}
|
|
1872
|
+
for _ in 0..fields.parse::<usize>()? {
|
|
1873
|
+
let (operation @ ("class-field" | "define-class-field"), field) = self.record()? else {
|
|
1874
|
+
return Err("Expected native instance field".into());
|
|
1875
|
+
};
|
|
1876
|
+
let [key] = field.as_slice() else {
|
|
1877
|
+
return Err("Invalid native instance field".into());
|
|
1878
|
+
};
|
|
1879
|
+
let value = self.expression(2)?;
|
|
1880
|
+
let key = quote(&decode(key)?);
|
|
1881
|
+
if operation == "define-class-field" {
|
|
1882
|
+
writeln!(out, " rawset(self, {key}, {value})")?;
|
|
1883
|
+
} else {
|
|
1884
|
+
writeln!(out, " self[{key}] = {value}")?;
|
|
1885
|
+
}
|
|
1886
|
+
}
|
|
1887
|
+
out.push_str(&self.block(2)?);
|
|
1888
|
+
out.push_str(" return self\n end\n");
|
|
1889
|
+
let base_value = if derived { ", base" } else { "" };
|
|
1890
|
+
writeln!(
|
|
1891
|
+
out,
|
|
1892
|
+
" return classValue(construct, prototype, methods, statics, {}{base_value})",
|
|
1893
|
+
quote(&decode(name)?)
|
|
1894
|
+
)?;
|
|
1895
|
+
out.push_str("end)()");
|
|
1896
|
+
Ok(out)
|
|
1897
|
+
}
|
|
1898
|
+
|
|
1899
|
+
fn project(
|
|
1900
|
+
mut self,
|
|
1901
|
+
workspace: &Path,
|
|
1902
|
+
addon: bool,
|
|
1903
|
+
link: Option<&str>,
|
|
1904
|
+
plugin: Option<AddonPlugin>,
|
|
1905
|
+
) -> Compile<Vec<u8>> {
|
|
1906
|
+
if self.records.next() != Some("kumi-project-1") {
|
|
1907
|
+
return Err("Unexpected Kumi project IR version".into());
|
|
1908
|
+
}
|
|
1909
|
+
let mut out = String::new();
|
|
1910
|
+
let mut modules = 0;
|
|
1911
|
+
let mut addon_assets = false;
|
|
1912
|
+
let mut addon_core = false;
|
|
1913
|
+
let mut identity = None;
|
|
1914
|
+
let mut fragments = Vec::new();
|
|
1915
|
+
loop {
|
|
1916
|
+
let (opcode, fields) = self.record()?;
|
|
1917
|
+
let mut dependencies = Vec::new();
|
|
1918
|
+
self.addon_globals.clear();
|
|
1919
|
+
let contents = match (opcode, fields.as_slice()) {
|
|
1920
|
+
("entry", [id])
|
|
1921
|
+
if identity.is_none() && modules > 0 && id.parse::<usize>()? == modules =>
|
|
1922
|
+
{
|
|
1923
|
+
break
|
|
1924
|
+
}
|
|
1925
|
+
("linked-module", [key, fingerprint])
|
|
1926
|
+
if link.is_some()
|
|
1927
|
+
&& identity.is_none()
|
|
1928
|
+
&& fingerprint.len() == 64
|
|
1929
|
+
&& fingerprint.as_bytes().iter().all(u8::is_ascii_hexdigit) =>
|
|
1930
|
+
{
|
|
1931
|
+
identity = Some((quote(&decode(key)?), quote(fingerprint)));
|
|
1932
|
+
continue;
|
|
1933
|
+
}
|
|
1934
|
+
("addon-core", [id]) if addon && !addon_core => {
|
|
1935
|
+
let id = id.parse::<usize>()?;
|
|
1936
|
+
if id == 0 || id > modules {
|
|
1937
|
+
return Err("The addon UI core must already be linked".into());
|
|
1938
|
+
}
|
|
1939
|
+
let binding = if self.fragments {
|
|
1940
|
+
"nativeContext.addonUi"
|
|
1941
|
+
} else {
|
|
1942
|
+
"addonUi"
|
|
1943
|
+
};
|
|
1944
|
+
writeln!(out, "{binding} = modules[{id}]")?;
|
|
1945
|
+
addon_core = true;
|
|
1946
|
+
continue;
|
|
1947
|
+
}
|
|
1948
|
+
("module", [id]) if id.parse::<usize>()? == modules + 1 => {
|
|
1949
|
+
self.module(modules + 1, &mut dependencies)?
|
|
1950
|
+
}
|
|
1951
|
+
("native-signal", [id, source, fingerprint])
|
|
1952
|
+
if id.parse::<usize>()? == modules + 1 =>
|
|
1953
|
+
{
|
|
1954
|
+
let signal = crate::native_ir::signal_from_hashes(source, fingerprint)?;
|
|
1955
|
+
crate::native_signal::emit(signal)
|
|
1956
|
+
}
|
|
1957
|
+
("direct-tga", [id, input]) if addon && id.parse::<usize>()? == modules + 1 => {
|
|
1958
|
+
let filename = decode(input)?;
|
|
1959
|
+
let input = Path::new(&filename);
|
|
1960
|
+
if !input.starts_with("Assets")
|
|
1961
|
+
|| !input
|
|
1962
|
+
.components()
|
|
1963
|
+
.all(|part| matches!(part, Component::Normal(_)))
|
|
1964
|
+
{
|
|
1965
|
+
return Err("Addon asset must be inside Assets".into());
|
|
1966
|
+
}
|
|
1967
|
+
let path = workspace.join(input);
|
|
1968
|
+
let (width, height) = crate::assets::direct_tga_dimensions(&path)
|
|
1969
|
+
.map_err(|error| format!("{filename}: {error}"))?;
|
|
1970
|
+
addon_assets = true;
|
|
1971
|
+
if self.fragments {
|
|
1972
|
+
self.addon_globals.insert("addonTexturePath");
|
|
1973
|
+
}
|
|
1974
|
+
texture_module(&filename, [width, height, width, height])
|
|
1975
|
+
}
|
|
1976
|
+
("addon-texture", [id, path, width, height, texture_width, texture_height])
|
|
1977
|
+
if addon && id.parse::<usize>()? == modules + 1 =>
|
|
1978
|
+
{
|
|
1979
|
+
let path = decode(path)?;
|
|
1980
|
+
let hash = path
|
|
1981
|
+
.strip_prefix("KumiGenerated/Assets/")
|
|
1982
|
+
.and_then(|name| name.strip_suffix(".tga"));
|
|
1983
|
+
if !hash.is_some_and(|hash| {
|
|
1984
|
+
hash.len() == 64 && hash.as_bytes().iter().all(u8::is_ascii_hexdigit)
|
|
1985
|
+
}) {
|
|
1986
|
+
return Err("Invalid converted addon texture path".into());
|
|
1987
|
+
}
|
|
1988
|
+
let width: u32 = width.parse()?;
|
|
1989
|
+
let height: u32 = height.parse()?;
|
|
1990
|
+
let texture_width: u32 = texture_width.parse()?;
|
|
1991
|
+
let texture_height: u32 = texture_height.parse()?;
|
|
1992
|
+
if width == 0
|
|
1993
|
+
|| height == 0
|
|
1994
|
+
|| texture_width > crate::assets::MAX_TEXTURE_SIZE
|
|
1995
|
+
|| texture_height > crate::assets::MAX_TEXTURE_SIZE
|
|
1996
|
+
|| texture_width
|
|
1997
|
+
!= width
|
|
1998
|
+
.checked_next_power_of_two()
|
|
1999
|
+
.ok_or("Invalid texture width")?
|
|
2000
|
+
|| texture_height
|
|
2001
|
+
!= height
|
|
2002
|
+
.checked_next_power_of_two()
|
|
2003
|
+
.ok_or("Invalid texture height")?
|
|
2004
|
+
{
|
|
2005
|
+
return Err("Invalid converted addon texture dimensions".into());
|
|
2006
|
+
}
|
|
2007
|
+
addon_assets = true;
|
|
2008
|
+
if self.fragments {
|
|
2009
|
+
self.addon_globals.insert("addonTexturePath");
|
|
2010
|
+
}
|
|
2011
|
+
texture_module(&path, [width, height, texture_width, texture_height])
|
|
2012
|
+
}
|
|
2013
|
+
_ => return Err("Unexpected Kumi project module record".into()),
|
|
2014
|
+
};
|
|
2015
|
+
modules += 1;
|
|
2016
|
+
if link.is_some() {
|
|
2017
|
+
let (key, fingerprint) = identity.take().ok_or("Missing linked module identity")?;
|
|
2018
|
+
writeln!(
|
|
2019
|
+
out,
|
|
2020
|
+
"modules[{modules}] = cachedNativeModule({key}, {fingerprint})"
|
|
2021
|
+
)?;
|
|
2022
|
+
write!(out, "if modules[{modules}] == nil then\n modules[{modules}] = initializeNativeModule({key}, {fingerprint}, ")?;
|
|
2023
|
+
if self.fragments {
|
|
2024
|
+
if self.addon_globals.contains("addonUi") && !addon_core {
|
|
2025
|
+
return Err(
|
|
2026
|
+
"The addon UI core must be linked before a module captures UI".into(),
|
|
2027
|
+
);
|
|
2028
|
+
}
|
|
2029
|
+
let mut factory = String::from("function(modules, nativeContext)\n");
|
|
2030
|
+
for name in &self.addon_globals {
|
|
2031
|
+
writeln!(factory, "local {name} = nativeContext.{name}")?;
|
|
2032
|
+
}
|
|
2033
|
+
factory.push_str(&contents);
|
|
2034
|
+
factory.push_str("end");
|
|
2035
|
+
let mut arguments = String::new();
|
|
2036
|
+
if dependencies.is_empty() {
|
|
2037
|
+
arguments.push_str(", nil");
|
|
2038
|
+
} else {
|
|
2039
|
+
arguments.push_str(", {");
|
|
2040
|
+
for (index, dependency) in dependencies.into_iter().enumerate() {
|
|
2041
|
+
if index != 0 {
|
|
2042
|
+
arguments.push_str(", ");
|
|
2043
|
+
}
|
|
2044
|
+
write!(arguments, "modules[{dependency}]")?;
|
|
2045
|
+
}
|
|
2046
|
+
arguments.push('}');
|
|
2047
|
+
}
|
|
2048
|
+
arguments.push_str(", nativeContext)\nend\n");
|
|
2049
|
+
fragments.push([
|
|
2050
|
+
key,
|
|
2051
|
+
fingerprint,
|
|
2052
|
+
std::mem::take(&mut out),
|
|
2053
|
+
factory,
|
|
2054
|
+
arguments,
|
|
2055
|
+
]);
|
|
2056
|
+
} else {
|
|
2057
|
+
out.push_str("function()\n");
|
|
2058
|
+
out.push_str(&contents);
|
|
2059
|
+
writeln!(out, " end)\nend")?;
|
|
2060
|
+
}
|
|
2061
|
+
} else {
|
|
2062
|
+
writeln!(out, "modules[{modules}] = (function()")?;
|
|
2063
|
+
out.push_str(&contents);
|
|
2064
|
+
writeln!(out, "end)()")?;
|
|
2065
|
+
}
|
|
2066
|
+
}
|
|
2067
|
+
if self.records.next().is_some() {
|
|
2068
|
+
return Err("Trailing compiler input".into());
|
|
2069
|
+
}
|
|
2070
|
+
writeln!(out, "return modules[{modules}]")?;
|
|
2071
|
+
let mut bundle = String::from(LUA_HEADER);
|
|
2072
|
+
if addon {
|
|
2073
|
+
bundle.push_str("local addonName, addonTable = ...\n");
|
|
2074
|
+
if let Some(plugin) = plugin {
|
|
2075
|
+
bundle.push_str("local addonApp\n");
|
|
2076
|
+
if matches!(plugin, AddonPlugin::Embeddable) {
|
|
2077
|
+
bundle.push_str("if type(addonTable) == 'table' and addonTable.ID == addonName and type(addonTable.RegisterPlugin) == 'function' then addonApp = addonTable end\n");
|
|
2078
|
+
}
|
|
2079
|
+
bundle.push_str("if addonApp == nil then\n local dependency = C_AddOns.GetAddOnDependencies(addonName)\n local core = dependency and _G[dependency]\n addonApp = assert(type(core) == 'table' and core, 'Load the core addon before ' .. addonName)\nend\n");
|
|
2080
|
+
} else {
|
|
2081
|
+
bundle.push_str("local addonApp = addonTable\n");
|
|
2082
|
+
}
|
|
2083
|
+
if self.addon_args {
|
|
2084
|
+
bundle.push_str("local addonArgs = { ... }\n");
|
|
2085
|
+
}
|
|
2086
|
+
if self.addon_locale {
|
|
2087
|
+
bundle.push_str(
|
|
2088
|
+
"local addonLocale = type(addonApp) == 'table' and addonApp.L or nil\n",
|
|
2089
|
+
);
|
|
2090
|
+
}
|
|
2091
|
+
if addon_core && !self.fragments {
|
|
2092
|
+
bundle.push_str("local addonUi\n");
|
|
2093
|
+
}
|
|
2094
|
+
if addon_assets {
|
|
2095
|
+
bundle.push_str("local function addonTexturePath(path)\n return 'Interface\\\\AddOns\\\\' .. (addonApp.ID or addonName) .. '\\\\' .. path\nend\n");
|
|
2096
|
+
}
|
|
2097
|
+
}
|
|
2098
|
+
if let Some(stamp) = link {
|
|
2099
|
+
writeln!(bundle, "local nativeBuild = {}", quote(stamp))?;
|
|
2100
|
+
bundle.push_str(include_str!("addon-link.lua"));
|
|
2101
|
+
}
|
|
2102
|
+
self.write_helpers(&mut bundle, link.is_some(), false)?;
|
|
2103
|
+
bundle.push_str("local modules = {}\n");
|
|
2104
|
+
if self.fragments {
|
|
2105
|
+
bundle.push_str("local nativeDefinitions = nativeRegistry.definitions\nif nativeDefinitions == nil then error('Missing native Kumi package definitions for ' .. tostring(addonName), 0) end\nlocal nativeContext = { addonName = addonName, addonTable = addonTable, addonApp = addonApp");
|
|
2106
|
+
for (enabled, name) in [
|
|
2107
|
+
(self.addon_args, "addonArgs"),
|
|
2108
|
+
(self.addon_locale, "addonLocale"),
|
|
2109
|
+
(addon_assets, "addonTexturePath"),
|
|
2110
|
+
] {
|
|
2111
|
+
if enabled {
|
|
2112
|
+
write!(bundle, ", {name} = {name}")?;
|
|
2113
|
+
}
|
|
2114
|
+
}
|
|
2115
|
+
bundle.push_str(" }\n");
|
|
2116
|
+
let stamp = link.ok_or("A package fragment requires a linked addon")?;
|
|
2117
|
+
let mut prelude = String::from(LUA_HEADER);
|
|
2118
|
+
prelude.push_str("local addonName, addonApp = ...\n");
|
|
2119
|
+
writeln!(prelude, "local nativeBuild = {}", quote(stamp))?;
|
|
2120
|
+
prelude.push_str(include_str!("addon-link.lua"));
|
|
2121
|
+
prelude.push_str("if nativeRegistry.definitions ~= nil then error('Duplicate native Kumi package definitions', 0) end\n");
|
|
2122
|
+
self.write_helpers(&mut prelude, true, true)?;
|
|
2123
|
+
prelude.push_str("local definitions = {}\n");
|
|
2124
|
+
let mut packet = b"kumi-addon-fragments-1\n".to_vec();
|
|
2125
|
+
write_field(&mut packet, &bundle)?;
|
|
2126
|
+
write_field(&mut packet, &prelude)?;
|
|
2127
|
+
write_field(&mut packet, &out)?;
|
|
2128
|
+
for module in fragments {
|
|
2129
|
+
for field in module {
|
|
2130
|
+
write_field(&mut packet, &field)?;
|
|
2131
|
+
}
|
|
2132
|
+
}
|
|
2133
|
+
return Ok(packet);
|
|
2134
|
+
}
|
|
2135
|
+
bundle.push_str(&out);
|
|
2136
|
+
Ok(bundle.into_bytes())
|
|
2137
|
+
}
|
|
2138
|
+
|
|
2139
|
+
fn write_helpers(&self, bundle: &mut String, linked: bool, all: bool) -> Compile<()> {
|
|
2140
|
+
if all || self.collections {
|
|
2141
|
+
if linked {
|
|
2142
|
+
bundle.push_str(
|
|
2143
|
+
"local collections = nativeRegistry.collections\nif collections == nil then\n",
|
|
2144
|
+
);
|
|
2145
|
+
} else {
|
|
2146
|
+
bundle.push_str("local collections\n");
|
|
2147
|
+
}
|
|
2148
|
+
bundle.push_str(include_str!("collections.lua"));
|
|
2149
|
+
if linked {
|
|
2150
|
+
bundle.push_str("nativeRegistry.collections = collections\nend\n");
|
|
2151
|
+
}
|
|
2152
|
+
}
|
|
2153
|
+
if all || self.arrays {
|
|
2154
|
+
bundle.push_str(include_str!("arrays.lua"));
|
|
2155
|
+
}
|
|
2156
|
+
if all || self.classes || linked && self.types {
|
|
2157
|
+
let registry = if linked {
|
|
2158
|
+
"nativeRegistry.classPrototypes"
|
|
2159
|
+
} else {
|
|
2160
|
+
"setmetatable({}, { __mode = 'kv' })"
|
|
2161
|
+
};
|
|
2162
|
+
writeln!(bundle, "local classPrototypes = {registry}")?;
|
|
2163
|
+
}
|
|
2164
|
+
if all || self.classes {
|
|
2165
|
+
bundle.push_str(include_str!("classes.lua"));
|
|
2166
|
+
}
|
|
2167
|
+
if all || self.types {
|
|
2168
|
+
bundle.push_str("local function valueType(value)\n local kind = type(value)\n if kind == 'nil' then return 'undefined' end\n if kind == 'table' then\n");
|
|
2169
|
+
if all || self.classes || linked {
|
|
2170
|
+
bundle.push_str(" if classPrototypes[value] then return 'function' end\n");
|
|
2171
|
+
}
|
|
2172
|
+
bundle.push_str(" return 'object'\n end\n return kind\nend\n");
|
|
2173
|
+
}
|
|
2174
|
+
if all || self.numbers {
|
|
2175
|
+
bundle.push_str(include_str!("numbers.lua"));
|
|
2176
|
+
}
|
|
2177
|
+
if all || self.wow_reads {
|
|
2178
|
+
bundle.push_str(include_str!("wow-reads.lua"));
|
|
2179
|
+
}
|
|
2180
|
+
if all || self.store_views {
|
|
2181
|
+
bundle.push_str(include_str!("setup-store.lua"));
|
|
2182
|
+
}
|
|
2183
|
+
if all || self.strings {
|
|
2184
|
+
bundle.push_str(include_str!("strings.lua"));
|
|
2185
|
+
}
|
|
2186
|
+
if all || self.tables {
|
|
2187
|
+
bundle.push_str(include_str!("records.lua"));
|
|
2188
|
+
}
|
|
2189
|
+
if all || self.errors {
|
|
2190
|
+
bundle.push_str(include_str!("errors.lua"));
|
|
2191
|
+
}
|
|
2192
|
+
Ok(())
|
|
2193
|
+
}
|
|
2194
|
+
}
|
|
2195
|
+
|
|
2196
|
+
const LUA_HEADER: &str = "-- Generated by kumi-pack compile. Lua 5.1.\n";
|
|
2197
|
+
|
|
2198
|
+
fn write_field(packet: &mut Vec<u8>, field: &str) -> Compile<()> {
|
|
2199
|
+
packet.extend_from_slice(&u32::try_from(field.len())?.to_le_bytes());
|
|
2200
|
+
packet.extend_from_slice(field.as_bytes());
|
|
2201
|
+
Ok(())
|
|
2202
|
+
}
|
|
2203
|
+
|
|
2204
|
+
fn texture_module(path: &str, [width, height, texture_width, texture_height]: [u32; 4]) -> String {
|
|
2205
|
+
let u_max = f64::from(width) / f64::from(texture_width);
|
|
2206
|
+
let v_max = f64::from(height) / f64::from(texture_height);
|
|
2207
|
+
format!(
|
|
2208
|
+
"return {{ default = {{ path = addonTexturePath({}), width = {width}, height = {height}, uMax = {u_max}, vMax = {v_max} }} }}\n",
|
|
2209
|
+
quote(&path.replace('/', "\\")),
|
|
2210
|
+
)
|
|
2211
|
+
}
|
|
2212
|
+
|
|
2213
|
+
fn update_number(preparation: &str, target: &str, operator: &str, result: &str) -> String {
|
|
2214
|
+
format!("(function() {preparation}local old = {target}; local new = old {operator} 1; {target} = new; return {result} end)()")
|
|
2215
|
+
}
|
|
2216
|
+
|
|
2217
|
+
pub enum AddonPlugin {
|
|
2218
|
+
External,
|
|
2219
|
+
Embeddable,
|
|
2220
|
+
}
|
|
2221
|
+
|
|
2222
|
+
pub struct ProjectCompile<'a> {
|
|
2223
|
+
pub workspace: &'a Path,
|
|
2224
|
+
pub sdk: &'a Path,
|
|
2225
|
+
pub config: Option<&'a Path>,
|
|
2226
|
+
pub source: &'a Path,
|
|
2227
|
+
pub output: &'a Path,
|
|
2228
|
+
pub check: bool,
|
|
2229
|
+
pub addon: bool,
|
|
2230
|
+
/// Compile an SDK test fixture: addon semantics plus its declared mock globals.
|
|
2231
|
+
pub test: bool,
|
|
2232
|
+
pub link: Option<&'a str>,
|
|
2233
|
+
pub assets: Option<&'a Path>,
|
|
2234
|
+
pub plugin: Option<AddonPlugin>,
|
|
2235
|
+
pub fragments: bool,
|
|
2236
|
+
}
|
|
2237
|
+
|
|
2238
|
+
pub fn compile(project: &Path, request: ProjectCompile<'_>) -> Compile<()> {
|
|
2239
|
+
let ProjectCompile {
|
|
2240
|
+
workspace,
|
|
2241
|
+
sdk,
|
|
2242
|
+
config,
|
|
2243
|
+
source,
|
|
2244
|
+
output,
|
|
2245
|
+
check,
|
|
2246
|
+
addon,
|
|
2247
|
+
test,
|
|
2248
|
+
link,
|
|
2249
|
+
assets,
|
|
2250
|
+
plugin,
|
|
2251
|
+
fragments,
|
|
2252
|
+
} = request;
|
|
2253
|
+
let mut command = Command::new("node");
|
|
2254
|
+
command
|
|
2255
|
+
.current_dir(project)
|
|
2256
|
+
.args(["--v8-pool-size=1", "--input-type=module", "-"])
|
|
2257
|
+
.arg(source)
|
|
2258
|
+
.arg(workspace)
|
|
2259
|
+
.arg(sdk)
|
|
2260
|
+
.arg(config.unwrap_or_else(|| Path::new("")))
|
|
2261
|
+
.arg(match (addon, test) {
|
|
2262
|
+
(_, true) => "test",
|
|
2263
|
+
(true, false) => "addon",
|
|
2264
|
+
(false, false) => "standalone",
|
|
2265
|
+
})
|
|
2266
|
+
.arg(link.unwrap_or(""))
|
|
2267
|
+
.arg(assets.unwrap_or_else(|| Path::new("")))
|
|
2268
|
+
.arg(if assets.is_some() {
|
|
2269
|
+
std::env::current_exe()?
|
|
2270
|
+
} else {
|
|
2271
|
+
PathBuf::new()
|
|
2272
|
+
})
|
|
2273
|
+
.arg(if check { "check" } else { "" })
|
|
2274
|
+
.arg(if fragments { "fragments" } else { "" })
|
|
2275
|
+
.env("UV_THREADPOOL_SIZE", "1")
|
|
2276
|
+
.stdin(Stdio::piped())
|
|
2277
|
+
.stdout(Stdio::piped())
|
|
2278
|
+
.stderr(Stdio::piped());
|
|
2279
|
+
let mut child = command.spawn()?;
|
|
2280
|
+
let sent = child
|
|
2281
|
+
.stdin
|
|
2282
|
+
.take()
|
|
2283
|
+
.ok_or("Missing TypeScript frontend stdin")?
|
|
2284
|
+
.write_all(include_bytes!("project-front.mjs"));
|
|
2285
|
+
let result = child.wait_with_output()?;
|
|
2286
|
+
if !result.status.success() {
|
|
2287
|
+
return Err(String::from_utf8_lossy(&result.stderr)
|
|
2288
|
+
.trim()
|
|
2289
|
+
.to_owned()
|
|
2290
|
+
.into());
|
|
2291
|
+
}
|
|
2292
|
+
sent?;
|
|
2293
|
+
let frontend = String::from_utf8(result.stdout)?;
|
|
2294
|
+
let lua = Emitter {
|
|
2295
|
+
records: frontend.lines(),
|
|
2296
|
+
collections: false,
|
|
2297
|
+
arrays: false,
|
|
2298
|
+
classes: false,
|
|
2299
|
+
types: false,
|
|
2300
|
+
numbers: false,
|
|
2301
|
+
wow_reads: false,
|
|
2302
|
+
store_views: false,
|
|
2303
|
+
strings: false,
|
|
2304
|
+
tables: false,
|
|
2305
|
+
errors: false,
|
|
2306
|
+
addon_args: false,
|
|
2307
|
+
addon_locale: false,
|
|
2308
|
+
fragments,
|
|
2309
|
+
addon_globals: BTreeSet::new(),
|
|
2310
|
+
loop_wrappers: Vec::new(),
|
|
2311
|
+
}
|
|
2312
|
+
.project(workspace, addon, link, plugin)?;
|
|
2313
|
+
if fs::read(output).ok().as_deref() != Some(&lua) {
|
|
2314
|
+
if check {
|
|
2315
|
+
return Err(format!("{} is stale; run kumi-pack compile", output.display()).into());
|
|
2316
|
+
}
|
|
2317
|
+
if let Some(parent) = output.parent() {
|
|
2318
|
+
fs::create_dir_all(parent)?;
|
|
2319
|
+
}
|
|
2320
|
+
let temporary = output.with_extension(format!("lua.{}.tmp", std::process::id()));
|
|
2321
|
+
let published = fs::write(&temporary, lua).and_then(|()| fs::rename(&temporary, output));
|
|
2322
|
+
if published.is_err() {
|
|
2323
|
+
let _ = fs::remove_file(&temporary);
|
|
2324
|
+
}
|
|
2325
|
+
published?;
|
|
2326
|
+
}
|
|
2327
|
+
println!("{} -> {}", source.display(), output.display());
|
|
2328
|
+
Ok(())
|
|
2329
|
+
}
|
|
2330
|
+
|
|
2331
|
+
pub fn emit_native(input: &Path, output: &Path) -> Compile<()> {
|
|
2332
|
+
let ir = crate::native_ir::read_signal(input)?;
|
|
2333
|
+
let lua = crate::native_signal::emit(ir);
|
|
2334
|
+
fs::write(output, lua)?;
|
|
2335
|
+
Ok(())
|
|
2336
|
+
}
|