Commit 6cbac839 authored by Michael Droettboom's avatar Michael Droettboom

Use variable naming convention to make types clearer

parent 593b3536
#include <emscripten.h> #include <emscripten.h>
// TODO: Use consistent naming so it's clear what's an id and what's a concrete value
EM_JS(void, hiwire_setup, (), { EM_JS(void, hiwire_setup, (), {
var hiwire = { var hiwire = {
objects: {}, objects: {},
id_src: 1 counter: 1
}; };
Module.hiwire_new_value = function(val) { Module.hiwire_new_value = function(jsval) {
var objects = hiwire.objects; var objects = hiwire.objects;
while (hiwire.id_src in objects) { while (hiwire.counter in objects) {
hiwire.id_src = (hiwire.id_src + 1) % 0x8fffffff; hiwire.counter = (hiwire.counter + 1) % 0x8fffffff;
} }
var id = hiwire.id_src; var idval = hiwire.counter;
objects[id] = val; objects[idval] = jsval;
hiwire.id_src = (hiwire.id_src + 1) % 0x8fffffff; hiwire.counter = (hiwire.counter + 1) % 0x8fffffff;
return id; return idval;
}; };
Module.hiwire_get_value = function(id) { Module.hiwire_get_value = function(idval) {
return hiwire.objects[id]; return hiwire.objects[idval];
}; };
Module.hiwire_decref = function(id) { Module.hiwire_decref = function(idval) {
var objects = hiwire.objects; var objects = hiwire.objects;
delete objects[id]; delete objects[idval];
}; };
}); });
EM_JS(int, hiwire_incref, (int id), { EM_JS(int, hiwire_incref, (int idval), {
return Module.hiwire_new_value(Module.hiwire_get_value(id)); return Module.hiwire_new_value(Module.hiwire_get_value(idval));
}); });
EM_JS(void, hiwire_decref, (int id), { EM_JS(void, hiwire_decref, (int idval), {
Module.hiwire_decref(id); Module.hiwire_decref(idval);
}); });
EM_JS(int, hiwire_int, (int value), { EM_JS(int, hiwire_int, (int val), {
return Module.hiwire_new_value(value); return Module.hiwire_new_value(val);
}); });
EM_JS(int, hiwire_double, (double value), { EM_JS(int, hiwire_double, (double val), {
return Module.hiwire_new_value(value); return Module.hiwire_new_value(val);
}); });
EM_JS(int, hiwire_string_utf8_length, (int pointer, int length), { EM_JS(int, hiwire_string_utf8_length, (int ptr, int len), {
var bytes = new Uint8Array(Module.HEAPU8.buffer, pointer, length); var bytes = new Uint8Array(Module.HEAPU8.buffer, ptr, len);
var value = new TextDecoder('utf-8').decode(bytes); var jsval = new TextDecoder('utf-8').decode(bytes);
return Module.hiwire_new_value(value); return Module.hiwire_new_value(jsval);
}); });
EM_JS(int, hiwire_string_utf8, (int pointer), { EM_JS(int, hiwire_string_utf8, (int ptr), {
return Module.hiwire_new_value(UTF8ToString(pointer)); return Module.hiwire_new_value(UTF8ToString(ptr));
}); });
EM_JS(int, hiwire_bytes, (int pointer, int length), { EM_JS(int, hiwire_bytes, (int ptr, int len), {
var bytes = new Uint8Array(Module.HEAPU8.buffer, pointer, length); var bytes = new Uint8Array(Module.HEAPU8.buffer, ptr, len);
return Module.hiwire_new_value(bytes); return Module.hiwire_new_value(bytes);
}); });
...@@ -76,83 +74,85 @@ EM_JS(int, hiwire_false, (), { ...@@ -76,83 +74,85 @@ EM_JS(int, hiwire_false, (), {
return Module.hiwire_new_value(false); return Module.hiwire_new_value(false);
}); });
EM_JS(int, hiwire_throw_error, (int id), { EM_JS(int, hiwire_throw_error, (int idmsg), {
var msg = Module.hiwire_get_value(id); var jsmsg = Module.hiwire_get_value(idmsg);
Module.hiwire_decref(id); Module.hiwire_decref(idmsg);
throw new Error(msg); throw new Error(jsmsg);
}); });
EM_JS(int, hiwire_array, (), { EM_JS(int, hiwire_array, (), {
return Module.hiwire_new_value([]); return Module.hiwire_new_value([]);
}); });
EM_JS(void, hiwire_push_array, (int id, int val), { EM_JS(void, hiwire_push_array, (int idarr, int idval), {
Module.hiwire_get_value(id).push(Module.hiwire_get_value(val)); Module.hiwire_get_value(idarr).push(Module.hiwire_get_value(idval));
}); });
EM_JS(int, hiwire_object, (), { EM_JS(int, hiwire_object, (), {
return Module.hiwire_new_value({}); return Module.hiwire_new_value({});
}); });
EM_JS(void, hiwire_push_object_pair, (int id, int key, int val), { EM_JS(void, hiwire_push_object_pair, (int idobj, int idkey, int idval), {
var jsobj = Module.hiwire_get_value(id); var jsobj = Module.hiwire_get_value(idobj);
var jskey = Module.hiwire_get_value(key); var jskey = Module.hiwire_get_value(idkey);
var jsval = Module.hiwire_get_value(val); var jsval = Module.hiwire_get_value(idval);
jsobj[jskey] = jsval; jsobj[jskey] = jsval;
}); });
EM_JS(int, hiwire_get_global, (int ptr), { EM_JS(int, hiwire_get_global, (int idname), {
var idx = UTF8ToString(ptr); var jsname = UTF8ToString(idname);
return Module.hiwire_new_value(window[idx]); return Module.hiwire_new_value(window[jsname]);
}); });
EM_JS(int, hiwire_get_member_string, (int ptr, int key), { EM_JS(int, hiwire_get_member_string, (int idobj, int idkey), {
var jskey = UTF8ToString(key); var jsobj = Module.hiwire_get_value(idobj);
var jsobj = Module.hiwire_get_value(ptr); var jskey = UTF8ToString(idkey);
return Module.hiwire_new_value(jsobj[jskey]); return Module.hiwire_new_value(jsobj[jskey]);
}); });
EM_JS(void, hiwire_set_member_string, (int ptr, int key, int val), { EM_JS(void, hiwire_set_member_string, (int idobj, int ptrkey, int idval), {
var jskey = UTF8ToString(key); var jsobj = Module.hiwire_get_value(idobj);
Module.hiwire_get_value(ptr)[jskey] = Module.hiwire_get_value(val); var jskey = UTF8ToString(ptrkey);
var jsval = Module.hiwire_get_value(idval);
jsobj[jskey] = jsval;
}); });
EM_JS(int, hiwire_get_member_int, (int ptr, int idx), { EM_JS(int, hiwire_get_member_int, (int idobj, int idx), {
var jsobj = Module.hiwire_get_value(ptr); var jsobj = Module.hiwire_get_value(idobj);
return Module.hiwire_new_value(jsobj[idx]); return Module.hiwire_new_value(jsobj[idx]);
}); });
EM_JS(void, hiwire_set_member_int, (int ptr, int idx, int val), { EM_JS(void, hiwire_set_member_int, (int idobj, int idx, int idval), {
Module.hiwire_get_value(ptr)[idx] = Module.hiwire_get_value(val); Module.hiwire_get_value(idobj)[idx] = Module.hiwire_get_value(idval);
}); });
EM_JS(void, hiwire_call, (int ptr, int args), { EM_JS(void, hiwire_call, (int idfunc, int idargs), {
var jsargs = Module.hiwire_get_value(args); var jsfunc = Module.hiwire_get_value(idfunc);
var callable = Module.hiwire_get_value(ptr); var jsargs = Module.hiwire_get_value(idargs);
return Module.hiwire_new_value(callable.apply(callable, jsargs)); return Module.hiwire_new_value(jsfunc.apply(jsfunc, jsargs));
}); });
EM_JS(void, hiwire_call_member, (int ptr, int name, int args), { EM_JS(void, hiwire_call_member, (int idobj, int ptrname, int idargs), {
var jsname = UTF8ToString(name); var jsobj = Module.hiwire_get_value(idobj);
var jsargs = Module.hiwire_get_value(args); var jsname = UTF8ToString(ptrname);
var callable = Module.hiwire_get_value(ptr); var jsargs = Module.hiwire_get_value(idargs);
return Module.hiwire_new_value(callable[jsname].apply(callable, jsargs)); return Module.hiwire_new_value(jsobj[jsname].apply(jsobj, jsargs));
}); });
EM_JS(void, hiwire_new, (int ptr, int args), { EM_JS(void, hiwire_new, (int idobj, int idargs), {
var jsargs = Module.hiwire_get_value(args); var jsobj = Module.hiwire_get_value(idobj);
var callable = Module.hiwire_get_value(ptr); var jsargs = Module.hiwire_get_value(idargs);
return Module.hiwire_new_value(new (Function.prototype.bind.apply(callable, jsargs))); return Module.hiwire_new_value(new (Function.prototype.bind.apply(jsobj, jsargs)));
}); });
EM_JS(void, hiwire_length, (int ptr), { EM_JS(void, hiwire_get_length, (int idobj), {
return Module.hiwire_get_value(ptr).length; return Module.hiwire_get_value(idobj).length;
}); });
EM_JS(void, hiwire_is_function, (int ptr), { EM_JS(void, hiwire_is_function, (int idobj), {
return typeof Module.hiwire_get_value(ptr) === 'function'; return typeof Module.hiwire_get_value(idobj) === 'function';
}); });
EM_JS(void, hiwire_to_string, (int ptr), { EM_JS(void, hiwire_to_string, (int idobj), {
return Module.hiwire_new_value(Module.hiwire_get_value(ptr).toString()); return Module.hiwire_new_value(Module.hiwire_get_value(idobj).toString());
}); });
...@@ -4,32 +4,32 @@ ...@@ -4,32 +4,32 @@
// TODO: Document me // TODO: Document me
void hiwire_setup(); void hiwire_setup();
int hiwire_incref(int); int hiwire_incref(int idval);
void hiwire_decref(int); void hiwire_decref(int idval);
int hiwire_int(int); int hiwire_int(int val);
int hiwire_double(double); int hiwire_double(double val);
int hiwire_string_utf8_length(int, int); int hiwire_string_utf8_length(int ptr, int len);
int hiwire_string_utf8(int); int hiwire_string_utf8(int ptr);
int hiwire_bytes(int, int); int hiwire_bytes(int ptr, int len);
int hiwire_undefined(); int hiwire_undefined();
int hiwire_null(); int hiwire_null();
int hiwire_true(); int hiwire_true();
int hiwire_false(); int hiwire_false();
int hiwire_array(); int hiwire_array();
int hiwire_push_array(int, int); int hiwire_push_array(int idobj, int idval);
int hiwire_object(); int hiwire_object();
int hiwire_push_object_pair(int, int, int); int hiwire_push_object_pair(int idobj, int idkey, int idval);
int hiwire_throw_error(int); int hiwire_throw_error(int idmsg);
int hiwire_get_global(int); int hiwire_get_global(int ptrname);
int hiwire_get_member_string(int, int); int hiwire_get_member_string(int idobj, int ptrname);
void hiwire_set_member_string(int, int, int); void hiwire_set_member_string(int idobj, int ptrname, int idval);
int hiwire_get_member_int(int, int); int hiwire_get_member_int(int idobj, int idx);
void hiwire_set_member_int(int, int, int); void hiwire_set_member_int(int idobj, int idx, int idval);
int hiwire_call(int, int); int hiwire_call(int idobj, int idargs);
int hiwire_call_member(int, int, int); int hiwire_call_member(int idobj, int ptrname, int idargs);
int hiwire_new(int, int); int hiwire_new(int idobj, int idargs);
int hiwire_length(int); int hiwire_get_length(int idobj);
int hiwire_is_function(int); int hiwire_is_function(int idobj);
int hiwire_to_string(int); int hiwire_to_string(int idobj);
#endif /* HIWIRE_H */ #endif /* HIWIRE_H */
...@@ -23,8 +23,8 @@ static void JsProxy_dealloc(JsProxy *self) { ...@@ -23,8 +23,8 @@ static void JsProxy_dealloc(JsProxy *self) {
static PyObject *JsProxy_Repr(PyObject *o) { static PyObject *JsProxy_Repr(PyObject *o) {
JsProxy *self = (JsProxy *)o; JsProxy *self = (JsProxy *)o;
int jsrepr = hiwire_to_string(self->js); int idrepr = hiwire_to_string(self->js);
PyObject *pyrepr = jsToPython(jsrepr); PyObject *pyrepr = jsToPython(idrepr);
return pyrepr; return pyrepr;
} }
...@@ -43,20 +43,20 @@ static PyObject *JsProxy_GetAttr(PyObject *o, PyObject *attr_name) { ...@@ -43,20 +43,20 @@ static PyObject *JsProxy_GetAttr(PyObject *o, PyObject *attr_name) {
return PyObject_GenericGetAttr(o, attr_name); return PyObject_GenericGetAttr(o, attr_name);
} }
int v = 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 (hiwire_is_function(v)) { if (hiwire_is_function(idresult)) {
hiwire_decref(v); hiwire_decref(idresult);
return JsBoundMethod_cnew(self->js, key); return JsBoundMethod_cnew(self->js, key);
} }
PyObject *result = jsToPython(v); PyObject *pyresult = jsToPython(idresult);
hiwire_decref(v); hiwire_decref(idresult);
return result; return pyresult;
} }
static int JsProxy_SetAttr(PyObject *o, PyObject *attr_name, PyObject *value) { static int JsProxy_SetAttr(PyObject *o, PyObject *attr_name, PyObject *pyvalue) {
JsProxy *self = (JsProxy *)o; JsProxy *self = (JsProxy *)o;
PyObject *attr_name_py_str = PyObject_Str(attr_name); PyObject *attr_name_py_str = PyObject_Str(attr_name);
...@@ -64,9 +64,9 @@ static int JsProxy_SetAttr(PyObject *o, PyObject *attr_name, PyObject *value) { ...@@ -64,9 +64,9 @@ static int JsProxy_SetAttr(PyObject *o, PyObject *attr_name, PyObject *value) {
return -1; return -1;
} }
char *key = PyUnicode_AsUTF8(attr_name_py_str); char *key = PyUnicode_AsUTF8(attr_name_py_str);
int jsvalue = pythonToJs(value); int idvalue = pythonToJs(pyvalue);
hiwire_set_member_string(self->js, (int)key, jsvalue); hiwire_set_member_string(self->js, (int)key, idvalue);
hiwire_decref(jsvalue); hiwire_decref(idvalue);
Py_DECREF(attr_name_py_str); Py_DECREF(attr_name_py_str);
return 0; return 0;
...@@ -77,19 +77,19 @@ static PyObject* JsProxy_Call(PyObject *o, PyObject *args, PyObject *kwargs) { ...@@ -77,19 +77,19 @@ static PyObject* JsProxy_Call(PyObject *o, PyObject *args, PyObject *kwargs) {
Py_ssize_t nargs = PyTuple_Size(args); Py_ssize_t nargs = PyTuple_Size(args);
int jsargs = hiwire_array(); int idargs = hiwire_array();
for (Py_ssize_t i; i < nargs; ++i) { for (Py_ssize_t i; i < nargs; ++i) {
int jsarg = pythonToJs(PyTuple_GET_ITEM(args, i)); int idarg = pythonToJs(PyTuple_GET_ITEM(args, i));
hiwire_push_array(jsargs, jsarg); hiwire_push_array(idargs, idarg);
hiwire_decref(jsarg); hiwire_decref(idarg);
} }
int jsresult = hiwire_call(self->js, jsargs); int idresult = hiwire_call(self->js, idargs);
hiwire_decref(jsargs); hiwire_decref(idargs);
PyObject *result = jsToPython(jsresult); PyObject *pyresult = jsToPython(idresult);
hiwire_decref(jsresult); hiwire_decref(idresult);
return result; return pyresult;
} }
static PyObject* JsProxy_New(PyObject *o, PyObject *args, PyObject *kwargs) { static PyObject* JsProxy_New(PyObject *o, PyObject *args, PyObject *kwargs) {
...@@ -97,41 +97,41 @@ static PyObject* JsProxy_New(PyObject *o, PyObject *args, PyObject *kwargs) { ...@@ -97,41 +97,41 @@ static PyObject* JsProxy_New(PyObject *o, PyObject *args, PyObject *kwargs) {
Py_ssize_t nargs = PyTuple_Size(args); Py_ssize_t nargs = PyTuple_Size(args);
int jsargs = hiwire_array(); int idargs = hiwire_array();
for (Py_ssize_t i; i < nargs; ++i) { for (Py_ssize_t i; i < nargs; ++i) {
int jsarg = pythonToJs(PyTuple_GET_ITEM(args, i)); int idarg = pythonToJs(PyTuple_GET_ITEM(args, i));
hiwire_push_array(jsargs, jsarg); hiwire_push_array(idargs, idarg);
hiwire_decref(jsarg); hiwire_decref(idarg);
} }
int jsresult = hiwire_new(self->js, jsargs); int idresult = hiwire_new(self->js, idargs);
hiwire_decref(jsargs); hiwire_decref(idargs);
PyObject *result = jsToPython(jsresult); PyObject *pyresult = jsToPython(idresult);
hiwire_decref(jsresult); hiwire_decref(idresult);
return result; return pyresult;
} }
Py_ssize_t JsProxy_length(PyObject *o) { Py_ssize_t JsProxy_length(PyObject *o) {
JsProxy *self = (JsProxy *)o; JsProxy *self = (JsProxy *)o;
return hiwire_length(self->js); return hiwire_get_length(self->js);
} }
PyObject* JsProxy_item(PyObject *o, Py_ssize_t idx) { PyObject* JsProxy_item(PyObject *o, Py_ssize_t idx) {
JsProxy *self = (JsProxy *)o; JsProxy *self = (JsProxy *)o;
int v = hiwire_get_member_int(self->js, idx); int idresult = hiwire_get_member_int(self->js, idx);
PyObject *result = jsToPython(v); PyObject *pyresult = jsToPython(idresult);
hiwire_decref(v); hiwire_decref(idresult);
return result; return pyresult;
} }
int JsProxy_ass_item(PyObject *o, Py_ssize_t idx, PyObject *value) { int JsProxy_ass_item(PyObject *o, Py_ssize_t idx, PyObject *value) {
JsProxy *self = (JsProxy *)o; JsProxy *self = (JsProxy *)o;
int js_value = pythonToJs(value); int idvalue = pythonToJs(value);
hiwire_set_member_int(self->js, idx, js_value); hiwire_set_member_int(self->js, idx, idvalue);
hiwire_decref(js_value); hiwire_decref(idvalue);
return 0; return 0;
} }
...@@ -167,10 +167,10 @@ static PyTypeObject JsProxyType = { ...@@ -167,10 +167,10 @@ static PyTypeObject JsProxyType = {
.tp_repr = JsProxy_Repr .tp_repr = JsProxy_Repr
}; };
PyObject *JsProxy_cnew(int v) { PyObject *JsProxy_cnew(int idobj) {
JsProxy *self; JsProxy *self;
self = (JsProxy *)JsProxyType.tp_alloc(&JsProxyType, 0); self = (JsProxy *)JsProxyType.tp_alloc(&JsProxyType, 0);
self->js = hiwire_incref(v); self->js = hiwire_incref(idobj);
return (PyObject *)self; return (PyObject *)self;
} }
...@@ -197,19 +197,19 @@ static PyObject* JsBoundMethod_Call(PyObject *o, PyObject *args, PyObject *kwarg ...@@ -197,19 +197,19 @@ static PyObject* JsBoundMethod_Call(PyObject *o, PyObject *args, PyObject *kwarg
Py_ssize_t nargs = PyTuple_Size(args); Py_ssize_t nargs = PyTuple_Size(args);
int jsargs = hiwire_array(); int idargs = hiwire_array();
for (Py_ssize_t i = 0; i < nargs; ++i) { for (Py_ssize_t i = 0; i < nargs; ++i) {
int jsarg = pythonToJs(PyTuple_GET_ITEM(args, i)); int idarg = pythonToJs(PyTuple_GET_ITEM(args, i));
hiwire_push_array(jsargs, jsarg); hiwire_push_array(idargs, idarg);
hiwire_decref(jsarg); hiwire_decref(idarg);
} }
int jsresult = hiwire_call_member(self->this_, (int)self->name, jsargs); int idresult = hiwire_call_member(self->this_, (int)self->name, idargs);
hiwire_decref(jsargs); hiwire_decref(idargs);
PyObject *result = jsToPython(jsresult); PyObject *pyresult = jsToPython(idresult);
hiwire_decref(jsresult); hiwire_decref(idresult);
return result; return pyresult;
} }
static PyTypeObject JsBoundMethodType = { static PyTypeObject JsBoundMethodType = {
......
...@@ -16,7 +16,9 @@ int pyimport(char *name) { ...@@ -16,7 +16,9 @@ int pyimport(char *name) {
} }
Py_DECREF(pyname); Py_DECREF(pyname);
return pythonToJs(pyval); int idval = pythonToJs(pyval);
Py_DECREF(pyval);
return idval;
} }
EM_JS(int, pyimport_Ready, (), { EM_JS(int, pyimport_Ready, (), {
......
...@@ -5,49 +5,49 @@ ...@@ -5,49 +5,49 @@
#include "js2python.h" #include "js2python.h"
#include "python2js.h" #include "python2js.h"
int pyproxy_has(int obj, int idx) { int pyproxy_has(int ptrobj, int idkey) {
PyObject *x = (PyObject *)obj; PyObject *pyobj = (PyObject *)ptrobj;
PyObject *pyidx = jsToPython(idx); PyObject *pykey = jsToPython(idkey);
int result = PyObject_HasAttr(x, pyidx) ? hiwire_true(): hiwire_false(); int result = PyObject_HasAttr(pyobj, pykey) ? hiwire_true(): hiwire_false();
Py_DECREF(pyidx); Py_DECREF(pykey);
return result; return result;
} }
int pyproxy_get(int obj, int idx) { int pyproxy_get(int ptrobj, int idkey) {
PyObject *x = (PyObject *)obj; PyObject *pyobj = (PyObject *)ptrobj;
PyObject *pyidx = jsToPython(idx); PyObject *pykey = jsToPython(idkey);
PyObject *attr = PyObject_GetAttr(x, pyidx); PyObject *pyattr = PyObject_GetAttr(pyobj, pykey);
Py_DECREF(pyidx); Py_DECREF(pykey);
if (attr == NULL) { if (pyattr == NULL) {
PyErr_Clear(); PyErr_Clear();
return hiwire_undefined(); return hiwire_undefined();
} }
int ret = pythonToJs(attr); int idattr = pythonToJs(pyattr);
Py_DECREF(attr); Py_DECREF(pyattr);
return ret; return idattr;
}; };
int pyproxy_set(int obj, int idx, int value) { int pyproxy_set(int ptrobj, int idkey, int idval) {
PyObject *x = (PyObject *)obj; PyObject *pyobj = (PyObject *)ptrobj;
PyObject *pyidx = jsToPython(idx); PyObject *pykey = jsToPython(idkey);
PyObject *pyvalue = jsToPython(value); PyObject *pyval = jsToPython(idval);
int ret = PyObject_SetAttr(x, pyidx, pyvalue); int result = PyObject_SetAttr(pyobj, pykey, pyval);
Py_DECREF(pyidx); Py_DECREF(pykey);
Py_DECREF(pyvalue); Py_DECREF(pyval);
if (ret) { if (result) {
return pythonExcToJs(); return pythonExcToJs();
} }
return value; return idval;
} }
int pyproxy_deleteProperty(int obj, int idx) { int pyproxy_deleteProperty(int ptrobj, int idkey) {
PyObject *x = (PyObject *)obj; PyObject *pyobj = (PyObject *)ptrobj;
PyObject *pyidx = jsToPython(idx); PyObject *pykey = jsToPython(idkey);
int ret = PyObject_DelAttr(x, pyidx); int ret = PyObject_DelAttr(pyobj, pykey);
Py_DECREF(pyidx); Py_DECREF(pykey);
if (ret) { if (ret) {
return pythonExcToJs(); return pythonExcToJs();
...@@ -56,131 +56,132 @@ int pyproxy_deleteProperty(int obj, int idx) { ...@@ -56,131 +56,132 @@ int pyproxy_deleteProperty(int obj, int idx) {
return hiwire_undefined(); return hiwire_undefined();
} }
int pyproxy_ownKeys(int obj) { int pyproxy_ownKeys(int ptrobj) {
PyObject *x = (PyObject *)obj; PyObject *pyobj = (PyObject *)ptrobj;
PyObject *dir = PyObject_Dir(x); PyObject *pydir = PyObject_Dir(pyobj);
if (dir == NULL) {
if (pydir == NULL) {
return pythonExcToJs(); return pythonExcToJs();
} }
int result = hiwire_array(); int iddir = hiwire_array();
Py_ssize_t n = PyList_Size(dir); Py_ssize_t n = PyList_Size(pydir);
for (Py_ssize_t i = 0; i < n; ++i) { for (Py_ssize_t i = 0; i < n; ++i) {
PyObject *entry = PyList_GetItem(dir, i); PyObject *pyentry = PyList_GetItem(pydir, i);
int jsentry = pythonToJs(entry); int identry = pythonToJs(pyentry);
hiwire_push_array(result, jsentry); hiwire_push_array(iddir, identry);
hiwire_decref(jsentry); hiwire_decref(identry);
} }
Py_DECREF(dir); Py_DECREF(pydir);
return result; return iddir;
} }
int pyproxy_enumerate(int obj) { int pyproxy_enumerate(int ptrobj) {
return pyproxy_ownKeys(obj); return pyproxy_ownKeys(ptrobj);
} }
int pyproxy_apply(int obj, int args) { int pyproxy_apply(int ptrobj, int idargs) {
PyObject *x = (PyObject *)obj; PyObject *pyobj = (PyObject *)ptrobj;
Py_ssize_t length = hiwire_length(args); Py_ssize_t length = hiwire_get_length(idargs);
PyObject *pyargs = PyTuple_New(length); PyObject *pyargs = PyTuple_New(length);
for (Py_ssize_t i = 0; i < length; ++i) { for (Py_ssize_t i = 0; i < length; ++i) {
int item = hiwire_get_member_int(args, i); int iditem = hiwire_get_member_int(idargs, i);
PyObject *pyitem = jsToPython(item); PyObject *pyitem = jsToPython(iditem);
PyTuple_SET_ITEM(pyargs, i, pyitem); PyTuple_SET_ITEM(pyargs, i, pyitem);
hiwire_decref(item); hiwire_decref(iditem);
} }
PyObject *result = PyObject_Call(x, pyargs, NULL); PyObject *pyresult = PyObject_Call(pyobj, pyargs, NULL);
if (result == NULL) { if (pyresult == NULL) {
Py_DECREF(pyargs); Py_DECREF(pyargs);
return pythonExcToJs(); return pythonExcToJs();
} }
int jsresult = pythonToJs(result); int idresult = pythonToJs(pyresult);
Py_DECREF(result); Py_DECREF(pyresult);
Py_DECREF(pyargs); Py_DECREF(pyargs);
return jsresult; return idresult;
} }
EM_JS(int, pyproxy_new, (int id), { EM_JS(int, pyproxy_new, (int ptrobj), {
var target = function() {}; var target = function() {};
target['$$'] = id; target['$$'] = ptrobj;
return Module.hiwire_new_value(new Proxy(target, Module.PyProxy)); return Module.hiwire_new_value(new Proxy(target, Module.PyProxy));
}); });
EM_JS(int, PyProxy_Ready, (), { EM_JS(int, PyProxy_Ready, (), {
Module.PyProxy = { Module.PyProxy = {
isExtensible: function() { return true }, isExtensible: function() { return true },
has: function (obj, idx) { has: function (jsobj, jskey) {
obj = obj['$$']; ptrobj = jsobj['$$'];
var idxid = Module.hiwire_new_value(idx); var idkey = Module.hiwire_new_value(jskey);
var result = _pyproxy_has(obj, idxid) != 0; var result = _pyproxy_has(ptrobj, idkey) != 0;
Module.hiwire_decref(idxid); Module.hiwire_decref(idkey);
return result; return result;
}, },
get: function (obj, idx) { get: function (jsobj, jskey) {
if (idx === 'toString') { if (jskey === 'toString') {
return function() { return function() {
// TODO: Cache repr // TODO: Cache repr
var repr = pyodide.pyimport('repr'); var repr = pyodide.pyimport('repr');
return repr(obj); return repr(jsobj);
} }
} else if (idx === '$$') { } else if (jskey === '$$') {
return obj['$$']; return jsobj['$$'];
} }
obj = obj['$$']; ptrobj = jsobj['$$'];
var idxid = Module.hiwire_new_value(idx); var idkey = Module.hiwire_new_value(jskey);
var resultid = _pyproxy_get(obj, idxid); var idresult = _pyproxy_get(ptrobj, idkey);
var result = Module.hiwire_get_value(resultid); var jsresult = Module.hiwire_get_value(idresult);
Module.hiwire_decref(idxid); Module.hiwire_decref(idkey);
Module.hiwire_decref(resultid); Module.hiwire_decref(idresult);
return result; return jsresult;
}, },
set: function (obj, idx, value) { set: function (jsobj, jskey, jsval) {
obj = obj['$$']; ptrobj = jsobj['$$'];
var idxid = Module.hiwire_new_value(idx); var idkey = Module.hiwire_new_value(jskey);
var valueid = Module.hiwire_new_value(value); var idval = Module.hiwire_new_value(jsval);
var resultid = _pyproxy_set(obj, idxid, valueid); var idresult = _pyproxy_set(ptrobj, idkey, idval);
var result = Module.hiwire_get_value(resultid); var jsresult = Module.hiwire_get_value(idresult);
Module.hiwire_decref(idxid); Module.hiwire_decref(idkey);
Module.hiwire_decref(valueid); Module.hiwire_decref(idval);
Module.hiwire_decref(resultid); Module.hiwire_decref(idresult);
return result; return jsresult;
}, },
deleteProperty: function (obj, idx) { deleteProperty: function (jsobj, jskey) {
obj = obj['$$']; ptrobj = jsobj['$$'];
var idxid = Module.hiwire_new_value(idx); var idkey = Module.hiwire_new_value(jskey);
var resultid = _pyproxy_deleteProperty(obj, idxid); var idresult = _pyproxy_deleteProperty(ptrobj, idkey);
var result = Module.hiwire_get_value(resultid); var jsresult = Module.hiwire_get_value(idresult);
Module.hiwire_decref(resultid); Module.hiwire_decref(idresult);
Module.hiwire_decref(idxid); Module.hiwire_decref(idkey);
return result; return jsresult;
}, },
ownKeys: function (obj) { ownKeys: function (jsobj) {
obj = obj['$$']; ptrobj = jsobj['$$'];
var resultid = _pyproxy_ownKeys(obj); var idresult = _pyproxy_ownKeys(ptrobj);
var result = Module.hiwire_get_value(resultid); var jsresult = Module.hiwire_get_value(idresult);
Module.hiwire_decref(resultid); Module.hiwire_decref(idresult);
result.push('toString'); jsresult.push('toString');
result.push('prototype'); jsresult.push('prototype');
return result; return jsresult;
}, },
enumerate: function (obj) { enumerate: function (jsobj) {
obj = obj['$$']; ptrobj = jsobj['$$'];
var resultid = _pyproxy_enumerate(obj); var idresult = _pyproxy_enumerate(ptrobj);
var result = Module.hiwire_get_value(resultid); var jsresult = Module.hiwire_get_value(idresult);
Module.hiwire_decref(resultid); Module.hiwire_decref(idresult);
result.push('toString'); jsresult.push('toString');
result.push('prototype'); jsresult.push('prototype');
return result; return jsresult;
}, },
apply: function (obj, thisArg, args) { apply: function (jsobj, jsthis, jsargs) {
obj = obj['$$']; ptrobj = jsobj['$$'];
var argsid = Module.hiwire_new_value(args); var idargs = Module.hiwire_new_value(jsargs);
var resultid = _pyproxy_apply(obj, argsid); var idresult = _pyproxy_apply(ptrobj, idargs);
var result = Module.hiwire_get_value(resultid); var jsresult = Module.hiwire_get_value(idresult);
Module.hiwire_decref(resultid); Module.hiwire_decref(idresult);
Module.hiwire_decref(argsid); Module.hiwire_decref(idargs);
return result; return jsresult;
}, },
}; };
......
...@@ -142,8 +142,8 @@ int pythonToJs(PyObject *x) { ...@@ -142,8 +142,8 @@ int pythonToJs(PyObject *x) {
int jsarray = hiwire_array(); int jsarray = hiwire_array();
size_t length = PySequence_Size(x); size_t length = PySequence_Size(x);
for (size_t i = 0; i < length; ++i) { for (size_t i = 0; i < length; ++i) {
PyObject *item = PySequence_GetItem(x, i); PyObject *pyitem = PySequence_GetItem(x, i);
if (item == NULL) { if (pyitem == NULL) {
// If something goes wrong converting the sequence (as is the case with // If something goes wrong converting the sequence (as is the case with
// Pandas data frames), fallback to the Python object proxy // Pandas data frames), fallback to the Python object proxy
hiwire_decref(jsarray); hiwire_decref(jsarray);
...@@ -151,35 +151,38 @@ int pythonToJs(PyObject *x) { ...@@ -151,35 +151,38 @@ int pythonToJs(PyObject *x) {
Py_INCREF(x); Py_INCREF(x);
return pyproxy_new((int)x); return pyproxy_new((int)x);
} }
int jsitem = pythonToJs(item); int jsitem = pythonToJs(pyitem);
if (jsitem == -1) { if (jsitem == -1) {
Py_DECREF(item); Py_DECREF(pyitem);
hiwire_decref(jsarray); hiwire_decref(jsarray);
return pythonExcToJs(); return pythonExcToJs();
} }
Py_DECREF(item); Py_DECREF(pyitem);
hiwire_push_array(jsarray, jsitem); hiwire_push_array(jsarray, jsitem);
hiwire_decref(jsitem);
} }
return jsarray; return jsarray;
} else if (PyDict_Check(x)) { } else if (PyDict_Check(x)) {
int jsdict = hiwire_object(); int jsdict = hiwire_object();
PyObject *k, *v; PyObject *pykey, *pyval;
Py_ssize_t pos = 0; Py_ssize_t pos = 0;
while (PyDict_Next(x, &pos, &k, &v)) { while (PyDict_Next(x, &pos, &pykey, &pyval)) {
int jsk = pythonToJs(k); int jskey = pythonToJs(pykey);
if (jsk == -1) { if (jskey == -1) {
hiwire_decref(jsdict); hiwire_decref(jsdict);
// TODO: Return a proxy instead here??? // TODO: Return a proxy instead here???
return pythonExcToJs(); return pythonExcToJs();
} }
int jsv = pythonToJs(v); int jsval = pythonToJs(pyval);
if (jsv == -1) { if (jsval == -1) {
// TODO: Return a proxy instead here??? // TODO: Return a proxy instead here???
hiwire_decref(jsk); hiwire_decref(jskey);
hiwire_decref(jsdict); hiwire_decref(jsdict);
return pythonExcToJs(); return pythonExcToJs();
} }
hiwire_push_object_pair(jsdict, jsk, jsv); hiwire_push_object_pair(jsdict, jskey, jsval);
hiwire_decref(jskey);
hiwire_decref(jsval);
} }
return jsdict; return jsdict;
} else { } else {
......
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