core.engine.load_string
Shared
Executes a buffer within the engine
Syntax
local result = core.engine.load_string(
buffer,
chunk_name = "",
auto_load = true,
use_env = false,
env = nil
)Parameters
| Type | Name | Description |
|---|---|---|
string | buffer | Buffer containing code to load and execute |
string | chunk_name | Label used in error messages to identify the source Example: "scripts/init.lua" |
bool | auto_load | Determines execution behavior: • When true - the loaded function executes immediately• When false - returns a callable function for deferred execution |
bool | use_env | Determines environment behavior: • When true - executes the buffer inside the provided env table• When false - executes the buffer in the default global environment |
table | env | A table to use as the execution environment. Reads and writes inside the buffer are scoped to this util.table. Typically used with __index = _G to retain access to globals |
Returns
| Type | Name | Description |
|---|---|---|
function | bool | result | Value depends on the auto_load argument:• When auto_load is false - the loaded function on success, false otherwise• When auto_load is true - true on successful execution, false otherwise |
Examples
local result = core.engine.load_string([[
core.engine.print("info", "Immediate", "Execution")
]], nil, true)local exec = core.engine.load_string([[
core.engine.print("info", "Delayed", "Execution")
]], nil, false)
if exec then
exec() -- Execute the loaded function manually
endlocal result = core.engine.load_string(
"local x =",
"scripts/init.lua"
) -- Error: scripts/init.lua:2: unexpected symbol near '<eof>'local env = setmetatable({}, {__index = _G})
core.engine.load_string([[
foo = "hello"
]], nil, true, true, env)
core.engine.load_string([[
core.engine.print("info", foo) -- prints "hello"
]], nil, true, true, env)