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.

  1. No code from text (load and similar). Other scripts are loaded only with ctx.script.load, which finds only your stored Scripts; it can only be called, not stored or passed around (Loading other scripts).
  2. No io, os, debug or coroutines, no goto and no print: write to the run's log with ctx.log.info. Results are the same on every run, except for what reads the clock (time.now()), randomness (uuid.v4()) or the outside world through ctx.
  3. Metatables honour only __index, __newindex and __tostring (Tables): setmetatable refuses the rest, so scripts can't redefine operators, and CoCoCo never runs a script's metatable function when it reads a table.
  4. Library tables are frozen.
  5. 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/__newindex chain, and running out of a limit can't be caught, nor can CoCoCo stopping the run (Errors and limits).
  6. ipairs and pairs are part of the loop syntax: a generic for accepts nothing else, and there are no first-class iterators. That's why there's no next; use table.isempty(t) to test for an empty table. gmatch is replaced by matchall, which returns an array.

Errors where standard Lua would carry on.

  1. Reading an undefined global is an error, rather than giving nil.
  2. Integer overflow is an error, rather than wrapping round.
  3. No automatic string-to-number conversion in arithmetic: use tonumber.
  4. Strings are UTF-8 text. String functions count bytes but never split a character, and \128–\255 escapes aren't allowed.
  5. No bitwise operators (& | ~ << >>): use the bit32 extra library.
  6. break outside a loop is an error, as are statements after return in a block.

Different, but predictable.

  1. ipairs fixes its number of passes when the loop starts; pairs order is defined (the array part, then map entries in the order added) and so is traversal of a table that changes (Tables).
  2. Table holes are defined (Tables).
  3. Error messages carry no file and line prefix: pcall gives exactly "boom", and the position is reported with the run's error separately.
  4. string.upper and string.lower handle all letters, not just ASCII.
  5. Some math functions can differ in the last digit between platforms (exp, log, pow and the trigonometric functions; The standard library).
  6. tostring never shows addresses (tables are "table", functions "function"), and inspect is likewise repeatable and shows no identities.
  7. Method calls work without metatables (Method calls): strings find methods in the string library, and tables by field lookup.
  8. 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 then is an error if someGlobal was never defined. Use a local with a default.
  • Using pcall to survive a limit. Running out of a limit can't be caught, by design, so pcall won't rescue a runaway loop.
  • Reaching for gmatch, next or math.random. None exist; The standard library lists the alternatives where there is one.
  • Writing a & b or a << n. The bitwise operators aren't part of the language; use the bit32 library.
  • 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 the true or false, 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.