Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/pyjs/pre_js/literal_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ const handler = {
deleteProperty: (map, k) => (map.has(k) ? map.delete(k) : delete map[k]),
get(map, k, proxy) {
if (k === _) return map;
// Stored keys must win over Map.prototype members (e.g. a key named
// "size" would otherwise shadow the entry count getter `map.size`).
if (map.has(k)) return map.get(k);
let v = map[k];
if (typeof v === "function" && k !== "constructor") {
v = v.bind(map);
}
v ||= map.get(k);
return v;
},
getOwnPropertyDescriptor(map, k) {
Expand Down
22 changes: 22 additions & 0 deletions tests/tests/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,25 @@ def test_fundamentals(self, test_input):
def test_none(self):
t = pyjs.to_js(None)
assert pyjs.pyjs_core._module._is_undefined(t) == True


class TestPyToJsDict:
def test_dict_size_key(self):
# regression: a dict key named "size" used to collide with
# Map.prototype.size (the entry count getter) in the LiteralMap get trap.
d = pyjs.to_js({"size": 7})
assert pyjs.to_py(d["size"]) == 7

def test_dict_int_values(self):
d = pyjs.to_js({"size": 32, "usage": 136, "zero": 0, "one": 1})
assert pyjs.to_py(d["size"]) == 32
assert pyjs.to_py(d["usage"]) == 136
assert pyjs.to_py(d["zero"]) == 0
assert pyjs.to_py(d["one"]) == 1

def test_dict_roundtrip(self):
x = {"size": 32, "label": "hello", "flag": True}
d = pyjs.to_js(x)
assert pyjs.to_py(d["size"]) == 32
assert pyjs.to_py(d["label"]) == "hello"
assert pyjs.to_py(d["flag"]) is True