2013-01-25 17:53:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Basic services that Lua libraries will probably need
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @author Brad Jorsch
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class provides some basic services that Lua libraries will probably need
|
|
|
|
*/
|
2022-08-03 10:03:12 +00:00
|
|
|
abstract class Scribunto_LuaLibraryBase {
|
2014-10-09 19:49:40 +00:00
|
|
|
/**
|
2022-08-03 10:03:12 +00:00
|
|
|
* @var Scribunto_LuaEngine
|
2014-10-09 19:49:40 +00:00
|
|
|
*/
|
2013-01-25 17:53:18 +00:00
|
|
|
private $engine;
|
|
|
|
|
|
|
|
/**
|
2022-08-03 10:03:12 +00:00
|
|
|
* @param Scribunto_LuaEngine $engine
|
2013-01-25 17:53:18 +00:00
|
|
|
*/
|
2022-08-03 10:03:12 +00:00
|
|
|
public function __construct( Scribunto_LuaEngine $engine ) {
|
2013-01-25 17:53:18 +00:00
|
|
|
$this->engine = $engine;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called to register the library.
|
|
|
|
*
|
2014-09-26 18:11:21 +00:00
|
|
|
* This should do any necessary setup and then call $this->getEngine()->registerInterface().
|
|
|
|
* The value returned by that call should be returned from this function,
|
|
|
|
* and must be for 'deferLoad' libraries to work right.
|
2013-01-25 17:53:18 +00:00
|
|
|
*
|
2017-12-25 08:06:19 +00:00
|
|
|
* @return array Lua package
|
2013-01-25 17:53:18 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
abstract public function register();
|
2013-01-25 17:53:18 +00:00
|
|
|
|
|
|
|
/**
|
2022-08-03 10:03:12 +00:00
|
|
|
* @return Scribunto_LuaEngine engine
|
2013-01-25 17:53:18 +00:00
|
|
|
*/
|
|
|
|
protected function getEngine() {
|
|
|
|
return $this->engine;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-08-03 10:03:12 +00:00
|
|
|
* @return Scribunto_LuaInterpreter interpreter
|
2013-01-25 17:53:18 +00:00
|
|
|
*/
|
|
|
|
protected function getInterpreter() {
|
|
|
|
return $this->engine->getInterpreter();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-10 09:44:08 +00:00
|
|
|
* @return Parser
|
2013-01-25 17:53:18 +00:00
|
|
|
*/
|
|
|
|
protected function getParser() {
|
|
|
|
return $this->engine->getParser();
|
|
|
|
}
|
|
|
|
|
2013-02-07 18:51:41 +00:00
|
|
|
/**
|
2022-01-10 09:44:08 +00:00
|
|
|
* @return Title
|
2013-02-07 18:51:41 +00:00
|
|
|
*/
|
|
|
|
protected function getTitle() {
|
|
|
|
return $this->getEngine()->getTitle();
|
|
|
|
}
|
|
|
|
|
2013-01-25 17:53:18 +00:00
|
|
|
/**
|
2022-01-10 09:44:08 +00:00
|
|
|
* @return ParserOptions
|
2013-01-25 17:53:18 +00:00
|
|
|
*/
|
|
|
|
protected function getParserOptions() {
|
|
|
|
return $this->engine->getParser()->getOptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Lua type corresponding to the type of the variable.
|
|
|
|
*
|
|
|
|
* If the variable does not correspond to any type, the PHP type will be
|
|
|
|
* returned (prefixed with "PHP"). For example, "PHP resource" or "PHP
|
|
|
|
* object of class Foo".
|
|
|
|
*
|
2017-09-24 17:38:14 +00:00
|
|
|
* @param mixed $var Variable to test
|
2013-01-25 17:53:18 +00:00
|
|
|
* @return string Type
|
|
|
|
*/
|
|
|
|
protected function getLuaType( $var ) {
|
2017-06-15 17:19:00 +00:00
|
|
|
static $luaTypes = [
|
2013-01-25 17:53:18 +00:00
|
|
|
'NULL' => 'nil',
|
|
|
|
'double' => 'number',
|
|
|
|
'integer' => 'number',
|
|
|
|
'string' => 'string',
|
|
|
|
'boolean' => 'boolean',
|
|
|
|
'array' => 'table',
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
2013-01-25 17:53:18 +00:00
|
|
|
$type = gettype( $var );
|
|
|
|
if ( isset( $luaTypes[$type] ) ) {
|
|
|
|
return $luaTypes[$type];
|
|
|
|
} elseif ( $this->getInterpreter()->isLuaFunction( $var ) ) {
|
|
|
|
return 'function';
|
|
|
|
} else {
|
|
|
|
$type = "PHP $type";
|
|
|
|
if ( is_object( $var ) ) {
|
|
|
|
$type .= " of class " . get_class( $var );
|
|
|
|
}
|
|
|
|
return $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the type of a variable
|
|
|
|
*
|
|
|
|
* If the type of the variable does not match the expected type,
|
2022-08-03 10:03:12 +00:00
|
|
|
* a Scribunto_LuaError will be thrown.
|
2013-01-25 17:53:18 +00:00
|
|
|
*
|
2017-09-24 17:38:14 +00:00
|
|
|
* @param string $name Name of the calling function (as seen from Lua)
|
|
|
|
* @param int $argIdx Index of the argument being tested (1-based)
|
|
|
|
* @param mixed $arg Variable to test
|
|
|
|
* @param string $expectType Lua type expected
|
2013-01-25 17:53:18 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function checkType( $name, $argIdx, $arg, $expectType ) {
|
|
|
|
$type = $this->getLuaType( $arg );
|
|
|
|
if ( $type !== $expectType ) {
|
2022-08-03 10:03:12 +00:00
|
|
|
throw new Scribunto_LuaError(
|
2013-01-25 17:53:18 +00:00
|
|
|
"bad argument #$argIdx to '$name' ($expectType expected, got $type)"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the type of a variable, with default if null
|
|
|
|
*
|
|
|
|
* If the variable is null, $default will be assigned. Otherwise, if the
|
|
|
|
* type of the variable does not match the expected type, a
|
2022-08-03 10:03:12 +00:00
|
|
|
* Scribunto_LuaError will be thrown.
|
2013-01-25 17:53:18 +00:00
|
|
|
*
|
2017-09-24 17:38:14 +00:00
|
|
|
* @param string $name Name of the calling function (as seen from Lua)
|
|
|
|
* @param int $argIdx Index of the argument being tested (1-based)
|
|
|
|
* @param mixed &$arg Variable to test/set
|
|
|
|
* @param string $expectType Lua type expected
|
|
|
|
* @param mixed $default Default value
|
2013-01-25 17:53:18 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function checkTypeOptional( $name, $argIdx, &$arg, $expectType, $default ) {
|
|
|
|
if ( $arg === null ) {
|
|
|
|
$arg = $default;
|
|
|
|
} else {
|
|
|
|
$this->checkType( $name, $argIdx, $arg, $expectType );
|
|
|
|
}
|
|
|
|
}
|
2013-02-01 20:43:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Increment the expensive function count, and throw if limit exceeded
|
|
|
|
*
|
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
public function incrementExpensiveFunctionCount() {
|
|
|
|
return $this->getEngine()->incrementExpensiveFunctionCount();
|
|
|
|
}
|
2013-01-25 17:53:18 +00:00
|
|
|
}
|