MediaWiki REL1_34
LibraryBase.php
Go to the documentation of this file.
1<?php
31 private $engine;
32
36 public function __construct( Scribunto_LuaEngine $engine ) {
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}
This class provides some basic services that Lua libraries will probably need.
getLuaType( $var)
Get the Lua type corresponding to the type of the variable.
getInterpreter()
Get the interpreter.
__construct(Scribunto_LuaEngine $engine)
checkType( $name, $argIdx, $arg, $expectType)
Check the type of a variable.
Scribunto_LuaEngine $engine
getEngine()
Get the engine.
checkTypeOptional( $name, $argIdx, &$arg, $expectType, $default)
Check the type of a variable, with default if null.
getParser()
Get the parser.
incrementExpensiveFunctionCount()
Increment the expensive function count, and throw if limit exceeded.
getTitle()
Get the title.
getParserOptions()
Get the parser options.