2012-01-28 16:22:18 +00:00
|
|
|
<?php
|
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
class Scribunto_LuaSandboxEngine extends Scribunto_LuaEngine {
|
|
|
|
public $options, $loaded = false;
|
2012-09-05 09:39:58 +00:00
|
|
|
protected $lineCache = array();
|
2012-09-25 04:55:11 +00:00
|
|
|
|
2013-01-25 17:53:18 +00:00
|
|
|
public function getPerformanceCharacteristics() {
|
|
|
|
return array(
|
|
|
|
'phpCallsRequireSerialization' => false,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-03-20 16:30:26 +00:00
|
|
|
public function getSoftwareInfo( &$software ) {
|
|
|
|
if ( is_callable( 'LuaSandbox::getVersionInfo' ) ) {
|
|
|
|
$versions = LuaSandbox::getVersionInfo();
|
|
|
|
} else {
|
|
|
|
$sandbox = new LuaSandbox;
|
|
|
|
list( $luaver ) = $sandbox->loadString( 'return _VERSION' )->call();
|
|
|
|
$versions = array(
|
|
|
|
'LuaSandbox' => phpversion( "LuaSandbox" ),
|
|
|
|
'Lua' => $luaver,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$software['[https://www.mediawiki.org/wiki/Extension:Scribunto#LuaSandbox LuaSandbox]'] = $versions['LuaSandbox'];
|
|
|
|
$software['[http://www.lua.org/ Lua]'] = str_replace( 'Lua ', '', $versions['Lua'] );
|
|
|
|
if ( isset( $versions['LuaJIT'] ) ) {
|
|
|
|
$software['[http://luajit.org/ LuaJIT]'] = str_replace( 'LuaJIT ', '', $versions['LuaJIT'] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-14 12:42:42 +00:00
|
|
|
private function getLimitReportData() {
|
|
|
|
$ret = array();
|
2012-01-28 16:22:18 +00:00
|
|
|
$this->load();
|
2012-09-05 09:39:58 +00:00
|
|
|
|
|
|
|
$t = $this->interpreter->getCPUUsage();
|
2013-03-14 12:42:42 +00:00
|
|
|
$ret['scribunto-limitreport-timeusage'] = array(
|
|
|
|
sprintf( "%.3f", $t ),
|
|
|
|
sprintf( "%.3f", $this->options['cpuLimit'] )
|
|
|
|
);
|
|
|
|
$ret['scribunto-limitreport-memusage'] = array(
|
|
|
|
$this->interpreter->getPeakMemoryUsage(),
|
|
|
|
$this->options['memoryLimit'],
|
|
|
|
);
|
2013-03-25 15:56:38 +00:00
|
|
|
|
|
|
|
$logs = $this->getLogBuffer();
|
|
|
|
if ( $logs !== '' ) {
|
|
|
|
$ret['scribunto-limitreport-logs'] = $logs;
|
|
|
|
}
|
|
|
|
|
2012-09-05 09:39:58 +00:00
|
|
|
if ( $t < 1.0 ) {
|
2013-03-14 12:42:42 +00:00
|
|
|
return $ret;
|
2012-09-05 09:39:58 +00:00
|
|
|
}
|
2013-03-14 12:42:42 +00:00
|
|
|
|
|
|
|
$percentProfile = $this->interpreter->getProfilerFunctionReport(
|
|
|
|
Scribunto_LuaSandboxInterpreter::PERCENT
|
|
|
|
);
|
2012-09-05 09:39:58 +00:00
|
|
|
if ( !count( $percentProfile ) ) {
|
2013-03-14 12:42:42 +00:00
|
|
|
return $ret;
|
2012-09-05 09:39:58 +00:00
|
|
|
}
|
2013-03-14 12:42:42 +00:00
|
|
|
$timeProfile = $this->interpreter->getProfilerFunctionReport(
|
|
|
|
Scribunto_LuaSandboxInterpreter::SECONDS
|
|
|
|
);
|
2012-09-05 09:39:58 +00:00
|
|
|
|
2013-03-14 12:42:42 +00:00
|
|
|
$lines = array();
|
|
|
|
$cumulativePercent = 0;
|
2012-09-05 09:39:58 +00:00
|
|
|
$num = $otherTime = $otherPercent = 0;
|
|
|
|
foreach ( $percentProfile as $name => $percent ) {
|
|
|
|
$time = $timeProfile[$name] * 1000;
|
|
|
|
$num++;
|
|
|
|
if ( $cumulativePercent <= 99 && $num <= 10 ) {
|
|
|
|
// Map some regularly appearing internal names
|
|
|
|
if ( preg_match( '/^<mw.lua:(\d+)>$/', $name, $m ) ) {
|
|
|
|
$line = $this->getMwLuaLine( $m[1] );
|
|
|
|
if ( preg_match( '/^\s*(local\s+)?function ([a-zA-Z0-9_.]*)/', $line, $m ) ) {
|
|
|
|
$name = $m[2] . ' ' . $name;
|
|
|
|
}
|
|
|
|
}
|
2013-03-14 12:42:42 +00:00
|
|
|
$lines[] = array( $name, sprintf( '%.0f', $time ), sprintf( '%.1f', $percent ) );
|
2012-09-05 09:39:58 +00:00
|
|
|
} else {
|
|
|
|
$otherTime += $time;
|
|
|
|
$otherPercent += $percent;
|
|
|
|
}
|
2013-03-14 12:42:42 +00:00
|
|
|
$cumulativePercent += $percent;
|
2012-09-05 09:39:58 +00:00
|
|
|
}
|
|
|
|
if ( $otherTime ) {
|
2013-03-14 12:42:42 +00:00
|
|
|
$lines[] = array( '[others]', sprintf( '%.0f', $otherTime ), sprintf( '%.1f', $otherPercent ) );
|
|
|
|
}
|
|
|
|
$ret['scribunto-limitreport-profile'] = $lines;
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLimitReport() {
|
|
|
|
$data = $this->getLimitReportData();
|
|
|
|
$lang = Language::factory( 'en' );
|
|
|
|
|
|
|
|
$t = $this->interpreter->getCPUUsage();
|
|
|
|
$s = 'Lua time usage: ' . sprintf( "%.3f", $data['scribunto-limitreport-timeusage'] ) . "s\n" .
|
|
|
|
'Lua memory usage: ' . $lang->formatSize( $data['scribunto-limitreport-memusage'] ) . "\n";
|
|
|
|
if ( isset( $data['scribunto-limitreport-profile'] ) ) {
|
|
|
|
$s .= "Lua Profile:\n";
|
|
|
|
$format = " %-59s %8.0f ms %8.1f%%\n";
|
|
|
|
foreach ( $data['scribunto-limitreport-profile'] as $line ) {
|
|
|
|
$s .= sprintf( $format, $line[0], $line[1], $line[2] );
|
|
|
|
}
|
2012-09-05 09:39:58 +00:00
|
|
|
}
|
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
|
2013-03-14 12:42:42 +00:00
|
|
|
public function reportLimitData( ParserOutput $output ) {
|
|
|
|
$data = $this->getLimitReportData();
|
|
|
|
foreach ( $data as $k => $v ) {
|
|
|
|
$output->setLimitReportData( $k, $v );
|
|
|
|
}
|
2013-03-25 15:56:38 +00:00
|
|
|
if ( isset( $data['scribunto-limitreport-logs'] ) ) {
|
|
|
|
$output->addModules( 'ext.scribunto' );
|
|
|
|
}
|
2013-03-14 12:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function formatLimitData( $key, &$value, &$report, $isHTML, $localize ) {
|
|
|
|
global $wgLang;
|
|
|
|
$lang = $localize ? $wgLang : Language::factory( 'en' );
|
|
|
|
switch ( $key ) {
|
2013-03-25 15:56:38 +00:00
|
|
|
case 'scribunto-limitreport-logs':
|
|
|
|
if ( $isHTML ) {
|
|
|
|
$report .= $this->formatHtmlLogs( $value, $localize );
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
2013-03-14 12:42:42 +00:00
|
|
|
case 'scribunto-limitreport-memusage':
|
|
|
|
$value = array_map( array( $lang, 'formatSize' ), $value );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $key !== 'scribunto-limitreport-profile' ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$keyMsg = wfMessage( 'scribunto-limitreport-profile' );
|
|
|
|
$msMsg = wfMessage( 'scribunto-limitreport-profile-ms' );
|
|
|
|
$percentMsg = wfMessage( 'scribunto-limitreport-profile-percent' );
|
|
|
|
if ( !$localize ) {
|
|
|
|
$keyMsg->inLanguage( 'en' )->useDatabase( false );
|
|
|
|
$msMsg->inLanguage( 'en' )->useDatabase( false );
|
|
|
|
$percentMsg->inLanguage( 'en' )->useDatabase( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
// To avoid having to do actual work in Message::fetchMessage for each
|
|
|
|
// line in the loops below, call ->exists() here to populate ->message.
|
|
|
|
$msMsg->exists();
|
|
|
|
$percentMsg->exists();
|
|
|
|
|
|
|
|
if ( $isHTML ) {
|
|
|
|
$report .= Html::openElement( 'tr' ) .
|
|
|
|
Html::rawElement( 'th', array( 'colspan' => 2 ), $keyMsg->parse() ) .
|
|
|
|
Html::closeElement( 'tr' ) .
|
|
|
|
Html::openElement( 'tr' ) .
|
|
|
|
Html::openElement( 'td', array( 'colspan' => 2 ) ) .
|
|
|
|
Html::openElement( 'table' );
|
|
|
|
foreach ( $value as $line ) {
|
|
|
|
$name = $line[0];
|
|
|
|
$location = '';
|
|
|
|
if ( preg_match( '/^(.*?) *<([^<>]+):(\d+)>$/', $name, $m ) ) {
|
|
|
|
$name = $m[1];
|
|
|
|
$title = Title::newFromText( $m[2] );
|
|
|
|
if ( $title && $title->getNamespace() === NS_MODULE ) {
|
|
|
|
$location = '<' . Linker::link( $title ) . ":{$m[3]}>";
|
|
|
|
} else {
|
|
|
|
$location = htmlspecialchars( "<{$m[2]}:{$m[3]}>" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$ms = clone $msMsg;
|
|
|
|
$ms->params( $line[1] );
|
|
|
|
$pct = clone $percentMsg;
|
|
|
|
$pct->params( $line[2] );
|
|
|
|
$report .= Html::openElement( 'tr' ) .
|
|
|
|
Html::element( 'td', null, $name ) .
|
|
|
|
Html::rawElement( 'td', null, $location ) .
|
|
|
|
Html::rawElement( 'td', array( 'align' => 'right' ), $ms->parse() ) .
|
|
|
|
Html::rawElement( 'td', array( 'align' => 'right' ), $pct->parse() ) .
|
|
|
|
Html::closeElement( 'tr' );
|
|
|
|
}
|
|
|
|
$report .= Html::closeElement( 'table' ) .
|
|
|
|
Html::closeElement( 'td' ) .
|
|
|
|
Html::closeElement( 'tr' );
|
|
|
|
} else {
|
|
|
|
$report .= $keyMsg->text() . ":\n";
|
|
|
|
foreach ( $value as $line ) {
|
|
|
|
$ms = clone $msMsg;
|
|
|
|
$ms->params( $line[1] );
|
|
|
|
$pct = clone $percentMsg;
|
|
|
|
$pct->params( $line[2] );
|
|
|
|
$report .= sprintf( " %-59s %11s %11s\n", $line[0], $ms->text(), $pct->text() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-05 09:39:58 +00:00
|
|
|
protected function getMwLuaLine( $lineNum ) {
|
|
|
|
if ( !isset( $this->lineCache['mw.lua'] ) ) {
|
|
|
|
$this->lineCache['mw.lua'] = file( $this->getLuaLibDir() . '/mw.lua' );
|
|
|
|
}
|
|
|
|
return $this->lineCache['mw.lua'][$lineNum - 1];
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2012-04-04 05:10:47 +00:00
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
function newInterpreter() {
|
|
|
|
return new Scribunto_LuaSandboxInterpreter( $this, $this->options );
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
class Scribunto_LuaSandboxInterpreter extends Scribunto_LuaInterpreter {
|
2014-08-17 21:11:26 +00:00
|
|
|
public $engine, $sandbox, $libraries, $profilerEnabled;
|
2012-01-28 16:22:18 +00:00
|
|
|
|
2012-09-25 04:55:11 +00:00
|
|
|
const SAMPLES = 0;
|
|
|
|
const SECONDS = 1;
|
|
|
|
const PERCENT = 2;
|
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
function __construct( $engine, $options ) {
|
Added tests and fixed bugs
* Added unit tests for the two Lua interpreter classes
* Fixed a bug in checkType()
* Have Scribunto_LuaSandboxInterpreter throw an exception on construct
when the extension doesn't exist, to match the standalone behaviour.
* In Scribunto_LuaSandboxInterpreter, removed debugging statements
accidentally left in.
* Convert LuaSandboxTimeoutError to the appropriate common error
message.
* Moved the option munging from the sandbox engine to the interpreter,
so that the interpreter can be unit tested separately.
* Use /bin/sh instead of bash for lua_ulimit.sh, since dash is smaller
and still supports ulimit.
* Use exec to run the lua binary, so that the vsize of the shell doesn't
add to the memory limit.
* Added a quit function to the standalone interpreter. Unused at present.
* Don't add a comma after the last element of a table in a Lua
expression.
* Make the SIGXCPU detection work: proc_open() runs the command via a
shell, which reports signals in the child via the exit status, so
proc_get_status() will never return a valid termsig element.
* In MWServer:call(), fixed a bug causing the return values to be
wrapped in an array.
* Fixed a misunderstanding of what select() does.
* In MWServer:getStatus(), fixed indexes so that vsize will be correct.
Removed RSS, since it wasn't used anyway and turns out to be measured
in multiples of the page size, and I couldn't be bothered trying to
fetch that from getconf. Return the PID and vsize as numbers rather
than strings.
* Added a simple table dump feature to MWServer:debug().
* Fixed brackets in MWServer:tostring().
* Added missing Linux 32-bit binary.
Change-Id: Ibf5f4656b1c0a9f81287d363184c3fe9d2abdafd
2012-04-16 04:41:08 +00:00
|
|
|
if ( !extension_loaded( 'luasandbox' ) ) {
|
2014-07-07 18:46:59 +00:00
|
|
|
throw new Scribunto_LuaInterpreterNotFoundError(
|
2012-04-19 07:40:56 +00:00
|
|
|
'The luasandbox extension is not present, this engine cannot be used.' );
|
Added tests and fixed bugs
* Added unit tests for the two Lua interpreter classes
* Fixed a bug in checkType()
* Have Scribunto_LuaSandboxInterpreter throw an exception on construct
when the extension doesn't exist, to match the standalone behaviour.
* In Scribunto_LuaSandboxInterpreter, removed debugging statements
accidentally left in.
* Convert LuaSandboxTimeoutError to the appropriate common error
message.
* Moved the option munging from the sandbox engine to the interpreter,
so that the interpreter can be unit tested separately.
* Use /bin/sh instead of bash for lua_ulimit.sh, since dash is smaller
and still supports ulimit.
* Use exec to run the lua binary, so that the vsize of the shell doesn't
add to the memory limit.
* Added a quit function to the standalone interpreter. Unused at present.
* Don't add a comma after the last element of a table in a Lua
expression.
* Make the SIGXCPU detection work: proc_open() runs the command via a
shell, which reports signals in the child via the exit status, so
proc_get_status() will never return a valid termsig element.
* In MWServer:call(), fixed a bug causing the return values to be
wrapped in an array.
* Fixed a misunderstanding of what select() does.
* In MWServer:getStatus(), fixed indexes so that vsize will be correct.
Removed RSS, since it wasn't used anyway and turns out to be measured
in multiples of the page size, and I couldn't be bothered trying to
fetch that from getconf. Return the PID and vsize as numbers rather
than strings.
* Added a simple table dump feature to MWServer:debug().
* Fixed brackets in MWServer:tostring().
* Added missing Linux 32-bit binary.
Change-Id: Ibf5f4656b1c0a9f81287d363184c3fe9d2abdafd
2012-04-16 04:41:08 +00:00
|
|
|
}
|
2012-04-13 10:38:12 +00:00
|
|
|
$this->engine = $engine;
|
|
|
|
$this->sandbox = new LuaSandbox;
|
|
|
|
$this->sandbox->setMemoryLimit( $options['memoryLimit'] );
|
|
|
|
$this->sandbox->setCPULimit( $options['cpuLimit'] );
|
2014-07-07 18:46:59 +00:00
|
|
|
if ( is_callable( array( $this->sandbox, 'enableProfiler' ) ) )
|
2012-12-27 23:26:06 +00:00
|
|
|
{
|
|
|
|
if ( !isset( $options['profilerPeriod'] ) ) {
|
|
|
|
$options['profilerPeriod'] = 0.02;
|
|
|
|
}
|
|
|
|
if ( $options['profilerPeriod'] ) {
|
|
|
|
$this->profilerEnabled = true;
|
|
|
|
$this->sandbox->enableProfiler( $options['profilerPeriod'] );
|
|
|
|
}
|
2012-09-05 09:39:58 +00:00
|
|
|
}
|
2012-04-13 10:38:12 +00:00
|
|
|
}
|
2012-01-28 16:46:15 +00:00
|
|
|
|
2012-04-24 10:40:40 +00:00
|
|
|
protected function convertSandboxError( $e ) {
|
|
|
|
$opts = array();
|
|
|
|
if ( isset( $e->luaTrace ) ) {
|
|
|
|
$opts['trace'] = $e->luaTrace;
|
|
|
|
}
|
|
|
|
$message = $e->getMessage();
|
|
|
|
if ( preg_match( '/^(.*?):(\d+): (.*)$/', $message, $m ) ) {
|
|
|
|
$opts['module'] = $m[1];
|
|
|
|
$opts['line'] = $m[2];
|
|
|
|
$message = $m[3];
|
2012-09-05 09:39:58 +00:00
|
|
|
}
|
2012-04-24 10:40:40 +00:00
|
|
|
return $this->engine->newLuaError( $message, $opts );
|
|
|
|
}
|
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
public function loadString( $text, $chunkName ) {
|
2012-01-28 16:22:18 +00:00
|
|
|
try {
|
2012-04-13 10:38:12 +00:00
|
|
|
return $this->sandbox->loadString( $text, $chunkName );
|
|
|
|
} catch ( LuaSandboxError $e ) {
|
2012-04-24 10:40:40 +00:00
|
|
|
throw $this->convertSandboxError( $e );
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2012-04-13 10:38:12 +00:00
|
|
|
}
|
2014-07-07 18:46:59 +00:00
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
public function registerLibrary( $name, $functions ) {
|
|
|
|
$realLibrary = array();
|
|
|
|
foreach ( $functions as $funcName => $callback ) {
|
|
|
|
$realLibrary[$funcName] = array(
|
|
|
|
new Scribunto_LuaSandboxCallback( $callback ),
|
2012-09-05 09:39:58 +00:00
|
|
|
$funcName );
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2012-04-13 10:38:12 +00:00
|
|
|
$this->sandbox->registerLibrary( $name, $realLibrary );
|
2012-01-28 16:22:18 +00:00
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
# TODO: replace this with
|
|
|
|
#$this->sandbox->registerVirtualLibrary(
|
|
|
|
# $name, array( $this, 'callback' ), $functions );
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
public function callFunction( $func /*, ... */ ) {
|
|
|
|
$args = func_get_args();
|
|
|
|
$func = array_shift( $args );
|
|
|
|
try {
|
2014-09-27 18:07:11 +00:00
|
|
|
$ret = call_user_func_array( array( $func, 'call' ), $args );
|
|
|
|
if ( $ret === false ) {
|
|
|
|
// Per the documentation on LuaSandboxFunction::call, a return value
|
|
|
|
// of false means that something went wrong and it's PHP's fault,
|
|
|
|
// so throw a "real" exception.
|
|
|
|
throw new MWException(
|
|
|
|
__METHOD__ . ': LuaSandboxFunction::call returned false' );
|
|
|
|
}
|
|
|
|
return $ret;
|
Added tests and fixed bugs
* Added unit tests for the two Lua interpreter classes
* Fixed a bug in checkType()
* Have Scribunto_LuaSandboxInterpreter throw an exception on construct
when the extension doesn't exist, to match the standalone behaviour.
* In Scribunto_LuaSandboxInterpreter, removed debugging statements
accidentally left in.
* Convert LuaSandboxTimeoutError to the appropriate common error
message.
* Moved the option munging from the sandbox engine to the interpreter,
so that the interpreter can be unit tested separately.
* Use /bin/sh instead of bash for lua_ulimit.sh, since dash is smaller
and still supports ulimit.
* Use exec to run the lua binary, so that the vsize of the shell doesn't
add to the memory limit.
* Added a quit function to the standalone interpreter. Unused at present.
* Don't add a comma after the last element of a table in a Lua
expression.
* Make the SIGXCPU detection work: proc_open() runs the command via a
shell, which reports signals in the child via the exit status, so
proc_get_status() will never return a valid termsig element.
* In MWServer:call(), fixed a bug causing the return values to be
wrapped in an array.
* Fixed a misunderstanding of what select() does.
* In MWServer:getStatus(), fixed indexes so that vsize will be correct.
Removed RSS, since it wasn't used anyway and turns out to be measured
in multiples of the page size, and I couldn't be bothered trying to
fetch that from getconf. Return the PID and vsize as numbers rather
than strings.
* Added a simple table dump feature to MWServer:debug().
* Fixed brackets in MWServer:tostring().
* Added missing Linux 32-bit binary.
Change-Id: Ibf5f4656b1c0a9f81287d363184c3fe9d2abdafd
2012-04-16 04:41:08 +00:00
|
|
|
} catch ( LuaSandboxTimeoutError $e ) {
|
2012-04-23 11:36:13 +00:00
|
|
|
throw $this->engine->newException( 'scribunto-common-timeout' );
|
2012-04-13 10:38:12 +00:00
|
|
|
} catch ( LuaSandboxError $e ) {
|
2012-04-24 10:40:40 +00:00
|
|
|
throw $this->convertSandboxError( $e );
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-13 20:50:44 +00:00
|
|
|
public function wrapPhpFunction( $callable ) {
|
|
|
|
if ( is_callable( array( $this->sandbox, 'wrapPhpFunction' ) ) ) {
|
|
|
|
return $this->sandbox->wrapPhpFunction( $callable );
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have to hack around the lack of the wrapper function by loading a
|
|
|
|
// dummy library with $callable, then extracting the function, and then
|
|
|
|
// for good measure nilling out the library table.
|
|
|
|
list( $name ) = $this->sandbox->loadString( '
|
|
|
|
for i = 0, math.huge do
|
|
|
|
if not _G["*LuaSandbox* temp" .. i] then return "*LuaSandbox* temp" .. i end
|
|
|
|
end
|
|
|
|
' )->call();
|
|
|
|
$this->sandbox->registerLibrary( $name, array( 'func' => $callable ) );
|
|
|
|
list( $func ) = $this->sandbox->loadString(
|
|
|
|
"local ret = _G['$name'].func _G['$name'] = nil return ret"
|
|
|
|
)->call();
|
|
|
|
return $func;
|
|
|
|
}
|
|
|
|
|
2012-12-24 14:45:16 +00:00
|
|
|
public function isLuaFunction( $object ) {
|
|
|
|
return $object instanceof LuaSandboxFunction;
|
|
|
|
}
|
|
|
|
|
2012-04-30 06:36:21 +00:00
|
|
|
public function getPeakMemoryUsage() {
|
|
|
|
return $this->sandbox->getPeakMemoryUsage();
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2012-04-27 05:36:48 +00:00
|
|
|
|
|
|
|
public function getCPUUsage() {
|
|
|
|
return $this->sandbox->getCPUUsage();
|
|
|
|
}
|
2012-09-05 09:39:58 +00:00
|
|
|
|
|
|
|
public function getProfilerFunctionReport( $units ) {
|
|
|
|
if ( $this->profilerEnabled ) {
|
2012-09-25 04:55:11 +00:00
|
|
|
static $unitsMap;
|
|
|
|
if ( !$unitsMap ) {
|
|
|
|
$unitsMap = array(
|
|
|
|
self::SAMPLES => LuaSandbox::SAMPLES,
|
|
|
|
self::SECONDS => LuaSandbox::SECONDS,
|
|
|
|
self::PERCENT => LuaSandbox::PERCENT );
|
|
|
|
}
|
|
|
|
return $this->sandbox->getProfilerFunctionReport( $unitsMap[$units] );
|
2012-09-05 09:39:58 +00:00
|
|
|
} else {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
}
|
2013-03-06 23:28:24 +00:00
|
|
|
|
|
|
|
public function pauseUsageTimer() {
|
|
|
|
if ( is_callable( array( $this->sandbox, 'pauseUsageTimer' ) ) ) {
|
|
|
|
$this->sandbox->pauseUsageTimer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function unpauseUsageTimer() {
|
|
|
|
if ( is_callable( array( $this->sandbox, 'unpauseUsageTimer' ) ) ) {
|
|
|
|
$this->sandbox->unpauseUsageTimer();
|
|
|
|
}
|
|
|
|
}
|
2012-04-13 10:38:12 +00:00
|
|
|
}
|
2012-04-05 07:58:02 +00:00
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
class Scribunto_LuaSandboxCallback {
|
|
|
|
function __construct( $callback ) {
|
|
|
|
$this->callback = $callback;
|
2012-04-05 07:58:02 +00:00
|
|
|
}
|
2012-01-28 16:22:18 +00:00
|
|
|
|
2012-09-05 09:39:58 +00:00
|
|
|
/**
|
2014-07-07 18:46:59 +00:00
|
|
|
* We use __call with a variable function name so that LuaSandbox will be
|
2012-09-05 09:39:58 +00:00
|
|
|
* able to return a meaningful function name in profiling data.
|
|
|
|
*/
|
|
|
|
function __call( $funcName, $args ) {
|
2012-01-28 16:22:18 +00:00
|
|
|
try {
|
2012-04-13 10:38:12 +00:00
|
|
|
return call_user_func_array( $this->callback, $args );
|
|
|
|
} catch ( Scribunto_LuaError $e ) {
|
|
|
|
throw new LuaSandboxRuntimeError( $e->getLuaMessage() );
|
2012-04-04 06:10:32 +00:00
|
|
|
}
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
|
|
|
}
|