bindbc-lua icon indicating copy to clipboard operation
bindbc-lua copied to clipboard

How do I call `lua_pushcfunction` ?

Open markusbkk opened this issue 1 year ago • 1 comments

I'm rather new to D so maybe I'm simply missing some fundamentals, but how do I add a C function to the stack?

My code:

import raylib;
import bindbc.lua;

extern(C) static int hello(lua_State *L) {
    DrawText("Hello, LUA!", 960, 300, 28, Colors.BLACK);
    return 1;
}

extern(C) void main()
{
    LuaSupport ret = loadLua();
    lua_State* mystate = luaL_newstate();
    luaL_openlibs(mystate);
    lua_pushcfunction(mystate, &hello);
}

This fails with cannot pass argument `& hello` of type `extern (C) int function(lua_State* L)` to parameter `extern (C) int function(lua_State*) nothrow f`

markusbkk avatar Oct 11 '24 10:10 markusbkk

Just mark your function as nothrow (write nothrow between the function’s parameters and the {) You won’t be able to throw exceptions from the function, but you can call functions that throw exceptions as long as you use a try-catch block that catches type Exception. P.S. marking a module-level function static (like your hello function) doesn’t do anything.

ichordev avatar Oct 11 '24 13:10 ichordev