@tilua-types/lua
v5.0.1
Published
The Lua 5.1 standard library as tilua types
Readme
@tilua-types/lua
The Lua 5.1 standard library as tilua
types: the base library (print, pairs, pcall, string, math, table,
os, coroutine, …) — all but type(): what a value is, is asked with the
language's own typeof v.
Only what Lua itself defines is here. buffer, bit32, utf8,
table.create, string.split and the rest of Luau's additions are in
@tilua-types/roblox — Luau is Roblox's language — which depends
on this package and extends it: a name declared in both keeps what this one
gave it.
Install this one directly only for plain Lua 5.1. For Luau or Roblox,
install @tilua-types/roblox; it brings this along.
npm i -D @tilua-types/lua{ "types": ["lua"], "paths": {}, "sourceMap": null }Several results
tilua has no packs: a function returns one value, and where Lua returns several, the types here say what tilua sees instead.
pcall,xpcall,coroutine.resumeanswer a result object:const { success, data, error } = pcall(load, "config") if (success) { use(data) } else { warn(error) }datais what the function returned.Arrays where Lua answers several:
string.findis[number, number, ...string[]] | nil(the positions, then the captures),math.modfis[number, number],nextis[key, value] | nil. Take them apart withconst [a, b] = ....One value where JavaScript would answer one:
string.gsubis the new string (Lua's count of replacements is left behind),string.byte(s, i)one code —string.byte(s, i, j)an array of them — andstring.matchthe captures as an array, ornil.Loops walk arrays like everything else:
pairs(t)andipairs(t)answer an iteration whose steps are one[key, value]each, ands:gmatch(p)an iterator function answering one match's captures.for (const [key, score] in pairs(scores)) { print(key, score) } for (const [word] in text:gmatch("%a+")) { print(word) }
None of these is Lua's own function. This package puts its own in place of
each global (runtime/result.luau, through lowering.mjs), for a call and for
the global read as a value alike, so what runs answers what the type says.
Methods of strings, arrays and tables
text:trim(), names:filter(f) and point:keys() are not this package's.
Strings, arrays and tables are tilua's own, so their metatables are declared
by the language (in @tilua/parser) and run by the compiler, with or without
a type library. A library adds to one with declare metatable of the same
target; @tilua-types/roblox gives a string Luau's split that way.
The console and error
console:log, console:warn, console:error and error name the place that
called them, the way a browser console does. The compiler writes the call site
into each call. The place is the file relative to the project, without
.tilua, and its line:
print("결과물") -- 결과물 (untouched)
console:warn("careful") -- [Modules/ABLModule:2]: careful
const double = (x: number) => x * 2
console:log({ a: 1 }, double) -- [Modules/ABLModule:4]: {
-- a = 1
-- } (x: number) => number (x: number) => x * 2
error("negative", 2) -- raised as "Modules/ABLModule.tilua:<caller's line>: negative"print(x)andwarn(x)are left as written. Useconsole:logandconsole:warnfor output that says where it came from.console:logexpands a table instead of printing its address. A function prints as its inferred type and the code it was written as. It is handled byruntime/console.luau, as areconsole:warnandconsole:error.console:errorreports with a traceback in the project's files, on another thread, so logging a problem never stops the code that logged it.errorstill raises, sopcallstill catches it. The message starts with the place instead of a bundle line, as Lua positions an error. Level 2 and higher are read from the stack. A message that is not a string, or level 0, is raised unchanged, as in Lua.errorread as a value (const raise = error) finds its place on the stack.
