bindbc / bindbc-lua Goto Github PK
View Code? Open in Web Editor NEWStatic & dynamic D bindings to the C API of Lua, compatible with BetterC, @nogc, and nothrow.
License: Boost Software License 1.0
Static & dynamic D bindings to the C API of Lua, compatible with BetterC, @nogc, and nothrow.
License: Boost Software License 1.0
This code
import bindbc.lua;
void main()
{
LuaSupport lua = loadLua();
auto L = luaL_newstate();
luaL_requiref(L, "_G", luaopen_base, 0);
lua_pop(L, 1);
lua_close(L);
}
generates a compile-time error like this:
source/app.d(32,15): Error: function pointer `luaL_requiref(lua_State* L, const(char)* modname, int function(lua_State* L) nothrow openf, int glb)` is not callable using argument types `(lua_State*, string, extern (C) int function(lua_State* L) nothrow @nogc, int)`
source/app.d(32,15): cannot pass argument `luaopen_base` of type `extern (C) int function(lua_State* L) nothrow @nogc` to parameter `int function(lua_State* L) nothrow openf`
/usr/bin/dmd failed with exit code 1.
I attempted to hack this by fiddling with @nogc
at declarations, but it caused even more problems. I believe some better investigation is required
Is it possible to support the latest Lua version such as 5.4.2?
~>0.1.1
.the callback types like lua_CFunction
are currently declared without an extern()
so they default to extern(D)
, but the calling convention for those isn't compatible with extern(C)
in all cases:
extern(D)
functions are passed their arguments in reverse order compared to extern(C)
(this doesn't affect the common lua_CFunction
since it has just 1 parameter)one of the types this affects is custom allocator functions since they have 4 parameters:
/+ dub.sdl:
dependency "bindbc-lua" version="~>0.4.1"
subConfigurations "bindbc-lua" "static" platform="posix"
versions "BindLua_Static" "LUA_54" platform="posix"
libs "lua5.4" platform="posix"
+/
import core.stdc.stdlib;
import std.exception;
import std.stdio;
import bindbc.lua;
// https://www.lua.org/manual/5.4/manual.html#lua_Alloc
extern (C) // <-- change to extern(D) and it breaks
void* myalloc(void* ud, void* ptr, size_t osize, size_t nsize) nothrow
{
writefln("ud=%s ptr=%s osize=%s nsize=%s", ud, ptr, osize, nsize).assumeWontThrow;
if (nsize == 0)
{ free(ptr); return null; }
else
return realloc(ptr, nsize);
}
// lua_Alloc with D calling convention (same as bindbc-lua)
alias lua_Alloc_D = extern (D) void* function(void* ud, void* ptr, size_t osize, size_t nsize) nothrow;
void main()
{
lua_State* L = luaL_newstate();
lua_setallocf(L, cast(lua_Alloc_D)&myalloc, null);
luaL_openlibs(L);
if (LUA_OK != luaL_dostring(L, `
print("hello world")
`)) lua_error(L);
lua_close(L);
}
with extern(C)
on the allocator function, it works normally but if changed to extern(D)
, it prints bogus values and crashes (tested on linux with dmd, 64-bit and 32-bit)
in general, any function that's going to be called by C code should have extern(C)
to work correctly
While according to official specifications ( https://www.lua.org/manual/5.4/manual.html#lua_Reader ) it should be a size_t*
, but in this library it's not a pointer.
https://github.com/BindBC/bindbc-lua/blob/master/source/bindbc/lua/v54/types.d#L124
https://github.com/BindBC/bindbc-lua/blob/master/source/bindbc/lua/v53/types.d#L85
Didn't check any of the even older ones.
extern (D)
is not the correct calling convention. This causes segfaults.
I get this error whenever I'm trying to using lua 5.4 with bindbc-lua:
../../../.dub/packages/bindbc-lua-0.4.0/bindbc-lua/source/bindbc/lua/v54/package.d(323,9): Error: return expression expected
Although I think it's something on my end, as it seems there is a return in the source code, but I'm not sure?
lua_getstack
should be declared as lua_getstack(lua_State *L, int level, lua_Debug *ar)
but is currently missing the level
parameter in the static bindings for Lua 5.1.
bindbc-lua/source/bindbc/lua/v53/binddynamic.d
Lines 345 to 364 in 8ff70f2
Looks like the 5.3 bindings just arent done yet, but it was a pain to figure out why it wasnt finding my shared library, so this issue can just serve as a warning
lua_checklong
is declared twice — both with the wrong parameterslua_optlong
is missingTrivia
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.