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

Use variable naming convention to make types clearer

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