| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon; |
| 4 |
|
| 5 |
use MediaWiki\Extension\Scribunto\ScribuntoException; |
| 6 |
use MediaWiki\Extension\Scribunto\ScribuntoModuleBase; |
| 7 |
use MediaWiki\Parser\PPFrame; |
| 8 |
use MediaWiki\Status\Status; |
| 9 |
|
| 10 |
class LuaModule extends ScribuntoModuleBase { |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
protected $initChunk; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
protected $engine; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public function __construct( LuaEngine $engine, $code, $chunkName ) { |
| 27 |
parent::__construct( $engine, $code, $chunkName ); |
| 28 |
} |
| 29 |
|
| 30 |
public function validate() { |
| 31 |
try { |
| 32 |
$this->getInitChunk(); |
| 33 |
} catch ( ScribuntoException $e ) { |
| 34 |
return $e->toStatus(); |
| 35 |
} |
| 36 |
return Status::newGood(); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function getInitChunk() { |
| 44 |
if ( !$this->initChunk ) { |
| 45 |
$this->initChunk = $this->engine->getInterpreter()->loadString( |
| 46 |
$this->code, |
| 47 |
|
| 48 |
'=' . $this->chunkName ); |
| 49 |
} |
| 50 |
return $this->initChunk; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function invoke( $name, $frame ) { |
| 62 |
$ret = $this->engine->executeModule( $this->getInitChunk(), $name, $frame ); |
| 63 |
|
| 64 |
if ( !isset( $ret ) ) { |
| 65 |
throw $this->engine->newException( |
| 66 |
'scribunto-common-nosuchfunction', [ 'args' => [ $name ] ] |
| 67 |
); |
| 68 |
} |
| 69 |
if ( !$this->engine->getInterpreter()->isLuaFunction( $ret ) ) { |
| 70 |
throw $this->engine->newException( |
| 71 |
'scribunto-common-notafunction', [ 'args' => [ $name ] ] |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
$result = $this->engine->executeFunctionChunk( $ret, $frame ); |
| 76 |
return $result[0] ?? null; |
| 77 |
} |
| 78 |
} |