2019-12-18 20:12:45 +00:00
|
|
|
<?php
|
|
|
|
|
2022-08-03 12:31:15 +00:00
|
|
|
namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon;
|
|
|
|
|
2022-04-07 23:12:32 +00:00
|
|
|
use MediaWiki\Extension\Scribunto\ScribuntoException;
|
|
|
|
use MediaWiki\Extension\Scribunto\ScribuntoModuleBase;
|
2024-10-20 09:54:11 +00:00
|
|
|
use MediaWiki\Parser\PPFrame;
|
2024-01-05 18:24:06 +00:00
|
|
|
use MediaWiki\Status\Status;
|
2022-04-07 23:12:32 +00:00
|
|
|
|
2022-08-03 12:31:15 +00:00
|
|
|
class LuaModule extends ScribuntoModuleBase {
|
2019-12-18 20:12:45 +00:00
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
protected $initChunk;
|
|
|
|
|
|
|
|
/**
|
2022-08-03 12:31:15 +00:00
|
|
|
* @var LuaEngine
|
2019-12-18 20:12:45 +00:00
|
|
|
*/
|
|
|
|
protected $engine;
|
|
|
|
|
|
|
|
/**
|
2022-08-03 12:31:15 +00:00
|
|
|
* @param LuaEngine $engine
|
2019-12-18 20:12:45 +00:00
|
|
|
* @param string $code
|
|
|
|
* @param string|bool $chunkName
|
|
|
|
*/
|
2022-08-03 12:31:15 +00:00
|
|
|
public function __construct( LuaEngine $engine, $code, $chunkName ) {
|
2019-12-18 20:12:45 +00:00
|
|
|
parent::__construct( $engine, $code, $chunkName );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validate() {
|
|
|
|
try {
|
|
|
|
$this->getInitChunk();
|
|
|
|
} catch ( ScribuntoException $e ) {
|
|
|
|
return $e->toStatus();
|
|
|
|
}
|
|
|
|
return Status::newGood();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the chunk which, when called, will return the export table.
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getInitChunk() {
|
|
|
|
if ( !$this->initChunk ) {
|
|
|
|
$this->initChunk = $this->engine->getInterpreter()->loadString(
|
|
|
|
$this->code,
|
|
|
|
// Prepending an "=" to the chunk name avoids truncation or a "[string" prefix
|
|
|
|
'=' . $this->chunkName );
|
|
|
|
}
|
|
|
|
return $this->initChunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoke a function within the module. Return the expanded wikitext result.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param PPFrame $frame
|
|
|
|
* @throws ScribuntoException
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function invoke( $name, $frame ) {
|
|
|
|
$ret = $this->engine->executeModule( $this->getInitChunk(), $name, $frame );
|
|
|
|
|
2024-10-27 11:25:57 +00:00
|
|
|
if ( $ret === null ) {
|
2019-12-18 20:12:45 +00:00
|
|
|
throw $this->engine->newException(
|
|
|
|
'scribunto-common-nosuchfunction', [ 'args' => [ $name ] ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ( !$this->engine->getInterpreter()->isLuaFunction( $ret ) ) {
|
|
|
|
throw $this->engine->newException(
|
|
|
|
'scribunto-common-notafunction', [ 'args' => [ $name ] ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $this->engine->executeFunctionChunk( $ret, $frame );
|
2022-04-16 21:09:10 +00:00
|
|
|
return $result[0] ?? null;
|
2019-12-18 20:12:45 +00:00
|
|
|
}
|
|
|
|
}
|