2012-04-19 07:40:56 +00:00
|
|
|
<?php
|
|
|
|
|
2022-07-30 22:43:58 +00:00
|
|
|
namespace MediaWiki\Extension\Scribunto\Tests\Engines\LuaCommon;
|
|
|
|
|
2022-08-03 12:31:15 +00:00
|
|
|
use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaInterpreter;
|
|
|
|
use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaInterpreterNotFoundError;
|
2022-04-07 23:12:32 +00:00
|
|
|
use MediaWiki\Extension\Scribunto\ScribuntoException;
|
2022-07-30 22:43:58 +00:00
|
|
|
use MediaWikiCoversValidator;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2022-04-07 23:12:32 +00:00
|
|
|
|
2022-07-30 22:43:58 +00:00
|
|
|
abstract class LuaInterpreterTest extends TestCase {
|
2019-07-22 00:49:41 +00:00
|
|
|
use MediaWikiCoversValidator;
|
|
|
|
|
2018-12-05 14:56:06 +00:00
|
|
|
/**
|
2021-01-23 01:33:17 +00:00
|
|
|
* @param array $opts
|
2022-08-03 12:31:15 +00:00
|
|
|
* @return LuaInterpreter
|
2018-12-05 14:56:06 +00:00
|
|
|
*/
|
2017-06-15 17:19:00 +00:00
|
|
|
abstract protected function newInterpreter( $opts = [] );
|
2014-07-07 18:46:59 +00:00
|
|
|
|
2021-07-23 10:49:13 +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();
|
2022-08-03 12:31:15 +00:00
|
|
|
} catch ( LuaInterpreterNotFoundError $e ) {
|
2012-04-19 07:40:56 +00:00
|
|
|
$this->markTestSkipped( "interpreter not available" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-12 11:43:44 +00:00
|
|
|
protected function getBusyLoop( $interpreter ) {
|
2022-04-16 21:09:10 +00:00
|
|
|
return $interpreter->loadString( '
|
2012-04-19 07:40:56 +00:00
|
|
|
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' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @dataProvider provideRoundtrip */
|
2020-06-27 11:45:24 +00:00
|
|
|
public function testRoundtrip( ...$args ) {
|
2012-04-19 07:40:56 +00:00
|
|
|
$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 */
|
2020-06-27 11:45:24 +00:00
|
|
|
public function testDoubleRoundtrip( ...$args ) {
|
2012-04-19 07:40:56 +00:00
|
|
|
$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 );
|
2022-01-10 09:45:50 +00:00
|
|
|
$this->assertNan( $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 );
|
2022-01-10 09:45:50 +00:00
|
|
|
$this->assertNan( $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;
|
|
|
|
}
|
|
|
|
|
2020-06-27 11:45:24 +00:00
|
|
|
public function passthru( ...$args ) {
|
2012-04-19 07:40:56 +00:00
|
|
|
return $args;
|
|
|
|
}
|
|
|
|
|
2023-05-20 17:35:54 +00:00
|
|
|
public static 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,
|
2022-07-30 18:46:09 +00:00
|
|
|
// Arbitrary large quantity of work for the loop
|
|
|
|
1e9,
|
|
|
|
// Early termination condition: 1 second CPU limit plus 1 second "fudge factor"
|
|
|
|
2
|
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();
|
2021-05-13 08:16:18 +00:00
|
|
|
$func = $interpreter->wrapPhpFunction( static 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;
|
|
|
|
|
2022-08-03 12:31:15 +00:00
|
|
|
// Like a first call to LuaEngine::registerInterface()
|
2018-12-05 14:56:06 +00:00
|
|
|
$interpreter->registerLibrary( 'mw_interface', [
|
2021-05-13 08:16:18 +00:00
|
|
|
'foo' => static function ( $v ) use ( &$test1Called ) {
|
2018-12-05 14:56:06 +00:00
|
|
|
$test1Called = $v;
|
|
|
|
},
|
|
|
|
] );
|
|
|
|
$interpreter->callFunction(
|
|
|
|
$interpreter->loadString( 'test1 = mw_interface; mw_interface = nil', 'test' )
|
|
|
|
);
|
2022-08-03 12:31:15 +00:00
|
|
|
// Like a second call to LuaEngine::registerInterface()
|
2018-12-05 14:56:06 +00:00
|
|
|
$interpreter->registerLibrary( 'mw_interface', [
|
2021-05-13 08:16:18 +00:00
|
|
|
'foo' => static function ( $v ) use ( &$test2Called ) {
|
2018-12-05 14:56:06 +00:00
|
|
|
$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
|
|
|
}
|