On this page
Scripting
Differences from Lua
Differences from Lua
The complete list. Each is deliberate and described above; don't "fix" code towards standard Lua's behaviour.
Needed for safety.
- No code from text (
loadand similar). Other scripts are loaded only withctx.script.load, which finds only your stored Scripts; it can only be called, not stored or passed around (Loading other scripts). - No
io,os,debugor coroutines, nogotoand noprint: write to the run's log withctx.log.info. Results are the same on every run, except for what reads the clock (time.now()), randomness (uuid.v4()) or the outside world throughctx. - Metatables honour only
__index,__newindexand__tostring(Tables):setmetatablerefuses the rest, so scripts can't redefine operators, and CoCoCo never runs a script's metatable function when it reads a table. - Library tables are frozen.
- Everything is limited: at most 250 values per expansion (Functions and multiple returns), a single value where a
script returns or a loaded Script exports, at most 8 tables in an
__index/__newindexchain, and running out of a limit can't be caught, nor can CoCoCo stopping the run (Errors and limits). ipairsandpairsare part of the loop syntax: a genericforaccepts nothing else, and there are no first-class iterators. That's why there's nonext; usetable.isempty(t)to test for an empty table.gmatchis replaced bymatchall, which returns an array.
Errors where standard Lua would carry on.
- Reading an undefined global is an error, rather than giving
nil. - Integer overflow is an error, rather than wrapping round.
- No automatic string-to-number conversion in arithmetic: use
tonumber. - Strings are UTF-8 text. String functions count bytes but never split a character,
and
\128–\255escapes aren't allowed. - No bitwise operators (
& | ~ << >>): use thebit32extra library. breakoutside a loop is an error, as are statements afterreturnin a block.
Different, but predictable.
ipairsfixes its number of passes when the loop starts;pairsorder is defined (the array part, then map entries in the order added) and so is traversal of a table that changes (Tables).- Table holes are defined (Tables).
- Error messages carry no file and line prefix:
pcallgives exactly"boom", and the position is reported with the run's error separately. string.upperandstring.lowerhandle all letters, not just ASCII.- Some math functions can differ in the last digit between platforms (
exp,log,powand the trigonometric functions; The standard library). tostringnever shows addresses (tables are"table", functions"function"), andinspectis likewise repeatable and shows no identities.- Method calls work without metatables (Method calls): strings find methods in the string library, and tables by field lookup.
- String patterns (the full Lua 5.4 language) are metered: a pattern that needs extreme backtracking ends the run, and can't be caught.
Additions. Type annotations (local x: number, function f(a: integer): string) are
a SwiftLua addition that plain Lua would reject. The standard library's additions beyond
Lua 5.1 (inspect, the table, set, string and math helpers, and the handy globals) add
to Lua rather than change it. Two differ in shape from common Lua habits:
table.sort_by returns a new array, and table.partition returns one table holding both
halves. (table.sort sorts in place, as in Lua.)
Common mistakes
Each of these fails to check, stops with an error, or quietly does the wrong thing.
- Testing a global that may not exist.
if someGlobal thenis an error ifsomeGlobalwas never defined. Use a local with a default. - Using
pcallto survive a limit. Running out of a limit can't be caught, by design, sopcallwon't rescue a runaway loop. - Reaching for
gmatch,nextormath.random. None exist; The standard library lists the alternatives where there is one. - Writing
a & bora << n. The bitwise operators aren't part of the language; use thebit32library. - Writing to a library table (
string.trim = …). They are frozen, so it's an error. - Taking one value from a function that waits (
local res = ctx.store.get("x")). You get thetrueorfalse, not the data (Functions that wait on the outside world). - Working round a missing feature. If something genuinely can't be done here (coroutines, files, a library CoCoCo doesn't have), say so rather than writing code that won't run.