Skip to content

Commit

Permalink
support for __call() metamethod, see: 13_test_share_call.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
untoxa committed Jun 15, 2021
1 parent e846734 commit 2a39b17
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
40 changes: 40 additions & 0 deletions lua/13_test_share_call.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package.cpath = getScriptPath() .. "/?.dll"
sh = require "lua_share"

function table.val_to_str ( v )
if "string" == type( v ) then
v = string.gsub( v, "\n", "\\n" )
if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
return "'" .. v .. "'"
end
return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
end
return "table" == type( v ) and table.tostring( v ) or tostring( v )
end
function table.key_to_str ( k )
if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
return k
end
return "[" .. table.val_to_str( k ) .. "]"
end
function table.tostring( tbl )
if type(tbl)~='table' then return table.val_to_str(tbl) end
local result, done = {}, {}
for k, v in ipairs( tbl ) do
table.insert( result, table.val_to_str( v ) )
done[ k ] = true
end
for k, v in pairs( tbl ) do
if not done[ k ] then
table.insert( result, table.key_to_str( k ) .. "=" .. table.val_to_str( v ) )
end
end
return "{" .. table.concat( result, "," ) .. "}"
end

function main()
local ns = sh.GetNameSpace("test_name_space")
ns["test"] = "Hello, world" -- must ensure test_name_space exists, call does not create the object
local a, b, c = ns(27.245, {1, 2, {3, "b"}}, 54) -- just call namespace as function
message("a = " .. tostring(a) .. " b = " .. table.tostring(b) .. " c = " .. tostring(c), 1)
end
3 changes: 3 additions & 0 deletions lua/lua_share_boot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ __default_namespace_metatable = {
if idx then return self.__data[idx] end
end
return nil
end,
__call = function(self, ...)
return ...
end
}

Expand Down
Binary file modified lua_share.res
Binary file not shown.
36 changes: 34 additions & 2 deletions lua_share_main.pas
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type tLuaShare = class(TLuaClass)

function __index(AContext: TLuaContext): integer;
function __newindex(AContext: TLuaContext): integer;
function __call(AContext: TLuaContext): integer;
function __IPC_index(AContext: TLuaContext): integer;
function __IPC_newindex(AContext: TLuaContext): integer;
function __IPC_call(AContext: TLuaContext): integer;
Expand Down Expand Up @@ -178,6 +179,36 @@ function tLuaShare.__newindex(AContext: TLuaContext): integer;
result:= 0;
end;

function tLuaShare.__call(AContext: TLuaContext): integer;
var namespace_name : ansistring;
ssize, i : longint;
begin
result:= 0;
if assigned(lua_storage_state) then try
EnterCriticalSection(lua_lock);
try
with AContext do begin
ssize:= AContext.StackSize;

namespace_name:= Stack[1].AsTable[namespace_item].AsString(datatable_name);
lua_getglobal(lua_storage_state, pAnsiChar(namespace_name));

for i:= 2 to ssize do __deepcopyvalue(CurrentState, lua_storage_state, i);

if (lua_pcall(lua_storage_state, ssize - 1, LUA_MULTRET, 0) = 0) then begin
result:= lua_gettop(lua_storage_state);
for i:= 1 to result do __deepcopyvalue(lua_storage_state, CurrentState, i);
lua_pop(lua_storage_state, result);
end else begin
// raise error here? suppress any errors by now
// get error example: lua_tolstring(lua_storage_state, -1, slen)
lua_pop(lua_storage_state, 1); // dispose error message
end;
end;
finally LeaveCriticalSection(lua_lock); end;
except on e: exception do messagebox(0, pAnsiChar(e.message), msgbox_err_title, MB_ICONERROR); end;
end;

function tLuaShare.__IPC_index(AContext: TLuaContext): integer;
begin result:= RPCCallNS(AContext, 'GetIPC', AContext.Stack[1].AsTable[namespace_item].AsString(datatable_name), [2], AContext.Stack[3].AsInteger(max_RPC_timeout)); end;

Expand Down Expand Up @@ -275,7 +306,7 @@ function tLuaShare.ShowMessageBox(AContext: TLuaContext): integer;
end;

function tLuaShare.GetNameSpace(AContext: TLuaContext): integer;
begin with AContext do result:= selfregister(CurrentState, pAnsiChar(Stack[1].AsString(datatable_name)), DeepCopy, __index, __newindex, nil); end;
begin with AContext do result:= selfregister(CurrentState, pAnsiChar(Stack[1].AsString(datatable_name)), DeepCopy, __index, __newindex, __call); end;

function tLuaShare.GetIPCNameSpace(AContext: TLuaContext): integer;
begin
Expand Down Expand Up @@ -325,6 +356,7 @@ function LuaAtPanic(astate: Lua_State): Integer; cdecl;
var err: ansistring;
len: size_t;
begin
result:= 0;
SetString(err, lua_tolstring(astate, -1, len), len);
raise Exception.CreateFmt('LUA ERROR: %s', [err]);
end;
Expand Down Expand Up @@ -386,7 +418,7 @@ function initialize_share(ALuaInstance: TLuaState): integer;
end else messagebox(0, pAnsiChar(format('Failed to find LUA library: %s', [lua_supported_libs[low(lua_supported_libs)]])), msgbox_err_title, MB_ICONERROR);
end;
if assigned(lua_share_instance) then begin
with lua_share_instance do result:= selfregister(ALuaInstance, datatable_name, DeepCopy, __index, __newindex, nil);
with lua_share_instance do result:= selfregister(ALuaInstance, datatable_name, DeepCopy, __index, __newindex, __call);
// register result table as a global variable:
lua_pushvalue(ALuaInstance, -1);
lua_setglobal(ALuaInstance, package_name);
Expand Down

0 comments on commit 2a39b17

Please sign in to comment.