From 8892204f24878c3c5084fd106b6e7397712a4ee6 Mon Sep 17 00:00:00 2001 From: George Kiagiadakis Date: Fri, 5 Apr 2024 21:16:11 +0300 Subject: [PATCH] wplua/sandbox: support mixing static methods and constructors in class identifiers Global class identifiers, such as "Node", "SessionItem", "Conf", etc are so far defined either as methods, which are constructors for the GObject class, or as tables, which contain "static" methods, i.e. methods that can be called without an instance. In some cases, we may want to mix a class being both instantiatable with a constructor and also have static methods. To support this, allow the class identifier be declared as a table with the constructor being defined as the "__new" method. This change allows calling the table as a method and execute "__new" underneath. For instance: ``` json = Conf.get_section_as_json("foobar") -- static method conf = Conf("/foo/bar") -- constructor, equivalent to Conf.__new("/foo/bar") ``` See also !629 --- modules/module-lua-scripting/wplua/sandbox.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/module-lua-scripting/wplua/sandbox.lua b/modules/module-lua-scripting/wplua/sandbox.lua index 9ffe8d40..9c2c17dd 100644 --- a/modules/module-lua-scripting/wplua/sandbox.lua +++ b/modules/module-lua-scripting/wplua/sandbox.lua @@ -43,6 +43,9 @@ function create_sandbox_env() if type(v) == "table" then SANDBOX_ENV[k] = setmetatable({}, { __index = v, + __call = function(t, ...) + return t["__new"](...) + end, __newindex = function(_, attr_name, _) error('Can not modify ' .. k .. '.' .. attr_name .. '. Protected by the sandbox.') end