Skip to content

Commit

Permalink
fix ffi.nullptr #2
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <[email protected]>
  • Loading branch information
zhaojh329 committed Jan 15, 2025
1 parent e826b2e commit 6c49e8f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
8 changes: 7 additions & 1 deletion ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ static struct ctype *ctype_new(lua_State *L, bool keep)
struct ctype *ct = lua_newuserdata(L, sizeof(struct ctype));

ct->type = CTYPE_VOID;
ct->ft = &ffi_type_void;
ct->is_const = false;

luaL_getmetatable(L, CTYPE_MT);
Expand Down Expand Up @@ -2470,7 +2471,12 @@ static void createmetatable(lua_State *L, const char *name, const struct luaL_Re
static void create_nullptr(lua_State *L)
{
struct ctype *ct = ctype_new(L, false);
ctype_to_ptr(L, ct);
struct ctype *ptr = ctype_new(L, false);

ct->type = CTYPE_PTR;
ct->is_const = false;
ct->ptr = ptr;

cdata_ptr_set(cdata_new(L, ct, NULL), NULL);
}

Expand Down
Binary file added tests/libtest.so
Binary file not shown.
3 changes: 3 additions & 0 deletions tests/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ ffi.cdef([[
]])

local tests = {
function()
print(ffi.nullptr)
end,
function()
local a = ffi.new('int [10]', {1, 2, 3})
assert(a[0] == 1)
Expand Down

0 comments on commit 6c49e8f

Please sign in to comment.