Page 3 of 4 FirstFirst 1234 LastLast
Results 21 to 30 of 36

Thread: using Delphi classes in lua :)

  1. #21

    using Delphi classes in lua :)

    You can post the unit here, I would offer some of my own webspace but just learned I need to clean up ... I got hacked apparantly.

  2. #22

    using Delphi classes in lua :)

    Quote Originally Posted by Robert Kosek
    You can post the unit here, I would offer some of my own webspace but just learned I need to clean up ... I got hacked apparantly.
    Sorry to hear you got hacked!

    Ok, here is my code so far. BTW, the last code chunk is a snippit only as the whole unit is 1272 lines long! So I don't think I should really post it here?

    The TLuaDataStore allows lua objects to be stored/retrieved by name across lua scripts as references are used.

    TheTLuaDataStoreManager allows TLuaDataStore objects to be created and retrieved by name to allow for multiple stores

    Here is an axample script

    Code:
    --  create the LuaClass
    a = NewLuaClass("Counter")
    
    a.Count = 4 + 5
    
    a.IncCount()
    
    --  retrieve a DataStore called "Vault" (created if necessary)
    b = DataStore("Vault")
    
    --  store a in it under some name
    b.StoreByName("SomeLuaThing",a)
    
    --  retrieve 'a' by name
    c = b.RetrieveByName("SomeLuaThing")
    
    print(c.Count)
    print(c.Count2x)
    
    DestroyLuaClass(a)
    LuaClasses unit

    Code:
    Unit LuaClasses;
    
    Interface
    
    Uses
        SysUtils,
        Classes,
        Lua,
        LuaObjects;
    
    Type
        TLuaPropertyType =
        (
            eUnknownProperty,
            eIntegerProperty,
            eNumberProperty,
            eStringProperty,
            eBooleanProperty,
            eLuaClassProperty);
    
        PLuaPropertyInfo = ^TLuaPropertyInfo;
        TLuaPropertyInfo = Packed Record
            Index     : Integer;
            PropType  : TLuaPropertyType;
            IsReadOnly: Boolean;
        End;
    
        TLuaClass = Class
        Private
            FMethods    : TStringList;
            FProperties : TStringList;
            FLibName: AnsiString;
        Protected
            Function  KeyIsReadonly(Key: AnsiString): Boolean;
            Function  KeyIsProperty(Key: AnsiString): Boolean;
            Function  KeyIsMethod  (Key: AnsiString): Boolean;
            Function  KeyType      (Key: AnsiString): TLuaPropertyType;
            Procedure ReadPropertyValue(L: lua_State; PropertyIndex: Integer); Virtual;
            Procedure WritePropertyValue(L: lua_State; PropertyIndex: Integer); Virtual;
            Procedure PushMethodsOntoLuaStack(L: lua_State; Table: Integer);
            Procedure RegisterLuaMethod(AName: AnsiString; MethodIndex: Integer);
            Procedure RegisterLuaProperty(AName: AnsiString;
                                          Index: Integer;
                                          PropType: TLuaPropertyType;
                                          IsReadOnly: Boolean);
            Procedure PushMethodOntoLuaStack(L: lua_State; Name: AnsiString; Index: Integer);
            Function  ReadKeyValue(L: lua_State; Key: AnsiString): Integer; Virtual;
            Procedure WriteKeyValue(L: lua_State; Key: AnsiString); Virtual;
            Function  CallLuaMethod(L: lua_State; MethodIndex: Integer): Integer; Virtual;
        Public
            Constructor Create; Virtual;
            Destructor  Destroy; Override;
    
            Procedure PushOntoLuaStack(L: lua_State);
            Procedure RegisterWithLua(L: lua_State);
    
            Property LibName: AnsiString Read FLibName Write FLibName;
        End;
    
        TLuaClassClass = Class Of TLuaClass;
    
        TRegisteredLuaClass = Packed Record
            ClassName: AnsiString;
            ClassType: TLuaClassClass;
        End;
    
        TLuaClassFactory = Class(TLuaClass)
        Private
            FRegisteredClasses: Array Of TRegisteredLuaClass;
    
            Function  NewLuaClass(L: lua_State): Integer;
            Function  DestroyLuaClass(L: lua_State): Integer;
        Protected
            Function  CallLuaMethod(L: lua_State; MethodIndex: Integer): Integer; Override;
        Public
            Constructor Create; Override;
            Destructor  Destroy; Override;
    
            Procedure RegisterLuaClass(ClassName: AnsiString; ClassType: TLuaClassClass);
            Function  LuaClassExists(ClassName: AnsiString): Boolean;
            Function  NewLuaClassByName(ClassName: AnsiString): TLuaClass;
        End;
    
        TLuaDataStore = Class(TLuaClass)
        Private
            FDataStoreRefs: TStringList;
            
            Function  StoreByName(L: lua_State): Integer;
            Function  RetrieveByName(L: lua_State): Integer;
        Protected
            Function  CallLuaMethod(L: lua_State; MethodIndex: Integer): Integer; Override;
        Public
            Constructor Create; Override;
            Destructor  Destroy; Override;
        End;
    
        TLuaDataStoreManager = Class(TLuaClass)
        Private
            FDataStores: TStringList;
            
            Function  DataStore(L: lua_State): Integer; 
            Function  GetDataStoreByKey(Key: AnsiString): TLuaDataStore;
        Protected
            Function  CallLuaMethod(L: lua_State; MethodIndex: Integer): Integer; Override;
        Public
            Constructor Create; Override;
            Destructor  Destroy; Override;
        End;
    
    Implementation
    
    {.......................................................}
    
    {.......................................................}
    Function LuaClassCheck(L: lua_State; Idx: Integer): TLuaClass;
    Begin
        Result := Nil;
    
        If (Not lua_isuserdata(L,Idx)) Then
        Begin
            LuaDoError(L,'LuaClass expected');
            Exit;
        End;
    
        Result := lua_touserdata(L,Idx);
    
        If (Not(Result Is TLuaClass)) Then
        Begin
            LuaDoError(L,'LuaClass expected');
            Exit;
        End;
    End;
    {.......................................................}
    
    {.......................................................}
    Function  LuaClassRedirector(L: lua_State): Integer; CDecl;
    Var
        LuaClass: TLuaClass;
        Index   : Integer;
    Begin
        LuaClass := lua_touserdata    (L, lua_upvalueindex(1));
        Index    := Trunc(lua_tonumber(L, lua_upvalueindex(2)));
    
        Result := LuaClass.CallLuaMethod(L,Index);
    End;
    {.......................................................}
    
    {.......................................................}
    Function LuaClassIndex(L: lua_State): Integer; CDecl;
    Var
        LuaClass: TLuaClass;
        Key    : AnsiString;
    Begin
        LuaClass := LuaClassCheck(L,lua_upvalueindex(1));
    
        Key := luaL_checkstring(L,2);
    
        If (Not LuaClass.KeyIsProperty(Key)) And (Not LuaClass.KeyIsMethod(Key)) Then
        Begin
            LuaDoError(L,LuaClass.ClassName + ': Unknown property/method "'+Key+'"');
            Exit;
        End;
    
        Result := LuaClass.ReadKeyValue(L,Key);
    End;
    {.......................................................}
    
    {.......................................................}
    Function LuaClassNewIndex(L: lua_State): Integer; CDecl;
    Var
        LuaClass  : TLuaClass;
        Key       : AnsiString;
        KeyType   : TLuaPropertyType;
        ValueType : Integer;
    Begin
        Result := 0;
    
        LuaClass := LuaClassCheck(L, lua_upvalueindex(1));
    
        Key := luaL_checkstring(L,2);
    
        If (LuaClass.KeyIsMethod(Key)) Then
        Begin
            LuaDoError(L,LuaClass.ClassName + ': Can''t assign values to method "'+Key+'"');
            Exit;
        End
        Else
        If (LuaClass.KeyIsProperty(Key)) Then
        Begin
            If (LuaClass.KeyIsReadOnly(Key)) Then
            Begin
                LuaDoError(L,LuaClass.ClassName + ': Can''t assign values to read-only property "'+Key+'"');
                Exit;
            End
            Else
            Begin
                KeyType   := LuaClass.KeyType(Key);
                ValueType := lua_type(L,3);
    
                If (KeyType = eUnknownProperty) Then
                Begin
                    LuaDoError(L,LuaClass.ClassName + ': Can''t write to unknown property "'+Key+'"');
                    Exit;
                End
                Else
                Begin
                    If (KeyType = eIntegerProperty) And (Not LuaIsInteger(L,3,LuaClass.ClassName + '.' + Key)) Then
                    Begin
                        Result := 0;
                        Exit;
                    End
                    Else
                    If (KeyType = eNumberProperty) And (Not LuaIsNumber(L,3,LuaClass.ClassName + '.' + Key)) Then
                    Begin
                        Result := 0;
                        Exit;
                    End
                    Else
                    If (KeyType = eStringProperty) And (Not LuaIsString(L,3,LuaClass.ClassName + '.' + Key)) Then
                    Begin
                        Result := 0;
                        Exit;
                    End
                    Else
                    If (KeyType = eBooleanProperty) And (Not LuaIsBoolean(L,3,LuaClass.ClassName + '.' + Key)) Then
                    Begin
                        Result := 0;
                        Exit;
                    End
                End;
            End;
        End
        Else
        Begin
            LuaDoError(L,LuaClass.ClassName + ': Unknown property/method "'+Key+'"');
            Exit;
        End;
    
        LuaClass.WriteKeyValue(L,Key);
    End;
    {.......................................................}
    
    {.......................................................}
    Constructor TLuaClass.Create;
    Begin
        Inherited Create;
    
        FMethods    := TStringList.Create;
        FMethods.Sorted := True;
        FMethods.Duplicates := dupError;
        FProperties := TStringList.Create;
        FProperties.Sorted := True;
        FProperties.Duplicates := dupError;
        FLibName := '';
    End;
    {.......................................................}
    
    {.......................................................}
    Destructor  TLuaClass.Destroy;
    Var
        i           : Integer;
        PropertyInfo: PLuaPropertyInfo;
    Begin
        For i := 0 To FProperties.Count - 1 Do
        Begin
            PropertyInfo := PLuaPropertyInfo(FProperties.Objects[i]);
    
            Dispose(PropertyInfo);
        End;
    
        FProperties.Free;
        FMethods.Free;
    
        Inherited Destroy;
    End;
    {.......................................................}
    
    {.......................................................}
    Procedure TLuaClass.RegisterLuaMethod(AName: AnsiString; MethodIndex: Integer);
    Begin
        FMethods.AddObject(AName,Pointer(MethodIndex));
    End;
    {.......................................................}
    
    {.......................................................}
    Procedure TLuaClass.RegisterLuaProperty(AName: AnsiString;
                                            Index: Integer;
                                            PropType: TLuaPropertyType;
                                            IsReadOnly: Boolean);
    Var
        PropertyInfo: PLuaPropertyInfo;
    Begin
        New(PropertyInfo);
    
        PropertyInfo.Index := Index;
        PropertyInfo.PropType := PropType;
        PropertyInfo.IsReadOnly := IsReadOnly;
    
        FProperties.AddObject(AName,TObject(PropertyInfo));
    End;
    {.......................................................}
    
    {.......................................................}
    Function  TLuaClass.CallLuaMethod(L: lua_State; MethodIndex: Integer): Integer;
    Begin
        Result := 0;
    End;
    {.......................................................}
    
    {.......................................................}
    Function  TLuaClass.KeyIsReadonly(Key: AnsiString): Boolean;
    Var
        Index       : Integer;
        PropertyInfo: PLuaPropertyInfo;
    Begin
        Result := False;
    
        If (FMethods.Find(Key,Index)) Then
        Begin
            Result := True;
        End
        Else
        If (FProperties.Find(Key,Index)) Then
        Begin
            PropertyInfo := PLuaPropertyInfo(FProperties.Objects[Index]);
            
            Result := PropertyInfo.IsReadOnly;
        End;
    End;
    {.......................................................}
    
    {.......................................................}
    Function  TLuaClass.KeyIsProperty(Key: AnsiString): Boolean;
    Var
        Index: Integer;
    Begin
        Result := FProperties.Find(Key,Index);
    End;
    {.......................................................}
    
    {.......................................................}
    Function  TLuaClass.KeyIsMethod  (Key: AnsiString): Boolean;
    Var
        Index: Integer;
    Begin
        Result := FMethods.Find(Key,Index);
    End;
    {.......................................................}
    
    {.......................................................}
    Function  TLuaClass.KeyType      (Key: AnsiString): TLuaPropertyType;
    Var
        Index   : Integer;
        PropInfo: PLuaPropertyInfo;
    Begin
        Result := eUnknownProperty;
    
        If (FProperties.Find(Key,Index)) Then
        Begin
            PropInfo := PLuaPropertyInfo(FProperties.Objects[Index]);
            Result := PropInfo.PropType;
        End;
    End;
    {.......................................................}
    
    {.......................................................}
    Procedure TLuaClass.ReadPropertyValue(L: lua_State; PropertyIndex: Integer);
    Begin
    End;
    {.......................................................}
    
    {.......................................................}
    Procedure TLuaClass.WritePropertyValue(L: lua_State; PropertyIndex: Integer);
    Begin
    End;
    {.......................................................}
    
    {.......................................................}
    Function  TLuaClass.ReadKeyValue(L: lua_State; Key: AnsiString): Integer;
    Var
        Index   : Integer;
        PropInfo: PLuaPropertyInfo;
    Begin
        If (KeyIsMethod(Key)) Then
        Begin
            FMethods.Find(Key,Index);
    
            PushMethodOntoLuaStack(L,Key,Integer(FMethods.Objects[Index]));
        End
        Else
        Begin
            PropInfo := PLuaPropertyInfo(FProperties.Objects[Index]);
    
            ReadPropertyValue(L,PropInfo.Index);
        End;
    
        Result := 1;
    End;
    {.......................................................}
    
    {.......................................................}
    Procedure TLuaClass.WriteKeyValue(L: lua_State; Key: AnsiString);
    Var
        Index   : Integer;
        PropInfo: PLuaPropertyInfo;
    Begin
        FProperties.Find(Key,Index);
    
        PropInfo := PLuaPropertyInfo(FProperties.Objects[Index]);
    
        WritePropertyValue(L,PropInfo.Index);
    End;
    {.......................................................}
    
    {.......................................................}
    Procedure TLuaClass.PushMethodOntoLuaStack(L: lua_State; Name: AnsiString; Index: Integer);
    Begin
        lua_pushstring   (L,PChar(Name));
        lua_pushlightuserdata(L,Self);
        lua_pushnumber(L,Index);
        lua_pushcclosure(L,LuaClassRedirector,2);
    End;
    {.......................................................}
    
    {.......................................................}
    Procedure TLuaClass.PushMethodsOntoLuaStack(L: lua_State; Table: Integer);
    Var
        i: Integer;
    Begin
        //  push methods onto the stack
        For i := 0 To FMethods.Count - 1 Do
        Begin
            PushMethodOntoLuaStack(L,FMethods.Strings[i],Integer(FMethods.Objects[i]));
    
            lua_settable(L,Table);
        End;
    End;
    {.......................................................}
    
    {.......................................................}
    Procedure TLuaClass.PushOntoLuaStack(L: lua_State);
    Var
        Table : Integer;
        MT    : Integer;
        i     : Integer;
    Begin
        lua_newtable(L);
    
        Table := lua_gettop(L);
    
        //  create a meta table containing the object pointer
        lua_newtable(L);
    
        MT := lua_gettop(L);
    
        lua_pushstring(L,'__index');
        lua_pushlightuserdata(L,Self);
        lua_pushnumber(L,i);
        lua_pushcclosure(L,LuaClassIndex,2);
        lua_settable(L,MT);
    
        lua_pushstring(L,'__newindex');
        lua_pushlightuserdata(L,Self);
        lua_pushnumber(L,i);
        lua_pushcclosure(L,LuaClassNewIndex,2);
        lua_settable(L,MT);
    
        lua_pushstring(L,'_Self');
        lua_pushlightuserdata(L,Self);
        lua_settable(L,MT);
    
        lua_setmetatable(L,Table);
    End;
    {.......................................................}
    
    {.......................................................}
    Procedure TLuaClass.RegisterWithLua(L: lua_State);
    Var
        i     : Integer;
        Table : Integer;
    Begin
        Table := LUA_GLOBALSINDEX;
    
        If &#40;FLibName <> ''&#41; Then
        Begin
            lua_newtable&#40;L&#41;;
    
            Table &#58;= lua_gettop&#40;L&#41;;
        End;
    
        PushMethodsOntoLuaStack&#40;L,Table&#41;;
    
        If &#40;FLibName <> ''&#41; Then
            lua_setglobal&#40;L,PChar&#40;FLibName&#41;&#41;;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Constructor TLuaClassFactory.Create;
    Begin
        Inherited Create;
    
        RegisterLuaMethod&#40;'NewLuaClass',0&#41;;
        RegisterLuaMethod&#40;'DestroyLuaClass',1&#41;;
    
        SetLength&#40;FRegisteredClasses,0&#41;;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Destructor  TLuaClassFactory.Destroy;
    Begin
        SetLength&#40;FRegisteredClasses,0&#41;;
    
        Inherited Destroy;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Function  TLuaClassFactory.CallLuaMethod&#40;L&#58; lua_State; MethodIndex&#58; Integer&#41;&#58; Integer;
    Begin
        Case MethodIndex Of
            0 &#58; Result &#58;= NewLuaClass&#40;L&#41;;
            1 &#58; Result &#58;= DestroyLuaClass&#40;L&#41;;
        Else
            LuaDoError&#40;L,Format&#40;
                'TLuaClassFactory.CallLuaMethod&#58; MethodIndex "%d" out of bounds',
                &#91;MethodIndex&#93;&#41;&#41;;
        End;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Procedure TLuaClassFactory.RegisterLuaClass&#40;ClassName&#58; AnsiString; ClassType&#58; TLuaClassClass&#41;;
    Var
        LuaClass&#58; TLuaClass;
    Begin
        If &#40;ClassName = ''&#41; Then
            Exit;
    
        If &#40;LuaClassExists&#40;ClassName&#41;&#41; Then
            Raise Exception.Create&#40;'TLuaClassFactory.RegisterLuaClass&#58; LuaClass "'+ClassName+'" is already registered'&#41;;
    
        SetLength&#40;FRegisteredClasses,Length&#40;FRegisteredClasses&#41; + 1&#41;;
        FRegisteredClasses&#91;High&#40;FRegisteredClasses&#41;&#93;.ClassName &#58;= ClassName;
        FRegisteredClasses&#91;High&#40;FRegisteredClasses&#41;&#93;.ClassType &#58;= ClassType;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Function  TLuaClassFactory.LuaClassExists&#40;ClassName&#58; AnsiString&#41;&#58; Boolean;
    Var
        i&#58; Integer;
    Begin
        Result &#58;= False;
    
        For i &#58;= 0 To High&#40;FRegisteredClasses&#41; Do
        Begin
            If &#40;ClassName = FRegisteredClasses&#91;i&#93;.ClassName&#41; Then
            Begin
                Result &#58;= True;
                Break;
            End;
        End;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Function  TLuaClassFactory.NewLuaClassByName&#40;ClassName&#58; AnsiString&#41;&#58; TLuaClass;
    Var
        i&#58; Integer;
    Begin
        Result &#58;= Nil;
    
        For i &#58;= 0 To High&#40;FRegisteredClasses&#41; Do
        Begin
            If &#40;ClassName = FRegisteredClasses&#91;i&#93;.ClassName&#41; Then
            Begin
                Result &#58;= FRegisteredClasses&#91;i&#93;.ClassType.Create;
                Break;
            End;
        End;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Function  TLuaClassFactory.NewLuaClass&#40;L&#58; lua_State&#41;&#58; Integer;
    Var
        Table     &#58; Integer;
        LuaClass  &#58; TLuaClass;
        i         &#58; Integer;
        ClassName &#58; AnsiString;
    Begin
        ClassName &#58;= luaL_checkstring&#40;L,1&#41;;
    
        LuaClass &#58;= NewLuaClassByName&#40;ClassName&#41;;
    
        If &#40;LuaClass = Nil&#41; Then
        Begin
            LuaDoError&#40;L,'NewLuaClass&#58; Unknown class type "'+ClassName+'"'&#41;;
            Exit;
        End;
    
        LuaClass.PushOntoLuaStack&#40;L&#41;;
    
        Result &#58;= 1;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Function  TLuaClassFactory.DestroyLuaClass&#40;L&#58; lua_State&#41;&#58; Integer;
    Var
        LuaRef  &#58; Integer;
        LuaClass&#58; TLuaClass;
    Begin
        luaL_checktype&#40;L,1,LUA_TTABLE&#41;;
    
        lua_getmetatable&#40;L,1&#41;;
    
        lua_pushstring&#40;L,'_Self'&#41;;
        lua_gettable&#40;L,-2&#41;;
    
        LuaClass &#58;= LuaClassCheck&#40;L,-1&#41;;
    
        If &#40;LuaClass = Nil&#41; Then
            Exit;
    
        LuaClass.Free;
    
        Result &#58;= 0;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Constructor TLuaDataStore.Create;
    Begin
        Inherited Create;
    
        FDataStoreRefs &#58;= TStringList.Create;
        FDataStoreRefs.Sorted &#58;= True;
    
        RegisterLuaMethod&#40;'StoreByName',0&#41;;
        RegisterLuaMethod&#40;'RetrieveByName',1&#41;;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Destructor  TLuaDataStore.Destroy;
    Begin
        FDataStoreRefs.Free;
    
        Inherited Destroy;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Function  TLuaDataStore.CallLuaMethod&#40;L&#58; lua_State; MethodIndex&#58; Integer&#41;&#58; Integer;
    Begin
        Case MethodIndex Of
            0 &#58; Result &#58;= StoreByName&#40;L&#41;;
            1 &#58; Result &#58;= RetrieveByName&#40;L&#41;;
        Else
            LuaDoError&#40;L,Format&#40;
                'TLuaDataStore.CallLuaMethod&#58; MethodIndex "%d" out of bounds',
                &#91;MethodIndex&#93;&#41;&#41;;
        End;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Function  TLuaDataStore.StoreByName&#40;L&#58; lua_State&#41;&#58; Integer;
    Var
        DataStoreKey&#58; AnsiString;
        DataStoreRef&#58; Integer;
        Index       &#58; Integer;
    Begin
        luaL_checktype&#40;L,1,LUA_TSTRING&#41;;
    
        DataStoreKey &#58;= lua_tostring&#40;L,1&#41;;
    
        DataStoreRef &#58;= luaL_ref&#40;L,LUA_REGISTRYINDEX&#41;;
    
        If &#40;FDataStoreRefs.Find&#40;DataStoreKey,Index&#41;&#41; Then
        Begin
            FDataStoreRefs.Objects&#91;Index&#93; &#58;= Pointer&#40;DataStoreRef&#41;;
        End
        Else
            FDataStoreRefs.AddObject&#40;DataStorekey,Pointer&#40;DataStoreRef&#41;&#41;;
    
        Result &#58;= 0;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Function  TLuaDataStore.RetrieveByName&#40;L&#58; lua_State&#41;&#58; Integer; 
    Var
        DataStoreKey&#58; AnsiString;
        DataStoreRef&#58; Integer;
        Index       &#58; Integer;
    Begin
        luaL_checktype&#40;L,1,LUA_TSTRING&#41;;
    
        DataStoreKey &#58;= lua_tostring&#40;L,1&#41;;
    
        If &#40;FDataStoreRefs.Find&#40;DataStoreKey,Index&#41;&#41; Then
        Begin
            DataStoreRef &#58;= Integer&#40;FDataStoreRefs.Objects&#91;Index&#93;&#41;;
    
            lua_rawgeti&#40;L, LUA_REGISTRYINDEX, DataStoreRef&#41;;
        End
        Else
            lua_pushnil&#40;L&#41;;
    
        Result &#58;= 1;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Constructor TLuaDataStoreManager.Create;
    Begin
        Inherited Create;
    
        FDataStores &#58;= TStringList.Create;
        FDataStores.Sorted &#58;= True;
    
        RegisterLuaMethod&#40;'DataStore',0&#41;;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Destructor  TLuaDataStoreManager.Destroy;
    Var
        i&#58; Integer;
    Begin
        For i &#58;= 0 To FDataStores.Count - 1 Do
            TLuaDataStore&#40;FDataStores.Objects&#91;i&#93;&#41;.Free;
    
        FDataStores.Free;
    
        Inherited Destroy;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Function  TLuaDataStoreManager.CallLuaMethod&#40;L&#58; lua_State; MethodIndex&#58; Integer&#41;&#58; Integer;
    Begin
        Case MethodIndex Of
            0 &#58; Result &#58;= DataStore&#40;L&#41;;
        Else
            LuaDoError&#40;L,Format&#40;
                'TLuaDataStoreManager.CallLuaMethod&#58; MethodIndex "%d" out of bounds',
                &#91;MethodIndex&#93;&#41;&#41;;
        End;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Function  TLuaDataStoreManager.GetDataStoreByKey&#40;Key&#58; AnsiString&#41;&#58; TLuaDataStore;
    Var
        Index&#58; Integer;
    Begin
        //  store DataStores in a list and retrieve by name if it exists,
        //  and only create it if it doesn't exist.
    
        If &#40;FDataStores.Find&#40;Key,Index&#41;&#41; Then
            Result &#58;= TLuaDataStore&#40;FDataStores.Objects&#91;Index&#93;&#41;
        Else
        Begin
            Result &#58;= TLuaDataStore.Create;
    
            FDataStores.AddObject&#40;Key,Result&#41;;
        End;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Function  TLuaDataStoreManager.DataStore&#40;L&#58; lua_State&#41;&#58; Integer; 
    Var
    Var
        Table       &#58; Integer;
        DataStore   &#58; TLuaDataStore;
        DataStoreKey&#58; AnsiString;
    Begin
        luaL_checktype&#40;L,1,LUA_TSTRING&#41;;
    
        DataStoreKey &#58;= lua_tostring&#40;L,1&#41;;
    
        DataStore &#58;= GetDataStoreByKey&#40;DataStoreKey&#41;;
    
        DataStore.PushOntoLuaStack&#40;L&#41;;
    
        Result &#58;= 1;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    End.
    necessary code snippit of my LuaObjects unit.

    Code:
    Procedure LuaDoError&#40;L&#58; lua_State; ErrorMsg&#58; String&#41;;
    Function  LuaIsInteger&#40;L&#58; lua_State; Idx&#58; Integer; ErrorMsg&#58; String&#41;&#58; Boolean;
    Function  LuaIsNumber&#40;L&#58; lua_State; Idx&#58; Integer; ErrorMsg&#58; String&#41;&#58; Boolean;
    Function  LuaIsString&#40;L&#58; lua_State; Idx&#58; Integer; ErrorMsg&#58; String&#41;&#58; Boolean;
    Function  LuaIsBoolean&#40;L&#58; lua_State; Idx&#58; Integer; ErrorMsg&#58; String&#41;&#58; Boolean;
    
    Implementation
    
    Procedure LuaDoError&#40;L&#58; lua_State; ErrorMsg&#58; String&#41;;
    Begin
        lua_pushstring&#40;L,PChar&#40;ErrorMsg&#41;&#41;;
        lua_error&#40;L&#41;;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Function  LuaIsInteger&#40;L&#58; lua_State; Idx&#58; Integer; ErrorMsg&#58; String&#41;&#58; Boolean;
    Var
        Value&#58; Double;
    Begin
        Result &#58;= True;
    
        If &#40;lua_type&#40;L,idx&#41; = LUA_TNUMBER&#41; Then
            Value &#58;= lua_tonumber&#40;L,Idx&#41;
        Else
        Begin
            Result &#58;= False;
            If &#40;ErrorMsg <> ''&#41; Then
                LuaDoError&#40;L,ErrorMsg + '&#58; "integer" expected, received "' + lua_typename&#40;L,lua_type&#40;L,Idx&#41;&#41; + '"'&#41;
            Else
                LuaDoError&#40;L,'"integer" expected, received "' + lua_typename&#40;L,lua_type&#40;L,Idx&#41;&#41; + '"'&#41;;
            Exit;
        End;
    
        If &#40;Abs&#40;Value - Trunc&#40;Value&#41;&#41; > 0.0001&#41; Then
        Begin
            Result &#58;= False;
            If &#40;ErrorMsg <> ''&#41; Then
                LuaDoError&#40;L,ErrorMsg + '&#58; "integer" expected, received "' + lua_typename&#40;L,lua_type&#40;L,Idx&#41;&#41; + '"'&#41;
            Else
                LuaDoError&#40;L,'"integer" expected, received "' + lua_typename&#40;L,lua_type&#40;L,Idx&#41;&#41; + '"'&#41;;
        End;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Function  LuaIsNumber&#40;L&#58; lua_State; Idx&#58; Integer; ErrorMsg&#58; String&#41;&#58; Boolean;
    Begin
        Result &#58;= True;
        
        If &#40;lua_type&#40;L,idx&#41; <> LUA_TNUMBER&#41; Then
        Begin
            Result &#58;= False;
            If &#40;ErrorMsg <> ''&#41; Then
                LuaDoError&#40;L,ErrorMsg + '&#58; "number" expected, received "' + lua_typename&#40;L,lua_type&#40;L,Idx&#41;&#41; + '"'&#41;
            Else
                LuaDoError&#40;L,'"number" expected, received "' + lua_typename&#40;L,lua_type&#40;L,Idx&#41;&#41; + '"'&#41;;
        End;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Function  LuaIsString&#40;L&#58; lua_State; Idx&#58; Integer; ErrorMsg&#58; String&#41;&#58; Boolean;
    Begin
        Result &#58;= True;
        
        If &#40;lua_type&#40;L,idx&#41; <> LUA_TSTRING&#41; Then
        Begin
            Result &#58;= False;
            If &#40;ErrorMsg <> ''&#41; Then
                LuaDoError&#40;L,ErrorMsg + '&#58; "string" expected, received "' + lua_typename&#40;L,lua_type&#40;L,Idx&#41;&#41; + '"'&#41;
            Else
                LuaDoError&#40;L,'"string" expected, received "' + lua_typename&#40;L,lua_type&#40;L,Idx&#41;&#41; + '"'&#41;;
        End;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Function  LuaIsBoolean&#40;L&#58; lua_State; Idx&#58; Integer; ErrorMsg&#58; String&#41;&#58; Boolean;
    Begin
        Result &#58;= True;
        
        If &#40;lua_type&#40;L,idx&#41; <> LUA_TBOOLEAN&#41; Then
        Begin
            Result &#58;= False;
            If &#40;ErrorMsg <> ''&#41; Then
                LuaDoError&#40;L,ErrorMsg + '&#58; "boolean" expected, received "' + lua_typename&#40;L,lua_type&#40;L,Idx&#41;&#41; + '"'&#41;
            Else
                LuaDoError&#40;L,'"boolean" expected, received "' + lua_typename&#40;L,lua_type&#40;L,Idx&#41;&#41; + '"'&#41;;
        End;
    End;
    &#123;.......................................................&#125;
    
    &#123;.......................................................&#125;
    Cheers,
    Paul.

  3. #23

    using Delphi classes in lua :)

    Wow, that's a sizable unit, even snipped!

    Well, after checking with the group I got the space at, the accounts weren't compromized, although somehow the hacker gained write access to the entire server. If you mail me a zip of the unit with your readme and license, I'll upload it. Send it to "thewickedflea <(at)> gmail >dot> com", you know the anti-spam drill.

  4. #24

    using Delphi classes in lua :)

    Quote Originally Posted by Robert Kosek
    Wow, that's a sizable unit, even snipped!

    Well, after checking with the group I got the space at, the accounts weren't compromized, although somehow the hacker gained write access to the entire server. If you mail me a zip of the unit with your readme and license, I'll upload it. Send it to "thewickedflea <(at)> gmail >dot> com", you know the anti-spam drill.
    Cool, ok

    I'll send you a zip file containing a complete demo source and all the needed units in their entirety

    It would be cool if you could post a link to the zip file when done :-)

    cheers,
    Paul.

  5. #25

    using Delphi classes in lua :)

    [quote="paul_nicholls"]
    Quote Originally Posted by Robert Kosek
    Wow, that's a sizable unit, even snipped!

    Well, after checking with the group I got the space at, the accounts weren't compromized, although somehow the hacker gained write access to the entire server. If you mail me a zip of the unit with your readme and license, I'll upload it. Send it to "thewickedflea <(at)> gmail >dot> com", you know the anti-spam drill.
    Cool, ok

    I'll send you a zip file containing a complete demo source and all the needed units in their entirety

    It would be cool if you could post a ]

    Hi Robert,
    I tried sending my zip file to your email address (edited, non-spammed version) above but got a Delivery Status Notification (Failure) email in response

    cheers,
    Paul.

  6. #26

    using Delphi classes in lua :)

    [quote="paul_nicholls"]
    Quote Originally Posted by paul_nicholls
    Quote Originally Posted by Robert Kosek
    Wow, that's a sizable unit, even snipped!

    Well, after checking with the group I got the space at, the accounts weren't compromized, although somehow the hacker gained write access to the entire server. If you mail me a zip of the unit with your readme and license, I'll upload it. Send it to "thewickedflea <(at)> gmail >dot> com", you know the anti-spam drill.
    Cool, ok

    I'll send you a zip file containing a complete demo source and all the needed units in their entirety

    It would be cool if you could post a ]

    Hi Robert,
    I tried sending my zip file to your email address (edited, non-spammed version) above but got a Delivery Status Notification (Failure) email in response

    cheers,
    Paul.
    If you want, send it to jdarling at eonclash dot com and I'll also be happy to place up a download link for you. Our servers have never been hacked, but they have crashed a few times. Now we have a NAS that backs everything up twice a day .

    On a side note, I've almost completed the class wrapper application that can read in a Delphi file and create class wrappers from it. Maybe you and I should team up and put our stuff together as I think between the two everything is more then covered and could be very useful to others.

  7. #27

    using Delphi classes in lua :)

    After quite a bit of work the first Alpha version of Pas2Lua is ready for others to rip apart and help with development. The basic idea is that using a template file for output the unit parses a Pascal Unit, extracts the objects and their definitions and then creates a Lua Wrapper.

    You can download the first Alpha exe and all files from:
    http://www.eonclash.com/pas2lua/pas2lua.zip

    The source (everything needed to compile except SynEdit) can be downloaded from:
    http://www.eonclash.com/pas2lua/pas2lua_src.zip

    I have performed minimal testing in that I know it parses and constructs for all of the unDelphiX and Delphi VCL components successfully.

    Known problems:
    It doesn't handle overloaded methods very nicely (you have to go in and play with the output)
    Wrapper methods don't always exist for types, thus you have to create them some times (hey thats always going to be true)
    Object Properties don't work right now (I'm lazy and don't have time)

    Hope everyone finds this of some use and not just a waste of my time

  8. #28

    using Delphi classes in lua :)

    [quote="jdarling"]
    Quote Originally Posted by paul_nicholls
    Quote Originally Posted by paul_nicholls
    Quote Originally Posted by Robert Kosek
    Wow, that's a sizable unit, even snipped!

    Well, after checking with the group I got the space at, the accounts weren't compromized, although somehow the hacker gained write access to the entire server. If you mail me a zip of the unit with your readme and license, I'll upload it. Send it to "thewickedflea <(at)> gmail >dot> com", you know the anti-spam drill.
    Cool, ok

    I'll send you a zip file containing a complete demo source and all the needed units in their entirety

    It would be cool if you could post a ]

    Hi Robert,
    I tried sending my zip file to your email address (edited, non-spammed version) above but got a Delivery Status Notification (Failure) email in response

    cheers,
    Paul.
    If you want, send it to jdarling at eonclash dot com and I'll also be happy to place up a download link for you. Our servers have never been hacked, but they have crashed a few times. Now we have a NAS that backs everything up twice a day .
    cool, I have resent the zip file to Robert and yourself :-)

    Quote Originally Posted by jdarling
    On a side note, I've almost completed the class wrapper application that can read in a Delphi file and create class wrappers from it.
    nice :-)

    Quote Originally Posted by jdarling
    Maybe you and I should team up and put our stuff together as I think between the two everything is more then covered and could be very useful to others.
    I think that would be an excellent idea!
    It isn't often I get to help others :-)

    For your info, I don't have heaps of free time as I am looking after my wife and new baby daughter, but I would still like to help

    cheers,
    Paul.

  9. #29

    using Delphi classes in lua :)

    Well I've found many problems with my importer and can safely say that the code it generates doesn't work right now. Soon though I hope to have it fixed. The basic problem is that I've screwed up some place and now its storing the Self Pointer in the Metatable. Witch means that each new instance changes the Self reference for all existing instances (oops). Have a message out to the LUA Mail Group, as they have helped me to find this type of error many times before.

    If anyone is interested in seeing my unDelphiX wrappers at the broken stage you can view them at: http://www.eonclash.com/LUA/DXWrappersLuaImport.pas

    Once I get everything working again though this file will be removed and I'll be placing the importer up for download.

  10. #30

    using Delphi classes in lua :)

    Interesting stuff guys. I'm too busy to try them at the moment, but I'll give you some thoughts on them in ~2 weeks.

    You can grab Paul's lua work here: http://freepgs.com/thewickedflea/lua_class.zip

Page 3 of 4 FirstFirst 1234 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •