lua-users home
lua-l archive

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


On 20 March 2016 at 06:38, Xavier Wang <weasley.wx@gmail.com> wrote:
> 2016-03-20 9:24 GMT+08:00 Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>:
>> I'm probably missing something but doesn't package.loadlib do what you want?
>> It'll find any symbol you ask but of course you can only use those that
>> are lua_CFunctions.
>>
>
> Sorry for my misunderstand words. I'm not want to *load* a module and
> find a symbol as a Lua C function, I want just *find* symbol from
> currently loaded modules!
>
> e.g. I have build a custom lua.exe by only build it with extra C
> sources, say path.c, miniz.c, and boot.c. they all export luaopen_
> functions. after that, when my script run require "path", Lua will
> find preload "path" module, and Lua module path.lua, and C module
> path.so/dll, after all, it finds luaopen_path in currenty host
> program, and it finds out it. so the module is loaded.
>
> or, my project has severals lua script embedded into my program, I
> just compile these into .c file:
> #define LUA_LIB
> #include <lua.h>
> LUALIB_API int luaopen_<scriptname>(lua_State *L) {
>   size_t chunksize = <chunksize>;
>   const char *chunk = ".....";
>   luaL_loadbuffer(....)
> }
>
> you can just build this .c file into your host program, you can use
> require to load this embedded Lua module, not need any register
> things. because the symbol luaopen_<scriptname> is already in your
> namespace!
>
> In this way, if you want debug module not loaded defaultly, just
> remove it out of linit.c list, and you can require "debug" to load it,
> not modify any other things.
>
> to load these functions, you should use GetModuleHandle(NULL) or
> dlopen(NULL) on Windows or Linux, but we can not do this by loadlib or
> require.
>
> what I want is when I compile a exe, I just add a source file that
> export a Lua library function (luaopen_whatever), and I can require
> it.

...without having to edit linit.c. That's the point, right?

I'm afraid there's no way to do this in ANSI C (or even in POSIX,
according to my reading of the dlfcn.h functions in `man dlopen`).

You might be able to implement this yourself in a Linux-specific
manner using the functions from link.h, but I never tried.

-- Hisham