Engine
engine.compile_string
Shared
Validates whether a buffer contains valid Lua syntax
Syntax
local result, error = engine.compile_string(buffer, chunk_name = "")Parameters
| Type | Name | Description |
|---|---|---|
string | buffer | Buffer containing code to be validated |
string | chunk_name | Label used in error messages to identify the source Example: "scripts/init.lua" |
Returns
| Type | Name | Description |
|---|---|---|
bool | result | true if the buffer is valid Lua syntax, false otherwise |
bool / string | error | false on success, or error message containing chunk name and line number on failure |
Examples
local result, err = engine.compile_string("engine.print('info', 'hello')")
if result then
engine.print("info", "Valid syntax")
else
engine.print("error", err)
endlocal result, err = engine.compile_string("local x =", "scripts/init.lua")
if not result then
engine.print("error", err) --scripts/init.lua:1: unexpected symbol near '<eof>'
endlocal code = "x = 1 + 1"
local try_expr = "return "..code
local exec = (
engine.compile_string(try_expr) and
engine.load_string(try_expr, nil, false)
) or engine.load_string(code, nil, false)
if exec then
exec()
end