LuaSandbox
class LuaSandbox (View source)
The LuaSandbox class creates a Lua environment and allows for execution of Lua code.
Constants
SAMPLES |
Used with |
SECONDS |
Used with |
PERCENT |
Used with |
Methods
Call a function in a Lua global variable.
Disable the profiler.
Enable the profiler.
Fetch the current CPU time usage of the Lua environment.
Fetch the current memory usage of the Lua environment.
Fetch the peak memory usage of the Lua environment.
Fetch profiler data.
Return the versions of LuaSandbox and Lua.
Load a precompiled binary chunk into the Lua environment.
Load Lua code into the Lua environment.
Pause the CPU usage timer.
Register a set of PHP functions as a Lua library.
Set the CPU time limit for the Lua environment.
Set the memory limit for the Lua environment.
Unpause the timer paused by LuaSandbox::pauseUsageTimer()
.
Wrap a PHP callable in a LuaSandboxFunction.
Details
array|bool
callFunction(string $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()```.
disableProfiler()
Disable the profiler.
bool
enableProfiler(float $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.
float
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.
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.
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:
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.
static array
getVersionInfo()
Return the versions of LuaSandbox and Lua.
LuaSandboxFunction
loadBinary(string $code, string $chunkName = '')
Load a precompiled binary chunk into the Lua environment.
Loads data generated by ```LuaSandboxFunction::dump()```.
LuaSandboxFunction
loadString(string $code, string $chunkName = '')
Load Lua code into the Lua environment.
This is the equivalent of standard Lua's ```loadstring()``` function.
bool
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(string $libname, array $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()```.
setCPULimit(bool|float $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.
setMemoryLimit(int $limit)
Set the memory limit for the Lua environment.
unpauseUsageTimer()
Unpause the timer paused by LuaSandbox::pauseUsageTimer()
.
LuaSandboxFunction
wrapPhpFunction(callable $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()```.