Debian Codegraph @

extensions/Scribunto/tests/phpunit/Engines/LuaCommon/LuaInterpreterTestBase.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Scribunto\Tests\Engines\LuaCommon;
4
5 use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaInterpreter;
6 use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaInterpreterNotFoundError;
7 use MediaWiki\Extension\Scribunto\ScribuntoException;
8 use MediaWikiCoversValidator;
9 use PHPUnit\Framework\TestCase;
10
11 abstract class LuaInterpreterTestBase extends TestCase {
12 use MediaWikiCoversValidator;
13
14 /**
15 * @param array $opts
16 * @return LuaInterpreter
17 */
18 abstract protected function newInterpreter( $opts = [] );
19
20 protected function setUp(): void {
21 parent::setUp();
22 try {
23 $this->newInterpreter();
24 } catch ( LuaInterpreterNotFoundError $e ) {
25 $this->markTestSkipped( "interpreter not available" );
26 }
27 }
28
29 protected function getBusyLoop( $interpreter ) {
30 return $interpreter->loadString( '
31 local args = {...}
32 local x, i
33 local s = string.rep("x", 1000000)
34 local n = args[1]
35 local e = args[2] and os.clock() + args[2] or nil
36 for i = 1, n do
37 x = x or string.find(s, "y", 1, true)
38 if e and os.clock() >= e then break end
39 end',
40 'busy' );
41 }
42
43 /** @dataProvider provideRoundtrip */
44 public function testRoundtrip( ...$args ) {
45 $args = $this->normalizeOrder( $args );
46 $interpreter = $this->newInterpreter();
47 $passthru = $interpreter->loadString( 'return ...', 'passthru' );
48 $ret = $interpreter->callFunction( $passthru, ...$args );
49 $ret = $this->normalizeOrder( $ret );
50 $this->assertSame( $args, $ret );
51 }
52
53 /** @dataProvider provideRoundtrip */
54 public function testDoubleRoundtrip( ...$args ) {
55 $args = $this->normalizeOrder( $args );
56
57 $interpreter = $this->newInterpreter();
58 $interpreter->registerLibrary( 'test',
59 [ 'passthru' => [ $this, 'passthru' ] ] );
60 $doublePassthru = $interpreter->loadString(
61 'return test.passthru(...)', 'doublePassthru' );
62
63 $finalArgs = $args;
64 array_unshift( $finalArgs, $doublePassthru );
65 $ret = $interpreter->callFunction( ...$finalArgs );
66 $ret = $this->normalizeOrder( $ret );
67 $this->assertSame( $args, $ret );
68 }
69
70 /**
71 * This cannot be done in testRoundtrip and testDoubleRoundtrip, because
72 * assertSame( NAN, NAN ) returns false.
73 */
74 public function testRoundtripNAN() {
75 $interpreter = $this->newInterpreter();
76
77 $passthru = $interpreter->loadString( 'return ...', 'passthru' );
78 $ret = $interpreter->callFunction( $passthru, NAN );
79 $this->assertNan( $ret[0], 'NaN was not passed through' );
80
81 $interpreter->registerLibrary( 'test',
82 [ 'passthru' => [ $this, 'passthru' ] ] );
83 $doublePassthru = $interpreter->loadString(
84 'return test.passthru(...)', 'doublePassthru' );
85 $ret = $interpreter->callFunction( $doublePassthru, NAN );
86 $this->assertNan( $ret[0], 'NaN was not double passed through' );
87 }
88
89 private function normalizeOrder( $a ) {
90 ksort( $a );
91 foreach ( $a as &$value ) {
92 if ( is_array( $value ) ) {
93 $value = $this->normalizeOrder( $value );
94 }
95 }
96 return $a;
97 }
98
99 public function passthru( ...$args ) {
100 return $args;
101 }
102
103 public static function provideRoundtrip() {
104 return [
105 [ 1 ],
106 [ true ],
107 [ false ],
108 [ 'hello' ],
109 [ implode( '', array_map( 'chr', range( 0, 255 ) ) ) ],
110 [ 1, 2, 3 ],
111 [ [] ],
112 [ [ 0 => 'foo', 1 => 'bar' ] ],
113 [ [ 1 => 'foo', 2 => 'bar' ] ],
114 [ [ 'x' => 'foo', 'y' => 'bar', 'z' => [] ] ],
115 [ INF ],
116 [ -INF ],
117 [ 'ok', null, 'ok' ],
118 [ null, 'ok' ],
119 [ 'ok', null ],
120 [ null ],
121 ];
122 }
123
124 public function testTimeLimit() {
125 if ( php_uname( 's' ) === 'Darwin' ) {
126 $this->markTestSkipped( "Darwin is lacking POSIX timer, skipping CPU time limiting test." );
127 }
128
129 $interpreter = $this->newInterpreter( [ 'cpuLimit' => 1 ] );
130 $chunk = $this->getBusyLoop( $interpreter );
131 try {
132 $interpreter->callFunction(
133 $chunk,
134 // Arbitrary large quantity of work for the loop
135 1e9,
136 // Early termination condition: 1 second CPU limit plus 1 second "fudge factor"
137 2
138 );
139 $this->fail( "Expected ScribuntoException was not thrown" );
140 } catch ( ScribuntoException $ex ) {
141 $this->assertSame( 'scribunto-common-timeout', $ex->messageName );
142 }
143 }
144
145 public function testTestMemoryLimit() {
146 $interpreter = $this->newInterpreter( [ 'memoryLimit' => 20 * 1e6 ] );
147 $chunk = $interpreter->loadString( '
148 t = {}
149 for i = 1, 10 do
150 t[#t + 1] = string.rep("x" .. i, 1000000)
151 end
152 ',
153 'memoryLimit' );
154 try {
155 $interpreter->callFunction( $chunk );
156 $this->fail( "Expected ScribuntoException was not thrown" );
157 } catch ( ScribuntoException $ex ) {
158 $this->assertSame( 'scribunto-lua-error', $ex->messageName );
159 $this->assertSame( 'not enough memory', $ex->messageArgs[1] );
160 }
161 }
162
163 public function testWrapPHPFunction() {
164 $interpreter = $this->newInterpreter();
165 $func = $interpreter->wrapPhpFunction( static function ( $n ) {
166 return [ 42, $n ];
167 } );
168 $res = $interpreter->callFunction( $func, 'From PHP' );
169 $this->assertEquals( [ 42, 'From PHP' ], $res );
170
171 $chunk = $interpreter->loadString( '
172 f = ...
173 return f( "From Lua" )
174 ',
175 'wrappedPhpFunction' );
176 $res = $interpreter->callFunction( $chunk, $func );
177 $this->assertEquals( [ 42, 'From Lua' ], $res );
178 }
179
180 public function testRegisterInterfaceWithSameName() {
181 $interpreter = $this->newInterpreter();
182 $test1Called = false;
183 $test2Called = false;
184
185 // Like a first call to LuaEngine::registerInterface()
186 $interpreter->registerLibrary( 'mw_interface', [
187 'foo' => static function ( $v ) use ( &$test1Called ) {
188 $test1Called = $v;
189 },
190 ] );
191 $interpreter->callFunction(
192 $interpreter->loadString( 'test1 = mw_interface; mw_interface = nil', 'test' )
193 );
194 // Like a second call to LuaEngine::registerInterface()
195 $interpreter->registerLibrary( 'mw_interface', [
196 'foo' => static function ( $v ) use ( &$test2Called ) {
197 $test2Called = $v;
198 },
199 ] );
200 $interpreter->callFunction(
201 $interpreter->loadString( 'test2 = mw_interface; mw_interface = nil', 'test' )
202 );
203 // Call both of the interfaces registered above.
204 $interpreter->callFunction(
205 $interpreter->loadString( 'test1.foo( "first" ); test2.foo( "second" )', 'test' )
206 );
207 $this->assertSame( 'first', $test1Called, 'test1.foo was called with "first"' );
208 $this->assertSame( 'second', $test2Called, 'test2.foo was called with "second"' );
209 }
210
211 }