lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


In the way of creating access to delphi objects the following came up:

procedure registerwithlua(L: Plua_State);
var
  methodtable, metatable, table: integer;
  TABLE_NAME: pchar;
begin

  //Create a table (represents class or object)
  TABLE_NAME:='testingtable';
  lua_newtable(L);
  table := lua_gettop(L);
  lua_pushvalue( L, table );
  lua_setglobal( L, TABLE_NAME );
  lua_dostring( L, pchar('"function tmp() error("Cannot modify constant
table: " TABLE_NAME "\" ) end"') );

  //Add a numeric value
  lua_pushvalue( L, table );
  lua_pushstring( L, pchar( 'name' ) );
  lua_pushnumber( L, 19 );
  lua_settable(L, table);

  //Add a string value
  lua_pushvalue( L, table );
  lua_pushstring( L, 'strname' );
  lua_pushstring( L, 'stringcontents' );
  lua_settable(L, table);

  //Add a function
  lua_pushvalue( L, table );
  lua_pushstring( L, 'druk' );
  lua_pushcfunction( L, lua_druk );
  lua_settable(L, table);

end;

Lua_druk looks like:
function lua_druk(L: Plua_state): Integer; cdecl;
begin
  writeln('DRUK: print some text from delphi');
  Result := 0;
end;

Calling it from lua goes like:
print(testingtable.name)
print(testingtable.strname)
testingtable.druk()

It will print on screen:
19
stringcontents
DRUK: print some text from delphi

This will allow us to create a table for the delphi class type (like
TMyObject with only a function Create() ) Allowing to call varname =
TMyObject.Create()
In delphi, lua_create (tmyobject.create calls lua_create) will create a new
var called 'varname' with in the properties, methods etc.
So all the above should be wrapped in something like a tluahelper class.
What do you think of it?