Commit 758ef4af authored by Michael Droettboom's avatar Michael Droettboom

Fix conversion from jsproxy to dict

parent 18bcd9e6
...@@ -189,7 +189,11 @@ EM_JS(int, hiwire_get_global, (int idname), { ...@@ -189,7 +189,11 @@ EM_JS(int, hiwire_get_global, (int idname), {
EM_JS(int, hiwire_get_member_string, (int idobj, int idkey), { EM_JS(int, hiwire_get_member_string, (int idobj, int idkey), {
var jsobj = Module.hiwire_get_value(idobj); var jsobj = Module.hiwire_get_value(idobj);
var jskey = UTF8ToString(idkey); var jskey = UTF8ToString(idkey);
return Module.hiwire_new_value(jsobj[jskey]); if (jskey in jsobj) {
return Module.hiwire_new_value(jsobj[jskey]);
} else {
return -1;
}
}); });
EM_JS(void, hiwire_set_member_string, (int idobj, int ptrkey, int idval), { EM_JS(void, hiwire_set_member_string, (int idobj, int ptrkey, int idval), {
...@@ -217,7 +221,11 @@ EM_JS(void, hiwire_set_member_int, (int idobj, int idx, int idval), { ...@@ -217,7 +221,11 @@ EM_JS(void, hiwire_set_member_int, (int idobj, int idx, int idval), {
EM_JS(int, hiwire_get_member_obj, (int idobj, int ididx), { EM_JS(int, hiwire_get_member_obj, (int idobj, int ididx), {
var jsobj = Module.hiwire_get_value(idobj); var jsobj = Module.hiwire_get_value(idobj);
var jsidx = Module.hiwire_get_value(ididx); var jsidx = Module.hiwire_get_value(ididx);
return Module.hiwire_new_value(jsobj[jsidx]); if (jsidx in jsobj) {
return Module.hiwire_new_value(jsobj[jsidx]);
} else {
return -1;
}
}); });
EM_JS(void, hiwire_set_member_obj, (int idobj, int ididx, int idval), { EM_JS(void, hiwire_set_member_obj, (int idobj, int ididx, int idval), {
...@@ -306,10 +314,14 @@ EM_JS(int, hiwire_get_iterator, (int idobj), { ...@@ -306,10 +314,14 @@ EM_JS(int, hiwire_get_iterator, (int idobj), {
} }
var jsobj = Module.hiwire_get_value(idobj); var jsobj = Module.hiwire_get_value(idobj);
if (typeof jsobj.next === 'function') { if (typeof jsobj.next === 'function') {
return Module.hiwire_new_value(jsobj); return Module.hiwire_new_value(jsobj);
} else if (typeof jsobj[Symbol.iterator] === 'function') { } else if (typeof jsobj[Symbol.iterator] === 'function') {
return Module.hiwire_new_value(jsobj[Symbol.iterator]()) return Module.hiwire_new_value(jsobj[Symbol.iterator]());
} else {
return Module.hiwire_new_value(
Object.entries(jsobj)[Symbol.iterator]()
);
} }
return -1; return -1;
// clang-format on // clang-format on
......
...@@ -62,6 +62,11 @@ JsProxy_GetAttr(PyObject* o, PyObject* attr_name) ...@@ -62,6 +62,11 @@ JsProxy_GetAttr(PyObject* o, PyObject* attr_name)
int idresult = hiwire_get_member_string(self->js, (int)key); int idresult = hiwire_get_member_string(self->js, (int)key);
Py_DECREF(str); Py_DECREF(str);
if (idresult == -1) {
PyErr_SetString(PyExc_AttributeError, key);
return NULL;
}
if (hiwire_is_function(idresult)) { if (hiwire_is_function(idresult)) {
hiwire_decref(idresult); hiwire_decref(idresult);
return JsBoundMethod_cnew(self->js, key); return JsBoundMethod_cnew(self->js, key);
...@@ -250,6 +255,10 @@ JsProxy_subscript(PyObject* o, PyObject* pyidx) ...@@ -250,6 +255,10 @@ JsProxy_subscript(PyObject* o, PyObject* pyidx)
int ididx = python2js(pyidx); int ididx = python2js(pyidx);
int idresult = hiwire_get_member_obj(self->js, ididx); int idresult = hiwire_get_member_obj(self->js, ididx);
hiwire_decref(ididx); hiwire_decref(ididx);
if (idresult == -1) {
PyErr_SetObject(PyExc_KeyError, pyidx);
return NULL;
}
PyObject* pyresult = js2python(idresult); PyObject* pyresult = js2python(idresult);
hiwire_decref(idresult); hiwire_decref(idresult);
return pyresult; return pyresult;
......
...@@ -368,7 +368,7 @@ def test_jsproxy(selenium): ...@@ -368,7 +368,7 @@ def test_jsproxy(selenium):
""" """
from js import TEST from js import TEST
del TEST.y del TEST.y
TEST.y""") is None hasattr(TEST, 'y')""") is False
selenium.run_js( selenium.run_js(
""" """
class Point { class Point {
...@@ -382,7 +382,7 @@ def test_jsproxy(selenium): ...@@ -382,7 +382,7 @@ def test_jsproxy(selenium):
""" """
from js import TEST from js import TEST
del TEST['y'] del TEST['y']
TEST['y']""") is None 'y' in TEST""") is False
assert selenium.run( assert selenium.run(
""" """
from js import TEST from js import TEST
...@@ -393,6 +393,16 @@ def test_jsproxy(selenium): ...@@ -393,6 +393,16 @@ def test_jsproxy(selenium):
from js import TEST from js import TEST
TEST != 'foo' TEST != 'foo'
""") """)
selenium.run_js(
"""
window.TEST = {foo: 'bar', baz: 'bap'}
""")
assert selenium.run(
"""
from js import TEST
dict(TEST) == {'foo': 'bar', 'baz': 'bap'}
"""
) is True
def test_jsproxy_iter(selenium): def test_jsproxy_iter(selenium):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment