rvirding / luerl

Lua in Erlang
Apache License 2.0
1.03k stars 141 forks source link

Metatable error #168

Closed samaaron closed 9 months ago

samaaron commented 9 months ago

There appears to be an issue with metatables. The following prints the output 3 and 5 in Lua 5.3. However, in Luerl it crashes with (FunctionClauseError) no function clause matching in :luerl_heap.raw_set_table_key/4.

MyArray = {}
MyArray.__index = MyArray

function MyArray.new(array)
  local self = setmetatable({}, MyArray)
  self.array = array
  return self
end

function MyArray:__index(key)
  return self.array[key]
end

function MyArray:__newindex(key, value)
  if type(key) == "number" then
    self.array[key] = value
  else
    rawset(self, key, value)
  end
end

local seq = MyArray.new({1,2,3,4})
print(seq[3])
seq[3] = 5
print(seq[3])

Note, this behaviour was discovered by @guyjbrown.

samaaron commented 9 months ago

Fixed by switching from luerl 1 to 1.1:

This is achieved by using the following mix declaration until 1.1 is on hex.

{:luerl, git: "https://github.com/rvirding/luerl.git", tag: "1.1"},