Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support metatable for ctype #5

Closed
vsergeev opened this issue Oct 18, 2020 · 2 comments
Closed

Support metatable for ctype #5

vsergeev opened this issue Oct 18, 2020 · 2 comments

Comments

@vsergeev
Copy link
Contributor

It would be nice to have metatable support for ctypes themselves, as with the LuaJIT FFI:

local ffi = require('cffi')

ffi.cdef[[
typedef struct point {
    int x;
    int y;
} point_t;
]]

local mt = {
    __tostring = function (self)
        return string.format("Point<%d, %d>", self.x, self.y)
    end,
    __index = {
        from_array = function (arr)
            return ffi.new("point_t", arr[1], arr[2])
        end
    }
}

Point = ffi.metatype("point_t", mt)

print(Point(1, 2)) --> Point<1, 2>
print(Point.from_array({3, 4})) --> expected: Point<3, 4>, cffi-lua gives: 'ctype' is not indexable

This allows implementing static methods on the ctype, like alternate constructors, or utility functions namespaced under the ctype.

@q66
Copy link
Owner

q66 commented Oct 18, 2020

metatype for ctypes is supported, but it seems this case is not handled, it probably should

@q66 q66 closed this as completed in 07b30b0 Oct 18, 2020
@vsergeev
Copy link
Contributor Author

Awesome, thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants