Commit 3831cf86 authored by Michael Droettboom's avatar Michael Droettboom

Consistent capitalization

parent c1e75744
...@@ -8,76 +8,76 @@ ...@@ -8,76 +8,76 @@
// Since we're going *to* Python, just let any Python exceptions at conversion // Since we're going *to* Python, just let any Python exceptions at conversion
// bubble out to Python // bubble out to Python
int _jsStringToPython(char *val) { int _js2python_string(char *val) {
return (int)PyUnicode_FromString(val); return (int)PyUnicode_FromString(val);
} }
int _jsNumberToPython(double val) { int _js2python_number(double val) {
return (int)PyFloat_FromDouble(val); return (int)PyFloat_FromDouble(val);
} }
int _pythonNone() { int _js2python_none() {
Py_INCREF(Py_None); Py_INCREF(Py_None);
return (int)Py_None; return (int)Py_None;
} }
int _pythonTrue() { int _js2python_true() {
Py_INCREF(Py_True); Py_INCREF(Py_True);
return (int)Py_True; return (int)Py_True;
} }
int _pythonFalse() { int _js2python_false() {
Py_INCREF(Py_False); Py_INCREF(Py_False);
return (int)Py_False; return (int)Py_False;
} }
int _jsPyProxyToPython(PyObject *val) { int _js2python_pyproxy(PyObject *val) {
Py_INCREF(val); Py_INCREF(val);
return (int)val; return (int)val;
} }
int _jsBytesToPython(char *bytes, int length) { int _js2python_bytes(char *bytes, int length) {
return (int)PyBytes_FromStringAndSize(bytes, length); return (int)PyBytes_FromStringAndSize(bytes, length);
} }
int _jsProxyToPython(int id) { int _js2python_jsproxy(int id) {
return (int)JsProxy_cnew(id); return (int)JsProxy_cnew(id);
} }
// TODO: Add some meaningful order // TODO: Add some meaningful order
EM_JS(int, __jsToPython, (int id), { EM_JS(int, __js2python, (int id), {
var value = Module.hiwire_get_value(id); var value = Module.hiwire_get_value(id);
var type = typeof value; var type = typeof value;
if (type === 'string') { if (type === 'string') {
var charptr = allocate(intArrayFromString(value), 'i8', ALLOC_NORMAL); var charptr = allocate(intArrayFromString(value), 'i8', ALLOC_NORMAL);
var result = __jsStringToPython(charptr); var result = __js2python_string(charptr);
_free(charptr); _free(charptr);
return result; return result;
} else if (type === 'number') { } else if (type === 'number') {
return __jsNumberToPython(value); return __js2python_number(value);
} else if (value === undefined || value === null) { } else if (value === undefined || value === null) {
return __pythonNone(); return __js2python_none();
} else if (value === true) { } else if (value === true) {
return __pythonTrue(); return __js2python_true();
} else if (value === false) { } else if (value === false) {
return __pythonFalse(); return __js2python_false();
} else if (Module.PyProxy.isPyProxy(value)) { } else if (Module.PyProxy.isPyProxy(value)) {
return __jsPyProxyToPython(Module.PyProxy.getPtr(value)); return __js2python_pyproxy(Module.PyProxy.getPtr(value));
} else if (value['byteLength'] !== undefined) { } else if (value['byteLength'] !== undefined) {
var bytes = allocate(value, 'i8', ALLOC_NORMAL); var bytes = allocate(value, 'i8', ALLOC_NORMAL);
var result = __jsBytesToPython(bytes, value['byteLength']); var result = __js2python_bytes(bytes, value['byteLength']);
_free(bytes); _free(bytes);
return result; return result;
} else { } else {
return __jsProxyToPython(id); return __js2python_jsproxy(id);
} }
}); });
PyObject *jsToPython(int id) { PyObject *js2python(int id) {
return (PyObject *)__jsToPython(id); return (PyObject *)__js2python(id);
} }
int jsToPython_Ready() { int js2python_init() {
return 0; return 0;
} }
...@@ -13,9 +13,9 @@ ...@@ -13,9 +13,9 @@
* occurred during the conversion, and the Python exception API should be used * occurred during the conversion, and the Python exception API should be used
* to obtain the exception. * to obtain the exception.
*/ */
PyObject *jsToPython(int x); PyObject *js2python(int x);
/** Initialize any global variables used by this module. */ /** Initialize any global variables used by this module. */
int jsToPython_Ready(); int js2python_init();
#endif /* JS2PYTHON_H */ #endif /* JS2PYTHON_H */
...@@ -46,7 +46,7 @@ static PyObject *JsImport_Call(PyObject *self, PyObject *args, PyObject *kwargs) ...@@ -46,7 +46,7 @@ static PyObject *JsImport_Call(PyObject *self, PyObject *args, PyObject *kwargs)
return NULL; return NULL;
} }
int jsval = hiwire_get_global((int)c); int jsval = hiwire_get_global((int)c);
PyObject *pyval = jsToPython(jsval); PyObject *pyval = js2python(jsval);
hiwire_decref(jsval); hiwire_decref(jsval);
if (PyDict_SetItem(d, key, pyval)) { if (PyDict_SetItem(d, key, pyval)) {
Py_DECREF(key); Py_DECREF(key);
...@@ -79,7 +79,7 @@ static PyObject *JsImport_New() { ...@@ -79,7 +79,7 @@ static PyObject *JsImport_New() {
return (PyObject *)self; return (PyObject *)self;
} }
int JsImport_Ready() { int JsImport_init() {
if (PyType_Ready(&JsImportType)) { if (PyType_Ready(&JsImportType)) {
return 1; return 1;
} }
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
#include <Python.h> #include <Python.h>
/** Install the import hook to support "from js import …". */ /** Install the import hook to support "from js import …". */
int JsImport_Ready(); int JsImport_init();
extern PyObject *globals; extern PyObject *globals;
......
...@@ -24,7 +24,7 @@ static void JsProxy_dealloc(JsProxy *self) { ...@@ -24,7 +24,7 @@ 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 idrepr = hiwire_to_string(self->js); int idrepr = hiwire_to_string(self->js);
PyObject *pyrepr = jsToPython(idrepr); PyObject *pyrepr = js2python(idrepr);
return pyrepr; return pyrepr;
} }
...@@ -51,7 +51,7 @@ static PyObject *JsProxy_GetAttr(PyObject *o, PyObject *attr_name) { ...@@ -51,7 +51,7 @@ static PyObject *JsProxy_GetAttr(PyObject *o, PyObject *attr_name) {
return JsBoundMethod_cnew(self->js, key); return JsBoundMethod_cnew(self->js, key);
} }
PyObject *pyresult = jsToPython(idresult); PyObject *pyresult = js2python(idresult);
hiwire_decref(idresult); hiwire_decref(idresult);
return pyresult; return pyresult;
} }
...@@ -64,7 +64,7 @@ static int JsProxy_SetAttr(PyObject *o, PyObject *attr_name, PyObject *pyvalue) ...@@ -64,7 +64,7 @@ static int JsProxy_SetAttr(PyObject *o, PyObject *attr_name, PyObject *pyvalue)
return -1; return -1;
} }
char *key = PyUnicode_AsUTF8(attr_name_py_str); char *key = PyUnicode_AsUTF8(attr_name_py_str);
int idvalue = pythonToJs(pyvalue); int idvalue = python2js(pyvalue);
hiwire_set_member_string(self->js, (int)key, idvalue); hiwire_set_member_string(self->js, (int)key, idvalue);
hiwire_decref(idvalue); hiwire_decref(idvalue);
Py_DECREF(attr_name_py_str); Py_DECREF(attr_name_py_str);
...@@ -80,14 +80,14 @@ static PyObject* JsProxy_Call(PyObject *o, PyObject *args, PyObject *kwargs) { ...@@ -80,14 +80,14 @@ static PyObject* JsProxy_Call(PyObject *o, PyObject *args, PyObject *kwargs) {
int idargs = hiwire_array(); int idargs = hiwire_array();
for (Py_ssize_t i; i < nargs; ++i) { for (Py_ssize_t i; i < nargs; ++i) {
int idarg = pythonToJs(PyTuple_GET_ITEM(args, i)); int idarg = python2js(PyTuple_GET_ITEM(args, i));
hiwire_push_array(idargs, idarg); hiwire_push_array(idargs, idarg);
hiwire_decref(idarg); hiwire_decref(idarg);
} }
int idresult = hiwire_call(self->js, idargs); int idresult = hiwire_call(self->js, idargs);
hiwire_decref(idargs); hiwire_decref(idargs);
PyObject *pyresult = jsToPython(idresult); PyObject *pyresult = js2python(idresult);
hiwire_decref(idresult); hiwire_decref(idresult);
return pyresult; return pyresult;
} }
...@@ -100,14 +100,14 @@ static PyObject* JsProxy_New(PyObject *o, PyObject *args, PyObject *kwargs) { ...@@ -100,14 +100,14 @@ static PyObject* JsProxy_New(PyObject *o, PyObject *args, PyObject *kwargs) {
int idargs = hiwire_array(); int idargs = hiwire_array();
for (Py_ssize_t i; i < nargs; ++i) { for (Py_ssize_t i; i < nargs; ++i) {
int idarg = pythonToJs(PyTuple_GET_ITEM(args, i)); int idarg = python2js(PyTuple_GET_ITEM(args, i));
hiwire_push_array(idargs, idarg); hiwire_push_array(idargs, idarg);
hiwire_decref(idarg); hiwire_decref(idarg);
} }
int idresult = hiwire_new(self->js, idargs); int idresult = hiwire_new(self->js, idargs);
hiwire_decref(idargs); hiwire_decref(idargs);
PyObject *pyresult = jsToPython(idresult); PyObject *pyresult = js2python(idresult);
hiwire_decref(idresult); hiwire_decref(idresult);
return pyresult; return pyresult;
} }
...@@ -122,14 +122,14 @@ PyObject* JsProxy_item(PyObject *o, Py_ssize_t idx) { ...@@ -122,14 +122,14 @@ PyObject* JsProxy_item(PyObject *o, Py_ssize_t idx) {
JsProxy *self = (JsProxy *)o; JsProxy *self = (JsProxy *)o;
int idresult = hiwire_get_member_int(self->js, idx); int idresult = hiwire_get_member_int(self->js, idx);
PyObject *pyresult = jsToPython(idresult); PyObject *pyresult = js2python(idresult);
hiwire_decref(idresult); hiwire_decref(idresult);
return pyresult; 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 idvalue = pythonToJs(value); int idvalue = python2js(value);
hiwire_set_member_int(self->js, idx, idvalue); hiwire_set_member_int(self->js, idx, idvalue);
hiwire_decref(idvalue); hiwire_decref(idvalue);
return 0; return 0;
...@@ -200,14 +200,14 @@ static PyObject* JsBoundMethod_Call(PyObject *o, PyObject *args, PyObject *kwarg ...@@ -200,14 +200,14 @@ static PyObject* JsBoundMethod_Call(PyObject *o, PyObject *args, PyObject *kwarg
int idargs = 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 idarg = pythonToJs(PyTuple_GET_ITEM(args, i)); int idarg = python2js(PyTuple_GET_ITEM(args, i));
hiwire_push_array(idargs, idarg); hiwire_push_array(idargs, idarg);
hiwire_decref(idarg); hiwire_decref(idarg);
} }
int idresult = hiwire_call_member(self->this_, (int)self->name, idargs); int idresult = hiwire_call_member(self->this_, (int)self->name, idargs);
hiwire_decref(idargs); hiwire_decref(idargs);
PyObject *pyresult = jsToPython(idresult); PyObject *pyresult = js2python(idresult);
hiwire_decref(idresult); hiwire_decref(idresult);
return pyresult; return pyresult;
} }
...@@ -242,7 +242,7 @@ int JsProxy_AsJs(PyObject *x) { ...@@ -242,7 +242,7 @@ int JsProxy_AsJs(PyObject *x) {
return hiwire_incref(js_proxy->js); return hiwire_incref(js_proxy->js);
} }
int JsProxy_Ready() { int JsProxy_init() {
return (PyType_Ready(&JsProxyType) || return (PyType_Ready(&JsProxyType) ||
PyType_Ready(&JsBoundMethodType)); PyType_Ready(&JsBoundMethodType));
} }
...@@ -26,6 +26,6 @@ int JsProxy_Check(PyObject *x); ...@@ -26,6 +26,6 @@ int JsProxy_Check(PyObject *x);
int JsProxy_AsJs(PyObject *x); int JsProxy_AsJs(PyObject *x);
/** Initialize global state for the JsProxy functionality. */ /** Initialize global state for the JsProxy functionality. */
int JsProxy_Ready(); int JsProxy_init();
#endif /* JSPROXY_H */ #endif /* JSPROXY_H */
...@@ -10,16 +10,6 @@ ...@@ -10,16 +10,6 @@
#include "python2js.h" #include "python2js.h"
#include "runpython.h" #include "runpython.h"
// TODO: Use static functions where appropriate
////////////////////////////////////////////////////////////
// Forward declarations
////////////////////////////////////////////////////////////
// Conversions
/* /*
TODO: This is a workaround for a weird emscripten compiler bug. The TODO: This is a workaround for a weird emscripten compiler bug. The
matplotlib/_qhull.so extension makes function pointer calls with these matplotlib/_qhull.so extension makes function pointer calls with these
...@@ -53,15 +43,17 @@ int main(int argc, char** argv) { ...@@ -53,15 +43,17 @@ int main(int argc, char** argv) {
Py_InitializeEx(0); Py_InitializeEx(0);
// cleanup naming of these functions // TODO cleanup naming of these functions
if (JsProxy_Ready() || if (
jsToPython_Ready() || js2python_init() ||
pythonToJs_Ready() || JsImport_init() ||
JsImport_Ready() || JsProxy_init() ||
runPython_Ready() || pyimport_init() ||
pyimport_Ready() || pyproxy_init() ||
PyProxy_Ready()) { python2js_init() ||
runpython_init()
) {
return 1; return 1;
} }
......
...@@ -7,24 +7,24 @@ ...@@ -7,24 +7,24 @@
extern PyObject *globals; extern PyObject *globals;
int pyimport(char *name) { int _pyimport(char *name) {
PyObject *pyname = PyUnicode_FromString(name); PyObject *pyname = PyUnicode_FromString(name);
PyObject *pyval = PyDict_GetItem(globals, pyname); PyObject *pyval = PyDict_GetItem(globals, pyname);
if (pyval == NULL) { if (pyval == NULL) {
Py_DECREF(pyname); Py_DECREF(pyname);
return pythonExcToJs(); return pythonexc2js();
} }
Py_DECREF(pyname); Py_DECREF(pyname);
int idval = pythonToJs(pyval); int idval = python2js(pyval);
Py_DECREF(pyval); Py_DECREF(pyval);
return idval; return idval;
} }
EM_JS(int, pyimport_Ready, (), { EM_JS(int, pyimport_init, (), {
Module.pyimport = function(name) { Module.pyimport = function(name) {
var pyname = allocate(intArrayFromString(name), 'i8', ALLOC_NORMAL); var pyname = allocate(intArrayFromString(name), 'i8', ALLOC_NORMAL);
var idresult = Module._pyimport(pyname); var idresult = Module.__pyimport(pyname);
jsresult = Module.hiwire_get_value(idresult); jsresult = Module.hiwire_get_value(idresult);
Module.hiwire_decref(idresult); Module.hiwire_decref(idresult);
_free(pyname); _free(pyname);
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
/** Makes `var foo = pyodide.pyimport('foo')` work in Javascript. /** Makes `var foo = pyodide.pyimport('foo')` work in Javascript.
*/ */
int pyimport(char *name); int pyimport_init();
int pyimport_Ready();
#endif /* PYIMPORT_H */ #endif /* PYIMPORT_H */
...@@ -5,17 +5,17 @@ ...@@ -5,17 +5,17 @@
#include "js2python.h" #include "js2python.h"
#include "python2js.h" #include "python2js.h"
int pyproxy_has(int ptrobj, int idkey) { int _pyproxy_has(int ptrobj, int idkey) {
PyObject *pyobj = (PyObject *)ptrobj; PyObject *pyobj = (PyObject *)ptrobj;
PyObject *pykey = jsToPython(idkey); PyObject *pykey = js2python(idkey);
int result = PyObject_HasAttr(pyobj, pykey) ? hiwire_true(): hiwire_false(); int result = PyObject_HasAttr(pyobj, pykey) ? hiwire_true(): hiwire_false();
Py_DECREF(pykey); Py_DECREF(pykey);
return result; return result;
} }
int pyproxy_get(int ptrobj, int idkey) { int _pyproxy_get(int ptrobj, int idkey) {
PyObject *pyobj = (PyObject *)ptrobj; PyObject *pyobj = (PyObject *)ptrobj;
PyObject *pykey = jsToPython(idkey); PyObject *pykey = js2python(idkey);
PyObject *pyattr = PyObject_GetAttr(pyobj, pykey); PyObject *pyattr = PyObject_GetAttr(pyobj, pykey);
Py_DECREF(pykey); Py_DECREF(pykey);
if (pyattr == NULL) { if (pyattr == NULL) {
...@@ -23,52 +23,52 @@ int pyproxy_get(int ptrobj, int idkey) { ...@@ -23,52 +23,52 @@ int pyproxy_get(int ptrobj, int idkey) {
return hiwire_undefined(); return hiwire_undefined();
} }
int idattr = pythonToJs(pyattr); int idattr = python2js(pyattr);
Py_DECREF(pyattr); Py_DECREF(pyattr);
return idattr; return idattr;
}; };
int pyproxy_set(int ptrobj, int idkey, int idval) { int _pyproxy_set(int ptrobj, int idkey, int idval) {
PyObject *pyobj = (PyObject *)ptrobj; PyObject *pyobj = (PyObject *)ptrobj;
PyObject *pykey = jsToPython(idkey); PyObject *pykey = js2python(idkey);
PyObject *pyval = jsToPython(idval); PyObject *pyval = js2python(idval);
int result = PyObject_SetAttr(pyobj, pykey, pyval); int result = PyObject_SetAttr(pyobj, pykey, pyval);
Py_DECREF(pykey); Py_DECREF(pykey);
Py_DECREF(pyval); Py_DECREF(pyval);
if (result) { if (result) {
return pythonExcToJs(); return pythonexc2js();
} }
return idval; return idval;
} }
int pyproxy_deleteProperty(int ptrobj, int idkey) { int _pyproxy_deleteProperty(int ptrobj, int idkey) {
PyObject *pyobj = (PyObject *)ptrobj; PyObject *pyobj = (PyObject *)ptrobj;
PyObject *pykey = jsToPython(idkey); PyObject *pykey = js2python(idkey);
int ret = PyObject_DelAttr(pyobj, pykey); int ret = PyObject_DelAttr(pyobj, pykey);
Py_DECREF(pykey); Py_DECREF(pykey);
if (ret) { if (ret) {
return pythonExcToJs(); return pythonexc2js();
} }
return hiwire_undefined(); return hiwire_undefined();
} }
int pyproxy_ownKeys(int ptrobj) { int _pyproxy_ownKeys(int ptrobj) {
PyObject *pyobj = (PyObject *)ptrobj; PyObject *pyobj = (PyObject *)ptrobj;
PyObject *pydir = PyObject_Dir(pyobj); PyObject *pydir = PyObject_Dir(pyobj);
if (pydir == NULL) { if (pydir == NULL) {
return pythonExcToJs(); return pythonexc2js();
} }
int iddir = hiwire_array(); int iddir = hiwire_array();
Py_ssize_t n = PyList_Size(pydir); 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 *pyentry = PyList_GetItem(pydir, i); PyObject *pyentry = PyList_GetItem(pydir, i);
int identry = pythonToJs(pyentry); int identry = python2js(pyentry);
hiwire_push_array(iddir, identry); hiwire_push_array(iddir, identry);
hiwire_decref(identry); hiwire_decref(identry);
} }
...@@ -77,26 +77,26 @@ int pyproxy_ownKeys(int ptrobj) { ...@@ -77,26 +77,26 @@ int pyproxy_ownKeys(int ptrobj) {
return iddir; return iddir;
} }
int pyproxy_enumerate(int ptrobj) { int _pyproxy_enumerate(int ptrobj) {
return pyproxy_ownKeys(ptrobj); return _pyproxy_ownKeys(ptrobj);
} }
int pyproxy_apply(int ptrobj, int idargs) { int _pyproxy_apply(int ptrobj, int idargs) {
PyObject *pyobj = (PyObject *)ptrobj; PyObject *pyobj = (PyObject *)ptrobj;
Py_ssize_t length = hiwire_get_length(idargs); 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 iditem = hiwire_get_member_int(idargs, i); int iditem = hiwire_get_member_int(idargs, i);
PyObject *pyitem = jsToPython(iditem); PyObject *pyitem = js2python(iditem);
PyTuple_SET_ITEM(pyargs, i, pyitem); PyTuple_SET_ITEM(pyargs, i, pyitem);
hiwire_decref(iditem); hiwire_decref(iditem);
} }
PyObject *pyresult = PyObject_Call(pyobj, pyargs, NULL); PyObject *pyresult = PyObject_Call(pyobj, pyargs, NULL);
if (pyresult == NULL) { if (pyresult == NULL) {
Py_DECREF(pyargs); Py_DECREF(pyargs);
return pythonExcToJs(); return pythonexc2js();
} }
int idresult = pythonToJs(pyresult); int idresult = python2js(pyresult);
Py_DECREF(pyresult); Py_DECREF(pyresult);
Py_DECREF(pyargs); Py_DECREF(pyargs);
return idresult; return idresult;
...@@ -108,7 +108,7 @@ EM_JS(int, pyproxy_new, (int ptrobj), { ...@@ -108,7 +108,7 @@ EM_JS(int, pyproxy_new, (int 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_init, (), {
Module.PyProxy = { Module.PyProxy = {
getPtr: function(jsobj) { getPtr: function(jsobj) {
return jsobj['$$']['ptr']; return jsobj['$$']['ptr'];
...@@ -120,7 +120,7 @@ EM_JS(int, PyProxy_Ready, (), { ...@@ -120,7 +120,7 @@ EM_JS(int, PyProxy_Ready, (), {
has: function (jsobj, jskey) { has: function (jsobj, jskey) {
ptrobj = this.getPtr(jsobj); ptrobj = this.getPtr(jsobj);
var idkey = Module.hiwire_new_value(jskey); var idkey = Module.hiwire_new_value(jskey);
var result = _pyproxy_has(ptrobj, idkey) != 0; var result = __pyproxy_has(ptrobj, idkey) != 0;
Module.hiwire_decref(idkey); Module.hiwire_decref(idkey);
return result; return result;
}, },
...@@ -136,7 +136,7 @@ EM_JS(int, PyProxy_Ready, (), { ...@@ -136,7 +136,7 @@ EM_JS(int, PyProxy_Ready, (), {
} }
ptrobj = this.getPtr(jsobj); ptrobj = this.getPtr(jsobj);
var idkey = Module.hiwire_new_value(jskey); var idkey = Module.hiwire_new_value(jskey);
var idresult = _pyproxy_get(ptrobj, idkey); var idresult = __pyproxy_get(ptrobj, idkey);
var jsresult = Module.hiwire_get_value(idresult); var jsresult = Module.hiwire_get_value(idresult);
Module.hiwire_decref(idkey); Module.hiwire_decref(idkey);
Module.hiwire_decref(idresult); Module.hiwire_decref(idresult);
...@@ -146,7 +146,7 @@ EM_JS(int, PyProxy_Ready, (), { ...@@ -146,7 +146,7 @@ EM_JS(int, PyProxy_Ready, (), {
ptrobj = this.getPtr(jsobj); ptrobj = this.getPtr(jsobj);
var idkey = Module.hiwire_new_value(jskey); var idkey = Module.hiwire_new_value(jskey);
var idval = Module.hiwire_new_value(jsval); var idval = Module.hiwire_new_value(jsval);
var idresult = _pyproxy_set(ptrobj, idkey, idval); var idresult = __pyproxy_set(ptrobj, idkey, idval);
var jsresult = Module.hiwire_get_value(idresult); var jsresult = Module.hiwire_get_value(idresult);
Module.hiwire_decref(idkey); Module.hiwire_decref(idkey);
Module.hiwire_decref(idval); Module.hiwire_decref(idval);
...@@ -156,7 +156,7 @@ EM_JS(int, PyProxy_Ready, (), { ...@@ -156,7 +156,7 @@ EM_JS(int, PyProxy_Ready, (), {
deleteProperty: function (jsobj, jskey) { deleteProperty: function (jsobj, jskey) {
ptrobj = this.getPtr(jsobj); ptrobj = this.getPtr(jsobj);
var idkey = Module.hiwire_new_value(jskey); var idkey = Module.hiwire_new_value(jskey);
var idresult = _pyproxy_deleteProperty(ptrobj, idkey); var idresult = __pyproxy_deleteProperty(ptrobj, idkey);
var jsresult = Module.hiwire_get_value(idresult); var jsresult = Module.hiwire_get_value(idresult);
Module.hiwire_decref(idresult); Module.hiwire_decref(idresult);
Module.hiwire_decref(idkey); Module.hiwire_decref(idkey);
...@@ -164,7 +164,7 @@ EM_JS(int, PyProxy_Ready, (), { ...@@ -164,7 +164,7 @@ EM_JS(int, PyProxy_Ready, (), {
}, },
ownKeys: function (jsobj) { ownKeys: function (jsobj) {
ptrobj = this.getPtr(jsobj); ptrobj = this.getPtr(jsobj);
var idresult = _pyproxy_ownKeys(ptrobj); var idresult = __pyproxy_ownKeys(ptrobj);
var jsresult = Module.hiwire_get_value(idresult); var jsresult = Module.hiwire_get_value(idresult);
Module.hiwire_decref(idresult); Module.hiwire_decref(idresult);
jsresult.push('toString'); jsresult.push('toString');
...@@ -173,7 +173,7 @@ EM_JS(int, PyProxy_Ready, (), { ...@@ -173,7 +173,7 @@ EM_JS(int, PyProxy_Ready, (), {
}, },
enumerate: function (jsobj) { enumerate: function (jsobj) {
ptrobj = this.getPtr(jsobj); ptrobj = this.getPtr(jsobj);
var idresult = _pyproxy_enumerate(ptrobj); var idresult = __pyproxy_enumerate(ptrobj);
var jsresult = Module.hiwire_get_value(idresult); var jsresult = Module.hiwire_get_value(idresult);
Module.hiwire_decref(idresult); Module.hiwire_decref(idresult);
jsresult.push('toString'); jsresult.push('toString');
...@@ -183,7 +183,7 @@ EM_JS(int, PyProxy_Ready, (), { ...@@ -183,7 +183,7 @@ EM_JS(int, PyProxy_Ready, (), {
apply: function (jsobj, jsthis, jsargs) { apply: function (jsobj, jsthis, jsargs) {
ptrobj = this.getPtr(jsobj); ptrobj = this.getPtr(jsobj);
var idargs = Module.hiwire_new_value(jsargs); var idargs = Module.hiwire_new_value(jsargs);
var idresult = _pyproxy_apply(ptrobj, idargs); var idresult = __pyproxy_apply(ptrobj, idargs);
var jsresult = Module.hiwire_get_value(idresult); var jsresult = Module.hiwire_get_value(idresult);
Module.hiwire_decref(idresult); Module.hiwire_decref(idresult);
Module.hiwire_decref(idargs); Module.hiwire_decref(idargs);
......
...@@ -8,6 +8,6 @@ ...@@ -8,6 +8,6 @@
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
int pyproxy_new(int obj); int pyproxy_new(int obj);
int PyProxy_Ready(); int pyproxy_init();
#endif /* PYPROXY_H */ #endif /* PYPROXY_H */
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
#include "jsproxy.h" #include "jsproxy.h"
#include "pyproxy.h" #include "pyproxy.h"
int pythonExcToJs() { int pythonexc2js() {
PyObject *type; PyObject *type;
PyObject *value; PyObject *value;
PyObject *traceback; PyObject *traceback;
...@@ -32,7 +32,7 @@ int pythonExcToJs() { ...@@ -32,7 +32,7 @@ int pythonExcToJs() {
if (repr == NULL) { if (repr == NULL) {
excval = hiwire_string_utf8((int)"Could not get repr for exception"); excval = hiwire_string_utf8((int)"Could not get repr for exception");
} else { } else {
excval = pythonToJs(repr); excval = python2js(repr);
Py_DECREF(repr); Py_DECREF(repr);
} }
} else { } else {
...@@ -64,7 +64,7 @@ int pythonExcToJs() { ...@@ -64,7 +64,7 @@ int pythonExcToJs() {
PyObject *pystr = PyUnicode_Join(newline, pylines); PyObject *pystr = PyUnicode_Join(newline, pylines);
printf("Python exception:\n"); printf("Python exception:\n");
printf("%s\n", PyUnicode_AsUTF8(pystr)); printf("%s\n", PyUnicode_AsUTF8(pystr));
excval = pythonToJs(pystr); excval = python2js(pystr);
Py_DECREF(pystr); Py_DECREF(pystr);
Py_DECREF(newline); Py_DECREF(newline);
Py_DECREF(pylines); Py_DECREF(pylines);
...@@ -86,7 +86,7 @@ int pythonExcToJs() { ...@@ -86,7 +86,7 @@ int pythonExcToJs() {
return -1; return -1;
} }
static int isTypeName(PyObject *x, const char *name) { static int is_type_name(PyObject *x, const char *name) {
PyObject *x_type = PyObject_Type(x); PyObject *x_type = PyObject_Type(x);
if (x_type == NULL) { if (x_type == NULL) {
// If we can't get a type, that's probably ok in this case... // If we can't get a type, that's probably ok in this case...
...@@ -103,7 +103,7 @@ static int isTypeName(PyObject *x, const char *name) { ...@@ -103,7 +103,7 @@ static int isTypeName(PyObject *x, const char *name) {
return result; return result;
} }
int pythonToJs(PyObject *x) { int python2js(PyObject *x) {
if (x == Py_None) { if (x == Py_None) {
return hiwire_undefined(); return hiwire_undefined();
} else if (x == Py_True) { } else if (x == Py_True) {
...@@ -113,32 +113,32 @@ int pythonToJs(PyObject *x) { ...@@ -113,32 +113,32 @@ int pythonToJs(PyObject *x) {
} else if (PyLong_Check(x)) { } else if (PyLong_Check(x)) {
long x_long = PyLong_AsLongLong(x); long x_long = PyLong_AsLongLong(x);
if (x_long == -1 && PyErr_Occurred()) { if (x_long == -1 && PyErr_Occurred()) {
return pythonExcToJs(); return pythonexc2js();
} }
return hiwire_int(x_long); return hiwire_int(x_long);
} else if (PyFloat_Check(x)) { } else if (PyFloat_Check(x)) {
double x_double = PyFloat_AsDouble(x); double x_double = PyFloat_AsDouble(x);
if (x_double == -1.0 && PyErr_Occurred()) { if (x_double == -1.0 && PyErr_Occurred()) {
return pythonExcToJs(); return pythonexc2js();
} }
return hiwire_double(x_double); return hiwire_double(x_double);
} else if (PyUnicode_Check(x)) { } else if (PyUnicode_Check(x)) {
Py_ssize_t length; Py_ssize_t length;
char *chars = PyUnicode_AsUTF8AndSize(x, &length); char *chars = PyUnicode_AsUTF8AndSize(x, &length);
if (chars == NULL) { if (chars == NULL) {
return pythonExcToJs(); return pythonexc2js();
} }
return hiwire_string_utf8_length((int)(void *)chars, length); return hiwire_string_utf8_length((int)(void *)chars, length);
} else if (PyBytes_Check(x)) { } else if (PyBytes_Check(x)) {
char *x_buff; char *x_buff;
Py_ssize_t length; Py_ssize_t length;
if (PyBytes_AsStringAndSize(x, &x_buff, &length)) { if (PyBytes_AsStringAndSize(x, &x_buff, &length)) {
return pythonExcToJs(); return pythonexc2js();
} }
return hiwire_bytes((int)(void *)x_buff, length); return hiwire_bytes((int)(void *)x_buff, length);
} else if (JsProxy_Check(x)) { } else if (JsProxy_Check(x)) {
return JsProxy_AsJs(x); return JsProxy_AsJs(x);
} else if (PyList_Check(x) || isTypeName(x, "<class 'numpy.ndarray'>")) { } else if (PyList_Check(x) || is_type_name(x, "<class 'numpy.ndarray'>")) {
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) {
...@@ -151,11 +151,11 @@ int pythonToJs(PyObject *x) { ...@@ -151,11 +151,11 @@ int pythonToJs(PyObject *x) {
Py_INCREF(x); Py_INCREF(x);
return pyproxy_new((int)x); return pyproxy_new((int)x);
} }
int jsitem = pythonToJs(pyitem); int jsitem = python2js(pyitem);
if (jsitem == -1) { if (jsitem == -1) {
Py_DECREF(pyitem); Py_DECREF(pyitem);
hiwire_decref(jsarray); hiwire_decref(jsarray);
return pythonExcToJs(); return pythonexc2js();
} }
Py_DECREF(pyitem); Py_DECREF(pyitem);
hiwire_push_array(jsarray, jsitem); hiwire_push_array(jsarray, jsitem);
...@@ -167,18 +167,18 @@ int pythonToJs(PyObject *x) { ...@@ -167,18 +167,18 @@ int pythonToJs(PyObject *x) {
PyObject *pykey, *pyval; PyObject *pykey, *pyval;
Py_ssize_t pos = 0; Py_ssize_t pos = 0;
while (PyDict_Next(x, &pos, &pykey, &pyval)) { while (PyDict_Next(x, &pos, &pykey, &pyval)) {
int jskey = pythonToJs(pykey); int jskey = python2js(pykey);
if (jskey == -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 pythonexc2js();
} }
int jsval = pythonToJs(pyval); int jsval = python2js(pyval);
if (jsval == -1) { if (jsval == -1) {
// TODO: Return a proxy instead here??? // TODO: Return a proxy instead here???
hiwire_decref(jskey); hiwire_decref(jskey);
hiwire_decref(jsdict); hiwire_decref(jsdict);
return pythonExcToJs(); return pythonexc2js();
} }
hiwire_push_object_pair(jsdict, jskey, jsval); hiwire_push_object_pair(jsdict, jskey, jsval);
hiwire_decref(jskey); hiwire_decref(jskey);
...@@ -191,6 +191,6 @@ int pythonToJs(PyObject *x) { ...@@ -191,6 +191,6 @@ int pythonToJs(PyObject *x) {
} }
} }
int pythonToJs_Ready() { int python2js_init() {
return 0; return 0;
} }
...@@ -9,16 +9,16 @@ ...@@ -9,16 +9,16 @@
/** Convert the active Python exception into a Javascript Error object. /** Convert the active Python exception into a Javascript Error object.
* \return A Javascript Error object * \return A Javascript Error object
*/ */
int pythonExcToJs(); int pythonexc2js();
/** Convert a Python object to a Javascript object. /** Convert a Python object to a Javascript object.
* \param The Python object * \param The Python object
* \return The Javascript object -- might be an Error object in the case of an exception. * \return The Javascript object -- might be an Error object in the case of an exception.
*/ */
int pythonToJs(PyObject *x); int python2js(PyObject *x);
/** Set up the global state for this module. /** Set up the global state for this module.
*/ */
int pythonToJs_Ready(); int python2js_init();
#endif /* PYTHON2JS_H */ #endif /* PYTHON2JS_H */
...@@ -21,7 +21,7 @@ static int is_whitespace(char x) { ...@@ -21,7 +21,7 @@ static int is_whitespace(char x) {
} }
} }
int runPython(char *code) { int _runPython(char *code) {
char *last_line = code; char *last_line = code;
while (*last_line != 0) { while (*last_line != 0) {
++last_line; ++last_line;
...@@ -59,7 +59,7 @@ int runPython(char *code) { ...@@ -59,7 +59,7 @@ int runPython(char *code) {
} }
ret = PyRun_StringFlags(code, Py_file_input, globals, globals, &cf); ret = PyRun_StringFlags(code, Py_file_input, globals, globals, &cf);
if (ret == NULL) { if (ret == NULL) {
return pythonExcToJs(); return pythonexc2js();
} }
Py_DECREF(ret); Py_DECREF(ret);
} }
...@@ -78,18 +78,18 @@ int runPython(char *code) { ...@@ -78,18 +78,18 @@ int runPython(char *code) {
} }
if (ret == NULL) { if (ret == NULL) {
return pythonExcToJs(); return pythonexc2js();
} }
int id = pythonToJs(ret); int id = python2js(ret);
Py_DECREF(ret); Py_DECREF(ret);
return id; return id;
} }
EM_JS(int, runPython_Ready, (), { EM_JS(int, runpython_init, (), {
Module.runPython = function (code) { Module.runPython = function (code) {
var pycode = allocate(intArrayFromString(code), 'i8', ALLOC_NORMAL); var pycode = allocate(intArrayFromString(code), 'i8', ALLOC_NORMAL);
var idresult = Module._runPython(pycode); var idresult = Module.__runPython(pycode);
jsresult = Module.hiwire_get_value(idresult); jsresult = Module.hiwire_get_value(idresult);
Module.hiwire_decref(idresult); Module.hiwire_decref(idresult);
_free(pycode); _free(pycode);
......
...@@ -5,10 +5,6 @@ ...@@ -5,10 +5,6 @@
*/ */
/** The primary entry point function that runs Python code. int runpython_init();
*/
int runPython(char *code);
int runPython_Ready();
#endif /* RUNPYTHON_H */ #endif /* RUNPYTHON_H */
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