lua-users home
lua-l archive

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


Baozeng:

On Wed, May 22, 2019 at 4:15 AM Baozeng <sploving1@gmail.com> wrote:
> I have a simple Lua file, for example, hello.lua. The contents of the file starts with a shebang

This is not a simple lua file, shebangs are not valid lua.

>  I first read the file content into a buffer. And then use luaL_loadbuffer and lua_pcall to call the function. However, I found that luaL_loadbuffer cannot return normally because it does not deal with the buffer started with a shebang.

If you take a look at the sources of luaL_loadfile() you'll notice it
skips initial garbage ( BOMs, shell comments ) with an auxiliary
skipcomment() function.

You have two simple solutions:

1.- If you can, use luaL_loadfile instead of loading yourself, let
auxlib do the work.

2.- Otherwise, reimplement ( by creative imitation if you want ) skip
comment to skip the shebang lines on the buffer ( test * buf, if it's
# increment it until you hit end of buffer decrementing sz along the
way, the usual stuff ) ( I would wrap this in a
load_buffer_skip_shebang ).

Francisco Olarte.