lua-users home
lua-l archive

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


On 29 July 2010 07:39, Duck <duck@roaming.ath.cx> wrote:
>
> LuaSocket annoyingly requires that the socket module be loaded first,
> creating the socket table, into which the shared object submodule is
> subsequently loaded. This makes it impossible (or, more accurately,
> extremely tiresome) to link socket.core statically.
>
> I wish that Lua+C "combo-modules" wouldn't rely on loading the Lua code
> first, and would build the core  code as an .so on its own.
>
> I simply changed luasocket.c to "luasockcore.c", like this (luasocket
> version 2.0.2):
>
>
> 92c92
> <         luaL_openlib(L, "socket", func, 0);
> ---
>>
>>        luaL_openlib(L, "socketcore", func, 0);
>
> 113c113
> < LUASOCKET_API int luaopen_socket_core(lua_State *L) {
> ---
>>
>> LUASOCKET_API int luaopen_socketcore(lua_State *L) {
>
>
> This allows a module called "socketcore" to be build and loaded on its own,
> so it can be initialised at load time (statically linked).
>
> Then change socket.lua like this, to load socketcore.so (if not statically
> linked) instead of looking for socket/core.so. This means that socketcore.so
> no longer needs a pre-existing socket table.
>
> 13c13,22
> < local socket = require("socket.core")
> ---
>>
>> -- Because the main C package is now just 'socketcore' and not
>> 'socket.core'
>> -- (in order to simplify static linking), the main C entrypoint doesn't
>> create
>> -- a table called 'socket' with a table called 'core' inside it, but
>> instead
>> -- creates a single top-level table called 'socketcore'.
>> -- So, when the call to module('socket') happens, we need to copy the
>> -- fields and methods of 'socketcore' into the new top-level table
>> 'socket'
>> -- created by the module function.
>>
>> local socket = require("socketcore")
>
> 15a25,26
>>
>> for k,v in base.pairs(socket) do base.socket[k] = v end
>>
> 42c53
> < try = newtry()
> ---
>>
>> try = socket.newtry()
>
> I'm hoping Diego switches to this way of loading when luasocket is upgraded
> for Lua 5.2.
>
> The mime/mime.core module pair needs changing similarly. And IIRC there are
> some Kepler modules which could do with this sort of modification too.
>
>
>

Could I put up the suggestion that the module adds nothing to the
global environment, but instead just returns the module? (that is, a
NULL argument to luaL_register)...
I believe this is a practice that should be followed by all lua
modules, and infact that argument to luaL_register should be
scrapped...

Daurn