How to load LUAC compiled code from a C array?

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

How to load LUAC compiled code from a C array?

Tony Papadimitriou
How would one do the equivalent of luaL_dostring() but instead of a null terminated C string with source code I want to load the binary output of luac compiled code.
And I want to get this code to be loaded from a C array, not from some external file.  Is this possible, and if so, can someone provide an example?
 
Thank you.
Reply | Threaded
Open this post in threaded view
|

Re: How to load LUAC compiled code from a C array?

Xavier Wang

Tony Papadimitriou <[hidden email]>于2017年2月28日 周二下午10:56写道:
How would one do the equivalent of luaL_dostring() but instead of a null terminated C string with source code I want to load the binary output of luac compiled code.
And I want to get this code to be loaded from a C array, not from some external file.  Is this possible, and if so, can someone provide an example?
 
Thank you.

luaL_loadbuffer
--
regards,
Xavier Wang.
Reply | Threaded
Open this post in threaded view
|

Re: How to load LUAC compiled code from a C array?

Francisco Olarte
In reply to this post by Tony Papadimitriou
Tony:

On Tue, Feb 28, 2017 at 3:56 PM, Tony Papadimitriou <[hidden email]> wrote:
> How would one do the equivalent of luaL_dostring() but instead of a null
> terminated C string with source code I want to load the binary output of
> luac compiled code.
> And I want to get this code to be loaded from a C array, not from some
> external file.  Is this possible, and if so, can someone provide an example?

The source Tony, use the source.

Now, seriously. Every luaL_* function is done in term of the
primitives in lua_*. If you look at lauxlib.h you'll find
luaL_do{string,file} are just luaL_loadXX plus lua_pcall. In turn all
luaL_load* end up in luaL_loadbufferx, which just uses lua_load (
that's the primitive one ) which is documented as reading either text
or binary. So binary is fine in load_buffer. As you probably do not
want to mingle with writing a reader function ( as lualib has a
perfectly fine one which it uses to implement the loadbuffers ) you
just need to copy a bit fom luaL_dostring and make yourself a
luaL_dobuffer[x] using loadBufferX and lua_pcall in the same way
luaL_dostring is done. Easy peasy, faster to do than tell.

Francisco Olarte.