Since: luasandbox >= 1.0.0

class LuaSandbox (View source)

The LuaSandbox class creates a Lua environment and allows for execution of Lua code.

Constants

SAMPLES

Used with LuaSandbox::getProfilerFunctionReport() to return timings in samples.

SECONDS

Used with LuaSandbox::getProfilerFunctionReport() to return timings in seconds.

PERCENT

Used with LuaSandbox::getProfilerFunctionReport() to return timings in percentages of the total.

Methods

array|bool
callFunction(string $name, array $arguments)

Call a function in a Lua global variable.

disableProfiler()

Disable the profiler.

bool
enableProfiler(float $period = 0.02)

Enable the profiler.

float
getCPUUsage()

Fetch the current CPU time usage of the Lua environment.

int
getMemoryUsage()

Fetch the current memory usage of the Lua environment.

int
getPeakMemoryUsage()

Fetch the peak memory usage of the Lua environment.

array
getProfilerFunctionReport(int $units = LuaSandbox::SECONDS)

Fetch profiler data.

static array
getVersionInfo()

Return the versions of LuaSandbox and Lua.

loadBinary(string $code, string $chunkName = '')

Load a precompiled binary chunk into the Lua environment.

loadString(string $code, string $chunkName = '')

Load Lua code into the Lua environment.

bool
pauseUsageTimer()

Pause the CPU usage timer.

registerLibrary(string $libname, array $functions)

Register a set of PHP functions as a Lua library.

setCPULimit(bool|float $limit)

Set the CPU time limit for the Lua environment.

setMemoryLimit(int $limit)

Set the memory limit for the Lua environment.

unpauseUsageTimer()

Unpause the timer paused by LuaSandbox::pauseUsageTimer().

wrapPhpFunction(callable $function)

Wrap a PHP callable in a LuaSandboxFunction.

Details

array|bool callFunction(string $name, array $arguments)

Since: luasandbox >= 1.0.0

Call a function in a Lua global variable.

If the name contains "." characters, the function is located via recursive table accesses, as if the name were a Lua expression.

If the variable does not exist, or is not a function, false will be returned and a warning issued.

For more information about calling Lua functions and the return values, see ```LuaSandboxFunction::call()```.

Parameters

string $name

Lua variable name.

array $arguments

Arguments to the function.

Return Value

array|bool

Returns an array of values returned by the Lua function, which may be empty, or false in case of failure.

See also

LuaSandboxFunction::call

disableProfiler()

Since: luasandbox >= 1.1.0

Disable the profiler.

bool enableProfiler(float $period = 0.02)

Since: luasandbox >= 1.1.0

Enable the profiler.

The profiler periodically samples the Lua environment to record the running function. Testing indicates that at least on Linux, setting a period less than 1ms will lead to a high overrun count but no performance problems.

Parameters

float $period

[optional]

Sampling period in seconds.

Return Value

bool

Returns a boolean indicating whether the profiler is enabled.

See also

LuaSandbox::disableProfiler
LuaSandbox::getProfilerFunctionReport

float getCPUUsage()

Since: luasandbox >= 1.0.0

Fetch the current CPU time usage of the Lua environment.

This includes time spent in PHP callbacks.

Note: On Windows, this function always returns zero. On operating systems that do not support CLOCK_THREAD_CPUTIME_ID, such as FreeBSD and Mac OS X, this function will return the elapsed wall-clock time, not CPU time.

Return Value

float

Returns the current CPU time usage in seconds.

See also

LuaSandbox::getMemoryUsage
LuaSandbox::getPeakMemoryUsage
LuaSandbox::setCPULimit

int getMemoryUsage()

Since: luasandbox >= 1.0.0

Fetch the current memory usage of the Lua environment.

int getPeakMemoryUsage()

Since: luasandbox >= 1.0.0

Fetch the peak memory usage of the Lua environment.

Return Value

int

Returns the current memory usage in bytes.

See also

LuaSandbox::getMemoryUsage
LuaSandbox::getCPUUsage
LuaSandbox::setMemoryLimit

array getProfilerFunctionReport(int $units = LuaSandbox::SECONDS)

Since: luasandbox >= 1.1.0

Fetch profiler data.

For a profiling instance previously started by ```LuaSandbox::enableProfiler()```, get a report of the cost of each function.

The measurement unit used for the cost is determined by the $units parameter:

  • ```LuaSandbox::SAMPLES``` Measure in number of samples.
  • ```LuaSandbox::SECONDS``` Measure in seconds of CPU time.
  • ```LuaSandbox::PERCENT``` Measure percentage of CPU time.
  • Note: On Windows, this function always returns an empty array. On operating systems that do not support CLOCK_THREAD_CPUTIME_ID, such as FreeBSD and Mac OS X, this function will report the elapsed wall-clock time, not CPU time.

    Parameters

    int $units

    Measurement unit constant.

    Return Value

    array

    Returns profiler measurements, sorted in descending order, as an associative array. Keys are the Lua function names (with source file and line defined in angle brackets), values are the measurements as integer or float.

    See also

    LuaSandbox::SAMPLES
    LuaSandbox::SECONDS
    LuaSandbox::PERCENT

    static array getVersionInfo()

    Since: luasandbox >= 1.6.0

    Return the versions of LuaSandbox and Lua.

    Return Value

    array

    Returns an array with two keys:

  • LuaSandbox (string), the version of the LuaSandbox extension.
  • Lua (string), the library name and version as defined by the LUA_RELEASE macro, for example, "Lua 5.1.5".
  • LuaSandboxFunction loadBinary(string $code, string $chunkName = '')

    Since: luasandbox >= 1.0.0

    Load a precompiled binary chunk into the Lua environment.

    Loads data generated by ```LuaSandboxFunction::dump()```.

    Parameters

    string $code

    Data from ```LuaSandboxFunction::dump()```.

    string $chunkName

    [optional]

    Name for the loaded function.

    Return Value

    LuaSandboxFunction

    See also

    LuaSandbox::loadString

    LuaSandboxFunction loadString(string $code, string $chunkName = '')

    Since: luasandbox >= 1.0.0

    Load Lua code into the Lua environment.

    This is the equivalent of standard Lua's ```loadstring()``` function.

    Parameters

    string $code

    Lua code.

    string $chunkName

    [optional]

    Name for the loaded chunk, for use in error traces.

    Return Value

    LuaSandboxFunction

    Returns a ```LuaSandboxFunction``` which, when executed, will execute the passed ```$code```.

    See also

    LuaSandbox::registerLibrary
    LuaSandbox::wrapPhpFunction

    bool pauseUsageTimer()

    Since: luasandbox >= 1.4.0

    Pause the CPU usage timer.

    This only has effect when called from within a callback from Lua. When execution returns to Lua, the timer will be automatically unpaused. If a new call into Lua is made, the timer will be unpaused for the duration of that call.

    If a PHP callback calls into Lua again with timer not paused, and then that Lua function calls into PHP again, the second PHP call will not be able to pause the timer. The logic is that even though the second PHP call would avoid counting the CPU usage against the limit, the first call still counts it.

    Return Value

    bool

    Returns a boolean indicating whether the timer is now paused.

    See also

    LuaSandbox::setCPULimit
    LuaSandbox::unpauseUsageTimer

    registerLibrary(string $libname, array $functions)

    Since: luasandbox >= 1.0.0

    Register a set of PHP functions as a Lua library.

    Registers a set of PHP functions as a Lua library, so that Lua can call the relevant PHP code.

    For more information about calling Lua functions and the return values, see ```LuaSandboxFunction::call()```.

    Parameters

    string $libname

    The name of the library. In the Lua state, the global variable of this name will be set to the table of functions. If the table already exists, the new functions will be added to it.

    array $functions

    Returns an array, where each key is a function name, and each value is a corresponding PHP callable.

    See also

    LuaSandbox::loadString
    LuaSandbox::wrapPhpFunction

    setCPULimit(bool|float $limit)

    Since: luasandbox >= 1.0.0

    Set the CPU time limit for the Lua environment.

    If the total user and system time used by the environment after the call to this method exceeds this limit, a ```LuaSandboxTimeoutError``` exception is thrown.

    Time used in PHP callbacks is included in the limit.

    Setting the time limit from a callback while Lua is running causes the timer to be reset, or started if it was not already running.

    Note: On Windows, the CPU limit will be ignored. On operating systems that do not support CLOCK_THREAD_CPUTIME_ID, such as FreeBSD and Mac OS X, wall-clock time rather than CPU time will be limited.

    Parameters

    bool|float $limit

    Limit as a float in seconds, or false for no limit.

    See also

    LuaSandbox::getCPUUsage
    LuaSandbox::setMemoryLimit

    setMemoryLimit(int $limit)

    Since: luasandbox >= 1.0.0

    Set the memory limit for the Lua environment.

    unpauseUsageTimer()

    Since: luasandbox >= 1.0.0

    Unpause the timer paused by LuaSandbox::pauseUsageTimer().

    LuaSandboxFunction wrapPhpFunction(callable $function)

    Since: luasandbox >= 1.2.0

    Wrap a PHP callable in a LuaSandboxFunction.

    Wraps a PHP callable in a ```LuaSandboxFunction```, so it can be passed into Lua as an anonymous function.

    The function must return either an array of values (which may be empty), or NULL which is equivalent to returning the empty array.

    Exceptions will be raised as errors in Lua, however only ```LuaSandboxRuntimeError``` exceptions may be caught inside Lua with ```pcall() or ```xpcall().

    For more information about calling Lua functions and the return values, see ```LuaSandboxFunction::call()```.

    Parameters

    callable $function

    Callable to wrap.

    Return Value

    LuaSandboxFunction

    See also

    LuaSandbox::loadString
    LuaSandbox::registerLibrary