Debian Codegraph @

extensions/Scribunto/includes/Engines/LuaCommon/LibraryBase.php

raw symbols SCIP
1 <?php
2 /**
3 * Basic services that Lua libraries will probably need
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author Brad Jorsch
22 */
23
24 namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon;
25
26 use MediaWiki\Parser\Parser;
27 use MediaWiki\Parser\ParserOptions;
28 use MediaWiki\Title\Title;
29
30 /**
31 * This class provides some basic services that Lua libraries will probably need
32 */
33 abstract class LibraryBase {
34 /**
35 * @var LuaEngine
36 */
37 private $engine;
38
39 /**
40 * @param LuaEngine $engine
41 */
42 public function __construct( LuaEngine $engine ) {
43 $this->engine = $engine;
44 }
45
46 /**
47 * Called to register the library.
48 *
49 * This should do any necessary setup and then call $this->getEngine()->registerInterface().
50 * The value returned by that call should be returned from this function,
51 * and must be for 'deferLoad' libraries to work right.
52 *
53 * @return array Lua package
54 */
55 abstract public function register();
56
57 /**
58 * @return LuaEngine engine
59 */
60 protected function getEngine() {
61 return $this->engine;
62 }
63
64 /**
65 * @return LuaInterpreter interpreter
66 */
67 protected function getInterpreter() {
68 return $this->engine->getInterpreter();
69 }
70
71 /**
72 * @return Parser
73 */
74 protected function getParser() {
75 return $this->engine->getParser();
76 }
77
78 /**
79 * @return Title
80 */
81 protected function getTitle() {
82 return $this->getEngine()->getTitle();
83 }
84
85 /**
86 * @return ParserOptions
87 */
88 protected function getParserOptions() {
89 return $this->engine->getParser()->getOptions();
90 }
91
92 /**
93 * Get the Lua type corresponding to the type of the variable.
94 *
95 * If the variable does not correspond to any type, the PHP type will be
96 * returned (prefixed with "PHP"). For example, "PHP resource" or "PHP
97 * object of class Foo".
98 *
99 * @param mixed $var Variable to test
100 * @return string Type
101 */
102 protected function getLuaType( $var ) {
103 static $luaTypes = [
104 'NULL' => 'nil',
105 'double' => 'number',
106 'integer' => 'number',
107 'string' => 'string',
108 'boolean' => 'boolean',
109 'array' => 'table',
110 ];
111 $type = gettype( $var );
112 if ( isset( $luaTypes[$type] ) ) {
113 return $luaTypes[$type];
114 } elseif ( $this->getInterpreter()->isLuaFunction( $var ) ) {
115 return 'function';
116 } else {
117 $type = "PHP $type";
118 if ( is_object( $var ) ) {
119 $type .= " of class " . get_class( $var );
120 }
121 return $type;
122 }
123 }
124
125 /**
126 * Check the type of a variable
127 *
128 * If the type of the variable does not match the expected type,
129 * a LuaError will be thrown.
130 *
131 * @param string $name Name of the calling function (as seen from Lua)
132 * @param int $argIdx Index of the argument being tested (1-based)
133 * @param mixed $arg Variable to test
134 * @param string $expectType Lua type expected
135 * @return void
136 */
137 protected function checkType( $name, $argIdx, $arg, $expectType ) {
138 $type = $this->getLuaType( $arg );
139 if ( $type !== $expectType ) {
140 throw new LuaError(
141 "bad argument #$argIdx to '$name' ($expectType expected, got $type)"
142 );
143 }
144 }
145
146 /**
147 * Check the type of a variable, with default if null
148 *
149 * If the variable is null, $default will be assigned. Otherwise, if the
150 * type of the variable does not match the expected type, a
151 * LuaError will be thrown.
152 *
153 * @param string $name Name of the calling function (as seen from Lua)
154 * @param int $argIdx Index of the argument being tested (1-based)
155 * @param mixed &$arg Variable to test/set
156 * @param string $expectType Lua type expected
157 * @param mixed $default Default value
158 * @return void
159 */
160 protected function checkTypeOptional( $name, $argIdx, &$arg, $expectType, $default ) {
161 if ( $arg === null ) {
162 $arg = $default;
163 } else {
164 $this->checkType( $name, $argIdx, $arg, $expectType );
165 }
166 }
167
168 /**
169 * Increment the expensive function count, and throw if limit exceeded
170 *
171 * @return null
172 */
173 public function incrementExpensiveFunctionCount() {
174 return $this->getEngine()->incrementExpensiveFunctionCount();
175 }
176 }
177
178 class_alias( LibraryBase::class, 'Scribunto_LuaLibraryBase' );