| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Scribunto\Engines\LuaStandalone; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaError; |
| 7 |
use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaInterpreter; |
| 8 |
use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaInterpreterNotExecutableError; |
| 9 |
use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaInterpreterNotFoundError; |
| 10 |
use MediaWiki\Extension\Scribunto\ScribuntoException; |
| 11 |
use Psr\Log\LoggerInterface; |
| 12 |
use Psr\Log\NullLogger; |
| 13 |
use RuntimeException; |
| 14 |
use UtfNormal\Validator; |
| 15 |
|
| 16 |
class LuaStandaloneInterpreter extends LuaInterpreter { |
| 17 |
|
| 18 |
protected static $nextInterpreterId = 0; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public $engine; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public $enableDebug; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public $proc; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public $writePipe; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public $readPipe; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public $exitError; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public $id; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
protected $logger; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
protected $callbacks; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function __construct( $engine, array $options ) { |
| 72 |
$this->id = self::$nextInterpreterId++; |
| 73 |
|
| 74 |
if ( $options['errorFile'] === null ) { |
| 75 |
$options['errorFile'] = wfGetNull(); |
| 76 |
} |
| 77 |
|
| 78 |
if ( $options['luaPath'] === null ) { |
| 79 |
$path = false; |
| 80 |
|
| 81 |
|
| 82 |
if ( PHP_OS == 'Linux' ) { |
| 83 |
if ( PHP_INT_SIZE == 4 ) { |
| 84 |
$path = 'lua5_1_5_linux_32_generic/lua'; |
| 85 |
} elseif ( PHP_INT_SIZE == 8 ) { |
| 86 |
$path = 'lua5_1_5_linux_64_generic/lua'; |
| 87 |
} |
| 88 |
} elseif ( PHP_OS == 'Windows' || PHP_OS == 'WINNT' || PHP_OS == 'Win32' ) { |
| 89 |
if ( PHP_INT_SIZE == 4 ) { |
| 90 |
$path = 'lua5_1_5_Win32_bin/lua5.1.exe'; |
| 91 |
} elseif ( PHP_INT_SIZE == 8 ) { |
| 92 |
$path = 'lua5_1_5_Win64_bin/lua5.1.exe'; |
| 93 |
} |
| 94 |
} elseif ( PHP_OS == 'Darwin' ) { |
| 95 |
$path = 'lua5_1_5_mac_lion_fat_generic/lua'; |
| 96 |
} |
| 97 |
if ( $path === false ) { |
| 98 |
throw new LuaInterpreterNotFoundError( |
| 99 |
'No Lua interpreter was given in the configuration, ' . |
| 100 |
'and no bundled binary exists for this platform.' ); |
| 101 |
} |
| 102 |
$options['luaPath'] = __DIR__ . "/binaries/$path"; |
| 103 |
|
| 104 |
if ( !is_executable( $options['luaPath'] ) ) { |
| 105 |
throw new LuaInterpreterNotExecutableError( |
| 106 |
sprintf( 'The lua binary (%s) is not executable.', $options['luaPath'] ) |
| 107 |
); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
$this->engine = $engine; |
| 112 |
$this->enableDebug = !empty( $options['debug'] ); |
| 113 |
$this->logger = $options['logger'] ?? new NullLogger(); |
| 114 |
|
| 115 |
$pipes = null; |
| 116 |
$cmd = wfEscapeShellArg( |
| 117 |
$options['luaPath'], |
| 118 |
__DIR__ . '/mw_main.lua', |
| 119 |
dirname( dirname( __DIR__ ) ), |
| 120 |
(string)$this->id, |
| 121 |
(string)PHP_INT_SIZE |
| 122 |
); |
| 123 |
if ( php_uname( 's' ) == 'Linux' ) { |
| 124 |
|
| 125 |
$cmd = wfEscapeShellArg( |
| 126 |
|
| 127 |
'exec', |
| 128 |
'/bin/sh', |
| 129 |
__DIR__ . '/lua_ulimit.sh', |
| 130 |
|
| 131 |
(string)$options['cpuLimit'], |
| 132 |
|
| 133 |
(string)( $options['cpuLimit'] + 1 ), |
| 134 |
(string)intval( $options['memoryLimit'] / 1024 ), |
| 135 |
$cmd ); |
| 136 |
} |
| 137 |
|
| 138 |
if ( php_uname( 's' ) == 'Windows NT' ) { |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
$cmd = '"' . $cmd . '"'; |
| 145 |
} |
| 146 |
|
| 147 |
$this->logger->debug( __METHOD__ . ": creating interpreter: $cmd" ); |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
if ( !function_exists( 'proc_open' ) ) { |
| 152 |
throw $this->engine->newException( 'scribunto-luastandalone-proc-error-proc-open' ); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
error_clear_last(); |
| 158 |
|
| 159 |
|
| 160 |
$this->proc = proc_open( |
| 161 |
$cmd, |
| 162 |
[ |
| 163 |
[ 'pipe', 'r' ], |
| 164 |
[ 'pipe', 'w' ], |
| 165 |
[ 'file', $options['errorFile'], 'a' ] |
| 166 |
], |
| 167 |
$pipes ); |
| 168 |
if ( !$this->proc ) { |
| 169 |
$err = error_get_last(); |
| 170 |
if ( !empty( $err['message'] ) ) { |
| 171 |
throw $this->engine->newException( 'scribunto-luastandalone-proc-error-msg', |
| 172 |
[ 'args' => [ $err['message'] ] ] ); |
| 173 |
} else { |
| 174 |
throw $this->engine->newException( 'scribunto-luastandalone-proc-error' ); |
| 175 |
} |
| 176 |
} |
| 177 |
$this->writePipe = $pipes[0]; |
| 178 |
$this->readPipe = $pipes[1]; |
| 179 |
} |
| 180 |
|
| 181 |
public function __destruct() { |
| 182 |
$this->terminate(); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public static function getLuaVersion( array $options ) { |
| 191 |
if ( $options['luaPath'] === null ) { |
| 192 |
|
| 193 |
if ( PHP_OS == 'Linux' ) { |
| 194 |
return 'Lua 5.1.5'; |
| 195 |
} elseif ( PHP_OS == 'Windows' || PHP_OS == 'WINNT' || PHP_OS == 'Win32' ) { |
| 196 |
return 'Lua 5.1.5'; |
| 197 |
} elseif ( PHP_OS == 'Darwin' ) { |
| 198 |
return 'Lua 5.1.5'; |
| 199 |
} else { |
| 200 |
return null; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
$cmd = wfEscapeShellArg( $options['luaPath'] ) . ' -v 2>&1'; |
| 209 |
|
| 210 |
$handle = popen( $cmd, 'r' ); |
| 211 |
if ( $handle ) { |
| 212 |
$ret = fgets( $handle, 80 ); |
| 213 |
pclose( $handle ); |
| 214 |
if ( $ret && preg_match( '/^Lua(?:JIT)? \S+/', $ret, $m ) ) { |
| 215 |
return $m[0]; |
| 216 |
} |
| 217 |
} |
| 218 |
return null; |
| 219 |
} |
| 220 |
|
| 221 |
public function terminate() { |
| 222 |
if ( $this->proc ) { |
| 223 |
$this->logger->debug( __METHOD__ . ": terminating" ); |
| 224 |
proc_terminate( $this->proc ); |
| 225 |
proc_close( $this->proc ); |
| 226 |
$this->proc = false; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
public function quit() { |
| 231 |
if ( !$this->proc ) { |
| 232 |
return; |
| 233 |
} |
| 234 |
$this->dispatch( [ 'op' => 'quit' ] ); |
| 235 |
proc_close( $this->proc ); |
| 236 |
} |
| 237 |
|
| 238 |
public function testquit() { |
| 239 |
if ( !$this->proc ) { |
| 240 |
return; |
| 241 |
} |
| 242 |
$this->dispatch( [ 'op' => 'testquit' ] ); |
| 243 |
proc_close( $this->proc ); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public function loadString( $text, $chunkName ) { |
| 252 |
$this->cleanupLuaChunks(); |
| 253 |
|
| 254 |
$result = $this->dispatch( [ |
| 255 |
'op' => 'loadString', |
| 256 |
'text' => $text, |
| 257 |
'chunkName' => $chunkName |
| 258 |
] ); |
| 259 |
return new LuaStandaloneInterpreterFunction( $this->id, $result[1] ); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
public function callFunction( $func, ...$args ) { |
| 264 |
if ( !( $func instanceof LuaStandaloneInterpreterFunction ) ) { |
| 265 |
throw new InvalidArgumentException( __METHOD__ . ': invalid function type' ); |
| 266 |
} |
| 267 |
if ( $func->interpreterId !== $this->id ) { |
| 268 |
throw new InvalidArgumentException( __METHOD__ . ': function belongs to a different interpreter' ); |
| 269 |
} |
| 270 |
$args = func_get_args(); |
| 271 |
unset( $args[0] ); |
| 272 |
|
| 273 |
|
| 274 |
$this->cleanupLuaChunks(); |
| 275 |
|
| 276 |
$result = $this->dispatch( [ |
| 277 |
'op' => 'call', |
| 278 |
'id' => $func->id, |
| 279 |
'nargs' => count( $args ), |
| 280 |
'args' => $args, |
| 281 |
] ); |
| 282 |
|
| 283 |
return array_values( $result ); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
public function wrapPhpFunction( $callable ) { |
| 288 |
static $uid = 0; |
| 289 |
$id = "anonymous*" . ++$uid; |
| 290 |
$this->callbacks[$id] = $callable; |
| 291 |
$ret = $this->dispatch( [ |
| 292 |
'op' => 'wrapPhpFunction', |
| 293 |
'id' => $id, |
| 294 |
] ); |
| 295 |
return $ret[1]; |
| 296 |
} |
| 297 |
|
| 298 |
public function cleanupLuaChunks() { |
| 299 |
if ( isset( LuaStandaloneInterpreterFunction::$anyChunksDestroyed[$this->id] ) ) { |
| 300 |
unset( LuaStandaloneInterpreterFunction::$anyChunksDestroyed[$this->id] ); |
| 301 |
$this->dispatch( [ |
| 302 |
'op' => 'cleanupChunks', |
| 303 |
'ids' => LuaStandaloneInterpreterFunction::$activeChunkIds[$this->id] |
| 304 |
] ); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
public function isLuaFunction( $object ) { |
| 310 |
return $object instanceof LuaStandaloneInterpreterFunction; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
public function registerLibrary( $name, array $functions ) { |
| 315 |
|
| 316 |
|
| 317 |
static $uid = 0; |
| 318 |
$uid++; |
| 319 |
|
| 320 |
$functionIds = []; |
| 321 |
foreach ( $functions as $funcName => $callback ) { |
| 322 |
$id = "$name-$funcName-$uid"; |
| 323 |
$this->callbacks[$id] = $callback; |
| 324 |
$functionIds[$funcName] = $id; |
| 325 |
} |
| 326 |
$this->dispatch( [ |
| 327 |
'op' => 'registerLibrary', |
| 328 |
'name' => $name, |
| 329 |
'functions' => $functionIds, |
| 330 |
] ); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
public function getStatus() { |
| 338 |
$result = $this->dispatch( [ |
| 339 |
'op' => 'getStatus', |
| 340 |
] ); |
| 341 |
return $result[1]; |
| 342 |
} |
| 343 |
|
| 344 |
public function pauseUsageTimer() { |
| 345 |
} |
| 346 |
|
| 347 |
public function unpauseUsageTimer() { |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
private static function fixNulls( array $array, $count ) { |
| 358 |
if ( count( $array ) === $count ) { |
| 359 |
return $array; |
| 360 |
} else { |
| 361 |
return array_replace( array_fill( 1, $count, null ), $array ); |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
protected function handleCall( $message ) { |
| 371 |
$message['args'] = self::fixNulls( $message['args'], $message['nargs'] ); |
| 372 |
try { |
| 373 |
$result = $this->callback( $message['id'], $message['args'] ); |
| 374 |
} catch ( LuaError $e ) { |
| 375 |
return [ |
| 376 |
'op' => 'error', |
| 377 |
'value' => $e->getLuaMessage(), |
| 378 |
]; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
if ( $result !== null && count( $result ) ) { |
| 383 |
$result = array_combine( range( 1, count( $result ) ), $result ); |
| 384 |
} else { |
| 385 |
$result = []; |
| 386 |
} |
| 387 |
|
| 388 |
return [ |
| 389 |
'op' => 'return', |
| 390 |
'nvalues' => count( $result ), |
| 391 |
'values' => $result |
| 392 |
]; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
protected function callback( $id, array $args ) { |
| 402 |
return ( $this->callbacks[$id] )( ...$args ); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
protected function handleError( $message ) { |
| 414 |
$opts = []; |
| 415 |
$message['value'] = Validator::cleanUp( $message['value'] ); |
| 416 |
if ( preg_match( '/^(.*?):(\d+): (.*)$/', $message['value'], $m ) ) { |
| 417 |
$opts['module'] = $m[1]; |
| 418 |
$opts['line'] = $m[2]; |
| 419 |
$message['value'] = $m[3]; |
| 420 |
} |
| 421 |
if ( isset( $message['trace'] ) ) { |
| 422 |
foreach ( $message['trace'] as &$val ) { |
| 423 |
$val = array_map( static function ( $val ) { |
| 424 |
if ( is_string( $val ) ) { |
| 425 |
$val = Validator::cleanUp( $val ); |
| 426 |
} |
| 427 |
return $val; |
| 428 |
}, $val ); |
| 429 |
} |
| 430 |
$opts['trace'] = array_values( $message['trace'] ); |
| 431 |
} |
| 432 |
throw $this->engine->newLuaError( $message['value'], $opts ); |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
protected function dispatch( $msgToLua ) { |
| 441 |
$this->sendMessage( $msgToLua ); |
| 442 |
while ( true ) { |
| 443 |
$msgFromLua = $this->receiveMessage(); |
| 444 |
|
| 445 |
switch ( $msgFromLua['op'] ) { |
| 446 |
case 'return': |
| 447 |
return self::fixNulls( $msgFromLua['values'], $msgFromLua['nvalues'] ); |
| 448 |
case 'call': |
| 449 |
$msgToLua = $this->handleCall( $msgFromLua ); |
| 450 |
$this->sendMessage( $msgToLua ); |
| 451 |
break; |
| 452 |
case 'error': |
| 453 |
$this->handleError( $msgFromLua ); |
| 454 |
|
| 455 |
default: |
| 456 |
$this->logger->error( __METHOD__ . ": invalid response op \"{$msgFromLua['op']}\"" ); |
| 457 |
throw $this->engine->newException( 'scribunto-luastandalone-decode-error' ); |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
protected function sendMessage( $msg ) { |
| 467 |
$this->debug( "TX ==> {$msg['op']}" ); |
| 468 |
$this->checkValid(); |
| 469 |
|
| 470 |
$encMsg = $this->encodeMessage( $msg ); |
| 471 |
if ( !fwrite( $this->writePipe, $encMsg ) ) { |
| 472 |
|
| 473 |
|
| 474 |
$this->handleIOError(); |
| 475 |
throw $this->engine->newException( 'scribunto-luastandalone-write-error' ); |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
protected function receiveMessage() { |
| 484 |
$this->checkValid(); |
| 485 |
|
| 486 |
$header = fread( $this->readPipe, 16 ); |
| 487 |
if ( strlen( $header ) !== 16 ) { |
| 488 |
$this->handleIOError(); |
| 489 |
throw $this->engine->newException( 'scribunto-luastandalone-read-error' ); |
| 490 |
} |
| 491 |
$length = $this->decodeHeader( $header ); |
| 492 |
|
| 493 |
|
| 494 |
$body = ''; |
| 495 |
$lengthRemaining = $length; |
| 496 |
while ( $lengthRemaining ) { |
| 497 |
$buffer = fread( $this->readPipe, $lengthRemaining ); |
| 498 |
if ( $buffer === false || feof( $this->readPipe ) ) { |
| 499 |
$this->handleIOError(); |
| 500 |
throw $this->engine->newException( 'scribunto-luastandalone-read-error' ); |
| 501 |
} |
| 502 |
$body .= $buffer; |
| 503 |
$lengthRemaining -= strlen( $buffer ); |
| 504 |
} |
| 505 |
$body = strtr( $body, [ |
| 506 |
'\\r' => "\r", |
| 507 |
'\\n' => "\n", |
| 508 |
'\\\\' => '\\', |
| 509 |
] ); |
| 510 |
$msg = unserialize( $body ); |
| 511 |
$this->debug( "RX <== {$msg['op']}" ); |
| 512 |
return $msg; |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
protected function encodeMessage( $message ) { |
| 521 |
$serialized = $this->encodeLuaVar( $message ); |
| 522 |
$length = strlen( $serialized ); |
| 523 |
$check = $length * 2 - 1; |
| 524 |
|
| 525 |
return sprintf( '%08x%08x%s', $length, $check, $serialized ); |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
protected function encodeLuaVar( $var, $level = 0 ) { |
| 535 |
if ( $level > 100 ) { |
| 536 |
throw new RuntimeException( __METHOD__ . ': recursion depth limit exceeded' ); |
| 537 |
} |
| 538 |
$type = gettype( $var ); |
| 539 |
switch ( $type ) { |
| 540 |
case 'boolean': |
| 541 |
return $var ? 'true' : 'false'; |
| 542 |
case 'integer': |
| 543 |
return $var; |
| 544 |
case 'double': |
| 545 |
if ( !is_finite( $var ) ) { |
| 546 |
if ( is_nan( $var ) ) { |
| 547 |
return '(0/0)'; |
| 548 |
} |
| 549 |
if ( $var === INF ) { |
| 550 |
return '(1/0)'; |
| 551 |
} |
| 552 |
if ( $var === -INF ) { |
| 553 |
return '(-1/0)'; |
| 554 |
} |
| 555 |
throw new InvalidArgumentException( __METHOD__ . ': cannot convert non-finite number' ); |
| 556 |
} |
| 557 |
return sprintf( '%.17g', $var ); |
| 558 |
case 'string': |
| 559 |
return '"' . |
| 560 |
strtr( $var, [ |
| 561 |
'"' => '\\"', |
| 562 |
'\\' => '\\\\', |
| 563 |
"\n" => '\\n', |
| 564 |
"\r" => '\\r', |
| 565 |
"\000" => '\\000', |
| 566 |
] ) . |
| 567 |
'"'; |
| 568 |
case 'array': |
| 569 |
$s = '{'; |
| 570 |
foreach ( $var as $key => $element ) { |
| 571 |
if ( $s !== '{' ) { |
| 572 |
$s .= ','; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
if ( is_int( $key ) && ( $key > 9007199254740992 || $key < -9007199254740992 ) ) { |
| 577 |
$key = (string)$key; |
| 578 |
} |
| 579 |
|
| 580 |
$s .= '[' . $this->encodeLuaVar( $key, $level + 1 ) . ']' . |
| 581 |
'=' . $this->encodeLuaVar( $element, $level + 1 ); |
| 582 |
} |
| 583 |
$s .= '}'; |
| 584 |
return $s; |
| 585 |
case 'object': |
| 586 |
if ( !( $var instanceof LuaStandaloneInterpreterFunction ) ) { |
| 587 |
throw new InvalidArgumentException( __METHOD__ . ': unable to convert object of type ' . |
| 588 |
get_class( $var ) ); |
| 589 |
} elseif ( $var->interpreterId !== $this->id ) { |
| 590 |
throw new InvalidArgumentException( |
| 591 |
__METHOD__ . ': unable to convert function belonging to a different interpreter' |
| 592 |
); |
| 593 |
} else { |
| 594 |
return 'chunks[' . intval( $var->id ) . ']'; |
| 595 |
} |
| 596 |
case 'resource': |
| 597 |
throw new InvalidArgumentException( __METHOD__ . ': unable to convert resource' ); |
| 598 |
case 'NULL': |
| 599 |
return 'nil'; |
| 600 |
default: |
| 601 |
throw new InvalidArgumentException( __METHOD__ . ': unable to convert variable of unknown type' ); |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
protected function decodeHeader( $header ) { |
| 611 |
$length = substr( $header, 0, 8 ); |
| 612 |
$check = substr( $header, 8, 8 ); |
| 613 |
if ( !preg_match( '/^[0-9a-f]+$/', $length ) || !preg_match( '/^[0-9a-f]+$/', $check ) ) { |
| 614 |
throw $this->engine->newException( 'scribunto-luastandalone-decode-error' ); |
| 615 |
} |
| 616 |
$length = hexdec( $length ); |
| 617 |
$check = hexdec( $check ); |
| 618 |
if ( $length * 2 - 1 !== $check ) { |
| 619 |
throw $this->engine->newException( 'scribunto-luastandalone-decode-error' ); |
| 620 |
} |
| 621 |
return $length; |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
protected function checkValid() { |
| 628 |
if ( !$this->proc ) { |
| 629 |
$this->logger->error( __METHOD__ . ": process already terminated" ); |
| 630 |
if ( $this->exitError ) { |
| 631 |
throw $this->exitError; |
| 632 |
} else { |
| 633 |
throw $this->engine->newException( 'scribunto-luastandalone-gone' ); |
| 634 |
} |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
protected function handleIOError() { |
| 642 |
$this->checkValid(); |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
proc_terminate( $this->proc ); |
| 648 |
while ( true ) { |
| 649 |
$status = proc_get_status( $this->proc ); |
| 650 |
|
| 651 |
|
| 652 |
'@phan-var array|false $status'; |
| 653 |
if ( $status === false ) { |
| 654 |
|
| 655 |
return; |
| 656 |
} |
| 657 |
if ( !$status['running'] ) { |
| 658 |
break; |
| 659 |
} |
| 660 |
|
| 661 |
usleep( 10000 ); |
| 662 |
} |
| 663 |
proc_close( $this->proc ); |
| 664 |
$this->proc = false; |
| 665 |
|
| 666 |
|
| 667 |
if ( !$status['signaled'] && ( $status['exitcode'] & 0x80 ) === 0x80 ) { |
| 668 |
$status['signaled'] = true; |
| 669 |
$status['termsig'] = $status['exitcode'] - 128; |
| 670 |
} |
| 671 |
|
| 672 |
if ( $status['signaled'] ) { |
| 673 |
if ( defined( 'SIGXCPU' ) && $status['termsig'] === SIGXCPU ) { |
| 674 |
$this->exitError = $this->engine->newException( 'scribunto-common-timeout' ); |
| 675 |
} else { |
| 676 |
$this->exitError = $this->engine->newException( 'scribunto-luastandalone-signal', |
| 677 |
[ 'args' => [ $status['termsig'] ] ] ); |
| 678 |
} |
| 679 |
} else { |
| 680 |
$this->exitError = $this->engine->newException( 'scribunto-luastandalone-exited', |
| 681 |
[ 'args' => [ $status['exitcode'] ] ] ); |
| 682 |
} |
| 683 |
throw $this->exitError; |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
protected function debug( $msg ) { |
| 690 |
if ( $this->enableDebug ) { |
| 691 |
$this->logger->debug( "Lua: $msg" ); |
| 692 |
} |
| 693 |
} |
| 694 |
} |