Tomash Brechko
2014-08-05 22:16:50 UTC
Hello,
According to documentation,
ctype = ffi.metatype(ct, metatable)
Creates a ctype object for the given ct and associates it with a
metatable.
As I read it it _first_ creates a ctype, and _then_ associates metatable
with the _newly created_ ctype. This is logical and expected. However:
-- code
ffi = require("ffi")
ffi.cdef("struct S { int i; }")
t1 = ffi.metatype("struct S", {
__newindex = function() print("META") end
})
v1 = ffi.new(t1)
v1.x = 0 -- prints "META" as expected
v2 = ffi.new("struct S")
v2.x = 0 -- ERR: prints "META" with plain "struct
S"
t3 = ffi.metatype("struct S", {}) -- ERR: throws 'cannot change a protected
metatable'
-- end
Looks like the metatable is associated with the ct passed to the function,
not the one returned. Tested with 2.0.3 and git v2.1.
According to documentation,
ctype = ffi.metatype(ct, metatable)
Creates a ctype object for the given ct and associates it with a
metatable.
As I read it it _first_ creates a ctype, and _then_ associates metatable
with the _newly created_ ctype. This is logical and expected. However:
-- code
ffi = require("ffi")
ffi.cdef("struct S { int i; }")
t1 = ffi.metatype("struct S", {
__newindex = function() print("META") end
})
v1 = ffi.new(t1)
v1.x = 0 -- prints "META" as expected
v2 = ffi.new("struct S")
v2.x = 0 -- ERR: prints "META" with plain "struct
S"
t3 = ffi.metatype("struct S", {}) -- ERR: throws 'cannot change a protected
metatable'
-- end
Looks like the metatable is associated with the ct passed to the function,
not the one returned. Tested with 2.0.3 and git v2.1.
--
Tomash Brechko
Tomash Brechko