|
@@ -1,7 +1,7 @@
|
|
|
//******************************************************************************
|
|
|
//*** LUA SCRIPT DELPHI UTILITIES ***
|
|
|
//*** ***
|
|
|
-//*** (c) 2005 Jean-François Goulet, Massimo Magnano, Kuma ***
|
|
|
+//*** (c) 2006 Jean-François Goulet, Massimo Magnano, Kuma ***
|
|
|
//*** ***
|
|
|
//*** ***
|
|
|
//******************************************************************************
|
|
@@ -12,9 +12,9 @@
|
|
|
//******************************************************************************
|
|
|
//** See Copyright Notice in lua.h
|
|
|
|
|
|
-//Revision 1.6
|
|
|
-// JF Adds :
|
|
|
-// LuaTableToVirtualTreeView
|
|
|
+//Revision 1.2
|
|
|
+// MaxM Adds :
|
|
|
+// GetCurrentFuncName, LuaSetTablePropertyFuncs
|
|
|
//
|
|
|
//Revision 1.1
|
|
|
// MaxM Adds :
|
|
@@ -33,7 +33,7 @@ unit LuaUtils;
|
|
|
interface
|
|
|
|
|
|
uses
|
|
|
- SysUtils, Classes, ComCtrls, lua, lualib, lauxlib, Variants, VirtualTrees;
|
|
|
+ SysUtils, Classes, ComCtrls, luaconf, lua, lualib, lauxlib, Variants;
|
|
|
|
|
|
const
|
|
|
ERR_Script ='Script Error : ';
|
|
@@ -48,12 +48,6 @@ type
|
|
|
constructor Create(Title: string; Line: Integer; Msg: string);
|
|
|
end;
|
|
|
|
|
|
- PBasicTreeData = ^TBasicTreeData;
|
|
|
- TBasicTreeData = record
|
|
|
- sName: String;
|
|
|
- sValue: String;
|
|
|
- end;
|
|
|
-
|
|
|
TVariantArray =array of Variant;
|
|
|
PVariantArray =^TVariantArray;
|
|
|
|
|
@@ -97,6 +91,9 @@ procedure LuaSetTableFunction(L: Plua_State; TableIndex: Integer; const Key: str
|
|
|
procedure LuaSetTableLightUserData(L: Plua_State; TableIndex: Integer; const Key: string; P: Pointer);
|
|
|
procedure LuaSetTableTMethod(L: Plua_State; TableIndex: Integer; const Key: string; M: TMethod);
|
|
|
procedure LuaSetTableClear(L: Plua_State; TableIndex: Integer);
|
|
|
+procedure LuaSetTablePropertyFuncs(L: PLua_State; TableIndex: Integer;
|
|
|
+ ReadFunc: lua_CFunction;
|
|
|
+ WriteFunc: lua_CFunction =nil);
|
|
|
procedure LuaRawSetTableValue(L: PLua_State; TableIndex: Integer; const Key: string; ValueIndex: Integer);
|
|
|
procedure LuaRawSetTableNil(L: Plua_State; TableIndex: Integer; const Key: string);
|
|
|
procedure LuaRawSetTableBoolean(L: Plua_State; TableIndex: Integer; const Key: string; B: Boolean);
|
|
@@ -109,16 +106,16 @@ function LuaGetMetaFunction(L: Plua_State; Index: Integer; Key: string): lua_CFu
|
|
|
procedure LuaSetMetaFunction(L: Plua_State; Index: Integer; Key: string; F: lua_CFunction);
|
|
|
|
|
|
procedure LuaShowStack(L: Plua_State; Caption: string = '');
|
|
|
-function LuaStackToStr(L: Plua_State; Index: Integer; MaxTable: Integer = -1; SubTableMax: Integer = 99; CheckCyclicReferencing: Boolean = True; TablePtrs: TList = nil): String;
|
|
|
+function LuaStackToStr(L: Plua_State; Index: Integer; MaxTable: Integer = -1; SubTableMax: Integer = 99): string;
|
|
|
procedure LuaRegisterCustom(L: PLua_State; TableIndex: Integer; const Name: PChar; F: lua_CFunction);
|
|
|
procedure LuaRegister(L: Plua_State; const Name: PChar; F: lua_CFunction);
|
|
|
procedure LuaRegisterMetatable(L: Plua_State; const Name: PChar; F: lua_CFunction);
|
|
|
procedure LuaRegisterProperty(L: PLua_State; const Name: PChar; ReadFunc, WriteFunc: lua_CFunction);
|
|
|
-procedure LuaStackToStrings(L: Plua_State; Lines: TStrings; MaxTable: Integer = -1; SubTableMax: Integer = 99; CheckCyclicReferencing: Boolean = True);
|
|
|
-procedure LuaLocalToStrings(L: Plua_State; Lines: TStrings; MaxTable: Integer = -1; Level: Integer = 0; SubTableMax: Integer = 99; CheckCyclicReferencing: Boolean = True);
|
|
|
-procedure LuaGlobalToStrings(L: PLua_State; Lines: TStrings; MaxTable: Integer = -1; SubTableMax: Integer = 99; CheckCyclicReferencing: Boolean = True);
|
|
|
-procedure LuaTableToStrings(L: Plua_State; Index: Integer; Lines: TStrings; MaxTable: Integer = -1; SubTableMax: Integer = 99; CheckCyclicReferencing: Boolean = True);
|
|
|
-procedure LuaTableToVirtualTreeView(L: Plua_State; Index: Integer; VTV: TVirtualStringTree; MaxTable: Integer; SubTableMax: Integer; CheckCyclicReferencing: Boolean);
|
|
|
+procedure LuaStackToStrings(L: Plua_State; Lines: TStrings; MaxTable: Integer = -1; SubTableMax: Integer = 99);
|
|
|
+procedure LuaLocalToStrings(L: Plua_State; Lines: TStrings; MaxTable: Integer = -1; Level: Integer = 0; SubTableMax: Integer = 99);
|
|
|
+procedure LuaGlobalToStrings(L: PLua_State; Lines: TStrings; MaxTable: Integer = -1; SubTableMax: Integer = 99);
|
|
|
+procedure LuaTableToStrings(L: Plua_State; Index: Integer; Lines: TStrings; MaxTable: Integer = -1; SubTableMax: Integer = 99);
|
|
|
+procedure LuaTableToTreeView(L: Plua_State; Index: Integer; TV: TTreeView; MaxTable: Integer = -1; SubTableMax: Integer = 99);
|
|
|
function LuaGetIdentValue(L: Plua_State; Ident: string; MaxTable: Integer = -1): string;
|
|
|
procedure LuaSetIdentValue(L: Plua_State; Ident, Value: string; MaxTable: Integer = -1);
|
|
|
procedure LuaLoadBuffer(L: Plua_State; const Code: string; const Name: string);
|
|
@@ -134,6 +131,10 @@ procedure LuaErrorFmt(L: Plua_State; const Fmt: string; const Args: array of Con
|
|
|
function LuaDataStrToStrings(const TableStr: string; Strings: TStrings): string;
|
|
|
function LuaDoFile(L: Plua_State): Integer; cdecl;
|
|
|
|
|
|
+function LuaGetCurrentFuncName(L: Plua_State) :String;
|
|
|
+function Lua_VoidFunc(L: Plua_State): Integer; cdecl;
|
|
|
+
|
|
|
+
|
|
|
const
|
|
|
LuaGlobalVariableStr = '[LUA_GLOBALSINDEX]';
|
|
|
var
|
|
@@ -141,7 +142,6 @@ var
|
|
|
OnLuaStdout: TOnLuaStdout;
|
|
|
DefaultMaxTable: Integer;
|
|
|
SubTableCount: Integer;
|
|
|
- SubTableCount2: Integer;
|
|
|
|
|
|
implementation
|
|
|
|
|
@@ -154,20 +154,18 @@ const
|
|
|
LF = #$0A;
|
|
|
CRLF = CR + LF;
|
|
|
|
|
|
-function Quote(const Str: String): String;
|
|
|
+function Quote(const Str: string): string;
|
|
|
begin
|
|
|
Result := AnsiQuotedStr(Str, QuoteStr);
|
|
|
end;
|
|
|
|
|
|
-function Dequote(const QuotedStr: String): String;
|
|
|
+function Dequote(const QuotedStr: string): string;
|
|
|
begin
|
|
|
- Result := QuotedStr;
|
|
|
-
|
|
|
- if ((AnsiStrScan(PChar(QuotedStr), QuoteStr) = QuotedStr) and (AnsiStrRScan(PChar(QuotedStr), QuoteStr) = QuoteStr)) then
|
|
|
- Result := Copy(QuotedStr, 2, Length(QuotedStr) - 2);
|
|
|
+ Result := AnsiDequotedStr(QuotedStr, QuoteStr);
|
|
|
end;
|
|
|
|
|
|
function fwriteex(F, S: PChar; Un, Len: Integer; L, Dummy: Integer): Integer;
|
|
|
+
|
|
|
var
|
|
|
Size: Integer;
|
|
|
begin
|
|
@@ -189,7 +187,7 @@ end;
|
|
|
|
|
|
function fputsex(const F, S: String; L, Dummy: Integer): Integer;
|
|
|
begin
|
|
|
- Result := fwriteex(PChar(F), PChar(S), SizeOf(Char), Length(S), L, Dummy);
|
|
|
+ Result := fwriteex(PChar(F), PChar(S), SizeOf(Char), L, Length(S), Dummy);
|
|
|
end;
|
|
|
|
|
|
function fputs(const S: string; Dummy: Integer): Integer;
|
|
@@ -266,9 +264,10 @@ begin
|
|
|
end;
|
|
|
|
|
|
function lua_io_writeex(L: Plua_State): Integer; cdecl;
|
|
|
+
|
|
|
function pushresult(L: Plua_State; I: Boolean; FileName: PChar): Integer;
|
|
|
begin
|
|
|
- lua_pushboolean(L, True);
|
|
|
+ lua_pushboolean(L, 1);
|
|
|
Result := 1;
|
|
|
end;
|
|
|
const
|
|
@@ -310,9 +309,10 @@ begin
|
|
|
end;
|
|
|
|
|
|
function lua_io_write(L: Plua_State): Integer; cdecl;
|
|
|
+
|
|
|
function pushresult(L: Plua_State; I: Boolean; FileName: PChar): Integer;
|
|
|
begin
|
|
|
- lua_pushboolean(L, True);
|
|
|
+ lua_pushboolean(L, 1);
|
|
|
Result := 1;
|
|
|
end;
|
|
|
const
|
|
@@ -347,12 +347,12 @@ end;
|
|
|
|
|
|
function LuaToBoolean(L: PLua_State; Index: Integer): Boolean;
|
|
|
begin
|
|
|
- Result := (lua_toboolean(L, Index) <> False);
|
|
|
+ Result := (lua_toboolean(L, Index) <> 0);
|
|
|
end;
|
|
|
|
|
|
procedure LuaPushBoolean(L: PLua_State; B: Boolean);
|
|
|
begin
|
|
|
- lua_pushboolean(L, B);
|
|
|
+ lua_pushboolean(L, Integer(B));
|
|
|
end;
|
|
|
|
|
|
function LuaToInteger(L: PLua_State; Index: Integer): Integer;
|
|
@@ -464,7 +464,7 @@ end;
|
|
|
function LuaGetTableBoolean(L: Plua_State; TableIndex: Integer; const Key: string): Boolean;
|
|
|
begin
|
|
|
LuaGetTable(L, TableIndex, Key);
|
|
|
- Result := (lua_toboolean(L, -1) <> False);
|
|
|
+ Result := (lua_toboolean(L, -1) <> 0);
|
|
|
lua_pop(L, 1);
|
|
|
end;
|
|
|
|
|
@@ -518,7 +518,7 @@ end;
|
|
|
function LuaRawGetTableBoolean(L: Plua_State; TableIndex: Integer; const Key: string): Boolean;
|
|
|
begin
|
|
|
LuaRawGetTable(L, TableIndex, Key);
|
|
|
- Result := (lua_toboolean(L, -1) <> False);
|
|
|
+ Result := (lua_toboolean(L, -1) <> 0);
|
|
|
lua_pop(L, 1);
|
|
|
end;
|
|
|
|
|
@@ -569,7 +569,7 @@ end;
|
|
|
procedure LuaSetTableBoolean(L: Plua_State; TableIndex: Integer; const Key: string; B: Boolean);
|
|
|
begin
|
|
|
LuaPushKeyString(L, TableIndex, Key);
|
|
|
- lua_pushboolean(L, B);
|
|
|
+ lua_pushboolean(L, Integer(B));
|
|
|
lua_settable(L, TableIndex);
|
|
|
end;
|
|
|
|
|
@@ -621,6 +621,17 @@ begin
|
|
|
end;
|
|
|
end;
|
|
|
|
|
|
+procedure LuaSetTablePropertyFuncs(L: PLua_State; TableIndex: Integer;
|
|
|
+ ReadFunc: lua_CFunction;
|
|
|
+ WriteFunc: lua_CFunction =nil);
|
|
|
+begin
|
|
|
+ if assigned(ReadFunc)
|
|
|
+ then LuaSetMetaFunction(L, TableIndex, '__index', ReadFunc);
|
|
|
+
|
|
|
+ if assigned(WriteFunc)
|
|
|
+ then LuaSetMetaFunction(L, TableIndex, '__newindex', WriteFunc);
|
|
|
+end;
|
|
|
+
|
|
|
procedure LuaRawSetTableValue(L: PLua_State; TableIndex: Integer; const Key: string; ValueIndex: Integer);
|
|
|
begin
|
|
|
TableIndex := LuaAbsIndex(L, TableIndex);
|
|
@@ -640,7 +651,7 @@ end;
|
|
|
procedure LuaRawSetTableBoolean(L: Plua_State; TableIndex: Integer; const Key: string; B: Boolean);
|
|
|
begin
|
|
|
LuaPushKeyString(L, TableIndex, Key);
|
|
|
- lua_pushboolean(L, B);
|
|
|
+ lua_pushboolean(L, Integer(B));
|
|
|
lua_rawset(L, TableIndex);
|
|
|
end;
|
|
|
|
|
@@ -691,11 +702,11 @@ function LuaGetMetaFunction(L: Plua_State; Index: Integer; Key: string): lua_CFu
|
|
|
begin
|
|
|
Result := nil;
|
|
|
Index := LuaAbsIndex(L, Index);
|
|
|
- if not lua_getmetatable(L, Index) then
|
|
|
+ if (lua_getmetatable(L, Index) = 0) then
|
|
|
Exit;
|
|
|
|
|
|
LuaGetTable(L, -1, Key);
|
|
|
- if lua_iscfunction(L, -1) then
|
|
|
+ if (lua_iscfunction(L, -1) <> 0) then
|
|
|
Result := lua_tocfunction(L, -1);
|
|
|
lua_pop(L, 2);
|
|
|
end;
|
|
@@ -720,7 +731,7 @@ procedure LuaSetMetaFunction(L: Plua_State; Index: Integer; Key: string; F: lua_
|
|
|
// ”�¶‚³‚¹‚Ä‚¢‚é�B
|
|
|
begin
|
|
|
Index := LuaAbsIndex(L, Index);
|
|
|
- if not lua_getmetatable(L, Index) then
|
|
|
+ if (lua_getmetatable(L, Index) = 0) then
|
|
|
lua_newtable(L);
|
|
|
|
|
|
LuaRawSetTableFunction(L, -1, Key, F);
|
|
@@ -733,14 +744,13 @@ end;
|
|
|
// Boolean: True/False
|
|
|
// stirng : "..."
|
|
|
// Table : { Key1=Value Key2=Value }
|
|
|
-function LuaStackToStr(L: Plua_State; Index: Integer; MaxTable: Integer; SubTableMax: Integer; CheckCyclicReferencing: Boolean; TablePtrs: TList): String;
|
|
|
+function LuaStackToStr(L: Plua_State; Index: Integer; MaxTable: Integer; SubTableMax: Integer): string;
|
|
|
var
|
|
|
pGLobalsIndexPtr: Pointer;
|
|
|
- bToFree: Boolean;
|
|
|
|
|
|
- function TableToStr(Index: Integer): String;
|
|
|
+ function TableToStr(Index: Integer): string;
|
|
|
var
|
|
|
- Key, Value: String;
|
|
|
+ Key, Value: string;
|
|
|
Count: Integer;
|
|
|
|
|
|
begin
|
|
@@ -748,7 +758,6 @@ var
|
|
|
Count := 0;
|
|
|
lua_pushnil(L);
|
|
|
|
|
|
-
|
|
|
// Go through the current table
|
|
|
while (lua_next(L, Index) <> 0) do
|
|
|
begin
|
|
@@ -762,22 +771,15 @@ var
|
|
|
|
|
|
// Key to string
|
|
|
if lua_type(L, -2) = LUA_TNUMBER then
|
|
|
- Key := '[' + Dequote(LuaStackToStr(L, -2, MaxTable, SubTableMax, CheckCyclicReferencing, TablePtrs)) + ']'
|
|
|
+ Key := '[' + Dequote(LuaStackToStr(L, -2, MaxTable, SubTableMax)) + ']'
|
|
|
else
|
|
|
- Key := Dequote(LuaStackToStr(L, -2, MaxTable, SubTableMax, CheckCyclicReferencing, TablePtrs));
|
|
|
+ Key := Dequote(LuaStackToStr(L, -2, MaxTable, SubTableMax));
|
|
|
|
|
|
// Value to string...
|
|
|
if ((Key = '_G') or (lua_topointer(L, -1) = pGLobalsIndexPtr)) then
|
|
|
Value := LuaGlobalVariableStr
|
|
|
- else if lua_type(L, -1) = LUA_TTABLE then
|
|
|
- begin
|
|
|
- if Assigned(TablePtrs) and CheckCyclicReferencing and (TablePtrs.IndexOf(lua_topointer(L, -1)) <> -1) then
|
|
|
- Value := '[CYCLIC_REFERENCING_DETECTED]'
|
|
|
- else
|
|
|
- Value := LuaStackToStr(L, -1, MaxTable, SubTableMax, CheckCyclicReferencing, TablePtrs);
|
|
|
- end
|
|
|
else
|
|
|
- Value := LuaStackToStr(L, -1, MaxTable, SubTableMax, CheckCyclicReferencing, TablePtrs);
|
|
|
+ Value := LuaStackToStr(L, -1, MaxTable, SubTableMax);
|
|
|
|
|
|
if (lua_type(L, -1) = LUA_TFUNCTION) then
|
|
|
Result := Result + Format('%s()=%p ', [Key, lua_topointer(L, -1)])
|
|
@@ -790,15 +792,8 @@ var
|
|
|
|
|
|
Result := Result + '}';
|
|
|
end;
|
|
|
-begin
|
|
|
- bToFree := False;
|
|
|
-
|
|
|
- if not Assigned(TablePtrs) then
|
|
|
- begin
|
|
|
- TablePtrs := TList.Create;
|
|
|
- bToFree := True;
|
|
|
- end;
|
|
|
|
|
|
+begin
|
|
|
if (MaxTable < 0) then
|
|
|
MaxTable := DefaultMaxTable;
|
|
|
|
|
@@ -812,35 +807,22 @@ begin
|
|
|
LUA_TNUMBER:
|
|
|
Result := Format('%g', [lua_tonumber(L, Index)]);
|
|
|
LUA_TBOOLEAN:
|
|
|
- Result := BoolToStr(lua_toboolean(L, Index), True);
|
|
|
+ Result := BoolToStr(lua_toboolean(L, Index) <> 0, True);
|
|
|
LUA_TSTRING:
|
|
|
Result := '"'+lua_tostring(L, Index)+'"';
|
|
|
LUA_TTABLE:
|
|
|
begin
|
|
|
if SubTableCount < SubTableMax then
|
|
|
begin
|
|
|
- if Assigned(TablePtrs) and CheckCyclicReferencing and (TablePtrs.IndexOf(lua_topointer(L, Index)) <> -1) then
|
|
|
- begin
|
|
|
- Result := '[CYCLIC_REFERENCING_DETECTED]'
|
|
|
- end
|
|
|
- else
|
|
|
- begin
|
|
|
- if Assigned(TablePtrs) then
|
|
|
- TablePtrs.Add(lua_topointer(L, Index));
|
|
|
-
|
|
|
- SubTableCount := SubTableCount + 1;
|
|
|
- Result := TableToStr(Index);
|
|
|
- SubTableCount := SubTableCount - 1;
|
|
|
-
|
|
|
- //if Assigned(TablePtrs) then
|
|
|
- //TablePtrs.Delete(TablePtrs.IndexOf(lua_topointer(L, Index)));
|
|
|
- end;
|
|
|
+ SubTableCount := SubTableCount + 1;
|
|
|
+ Result := TableToStr(Index);
|
|
|
+ SubTableCount := SubTableCount - 1;
|
|
|
end
|
|
|
else
|
|
|
Result := '[SUB_TABLE_MAX_LEVEL_HAS_BEEN_REACHED]';
|
|
|
end;
|
|
|
LUA_TFUNCTION:
|
|
|
- if lua_iscfunction(L, Index) then
|
|
|
+ if (lua_iscfunction(L, Index) <> 0) then
|
|
|
Result := Format('CFUNC:%p', [Pointer(lua_tocfunction(L, Index))])
|
|
|
else
|
|
|
Result := Format('FUNC:%p', [lua_topointer(L, Index)]);
|
|
@@ -853,9 +835,6 @@ begin
|
|
|
else
|
|
|
Assert(False);
|
|
|
end;
|
|
|
-
|
|
|
- if bToFree then
|
|
|
- TablePtrs.Free;
|
|
|
end;
|
|
|
|
|
|
procedure LuaShowStack(L: Plua_State; Caption: string);
|
|
@@ -865,12 +844,11 @@ var
|
|
|
begin
|
|
|
N := lua_gettop(L);
|
|
|
S := '[' + Caption + ']';
|
|
|
-
|
|
|
for I := N downto 1 do
|
|
|
begin
|
|
|
- S := S + CRLF + Format('%3d,%3d:%s', [LuaAbsIndex(L, I), LuaIncIndex(L, I), LuaStackToStr(L, I, -1)]);
|
|
|
+ S := S + CRLF + Format('%3d,%3d:%s', [LuaAbsIndex(L, I), LuaIncIndex(L, I),
|
|
|
+ LuaStackToStr(L, I, -1)]);
|
|
|
end;
|
|
|
-
|
|
|
ShowMessage(S);
|
|
|
end;
|
|
|
|
|
@@ -976,19 +954,16 @@ begin
|
|
|
lua_pop(L, Count + 1);
|
|
|
end;
|
|
|
|
|
|
-procedure LuaStackToStrings(L: Plua_State; Lines: TStrings; MaxTable: Integer; SubTableMax: Integer; CheckCyclicReferencing: Boolean);
|
|
|
+procedure LuaStackToStrings(L: Plua_State; Lines: TStrings; MaxTable: Integer = -1; SubTableMax: Integer = 99);
|
|
|
var
|
|
|
I: Integer;
|
|
|
begin
|
|
|
Lines.Clear;
|
|
|
-
|
|
|
for I := lua_gettop(L) downto 1 do
|
|
|
- begin
|
|
|
- Lines.Add(LuaStackToStr(L, I, MaxTable, SubTableMax, CheckCyclicReferencing));
|
|
|
- end;
|
|
|
+ Lines.Add(LuaStackToStr(L, I, MaxTable, SubTableMax));
|
|
|
end;
|
|
|
|
|
|
-procedure LuaLocalToStrings(L: Plua_State; Lines: TStrings; MaxTable: Integer; Level: Integer; SubTableMax: Integer; CheckCyclicReferencing: Boolean);
|
|
|
+procedure LuaLocalToStrings(L: Plua_State; Lines: TStrings; MaxTable: Integer; Level: Integer; SubTableMax: Integer);
|
|
|
var
|
|
|
Name: PChar;
|
|
|
Index: Integer;
|
|
@@ -998,129 +973,87 @@ begin
|
|
|
AR := @Debug;
|
|
|
Lines.Clear;
|
|
|
Index := 1;
|
|
|
-
|
|
|
if (lua_getstack(L, Level, AR) = 0) then
|
|
|
Exit;
|
|
|
|
|
|
Name := lua_getlocal(L, AR, Index);
|
|
|
-
|
|
|
while (Name <> nil) do
|
|
|
begin
|
|
|
- Lines.Values[Name] := LuaStackToStr(L, -1, MaxTable, SubTableMax, CheckCyclicReferencing);
|
|
|
+ Lines.Values[Name] := LuaStackToStr(L, -1, MaxTable, SubTableMax);
|
|
|
lua_pop(L, 1);
|
|
|
Inc(Index);
|
|
|
Name := lua_getlocal(L, AR, Index);
|
|
|
end;
|
|
|
end;
|
|
|
|
|
|
-procedure LuaGlobalToStrings(L: PLua_State; Lines: TStrings; MaxTable: Integer; SubTableMax: Integer; CheckCyclicReferencing: Boolean);
|
|
|
+procedure LuaGlobalToStrings(L: PLua_State; Lines: TStrings; MaxTable: Integer; SubTableMax: Integer);
|
|
|
begin
|
|
|
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
|
|
LuaTableToStrings(L, -1, Lines, MaxTable, SubTableMax);
|
|
|
lua_pop(L, 1);
|
|
|
end;
|
|
|
|
|
|
-procedure LuaTableToStrings(L: Plua_State; Index: Integer; Lines: TStrings; MaxTable: Integer; SubTableMax: Integer; CheckCyclicReferencing: Boolean);
|
|
|
+procedure LuaTableToStrings(L: Plua_State; Index: Integer; Lines: TStrings; MaxTable: Integer; SubTableMax: Integer);
|
|
|
var
|
|
|
Key, Value: string;
|
|
|
begin
|
|
|
Index := LuaAbsIndex(L, Index);
|
|
|
Lines.Clear;
|
|
|
+
|
|
|
lua_pushnil(L);
|
|
|
-
|
|
|
while (lua_next(L, Index) <> 0) do
|
|
|
begin
|
|
|
- Key := Dequote(LuaStackToStr(L, -2, MaxTable, SubTableMax, CheckCyclicReferencing));
|
|
|
- Value := LuaStackToStr(L, -1, MaxTable, SubTableMax, CheckCyclicReferencing);
|
|
|
+ Key := Dequote(LuaStackToStr(L, -2, MaxTable, SubTableMax));
|
|
|
+ Value := LuaStackToStr(L, -1, MaxTable, SubTableMax);
|
|
|
Lines.Values[Key] := Value;
|
|
|
lua_pop(L, 1);
|
|
|
end;
|
|
|
end;
|
|
|
|
|
|
-procedure LuaTableToVirtualTreeView(L: Plua_State; Index: Integer; VTV: TVirtualStringTree; MaxTable: Integer; SubTableMax: Integer; CheckCyclicReferencing: Boolean);
|
|
|
+procedure LuaTableToTreeView(L: Plua_State; Index: Integer; TV: TTreeView; MaxTable: Integer; SubTableMax: Integer);
|
|
|
var
|
|
|
pGLobalsIndexPtr: Pointer;
|
|
|
- PtrsList: TList;
|
|
|
- pTreeNodeData: PBasicTreeData;
|
|
|
|
|
|
// Go through all child of current table and create nodes
|
|
|
- procedure ParseTreeNode(TreeNode: PVirtualNode; Index: Integer);
|
|
|
+ procedure ParseTreeNode(TreeNode: TTreeNode; Index: Integer);
|
|
|
var
|
|
|
Key: string;
|
|
|
- pData: PBasicTreeData;
|
|
|
- pNode: PVirtualNode;
|
|
|
begin
|
|
|
- // Retreive absolute index
|
|
|
Index := LuaAbsIndex(L, Index);
|
|
|
- lua_pushnil(L);
|
|
|
|
|
|
+ lua_pushnil(L);
|
|
|
while (lua_next(L, Index) <> 0) do
|
|
|
begin
|
|
|
- if Assigned(TreeNode) then
|
|
|
- pTreeNodeData := VTV.GetNodeData(TreeNode)
|
|
|
+ Key := Dequote(LuaStackToStr(L, -2, MaxTable, SubTableMax));
|
|
|
+ if (lua_type(L, -1) <> LUA_TTABLE) then
|
|
|
+ TV.Items.AddChild(TreeNode, Key + '=' + LuaStackToStr(L, -1, MaxTable, SubTableMax))
|
|
|
else
|
|
|
- pTreeNodeData := nil;
|
|
|
-
|
|
|
- if (pTreeNodeData = nil) or (pTreeNodeData.sValue <> '[CYCLIC_REFERENCING_DETECTED]') then
|
|
|
begin
|
|
|
- Key := Dequote(LuaStackToStr(L, -2, MaxTable, SubTableMax, CheckCyclicReferencing));
|
|
|
-
|
|
|
- if lua_type(L, -1) <> LUA_TTABLE then
|
|
|
+ if ((Key = '_G') or (lua_topointer(L, -1) = pGLobalsIndexPtr)) then
|
|
|
begin
|
|
|
- pData := VTV.GetNodeData(VTV.AddChild(TreeNode));
|
|
|
- pData.sName := Key;
|
|
|
- pData.sValue := LuaStackToStr(L, -1, MaxTable, SubTableMax, CheckCyclicReferencing);
|
|
|
+ TV.Items.AddChild(TreeNode, Key + '=[LUA_GLOBALSINDEX]')
|
|
|
end
|
|
|
else
|
|
|
begin
|
|
|
- if ((Key = '_G') or (lua_topointer(L, -1) = pGLobalsIndexPtr)) then
|
|
|
- begin
|
|
|
- pData := VTV.GetNodeData(VTV.AddChild(TreeNode));
|
|
|
- pData.sName := Key;
|
|
|
- pData.sValue := '[LUA_GLOBALSINDEX]';
|
|
|
- end
|
|
|
- else
|
|
|
+ if SubTableCount < SubTableMax then
|
|
|
begin
|
|
|
- pNode := VTV.AddChild(TreeNode);
|
|
|
- pData := VTV.GetNodeData(pNode);
|
|
|
- pData.sName := Key;
|
|
|
-
|
|
|
- if CheckCyclicReferencing and (PtrsList.IndexOf(lua_topointer(L, -1)) <> -1) then
|
|
|
- pData.sValue := '[CYCLIC_REFERENCING_DETECTED]'
|
|
|
- else
|
|
|
- pData.sValue := LuaStackToStr(L, -1, MaxTable, SubTableMax, CheckCyclicReferencing);
|
|
|
-
|
|
|
- if SubTableCount < SubTableMax then
|
|
|
- begin
|
|
|
- if CheckCyclicReferencing then
|
|
|
- PtrsList.Add(lua_topointer(L, -1));
|
|
|
-
|
|
|
- SubTableCount := SubTableCount + 1;
|
|
|
- ParseTreeNode(pNode, -1);
|
|
|
- SubTableCount := SubTableCount - 1;
|
|
|
-
|
|
|
- if not Assigned(TreeNode) then
|
|
|
- PtrsList.Clear;
|
|
|
- end;
|
|
|
+ SubTableCount := SubTableCount + 1;
|
|
|
+ ParseTreeNode(TV.Items.AddChild(TreeNode, Key), -1);
|
|
|
+ SubTableCount := SubTableCount - 1;
|
|
|
end;
|
|
|
end;
|
|
|
end;
|
|
|
-
|
|
|
lua_pop(L, 1);
|
|
|
end;
|
|
|
end;
|
|
|
begin
|
|
|
- PtrsList := TList.Create;
|
|
|
Assert(lua_type(L, Index) = LUA_TTABLE);
|
|
|
- lua_checkstack(L, SubTableMax * 3); // Ensure there is enough space on stack to work with according to user's setting
|
|
|
- pGLobalsIndexPtr := lua_topointer(L, LUA_GLOBALSINDEX); // Retrieve globals index pointer for later conditions
|
|
|
- VTV.BeginUpdate;
|
|
|
- VTV.Clear;
|
|
|
+ TV.Items.BeginUpdate;
|
|
|
+ TV.Items.Clear;
|
|
|
try
|
|
|
ParseTreeNode(nil, Index);
|
|
|
finally
|
|
|
- VTV.EndUpdate;
|
|
|
- PtrsList.Free;
|
|
|
+ TV.Items.EndUpdate;
|
|
|
end;
|
|
|
end;
|
|
|
|
|
@@ -1156,13 +1089,11 @@ begin
|
|
|
Mask := lua_gethookmask(L);
|
|
|
Count := lua_gethookcount(L);
|
|
|
lua_sethook(L, Hook, 0, Count);
|
|
|
-
|
|
|
if (lua_pcall(L, 0, 0, 0) = 0) then
|
|
|
LuaRawGetTable(L, LUA_GLOBALSINDEX, DebugValue);
|
|
|
-
|
|
|
- Result := LuaStackToStr(L, -1, MaxTable, MaxTable, True);
|
|
|
+ Result := LuaStackToStr(L, -1, MaxTable);
|
|
|
lua_remove(L, -1);
|
|
|
- luaL_dostring(L, DebugValue + '=nil');
|
|
|
+ lua_dostring(L, DebugValue + '=nil');
|
|
|
lua_sethook(L, Hook, Mask, Count);
|
|
|
end;
|
|
|
|
|
@@ -1261,7 +1192,7 @@ begin
|
|
|
if (luaL_loadbuffer(L, PChar(Code), Length(Code), PChar(Name)) = 0) then
|
|
|
Exit;
|
|
|
|
|
|
- LuaProcessErrorMessage(LuaStackToStr(L, -1, -1, 99, True), Title, Line, Msg);
|
|
|
+ LuaProcessErrorMessage(LuaStackToStr(L, -1, -1), Title, Line, Msg);
|
|
|
raise ELuaException.Create(Title, Line, Msg);
|
|
|
end;
|
|
|
|
|
@@ -1286,13 +1217,19 @@ begin
|
|
|
if (lua_pcall(L, NArgs, NResults, ErrFunc) = 0) then
|
|
|
Exit;
|
|
|
|
|
|
- LuaProcessErrorMessage(Dequote(LuaStackToStr(L, -1, -1, 99, True)), Title, Line, Msg);
|
|
|
+ LuaProcessErrorMessage(Dequote(LuaStackToStr(L, -1, -1)),
|
|
|
+ Title, Line, Msg);
|
|
|
raise ELuaException.Create(Title, Line, Msg);
|
|
|
end;
|
|
|
|
|
|
-function LuaPCallFunction(L: Plua_State; FunctionName :String; const Args: array of Variant; Results : PVariantArray; ErrFunc: Integer=0; NResults :Integer=LUA_MULTRET):Integer;
|
|
|
+function LuaPCallFunction(L: Plua_State; FunctionName :String;
|
|
|
+ const Args: array of Variant;
|
|
|
+ Results : PVariantArray;
|
|
|
+ ErrFunc: Integer=0;
|
|
|
+ NResults :Integer=LUA_MULTRET):Integer;
|
|
|
var
|
|
|
NArgs, i: Integer;
|
|
|
+
|
|
|
begin
|
|
|
//Put Function To Call on the Stack
|
|
|
luaPushString(L, FunctionName);
|
|
@@ -1590,6 +1527,26 @@ begin
|
|
|
lua_error(L);
|
|
|
end;
|
|
|
|
|
|
+function LuaGetCurrentFuncName(L: Plua_State) :String;
|
|
|
+var
|
|
|
+ ar :lua_Debug;
|
|
|
+
|
|
|
+begin
|
|
|
+ FillChar(ar, sizeof(ar), 0);
|
|
|
+ Result :='';
|
|
|
+
|
|
|
+ if (lua_getStack(L, 0, @ar) = 1)
|
|
|
+ then begin
|
|
|
+ lua_getinfo(L, 'n', @ar);
|
|
|
+ Result :=ar.name;
|
|
|
+ end;
|
|
|
+end;
|
|
|
+
|
|
|
+function Lua_VoidFunc(L: Plua_State): Integer; cdecl;
|
|
|
+begin
|
|
|
+ Result := 0;
|
|
|
+end;
|
|
|
+
|
|
|
initialization
|
|
|
DefaultMaxTable := 256;
|
|
|
|