MediaWiki  1.34.0
LibraryBase.php
Go to the documentation of this file.
1 <?php
27 abstract class Scribunto_LuaLibraryBase {
31  private $engine;
32 
37  $this->engine = $engine;
38  }
39 
49  abstract public function register();
50 
56  protected function getEngine() {
57  return $this->engine;
58  }
59 
65  protected function getInterpreter() {
66  return $this->engine->getInterpreter();
67  }
68 
74  protected function getParser() {
75  return $this->engine->getParser();
76  }
77 
83  protected function getTitle() {
84  return $this->getEngine()->getTitle();
85  }
86 
92  protected function getParserOptions() {
93  return $this->engine->getParser()->getOptions();
94  }
95 
106  protected function getLuaType( $var ) {
107  static $luaTypes = [
108  'NULL' => 'nil',
109  'double' => 'number',
110  'integer' => 'number',
111  'string' => 'string',
112  'boolean' => 'boolean',
113  'array' => 'table',
114  ];
115  $type = gettype( $var );
116  if ( isset( $luaTypes[$type] ) ) {
117  return $luaTypes[$type];
118  } elseif ( $this->getInterpreter()->isLuaFunction( $var ) ) {
119  return 'function';
120  } else {
121  $type = "PHP $type";
122  if ( is_object( $var ) ) {
123  $type .= " of class " . get_class( $var );
124  }
125  return $type;
126  }
127  }
128 
141  protected function checkType( $name, $argIdx, $arg, $expectType ) {
142  $type = $this->getLuaType( $arg );
143  if ( $type !== $expectType ) {
144  throw new Scribunto_LuaError(
145  "bad argument #$argIdx to '$name' ($expectType expected, got $type)"
146  );
147  }
148  }
149 
164  protected function checkTypeOptional( $name, $argIdx, &$arg, $expectType, $default ) {
165  if ( $arg === null ) {
166  $arg = $default;
167  } else {
168  $this->checkType( $name, $argIdx, $arg, $expectType );
169  }
170  }
171 
178  return $this->getEngine()->incrementExpensiveFunctionCount();
179  }
180 }
Scribunto_LuaError
Definition: LuaCommon.php:992
Scribunto_LuaLibraryBase\getTitle
getTitle()
Get the title.
Definition: LibraryBase.php:83
Scribunto_LuaLibraryBase\getEngine
getEngine()
Get the engine.
Definition: LibraryBase.php:56
Scribunto_LuaLibraryBase\getParser
getParser()
Get the parser.
Definition: LibraryBase.php:74
Scribunto_LuaLibraryBase\checkType
checkType( $name, $argIdx, $arg, $expectType)
Check the type of a variable.
Definition: LibraryBase.php:141
Scribunto_LuaLibraryBase\getInterpreter
getInterpreter()
Get the interpreter.
Definition: LibraryBase.php:65
Scribunto_LuaEngine
Definition: LuaCommon.php:6
Scribunto_LuaLibraryBase\checkTypeOptional
checkTypeOptional( $name, $argIdx, &$arg, $expectType, $default)
Check the type of a variable, with default if null.
Definition: LibraryBase.php:164
Scribunto_LuaLibraryBase\$engine
Scribunto_LuaEngine $engine
Definition: LibraryBase.php:31
Scribunto_LuaLibraryBase
This class provides some basic services that Lua libraries will probably need.
Definition: LibraryBase.php:27
Scribunto_LuaLibraryBase\getParserOptions
getParserOptions()
Get the parser options.
Definition: LibraryBase.php:92
Scribunto_LuaLibraryBase\__construct
__construct(Scribunto_LuaEngine $engine)
Definition: LibraryBase.php:36
Scribunto_LuaLibraryBase\incrementExpensiveFunctionCount
incrementExpensiveFunctionCount()
Increment the expensive function count, and throw if limit exceeded.
Definition: LibraryBase.php:177
Scribunto_LuaLibraryBase\getLuaType
getLuaType( $var)
Get the Lua type corresponding to the type of the variable.
Definition: LibraryBase.php:106
$type
$type
Definition: testCompression.php:48