@tilua/compiler 1.0.2 → 2.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
@@ -65,10 +65,14 @@ JavaScript module the build loads.
65
65
  | `function f(n = 1, { x })` | `function f(n, arg) if n == nil then n = 1 end local x = arg.x ...` |
66
66
  | `const` / `let` | `local` |
67
67
  | `class C { … }` | one table for the class, one per instance — see below |
68
- | `new C(x)` | `C.new(x)` |
69
- | `[...]` | `{...}` — the varargs as an array |
68
+ | `return [a, b]` | `return { a, b }` — several results are an array |
69
+ | `for (const item in xs)` | `for _, item in xs do … end` — an array or a table walked for its values |
70
+ | `for (const item in step)` | `for item in step do … end` — an iterator function |
71
+ | `for (const [k, v] in pairs(t))` | `for item in unpack(pairs(t), 1, 3) do local k, v = item[1], item[2] … end` — an iteration is `[step, state, first]` |
72
+ | `pcall(f)`, and other globals Luau answers several values from | the type library's own function, answering one — see below |
70
73
  | `function f(a, ...rest)` | `function f(a, ...) local rest = {...}` |
71
- | `f(a, ...xs)` / `return ...xs` | `table.unpack(xs)` in the last position |
74
+ | `scriptArgs` | the chunk's own `...`, as `local scriptArgs = { ... }` |
75
+ | `f(a, ...xs)` | `table.unpack(xs)` in the last position |
72
76
  | `f(...xs, a)` | the list built first, then `table.unpack` of it |
73
77
  | `x as T`, `x satisfies T`, types, `declare` | removed |
74
78
  | `names:filter(f)` | whatever the type library that declared `filter` says — see below |
@@ -113,8 +117,16 @@ tilua_accessors(Dog)
113
117
 
114
118
  `__init` is what `super(...)` calls: it runs the constructor on an instance
115
119
  that already exists, so a derived class builds one table, not one per level.
116
- `new` is a real function on the class table — `new Dog(x)` and `Dog.new(x)`
117
- are the same call.
120
+ `new` is a real function on the class table, and `Dog.new(x)` is how an
121
+ instance is built — the way Luau builds one. An `abstract class` has no `new`,
122
+ and an `abstract` method no function: both exist only to be checked.
123
+
124
+ A method named for a metamethod — `__add`, `__tostring`, `__call`, … — is an
125
+ ordinary function on the class table, and since the class table is its
126
+ instances' metatable, it works as one. Luau reads metamethods off the
127
+ metatable with `rawget`, never through its `__index`, so a class would not
128
+ inherit its base's: `tilua_class` copies them into each class that extends
129
+ another, and one the class writes itself replaces the copy.
118
130
 
119
131
  Two links come with every class, and they cost an instance nothing because
120
132
  they live on the class table: `ClassObject`, which an instance reads through
@@ -168,6 +180,33 @@ needed it; the receiver becomes the call's first argument unless
168
180
  `passReceiver: false`. The last library loaded is asked first, and `undefined`
169
181
  leaves an ordinary Luau method call.
170
182
 
183
+ Hooks also get the call site (`at`) and what the compiler knew about each
184
+ argument (`arguments`: type, the code it was written as, and what a name was
185
+ declared as). An answer can `prepend` Luau expressions ahead of the written
186
+ arguments. `globalCall` and `globalValue` answer for a global, such as
187
+ `print(x)` or a bare `print`. A runtime can read `__LINES__` to reach the
188
+ bundle's line map. See the `@tilua/parser` README for the details.
189
+ `console:log` is not built into the compiler: `@tilua-types/lua` implements it
190
+ with these hooks.
191
+
192
+ ## Errors at runtime
193
+
194
+ A bundle is one Luau file, so a position Luau reports names a line of that
195
+ file. The entry therefore runs under `xpcall`. If an error reaches the top,
196
+ its message is rewritten from bundle lines to the places in the project that
197
+ wrote them, a traceback of those places is added, and the error is raised
198
+ again:
199
+
200
+ ```
201
+ src/util.tilua:3: attempt to index nil with 'value'
202
+ at src/util.tilua:3 (boom)
203
+ at src/main.tilua:3 (load)
204
+ ```
205
+
206
+ A message that is not a string is raised unchanged. A position that is already
207
+ in the project's files, such as one from `error` in `@tilua-types/lua`, is
208
+ left as it is.
209
+
171
210
  ## Modules
172
211
 
173
212
  Roblox's `require` takes an Instance, so the bundle has its own. Each module