Tomash Brechko
2014-08-05 17:32:57 UTC
Hello,
The following code throws 'stack overflow' error:
-- code
ffi = require("ffi")
s = ffi.new("struct { int i; }")
mt = {
__index = s,
__newindex = function(t, k, v)
print(k..": "..s[k].." -> "..v)
s[k] = v
end
}
print("Lua table:")
t = setmetatable({}, mt)
t.i = 21 -- prints "i: 0 -> 21"
print("s.i = "..s.i) -- prints "s.i = 21"
print("t.i = "..t.i) -- prints "t.i = 21"
print("LuaJIT C struct:")
v = ffi.metatype("struct {}", mt)()
v.i = 42 -- prints "i: 21 -> 42"
print("s.i = "..s.i) -- prints "s.i = 42"
print("v.i = "..v.i) -- read throws 'stack overflow'
-- end
Tried with official LuaJIT 2.0.3, git master and git v2.1. I think this is
a LuaJIT bug (sorry if not :)).
What I'm trying to achieve is a LuaJIT C structs with efficient field read
operation and arbitrary callback on field write operation. I believe that
using C struct for __index together with ffi.metatype() requirement that
metatable and its __index must not change are the sufficient preconditions
for read access to be efficiently compiled as if no metatable is there.
Please correct me if I'm wrong, in that case any suggestion how to achieve
the desired are welcome.
Thanks in advance,
The following code throws 'stack overflow' error:
-- code
ffi = require("ffi")
s = ffi.new("struct { int i; }")
mt = {
__index = s,
__newindex = function(t, k, v)
print(k..": "..s[k].." -> "..v)
s[k] = v
end
}
print("Lua table:")
t = setmetatable({}, mt)
t.i = 21 -- prints "i: 0 -> 21"
print("s.i = "..s.i) -- prints "s.i = 21"
print("t.i = "..t.i) -- prints "t.i = 21"
print("LuaJIT C struct:")
v = ffi.metatype("struct {}", mt)()
v.i = 42 -- prints "i: 21 -> 42"
print("s.i = "..s.i) -- prints "s.i = 42"
print("v.i = "..v.i) -- read throws 'stack overflow'
-- end
Tried with official LuaJIT 2.0.3, git master and git v2.1. I think this is
a LuaJIT bug (sorry if not :)).
What I'm trying to achieve is a LuaJIT C structs with efficient field read
operation and arbitrary callback on field write operation. I believe that
using C struct for __index together with ffi.metatype() requirement that
metatable and its __index must not change are the sufficient preconditions
for read access to be efficiently compiled as if no metatable is there.
Please correct me if I'm wrong, in that case any suggestion how to achieve
the desired are welcome.
Thanks in advance,
--
Tomash Brechko
Tomash Brechko