[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: delphi object (class) with lua?
- From: "Romulo A. Bahiense" <romulo@...>
- Date: Tue, 25 May 2004 19:18:16 -0300
If lua is able to use these classes it is more direct then
inventing a new structure for use with lua.
Could you give an example of hand-registered functions as i believe a
function inside a class is not equal to a normal function even if you add
cdecl; to it.
That is what I'm afraid of. Object methods (TClass.SomeMethod) is different
from a standard function. A method function requires an instance of the
associated object (of course, there is 'class function's but the idea is
the same).
'hand-registered' functions are nothing more than inventing a new structure
to use with lua. You will have to create an userdata (full userdata)
pointing to object's address (instance) and you will have to create as many
functions as methods you want to call from lua (it is kind obvious if you
realize that TClass.DoSomething(S: String; i: Integer; P: Pointer) is far
different of lua_class_dosomething(L: Plua_State): Integer ;) ).
Have you ever seen Delphi Web Script? www.dwscript.com
What I'm trying to say is almost the same thing they do to emulate Delphi's
behavior.
The logic behind it is something like this:
Please note that in this example I'm considering that you will have
functions in lua as: TMyObject_Method(obj, par1, par2, ..., parN) and not
TMyObject:Method(par1, par2, ..., parN) just to not complicated a lot the
examples
function lua_myobject_create(L: Plua_state): Integer;
begin
// here you have to push into the stack a instance of the object
lua_pushlightuserdata(L, Pointer(TMyObject.Create));
Result:= 1;
end;
// dosomething is a method receiving only 1 parameter of type String
function lua_myobject_dosomething(L: Plua_state): Integer;
var
o: TMyObject;
begin
o:= TMyObject(lua_touserdata(L, 1));
o.DoSomething(lua_tostring(L, 2));
Result:= 0; // no return values
end;
{...}
procedure lua_register_myobject(L: Plua_state);
begin
lua_register(L, 'TMyObject_Create', @lua_myobject_create);
lua_register(L, 'TMyObject_DoSomething', @lua_myobject_dosomething);
end;
on lua:
local obj = TMyObject_Create();
TMyObject_DoSomething(obj, 'Param');
I'm not considering a table solution because there are many ways to do
this. If you want a quick way to do this using the above code:
local TMyObject = {
__instance = 0,
Create = function()
local t = TMyObject;
t.__instance = TMyObject_Create()
return t;
end,
DoSomething = function(self, s)
TMyObject_DoSomething(self.__instance, s)
end
}
local obj = TMyObject.Create()
obj:DoSomething('Param')
There are a lot of ways to do the Lua part specially because there is no
built-in OO system.
Ah! I was almost forgetting: I have not tested this code ;)
Romulo