You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+44-33Lines changed: 44 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,57 +24,64 @@
24
24
The main goal of this fork is to bring **Kotlin-style zero-overhead `@inline` functions** to TypeScriptToLua. Unlike experimental community plugins, this feature is baked directly into the compiler engine, supporting deep cross-file analysis and complex control flows.
25
25
26
26
### 1. Cross-File Macro Inlining
27
-
* Functions marked with JSDoc `/** @inline */` are completely unwrapped into the calling expression/statement.
28
-
* Works seamlessly across different files and modules via strict TS Symbol aliasing resolution.
27
+
28
+
- Functions marked with JSDoc `/** @inline */` are completely unwrapped into the calling expression/statement.
29
+
- Works seamlessly across different files and modules via strict TS Symbol aliasing resolution.
29
30
30
31
### 2. High-Order Functions & Lambda Optimization
31
-
* Fully supports inlining functions that accept other functions (lambdas/callbacks) as arguments (e.g., custom `filter`, `map`, `forEach`).
32
-
* Arrow functions and inline expressions are embedded into loops and conditions without creating closures or allocating anonymous tables in Lua.
32
+
33
+
- Fully supports inlining functions that accept other functions (lambdas/callbacks) as arguments (e.g., custom `filter`, `map`, `forEach`).
34
+
- Arrow functions and inline expressions are embedded into loops and conditions without creating closures or allocating anonymous tables in Lua.
33
35
34
36
### 3. Adaptive Control Flow (Target-Specific Compilation)
35
-
***Lua 5.2+, JIT, Luau**: Translates deep `return` statements in complex multi-return lambdas into high-performance native `goto` jumps with macro hygiene (guaranteed unique labels).
36
-
***Lua 5.1 / Universal**: Automatically downgrades complex control flow into efficient state-machine blocks using temporary execution flags (`____done_...`), ensuring 100% runtime compatibility.
37
+
38
+
-**Lua 5.2+, JIT, Luau**: Translates deep `return` statements in complex multi-return lambdas into high-performance native `goto` jumps with macro hygiene (guaranteed unique labels).
39
+
-**Lua 5.1 / Universal**: Automatically downgrades complex control flow into efficient state-machine blocks using temporary execution flags (`____done_...`), ensuring 100% runtime compatibility.
37
40
38
41
### 4. Zero-Overhead Fast Path & `$multi` Integration
39
-
* Triivial functions (single trailing return expressions) skip block overhead and flatten directly into equations or native multiple assignments.
40
-
* Perfect cooperation with `LuaMultiReturn` / `$multi` compiler macro.
42
+
43
+
- Triivial functions (single trailing return expressions) skip block overhead and flatten directly into equations or native multiple assignments.
44
+
- Perfect cooperation with `LuaMultiReturn` / `$multi` compiler macro.
41
45
42
46
### 5. New CLI / tsconfig.json compiler options
43
-
*`inlineGenerateComment`: (boolean) Generates `-- Start inline [name]` annotations directly in the emitted Lua files to simplify debugging.
44
-
*`inlineRemoveDefault`: (boolean) Globally toggles whether to strip the original inline function declarations from the compiled output. Can be overridden per function via `/** @inline toggle */`.
47
+
48
+
-`inlineGenerateComment`: (boolean) Generates `-- Start inline [name]` annotations directly in the emitted Lua files to simplify debugging.
49
+
-`inlineRemoveDefault`: (boolean) Globally toggles whether to strip the original inline function declarations from the compiled output. Can be overridden per function via `/** @inline toggle */`.
45
50
46
51
---
47
52
48
53
## 🛠️ Quick Example
49
54
50
55
### TypeScript Source Code
56
+
51
57
```typescript
52
58
/**@inline*/
53
59
function filter<T>(arr:T[], pred: (x:T) =>boolean):T[] {
54
-
const result:T[] = [];
55
-
for (const item ofarr) {
56
-
if (pred(item)) result.push(item);
57
-
}
58
-
returnresult;
60
+
const result:T[] = [];
61
+
for (const item ofarr) {
62
+
if (pred(item)) result.push(item);
63
+
}
64
+
returnresult;
59
65
}
60
66
61
67
exportfunction test() {
62
-
const data = [1, 2, 3, 4, 5, 6];
63
-
64
-
// Inlining simple conditions
65
-
const evens =filter(data, (n)=>n%2==0);
66
-
67
-
// Inlining heavy block expressions with early returns
68
-
const withBlock =filter(data, (n)=> {
69
-
if (n==3) returntrue;
70
-
returnn>4;
71
-
});
72
-
73
-
return [evens, withBlock];
68
+
const data = [1, 2, 3, 4, 5, 6];
69
+
70
+
// Inlining simple conditions
71
+
const evens =filter(data, n=>n%2==0);
72
+
73
+
// Inlining heavy block expressions with early returns
@@ -110,25 +117,29 @@ function ____exports.test(self)
110
117
return {evens, withBlock}
111
118
end
112
119
```
120
+
113
121
> 💡 **Note on Variable Naming:** To make this example easy to read, some variable names have been manually cleaned up. In actual generated Lua code, the compiler enforces strict **macro hygiene** by automatically renaming local variables and arguments inside inline blocks (e.g., transforming `item` into `____item_inline_1`). This ensures complete scope isolation and completely prevents name collision bugs with the surrounding code!
122
+
114
123
---
115
124
116
125
## ⚠️ Known Limitations & AI Disclaimer
117
126
118
127
This inliner was developed as a powerful custom extension of the compiler. While it passes all core integration tests, please keep the following trade-offs and architectural quirks in mind:
119
128
120
129
### 1. Short-circuit Evaluation Side-Effects
121
-
***The Issue:** If you use inline functions inside logical conditions (e.g., `if (isValid() && fetchProps())`), the compiler hoists evaluation do-blocks *before* executing the `if` statement itself.
122
-
***The Result:** Both inline functions will **always** be executed, even if the first one returns `false`. This can cause unexpected side-effects. It is highly recommended to store inline function results in local variables manually before using them in complex conditional logic.
130
+
131
+
-**The Issue:** If you use inline functions inside logical conditions (e.g., `if (isValid() && fetchProps())`), the compiler hoists evaluation do-blocks _before_ executing the `if` statement itself.
132
+
-**The Result:** Both inline functions will **always** be executed, even if the first one returns `false`. This can cause unexpected side-effects. It is highly recommended to store inline function results in local variables manually before using them in complex conditional logic.
123
133
124
134
### 2. Emitted Code Debugging (Source Maps)
125
-
* Deeply nested inlining of complex statements—especially with state-machine generation for older Lua 5.1 targets—can complicate accurate runtime line debugging.
126
-
* To make code tracking easier during development, always enable the `--inlineGenerateComment true` flag.
135
+
136
+
- Deeply nested inlining of complex statements—especially with state-machine generation for older Lua 5.1 targets—can complicate accurate runtime line debugging.
137
+
- To make code tracking easier during development, always enable the `--inlineGenerateComment true` flag.
127
138
128
139
### 3. 🤖 AI-Assisted Development Notice
129
-
* About **70% of this compiler fork's logic was built in collaboration with AI (Qwen)** directly inside the IDE.
130
-
* Because of this AI-driven approach, some edge cases might still be unhandled, and deep internal refactoring can be highly complex. However, it completely fulfills its goal for practical, micro-optimization tasks! "It works on my machine" ™️ — use it with care and feel free to submit PRs for any bugs you find.
131
140
141
+
- About **70% of this compiler fork's logic was built in collaboration with AI (Qwen)** directly inside the IDE.
142
+
- Because of this AI-driven approach, some edge cases might still be unhandled, and deep internal refactoring can be highly complex. However, it completely fulfills its goal for practical, micro-optimization tasks! "It works on my machine" ™️ — use it with care and feel free to submit PRs for any bugs you find.
0 commit comments