4

EDIT: This has been tracked down to a more general problem with shared libraries, the d runtime and os x. See here: Initializing the D runtime on OS X

I'm trying to get a simple d function accessible from the Lua stand-alone interpreter.

I couldn't see an immediately obvious way to get the lua instance to recognise a d library so i tried this hack

import luad.all, luad.c.all;

extern (C) int luaopen_luad_test(lua_State* L) {
    auto lua = new LuaState(L);
    lua["addition"] = &addition;
    return(0);
}

int addition(int a, int b)
{
    return(a+b);
}

I know that when I call require("luad_test") it will call luaopen_luad_test(lua_State* L) giving me access to the lua_State of the interpreter. However, when I call require i just get a seg fault.

Am I looking at this completely the wrong way?

edit: I'm using lua 5.1.5 on os x and i've added ";?.dylib" to package.cpath in order to allow loading .dylib instead of .so

edit2: I've narrowed it down a bit. Any use of new in luaopen_luad_test causes a segfault.

Community
  • 1
  • 1
John_C
  • 788
  • 5
  • 17

2 Answers2

2

One possibility is that you haven't set up the D runtime and the GC isn't in a valid state.

I've never used luad (or D without it's providing the main function) so I could be off base.

BCS
  • 75,627
  • 68
  • 187
  • 294
  • That's almost certainly the case, but starting the runtime by using the intialize function also causes a segfault. – John_C Jun 21 '12 at 19:46
2

You have to initialise the D runtime library. Try the following:

import luad.all, luad.c.all;

extern (C) int luaopen_luad_test(lua_State* L) {
  Runtime.initialize();
  static __gshared LuaState lua = new LuaState(L);
  lua["addition"] = &addition;
  return(0);
} // luaopen_luad_test() C function

int addition(int a, int b) {
  return(a+b);
} // addition() function
DejanLekic
  • 18,787
  • 4
  • 46
  • 77
  • (for documentation from #d) calling Runtime.initialize causes a segfault as well, so it never gets to the __gshared LuaState etc... – John_C Jun 22 '12 at 12:09
  • OS X Lion, compiling with dmd 2.059 and running with Lua 5.1.5 – John_C Jun 22 '12 at 16:16
  • I made a quick test with loading a .dylib written in d from c and got exactly the same problems. It seems that there's more to setting up the runtime on os x than just calling Runtime.initialize() – John_C Jun 23 '12 at 11:30
  • Seeing as this is no longer a luad problem, i've started a new question: http://stackoverflow.com/questions/11170139/initializing-the-d-runtime-on-os-x – John_C Jun 23 '12 at 19:39