Skip to content

Commit

Permalink
Fix error in OrderedMap.set when key is a method name
Browse files Browse the repository at this point in the history
Use raw access to check if a key exists, so that keys naming
OrderedMap methods are handled correctly.

Fixes lunarmodules#173.
  • Loading branch information
mpeterv committed Dec 22, 2015
1 parent 2c02bda commit 16f1ed9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lua/pl/OrderedMap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ end
-- @param val the value
-- @return the map
function OrderedMap:set (key,val)
if self[key] == nil and val ~= nil then -- new key
self._keys:append(key) -- we keep in order
rawset(self,key,val) -- don't want to provoke __newindex!
if rawget(self, key) == nil and val ~= nil then -- new key
self._keys:append(key) -- we keep in order
rawset(self,key,val) -- don't want to provoke __newindex!
else -- existing key-value pair
if val == nil then
self._keys:remove_value(key)
Expand Down
4 changes: 4 additions & 0 deletions tests/test-set.lua
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ asserteq2 ('one',1,fn())
asserteq2 ('two',2,fn())
asserteq2 ('three',3,fn())

-- Keys overriding methods can be used.
m:set('set', 4)
asserteq(m:values(),List{1,2,3,4})

o1 = OrderedMap {{z=2},{beta=1},{name='fred'}}
asserteq(tostring(o1),'{z=2,beta=1,name="fred"}')

Expand Down

0 comments on commit 16f1ed9

Please sign in to comment.