For you info, using jdarling's idea of an empty table + __index and __newindex I have updated my TLuaClass to check/protect methods + properties
example:
class declaration
Code:
TLuaClassCounter = Class(TLuaClass)
Private
FCount: Integer;
Function IncCount(L: lua_State): Integer;
Function GetCount(L: lua_State): Integer;
Protected
Function CallLuaMethod(L: lua_State; MethodIndex: Integer): Integer; Override;
Procedure ReadPropertyValue(L: lua_State; PropertyIndex: Integer); Override;
Procedure WritePropertyValue(L: lua_State; PropertyIndex: Integer); Override;
Public
Constructor Create; Override;
End;
class body
Code:
Constructor TLuaClassCounter.Create;
Begin
Inherited Create;
RegisterLuaMethod ('IncCount',0);
RegisterLuaMethod ('GetCount',1);
RegisterLuaProperty('Count' ,0,eIntegerProperty,False);
RegisterLuaProperty('Count2x' ,1,eIntegerProperty,True);
RegisterLuaProperty('Self' ,2,eLuaClassProperty,True);
FCount := 0;
End;
{.......................................................}
{.......................................................}
Function TLuaClassCounter.IncCount(L: lua_State): Integer;
Begin
Inc(FCount);
Result := 0;
End;
{.......................................................}
{.......................................................}
Function TLuaClassCounter.GetCount(L: lua_State): Integer;
Begin
lua_pushnumber(L,FCount);
Result := 1;
End;
{.......................................................}
{.......................................................}
Function TLuaClassCounter.CallLuaMethod(L: lua_State; MethodIndex: Integer): Integer;
Begin
Result := 0;
Case MethodIndex Of
0 : Result := IncCount(L);
1 : Result := GetCount(L);
Else
End;
End;
{.......................................................}
{.......................................................}
Procedure TLuaClassCounter.ReadPropertyValue(L: lua_State; PropertyIndex: Integer);
Begin
Case PropertyIndex Of
0 : lua_pushnumber(L,FCount);
1 : lua_pushnumber(L,FCount * 2);
2 : PushOntoLuaStack(L);
Else
End;
Result := 1;
End;
{.......................................................}
{.......................................................}
Procedure TLuaClassCounter.WritePropertyValue(L: lua_State; PropertyIndex: Integer);
Var
Value: Double;
Begin
Case PropertyIndex Of
0 : FCount := Trunc(lua_tonumber(L,3));
Else
End;
End;
{.......................................................}
{.......................................................}
Behind the scenes, no properties or methods can be overwritten in a Lua script, and read-only properties can't be written to in Lua script
Also, you can't assign the wrong type to writable properties and it will tell you
BTW, is there anywhere on this site that one can post largeish code/units to share with people?
cheers,
Paul
Bookmarks