2012-04-19 07:40:56 +00:00
|
|
|
<?php
|
|
|
|
|
2019-07-22 00:49:41 +00:00
|
|
|
abstract class Scribunto_LuaInterpreterTest extends PHPUnit\Framework\TestCase {
|
|
|
|
use MediaWikiCoversValidator;
|
|
|
|
|
2018-12-05 14:56:06 +00:00
|
|
|
/**
|
|
|
|
* @return Scribunto_LuaInterpreter
|
|
|
|
*/
|
2017-06-15 17:19:00 +00:00
|
|
|
abstract protected function newInterpreter( $opts = [] );
|
2014-07-07 18:46:59 +00:00
|
|
|
|
2019-10-11 18:31:29 +00:00
|
|
|
protected function setUp() : void {
|
2012-12-20 16:36:48 +00:00
|
|
|
parent::setUp();
|
2012-04-19 07:40:56 +00:00
|
|
|
try {
|
|
|
|
$this->newInterpreter();
|
|
|
|
} catch ( Scribunto_LuaInterpreterNotFoundError $e ) {
|
|
|
|
$this->markTestSkipped( "interpreter not available" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-12 11:43:44 +00:00
|
|
|
protected function getBusyLoop( $interpreter ) {
|
2012-04-19 07:40:56 +00:00
|
|
|
$chunk = $interpreter->loadString( '
|
|
|
|
local args = {...}
|
|
|
|
local x, i
|
|
|
|
local s = string.rep("x", 1000000)
|
|
|
|
local n = args[1]
|
2019-06-12 14:30:47 +00:00
|
|
|
local e = args[2] and os.clock() + args[2] or nil
|
2012-04-19 07:40:56 +00:00
|
|
|
for i = 1, n do
|
|
|
|
x = x or string.find(s, "y", 1, true)
|
2019-06-12 14:30:47 +00:00
|
|
|
if e and os.clock() >= e then break end
|
2014-07-07 18:46:59 +00:00
|
|
|
end',
|
2012-04-19 07:40:56 +00:00
|
|
|
'busy' );
|
|
|
|
return $chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @dataProvider provideRoundtrip */
|
2014-11-12 11:43:44 +00:00
|
|
|
public function testRoundtrip( /*...*/ ) {
|
2012-04-19 07:40:56 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
$args = $this->normalizeOrder( $args );
|
|
|
|
$interpreter = $this->newInterpreter();
|
|
|
|
$passthru = $interpreter->loadString( 'return ...', 'passthru' );
|
2018-06-08 07:56:03 +00:00
|
|
|
$ret = $interpreter->callFunction( $passthru, ...$args );
|
2012-04-19 07:40:56 +00:00
|
|
|
$ret = $this->normalizeOrder( $ret );
|
|
|
|
$this->assertSame( $args, $ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @dataProvider provideRoundtrip */
|
2014-11-12 11:43:44 +00:00
|
|
|
public function testDoubleRoundtrip( /* ... */ ) {
|
2012-04-19 07:40:56 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
$args = $this->normalizeOrder( $args );
|
|
|
|
|
|
|
|
$interpreter = $this->newInterpreter();
|
|
|
|
$interpreter->registerLibrary( 'test',
|
2017-06-15 17:19:00 +00:00
|
|
|
[ 'passthru' => [ $this, 'passthru' ] ] );
|
2014-07-07 18:46:59 +00:00
|
|
|
$doublePassthru = $interpreter->loadString(
|
2012-04-19 07:40:56 +00:00
|
|
|
'return test.passthru(...)', 'doublePassthru' );
|
|
|
|
|
|
|
|
$finalArgs = $args;
|
|
|
|
array_unshift( $finalArgs, $doublePassthru );
|
2018-06-08 07:56:03 +00:00
|
|
|
$ret = $interpreter->callFunction( ...$finalArgs );
|
2012-04-19 07:40:56 +00:00
|
|
|
$ret = $this->normalizeOrder( $ret );
|
|
|
|
$this->assertSame( $args, $ret );
|
|
|
|
}
|
|
|
|
|
2012-12-13 18:37:09 +00:00
|
|
|
/**
|
|
|
|
* This cannot be done in testRoundtrip and testDoubleRoundtrip, because
|
|
|
|
* assertSame( NAN, NAN ) returns false.
|
|
|
|
*/
|
2014-11-12 11:43:44 +00:00
|
|
|
public function testRoundtripNAN() {
|
2012-12-13 18:37:09 +00:00
|
|
|
$interpreter = $this->newInterpreter();
|
|
|
|
|
|
|
|
$passthru = $interpreter->loadString( 'return ...', 'passthru' );
|
|
|
|
$ret = $interpreter->callFunction( $passthru, NAN );
|
2013-02-26 00:56:15 +00:00
|
|
|
$this->assertTrue( is_nan( $ret[0] ), 'NaN was not passed through' );
|
2012-12-13 18:37:09 +00:00
|
|
|
|
|
|
|
$interpreter->registerLibrary( 'test',
|
2017-06-15 17:19:00 +00:00
|
|
|
[ 'passthru' => [ $this, 'passthru' ] ] );
|
2012-12-13 18:37:09 +00:00
|
|
|
$doublePassthru = $interpreter->loadString(
|
|
|
|
'return test.passthru(...)', 'doublePassthru' );
|
|
|
|
$ret = $interpreter->callFunction( $doublePassthru, NAN );
|
2013-02-26 00:56:15 +00:00
|
|
|
$this->assertTrue( is_nan( $ret[0] ), 'NaN was not double passed through' );
|
2012-12-13 18:37:09 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 11:43:44 +00:00
|
|
|
private function normalizeOrder( $a ) {
|
2012-04-19 07:40:56 +00:00
|
|
|
ksort( $a );
|
|
|
|
foreach ( $a as &$value ) {
|
|
|
|
if ( is_array( $value ) ) {
|
|
|
|
$value = $this->normalizeOrder( $value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
|
2014-11-12 11:43:44 +00:00
|
|
|
public function passthru( /* ... */ ) {
|
2012-04-19 07:40:56 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
return $args;
|
|
|
|
}
|
|
|
|
|
2014-11-12 11:43:44 +00:00
|
|
|
public function provideRoundtrip() {
|
2017-06-15 17:19:00 +00:00
|
|
|
return [
|
|
|
|
[ 1 ],
|
|
|
|
[ true ],
|
|
|
|
[ false ],
|
|
|
|
[ 'hello' ],
|
|
|
|
[ implode( '', array_map( 'chr', range( 0, 255 ) ) ) ],
|
|
|
|
[ 1, 2, 3 ],
|
|
|
|
[ [] ],
|
|
|
|
[ [ 0 => 'foo', 1 => 'bar' ] ],
|
|
|
|
[ [ 1 => 'foo', 2 => 'bar' ] ],
|
|
|
|
[ [ 'x' => 'foo', 'y' => 'bar', 'z' => [] ] ],
|
|
|
|
[ INF ],
|
|
|
|
[ -INF ],
|
|
|
|
[ 'ok', null, 'ok' ],
|
|
|
|
[ null, 'ok' ],
|
|
|
|
[ 'ok', null ],
|
|
|
|
[ null ],
|
|
|
|
];
|
2012-04-19 07:40:56 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 11:43:44 +00:00
|
|
|
public function testTimeLimit() {
|
2015-06-21 04:38:39 +00:00
|
|
|
if ( php_uname( 's' ) === 'Darwin' ) {
|
2012-09-13 00:44:06 +00:00
|
|
|
$this->markTestSkipped( "Darwin is lacking POSIX timer, skipping CPU time limiting test." );
|
|
|
|
}
|
|
|
|
|
2019-07-22 13:07:57 +00:00
|
|
|
$interpreter = $this->newInterpreter( [ 'cpuLimit' => 1 ] );
|
2012-04-19 07:40:56 +00:00
|
|
|
$chunk = $this->getBusyLoop( $interpreter );
|
2015-02-19 22:12:49 +00:00
|
|
|
try {
|
2019-06-12 14:30:47 +00:00
|
|
|
$interpreter->callFunction(
|
|
|
|
$chunk,
|
|
|
|
1e9, // Arbitrary large quantity of work for the loop
|
2019-07-22 13:07:57 +00:00
|
|
|
2 // Early termination condition: 1 second CPU limit plus 1 second "fudge factor"
|
2019-06-12 14:30:47 +00:00
|
|
|
);
|
2015-02-19 22:12:49 +00:00
|
|
|
$this->fail( "Expected ScribuntoException was not thrown" );
|
|
|
|
} catch ( ScribuntoException $ex ) {
|
|
|
|
$this->assertSame( 'scribunto-common-timeout', $ex->messageName );
|
|
|
|
}
|
2012-04-19 07:40:56 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 11:43:44 +00:00
|
|
|
public function testTestMemoryLimit() {
|
2017-06-15 17:19:00 +00:00
|
|
|
$interpreter = $this->newInterpreter( [ 'memoryLimit' => 20 * 1e6 ] );
|
2012-04-19 07:40:56 +00:00
|
|
|
$chunk = $interpreter->loadString( '
|
|
|
|
t = {}
|
|
|
|
for i = 1, 10 do
|
|
|
|
t[#t + 1] = string.rep("x" .. i, 1000000)
|
|
|
|
end
|
|
|
|
',
|
|
|
|
'memoryLimit' );
|
2015-02-19 22:12:49 +00:00
|
|
|
try {
|
|
|
|
$interpreter->callFunction( $chunk );
|
|
|
|
$this->fail( "Expected ScribuntoException was not thrown" );
|
|
|
|
} catch ( ScribuntoException $ex ) {
|
|
|
|
$this->assertSame( 'scribunto-lua-error', $ex->messageName );
|
|
|
|
$this->assertSame( 'not enough memory', $ex->messageArgs[1] );
|
|
|
|
}
|
2012-04-19 07:40:56 +00:00
|
|
|
}
|
2012-12-13 20:50:44 +00:00
|
|
|
|
2014-11-12 11:43:44 +00:00
|
|
|
public function testWrapPHPFunction() {
|
2012-12-13 20:50:44 +00:00
|
|
|
$interpreter = $this->newInterpreter();
|
|
|
|
$func = $interpreter->wrapPhpFunction( function ( $n ) {
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ 42, $n ];
|
2012-12-13 20:50:44 +00:00
|
|
|
} );
|
|
|
|
$res = $interpreter->callFunction( $func, 'From PHP' );
|
2017-06-15 17:19:00 +00:00
|
|
|
$this->assertEquals( [ 42, 'From PHP' ], $res );
|
2012-12-13 20:50:44 +00:00
|
|
|
|
|
|
|
$chunk = $interpreter->loadString( '
|
|
|
|
f = ...
|
|
|
|
return f( "From Lua" )
|
|
|
|
',
|
|
|
|
'wrappedPhpFunction' );
|
|
|
|
$res = $interpreter->callFunction( $chunk, $func );
|
2017-06-15 17:19:00 +00:00
|
|
|
$this->assertEquals( [ 42, 'From Lua' ], $res );
|
2012-12-13 20:50:44 +00:00
|
|
|
}
|
2018-12-05 14:56:06 +00:00
|
|
|
|
|
|
|
public function testRegisterInterfaceWithSameName() {
|
|
|
|
$interpreter = $this->newInterpreter();
|
|
|
|
$test1Called = false;
|
|
|
|
$test2Called = false;
|
|
|
|
|
|
|
|
// Like a first call to Scribunto_LuaEngine::registerInterface()
|
|
|
|
$interpreter->registerLibrary( 'mw_interface', [
|
|
|
|
'foo' => function ( $v ) use ( &$test1Called ) {
|
|
|
|
$test1Called = $v;
|
|
|
|
},
|
|
|
|
] );
|
|
|
|
$interpreter->callFunction(
|
|
|
|
$interpreter->loadString( 'test1 = mw_interface; mw_interface = nil', 'test' )
|
|
|
|
);
|
|
|
|
// Like a second call to Scribunto_LuaEngine::registerInterface()
|
|
|
|
$interpreter->registerLibrary( 'mw_interface', [
|
|
|
|
'foo' => function ( $v ) use ( &$test2Called ) {
|
|
|
|
$test2Called = $v;
|
|
|
|
},
|
|
|
|
] );
|
|
|
|
$interpreter->callFunction(
|
|
|
|
$interpreter->loadString( 'test2 = mw_interface; mw_interface = nil', 'test' )
|
|
|
|
);
|
|
|
|
// Call both of the interfaces registered above.
|
|
|
|
$interpreter->callFunction(
|
|
|
|
$interpreter->loadString( 'test1.foo( "first" ); test2.foo( "second" )', 'test' )
|
|
|
|
);
|
|
|
|
$this->assertSame( 'first', $test1Called, 'test1.foo was called with "first"' );
|
|
|
|
$this->assertSame( 'second', $test2Called, 'test2.foo was called with "second"' );
|
|
|
|
}
|
|
|
|
|
2012-04-19 07:40:56 +00:00
|
|
|
}
|