7 require_once __DIR__ .
'/../LuaCommon/LuaInterpreterTest.php';
9 use Wikimedia\TestingAccessWrapper;
20 'memoryLimit' => 50000000,
26 return trim( $size ) * 1024;
38 $interpreter->testquit();
39 $this->fail(
'Expected exception not thrown' );
41 $this->assertSame(
'scribunto-luastandalone-exited', $ex->
getMessageName() );
42 $this->assertSame( [
'[UNKNOWN]', 42 ], $ex->messageArgs );
49 proc_terminate( $interpreter->proc, 15 );
51 $interpreter->loadString(
'return ...',
'test' );
52 $this->fail(
'Expected exception not thrown' );
54 $this->assertSame(
'scribunto-luastandalone-signal', $ex->
getMessageName() );
55 $this->assertSame( [
'[UNKNOWN]', 15 ], $ex->messageArgs );
60 $startTime = microtime(
true );
61 if ( php_uname(
's' ) !==
'Linux' ) {
62 $this->markTestSkipped(
"getStatus() not supported on platforms other than Linux" );
66 $engine = TestingAccessWrapper::newFromObject( $interpreter->engine );
67 $status = $interpreter->getStatus();
69 $this->assertInternalType(
'integer',
$status[
'pid'] );
70 $initialVsize = $this->
getVsize( $pid );
71 $this->assertGreaterThan( 0, $initialVsize,
'Initial vsize' );
75 while ( microtime(
true ) - $startTime < 1 ) {
76 $interpreter->callFunction( $chunk, 100 );
78 $status = $interpreter->getStatus();
80 $time =
$status[
'time'] / $engine->getClockTick();
81 $this->assertGreaterThan( 0.1, $time,
'getStatus() time usage' );
82 $this->assertLessThan( 1.5, $time,
'getStatus() time usage' );
83 $this->assertEquals( $vsize,
$status[
'vsize'],
'vsize', $vsize * 0.1 );
92 $ret = $interpreter->callFunction(
93 $interpreter->loadString(
94 'local t, r = ..., {}; for k, v in pairs( t ) do r[v] = type(k) end return r',
'test'
98 ksort( $ret[0], SORT_STRING );
99 $this->assertSame( $expect, $ret[0] );
103 if ( PHP_INT_MAX > 9007199254740992 ) {
105 '9007199254740992' =>
'max',
'9007199254740993' =>
'max+1',
106 '-9007199254740992' =>
'min',
'-9007199254740993' =>
'min-1',
110 '2147483647' =>
'max',
'2147483648' =>
'max+1',
111 '-2147483648' =>
'min',
'-2147483649' =>
'min-1',
116 'simple integers' => [
117 [ -10 =>
'minus ten', 0 =>
'zero', 10 =>
'ten' ],
118 [
'minus ten' =>
'number',
'ten' =>
'number',
'zero' =>
'number' ],
120 'maximal values' => [
122 [
'max' =>
'number',
'max+1' =>
'string',
'min' =>
'number',
'min-1' =>
'string' ],
131 if ( $expect instanceof Exception ) {
132 $this->setExpectedException( Scribunto_LuaError::class, $expect->getMessage() );
136 $ret = $interpreter->callFunction(
137 $interpreter->loadString(
"return { $lua }",
'test' )
139 if ( $expect instanceof Exception ) {
140 $this->fail(
'Expected exception not thrown' );
142 ksort( $ret[0], SORT_STRING );
143 $this->assertSame( $expect, $ret[0] );
147 if ( PHP_INT_MAX > 9007199254740992 ) {
148 $max =
'9223372036854774784';
149 $max2 =
'9223372036854775808';
150 $min =
'-9223372036854775808';
151 $min2 =
'-9223372036854775809';
154 $max2 =
'2147483648';
155 $min =
'-2147483648';
156 $min2 =
'-2147483649';
160 'simple integers' => [
161 '[-10] = "minus ten", [0] = "zero", [10] = "ten"',
162 [ -10 =>
'minus ten', 0 =>
'zero', 10 =>
'ten' ],
164 'stringified integers' => [
165 '["-10"] = "minus ten", ["0"] = "zero", ["10"] = "ten"',
166 [ -10 =>
'minus ten', 0 =>
'zero', 10 =>
'ten' ],
168 'maximal integers' => [
169 "['$max'] = 'near max', ['$max2'] = 'max+1', ['$min'] = 'min', ['$min2'] = 'min-1'",
170 [ $min =>
'min', $min2 =>
'min-1', $max =>
'near max', $max2 =>
'max+1' ],
173 '[0] = "number zero", ["0"] = "string zero"',
174 new Exception(
'Collision for array key 0 when passing data from Lua to PHP.' ),
176 'collision (float)' => [
177 '[1.5] = "number 1.5", ["1.5"] = "string 1.5"',
178 new Exception(
'Collision for array key 1.5 when passing data from Lua to PHP.' ),
180 'collision (inf)' => [
181 '[1/0] = "number inf", ["inf"] = "string inf"',
182 new Exception(
'Collision for array key inf when passing data from Lua to PHP.' ),
191 $ret = $interpreter->callFunction(
192 $interpreter->loadString(
'return function() return "testFreeFunction #1" end',
'test' )
195 $interpreter->cleanupLuaChunks();
197 [
'testFreeFunction #1' ], $interpreter->callFunction( $ret[0] ),
198 'Test that function #1 was not freed while a reference exists'
201 $interpreter->cleanupLuaChunks();
204 $interpreter->callFunction( $testfunc );
205 $this->fail(
"Expected exception because function #1 should have been freed" );
208 "function id $id does not exist", $e->messageArgs[1],
209 'Testing for expected error when calling a freed function #1'
214 $ret = $interpreter->callFunction(
215 $interpreter->loadString(
'return function() return "testFreeFunction #2" end',
'test' )
220 $interpreter->cleanupLuaChunks();
222 [
'testFreeFunction #2' ], $interpreter->callFunction( $func ),
223 'Test that function #2 was not freed while a reference exists'
226 $interpreter->cleanupLuaChunks();
229 $interpreter->callFunction( $testfunc );
230 $this->fail(
"Expected exception because function #2 should have been freed" );
233 "function id $id does not exist", $e->messageArgs[1],
234 'Testing for expected error when calling a freed function #2'
239 $ret = $interpreter->callFunction(
240 $interpreter->loadString(
'return function() return "testFreeFunction #3" end',
'test' )
243 $func = clone $ret[0];
245 $interpreter->cleanupLuaChunks();
247 [
'testFreeFunction #3' ], $interpreter->callFunction( $func ),
248 'Test that function #3 was not freed while a reference exists'
251 $interpreter->cleanupLuaChunks();
254 $interpreter->callFunction( $testfunc );
255 $this->fail(
"Expected exception because function #3 should have been freed" );
258 "function id $id does not exist", $e->messageArgs[1],
259 'Testing for expected error when calling a freed function #3'