Re:Lua with Delphi

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Re:Lua with Delphi

marcus.cf
As always, i forgot the attachments. Here they are.


__________________________________________________________________________
Seleção de Softwares UOL.
10 softwares escolhidos pelo UOL para você e sua família.
http://www.uol.com.br/selecao

Attachment: luapas.zip
Description: Binary data

Reply | Threaded
Open this post in threaded view
|

Re: Re:Lua with Delphi

Jilani Khaldi-3
On Thu, 19 Jun 2003 00:00:17 -0300, marcus.cf <[hidden email]> wrote:

As always, i forgot the attachments. Here they are.
Sorry, I can't see it or read it.  Could you please send it to me? Thanks!

jilani


Reply | Threaded
Open this post in threaded view
|

Calling Lua function from C

Max Ischenko-2
Hi,

I have a function in Lua that returns a function value:
function foobar()
 return loadstring('2+3')
end

I'm trying to call the foobar from C and to save returned value (to be
able to call it later).
How am I supposed to do this?

   lua_pushstring(L, "foobar");
   lua_gettable(L, LUA_GLOBALSINDEX);
   lua_call(L, 0, 1);

   lua_tocfunction(L, -1); // returns NULL
   lua_typename(L, lua_type(L, -1)); // prints "function"

Is there a way to store a Lua function as C object (and call it) or I
should be using some other way?



-- 
Regards, max.

Reply | Threaded
Open this post in threaded view
|

Re: Calling Lua function from C

Peter Shook-5

Max Ischenko wrote:
I have a function in Lua that returns a function value:
function foobar()
 return loadstring('2+3')
end

I'm trying to call the foobar from C and to save returned value (to be
able to call it later).
How am I supposed to do this?

   lua_pushstring(L, "foobar");
   lua_gettable(L, LUA_GLOBALSINDEX);
   lua_call(L, 0, 1);

   lua_tocfunction(L, -1); // returns NULL
   lua_typename(L, lua_type(L, -1)); // prints "function"

Is there a way to store a Lua function as C object (and call it) or I
should be using some other way?

A Lua function can be stored in the Registry just like any other Lua value.

  /* take func from top of stack and store it in the Registry */
  int func_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  /* now store func_ref some place safe in C */

Later on, you can get it back and call it.

  lua_rawgeti(L, LUA_REGISTRYINDEX, func_ref);
  lua_pcall(L, 0, 0, 0);

- Petet Shook


Reply | Threaded
Open this post in threaded view
|

Re: Calling Lua function from C

Max Ischenko-2
> >I'm trying to call the foobar from C and to save returned value (to be
> >able to call it later).

> >Is there a way to store a Lua function as C object (and call it) or I
> >should be using some other way?

> A Lua function can be stored in the Registry just like any other Lua 
> value.

>   /* take func from top of stack and store it in the Registry */
>   int func_ref = luaL_ref(L, LUA_REGISTRYINDEX);
>   /* now store func_ref some place safe in C */

> Later on, you can get it back and call it.

>   lua_rawgeti(L, LUA_REGISTRYINDEX, func_ref);
>   lua_pcall(L, 0, 0, 0);


That's works perfectly, thanks!

But then another issue arises -- for each function returned
I have to have a duplicate entry in the Registry. While I'm going to
have hundreds of such refs this could become a problem. May be I just
could setup some hash table in Lua with integer indicies and return it
instead of function...

-- 
Regards, max.

Reply | Threaded
Open this post in threaded view
|

Re: Calling Lua function from C

Peter Shook-5

Max Ischenko wrote:
But then another issue arises -- for each function returned
I have to have a duplicate entry in the Registry. While I'm going to
have hundreds of such refs this could become a problem. May be I just
could setup some hash table in Lua with integer indicies and return it
instead of function...

Hi Max,

I'm not sure I follow. You have a lot of functions in Lua, that you want to call from C later on, but you don't need to give them names?

function foobar()
  return {
    assert(loadfile'setup');
    assert(loadstring'return 2+3');
    function() return 4+5 end;
    function() print(whatever) end;
  }
end

So you want to call foobar from C, store the table of functions somewhere, then later on, call each of the functions in a loop or something? Basically the following in C ?

  t = foobar()
  for n,v in t do print( v() ) end

- Peter Shook







Reply | Threaded
Open this post in threaded view
|

Re: Calling Lua function from C

Max Ischenko-2
Peter Shook wrote:

> >But then another issue arises -- for each function returned
> >I have to have a duplicate entry in the Registry. While I'm going to
> >have hundreds of such refs this could become a problem. May be I just
> >could setup some hash table in Lua with integer indicies and return it
> >instead of function...

> I'm not sure I follow.  You have a lot of functions in Lua, that you 
> want to call from C later on, but you don't need to give them names?

Exactly.


> So you want to call foobar from C, store the table of functions 
> somewhere, then later on, call each of the functions in a loop or 
> something?  Basically the following in C ?

Actually, use case is this: given a Lua expr. I'd like to compile it
(into anonymous function) and return pointer to it back to C. Then I'm
going to evaluate that expr (function) many times with different args.
There could be hundreds of such expr. around.

As it turned out, I can't easily store a pointer to Lua function in C,
I'm thinking about returning something more simple (like integer) to
escape the need for luaL_ref/luaL_unref and setup a hash table in Lua
with integer<->function mapping.



-- 
Regards, max.

l
Reply | Threaded
Open this post in threaded view
|

`break [N]' and `continue [N]' in Lua

l
Hi,

The attached file contains modified Lua 5 parser and lexer files with
added `continue' keyword. In addition, both `break' and `continue'
statements may have optional integer argument specifying which loop is
to be broken or continued. For example:

for i = 1, 10 do
    for j = 1, 10 do
        for k = 1, 10 do
            break -- breaks `for k' loop
            break 1 -- breaks `for k' loop
            break 2 -- breaks `for j' loop
            break 3 -- breaks `for i' loop
            break 4 -- error, no loop to break
            continue 2 -- continues `for j' loop
            ...
        end
    end
end

Compared with the version I posted previously, this is a bug fix
version (upvalues were not closed properly - thanks to Rici for
finding the bug).

Regards,
Leszek

Attachment: cont2.tar.gz
Description: GNU Zip compressed data