@tilua/compiler 4.0.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -11,7 +11,8 @@ npx tilua src/main.tilua --out build/main.luau
11
11
  ```
12
12
 
13
13
  ```
14
- tilua <entry> [--out <file>] [--config <tilua.config.json>] [--noCheck] [--target luau|lua51]
14
+ tilua <entry> [--out <file>] [--output single|chunks|modules] [--config <tilua.config.json>]
15
+ [--noCheck] [--target luau|lua51]
15
16
  ```
16
17
 
17
18
  Every module the entry imports is bundled in. Problems are reported as
@@ -24,6 +25,9 @@ Without `--out`, the entry's own path with `.luau` is written. Without
24
25
  [`@tilua/parser`](https://www.npmjs.com/package/@tilua/parser) README for what
25
26
  goes in it.
26
27
 
28
+ `--output` (or `"output"` in the config) makes the bundle more than one file —
29
+ see [Several files](#several-files).
30
+
27
31
  ## API
28
32
 
29
33
  ```ts
@@ -35,6 +39,7 @@ const result = await bundle({
35
39
  config: { types: ["roblox"], paths: { "@shared/*": ["src/shared/*"] } },
36
40
  })
37
41
  result.code // the Luau bundle, unless a module failed
42
+ result.files // [{ path, code, modules }] — one, unless `output` says otherwise
38
43
  result.diagnostics // { file, line, column, message, category: "syntax" | "module" | "type" | "config" }
39
44
 
40
45
  // One file, no imports. The config is optional here too, and brings the same
@@ -75,9 +80,32 @@ JavaScript module the build loads.
75
80
  | `scriptArgs` | the chunk's own `...`, as `local scriptArgs = { ... }` |
76
81
  | `f(a, ...xs)` | `table.unpack(xs)` in the last position |
77
82
  | `f(...xs, a)` | the list built first, then `table.unpack` of it |
78
- | `x as T`, `x satisfies T`, types, `declare` | removed |
83
+ | `import("./shop")` | `G.import("src/shop")` — the bundle's `require`, wrapped |
84
+ | `a ?? b` | `if a ~= nil then a else b` for a name; otherwise a function that reads `a` once |
85
+ | `n++` / `n--` | `n += 1` / `n -= 1` |
86
+ | `"key" in t` | `(t["key"] ~= nil)` |
87
+ | `v instanceof Dog` | `tilua_instanceof(v, Dog)` — walks `v.ClassObject`, then each `.ParentClass` |
88
+ | `{ a: 1, get b() { … }, set b(v) { … } }` | `tilua_accessors({ a = 1 }, { b = … }, { b = … })` — a metatable runs the accessors |
89
+ | `x as T`, `x satisfies T`, types, `interface`, `export type { … }`, `declare` | removed |
79
90
  | `names:filter(f)` | whatever the type library that declared `filter` says — see below |
80
91
 
92
+ ## Lua 5.1
93
+
94
+ `"target": "lua51"` (or `--target lua51`) writes what stock Lua 5.1 runs. On
95
+ top of the table above:
96
+
97
+ | Luau | Lua 5.1 |
98
+ |---|---|
99
+ | `a += b`, `a // b`, `continue`, `if c then a else b`, `1_000`, `0b101` | assignments, `math.floor`, a `break` out of a one-pass `repeat`, `and`/`or` or a call, plain numbers |
100
+ | `for _, item in xs do` | `ipairs(xs)` where `xs` is an array by its type; otherwise `tilua_each(xs)`, which walks a table as Luau would — its `__iter` if it has one, `next` if not |
101
+ | `if a == nil then nil else a.b` (`a?.b`) | the function form, which keeps a `false` |
102
+ | `table.move`, `table.unpack` | `tilua_move`, `unpack` |
103
+ | `@native` and other attributes | dropped |
104
+
105
+ An object's `freeze` does nothing there and `isFrozen` answers `false`: Lua 5.1
106
+ cannot freeze a table. `npm test` with `LUA51` naming a Lua 5.1 interpreter
107
+ builds a program for both targets and checks they print the same.
108
+
81
109
  ## `typeof`, arrays and objects
82
110
 
83
111
  Luau cannot tell an array from an object: both are tables, and an empty one
@@ -169,10 +197,12 @@ the fast one.
169
197
 
170
198
  ## What a type library lowers
171
199
 
172
- The compiler lowers tilua. What a *library* gives a value, the library also
173
- says how to run: `names:filter(f)` is a call to a function because
174
- [`@tilua-types/lua`](https://www.npmjs.com/package/@tilua-types/lua) declares
175
- the method and ships the Luau behind it.
200
+ The compiler lowers tilua, and Lua's standard library, which every file has:
201
+ `pcall` answers one value, `console:log` prints where it was called, `error`
202
+ names the project's file (runtime/console.luau, runtime/result.luau). What a
203
+ *library* gives a value, the library also says how to run: `names:first()` is
204
+ a call to a function because the library that declared `first` ships the Luau
205
+ behind it.
176
206
 
177
207
  A library names a module in its package.json (`"tilua": { "lowering":
178
208
  "lowering.mjs" }`) whose default export answers for a call. The contract is
@@ -207,8 +237,8 @@ declared as). An answer can `prepend` Luau expressions ahead of the written
207
237
  arguments. `globalCall` and `globalValue` answer for a global, such as
208
238
  `print(x)` or a bare `print`. A runtime can read `__LINES__` to reach the
209
239
  bundle's line map, and wraps a new array it answers with in `__ARRAY__(...)`. See the `@tilua/parser` README for the details.
210
- `console:log` is not built into the compiler: `@tilua-types/lua` implements it
211
- with these hooks.
240
+ The standard library's own lowering (src/standard.ts) is written with these
241
+ hooks too; `"noLib": true` leaves it out with the library.
212
242
 
213
243
  ## Errors at runtime
214
244
 
@@ -225,9 +255,13 @@ src/util.tilua:3: attempt to index nil with 'value'
225
255
  ```
226
256
 
227
257
  A message that is not a string is raised unchanged. A position that is already
228
- in the project's files, such as one from `error` in `@tilua-types/lua`, is
258
+ in the project's files, such as one from the standard library's `error`, is
229
259
  left as it is.
230
260
 
261
+ With several files each has its own line map, and a position is read by the
262
+ map of the file it names — matched by the end of that file's path, since the
263
+ Lua running it may call it by a full path, a relative one, or an Instance's.
264
+
231
265
  ## Modules
232
266
 
233
267
  Roblox's `require` takes an Instance, so the bundle has its own. Each module
@@ -281,6 +315,61 @@ Modules follow ES module rules, including when they import each other:
281
315
  - **Types only.** `import type` is erased whatever it names, and so is an
282
316
  import used only as a type. A module reached only that way is left out of
283
317
  the bundle and never runs.
318
+ - **`import()`.** `import("./shop")` is `G.import("src/shop")`, and `G.import`
319
+ is a wrapper around `G.require`: it answers the module's exports table — the
320
+ value `import * as Shop` names — where the code runs, loading the module if
321
+ nothing has yet. Nothing is awaited: Lua has no promises, and a module is
322
+ loaded by calling it. The path is a string literal; the module it names is
323
+ part of the bundle like any other.
324
+
325
+ ## Several files
326
+
327
+ `"output"` in `tilua.config.json` (or `--output`, or `bundle({ output })`)
328
+ says how many files the project becomes:
329
+
330
+ | `output` | files |
331
+ |---|---|
332
+ | `"single"` (default) | one, holding every module |
333
+ | `"chunks"` | the entry's, holding what it reaches through `import` statements; one for each `import()` that leads somewhere else, holding what only it reaches; and one for what several of those share |
334
+ | `"modules"` | one per module, in the project's own tree |
335
+
336
+ ```
337
+ tilua src/main.tilua --out build/main.luau --output chunks
338
+ # build/main.luau, build/main_src_shop.luau, ...
339
+
340
+ tilua src/main.tilua --out build --output modules
341
+ # build/src/main.luau, build/src/shop.luau, ...
342
+ ```
343
+
344
+ With `"modules"`, `--out` is the folder the tree goes in — by default the
345
+ project's, so each `.luau` lands beside its `.tilua`. From the API,
346
+ `result.files` holds every file with the path it has to be written at, the
347
+ entry's first; `outFile` names the entry's file, which the others are named
348
+ after.
349
+
350
+ The module table is the same one, filled by more than one file. The entry's
351
+ file has the runtime; every other file is a function that adds its modules:
352
+
353
+ ```lua
354
+ return function(G)
355
+ G.modules["src/shop"] = { load = function(exports) ... end }
356
+ end
357
+ ```
358
+
359
+ `G.files` says which file each of those modules is in, and `require` loads
360
+ that file, once, the first time one of its modules is asked for — by an
361
+ `import()`, or by an `import` statement in a module that was itself loaded
362
+ late. So a file costs nothing until then.
363
+
364
+ How a file is loaded is the one thing the two targets do not share:
365
+
366
+ | `target` | loads a file with |
367
+ |---|---|
368
+ | `"luau"` | `require("./main_src_shop")(G)` — Luau's `require` by path, read from the entry's own file. In Roblox each file is a ModuleScript beside the entry's |
369
+ | `"lua51"` | `loadfile(folder .. "main_src_shop.luau")()(G)`, where `folder` is the entry's own, read off `debug.getinfo` |
370
+
371
+ Either way the files have to stay where they were written, relative to the
372
+ entry's.
284
373
 
285
374
  ## Development
286
375