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

MetaType __len not working correctly #31

Closed
mavriq-dev opened this issue Jun 18, 2022 · 16 comments
Closed

MetaType __len not working correctly #31

mavriq-dev opened this issue Jun 18, 2022 · 16 comments

Comments

@mavriq-dev
Copy link

I'm trying to duplicate the following code from luajit ffi

local cffi = require("cffi")
cffi.cdef[[
typedef struct { double x, y; } point_t;
]]

local point
local mt = {
  __add = function(a, b) return point(a.x+b.x, a.y+b.y) end,
  __len = function(a) return math.sqrt(a.x*a.x + a.y*a.y) end,
  __index = {
    area = function(a) return a.x*a.x + a.y*a.y end,
  },
}
point = cffi.metatype("point_t", mt)

local a = point(3, 4)
print(a.x, a.y)  --> 3  4
print(#a)        --> 5
print(a:area())  --> 25
local b = a + point(0.5, 8)
print(#b)        --> 12.5

Everything works with the exception of __len. when the code reaches print(#a) I get the error 'struct 0' is not callable.

__add and __index both work fine if I remove the code using __len Tried on macos and Windows.

@mavriq-dev
Copy link
Author

I should also mention this is Lua 5.3, both stand alone an embedded in an application (Reaper).

@mavriq-dev
Copy link
Author

I also tried the code from lua-cffi documentation that is similar:

local cffi = require("cffi")
cffi.cdef [[
    typedef struct point_t { double x; double y; } point_t;
]]

local point
point = cffi.metatype("point_t", {
    __add = function(a, b)
        return point(a.x + b.x, a.y + b.y)
    end,
    __len = function(a)
        return math.sqrt(a:area())
    end,
    __index = {
        area = function(a)
            return a.x * a.x + a.y * a.y
        end
    }
})

local pt = point(3, 4)
print(pt.x, pt.y) -- 3, 4
print(#pt) -- 5
print(pt:area()) -- 25
local pt2 = a + point(0.5, 8)
print(#pt2) -- 12.5

with simliar results 'struct point_t' is not callable

@q66
Copy link
Owner

q66 commented Jun 18, 2022

i will look into it later and add a testcase, seems like a bug

but I'm not home until end of the upcoming week, so it'll have to wait a little

@mavriq-dev
Copy link
Author

Sounds good. Much appreciated. This is an invaluable resource.

FYI, This affects latest version, 0.2.1 and 0.2.0. I built and tested them all. So it must have been around for a while.

@q66
Copy link
Owner

q66 commented Jun 18, 2022

it's probably been there from the beginning, it's just surprising there is not a test case for it, since the test suite has grown relatively extensive (and largely on par with luajit's own ffi tests)

@mavriq-dev
Copy link
Author

Ya, I did look in the tests to see if I was missing something. I noticed there are relatively few metamethod tests given the number that Lua now supports. The other tests are quite extensive.

@q66
Copy link
Owner

q66 commented Jun 18, 2022

most of the metamethods should not need testing as they are implemented identically to others (the code paths are the same, as they are generated via a macro)

unary metamethods are a bit special though

@q66 q66 closed this as completed in b73880e Jun 18, 2022
@q66
Copy link
Owner

q66 commented Jun 19, 2022

this should now be fixed in a way that i'm happy with, feel free to give it a test if you like (i'll cut a release during next week)

@mavriq-dev
Copy link
Author

mavriq-dev commented Jun 19, 2022 via email

@q66
Copy link
Owner

q66 commented Jun 19, 2022

never tried, i always installed libffi from homebrew when testing on macos, but never tested arm64 (should probably be the same?)

but libffi has no dependencies, so as long as you have the xcode commandline tools installed, i don't see why the usual configure+make+make install method wouldn't work?

@mavriq-dev
Copy link
Author

mavriq-dev commented Jun 19, 2022 via email

@q66
Copy link
Owner

q66 commented Jun 19, 2022

as long as the fork does not break API (at least those that cffi uses), it will work (cffi does not make any non-portable assumptions about the target or the library)

never tried cross-compilation on macos; the generic approach with configure scripts is to pass --host with the correct triplet (not --target) and possibly set CC (and possibly some other tool vars, like AR) to the right cross-compiler (you might also want --disable-shared and static-link libffi into the module)

@mavriq-dev
Copy link
Author

mavriq-dev commented Jun 19, 2022 via email

@mavriq-dev
Copy link
Author

mavriq-dev commented Jun 20, 2022 via email

@mavriq-dev
Copy link
Author

mavriq-dev commented Jun 20, 2022 via email

@mavriq-dev
Copy link
Author

mavriq-dev commented Oct 11, 2022 via email

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