tags:

views:

321

answers:

4

This is a little convoluted, but lets try:

I'm integrating LUA scripting into my game engine, and I've done this in the past on win32 in an elegant way. On win32 all I did was to mark all of the functions I wanted to expose to LUA as export functions. Then, to integrate them into LUA, I'd parse the PE header of the executable, unmangle the names, parse the parameters and such, then register them with my LUA runtime. This allowed me to avoid manually registering every function individually just to expose them to LUA.

Now, flash forward to today where I'm working on the iPhone. I've looked through some Unix stuff and I've gotten very close to taking a similar approach, however I'm not sure it will actually work.

I'm not entirely familiar with Unix, but here is what I have so far on iPhone:

    Step 1: Query for the executable path through objective-C and get the path of my app
    Step 2: Use dlopen to get a handle to my app using: `dlopen(path, RTLD_NOW)`
    Step 3: Use `dlsym( libraryHandle, objectName )` to attempt to get the address of a known symbol.

The above steps won't actually get me to where I want to be, but even that doesn't work. Does anyone have any experience doing this type of thing on Unix? Are there any headers or functions I can google to put me on the right track?

Thanks;)

A: 

You need to pass --export-dynamic to the linker (via -Wl,--export-dynamic).

Note: This is for Linux, but could be a starting point for your search.

References:

CesarB
+3  A: 

iPhone does not support dynamic linking after the initital application launch. While what you want to do does not actually require linking in any new application TEXT, it would not shock me to find out that some of the dl* functions do not behave as expected.

You may be able to write some platform specific code, but I recommend using a technique developed by the various BSDs called linker sets. Bascially you annotate the functions you want to do something with (just like you currently mark them for export). Through some preprocessor magic they store the annotations, sometimes in an extra segment in the binary image, then have code that grabs that data and enumerates its. So you simply add all the functions you want into the linker set, then walk through the linker set and register all the functions in it with lua.

I know people have gotten this stuff up and running on Windows and Linux, I have used it on Mac OS X and various *BSDs. I am linking the FreeBSD linker_set implementation, but I have not personally seen the Windows implementation.

Louis Gerbarg
A: 

If static linking is an option, integrate that into the linker script. Before linking, do "nm" on all object files, extract the global symbols, and generate a C file containing a (preferably sorted/hashed) mapping of all symbol names to symbol values:

  struct symbol{ char*  name; void * value } symbols = [
     {"foo", foo},
     {"bar", bar},
     ...
     {0,0}};

If you want to be selective in what you expose, it might be easiest to implement a naming schema, e.g. prefixing all functions/methods with Lua_.

Alternatively, you can create a trivial macro,

   #define ForLua(X) X

and then grep the sources for ForLua, to select the symbols that you want to incorporate.

Martin v. Löwis
A: 

You could just generate a mapfile and use that instead, no?