Multiple luaL_loadfile()/lua_pcall() and persistance

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

Multiple luaL_loadfile()/lua_pcall() and persistance

Laurent FAILLIE
Hello,

I'm on a tool that load then execute several Lua scripts so I have many call to luaL_loadfile()/lua_pcall() pairs. My question is about code persistence.

3 examples :

--- script1.lua ---
print "I'm doing very interesting stuffs"
---

--- script2.lua ---
function toto()
   print "even more interesting stuffs"
end
---

--- script3.lua ---
print "Yeah, I'm here"

function tata()
  print "I'm the most interesting one"
end
---

My assertions are the following :

1/ content of script1 is only one shot so it is collected and resource are freed
2/ toto() and tata() kept, ready to be called by other code.
3/ after loading/calling all those 3 script, in Lua's memory only toto() and tata() remain, and "external" prints get collected after execution.

Am I right ?

Thanks

Laurent

Reply | Threaded
Open this post in threaded view
|

Re: Multiple luaL_loadfile()/lua_pcall() and persistance

Roberto Ierusalimschy
> I'm on a tool that load then execute several Lua scripts so I have many call to luaL_loadfile()/lua_pcall() pairs. My question is about code persistence.
>
> 3 examples :
>
> --- script1.lua ---
> print "I'm doing very interesting stuffs"
> ---
>
> --- script2.lua ---
> function toto()
>    print "even more interesting stuffs"
> end
> ---
>
> --- script3.lua ---
> print "Yeah, I'm here"
>
> function tata()
>   print "I'm the most interesting one"
> end
> ---
>
> My assertions are the following :
>
> 1/ content of script1 is only one shot so it is collected and resource are freed
> 2/ toto() and tata() kept, ready to be called by other code.
> 3/ after loading/calling all those 3 script, in Lua's memory only toto() and tata() remain, and "external" prints get collected after execution.
>
> Am I right ?

Yes. 'toto' and 'tata' are kept because they are assigned to global
variables. Everything else is eventually collected, provided you removed
from the stack the result from 'luaL_loadfile()' after calling
'lua_pcall()'.

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Multiple luaL_loadfile()/lua_pcall() and persistance

Laurent FAILLIE
In reply to this post by Laurent FAILLIE
Great, thanks Roberto,
 
> eventually collected, provided you removed
> from the stack the result from
> 'luaL_loadfile()' after calling
> 'lua_pcall()'.

Do I have to remove anything from the stack ?
As per the manual (https://www.lua.org/manual/5.1/manual.html#lua_pcall), "lua_pcall always removes the function and its arguments from the stack"
So I guessed everything is cleaned up, no ?


Reply | Threaded
Open this post in threaded view
|

Re: Multiple luaL_loadfile()/lua_pcall() and persistance

Roberto Ierusalimschy
> > eventually collected, provided you removed
> > from the stack the result from
> > 'luaL_loadfile()' after calling
> > 'lua_pcall()'.
>
> Do I have to remove anything from the stack ?
> As per the manual (https://www.lua.org/manual/5.1/manual.html#lua_pcall), "lua_pcall always removes the function and its arguments from the stack"
> So I guessed everything is cleaned up, no ?

Sure; my bad.

-- Roberto