Browse Source

CH: Support of Lua 5.1

Jean-Francois Goulet 19 years ago
parent
commit
8cc1eeca7b
5 changed files with 1081 additions and 541 deletions
  1. 129 116
      LuaEdit/LuaCore/LuaUtils.pas
  2. 40 41
      LuaEdit/LuaCore/lauxlib.pas
  3. 855 365
      LuaEdit/LuaCore/lua.pas
  4. 55 17
      LuaEdit/LuaCore/lualib.pas
  5. 2 2
      LuaEdit/Syntax/LuaSyntax.pas

+ 129 - 116
LuaEdit/LuaCore/LuaUtils.pas

@@ -109,17 +109,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 LuaSetMetaFunction(L: Plua_State; Index: Integer; Key: string; F: lua_CFunction);
 
 
 procedure LuaShowStack(L: Plua_State; Caption: string = '');
 procedure LuaShowStack(L: Plua_State; Caption: string = '');
-function LuaStackToStr(L: Plua_State; Index: Integer; MaxTable: Integer = -1; SubTableMax: Integer = 99): string;
+function LuaStackToStr(L: Plua_State; Index: Integer; MaxTable: Integer = -1; SubTableMax: Integer = 99; CheckCyclicReferencing: Boolean = True; TablePtrs: TList = nil): String;
 procedure LuaRegisterCustom(L: PLua_State; TableIndex: Integer; const Name: PChar; F: lua_CFunction);
 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 LuaRegister(L: Plua_State; const Name: PChar; F: lua_CFunction);
 procedure LuaRegisterMetatable(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 LuaRegisterProperty(L: PLua_State; const Name: PChar; ReadFunc, WriteFunc: lua_CFunction);
-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 LuaTableToVirtualTreeView(L: Plua_State; Index: Integer; VTV: TVirtualStringTree; MaxTable: Integer; SubTableMax: Integer);
-procedure LuaTableToTreeView(L: Plua_State; Index: Integer; TV: TTreeView; MaxTable: Integer = -1; SubTableMax: Integer = 99);
+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);
 function LuaGetIdentValue(L: Plua_State; Ident: string; MaxTable: Integer = -1): string;
 function LuaGetIdentValue(L: Plua_State; Ident: string; MaxTable: Integer = -1): string;
 procedure LuaSetIdentValue(L: Plua_State; Ident, Value: string; MaxTable: Integer = -1);
 procedure LuaSetIdentValue(L: Plua_State; Ident, Value: string; MaxTable: Integer = -1);
 procedure LuaLoadBuffer(L: Plua_State; const Code: string; const Name: string);
 procedure LuaLoadBuffer(L: Plua_State; const Code: string; const Name: string);
@@ -187,7 +186,7 @@ end;
 
 
 function fputsex(const F, S: String; L, Dummy: Integer): Integer;
 function fputsex(const F, S: String; L, Dummy: Integer): Integer;
 begin
 begin
-  Result := fwriteex(PChar(F), PChar(S), SizeOf(Char), L, Length(S), Dummy);
+  Result := fwriteex(PChar(F), PChar(S), SizeOf(Char), Length(S), L, Dummy);
 end;
 end;
 
 
 function fputs(const S: string; Dummy: Integer): Integer;
 function fputs(const S: string; Dummy: Integer): Integer;
@@ -266,7 +265,7 @@ end;
 function lua_io_writeex(L: Plua_State): Integer; cdecl;
 function lua_io_writeex(L: Plua_State): Integer; cdecl;
   function pushresult(L: Plua_State; I: Boolean; FileName: PChar): Integer;
   function pushresult(L: Plua_State; I: Boolean; FileName: PChar): Integer;
   begin
   begin
-    lua_pushboolean(L, 1);
+    lua_pushboolean(L, True);
     Result := 1;
     Result := 1;
   end;
   end;
 const
 const
@@ -310,7 +309,7 @@ end;
 function lua_io_write(L: Plua_State): Integer; cdecl;
 function lua_io_write(L: Plua_State): Integer; cdecl;
   function pushresult(L: Plua_State; I: Boolean; FileName: PChar): Integer;
   function pushresult(L: Plua_State; I: Boolean; FileName: PChar): Integer;
   begin
   begin
-    lua_pushboolean(L, 1);
+    lua_pushboolean(L, True);
     Result := 1;
     Result := 1;
   end;
   end;
 const
 const
@@ -345,12 +344,12 @@ end;
 
 
 function LuaToBoolean(L: PLua_State; Index: Integer): Boolean;
 function LuaToBoolean(L: PLua_State; Index: Integer): Boolean;
 begin
 begin
-  Result := (lua_toboolean(L, Index) <> 0);
+  Result := (lua_toboolean(L, Index) <> False);
 end;
 end;
 
 
 procedure LuaPushBoolean(L: PLua_State; B: Boolean);
 procedure LuaPushBoolean(L: PLua_State; B: Boolean);
 begin
 begin
-  lua_pushboolean(L, Integer(B));
+  lua_pushboolean(L, B);
 end;
 end;
 
 
 function LuaToInteger(L: PLua_State; Index: Integer): Integer;
 function LuaToInteger(L: PLua_State; Index: Integer): Integer;
@@ -462,7 +461,7 @@ end;
 function LuaGetTableBoolean(L: Plua_State; TableIndex: Integer; const Key: string): Boolean;
 function LuaGetTableBoolean(L: Plua_State; TableIndex: Integer; const Key: string): Boolean;
 begin
 begin
   LuaGetTable(L, TableIndex, Key);
   LuaGetTable(L, TableIndex, Key);
-  Result := (lua_toboolean(L, -1) <> 0);
+  Result := (lua_toboolean(L, -1) <> False);
   lua_pop(L, 1);
   lua_pop(L, 1);
 end;
 end;
 
 
@@ -516,7 +515,7 @@ end;
 function LuaRawGetTableBoolean(L: Plua_State; TableIndex: Integer; const Key: string): Boolean;
 function LuaRawGetTableBoolean(L: Plua_State; TableIndex: Integer; const Key: string): Boolean;
 begin
 begin
   LuaRawGetTable(L, TableIndex, Key);
   LuaRawGetTable(L, TableIndex, Key);
-  Result := (lua_toboolean(L, -1) <> 0);
+  Result := (lua_toboolean(L, -1) <> False);
   lua_pop(L, 1);
   lua_pop(L, 1);
 end;
 end;
 
 
@@ -567,7 +566,7 @@ end;
 procedure LuaSetTableBoolean(L: Plua_State; TableIndex: Integer; const Key: string; B: Boolean);
 procedure LuaSetTableBoolean(L: Plua_State; TableIndex: Integer; const Key: string; B: Boolean);
 begin
 begin
   LuaPushKeyString(L, TableIndex, Key);
   LuaPushKeyString(L, TableIndex, Key);
-  lua_pushboolean(L, Integer(B));
+  lua_pushboolean(L, B);
   lua_settable(L, TableIndex);
   lua_settable(L, TableIndex);
 end;
 end;
 
 
@@ -638,7 +637,7 @@ end;
 procedure LuaRawSetTableBoolean(L: Plua_State; TableIndex: Integer; const Key: string; B: Boolean); 
 procedure LuaRawSetTableBoolean(L: Plua_State; TableIndex: Integer; const Key: string; B: Boolean); 
 begin
 begin
   LuaPushKeyString(L, TableIndex, Key);
   LuaPushKeyString(L, TableIndex, Key);
-  lua_pushboolean(L, Integer(B));
+  lua_pushboolean(L, B);
   lua_rawset(L, TableIndex);
   lua_rawset(L, TableIndex);
 end;
 end;
 
 
@@ -689,11 +688,11 @@ function LuaGetMetaFunction(L: Plua_State; Index: Integer; Key: string): lua_CFu
 begin
 begin
   Result := nil;
   Result := nil;
   Index := LuaAbsIndex(L, Index);
   Index := LuaAbsIndex(L, Index);
-  if (lua_getmetatable(L, Index) = 0) then
+  if not lua_getmetatable(L, Index) then
     Exit;
     Exit;
 
 
   LuaGetTable(L, -1, Key);
   LuaGetTable(L, -1, Key);
-  if (lua_iscfunction(L, -1) <> 0) then
+  if lua_iscfunction(L, -1) then
     Result := lua_tocfunction(L, -1);
     Result := lua_tocfunction(L, -1);
   lua_pop(L, 2);
   lua_pop(L, 2);
 end;
 end;
@@ -718,7 +717,7 @@ procedure LuaSetMetaFunction(L: Plua_State; Index: Integer; Key: string; F: lua_
 // ”­�¶‚³‚¹‚Ä‚¢‚é�B
 // ”­�¶‚³‚¹‚Ä‚¢‚é�B
 begin
 begin
   Index := LuaAbsIndex(L, Index);
   Index := LuaAbsIndex(L, Index);
-  if (lua_getmetatable(L, Index) = 0) then
+  if not lua_getmetatable(L, Index) then
     lua_newtable(L);
     lua_newtable(L);
 
 
   LuaRawSetTableFunction(L, -1, Key, F);
   LuaRawSetTableFunction(L, -1, Key, F);
@@ -731,13 +730,14 @@ end;
 // Boolean: True/False
 // Boolean: True/False
 // stirng : "..."
 // stirng : "..."
 // Table  : { Key1=Value Key2=Value }
 // Table  : { Key1=Value Key2=Value }
-function LuaStackToStr(L: Plua_State; Index: Integer; MaxTable: Integer; SubTableMax: Integer): string;
+function LuaStackToStr(L: Plua_State; Index: Integer; MaxTable: Integer; SubTableMax: Integer; CheckCyclicReferencing: Boolean; TablePtrs: TList): String;
 var
 var
   pGLobalsIndexPtr: Pointer;
   pGLobalsIndexPtr: Pointer;
+  bToFree: Boolean;
 
 
-  function TableToStr(Index: Integer): string;
+  function TableToStr(Index: Integer): String;
   var
   var
-    Key, Value: string;
+    Key, Value: String;
     Count: Integer;
     Count: Integer;
 
 
   begin
   begin
@@ -759,15 +759,22 @@ var
 
 
       // Key to string
       // Key to string
       if lua_type(L, -2) = LUA_TNUMBER then
       if lua_type(L, -2) = LUA_TNUMBER then
-        Key := '[' + Dequote(LuaStackToStr(L, -2, MaxTable, SubTableMax)) + ']'
+        Key := '[' + Dequote(LuaStackToStr(L, -2, MaxTable, SubTableMax, CheckCyclicReferencing, TablePtrs)) + ']'
       else
       else
-        Key := Dequote(LuaStackToStr(L, -2, MaxTable, SubTableMax));
+        Key := Dequote(LuaStackToStr(L, -2, MaxTable, SubTableMax, CheckCyclicReferencing, TablePtrs));
 
 
       // Value to string...
       // Value to string...
       if ((Key = '_G') or (lua_topointer(L, -1) = pGLobalsIndexPtr)) then
       if ((Key = '_G') or (lua_topointer(L, -1) = pGLobalsIndexPtr)) then
         Value := LuaGlobalVariableStr
         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
       else
-        Value := LuaStackToStr(L, -1, MaxTable, SubTableMax);
+        Value := LuaStackToStr(L, -1, MaxTable, SubTableMax, CheckCyclicReferencing, TablePtrs);
 
 
       if (lua_type(L, -1) = LUA_TFUNCTION) then
       if (lua_type(L, -1) = LUA_TFUNCTION) then
         Result := Result + Format('%s()=%p ', [Key, lua_topointer(L, -1)])
         Result := Result + Format('%s()=%p ', [Key, lua_topointer(L, -1)])
@@ -778,10 +785,17 @@ var
       lua_pop(L, 1);
       lua_pop(L, 1);
     end;
     end;
 
 
-
     Result := Result + '}';
     Result := Result + '}';
   end;
   end;
 begin
 begin
+  bToFree := False;
+
+  if not Assigned(TablePtrs) then
+  begin
+    TablePtrs := TList.Create;
+    bToFree := True;
+  end;
+
   if (MaxTable < 0) then
   if (MaxTable < 0) then
     MaxTable := DefaultMaxTable;
     MaxTable := DefaultMaxTable;
 
 
@@ -795,22 +809,35 @@ begin
   LUA_TNUMBER:
   LUA_TNUMBER:
     Result := Format('%g', [lua_tonumber(L, Index)]);
     Result := Format('%g', [lua_tonumber(L, Index)]);
   LUA_TBOOLEAN:
   LUA_TBOOLEAN:
-    Result := BoolToStr(lua_toboolean(L, Index) <> 0, True);
+    Result := BoolToStr(lua_toboolean(L, Index), True);
   LUA_TSTRING:
   LUA_TSTRING:
     Result := '"'+lua_tostring(L, Index)+'"';
     Result := '"'+lua_tostring(L, Index)+'"';
   LUA_TTABLE:
   LUA_TTABLE:
   begin
   begin
     if SubTableCount < SubTableMax then
     if SubTableCount < SubTableMax then
     begin
     begin
-      SubTableCount := SubTableCount + 1;
-      Result := TableToStr(Index);
-      SubTableCount := SubTableCount - 1;
+      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;
     end
     end
     else
     else
       Result := '[SUB_TABLE_MAX_LEVEL_HAS_BEEN_REACHED]';
       Result := '[SUB_TABLE_MAX_LEVEL_HAS_BEEN_REACHED]';
   end;
   end;
   LUA_TFUNCTION:
   LUA_TFUNCTION:
-    if (lua_iscfunction(L, Index) <> 0) then
+    if lua_iscfunction(L, Index) then
       Result := Format('CFUNC:%p', [Pointer(lua_tocfunction(L, Index))])
       Result := Format('CFUNC:%p', [Pointer(lua_tocfunction(L, Index))])
     else
     else
       Result := Format('FUNC:%p', [lua_topointer(L, Index)]);
       Result := Format('FUNC:%p', [lua_topointer(L, Index)]);
@@ -823,6 +850,9 @@ begin
   else
   else
     Assert(False);
     Assert(False);
   end;
   end;
+
+  if bToFree then
+    TablePtrs.Free;
 end;
 end;
 
 
 procedure LuaShowStack(L: Plua_State; Caption: string);
 procedure LuaShowStack(L: Plua_State; Caption: string);
@@ -832,11 +862,12 @@ var
 begin
 begin
   N := lua_gettop(L);
   N := lua_gettop(L);
   S := '[' + Caption + ']';
   S := '[' + Caption + ']';
+  
   for I := N downto 1 do
   for I := N downto 1 do
   begin
   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;
   end;
+
   ShowMessage(S);
   ShowMessage(S);
 end;
 end;
 
 
@@ -942,16 +973,19 @@ begin
   lua_pop(L, Count + 1);
   lua_pop(L, Count + 1);
 end;
 end;
 
 
-procedure LuaStackToStrings(L: Plua_State; Lines: TStrings; MaxTable: Integer; SubTableMax: Integer);
+procedure LuaStackToStrings(L: Plua_State; Lines: TStrings; MaxTable: Integer; SubTableMax: Integer; CheckCyclicReferencing: Boolean);
 var
 var
   I: Integer;
   I: Integer;
 begin
 begin
   Lines.Clear;
   Lines.Clear;
+  
   for I := lua_gettop(L) downto 1 do
   for I := lua_gettop(L) downto 1 do
-    Lines.Add(LuaStackToStr(L, I, MaxTable, SubTableMax));
+  begin
+    Lines.Add(LuaStackToStr(L, I, MaxTable, SubTableMax, CheckCyclicReferencing));
+  end;
 end;
 end;
 
 
-procedure LuaLocalToStrings(L: Plua_State; Lines: TStrings; MaxTable: Integer; Level: Integer; SubTableMax: Integer);
+procedure LuaLocalToStrings(L: Plua_State; Lines: TStrings; MaxTable: Integer; Level: Integer; SubTableMax: Integer; CheckCyclicReferencing: Boolean);
 var
 var
   Name: PChar;
   Name: PChar;
   Index: Integer;
   Index: Integer;
@@ -961,46 +995,50 @@ begin
   AR := @Debug;
   AR := @Debug;
   Lines.Clear;
   Lines.Clear;
   Index := 1;
   Index := 1;
+  
   if (lua_getstack(L, Level, AR) = 0) then
   if (lua_getstack(L, Level, AR) = 0) then
     Exit;
     Exit;
 
 
   Name := lua_getlocal(L, AR, Index);
   Name := lua_getlocal(L, AR, Index);
+  
   while (Name <> nil) do
   while (Name <> nil) do
   begin
   begin
-    Lines.Values[Name] := LuaStackToStr(L, -1, MaxTable, SubTableMax);
+    Lines.Values[Name] := LuaStackToStr(L, -1, MaxTable, SubTableMax, CheckCyclicReferencing);
     lua_pop(L, 1);
     lua_pop(L, 1);
     Inc(Index);
     Inc(Index);
     Name := lua_getlocal(L, AR, Index);
     Name := lua_getlocal(L, AR, Index);
   end;
   end;
 end;
 end;
 
 
-procedure LuaGlobalToStrings(L: PLua_State; Lines: TStrings; MaxTable: Integer; SubTableMax: Integer);
+procedure LuaGlobalToStrings(L: PLua_State; Lines: TStrings; MaxTable: Integer; SubTableMax: Integer; CheckCyclicReferencing: Boolean);
 begin
 begin
   lua_pushvalue(L, LUA_GLOBALSINDEX);
   lua_pushvalue(L, LUA_GLOBALSINDEX);
   LuaTableToStrings(L, -1, Lines, MaxTable, SubTableMax);
   LuaTableToStrings(L, -1, Lines, MaxTable, SubTableMax);
   lua_pop(L, 1);
   lua_pop(L, 1);
 end;
 end;
 
 
-procedure LuaTableToStrings(L: Plua_State; Index: Integer; Lines: TStrings; MaxTable: Integer; SubTableMax: Integer);
+procedure LuaTableToStrings(L: Plua_State; Index: Integer; Lines: TStrings; MaxTable: Integer; SubTableMax: Integer; CheckCyclicReferencing: Boolean);
 var
 var
   Key, Value: string;
   Key, Value: string;
 begin
 begin
   Index := LuaAbsIndex(L, Index);
   Index := LuaAbsIndex(L, Index);
   Lines.Clear;
   Lines.Clear;
-
   lua_pushnil(L);
   lua_pushnil(L);
+  
   while (lua_next(L, Index) <> 0) do
   while (lua_next(L, Index) <> 0) do
   begin
   begin
-    Key := Dequote(LuaStackToStr(L, -2, MaxTable, SubTableMax));
-    Value := LuaStackToStr(L, -1, MaxTable, SubTableMax);
+    Key := Dequote(LuaStackToStr(L, -2, MaxTable, SubTableMax, CheckCyclicReferencing));
+    Value := LuaStackToStr(L, -1, MaxTable, SubTableMax, CheckCyclicReferencing);
     Lines.Values[Key] := Value;
     Lines.Values[Key] := Value;
     lua_pop(L, 1);
     lua_pop(L, 1);
   end;
   end;
 end;
 end;
 
 
-procedure LuaTableToVirtualTreeView(L: Plua_State; Index: Integer; VTV: TVirtualStringTree; MaxTable: Integer; SubTableMax: Integer);
+procedure LuaTableToVirtualTreeView(L: Plua_State; Index: Integer; VTV: TVirtualStringTree; MaxTable: Integer; SubTableMax: Integer; CheckCyclicReferencing: Boolean);
 var
 var
   pGLobalsIndexPtr: Pointer;
   pGLobalsIndexPtr: Pointer;
+  PtrsList: TList;
+  pTreeNodeData: PBasicTreeData;
   
   
   // Go through all child of current table and create nodes
   // Go through all child of current table and create nodes
   procedure ParseTreeNode(TreeNode: PVirtualNode; Index: Integer);
   procedure ParseTreeNode(TreeNode: PVirtualNode; Index: Integer);
@@ -1011,45 +1049,65 @@ var
   begin
   begin
     // Retreive absolute index 
     // Retreive absolute index 
     Index := LuaAbsIndex(L, Index);
     Index := LuaAbsIndex(L, Index);
-
-
     lua_pushnil(L);
     lua_pushnil(L);
+
     while (lua_next(L, Index) <> 0) do
     while (lua_next(L, Index) <> 0) do
     begin
     begin
-      Key := Dequote(LuaStackToStr(L, -2, MaxTable, SubTableMax));
-      if lua_type(L, -1) <> LUA_TTABLE then
-      begin
-        pData := VTV.GetNodeData(VTV.AddChild(TreeNode));
-        pData.sName := Key;
-        pData.sValue := LuaStackToStr(L, -1, MaxTable, SubTableMax);
-      end
+      if Assigned(TreeNode) then
+        pTreeNodeData := VTV.GetNodeData(TreeNode)
       else
       else
+        pTreeNodeData := nil;
+
+      if (pTreeNodeData = nil) or (pTreeNodeData.sValue <> '[CYCLIC_REFERENCING_DETECTED]') then
       begin
       begin
-        if ((Key = '_G') or (lua_topointer(L, -1) = pGLobalsIndexPtr)) then
+        Key := Dequote(LuaStackToStr(L, -2, MaxTable, SubTableMax, CheckCyclicReferencing));
+
+        if lua_type(L, -1) <> LUA_TTABLE then
         begin
         begin
           pData := VTV.GetNodeData(VTV.AddChild(TreeNode));
           pData := VTV.GetNodeData(VTV.AddChild(TreeNode));
           pData.sName := Key;
           pData.sName := Key;
-          pData.sValue := '[LUA_GLOBALSINDEX]';
+          pData.sValue := LuaStackToStr(L, -1, MaxTable, SubTableMax, CheckCyclicReferencing);
         end
         end
         else
         else
         begin
         begin
-          pNode := VTV.AddChild(TreeNode);
-          pData := VTV.GetNodeData(pNode);
-          pData.sName := Key;
-          pData.sValue := LuaStackToStr(L, -1, MaxTable, SubTableMax);
-
-          if SubTableCount < SubTableMax then
+          if ((Key = '_G') or (lua_topointer(L, -1) = pGLobalsIndexPtr)) then
           begin
           begin
-            SubTableCount := SubTableCount + 1;
-            ParseTreeNode(pNode, -1);
-            SubTableCount := SubTableCount - 1;
+            pData := VTV.GetNodeData(VTV.AddChild(TreeNode));
+            pData.sName := Key;
+            pData.sValue := '[LUA_GLOBALSINDEX]';
+          end
+          else
+          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;
           end;
           end;
         end;
         end;
       end;
       end;
-        lua_pop(L, 1);
+
+      lua_pop(L, 1);
     end;
     end;
   end;
   end;
 begin
 begin
+  PtrsList := TList.Create;
   Assert(lua_type(L, Index) = LUA_TTABLE);
   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
   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
   pGLobalsIndexPtr := lua_topointer(L, LUA_GLOBALSINDEX); // Retrieve globals index pointer for later conditions
@@ -1059,53 +1117,7 @@ begin
     ParseTreeNode(nil, Index);
     ParseTreeNode(nil, Index);
   finally
   finally
     VTV.EndUpdate;
     VTV.EndUpdate;
-  end;
-end;
-
-procedure LuaTableToTreeView(L: Plua_State; Index: Integer; TV: TTreeView; MaxTable: Integer; SubTableMax: Integer);
-var
-  pGLobalsIndexPtr: Pointer;
-  
-  // Go through all child of current table and create nodes
-  procedure ParseTreeNode(TreeNode: TTreeNode; Index: Integer);
-  var
-    Key: string;
-  begin
-    Index := LuaAbsIndex(L, Index);
-
-    lua_pushnil(L);
-    while (lua_next(L, Index) <> 0) do
-    begin
-      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
-      begin
-        if ((Key = '_G') or (lua_topointer(L, -1) = pGLobalsIndexPtr)) then
-        begin
-          TV.Items.AddChild(TreeNode, Key + '=[LUA_GLOBALSINDEX]')
-        end
-        else
-        begin
-          if SubTableCount < SubTableMax then
-          begin
-            SubTableCount := SubTableCount + 1;
-            ParseTreeNode(TV.Items.AddChild(TreeNode, Key), -1);
-            SubTableCount := SubTableCount - 1;
-          end;
-        end;
-      end;
-      lua_pop(L, 1);
-    end;
-  end;
-begin
-  Assert(lua_type(L, Index) = LUA_TTABLE);
-  TV.Items.BeginUpdate;
-  TV.Items.Clear;
-  try
-    ParseTreeNode(nil, Index);
-  finally
-    TV.Items.EndUpdate;
+    PtrsList.Free;
   end;
   end;
 end;
 end;
 
 
@@ -1141,11 +1153,13 @@ begin
   Mask := lua_gethookmask(L);
   Mask := lua_gethookmask(L);
   Count := lua_gethookcount(L);
   Count := lua_gethookcount(L);
   lua_sethook(L, Hook, 0, Count);
   lua_sethook(L, Hook, 0, Count);
+
   if (lua_pcall(L, 0, 0, 0) = 0) then
   if (lua_pcall(L, 0, 0, 0) = 0) then
     LuaRawGetTable(L, LUA_GLOBALSINDEX, DebugValue);
     LuaRawGetTable(L, LUA_GLOBALSINDEX, DebugValue);
-  Result := LuaStackToStr(L, -1, MaxTable);
+
+  Result := LuaStackToStr(L, -1, MaxTable, MaxTable, True);
   lua_remove(L, -1);
   lua_remove(L, -1);
-  lua_dostring(L, DebugValue + '=nil');
+  luaL_dostring(L, DebugValue + '=nil');
   lua_sethook(L, Hook, Mask, Count);
   lua_sethook(L, Hook, Mask, Count);
 end;
 end;
 
 
@@ -1244,7 +1258,7 @@ begin
   if (luaL_loadbuffer(L, PChar(Code), Length(Code), PChar(Name)) = 0) then
   if (luaL_loadbuffer(L, PChar(Code), Length(Code), PChar(Name)) = 0) then
     Exit;
     Exit;
 
 
-  LuaProcessErrorMessage(LuaStackToStr(L, -1, -1), Title, Line, Msg);
+  LuaProcessErrorMessage(LuaStackToStr(L, -1, -1, 99, True), Title, Line, Msg);
   raise ELuaException.Create(Title, Line, Msg);
   raise ELuaException.Create(Title, Line, Msg);
 end;
 end;
 
 
@@ -1269,8 +1283,7 @@ begin
   if (lua_pcall(L, NArgs, NResults, ErrFunc) = 0) then
   if (lua_pcall(L, NArgs, NResults, ErrFunc) = 0) then
     Exit;
     Exit;
 
 
-  LuaProcessErrorMessage(Dequote(LuaStackToStr(L, -1, -1)),
-    Title, Line, Msg);
+  LuaProcessErrorMessage(Dequote(LuaStackToStr(L, -1, -1, 99, True)), Title, Line, Msg);
   raise ELuaException.Create(Title, Line, Msg);
   raise ELuaException.Create(Title, Line, Msg);
 end;
 end;
 
 

+ 40 - 41
LuaEdit/LuaCore/lauxlib.pas

@@ -1,7 +1,13 @@
 (*
 (*
-** $Id: lauxlib.pas,v 1.1 2005-04-24 19:31:20 jfgoulet Exp $
+** $Id: lauxlib.pas,v 1.2 2006-10-27 03:32:17 jfgoulet Exp $
 ** Auxiliary functions for building Lua libraries
 ** Auxiliary functions for building Lua libraries
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
+**
+**
+** 10/02/2006 Jean-Francois Goulet - Removed deprecated function lua_loadlib()
+**                                   for compatibility with Lua 5.1.
+** 10/02/2006 Jean-Francois Goulet - Removed deprecated function lua_dostring()
+**                                   for compatibility with Lua 5.1.
 *)
 *)
 unit lauxlib;
 unit lauxlib;
 
 
@@ -22,64 +28,59 @@ type
   end;
   end;
   PluaL_reg = ^luaL_reg;
   PluaL_reg = ^luaL_reg;
 
 
-procedure luaL_openlib(L: Plua_State; libname: PChar;
-                               R: PluaL_reg; Nup: Integer);
-  cdecl external 'lua.dll';
 function luaL_getmetafield(L: Plua_State; Obj: Integer; const E: PChar): Integer;
 function luaL_getmetafield(L: Plua_State; Obj: Integer; const E: PChar): Integer;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 function luaL_callmeta(L: Plua_State; Obj: Integer; const E: PChar): Integer;
 function luaL_callmeta(L: Plua_State; Obj: Integer; const E: PChar): Integer;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 function luaL_typerror(L: Plua_State; NArg: Integer; const TName: PChar): Integer;
 function luaL_typerror(L: Plua_State; NArg: Integer; const TName: PChar): Integer;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 function luaL_argerror(L: Plua_State; NumArg: Integer; const ExtraMsg: PChar): Integer;
 function luaL_argerror(L: Plua_State; NumArg: Integer; const ExtraMsg: PChar): Integer;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 function luaL_checklstring(L: Plua_State; NumArg: Integer; S: Psize_t): PChar;
 function luaL_checklstring(L: Plua_State; NumArg: Integer; S: Psize_t): PChar;
-  cdecl external 'lua.dll';
-function luaL_optlstring(L: Plua_State; NumArg: Integer;
-                                           const Def: PChar; S: Psize_t): PChar;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
+function luaL_optlstring(L: Plua_State; NumArg: Integer; const Def: PChar; S: Psize_t): PChar;
+  cdecl external 'lua5.1.dll';
 function luaL_checknumber(L: Plua_State; NumArg: Integer): lua_Number;
 function luaL_checknumber(L: Plua_State; NumArg: Integer): lua_Number;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 function luaL_optnumber(L: Plua_State; NArg: Integer; Def: Lua_Number): lua_Number;
 function luaL_optnumber(L: Plua_State; NArg: Integer; Def: Lua_Number): lua_Number;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 
 
 procedure luaL_checkstack(L: Plua_State; SZ: Integer; const Msg: PChar);
 procedure luaL_checkstack(L: Plua_State; SZ: Integer; const Msg: PChar);
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 procedure luaL_checktype(L: Plua_State; NArg: Integer; T: Integer);
 procedure luaL_checktype(L: Plua_State; NArg: Integer; T: Integer);
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 procedure luaL_checkany(L: Plua_State; NArg: Integer);
 procedure luaL_checkany(L: Plua_State; NArg: Integer);
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 
 
 function   luaL_newmetatable(L: Plua_State; const TName: PChar): Integer;
 function   luaL_newmetatable(L: Plua_State; const TName: PChar): Integer;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 procedure  luaL_getmetatable(L: Plua_State; const TName: PChar);
 procedure  luaL_getmetatable(L: Plua_State; const TName: PChar);
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 function luaL_checkudata(L: Plua_State; UD: Integer; const TName: PChar): Pointer;
 function luaL_checkudata(L: Plua_State; UD: Integer; const TName: PChar): Pointer;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 
 
 procedure luaL_where(L: Plua_State; LVL: Integer);
 procedure luaL_where(L: Plua_State; LVL: Integer);
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 function luaL_error(L: Plua_State; const Fmt: PChar): Integer; varargs;
 function luaL_error(L: Plua_State; const Fmt: PChar): Integer; varargs;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 
 
 function luaL_findstring(const ST: PChar; lst: array of PChar): Integer;
 function luaL_findstring(const ST: PChar; lst: array of PChar): Integer;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 
 
 function luaL_ref(L: Plua_State; T: Integer): Integer;
 function luaL_ref(L: Plua_State; T: Integer): Integer;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 procedure luaL_unref(L: Plua_State; T: Integer; Ref: Integer);
 procedure luaL_unref(L: Plua_State; T: Integer; Ref: Integer);
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 
 
 function luaL_getn(L: Plua_State; T: Integer): Integer;
 function luaL_getn(L: Plua_State; T: Integer): Integer;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 procedure luaL_setn(L: Plua_State; T: Integer; N: Integer);
 procedure luaL_setn(L: Plua_State; T: Integer; N: Integer);
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 
 
 function luaL_loadfile(L: Plua_State; const FileName: PChar): Integer;
 function luaL_loadfile(L: Plua_State; const FileName: PChar): Integer;
-  cdecl external 'lua.dll';
-function luaL_loadbuffer(L: Plua_State; const Buff: PChar; SZ: size_t;
-                                const Name: PChar): Integer;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
+function luaL_loadbuffer(L: Plua_State; const Buff: PChar; SZ: size_t; const Name: PChar): Integer;
+  cdecl external 'lua5.1.dll';
 
 
 
 
 
 
@@ -128,17 +129,17 @@ procedure luaL_putchar(B: PluaL_Buffer; C: Char);
 function luaL_addsize(B: PLuaL_Buffer; N: Integer): PChar;
 function luaL_addsize(B: PLuaL_Buffer; N: Integer): PChar;
 
 
 procedure luaL_buffinit(L: Plua_State; B: PluaL_Buffer);
 procedure luaL_buffinit(L: Plua_State; B: PluaL_Buffer);
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 function luaL_prepbuffer(B: PluaL_Buffer): PChar;
 function luaL_prepbuffer(B: PluaL_Buffer): PChar;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 procedure luaL_addlstring(B: PluaL_Buffer; const S: PChar; L: size_t);
 procedure luaL_addlstring(B: PluaL_Buffer; const S: PChar; L: size_t);
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 procedure luaL_addstring(B: PluaL_Buffer; const S: PChar);
 procedure luaL_addstring(B: PluaL_Buffer; const S: PChar);
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 procedure luaL_addvalue(B: PluaL_Buffer);
 procedure luaL_addvalue(B: PluaL_Buffer);
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 procedure luaL_pushresult(B: PluaL_Buffer);
 procedure luaL_pushresult(B: PluaL_Buffer);
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 
 
 
 
 (* }====================================================== *)
 (* }====================================================== *)
@@ -150,12 +151,10 @@ procedure luaL_pushresult(B: PluaL_Buffer);
 *)
 *)
 
 
 function lua_dofile(L: Plua_State; const FileName: PChar): Integer;
 function lua_dofile(L: Plua_State; const FileName: PChar): Integer;
-  cdecl external 'lua.dll';
-function lua_dostring(L: Plua_State; const Str: PChar): Integer;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 function lua_dobuffer(L: Plua_State; const Buff: PChar; SZ: size_t;
 function lua_dobuffer(L: Plua_State; const Buff: PChar; SZ: size_t;
                                const N: PChar): Integer;
                                const N: PChar): Integer;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 
 
 function luaL_check_lstr(L: Plua_State; NumArg: Integer; S: Psize_t): PChar;
 function luaL_check_lstr(L: Plua_State; NumArg: Integer; S: Psize_t): PChar;
 function luaL_opt_lstr(L: Plua_State; NumArg: Integer;
 function luaL_opt_lstr(L: Plua_State; NumArg: Integer;

+ 855 - 365
LuaEdit/LuaCore/lua.pas

@@ -1,350 +1,453 @@
-(*
-** $Id: lua.pas,v 1.1 2005-04-24 19:31:20 jfgoulet Exp $
-** Lua - An Extensible Extension Language
-** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
-** http://www.lua.org	mailto:[email protected]
-** See Copyright Notice at the end of this file
-*)
-unit lua;
+unit Lua;
 
 
-{$IFNDEF lua_h}
-{$DEFINE lua_h}
-{$ENDIF}
+(*
+ * A complete Pascal wrapper for Lua 5.1 DLL module.
+ *
+ * Created by Geo Massar, 2006
+ * Distributed as free/open source.
+ *)
 
 
 interface
 interface
 
 
+type
+  size_t   = type Cardinal;
+  Psize_t  = ^size_t;
+  PPointer = ^Pointer;
+
+  lua_State = record end;
+  Plua_State = ^lua_State;
+
 const
 const
- CONST_LUA_VERSION = 'Lua 5.0';
- CONST_LUA_COPYRIGHT = 'Copyright (C) 1994-2003 Tecgraf, PUC-Rio';
- CONST_LUA_AUTHORS = 'R. Ierusalimschy, L. H. de Figueiredo & W. Celes';
+  LuaDLL = 'lua5.1.dll';
 
 
-(* option for multiple returns in `lua_pcall' and `lua_call' *)
- LUA_MULTRET = (-1);
 
 
+(*****************************************************************************)
+(*                               luaconfig.h                                 *)
+(*****************************************************************************)
 
 
 (*
 (*
-** pseudo-indices
+** $Id: lua.pas,v 1.2 2006-10-27 03:32:17 jfgoulet Exp $
+** Configuration file for Lua
+** See Copyright Notice in lua.h
 *)
 *)
-  LUA_REGISTRYINDEX = (-10000);
-  LUA_GLOBALSINDEX = (-10001);
 
 
-  function lua_upvalueindex(I: Integer): Integer;
+(*
+** {==================================================================
+@@ LUA_NUMBER is the type of numbers in Lua.
+** CHANGE the following definitions only if you want to build Lua
+** with a number type different from double. You may also need to
+** change lua_number2int & lua_number2integer.
+** ===================================================================
+*)
+type
+  LUA_NUMBER_  = type Double;            // ending underscore is needed in Pascal
+  LUA_INTEGER_ = type Integer;
 
 
+(*
+@@ LUA_IDSIZE gives the maximum size for the description of the source
+@* of a function in debug information.
+** CHANGE it if you want a different size.
+*)
 const
 const
+  LUA_IDSIZE = 60;
 
 
-(* error codes for `lua_load' and `lua_pcall' *)
-  LUA_ERRRUN = 1;
-  LUA_ERRFILE = 2;
-  LUA_ERRSYNTAX = 3;
-  LUA_ERRMEM = 4;
-  LUA_ERRERR = 5;
+(*
+@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system.
+*)
+const
+  LUAL_BUFFERSIZE = 1024;
 
 
+(*
+@@ LUA_PROMPT is the default prompt used by stand-alone Lua.
+@@ LUA_PROMPT2 is the default continuation prompt used by stand-alone Lua.
+** CHANGE them if you want different prompts. (You can also change the
+** prompts dynamically, assigning to globals _PROMPT/_PROMPT2.)
+*)
+const
+  LUA_PROMPT  = '> ';
+  LUA_PROMPT2 = '>> ';
 
 
-type
-  lua_State = record
-  end;
-  Plua_State = ^lua_State;
+(*
+@@  LUA_NUMBER_SCAN is the default scan number format in Lua
+** Formats for Lua numbers
+** Added by Jean-Francois Goulet - 10/02/2006
+*)
+{$IFNDEF LUA_NUMBER_SCAN}
+const
+  LUA_NUMBER_SCAN = '%lf';
+{$ENDIF}
 
 
-  lua_CFunction = function (L: Plua_State): Integer; cdecl;
-  size_t = Integer;
-  Psize_t = ^size_t;
+(*
+@@  LUA_NUMBER_FMT is the default number format in Lua
+** Formats for Lua numbers
+** Added by Jean-Francois Goulet - 10/02/2006
+*)
+{$IFNDEF LUA_NUMBER_FMT}
+const
+  LUA_NUMBER_FMT = '%.14g';
+{$ENDIF}
 
 
 (*
 (*
-** functions that read/write blocks when loading/dumping Lua chunks
+@@ lua_readline defines how to show a prompt and then read a line from
+@* the standard input.
+@@ lua_saveline defines how to "save" a read line in a "history".
+@@ lua_freeline defines how to free a line read by lua_readline.
+** CHANGE them if you want to improve this functionality (e.g., by using
+** GNU readline and history facilities).
 *)
 *)
-  lua_Chunkreader = function (L: Plua_State; UD: Pointer; var SZ: size_t): PChar;
-  lua_Chunkwriter = function (L: Plua_State; const P: Pointer; SZ: size_t; UD: Pointer): Integer;
+function  lua_readline(L : Plua_State; var b : PChar; p : PChar): Boolean;
+procedure lua_saveline(L : Plua_State; idx : Integer);
+procedure lua_freeline(L : Plua_State; b : PChar);
 
 
+(*
+@@ lua_stdin_is_tty detects whether the standard input is a 'tty' (that
+@* is, whether we're running lua interactively).
+** CHANGE it if you have a better definition for non-POSIX/non-Windows
+** systems.
+*/
+#include <io.h>
+#include <stdio.h>
+#define lua_stdin_is_tty()	_isatty(_fileno(stdin))
+*)
 const
 const
+  lua_stdin_is_tty = TRUE;
+
+(*****************************************************************************)
+(*                                  lua.h                                    *)
+(*****************************************************************************)
+
 (*
 (*
-** basic types
+** $Id: lua.pas,v 1.2 2006-10-27 03:32:17 jfgoulet Exp $
+** Lua - An Extensible Extension Language
+** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
+** See Copyright Notice at the end of this file
 *)
 *)
-  LUA_TNONE = (-1);
 
 
-  LUA_TNIL = 0;
-  LUA_TBOOLEAN = 1;
-  LUA_TLIGHTUSERDATA = 2;
-  LUA_TNUMBER = 3;
-  LUA_TSTRING = 4;
-  LUA_TTABLE = 5;
-  LUA_TFUNCTION = 6;
-  LUA_TUSERDATA = 7;
-  LUA_TTHREAD = 8;
+const
+  LUA_VERSION     = 'Lua 5.1';
+  LUA_VERSION_NUM = 501;
+  LUA_COPYRIGHT   = 'Copyright (C) 1994-2006 Tecgraf, PUC-Rio';
+  LUA_AUTHORS     = 'R. Ierusalimschy, L. H. de Figueiredo & W. Celes';
 
 
+  (* mark for precompiled code (`<esc>Lua') *)
+  LUA_SIGNATURE = #27'Lua';
 
 
-(* minimum Lua stack available to a C function *)
-  LUA_MINSTACK = 20;
+  (* option for multiple returns in `lua_pcall' and `lua_call' *)
+  LUA_MULTRET = -1;
 
 
+  (*
+  ** pseudo-indices
+  *)
+  LUA_REGISTRYINDEX = -10000;
+  LUA_ENVIRONINDEX  = -10001;
+  LUA_GLOBALSINDEX  = -10002;
 
 
-(*
-** generic extra include file
-*)
-{$IFDEF LUA_USER_H}
-{$INCLUDE} LUA_USER_H
-{$ENDIF}
+function lua_upvalueindex(idx : Integer) : Integer;   // a marco
 
 
+const
+  (* thread status; 0 is OK *)
+  LUA_YIELD_    = 1;     // Note: the ending underscore is needed in Pascal
+  LUA_ERRRUN    = 2;
+  LUA_ERRSYNTAX = 3;
+  LUA_ERRMEM    = 4;
+  LUA_ERRERR    = 5;
 
 
-(* type of numbers in Lua *)
 type
 type
-{$IFNDEF LUA_NUMBER}
-  lua_Number = Double;
-{$ELSE}
-  lua_Number = LUA_NUMBER;
-{$ENDIF}
+  lua_CFunction = function(L : Plua_State) : Integer; cdecl;
+
+  (*
+  ** functions that read/write blocks when loading/dumping Lua chunks
+  *)
+  lua_Reader = function (L : Plua_State; ud : Pointer;
+                         sz : Psize_t) : PChar; cdecl;
+  lua_Writer = function (L : Plua_State; const p : Pointer; sz : size_t;
+                         ud : Pointer) : Integer; cdecl;
+
+  (*
+  ** prototype for memory-allocation functions
+  *)
+  lua_Alloc = function (ud, ptr : Pointer;
+                        osize, nsize : size_t) : Pointer; cdecl;
 
 
+const
+  (*
+  ** basic types
+  *)
+  LUA_TNONE          = -1;
 
 
-(* mark for all API functions *)
+  LUA_TNIL           = 0;
+  LUA_TBOOLEAN       = 1;
+  LUA_TLIGHTUSERDATA = 2;
+  LUA_TNUMBER        = 3;
+  LUA_TSTRING        = 4;
+  LUA_TTABLE         = 5;
+  LUA_TFUNCTION      = 6;
+  LUA_TUSERDATA	     = 7;
+  LUA_TTHREAD        = 8;
+
+  (* minimum Lua stack available to a C function *)
+  LUA_MINSTACK = 20;
 
 
+type
+  (* type of numbers in Lua *)
+  lua_Number = LUA_NUMBER_;
+
+  (* type for integer functions *)
+  lua_Integer = LUA_INTEGER_;
 
 
 (*
 (*
 ** state manipulation
 ** state manipulation
 *)
 *)
-function lua_open: Plua_State;
-  cdecl external 'lua.dll';
+function  lua_newstate(f : lua_Alloc; ud : Pointer) : Plua_State;
+  cdecl; external LuaDLL;
 procedure lua_close(L: Plua_State);
 procedure lua_close(L: Plua_State);
-  cdecl external 'lua.dll';
-function lua_newthread(L: Plua_State): Plua_State;
-  cdecl external 'lua.dll';
+  cdecl; external LuaDLL;
+function  lua_newthread(L : Plua_State) : Plua_State;
+  cdecl; external LuaDLL;
 
 
-function lua_atpanic(L: Plua_State; Panicf: lua_CFunction): lua_CFunction;
-  cdecl external 'lua.dll';
+function  lua_atpanic(L : Plua_State; panicf : lua_CFunction) : lua_CFunction;
+  cdecl; external LuaDLL;
 
 
 
 
 (*
 (*
 ** basic stack manipulation
 ** basic stack manipulation
 *)
 *)
-function lua_gettop(L: Plua_State): Integer;
-  cdecl external 'lua.dll';
-procedure lua_settop(L: Plua_State; Idx: Integer);
-  cdecl external 'lua.dll';
-procedure lua_pushvalue(L: Plua_State; Idx: Integer);
-  cdecl external 'lua.dll';
-procedure lua_remove(L: Plua_State; Idx: Integer);
-  cdecl external 'lua.dll';
-procedure lua_insert(L: Plua_State; Idx: Integer);
-  cdecl external 'lua.dll';
-procedure lua_replace(L: Plua_State; Idx: Integer);
-  cdecl external 'lua.dll';
-function lua_checkstack(L: Plua_State; SZ: Integer): Integer;
-  cdecl external 'lua.dll';
-
-procedure lua_xmove (Src, Dst: Plua_State; N: Integer);
-  cdecl external 'lua.dll';
+function  lua_gettop(L : Plua_State) : Integer;
+  cdecl; external LuaDLL;
+procedure lua_settop(L : Plua_State; idx : Integer);
+  cdecl; external LuaDLL;
+procedure lua_pushvalue(L : Plua_State; idx : Integer);
+  cdecl; external LuaDLL;
+procedure lua_remove(L : Plua_State; idx : Integer);
+  cdecl; external LuaDLL;
+procedure lua_insert(L : Plua_State; idx : Integer);
+  cdecl; external LuaDLL;
+procedure lua_replace(L : Plua_State; idx : Integer);
+  cdecl; external LuaDLL;
+function  lua_checkstack(L : Plua_State; sz : Integer) : LongBool;
+  cdecl; external LuaDLL;
+
+procedure lua_xmove(src, dest : Plua_State; n : Integer);
+  cdecl; external LuaDLL;
 
 
 
 
 (*
 (*
 ** access functions (stack -> C)
 ** access functions (stack -> C)
 *)
 *)
-
-function lua_isnumber(L: Plua_State; Idx: Integer): Integer;
-  cdecl external 'lua.dll';
-function lua_isstring(L: Plua_State; Idx: Integer): Integer;
-  cdecl external 'lua.dll';
-function lua_iscfunction(L: Plua_State; Idx: Integer): Integer;
-  cdecl external 'lua.dll';
-function lua_isuserdata(L: Plua_State; Idx: Integer): Integer;
-  cdecl external 'lua.dll';
-function lua_type(L: Plua_State; Idx: Integer): Integer;
-  cdecl external 'lua.dll';
-function lua_typename(L: Plua_State; TP: Integer): PChar;
-  cdecl external 'lua.dll';
-
-function lua_equal(L: Plua_State; Idx1: Integer; Idx2: Integer): Integer;
-  cdecl external 'lua.dll';
-function lua_rawequal(L: Plua_State; Idx1: Integer; Idx2: Integer): Integer;
-  cdecl external 'lua.dll';
-function lua_lessthan(L: Plua_State; Idx1: Integer; Idx2: Integer): Integer;
-  cdecl external 'lua.dll';
-
-function lua_tonumber(L: Plua_State; Idx: Integer): lua_Number;
-  cdecl external 'lua.dll';
-function lua_toboolean(L: Plua_State; Idx: Integer): Integer;
-  cdecl external 'lua.dll';
-function lua_tostring(L: Plua_State; Idx: Integer): PChar;
-  cdecl external 'lua.dll';
-function lua_strlen(L: Plua_State; Idx: Integer): size_t;
-  cdecl external 'lua.dll';
-function lua_tocfunction(L: Plua_State; Idx: Integer): lua_CFunction;
-  cdecl external 'lua.dll';
-function lua_touserdata(L: Plua_State; Idx: Integer): Pointer;
-  cdecl external 'lua.dll';
-function lua_tothread(L: Plua_State; Idx: Integer): Plua_State;
-  cdecl external 'lua.dll';
-function lua_topointer(L: Plua_State; Idx: Integer): Pointer;
-  cdecl external 'lua.dll';
+function lua_isnumber(L : Plua_State; idx : Integer) : LongBool;
+  cdecl; external LuaDLL;
+function lua_isstring(L : Plua_State; idx : Integer) : LongBool;
+  cdecl; external LuaDLL;
+function lua_iscfunction(L : Plua_State; idx : Integer) : LongBool;
+  cdecl; external LuaDLL;
+function lua_isuserdata(L : Plua_State; idx : Integer) : LongBool;
+  cdecl; external LuaDLL;
+function lua_type(L : Plua_State; idx : Integer) : Integer;
+  cdecl; external LuaDLL;
+function lua_typename(L : Plua_State; tp : Integer) : PChar;
+  cdecl; external LuaDLL;
+
+function lua_equal(L : Plua_State; idx1, idx2 : Integer) : LongBool;
+  cdecl; external LuaDLL;
+function lua_rawequal(L : Plua_State; idx1, idx2 : Integer) : LongBool;
+  cdecl; external LuaDLL;
+function lua_lessthan(L : Plua_State; idx1, idx2 : Integer) : LongBool;
+  cdecl; external LuaDLL;
+
+function lua_tonumber(L : Plua_State; idx : Integer) : lua_Number;
+  cdecl; external LuaDLL;
+function lua_tointeger(L : Plua_State; idx : Integer) : lua_Integer;
+  cdecl; external LuaDLL;
+function lua_toboolean(L : Plua_State; idx : Integer) : LongBool;
+  cdecl; external LuaDLL;
+function lua_tolstring(L : Plua_State; idx : Integer;
+                       len : Psize_t) : PChar;
+  cdecl; external LuaDLL;
+function lua_objlen(L : Plua_State; idx : Integer) : size_t;
+  cdecl; external LuaDLL;
+function lua_tocfunction(L : Plua_State; idx : Integer) : lua_CFunction;
+  cdecl; external LuaDLL;
+function lua_touserdata(L : Plua_State; idx : Integer) : Pointer;
+  cdecl; external LuaDLL;
+function lua_tothread(L : Plua_State; idx : Integer) : Plua_State;
+  cdecl; external LuaDLL;
+function lua_topointer(L : Plua_State; idx : Integer) : Pointer;
+  cdecl; external LuaDLL;
 
 
 
 
 (*
 (*
 ** push functions (C -> stack)
 ** push functions (C -> stack)
 *)
 *)
-procedure lua_pushnil(L: Plua_State);
-  cdecl external 'lua.dll';
-procedure lua_pushnumber(L: Plua_State; N: lua_Number);
-  cdecl external 'lua.dll';
-procedure lua_pushlstring(L: Plua_State; const S: PChar; N: size_t);
-  cdecl external 'lua.dll';
-procedure lua_pushstring(L: Plua_State; const S: PChar);
-  cdecl external 'lua.dll';
-function lua_pushvfstring(L: Plua_State; const Fmt: PChar; Argp: Pointer): PChar;
-  cdecl external 'lua.dll';
-function lua_pushfstring(L: Plua_State; const Fmt: PChar): PChar; varargs;
-  cdecl external 'lua.dll';
-procedure lua_pushcclosure(L: Plua_State; Fn: lua_CFunction; N: Integer);
-  cdecl external 'lua.dll';
-procedure lua_pushboolean(L: Plua_State; B: Integer);
-  cdecl external 'lua.dll';
-procedure lua_pushlightuserdata(L: Plua_State; P: Pointer);
-  cdecl external 'lua.dll';
+procedure lua_pushnil(L : Plua_State);
+  cdecl; external LuaDLL;
+procedure lua_pushnumber(L : Plua_State; n : lua_Number);
+  cdecl; external LuaDLL;
+procedure lua_pushinteger(L : Plua_State; n : lua_Integer);
+  cdecl; external LuaDLL;
+procedure lua_pushlstring(L : Plua_State; const s : PChar; ls : size_t);
+  cdecl; external LuaDLL;
+procedure lua_pushstring(L : Plua_State; const s : PChar);
+  cdecl; external LuaDLL;
+function  lua_pushvfstring(L : Plua_State;
+                           const fmt : PChar; argp : Pointer) : PChar;
+  cdecl; external LuaDLL;
+function  lua_pushfstring(L : Plua_State; const fmt : PChar) : PChar; varargs;
+  cdecl; external LuaDLL;
+procedure lua_pushcclosure(L : Plua_State; fn : lua_CFunction; n : Integer);
+  cdecl; external LuaDLL;
+procedure lua_pushboolean(L : Plua_State; b : LongBool);
+  cdecl; external LuaDLL;
+procedure lua_pushlightuserdata(L : Plua_State; p : Pointer);
+  cdecl; external LuaDLL;
+function  lua_pushthread(L : Plua_state) : Cardinal;
+  cdecl; external LuaDLL;
 
 
 
 
 (*
 (*
 ** get functions (Lua -> stack)
 ** get functions (Lua -> stack)
 *)
 *)
-procedure lua_gettable(L: Plua_State; Idx: Integer);
-  cdecl external 'lua.dll';
-procedure lua_rawget(L: Plua_State; Idx: Integer);
-  cdecl external 'lua.dll';
-procedure lua_rawgeti(L: Plua_State; Idx: Integer; N: Integer);
-  cdecl external 'lua.dll';
-procedure lua_newtable(L: Plua_State);
-  cdecl external 'lua.dll';
-function lua_newuserdata(L: Plua_State; SZ: size_t): Pointer;
-  cdecl external 'lua.dll';
-function lua_getmetatable(L: Plua_State; ObjIndex: Integer): Integer;
-  cdecl external 'lua.dll';
-procedure lua_getfenv(L: Plua_State; Idx: Integer);
-  cdecl external 'lua.dll';
+procedure lua_gettable(L : Plua_State ; idx : Integer);
+  cdecl; external LuaDLL;
+procedure lua_getfield(L : Plua_State; idx : Integer; k : PChar);
+  cdecl; external LuaDLL;
+procedure lua_rawget(L : Plua_State; idx : Integer);
+  cdecl; external LuaDLL;
+procedure lua_rawgeti(L : Plua_State; idx, n : Integer);
+  cdecl; external LuaDLL;
+procedure lua_createtable(L : Plua_State; narr, nrec : Integer);
+  cdecl; external LuaDLL;
+function  lua_newuserdata(L : Plua_State; sz : size_t) : Pointer;
+  cdecl; external LuaDLL;
+function  lua_getmetatable(L : Plua_State; objindex : Integer) : LongBool;
+  cdecl; external LuaDLL;
+procedure lua_getfenv(L : Plua_State; idx : Integer);
+  cdecl; external LuaDLL;
 
 
 
 
 (*
 (*
 ** set functions (stack -> Lua)
 ** set functions (stack -> Lua)
 *)
 *)
-procedure lua_settable(L: Plua_State; Idx: Integer);
-  cdecl external 'lua.dll';
-procedure lua_rawset(L: Plua_State; Idx: Integer);
-  cdecl external 'lua.dll';
-procedure lua_rawseti(L: Plua_State; Idx: Integer; N: Integer);
-  cdecl external 'lua.dll';
-function lua_setmetatable(L: Plua_State; ObjIndex: Integer): Integer;
-  cdecl external 'lua.dll';
-function lua_setfenv(L: Plua_State; Idx: Integer): Integer;
-  cdecl external 'lua.dll';
-
+procedure lua_settable(L : Plua_State; idx : Integer);
+  cdecl; external LuaDLL;
+procedure lua_setfield(L : Plua_State; idx : Integer; const k : PChar);
+  cdecl; external LuaDLL;
+procedure lua_rawset(L : Plua_State; idx : Integer);
+  cdecl; external LuaDLL;
+procedure lua_rawseti(L : Plua_State; idx , n: Integer);
+  cdecl; external LuaDLL;
+function lua_setmetatable(L : Plua_State; objindex : Integer): LongBool;
+  cdecl; external LuaDLL;
+function lua_setfenv(L : Plua_State; idx : Integer): LongBool;
+  cdecl; external LuaDLL;
 
 
 (*
 (*
 ** `load' and `call' functions (load and run Lua code)
 ** `load' and `call' functions (load and run Lua code)
 *)
 *)
-procedure lua_call(L: Plua_State; NArgs: Integer; NResults: Integer);
-  cdecl external 'lua.dll';
-function lua_pcall(L: Plua_State; NArgs: Integer; NResults: Integer; ErrFunc: Integer): Integer;
-  cdecl external 'lua.dll';
-function lua_cpcall(L: Plua_State; Func: lua_CFunction; UD: Pointer): Integer;
-  cdecl external 'lua.dll';
-function lua_load(L: Plua_State; Reader: lua_Chunkreader; DT: Pointer;
-                        const ChunkName: PChar): Integer;
-  cdecl external 'lua.dll';
-
-function lua_dump(L: Plua_State; Writer: lua_Chunkwriter; Data: Pointer): Integer;
-  cdecl external 'lua.dll';
+procedure lua_call(L : Plua_State; nargs, nresults : Integer);
+  cdecl; external LuaDLL;
+function  lua_pcall(L : Plua_State;
+                    nargs, nresults, errfunc : Integer) : Integer;
+  cdecl; external LuaDLL;
+function  lua_cpcall(L : Plua_State;
+                     func : lua_CFunction; ud : Pointer) : Integer;
+  cdecl; external LuaDLL;
+function  lua_load(L : Plua_State; reader : lua_Reader;
+                   dt : Pointer; const chunkname : PChar) : Integer;
+  cdecl; external LuaDLL;
+
+function lua_dump(L : Plua_State; writer : lua_Writer; data: Pointer) : Integer;
+  cdecl; external LuaDLL;
 
 
 
 
 (*
 (*
 ** coroutine functions
 ** coroutine functions
 *)
 *)
-function lua_yield(L: Plua_State; NResults: Integer): Integer;
-  cdecl external 'lua.dll';
-function lua_resume(L: Plua_State; NArg: Integer): Integer;
-  cdecl external 'lua.dll';
+function lua_yield(L : Plua_State; nresults : Integer) : Integer;
+  cdecl; external LuaDLL;
+function lua_resume(L : Plua_State; narg : Integer) : Integer;
+  cdecl; external LuaDLL;
+function lua_status(L : Plua_State) : Integer;
+  cdecl; external LuaDLL;
 
 
 (*
 (*
-** garbage-collection functions
+** garbage-collection functions and options
 *)
 *)
-function lua_getgcthreshold(L: Plua_State): Integer;
-  cdecl external 'lua.dll';
-function lua_getgccount(L: Plua_State): Integer;
-  cdecl external 'lua.dll';
-procedure lua_setgcthreshold(L: Plua_State; NewThreshold: Integer);
-  cdecl external 'lua.dll';
+const
+  LUA_GCSTOP       = 0;
+  LUA_GCRESTART    = 1;
+  LUA_GCCOLLECT    = 2;
+  LUA_GCCOUNT      = 3;
+  LUA_GCCOUNTB	   = 4;
+  LUA_GCSTEP       = 5;
+  LUA_GCSETPAUSE   = 6;
+  LUA_GCSETSTEPMUL = 7;
+
+function lua_gc(L : Plua_State; what, data : Integer) : Integer;
+  cdecl; external LuaDLL;
 
 
 (*
 (*
 ** miscellaneous functions
 ** miscellaneous functions
 *)
 *)
+function lua_error(L : Plua_State) : Integer;
+  cdecl; external LuaDLL;
 
 
-function lua_version: PChar;
-  cdecl external 'lua.dll';
-
-function lua_error(L: Plua_State): Integer;
-  cdecl external 'lua.dll';
-
-function lua_next(L: Plua_State; Idx: Integer): Integer;
-  cdecl external 'lua.dll';
-
-procedure lua_concat(L: Plua_State; N: Integer);
-  cdecl external 'lua.dll';
+function lua_next(L : Plua_State; idx : Integer) : Integer;
+  cdecl; external LuaDLL;
 
 
+procedure lua_concat(L : Plua_State; n : Integer);
+  cdecl; external LuaDLL;
 
 
+function  lua_getallocf(L : Plua_State; ud : PPointer) : lua_Alloc;
+  cdecl; external LuaDLL;
+procedure lua_setallocf(L : Plua_State; f : lua_Alloc; ud : Pointer);
+  cdecl; external LuaDLL;
 
 
 (*
 (*
 ** ===============================================================
 ** ===============================================================
 ** some useful macros
 ** some useful macros
 ** ===============================================================
 ** ===============================================================
 *)
 *)
+procedure lua_pop(L : Plua_State; n : Integer);
 
 
-function lua_boxpointer(L: Plua_State; U: Pointer): Pointer;
-function lua_unboxpointer(L: Plua_State; I: Integer): Pointer;
-procedure lua_pop(L: Plua_State; N: Integer);
-procedure lua_register(L: Plua_State; const N: PChar; F: lua_CFunction);
-procedure lua_pushcfunction(L: Plua_State; F: lua_CFunction);
-function lua_isfunction(L: Plua_State; N: Integer): Boolean;
-function lua_istable(L: Plua_State; N: Integer): Boolean;
-function lua_islightuserdata(L: Plua_State; N: Integer): Boolean;
-function lua_isnil(L: Plua_State; N: Integer): Boolean;
-function lua_isboolean(L: Plua_State; N: Integer): Boolean;
-function lua_isnone(L: Plua_State; N: Integer): Boolean;
-function lua_isnoneornil(L: Plua_State; N: Integer): Boolean;
-procedure lua_pushliteral(L: Plua_State; const S: PChar);
+procedure lua_newtable(L : Plua_State);
 
 
-(*
-** compatibility macros and functions
-*)
+procedure lua_register(L : Plua_State; n : PChar; f : lua_CFunction);
 
 
-function lua_pushupvalues(L: Plua_State): Integer;
-  cdecl; external 'lua.dll';
+procedure lua_pushcfunction(L : Plua_State; f : lua_CFunction);
 
 
-procedure lua_getregistry(L: Plua_State);
-procedure lua_setglobal(L: Plua_State; const S: PChar);
-procedure lua_getglobal(L: Plua_State; const S: PChar);
+function  lua_strlen(L : Plua_State; idx : Integer) : Integer;
 
 
-(* compatibility with ref system *)
+function lua_isfunction(L : Plua_State; n : Integer) : Boolean;
+function lua_istable(L : Plua_State; n : Integer) : Boolean;
+function lua_islightuserdata(L : Plua_State; n : Integer) : Boolean;
+function lua_isnil(L : Plua_State; n : Integer) : Boolean;
+function lua_isboolean(L : Plua_State; n : Integer) : Boolean;
+function lua_isthread(L : Plua_State; n : Integer) : Boolean;
+function lua_isnone(L : Plua_State; n : Integer) : Boolean;
+function lua_isnoneornil(L : Plua_State; n : Integer) : Boolean;
 
 
-(* pre-defined references *)
-const
-  LUA_NOREF = (-2);
-  LUA_REFNIL = (-1);
+procedure lua_pushliteral(L : Plua_State; s : PChar);
+
+procedure lua_setglobal(L : Plua_State; s : PChar);
+procedure lua_getglobal(L : Plua_State; s : PChar);
+
+function lua_tostring(L : Plua_State; idx : Integer) : PChar;
 
 
-procedure lua_ref(L: Plua_State; Lock: Integer);
-procedure lua_unref(L: Plua_State; Ref: Integer);
-procedure lua_getref(L: Plua_State; Ref: Integer);
 
 
 (*
 (*
-** {======================================================================
-** useful definitions for Lua kernel and libraries
-** =======================================================================
+** compatibility macros and functions
 *)
 *)
+function lua_open : Plua_State;
 
 
-(* formats for Lua numbers *)
-{$IFNDEF LUA_NUMBER_SCAN}
-const
-  LUA_NUMBER_SCAN = '%lf';
-{$ENDIF}
+procedure lua_getregistry(L : Plua_State);
 
 
-{$IFNDEF LUA_NUMBER_FMT}
-const
-  LUA_NUMBER_FMT = '%.14g';
-{$ENDIF}
+function lua_getgccount(L : Plua_State) : Integer;
 
 
-(* }====================================================================== *)
+type
+  lua_Chuckreader = type lua_Reader;
+  lua_Chuckwriter = type lua_Writer;
 
 
+(* ====================================================================== *)
 
 
 (*
 (*
 ** {======================================================================
 ** {======================================================================
@@ -352,220 +455,607 @@ const
 ** =======================================================================
 ** =======================================================================
 *)
 *)
 
 
-const
 (*
 (*
 ** Event codes
 ** Event codes
 *)
 *)
-  LUA_HOOKCALL = 0;
-  LUA_HOOKRET = 1;
-  LUA_HOOKLINE = 2;
-  LUA_HOOKCOUNT = 3;
+const
+  LUA_HOOKCALL    = 0;
+  LUA_HOOKRET     = 1;
+  LUA_HOOKLINE    = 2;
+  LUA_HOOKCOUNT   = 3;
   LUA_HOOKTAILRET = 4;
   LUA_HOOKTAILRET = 4;
 
 
 
 
 (*
 (*
 ** Event masks
 ** Event masks
 *)
 *)
-  LUA_MASKCALL = (1 shl LUA_HOOKCALL);
-  LUA_MASKRET = (1 shl LUA_HOOKRET);
-  LUA_MASKLINE = (1 shl LUA_HOOKLINE);
-  LUA_MASKCOUNT = (1 shl LUA_HOOKCOUNT);
-
-const
-  LUA_IDSIZE = 60;
+  LUA_MASKCALL  = 1 shl LUA_HOOKCALL;
+  LUA_MASKRET   = 1 shl LUA_HOOKRET;
+  LUA_MASKLINE  = 1 shl LUA_HOOKLINE;
+  LUA_MASKCOUNT = 1 shl LUA_HOOKCOUNT;
 
 
 type
 type
-  lua_Debug = record
-    event: Integer;
-    name: PChar; (* (n) *)
-    namewhat: PChar; (* (n) `global', `local', `field', `method' *)
-    what: PChar; (* (S) `Lua', `C', `main', `tail' *)
-    source: PChar; (* (S) *)
-    currentline: Integer;  (* (l) *)
-    nups: Integer;   (* (u) number of upvalues *)
-    linedefined: Integer;  (* (S) *)
-    short_src: array [0..LUA_IDSIZE - 1] of Char; (* (S) *)
+  lua_Debug = packed record
+    event : Integer;
+    name : PChar;          (* (n) *)
+    namewhat : PChar;      (* (n) `global', `local', `field', `method' *)
+    what : PChar;          (* (S) `Lua', `C', `main', `tail' *)
+    source : PChar;        (* (S) *)
+    currentline : Integer; (* (l) *)
+    nups : Integer;        (* (u) number of upvalues *)
+    linedefined : Integer; (* (S) *)
+    lastlinedefined : Integer; (* (S) *) // 10/03/2006 Jean-Francois Goulet - Added new field for Lua 5.1 compatibility
+    short_src : array [0..LUA_IDSIZE-1] of Char; (* (S) *)
     (* private part *)
     (* private part *)
-    i_ci: Integer;  (* active function *)
+    i_ci : Integer;        (* active function *)
   end;
   end;
   Plua_Debug = ^lua_Debug;
   Plua_Debug = ^lua_Debug;
 
 
-  lua_Hook = procedure (L: Plua_State; AR: Plua_Debug); cdecl;
+  (* Functions to be called by the debuger in specific events *)
+  lua_Hook = procedure (L : Plua_State; AR : Plua_Debug); cdecl;
+
+function lua_getstack(L : Plua_State; Level : Integer;
+                      AR : Plua_Debug) : Integer;
+  cdecl; external LuaDLL;
+function lua_getinfo(L : Plua_State; const what : PChar;
+                     AR : Plua_Debug): Integer;
+  cdecl; external LuaDLL;
+function lua_getlocal(L : Plua_State;
+                      const AR : Plua_Debug; n : Integer) : PChar;
+  cdecl; external LuaDLL;
+function lua_setlocal(L : Plua_State;
+                      const AR : Plua_Debug; n : Integer) : PChar;
+  cdecl; external LuaDLL;
+function lua_getupvalue(L : Plua_State; funcindex, n : Integer) : PChar;
+  cdecl; external LuaDLL;
+function lua_setupvalue(L : Plua_State; funcindex, n : Integer) : PChar;
+  cdecl; external LuaDLL;
+
+function lua_sethook(L : Plua_State; func : lua_Hook;
+                     mask, count: Integer): Integer;
+  cdecl; external LuaDLL;
+function lua_gethook(L : Plua_State) : lua_Hook;
+  cdecl; external LuaDLL;
+function lua_gethookmask(L : Plua_State) : Integer;
+  cdecl; external LuaDLL;
+function lua_gethookcount(L : Plua_State) : Integer;
+  cdecl; external LuaDLL;
+
+
+(*****************************************************************************)
+(*                                  lualib.h                                 *)
+(*****************************************************************************)
 
 
+(*
+** $Id: lua.pas,v 1.2 2006-10-27 03:32:17 jfgoulet Exp $
+** Lua standard libraries
+** See Copyright Notice at the end of this file
+*)
 
 
-function lua_getstack(L: Plua_State; Level: Integer; AR: Plua_Debug): Integer;
-  cdecl external 'lua.dll';
-function lua_getinfo(L: Plua_State; const What: PChar; AR: Plua_Debug): Integer;
-  cdecl external 'lua.dll';
-function lua_getlocal(L: Plua_State; const AR: Plua_Debug; N: Integer): PChar;
-  cdecl external 'lua.dll';
-function lua_setlocal(L: Plua_State; const AR: Plua_Debug; N: Integer): PChar;
-  cdecl external 'lua.dll';
-function lua_getupvalue(L: Plua_State; FuncIndex: Integer; N: Integer): PChar;
-  cdecl external 'lua.dll';
-function lua_setupvalue(L: Plua_State; FuncIndex: Integer; N: Integer): PChar;
-  cdecl external 'lua.dll';
+const
+  (* Key to file-handle type *)
+  LUA_FILEHANDLE  = 'FILE*';
 
 
-function lua_sethook(L: Plua_State; Func: lua_Hook; Mask: Integer; Count: Integer): Integer;
-  cdecl external 'lua.dll';
-function lua_gethook(L: Plua_State): lua_Hook;
-  cdecl external 'lua.dll';
-function lua_gethookmask(L: Plua_State): Integer;
-  cdecl external 'lua.dll';
-function lua_gethookcount(L: Plua_State): Integer;
-  cdecl external 'lua.dll';
+  LUA_COLIBNAME   = 'coroutine';
+  LUA_TABLIBNAME  = 'table';
+  LUA_IOLIBNAME   = 'io';
+  LUA_OSLIBNAME   = 'os';
+  LUA_STRLIBNAME  = 'string';
+  LUA_MATHLIBNAME = 'math';
+  LUA_DBLIBNAME   = 'debug';
+  LUA_LOADLIBNAME = 'package';
 
 
-(* }====================================================================== *)
+function luaopen_base(L : Plua_State) : Integer;
+  cdecl; external LuaDLL;
 
 
+function luaopen_table(L : Plua_State) : Integer;
+  cdecl; external LuaDLL;
 
 
-(******************************************************************************
-* Copyright (C) 1994-2003 Tecgraf, PUC-Rio.  All rights reserved.
-*
-* Permission is hereby granted, free of charge, to any person obtaining
-* a copy of this software and associated documentation files (the
-* "Software"), to deal in the Software without restriction, including
-* without limitation the rights to use, copy, modify, merge, publish,
-* distribute, sublicense, and/or sell copies of the Software, and to
-* permit persons to whom the Software is furnished to do so, subject to
-* the following conditions:
-*
-* The above copyright notice and this permission notice shall be
-* included in all copies or substantial portions of the Software.
-*
-* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
-* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-******************************************************************************)
+function luaopen_io(L : Plua_State) : Integer;
+  cdecl; external LuaDLL;
 
 
+function luaopen_os(L : Plua_State) : Integer;
+  cdecl; external LuaDLL;
 
 
-implementation
+function luaopen_string(L : Plua_State) : Integer;
+  cdecl; external LuaDLL;
 
 
-uses
-  SysUtils, lauxlib;
+function luaopen_math(L : Plua_State) : Integer;
+  cdecl; external LuaDLL;
+
+function luaopen_debug(L : Plua_State) : Integer;
+  cdecl; external LuaDLL;
+
+function luaopen_package(L : Plua_State) : Integer;
+  cdecl; external LuaDLL;
+
+procedure luaL_openlibs(L : Plua_State);
+  cdecl; external LuaDLL;
+
+procedure lua_assert(x : Boolean);    // a macro
+
+
+(*****************************************************************************)
+(*                                  lauxlib.h                                *)
+(*****************************************************************************)
+
+(*
+** $Id: lua.pas,v 1.2 2006-10-27 03:32:17 jfgoulet Exp $
+** Auxiliary functions for building Lua libraries
+** See Copyright Notice at the end of this file.
+*)
+
+// not compatibility with the behavior of setn/getn in Lua 5.0
+function  luaL_getn(L : Plua_State; idx : Integer) : Integer;
+procedure luaL_setn(L : Plua_State; i, j : Integer);
 
 
 const
 const
-  MAX_SIZE = High(Integer);
-  MAX_POINTER_ARRAY = MAX_SIZE div SizeOf(Pointer);
+  LUA_ERRFILE = LUA_ERRERR + 1;
+
+type
+  luaL_Reg = packed record
+    name : PChar;
+    func : lua_CFunction;
+  end;
+  PluaL_Reg = ^luaL_Reg;
+
+
+procedure luaL_openlib(L : Plua_State; const libname : PChar;
+                       const lr : PluaL_Reg; nup : Integer);
+  cdecl; external LuaDLL;
+procedure luaL_register(L : Plua_State; const libname : PChar;
+                       const lr : PluaL_Reg);
+  cdecl; external LuaDLL;
+function luaL_getmetafield(L : Plua_State; obj : Integer;
+                           const e : PChar) : Integer;
+  cdecl; external LuaDLL;
+function luaL_callmeta(L : Plua_State; obj : Integer;
+                       const e : PChar) : Integer;
+  cdecl; external LuaDLL;
+function luaL_typerror(L : Plua_State; narg : Integer;
+                       const tname : PChar) : Integer;
+  cdecl; external LuaDLL;
+function luaL_argerror(L : Plua_State; numarg : Integer;
+                       const extramsg : PChar) : Integer;
+  cdecl; external LuaDLL;
+function luaL_checklstring(L : Plua_State; numArg : Integer;
+                           ls : Psize_t) : PChar;
+  cdecl; external LuaDLL;
+function luaL_optlstring(L : Plua_State; numArg : Integer;
+                         const def: PChar; ls: Psize_t) : PChar;
+  cdecl; external LuaDLL;
+function luaL_checknumber(L : Plua_State; numArg : Integer) : lua_Number;
+  cdecl; external LuaDLL;
+function luaL_optnumber(L : Plua_State; nArg : Integer;
+                        def : lua_Number) : lua_Number;
+  cdecl; external LuaDLL;
+
+function luaL_checkinteger(L : Plua_State; numArg : Integer) : lua_Integer;
+  cdecl; external LuaDLL;
+function luaL_optinteger(L : Plua_State; nArg : Integer;
+                        def : lua_Integer) : lua_Integer;
+  cdecl; external LuaDLL;
+
+procedure luaL_checkstack(L : Plua_State; sz : Integer; const msg : PChar);
+  cdecl; external LuaDLL;
+procedure luaL_checktype(L : Plua_State; narg, t : Integer);
+  cdecl; external LuaDLL;
+procedure luaL_checkany(L : Plua_State; narg : Integer);
+  cdecl; external LuaDLL;
+
+function luaL_newmetatable(L : Plua_State; const tname : PChar) : Integer;
+  cdecl; external LuaDLL;
+function luaL_checkudata(L : Plua_State; ud : Integer;
+                         const tname : PChar) : Pointer;
+  cdecl; external LuaDLL;
+
+procedure luaL_where(L : Plua_State; lvl : Integer);
+  cdecl; external LuaDLL;
+function  luaL_error(L : Plua_State; const fmt : PChar) : Integer; varargs;
+  cdecl; external LuaDLL;
+
+function luaL_checkoption(L : Plua_State; narg : Integer; const def : PChar;
+                          const lst : array of PChar) : Integer;
+  cdecl; external LuaDLL;
+
+function  luaL_ref(L : Plua_State; t : Integer) : Integer;
+  cdecl; external LuaDLL;
+procedure luaL_unref(L : Plua_State; t, ref : Integer);
+  cdecl; external LuaDLL;
+
+function luaL_loadfile(L : Plua_State; const filename : PChar) : Integer;
+  cdecl; external LuaDLL;
+function luaL_loadbuffer(L : Plua_State; const buff : PChar;
+                         sz : size_t; const name: PChar) : Integer;
+  cdecl; external LuaDLL;
+
+function luaL_loadstring(L : Plua_State; const s : Pchar) : Integer;
+  cdecl; external LuaDLL;
+
+function luaL_newstate : Plua_State;
+  cdecl; external LuaDLL;
+
+function luaL_gsub(L : Plua_State; const s, p, r : PChar) : PChar;
+  cdecl; external LuaDLL;
+
+function luaL_findtable(L : Plua_State; idx : Integer;
+                        const fname : PChar; szhint : Integer) : PChar;
+  cdecl; external LuaDLL;
+
+
+(*
+** ===============================================================
+** some useful macros
+** ===============================================================
+*)
+
+function luaL_argcheck(L : Plua_State; cond : Boolean; numarg : Integer;
+                       extramsg : PChar): Integer;
+function luaL_checkstring(L : Plua_State; n : Integer) : PChar;
+function luaL_optstring(L : Plua_State; n : Integer; d : PChar) : PChar;
+function luaL_checkint(L : Plua_State; n : Integer) : Integer;
+function luaL_optint(L : Plua_State; n, d : Integer): Integer;
+function luaL_checklong(L : Plua_State; n : LongInt) : LongInt;
+function luaL_optlong(L : Plua_State; n : Integer; d : LongInt) : LongInt;
+
+function luaL_typename(L : Plua_State; idx : Integer) : PChar;
+
+function luaL_dofile(L : Plua_State; fn : PChar) : Integer;
+
+function luaL_dostring(L : Plua_State; s : PChar) : Integer;
+
+procedure luaL_getmetatable(L : Plua_State; n : PChar);
+
+(* not implemented yet
+#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
+*)
+
+(*
+** {======================================================
+** Generic Buffer manipulation
+** =======================================================
+*)
 
 
 type
 type
-  TPointerArray = array [0..MAX_POINTER_ARRAY - 1] of Pointer;
-  PPointerArray = ^TPointerArray;
+  luaL_Buffer = packed record
+    p : PChar;       (* current position in buffer *)
+    lvl : Integer;   (* number of strings in the stack (level) *)
+    L : Plua_State;
+    buffer : array [0..LUAL_BUFFERSIZE-1] of Char;
+  end;
+  PluaL_Buffer = ^luaL_Buffer;
+
+procedure luaL_addchar(B : PluaL_Buffer; c : Char);
+
+(* compatibility only *)
+procedure luaL_putchar(B : PluaL_Buffer; c : Char);
+
+procedure luaL_addsize(B : PluaL_Buffer; n : Integer);
+
+procedure luaL_buffinit(L : Plua_State; B : PluaL_Buffer);
+  cdecl; external LuaDLL;
+function  luaL_prepbuffer(B : PluaL_Buffer) : PChar;
+  cdecl; external LuaDLL;
+procedure luaL_addlstring(B : PluaL_Buffer; const s : PChar; ls : size_t);
+  cdecl; external LuaDLL;
+procedure luaL_addstring(B : PluaL_Buffer; const s : PChar);
+  cdecl; external LuaDLL;
+procedure luaL_addvalue(B : PluaL_Buffer);
+  cdecl; external LuaDLL;
+procedure luaL_pushresult(B : PluaL_Buffer);
+  cdecl; external LuaDLL;
+
+(* ====================================================== *)
+
+
+(* compatibility with ref system *)
+
+(* pre-defined references *)
+const
+  LUA_NOREF  = -2;
+  LUA_REFNIL = -1;
+
+function lua_ref(L : Plua_State; lock : Boolean) : Integer;
+
+procedure lua_unref(L : Plua_State; ref : Integer);
+
+procedure lua_getref(L : Plua_State; ref : Integer);
+
+
+(******************************************************************************)
+(******************************************************************************)
+(******************************************************************************)
+
+implementation
+
+uses
+  SysUtils;
+
+(*****************************************************************************)
+(*                            luaconfig.h                                    *)
+(*****************************************************************************)
+
+function  lua_readline(L : Plua_State; var b : PChar; p : PChar): Boolean;
+var
+  s : String;
+begin
+  Write(p);                        // show prompt
+  ReadLn(s);                       // get line
+  b := PChar(s);                   //   and return it
+  result := (b[0] <> #4);          // test for ctrl-D
+end;
+
+procedure lua_saveline(L : Plua_State; idx : Integer);
+begin
+end;
+
+procedure lua_freeline(L : Plua_State; b : PChar);
+begin
+end;
+
+
+(*****************************************************************************)
+(*                                  lua.h                                    *)
+(*****************************************************************************)
+
+function lua_upvalueindex(idx : Integer) : Integer;
+begin
+  result := LUA_GLOBALSINDEX - idx;
+end;
+
+procedure lua_pop(L : Plua_State; n : Integer);
+begin
+  lua_settop(L, -n - 1);
+end;
+
+procedure lua_newtable(L : Plua_State);
+begin
+  lua_createtable(L, 0, 0);
+end;
+
+procedure lua_register(L : Plua_State; n : PChar; f : lua_CFunction);
+begin
+  lua_pushcfunction(L, f);
+  lua_setglobal(L, n);
+end;
 
 
-function lua_upvalueindex(I: Integer): Integer;
+procedure lua_pushcfunction(L : Plua_State; f : lua_CFunction);
 begin
 begin
-  Result := LUA_GLOBALSINDEX - I;
+  lua_pushcclosure(L, f, 0);
 end;
 end;
 
 
-function lua_boxpointer(L: Plua_State; U: Pointer): Pointer;
+function  lua_strlen(L : Plua_State; idx : Integer) : Integer;
 begin
 begin
-  PPointerArray(lua_newuserdata(L, sizeof(U)))^[0] := U;
-  Result := U;
+  result := lua_objlen(L, idx);
 end;
 end;
 
 
-function lua_unboxpointer(L: Plua_State; I: Integer): Pointer;
+function lua_isfunction(L : Plua_State; n : Integer) : Boolean;
 begin
 begin
-  Result := PPointerArray(lua_touserdata(L, I))^[0];
+  result := lua_type(L, n) = LUA_TFUNCTION;
 end;
 end;
 
 
-procedure lua_pop(L: Plua_State; N: Integer);
+function lua_istable(L : Plua_State; n : Integer) : Boolean;
 begin
 begin
-  lua_settop(L, -(N)-1);
+  result := lua_type(L, n) = LUA_TTABLE;
 end;
 end;
 
 
-procedure lua_register(L: Plua_State; const N: PChar; F: lua_CFunction);
+function lua_islightuserdata(L : Plua_State; n : Integer) : Boolean;
 begin
 begin
-  lua_pushstring(L, N);
-  lua_pushcfunction(L, F);
-  lua_settable(L, LUA_GLOBALSINDEX);
+  result := lua_type(L, n) = LUA_TLIGHTUSERDATA;
 end;
 end;
 
 
-procedure lua_pushcfunction(L: Plua_State; F: lua_CFunction);
+function lua_isnil(L : Plua_State; n : Integer) : Boolean;
 begin
 begin
-  lua_pushcclosure(L, F, 0);
+  result := lua_type(L, n) = LUA_TNIL;
 end;
 end;
 
 
-function lua_isfunction(L: Plua_State; N: Integer): Boolean;
+function lua_isboolean(L : Plua_State; n : Integer) : Boolean;
 begin
 begin
-  Result := lua_type(L, n) = LUA_TFUNCTION;
+  result := lua_type(L, n) = LUA_TBOOLEAN;
 end;
 end;
 
 
-function lua_istable(L: Plua_State; N: Integer): Boolean;
+function lua_isthread(L : Plua_State; n : Integer) : Boolean;
 begin
 begin
-  Result := lua_type(L, n) = LUA_TTABLE;
+  result := lua_type(L, n) = LUA_TTHREAD;
 end;
 end;
 
 
-function lua_islightuserdata(L: Plua_State; N: Integer): Boolean;
+function lua_isnone(L : Plua_State; n : Integer) : Boolean;
 begin
 begin
-  Result := lua_type(L, n) = LUA_TLIGHTUSERDATA;
+  result := lua_type(L, n) = LUA_TNONE;
 end;
 end;
 
 
-function lua_isnil(L: Plua_State; N: Integer): Boolean;
+function lua_isnoneornil(L : Plua_State; n : Integer) : Boolean;
 begin
 begin
-  Result := lua_type(L, n) = LUA_TNIL;
+  result := lua_type(L, n) <= 0;
 end;
 end;
 
 
-function lua_isboolean(L: Plua_State; N: Integer): Boolean;
+procedure lua_pushliteral(L : Plua_State; s : PChar);
 begin
 begin
-  Result := lua_type(L, n) = LUA_TBOOLEAN;
+  lua_pushlstring(L, s, StrLen(s));
 end;
 end;
 
 
-function lua_isnone(L: Plua_State; N: Integer): Boolean;
+procedure lua_setglobal(L : Plua_State; s : PChar);
 begin
 begin
-  Result := lua_type(L, n) = LUA_TNONE;
+  lua_setfield(L, LUA_GLOBALSINDEX, s);
 end;
 end;
 
 
-function lua_isnoneornil(L: Plua_State; N: Integer): Boolean;
+procedure lua_getglobal(L: Plua_State; s: PChar);
 begin
 begin
-  Result := lua_type(L, n) <= 0;
+  lua_getfield(L, LUA_GLOBALSINDEX, s);
 end;
 end;
 
 
-procedure lua_pushliteral(L: Plua_State; const S: PChar);
+function lua_tostring(L : Plua_State; idx : Integer) : PChar;
 begin
 begin
-  lua_pushlstring(L, S, StrLen(S));
+  result := lua_tolstring(L, idx, nil);
 end;
 end;
 
 
-procedure lua_getregistry(L: Plua_State);
+function lua_open : Plua_State;
+begin
+  result := luaL_newstate;
+end;
+
+procedure lua_getregistry(L : Plua_State);
 begin
 begin
   lua_pushvalue(L, LUA_REGISTRYINDEX);
   lua_pushvalue(L, LUA_REGISTRYINDEX);
 end;
 end;
 
 
-procedure lua_setglobal(L: Plua_State; const S: PChar);
+function lua_getgccount(L : Plua_State) : Integer;
+begin
+  result := lua_gc(L, LUA_GCCOUNT, 0);
+end;
+
+
+(*****************************************************************************)
+(*                                  lualib.h                                 *)
+(*****************************************************************************)
+
+procedure lua_assert(x : Boolean);
+begin
+end;
+
+
+(*****************************************************************************)
+(*                                  lauxlib.h    n                           *)
+(*****************************************************************************)
+
+function luaL_getn(L : Plua_State; idx : Integer) : Integer;
+begin
+  result := lua_objlen(L, idx);
+end;
+
+procedure luaL_setn(L : plua_State; i, j : Integer);
+begin
+  (* no op *)
+end;
+
+function luaL_argcheck(L : Plua_State; cond : Boolean; numarg : Integer;
+                       extramsg : PChar): Integer;
+begin
+  if not cond then
+    result := luaL_argerror(L, numarg, extramsg)
+  else
+    result := 0;
+end;
+
+function luaL_checkstring(L : Plua_State; n : Integer) : PChar;
+begin
+  result := luaL_checklstring(L, n, nil);
+end;
+
+function luaL_optstring(L : Plua_State; n : Integer; d : PChar) : PChar;
+begin
+  result := luaL_optlstring(L, n, d, nil);
+end;
+
+function luaL_checkint(L : Plua_State; n : Integer) : Integer;
+begin
+  result := luaL_checkinteger(L, n);
+end;
+
+function luaL_optint(L : Plua_State; n, d : Integer): Integer;
+begin
+  result := luaL_optinteger(L, n, d);
+end;
+
+function luaL_checklong(L : Plua_State; n : LongInt) : LongInt;
+begin
+  result := luaL_checkinteger(L, n);
+end;
+
+function luaL_optlong(L : Plua_State; n : Integer; d : LongInt) : LongInt;
+begin
+  result := luaL_optinteger(L, n, d);
+end;
+
+function luaL_typename(L : Plua_State; idx : Integer) : PChar;
+begin
+  result := lua_typename( L, lua_type(L, idx) );
+end;
+
+function luaL_dofile(L : Plua_State; fn : PChar) : Integer;
+begin
+  result := luaL_loadfile(L, fn);
+  if result = 0 then
+    result := lua_pcall(L, 0, 0, 0);
+end;
+
+function luaL_dostring(L : Plua_State; s : PChar) : Integer;
 begin
 begin
-  lua_pushstring(L, S);
-  lua_insert(L, -2);
-  lua_settable(L, LUA_GLOBALSINDEX);
+  result := luaL_loadstring(L, s);
+  if result = 0 then
+    result := lua_pcall(L, 0, 0, 0);
 end;
 end;
 
 
-procedure lua_getglobal(L: Plua_State; const S: PChar);
+procedure luaL_getmetatable(L : Plua_State; n : PChar);
 begin
 begin
-  lua_pushstring(L, S);
-  lua_gettable(L, LUA_GLOBALSINDEX);
+  lua_getfield(L, LUA_REGISTRYINDEX, n);
 end;
 end;
 
 
-procedure lua_ref(L: Plua_State; Lock: Integer);
+procedure luaL_addchar(B : PluaL_Buffer; c : Char);
 begin
 begin
-  if (Lock <> 0) then
-  begin
-    luaL_ref(L, LUA_REGISTRYINDEX);
-  end else
-  begin
+  if not(B^.p < B^.buffer + LUAL_BUFFERSIZE) then
+    luaL_prepbuffer(B);
+  B^.p^ := c;
+  Inc(B^.p);
+end;
+
+procedure luaL_putchar(B : PluaL_Buffer; c : Char);
+begin
+  luaL_addchar(B, c);
+end;
+
+procedure luaL_addsize(B : PluaL_Buffer; n : Integer);
+begin
+  Inc(B^.p, n);
+end;
+
+function lua_ref(L : Plua_State; lock : Boolean) : Integer;
+begin
+  if lock then
+    result := luaL_ref(L, LUA_REGISTRYINDEX)
+  else begin
     lua_pushstring(L, 'unlocked references are obsolete');
     lua_pushstring(L, 'unlocked references are obsolete');
     lua_error(L);
     lua_error(L);
+    result := 0;
   end;
   end;
 end;
 end;
 
 
-procedure lua_unref(L: Plua_State; Ref: Integer);
+procedure lua_unref(L : Plua_State; ref : Integer);
 begin
 begin
-  luaL_unref(L, LUA_REGISTRYINDEX, Ref);
+  luaL_unref(L, LUA_REGISTRYINDEX, ref);
 end;
 end;
 
 
-procedure lua_getref(L: Plua_State; Ref: Integer);
+procedure lua_getref(L : Plua_State; ref : Integer);
 begin
 begin
-  lua_rawgeti(L, LUA_REGISTRYINDEX, Ref);
+  lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
 end;
 end;
 
 
+
+(******************************************************************************
+* Original copyright for the lua source and headers:
+*  1994-2004 Tecgraf, PUC-Rio.
+*  www.lua.org.
+*
+*
+* Permission is hereby granted, free of charge, to any person obtaining
+* a copy of this software and associated documentation files (the
+* "Software"), to deal in the Software without restriction, including
+* without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to
+* permit persons to whom the Software is furnished to do so, subject to
+* the following conditions:
+*
+* The above copyright notice and this permission notice shall be
+* included in all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+******************************************************************************)
+
 end.
 end.
+

+ 55 - 17
LuaEdit/LuaCore/lualib.pas

@@ -1,7 +1,13 @@
 (*
 (*
-** $Id: lualib.pas,v 1.1 2005-04-24 19:31:20 jfgoulet Exp $
+** $Id: lualib.pas,v 1.2 2006-10-27 03:32:17 jfgoulet Exp $
 ** Lua standard libraries
 ** Lua standard libraries
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
+**
+**
+** 10/02/2006 Jean-Francois Goulet - Removed the luaopen_loadlib declaration for
+**                                   Lua 5.1 compatibility.
+** 10/02/2006 Jean-Francois Goulet - Modified all lua_****libopen() functions to
+**                                   support in-lua calls for Lua 5.1 compatibility.
 *)
 *)
 unit lualib;
 unit lualib;
 
 
@@ -15,6 +21,7 @@ uses
   lua;
   lua;
 
 
 const
 const
+  LUA_PACKLIBNAME = 'package';
   LUA_COLIBNAME = 'coroutine';
   LUA_COLIBNAME = 'coroutine';
   LUA_TABLIBNAME = 'table';
   LUA_TABLIBNAME = 'table';
   LUA_IOLIBNAME = 'io';
   LUA_IOLIBNAME = 'io';
@@ -24,26 +31,25 @@ const
   LUA_DBLIBNAME = 'debug';
   LUA_DBLIBNAME = 'debug';
 
 
 function luaopen_base(L: Plua_State): Integer;
 function luaopen_base(L: Plua_State): Integer;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
+
+function luaopen_package(L: Plua_State): Integer;
+  cdecl external 'lua5.1.dll';
 
 
 function luaopen_table(L: Plua_State): Integer;
 function luaopen_table(L: Plua_State): Integer;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 
 
 function luaopen_io(L: Plua_State): Integer;
 function luaopen_io(L: Plua_State): Integer;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 
 
 function luaopen_string(L: Plua_State): Integer;
 function luaopen_string(L: Plua_State): Integer;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 
 
 function luaopen_math(L: Plua_State): Integer;
 function luaopen_math(L: Plua_State): Integer;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 
 
 function luaopen_debug(L: Plua_State): Integer;
 function luaopen_debug(L: Plua_State): Integer;
-  cdecl external 'lua.dll';
-
-
-function luaopen_loadlib(L: Plua_State): Integer;
-  cdecl external 'lua.dll';
+  cdecl external 'lua5.1.dll';
 
 
 
 
 (* to help testing the libraries *)
 (* to help testing the libraries *)
@@ -54,6 +60,7 @@ function luaopen_loadlib(L: Plua_State): Integer;
 
 
 (* compatibility code *)
 (* compatibility code *)
 function lua_baselibopen(L: Plua_State): Integer;
 function lua_baselibopen(L: Plua_State): Integer;
+function lua_packlibopen(L: Plua_State): Integer;
 function lua_tablibopen(L: Plua_State): Integer;
 function lua_tablibopen(L: Plua_State): Integer;
 function lua_iolibopen(L: Plua_State): Integer;
 function lua_iolibopen(L: Plua_State): Integer;
 function lua_strlibopen(L: Plua_State): Integer;
 function lua_strlibopen(L: Plua_State): Integer;
@@ -64,32 +71,63 @@ implementation
 
 
 function lua_baselibopen(L: Plua_State): Integer;
 function lua_baselibopen(L: Plua_State): Integer;
 begin
 begin
-  Result := luaopen_base(L);
+  lua_pushcfunction(L, luaopen_base);
+  lua_pushstring(L, '');
+  lua_call(L, 1, 0);
+  Result := 1;
+end;
+
+function lua_packlibopen(L: Plua_State): Integer;
+begin
+  lua_pushcfunction(L, luaopen_package);
+  lua_pushstring(L, 'package');
+  lua_call(L, 1, 0);
+  Result := 1;
 end;
 end;
 
 
 function lua_tablibopen(L: Plua_State): Integer;
 function lua_tablibopen(L: Plua_State): Integer;
 begin
 begin
-  Result := luaopen_table(L);
+  lua_pushcfunction(L, luaopen_table);
+  lua_pushstring(L, 'table');
+  lua_call(L, 1, 0);
+  Result := 1;
 end;
 end;
 
 
 function lua_iolibopen(L: Plua_State): Integer;
 function lua_iolibopen(L: Plua_State): Integer;
 begin
 begin
-  Result := luaopen_io(L);
+  lua_pushcfunction(L, luaopen_io);
+  lua_pushstring(L, 'io');
+  lua_call(L, 1, 0);
+  Result := 1;
+
+  lua_pushcfunction(L, luaopen_os);
+  lua_pushstring(L, 'os');
+  lua_call(L, 1, 0);
+  Result := 1;
 end;
 end;
 
 
 function lua_strlibopen(L: Plua_State): Integer;
 function lua_strlibopen(L: Plua_State): Integer;
 begin
 begin
-  Result := luaopen_string(L);
+  lua_pushcfunction(L, luaopen_string);
+  lua_pushstring(L, 'string');
+  lua_call(L, 1, 0);
+  Result := 1;
 end;
 end;
 
 
 function lua_mathlibopen(L: Plua_State): Integer;
 function lua_mathlibopen(L: Plua_State): Integer;
 begin
 begin
-  Result := luaopen_math(L);
+  lua_pushcfunction(L, luaopen_math);
+  lua_pushstring(L, 'math');
+  lua_call(L, 1, 0);
+  Result := 1;
 end;
 end;
 
 
 function lua_dblibopen(L: Plua_State): Integer;
 function lua_dblibopen(L: Plua_State): Integer;
 begin
 begin
-  Result := luaopen_debug(L);
+  lua_pushcfunction(L, luaopen_debug);
+  lua_pushstring(L, 'debug');
+  lua_call(L, 1, 0);
+  Result := 1;
 end;
 end;
 
 
 end.
 end.

+ 2 - 2
LuaEdit/Syntax/LuaSyntax.pas

@@ -27,7 +27,7 @@ replace them with the notice and other provisions required by the GPL.
 If you do not delete the provisions above, a recipient may use your version
 If you do not delete the provisions above, a recipient may use your version
 of this file under either the MPL or the GPL.
 of this file under either the MPL or the GPL.
 
 
-$Id: LuaSyntax.pas,v 1.1 2005-04-02 16:41:14 jfgoulet Exp $
+$Id: LuaSyntax.pas,v 1.2 2006-10-27 03:32:56 jfgoulet Exp $
 
 
 You may retrieve the latest version of this file at the SynEdit home page,
 You may retrieve the latest version of this file at the SynEdit home page,
 located at http://SynEdit.SourceForge.net
 located at http://SynEdit.SourceForge.net
@@ -859,7 +859,7 @@ end;
 function TSynLuaSyn.GetKeyWords: string;
 function TSynLuaSyn.GetKeyWords: string;
 begin
 begin
   Result := 
   Result := 
-    'and,break,do,dofile,else,elseif,end,exit,false,for,function,if,in,loa' +
+    'and,break,div,do,dofile,else,elseif,end,exit,false,for,function,if,in,loa' +
     'ddll,local,nil,not,or,print,repeat,return,Sleep,then,true,type,until,w' +
     'ddll,local,nil,not,or,print,repeat,return,Sleep,then,true,type,until,w' +
     'hile';
     'hile';
 end;
 end;