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

callFunction($name, array $arguments)

Call a function in a Lua global variable.

disableProfiler()

Disable the profiler.

enableProfiler($period = 0.02)

Enable the profiler.

getCPUUsage()

Fetch the current CPU time usage of the Lua environment.

getMemoryUsage()

Fetch the current memory usage of the Lua environment.

getPeakMemoryUsage()

Fetch the peak memory usage of the Lua environment.

getProfilerFunctionReport($units = LuaSandbox::SECONDS)

Fetch profiler data.

static 
getVersionInfo()

Return the versions of LuaSandbox and Lua.

loadBinary($code, $chunkName = '')

Load a precompiled binary chunk into the Lua environment.

loadString($code, $chunkName = '')

Load Lua code into the Lua environment.

pauseUsageTimer()

Pause the CPU usage timer.

registerLibrary($libname, $functions)

Register a set of PHP functions as a Lua library.

setCPULimit($limit)

Set the CPU time limit for the Lua environment.

setMemoryLimit($limit)

Set the memory limit for the Lua environment.

unpauseUsageTimer()

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

wrapPhpFunction($function)

Wrap a PHP callable in a LuaSandboxFunction.

Details

callFunction($name, array $arguments)

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

$name
array $arguments

disableProfiler()

Disable the profiler.

enableProfiler($period = 0.02)

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

$period

getCPUUsage()

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.

getMemoryUsage()

Fetch the current memory usage of the Lua environment.

getPeakMemoryUsage()

Fetch the peak memory usage of the Lua environment.

getProfilerFunctionReport($units = LuaSandbox::SECONDS)

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

    $units

    static getVersionInfo()

    Return the versions of LuaSandbox and Lua.

    loadBinary($code, $chunkName = '')

    Load a precompiled binary chunk into the Lua environment.

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

    Parameters

    $code
    $chunkName

    loadString($code, $chunkName = '')

    Load Lua code into the Lua environment.

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

    Parameters

    $code
    $chunkName

    pauseUsageTimer()

    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.

    registerLibrary($libname, $functions)

    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

    $libname
    $functions

    setCPULimit($limit)

    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

    $limit

    setMemoryLimit($limit)

    Set the memory limit for the Lua environment.

    Parameters

    $limit

    unpauseUsageTimer()

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

    wrapPhpFunction($function)

    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

    $function