# LuaSandbox

# Introduction

LuaSandbox is an extension for PHP 7 and PHP 8 to allow safely running untrusted Lua 5.1 code from within PHP.

Differences compared to the Lua extension:

- LuaSandbox has support for time and memory limits.
- LuaSandbox provides a default-safe environment for running untrusted code. Stock Lua functions were reviewed for security, and several were patched accordingly.
- LuaSandbox has a PHP interface which is more complex, precise and powerful, but it is less convenient for developers.
- LuaSandbox supports only Lua 5.1. It is difficult to change this, because LuaSandbox uses heavily modified Lua standard libraries, and due to the lack of backwards compatibility between major Lua versions. LuaSandbox aims to maximise backwards compatibility with user-supplied scripts.

- Installing/Configuring
  - Requirements
  - Installation
- Differences from Standard Lua
- Examples
  - Basic usage for LuaSandbox
- LuaSandbox — The LuaSandbox class
  - LuaSandbox::callFunction — Call a function in a Lua global variable
  - LuaSandbox::disableProfiler — Disable the profiler
  - LuaSandbox::enableProfiler — Enable the profiler.
  - LuaSandbox::getCPUUsage — Fetch the current CPU time usage of the Lua environment
  - LuaSandbox::getMemoryUsage — Fetch the current memory usage of the Lua environment
  - LuaSandbox::getPeakMemoryUsage — Fetch the peak memory usage of the Lua environment
  - LuaSandbox::getProfilerFunctionReport — Fetch profiler data
  - LuaSandbox::getVersionInfo — Return the versions of LuaSandbox and Lua
  - LuaSandbox::loadBinary — Load a precompiled binary chunk into the Lua environment
  - LuaSandbox::loadString — Load Lua code into the Lua environment
  - LuaSandbox::pauseUsageTimer — Pause the CPU usage timer
  - LuaSandbox::registerLibrary — Register a set of PHP functions as a Lua library
  - LuaSandbox::setCPULimit — Set the CPU time limit for the Lua environment
  - LuaSandbox::setMemoryLimit — Set the memory limit for the Lua environment
  - LuaSandbox::unpauseUsageTimer — Unpause the timer paused by LuaSandbox::pauseUsageTimer
  - LuaSandbox::wrapPhpFunction — Wrap a PHP callable in a LuaSandboxFunction
- LuaSandboxFunction — The LuaSandboxFunction class
  - LuaSandboxFunction::call — Call a Lua function
  - LuaSandboxFunction::__construct — Unused
  - LuaSandboxFunction::dump — Dump the function as a binary blob
- LuaSandboxError — The LuaSandboxError class
- LuaSandboxErrorError — The LuaSandboxErrorError class
- LuaSandboxFatalError — The LuaSandboxFatalError class
- LuaSandboxMemoryError — The LuaSandboxMemoryError class
- LuaSandboxRuntimeError — The LuaSandboxRuntimeError class
- LuaSandboxSyntaxError — The LuaSandboxSyntaxError class
- LuaSandboxTimeoutError — The LuaSandboxTimeoutError class

`↑` and `↓` to navigate • `Enter` to select • `Esc` to close • `/` to open