creating and evaluating functions -- Lua 4.0 vs Lua 5.0

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

creating and evaluating functions -- Lua 4.0 vs Lua 5.0

Max Ischenko-2
Hi,

I have this code snippet:


function compile(spec)
	local funcBody = string.format('return function() return %s end', spec)
	return loadstring(funcBody)
end

f = compile('2==3')
print(f())

This prints 0 on Lua 4.0 and function: 0x804eb60 on Lua 5.0
To print false (a.k.a 0) in Lua 5.0 I had to wrote
print(f()())

Why I this? Did I write compile function wrong?

My goal is to create a Lua function from the user-specified string and
evaluate it.

-- 
Regards, max.

Reply | Threaded
Open this post in threaded view
|

Re: creating and evaluating functions -- Lua 4.0 vs Lua 5.0

David Jones-2
In message <20030618135518.GA29228@...>, Max Ischenko writes:
> Hi,
> 
> I have this code snippet:
> 
> 
> function compile(spec)
> 	local funcBody = string.format('return function() return %s end', spec)
> 	return loadstring(funcBody)
> end
> 
> f = compile('2==3')
> print(f())
> 
> This prints 0 on Lua 4.0 and function: 0x804eb60 on Lua 5.0
> To print false (a.k.a 0) in Lua 5.0 I had to wrote
> print(f()())
> 
> Why I this? Did I write compile function wrong?

Presumably you were using dostring in Lua 4.0 and are now using
loadstring in Lua 5.0?  Although the manual deprecates dostring in
favour of loadstring they do not have identical behaviour.

dostring will compile _and_ execute the string.

loadstring will compile it and return a function that when called has
the same effect as executing the string.

In Lua 4.0 this 'return function() return ... end' was a way to achieve
the delayed execution that loadstring already does in Lua 5.

Summary:

in Lua 5.0 use

function compile(spec)
  return loadstring(string.format('return %s', spec))
end

Hope that helps.

Cheers,
 drj

Reply | Threaded
Open this post in threaded view
|

Re: creating and evaluating functions -- Lua 4.0 vs Lua 5.0

Max Ischenko-2
David Jones wrote:

> > function compile(spec)
> > 	local funcBody = string.format('return function() return %s end', spec)
> > 	return loadstring(funcBody)
> > end
> > Why I this? Did I write compile function wrong?

> Presumably you were using dostring in Lua 4.0 and are now using
> loadstring in Lua 5.0?  Although the manual deprecates dostring in
> favour of loadstring they do not have identical behaviour.

[ explanation skipped ]

> Hope that helps.

Yes, thanks!
That's exactly what I tried to achieve.

Thanks again for your quick and clarifying response.



-- 
Regards, max.

Reply | Threaded
Open this post in threaded view
|

Re: creating and evaluating functions -- Lua 4.0 vs Lua 5.0

Jilani Khaldi-3
In reply to this post by David Jones-2
Hi All,
as a matter of fact, I am using Lua 4.0 code with Delphi where this code runs fine with Lua4:
k := lua_dostring(L,LuaCode);
where k is an integer and LuaCode is PChar (char *LuaCode)
L = PLua_State (Lua_State *L);
With Lua5 this code doesn't run anymore. I have tried the function lua_load

lua_load: function(L: Plua_State; reader: lua_Chunkreader; data: Pointer; const chunkname: PChar): Integer; cdecl;

I have tried many parameters but I can't find the right ones to put in it.

k := lua_load(L, ...);

In this case what are "reader", "lua_Chunkreader", "data", "chunkname" parameters?

Thanks!

jilani


Reply | Threaded
Open this post in threaded view
|

Re: creating and evaluating functions -- Lua 4.0 vs Lua 5.0

marcus.cf
In reply to this post by Max Ischenko-2
lua_load is a very generic function to
load code (you execute it afterwards
with lua_call or lua_pcall). Try
luaL_loadfile and luaL_loadbuffer.
But, if you prefer, lua_dostring still
exists, it is declared in lauxlib.h
(lauxlib.pas, for Delphi).

Are you using THLua or doing your own
"header files?". I was converting the
latest THLua (Lua 5.0 w0) to Lua 5.0
final, in case you are interested.

> Hi All,
> as a matter of fact, I am using Lua 4.0 code with Delphi where this code 
> runs fine with Lua4:
> k := lua_dostring(L,LuaCode);
> where k is an integer and LuaCode is PChar (char *LuaCode)
> L = PLua_State (Lua_State *L);
> With Lua5 this code doesn't run anymore. I have tried the function lua_load
> 
> lua_load: function(L: Plua_State; reader: lua_Chunkreader; data: Pointer; 
> const chunkname: PChar): Integer; cdecl;
> 
> I have tried many parameters but I can't find the right ones to put in it.
> 
> k := lua_load(L, ...);
> 
> In this case what are "reader", "lua_Chunkreader", "data", "chunkname" 
> parameters?
> 
> Thanks!
> 
> jilani
> 
> 

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



Reply | Threaded
Open this post in threaded view
|

Re: creating and evaluating functions -- Lua 4.0 vs Lua 5.0

Jilani Khaldi-3
lua_load is a very generic function to
load code (you execute it afterwards
with lua_call or lua_pcall). Try
luaL_loadfile and luaL_loadbuffer.
But, if you prefer, lua_dostring still
exists, it is declared in lauxlib.h
(lauxlib.pas, for Delphi).
"lua_dostring" just ends the program without telling any kind of error.

Are you using THLua or doing your own
"header files?". I was converting the
latest THLua (Lua 5.0 w0) to Lua 5.0
final, in case you are interested.
Yes. Thank you very much.

jilani


Reply | Threaded
Open this post in threaded view
|

Re: creating and evaluating functions -- Lua 4.0 vs Lua 5.0

Jilani Khaldi-3
In reply to this post by marcus.cf
Hi,

lua_load is a very generic function to
load code (you execute it afterwards
with lua_call or lua_pcall). Try
luaL_loadfile and luaL_loadbuffer.
But, if you prefer, lua_dostring still
exists, it is declared in lauxlib.h
(lauxlib.pas, for Delphi).
Trying with "luaL_loadbuffer", the result is always "Error! ...".
...
 st := 'print("Hello Lua!")';
 k := luaL_loadbuffer(L,PChar(st), sizeof(st), '');
 if k <> 0 then
   ShowMessage('Error! Lua interpreter failed');
...

Trying with "luaL_loadfile", the result is always the same "Error! ...".
...
 st := 'print("Hello Lua!")';
 k := luaL_loadbuffer(L,PChar(st));
 if k <> 0 then
   ShowMessage('Error! Lua interpreter failed');
...

What am I missing with paramaters?
Thanks!

jilani