[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: R: Re: R: Re: How to export associative arrays to an embedded lua script?
- From: "linuxfan@..." <linuxfan@...>
- Date: Tue, 13 Oct 2015 15:29:12 +0200 (CEST)
> Are you reading this as email or through a website? Because email
>offset a "respond" option that should keep the post in the
Yes, I'm using a stupid webmail client. I can't do
> Here's a simple example. It's in C, not Pascal, but it does
Thank you all for you help, this example was perfect.
Finally I went the full userdata way. Tables could do well too, but use
slightly more lua memory (circa 3K vs 2K).
First I create my
(L, '__index'); // cell key
lua_pushcfunction(L, xl_getxtable); //
cell value, my handler
lua_rawset(L, -3); // Stores the pair in
(the last 3 lines are repeated for every needed method).
Then, for every table to expose:
p := lua_newuserdata(L, sizeof
ppointer(p)^ := pointer(xEnviron);
The first line creates a
lua userdata and returns the address of the reserved memory; the second
line writes to that memory the address of my structure; then the
metatable is linked to the userdata, and finally a name is given to the
The handler for method __index is like this:
(L)<>2 then exit; // test not strictly required
st := lua_tostring(L,
2); // field to be accessed
ptr := lua_touserdata(L, 1); //
get the reserved memory for this userdata
ptr := PPointer(ptr)
^; // get what I wrote inside it (address of my structure)
result := 1; // one value will be returned on the stack
ptr=pointer(xEnviron) then begin
The last 4 lines are repeated for every table (5 in total
The "not required" test is not required because there is no
way for lua code to reach this handler without passing the correct
The tests "if ptr=...." would be also not required for the
same reason, a typecast could be used to deploy the pointer directly.
Have a nice day, best regards,