mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Scribunto
synced 2024-11-25 00:26:44 +00:00
cebe775ee8
Package library: * Added a simulation of the Lua 5.1 package library. * Removed mw.import(), replaced it with a package loader. Packages can be retrieved from the wiki, using require('Module:Foo'), or from files distributed with Scribunto, using require('foo'). The "Module:" prefix allows for source compatibility with existing Lua code. * Added a couple of libraries from LuaForge: luabit and stringtools. * Made fetchModuleFromParser() return null on error instead of throwing an exception, to more easily support the desired behaviour of the package loader, which needs to return null on error. * Renamed mw.setupEnvironment() to mw.setup() since it is setting up things other than the environment now. * In MWServer:handleRegisterLibrary(), remove the feature which interprets dots in library names, since LuaSandbox doesn't support this. Improved module isolation and related refactoring: * Expose restricted versions of getfenv() and setfenv() to user Lua code. Requires luasandbox r114952. * Don't cache the export list returned by module execution for later function calls. This breaks isolation of #invoke calls, since the local variables are persistent. * Removed ScribuntoFunctionBase and its children, since it doesn't really have a purpose if it can't cache anything. Instead, invoke functions using a module method called invoke(). * Removed Module::initialize(), replaced it with a validate() function. This is a more elegant interface and works better with the new module caching scheme. * Use a Status object for the return value of Engine::validate() instead of an array. Use the formatting facilities of the Status class. Other: * Removed "too many returns" error, doesn't fit in with Lua conventions. * Use the standalone engine by default, so that the extension will work without configuration for more people. * Added an accessor for $engine->interpreter * Fix mw.clone() to correctly clone metatables * If the standalone interpreter exits due to an error, there are some contexts where the initial error will be caught and ignored, and the user will see the error from checkValid() instead. In this case, rethrow the original error for a more informative message. * Load mw.lua into the initial standalone environment, to reduce code duplication between mw.lua and MWServer.lua. * Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions that return no results. * Fixed a bug in encodeLuaVar() for strings with "\r". Added test case. * In MWServer.lua, don't call error() for internal errors, instead just print the error and exit. This avoids a protocol violation when an error is encountered from within handleCall(). * Added lots of documentation. Lua doc comments are in LuaDoc format. Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
90 lines
2.2 KiB
Lua
90 lines
2.2 KiB
Lua
|
|
local setmetatable, getmetatable, tostring, load, pairs, ipairs, next, assert, unpack
|
|
= setmetatable, getmetatable, tostring, load, pairs, ipairs, next, assert, unpack
|
|
local tconcat
|
|
= table.concat
|
|
local strformat
|
|
= string.format
|
|
local cowrap, yield
|
|
= coroutine.wrap, coroutine.yield
|
|
local _G = _G
|
|
|
|
setfenv(1,{})
|
|
|
|
local sb_meta = {
|
|
__tostring = function(self) return tconcat(self); end;
|
|
__call = function(self,v) self[#self+1] = tostring(v); return self; end;
|
|
__index = {
|
|
add = function(self,v)
|
|
self[#self+1] = tostring(v)
|
|
return self
|
|
end;
|
|
addquoted = function(self, v)
|
|
self[#self+1] = strformat('%q', tostring(v))
|
|
return self
|
|
end;
|
|
compile = function(self, chunk_name)
|
|
local localvars = self.localvars
|
|
local params = self.params
|
|
if not next(localvars) and not params then
|
|
local chunkthread = cowrap(function()
|
|
for _,chunk in ipairs(self) do
|
|
yield(chunk)
|
|
end
|
|
yield(nil)
|
|
end)
|
|
return assert(load(chunkthread,chunk_name))
|
|
end
|
|
local locals, ilocals = {}, 0
|
|
local chunkthread = cowrap(function()
|
|
if next(localvars) then
|
|
yield 'local '
|
|
for k,v in pairs(localvars) do
|
|
yield(k)
|
|
if next(localvars,k) then
|
|
yield ','
|
|
end
|
|
ilocals = ilocals+1
|
|
locals[ilocals] = v
|
|
end
|
|
yield ' = ...; '
|
|
end
|
|
yield 'return function('
|
|
if params then
|
|
yield(params)
|
|
end
|
|
yield ') '
|
|
for _,chunk in ipairs(self) do
|
|
yield(chunk)
|
|
end
|
|
yield '\nend'
|
|
yield(nil)
|
|
end)
|
|
local funcbuilder = assert(load(chunkthread,chunk_name))
|
|
return funcbuilder(unpack(locals,1,ilocals))
|
|
end;
|
|
localvar = function(self, val)
|
|
local lv = self.localvars
|
|
local i = 1
|
|
local lvname
|
|
while true do
|
|
lvname = '__lv_' .. i
|
|
if not lv[lvname] then
|
|
break
|
|
end
|
|
i = i + 1
|
|
end
|
|
lv[lvname] = val
|
|
return lvname
|
|
end;
|
|
}
|
|
}
|
|
|
|
local function stringbuilder()
|
|
return setmetatable({localvars={}}, sb_meta)
|
|
end
|
|
|
|
_G.stringbuilder = stringbuilder
|
|
|
|
return stringbuilder
|