Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
LuaModule
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 4
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 validate
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getInitChunk
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 invoke
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon;
4
5use MediaWiki\Extension\Scribunto\ScribuntoException;
6use MediaWiki\Extension\Scribunto\ScribuntoModuleBase;
7use MediaWiki\Status\Status;
8use PPFrame;
9
10class LuaModule extends ScribuntoModuleBase {
11    /**
12     * @var mixed
13     */
14    protected $initChunk;
15
16    /**
17     * @var LuaEngine
18     */
19    protected $engine;
20
21    /**
22     * @param LuaEngine $engine
23     * @param string $code
24     * @param string|bool $chunkName
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     * Get the chunk which, when called, will return the export table.
41     * @return mixed
42     */
43    public function getInitChunk() {
44        if ( !$this->initChunk ) {
45            $this->initChunk = $this->engine->getInterpreter()->loadString(
46                $this->code,
47                // Prepending an "=" to the chunk name avoids truncation or a "[string" prefix
48                '=' . $this->chunkName );
49        }
50        return $this->initChunk;
51    }
52
53    /**
54     * Invoke a function within the module. Return the expanded wikitext result.
55     *
56     * @param string $name
57     * @param PPFrame $frame
58     * @throws ScribuntoException
59     * @return string|null
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}