4 use MediaWikiCoversValidator;
5 use PHPUnit4And6Compat;
17 $this->markTestSkipped(
"interpreter not available" );
22 $chunk = $interpreter->loadString(
'
25 local s = string.rep("x", 1000000)
27 local e = args[2] and os.clock() + args[2] or nil
29 x = x or string.find(s, "y", 1, true)
30 if e and os.clock() >= e then break end
38 $args = func_get_args();
41 $passthru = $interpreter->loadString(
'return ...',
'passthru' );
42 $ret = $interpreter->callFunction( $passthru, ...
$args );
44 $this->assertSame(
$args, $ret );
49 $args = func_get_args();
53 $interpreter->registerLibrary(
'test',
54 [
'passthru' => [ $this,
'passthru' ] ] );
55 $doublePassthru = $interpreter->loadString(
56 'return test.passthru(...)',
'doublePassthru' );
59 array_unshift( $finalArgs, $doublePassthru );
60 $ret = $interpreter->callFunction( ...$finalArgs );
62 $this->assertSame(
$args, $ret );
72 $passthru = $interpreter->loadString(
'return ...',
'passthru' );
73 $ret = $interpreter->callFunction( $passthru, NAN );
74 $this->assertTrue( is_nan( $ret[0] ),
'NaN was not passed through' );
76 $interpreter->registerLibrary(
'test',
77 [
'passthru' => [ $this,
'passthru' ] ] );
78 $doublePassthru = $interpreter->loadString(
79 'return test.passthru(...)',
'doublePassthru' );
80 $ret = $interpreter->callFunction( $doublePassthru, NAN );
81 $this->assertTrue( is_nan( $ret[0] ),
'NaN was not double passed through' );
86 foreach ( $a as &$value ) {
87 if ( is_array( $value ) ) {
95 $args = func_get_args();
105 [ implode(
'', array_map(
'chr', range( 0, 255 ) ) ) ],
108 [ [ 0 =>
'foo', 1 =>
'bar' ] ],
109 [ [ 1 =>
'foo', 2 =>
'bar' ] ],
110 [ [
'x' =>
'foo',
'y' =>
'bar',
'z' => [] ] ],
113 [
'ok',
null,
'ok' ],
121 if ( php_uname(
's' ) ===
'Darwin' ) {
122 $this->markTestSkipped(
"Darwin is lacking POSIX timer, skipping CPU time limiting test." );
128 $interpreter->callFunction(
133 $this->fail(
"Expected ScribuntoException was not thrown" );
135 $this->assertSame(
'scribunto-common-timeout', $ex->messageName );
140 $interpreter = $this->
newInterpreter( [
'memoryLimit' => 20 * 1e6 ] );
141 $chunk = $interpreter->loadString(
'
144 t[#t + 1] = string.rep("x" .. i, 1000000)
149 $interpreter->callFunction( $chunk );
150 $this->fail(
"Expected ScribuntoException was not thrown" );
152 $this->assertSame(
'scribunto-lua-error', $ex->messageName );
153 $this->assertSame(
'not enough memory', $ex->messageArgs[1] );
159 $func = $interpreter->wrapPhpFunction(
function ( $n ) {
162 $res = $interpreter->callFunction( $func,
'From PHP' );
163 $this->assertEquals( [ 42,
'From PHP' ],
$res );
165 $chunk = $interpreter->loadString(
'
167 return f( "From Lua" )
169 'wrappedPhpFunction' );
170 $res = $interpreter->callFunction( $chunk, $func );
171 $this->assertEquals( [ 42,
'From Lua' ],
$res );
176 $test1Called =
false;
177 $test2Called =
false;
180 $interpreter->registerLibrary(
'mw_interface', [
181 'foo' =>
function ( $v ) use ( &$test1Called ) {
185 $interpreter->callFunction(
186 $interpreter->loadString(
'test1 = mw_interface; mw_interface = nil',
'test' )
189 $interpreter->registerLibrary(
'mw_interface', [
190 'foo' =>
function ( $v ) use ( &$test2Called ) {
194 $interpreter->callFunction(
195 $interpreter->loadString(
'test2 = mw_interface; mw_interface = nil',
'test' )
198 $interpreter->callFunction(
199 $interpreter->loadString(
'test1.foo( "first" ); test2.foo( "second" )',
'test' )
201 $this->assertSame(
'first', $test1Called,
'test1.foo was called with "first"' );
202 $this->assertSame(
'second', $test2Called,
'test2.foo was called with "second"' );