lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


This great, thanks for the contrib.

I have a hacked version of bin2c that generates
luaL_loadbuffer() instead of lua_dobuffer.

and lua_pcall() is invoked by app at a later phase.

The reasons for this are:

1) the lua dostring/dobuffer are deprecated, 
I presume that this also applies to lua_dostring/lua_dobuffer 
(they are in the compatibility section of auxlib.c). 
I find it strange that the bin2c.c in the 5.0 dustro should
use deprected calls.

2) luaL_loadbuffer()/lua_pcall() gives a finer grained control
over error message processing for the embedded code.

3) lua_dobuffer() calls the depecated _ALERT if it exists,
alternatively if _ALERT does not exist it writes to 
stderr. In the library code this is the *only*
signifcant write to stderr (there also one in the debug
library). When an executable does not have stderr e.g.
a Win32 GUI app, lua_dobuffer() becomes a little messy.

The code I generated looked like

{
  static const unsigned char B1[]={
     [...]
  ret = luaL_loadbuffer(L,(const char*)B1,sizeof(B1),"=sample");
}
 
Which means that one needs to declare "int ret;"

Any thoughts on making the luaL_loadbuffer approach
tidier?

David B.

+++++++++++++++++++++++++++++++++
>Here's a 'lualized' version of 'etc/bin2c.c' found in the Lua 5.0.1 
>package.
>
>I didn't exactly need it, but compiling bin2c.c separately for each 
>platform
>didn't seem such a good idea.
>
>I've also fixed a 'feature' that caused warnings when compiled on Win32,
>if the filenames used had subpaths within them (s.a. "temp\whatever").
>
>-ak
>
>
>--
>-- BIN2C.LUA
>--
>-- Convert files to byte arrays for automatic loading with lua_dobuffer
>--
>-- Based on 'etc/bin2c.c' of Lua 5.0.1 sources by:
>--      Luiz Henrique de Figueiredo (lhf@tecgraf.puc-rio.br)
>--
>-- Fixed so that subdirectory names are not included in debug info:
>--      Asko Kauppi (asko.kauppi@sci.fi)
>--
>
>--
>local function dump( f, id )
>     local str= "static const unsigned char B"..id.."[]={\n"
>
>     while true do
>         for n=1,20 do
>             local c= f:read(1)
>             if not c then
>                 print( str.."\n};\n" ); return  -- the end
>             end
>             str= str.. string.format( "3u,", string.byte(c) )
>         end
>         print(str)
>         str= ""
>     end
>end
>
>--
>local function fdump( fn, id )
>     --
>     local f= io.open( fn, "rb" )    -- must open as binary
>
>     if not f then
>         error( "bin2c: cannot open "..fn )
>     else
>         print( "/* "..fn.." */" )
>         dump( f, id )
>         f:close()
>     end
>end
>
>--
>local function emit( fn, id )
>     local _,_, base= string.find( fn, ".+[/\\](.-)$" )    -- remove path
>     print( ' lua_dobuffer(L,(const 
>char*)B'..id..',sizeof(B'..id..'),"'..(base or fn)..'");' )
>end
>
>--
>local function main( argv )
>     --
>     print "/* code automatically generated by bin2c -- DO NOT EDIT */"
>     print "{"
>
>     if not argv[1] then   -- use stdin (no params)
>         --
>         if os.getenv("WINDIR") then
>             error "using stdin not allowed on Win32!"  -- it wouldn't 
>be binary
>         end
>
>         dump(io.stdin,0)
>         emit("=stdin",0)
>     else
>         print "/* #include'ing this file in a C program is equivalent 
>to calling"
>         for _,v in ipairs(argv) do
>             print( '  lua_dofile(L,"'.. 
>string.gsub(v,'\\','\\\\')..'");' )
>         end
>         print "*/"
>         for i,v in ipairs(argv) do fdump(v,i) end
>         for i,v in ipairs(argv) do emit(v,i) end
>     end
>
>     print "}"
>     return 0
>end
>
>return main(arg)