Luau scripts: code server actions and data-only addon hooks
ERPForAll runs the Python bodies of Odoo code server actions, and the hooks and post/end
migrations of data-only addons, as Luau (ADR 0001, ADR 0009 §6-§7). Scripts run in a sandbox
(crates/erp-script, design: docs/design/m2-script.md) with the rights of the user who triggers
them (ADR 0009 §10).
What a script can use
Section titled “What a script can use”- Basic functions:
assert error ipairs next pairs pcall rawequal rawget rawlen rawset select tonumber tostring type typeof unpack. - Libraries:
math,string,table,utf8,bit32(read-only: you cannot replace their functions). - What the runner injects. For server actions that is
env(env.search, …),record/records,contextandlog.
Not available: require, load/loadstring, getfenv/setfenv, setmetatable/getmetatable,
coroutine, print, os, io, debug, collectgarbage, _G. Scripts are plain source text;
precompiled bytecode is rejected.
Results
Section titled “Results”A script’s result is what it returns: one value (use a table for several). An Odoo-style
action = {...} assignment works where the runner asks for the action global (server actions do).
if record ~= nil then local data = env.read(record.model, {record.id}, {"is_company"})[1] if data.is_company then action = { type = "ir.actions.act_url", url = "https://example.com/p/" .. record.id } endendReturned tables must be plain data: arrays with keys 1..n, or records with string keys. Holes,
mixed keys, functions, cycles, NaN and infinity are errors, not silently converted.
Limits
Section titled “Limits”Each run has limits (defaults, which a host may only lower): 256 KiB of source, 10 million steps (an
approximate measure: calls, loop iterations, pattern-matching steps), 5 seconds, 16 MiB of memory.
string.find/match/gmatch/gsub accept subjects up to 64 KiB and patterns up to 256 bytes.
When a limit is reached, or a host function (env.write, …) fails, the run stops for good: pcall
cannot catch it, and the action fails with the real error (for example the AccessError from the
ORM). pcall still catches your own error(...) calls and returns the message string.
Differences from plain Luau
Section titled “Differences from plain Luau”tostring(t)andstring.format("%s", t)printtable,function, … for reference values (no addresses).error(v)with a table or function raises its type name; use string messages.table.sortandtable.movecount against the step limit in proportion to the table size.- A returned value (and values passed to host functions) may total at most the memory limit in string bytes: returning one big string many times in a table fails instead of copying it.
Server actions
Section titled “Server actions”code actions execute once for the active set, including an empty set. record is
the first {id, model} descriptor or nil; records is the descriptor array. Field
values are explicit reads, not attributes on a descriptor. env.uid, env.context
and context describe the caller. Mutating a Luau table never changes server rights.
| Function | Arguments and result |
|---|---|
env.search | (model, domain[, limit]) → descriptor array; domain uses Odoo list syntax, limit defaults to256, range1–256 |
env.read | (model, ids, fields) → row dictionaries; 1–32 explicit field names; relations use raw IDs |
env.write | (model, ids, values) → nil; values use ORM field conversion and x2many command arrays |
env.create | (model, values) → one descriptor |
env.unlink | (model, ids) → nil |
env.ref | (module.name) → existing descriptor after target read authorization; absent IDs fail |
log | (string) → nil; combined log text ≤64KiB, emitted through server tracing after success |
All host operations use the caller’s transaction, model/record rules, field groups,
companies and context. IDs must be positive and unique (at most256 per call);
create/write dictionaries have at most32 fields and cannot set id. Nested x2many
commands are inspected before dispatch, including HTML refusal and a conservative
256-command-record work bound (multiplied by the active parent count). The sandbox
provides no sudo, impersonation, arbitrary method dispatch or raw SQL API.
Return one action dictionary or assign action; both non-nil is an error. Nil
means no client action. A client action must have a nonempty string type.
evaluation_type=equation evaluates its Luau expression per active record, then
writes using the normal field converter; e.g. env.read(record.model, {record.id}, {'name'})[1].name .. '-updated'. The active context is per record for equations.
The bridge accepts at most256 active records,1024 host calls per evaluation,256 context entries, and1MiB logical string/byte content per converted request/response (max100k nodes, depth32). These are admission limits, not a measured heap or wire size. Generic ORM reads may materialize stored/computed fields before the bridge’s conversion refusal; no exact database-transfer ceiling is claimed. A stored action code field also observes the tree’s64KiB per-field bound.
A per-process semaphore admits at most the host’s available CPU parallelism. Busy workers refuse immediately; replicas have independent permits (cluster ceiling is replicas × per-process permits). A permit stays owned by the blocking VM until exit. Cancellation disconnects host requests and trips a sticky VM flag. Scripts cannot continue after host failures; the primary ORM error retains its kind/message/SQLSTATE and source. Always roll back on error, timeout or cancellation, including after earlier successful host writes. No callback retains the transaction borrow.
run_as_superuser stays refused until audited elevation exists. Sequence generation requires secured
base sequence dispatch; the raw sequence access gap is not bypassed. HTML updates
remain refused pending the HTML sanitizer engine, including equations. This is not
complete object-write/HTML parity. Product performance remains deferred to milestone
end (ADR0065).