lua-users home
lua-l archive

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


On Jan 06, 2004 at 12:17 -0500, Thatcher Ulrich wrote:
> 
> I don't have an OSX machine, but the luacheia project is set up to use
> the sourceforge compile-farm to compile luacheia on lots of platforms
> (including OSX) and run self-tests, so I can drop Eli's code into
> luacheia and see what happens...

I did this and fixed a few little bugs, and it seems to work fine.
The fixes were using lua_pushfstring instead of malloc/sprintf, using
lua_pushcclosure instead of lua_pushcfunction (the original code might
have been for an earlier version of Lua?), and adding a "const" in
front of "char *symname;" to avoid a warning.

Here is the code I'm using:

----
#ifdef USE_DYLD
#define LOADLIB

#include <mach-o/dyld.h>

static int loadlib(lua_State *L)
{
        const char *path=luaL_checkstring(L,1);
        const char *init=luaL_checkstring(L,2);

        NSObjectFileImage image;
        NSModule module = NULL;
        NSObjectFileImageReturnCode res;
        NSSymbol sym;
        const char *symname;

        res = NSCreateObjectFileImageFromFile(path, &image);
        if (res != NSObjectFileImageSuccess)
        {
                lua_pushnil(L);
                switch (res)
                {
                        case NSObjectFileImageFailure:
                                lua_pushstring(L,"Unknown error.");
                                break;
                        case NSObjectFileImageInappropriateFile:
                                lua_pushstring(L,"File is invalid type.");
                                break;
                        case NSObjectFileImageArch:
                                lua_pushstring(L,"File is for wrong CPU architecture.");
                                break;
                        case NSObjectFileImageFormat:
                                lua_pushstring(L,"File does not appear to be a Mach-O file.");
                                break;
                        case NSObjectFileImageAccess:
                                lua_pushstring(L,"Permission denied.");
                                break;
                        default:
                                lua_pushstring(L,"Unknown error.");
                }
                lua_pushstring(L,"open");
                return 3;
        }

        module = NSLinkModule(image, path, NSLINKMODULE_OPTION_PRIVATE | NSLINK\MODULE_OPTION_RETURN_ON_ERROR);
                                                                                
        if (module == NULL)
        {
                NSLinkEditErrors dylderr;
                int dylderrno;
                const char *dyldfile;
                const char *errstr;
                NSLinkEditError(&dylderr, &dylderrno, &dyldfile, &errstr);
                                                                                
                lua_pushnil(L);
                lua_pushstring(L,errstr);
                lua_pushstring(L,"init");
                return 3;
        }

        NSDestroyObjectFileImage(image);

        lua_pushfstring(L, "_%s", init);
        symname = lua_tostring(L, -1);
        sym = NSLookupSymbolInModule(module, symname);
        if (sym == NULL)
        {
                lua_pushnil(L);
                lua_pushstring(L,symname);
                lua_pushstring(L,"init");
                lua_remove(L, -4);
                return 3;
        }

        lua_pop(L, 1);
        lua_pushlightuserdata(L, module);
        lua_pushcclosure(L, NSAddressOfSymbol(sym), 1);
        return 1;
}
#endif /* USE_DYLD */