4
votes

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.

2

2 Answers

2
votes

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.

2
votes

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