compilestring
Compiles source into a callable closure, without running it.
Parameters
source | string | Quirrel source code |
name | string | shown in a stack trace and in diagnostics in place of a file name; defaults to "unnamedbuffer" optional |
bindings | table|null | names visible inside source optional |
Return value
The compiled closure. Call it to run the code.
Errors
Throws a plain string on a syntax error, such as end of statement expected (; or lf). Unlike compiling a file, the message carries no line or column, so a caller that wants a location has to search source for it.
Notes
A name found in bindings is baked into the compiled closure as a constant at compile time, the same way an imported name is: source cannot see later writes to bindings, only the value each name held while source was compiled.
Every base library function is added to bindings before compiling, overwriting any entry already using that name, so source always has print, assert and the rest, however bindings was built, and cannot have them shadowed.
Example
let f = compilestring("return 1 + 2")
println("f() =", f())
// bindings supply names visible inside the compiled code, baked in at compile time
let bindings = {FOO = 42}
let g = compilestring("return FOO", "g-src", bindings)
println("g() =", g())
bindings.FOO = 0 // too late: g already carries its own copy of the value
println("g() =", g())
try {
compilestring("this is not )( valid squirrel")
} catch (e) {
println("compilestring(...) throws:", e)
}f() = 3
g() = 42
g() = 42
compilestring(...) throws: ERROR: end of statement expected (; or lf)See also
getroottable | Returns the root table. |
| globals | module index |