Skip to content

This module is work for cdata reference cdata in luajit ffi.

License

Notifications You must be signed in to change notification settings

vkensou/cdatarefcdata

Repository files navigation

cdatarefcdata

This module is work for cdata reference cdata in luajit ffi.

Here is a example:

ffi.cdef[[
    struct A { int id; };
    struct B { int id; struct A* a; };
]]

function createB()
    local a = ffi.new("struct A")
    local b = ffi.new("struct B")
    b.a = a
    return b
end

Above code won't work properly. Because in luajit ffi, you must reference a and b manually.

So I made this library to automatically reference cdatas. Here are 2 ways to do this: save all cdatas in one c array (see here), or save references in lua table.

save in c array

local crc = require("crcarray")

function createB()
    local b = crc.start("struct B")
    crc.setValue(b, "int", "id", 987)
    local a = crc.malloc("struct A")
    crc.setReference(b, "a", a)
    crc.setValue(a, "int", "id", 654)
    return crc.complete()
end

local b, chunk = createB()
print(b.id)
print(b.a.id)

save in lua table

local crc = require("crctable")

local M_gc_free = { __gc = function(t) print("gc") 
	crc.free(t) 
	end }

ffi.metatype("struct A", M_gc_free)
ffi.metatype("struct B", M_gc_free)

function createB()
    local a = crc.malloc("struct A")
    a.id = 654
    local b = crc.malloc("struct B")
    b.id = 987
    crc.setReference(b, "a", a)
    return b
end

b = createB()
print(b.id)
print(b.a.id)

About

This module is work for cdata reference cdata in luajit ffi.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages