--如此每收到1个消息就开启1个协程, 所以协程数量增长很快,
--如果协程运行结束, 就要进行回收, 即可在C中编写,也可在LUA中
function 回收协程()
local 总数=0
local 回收=0
for i in pairs(mir.copool) do
总数 = 总数+1
if coroutine.status(mir.copool[i])=='dead' then
mir.copool[i]=nil --垃圾回收器会自动回收该协程
回收=回收+1
end
end
--if 回收>0 then print('协程总数,回收,剩余=',总数,回收,总数-回收) end
end
TMethodRec = packed record
wSize: Word;
pCode: Pointer;
sName: ShortString;
end;
var
MethodTable: PAnsiChar;
MethodRec: PMethodRec;
wCount: Word;
nMethod: Integer;
begin
// Get a pointer to the class's published method table
MethodTable := PAnsiChar(Pointer(PAnsiChar(Cls) + vmtMethodTable)^);
if (MethodTable <> Nil) then
begin
// Get the count of the methods in the table
Move(MethodTable^, wCount, 2);
// Position the MethodRec pointer at the first method in the table
// (skip over the 2-byte method count)
MethodRec := PMethodRec(MethodTable + 2);
// Iterate through all the published methods of this class
for nMethod := 0 to wCount - 1 do
begin
// Add the method name to the lua functions
// 名称:MethodRec.sName 地址:MethodRec.pcode
lua_pushcclosure(L, MethodRec.pcode, 0); //地址
lua_setfield(L, -2, PAnsiChar(AnsiString(MethodRec.sName))); //名称
// Skip to the next method
MethodRec := PMethodRec(PAnsiChar(MethodRec) + MethodRec.wSize);
end;
end;
end;
{ mylib }
function Tmylib.xx: integer;
begin
lua_pushstring(self, 'mylib.xx');
result:=1;
end;
function Tmylib.yy: integer;
begin
lua_pushinteger(self, 1818);
result:=1;
end;
type TLuaDelphi = class(TLuaPackage)
private
function TList__index: integer;cdecl;
function TList__indexof: integer;cdecl;
published
function TList: integer;cdecl;
end;
var
List:TList;
i:integer;
function open_Delphi(L:lua_State): Integer; cdecl;
implementation
function open_Delphi(L:lua_State): Integer; cdecl;
begin
RegisterFunctions(L, TLuaDelphi);
//用于测试TList
if List = nil then List := TList.create;
lua_pushinteger(L, integer(List));
lua_setfield(L, -2, 'testlist');
for i:=1 to 10 do begin
List.Add(pointer(i*100));
end;
//
//Delphi TList类
luaL_newmetatable(L, 'Delphi.TList');
lua_pushcclosure(L, @TLuaDelphi.TList__index, 0);
lua_setfield(L, -2, '__index');
end;
{ TLuaDelphi }
function TLuaDelphi.TList: integer;
var
str:string;
p : integer;
begin
p := lua_tointeger(self,1);
lua_pushlightuserdata(self, pointer(p));
luaL_getmetatable(self, 'Delphi.TList');
lua_setmetatable(self, -2);
result:=1;
end;
function TLuaDelphi.TList__index: integer; //堆栈List,Index
var
List: Classes.TList;
Key : pAnsiChar;
Index : integer;
begin
result := 1;
try
List := Classes.TList( lua_touserdata(self, 1));
Key := lua_tostring(self, 2);
Index := lua_tointeger(self, 2);
if (index>0) and (index<=List.Count) then begin
lua_pushinteger(self, integer(List.Items[Index-1]));
end else if AnsiStrIComp(Key, 'count')=0 then begin
lua_pushinteger(self, List.Count);
end else if AnsiStrIComp(Key, 'indexof')=0 then begin
lua_pushcclosure(self, pointer(@TLuaDelphi.TList__indexof), 0);
end else begin
result := 0;
end;
except
lua_pushstring(self, 'Trace List,Index,Key');
lua_pushinteger(self, integer(List));
lua_pushinteger(self, Index);
lua_pushstring(self, Key);
error();
result :=0 ;
end
end;
//脚本调用 list:indexof(item)
function TLuaDelphi.TList__indexof: integer;
var
List: Classes.TList;
Item: Integer;
begin
try
List := Classes.TList( lua_touserdata(self, 1));
Item := lua_tointeger( self, 2);
lua_pushinteger(self, List.IndexOf(pointer(Item))+1);
result:=1;
except
lua_pushstring(self, 'Trace List,Item');
lua_pushinteger(self, integer(List));
lua_pushinteger(self, Item);
error();
result := 0;
end;
end;