2012-12-13 18:53:29 +00:00
|
|
|
<?php
|
|
|
|
|
2013-11-01 15:53:56 +00:00
|
|
|
/**
|
|
|
|
* @group Lua
|
|
|
|
* @group LuaSandbox
|
|
|
|
* @group LuaStandalone
|
|
|
|
*/
|
2012-12-13 18:53:29 +00:00
|
|
|
class Scribunto_LuaEnvironmentComparisonTest extends MediaWikiTestCase {
|
2014-08-17 21:11:26 +00:00
|
|
|
public $sandboxOpts = array(
|
2012-12-13 18:53:29 +00:00
|
|
|
'memoryLimit' => 50000000,
|
|
|
|
'cpuLimit' => 30,
|
|
|
|
'allowEnvFuncs' => true,
|
|
|
|
);
|
2014-08-17 21:11:26 +00:00
|
|
|
public $standaloneOpts = array(
|
2012-12-13 18:53:29 +00:00
|
|
|
'errorFile' => null,
|
|
|
|
'luaPath' => null,
|
|
|
|
'memoryLimit' => 50000000,
|
|
|
|
'cpuLimit' => 30,
|
|
|
|
'allowEnvFuncs' => true,
|
|
|
|
);
|
|
|
|
|
|
|
|
protected $engines = array();
|
|
|
|
|
2014-11-12 11:43:44 +00:00
|
|
|
private function makeEngine( $class, $opts ) {
|
2012-12-13 18:53:29 +00:00
|
|
|
$parser = new Parser;
|
|
|
|
$options = new ParserOptions;
|
|
|
|
$options->setTemplateCallback( array( $this, 'templateCallback' ) );
|
|
|
|
$parser->startExternalParse( Title::newMainPage(), $options, Parser::OT_HTML, true );
|
2013-03-06 23:17:20 +00:00
|
|
|
$engine = new $class ( array( 'parser' => $parser ) + $opts );
|
|
|
|
$parser->scribunto_engine = $engine;
|
|
|
|
$engine->setTitle( $parser->getTitle() );
|
|
|
|
$engine->getInterpreter();
|
|
|
|
return $engine;
|
|
|
|
}
|
|
|
|
|
2014-11-12 11:21:38 +00:00
|
|
|
protected function setUp() {
|
2013-03-06 23:17:20 +00:00
|
|
|
parent::setUp();
|
2012-12-13 18:53:29 +00:00
|
|
|
|
|
|
|
try {
|
2013-03-06 23:17:20 +00:00
|
|
|
$this->engines['LuaSandbox'] = $this->makeEngine(
|
|
|
|
'Scribunto_LuaSandboxEngine', $this->sandboxOpts
|
2012-12-13 18:53:29 +00:00
|
|
|
);
|
|
|
|
} catch ( Scribunto_LuaInterpreterNotFoundError $e ) {
|
|
|
|
$this->markTestSkipped( "LuaSandbox interpreter not available" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2013-03-06 23:17:20 +00:00
|
|
|
$this->engines['LuaStandalone'] = $this->makeEngine(
|
|
|
|
'Scribunto_LuaStandaloneEngine', $this->standaloneOpts
|
2012-12-13 18:53:29 +00:00
|
|
|
);
|
|
|
|
} catch ( Scribunto_LuaInterpreterNotFoundError $e ) {
|
|
|
|
$this->markTestSkipped( "LuaStandalone interpreter not available" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-12 11:21:38 +00:00
|
|
|
protected function tearDown() {
|
2012-12-13 18:53:29 +00:00
|
|
|
foreach ( $this->engines as $engine ) {
|
|
|
|
$engine->destroy();
|
|
|
|
}
|
|
|
|
$this->engines = array();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getGlobalEnvironment( $engine ) {
|
|
|
|
static $script = <<<LUA
|
|
|
|
xxxseen = {}
|
|
|
|
function xxxGetTable( t )
|
|
|
|
if xxxseen[t] then
|
2012-12-24 14:31:50 +00:00
|
|
|
return 'table'
|
2012-12-13 18:53:29 +00:00
|
|
|
end
|
|
|
|
local ret = {}
|
|
|
|
xxxseen[t] = ret
|
|
|
|
for k, v in pairs( t ) do
|
|
|
|
if k ~= '_G' and string.sub( k, 1, 3 ) ~= 'xxx' then
|
|
|
|
if type( v ) == 'table' then
|
|
|
|
ret[k] = xxxGetTable( v )
|
|
|
|
elseif type( v ) == 'string'
|
|
|
|
or type( v ) == 'number'
|
|
|
|
or type( v ) == 'boolean'
|
|
|
|
or type( v ) == 'nil'
|
|
|
|
then
|
|
|
|
ret[k] = v
|
|
|
|
else
|
|
|
|
ret[k] = type( v )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
return xxxGetTable( _G )
|
|
|
|
LUA;
|
|
|
|
$func = $engine->getInterpreter()->loadString( $script, 'script' );
|
|
|
|
return $engine->getInterpreter()->callFunction( $func );
|
|
|
|
}
|
|
|
|
|
2014-11-12 11:43:44 +00:00
|
|
|
public function testGlobalEnvironment() {
|
2012-12-13 18:53:29 +00:00
|
|
|
// Grab the first engine as the "standard"
|
|
|
|
reset( $this->engines );
|
|
|
|
list( $firstName, $firstEngine ) = each( $this->engines );
|
|
|
|
$firstEnv = $this->getGlobalEnvironment( $firstEngine );
|
|
|
|
|
|
|
|
// Test all others against it
|
|
|
|
while ( list( $secondName, $secondEngine ) = each( $this->engines ) ) {
|
|
|
|
$secondEnv = $this->getGlobalEnvironment( $secondEngine );
|
|
|
|
$this->assertEquals( $firstEnv, $secondEnv,
|
|
|
|
"Environments for $firstName and $secondName are not equivalent" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|