numberZero / libluabox

MIT License
0 stars 0 forks source link
minetest

LuaBox library

This is a helper library providing simple general-purpose sandboxing system.

License: MIT (see LICENSE.txt for details).

API

All functions are contained in the libluabox namespace.

Using LuaBox

Example:

local a, b = 12, 5
local sb = libluabox.create_sandbox("Example1")
libluabox.load_code(sb, "c = a + b")
sb.env.a = a
sb.env.b = b
if libluabox.run(sb) then
    print(("Success! %d + %d is %d"):format(a, b, sb.env.c))
else
    print("Fail")
end
-- drop reference to `sb`

create_sandbox(name, options)

Creates and returns a sandbox with label name. options should be a table, if supplied.

Supported options are:

load_code(sandbox, code)

Loads code from string code into the sandbox, making it ready to run.

Returns true on success, false and error message on failure.

run(sandbox)

Runs the code loaded in the sandbox.

Returns true on successful run, false and message in case of error (including code timing out).

Sandbox

Main object managed by the library.

Public properties:

Mods may add properties of form modname:property, if necessary.

Core libraries

Extra libraries

Extending libluabox

register_library(name, library, auto_include)

Adds library with name name to the list of extra libraries available.

If auto_include is true, the library will be added to all new sandboxes by default.

add_library(sandbox, library)

Add (unregistered) library to sandbox environment.

Library format

The library structure is simple:

{
    package1 = { <functions> },
    package2 = { <functions> },
    ...
}

A shallow copy of each table from the library is added to the sandbox environment with the key as the name, merging with existing table with the same name, if any.