bindbc-lua :: Return tables from D

luaaaa dummy at dummy.org
Sat Sep 25 15:18:26 UTC 2021


Hi. I got a problem when I used bindbc-lua. I wanna return Lua 
table by D function like this:

**Simple example (Working)**
```
import std.stdio:writeln;
import std.conv:to;
import bindbc.lua;

void main() { // dmd 2.097.2 x64 on Mac & LUA_51, static
	lua_State *L = luaL_newstate();
     luaL_openlibs(L);
	string str = `print("Sup! I'm Lua!")`;
	luaL_loadbuffer(L, cast(const(char)*)str, str.length, "line");
	int error = lua_pcall(L, 0, 0, 0);
	if(error !=0)
		writeln(lua_tostring(L, -1).to!string); // Working fine
}
```

**Return Lua's table by D function (Can't compile it)**
```
import std.stdio:writeln;
import std.conv:to;
import bindbc.lua;

void main() { /* dmd 2.097.2 x64 on Mac & LUA_51, static */
	lua_State *L = luaL_newstate();
     luaL_openlibs(L);
	luaL_register(L, cast(const(char)*)"funcD", test);
	string str = `x = funcD()`;
	luaL_loadbuffer(L, cast(const(char)*)str, str.length, "line");
	int error = lua_pcall(L, 0, 0, 0);
	if(error !=0)
		writeln(lua_tostring(L, -1).to!string);
}

static int test(lua_State* L) {
	lua_newtable(L); // Make new table => [0:test_data]
     lua_pushstring(L, cast(const(char)*)"0");
     lua_pushstring(L, cast(const(char)*)"test_data");
     lua_settable(L, -3);
     return 1;
}
```

Please any advise me. Thanks!


More information about the Digitalmars-d-learn mailing list