我正在D2为Lua 编写绑定.这是一个Lua头文件.
typedef int (*lua_CFunction) (lua_State *L);
Run Code Online (Sandbox Code Playgroud)
我假设等效的D2陈述是:
extern(C) alias int function( lua_State* L ) lua_CFunction;
Run Code Online (Sandbox Code Playgroud)
Lua还提供了api功能:
void lua_pushcfunction( lua_State* L, string name, lua_CFunction func );
Run Code Online (Sandbox Code Playgroud)
如果我想推送一个D2函数,它必须是extern(C)还是我可以使用该函数?
int dfunc( lua_State* L )
{
std.stdio.writeln("dfunc");
}
extern(C) int cfunc( lua_State* L )
{
std.stdio.writeln("cfunc");
}
lua_State* L = lua_newstate();
lua_pushcfunction(L, "cfunc", &cfunc); //This will definitely work.
lua_pushcfunction(L, "dfunc", &dfunc); //Will this work?
Run Code Online (Sandbox Code Playgroud)
如果我只能使用cfunc,为什么?我不需要做那样的事情C++.我可以将C++函数的地址传递给C一切正常工作.