Commit d7e4cb8a authored by Michael Droettboom's avatar Michael Droettboom

Convenient access to repr() on the Javascript side

parent 8e3e3608
#include "js2python.hpp" #include "js2python.hpp"
#include "jsproxy.hpp" #include "jsproxy.hpp"
#include "pyproxy.hpp"
using emscripten::val; using emscripten::val;
...@@ -20,7 +21,14 @@ PyObject *jsToPython(val x) { ...@@ -20,7 +21,14 @@ PyObject *jsToPython(val x) {
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} else { } else {
return JsProxy_cnew(x); try {
Py py_x = x.as<Py>();
PyObject *pypy_x = py_x.x;
Py_INCREF(pypy_x);
return pypy_x;
} catch (...) {
return JsProxy_cnew(x);
}
} }
} }
......
...@@ -21,9 +21,24 @@ using emscripten::val; ...@@ -21,9 +21,24 @@ using emscripten::val;
// Conversions // Conversions
val repr(val v) {
PyObject *pyv = jsToPython(v);
PyObject *r = PyObject_Repr(pyv);
if (r == NULL) {
return pythonExcToJs();
}
PyObject_Print(r, 0, 0);
val result = pythonToJs(r);
Py_DECREF(r);
Py_DECREF(pyv);
return result;
}
EMSCRIPTEN_BINDINGS(python) { EMSCRIPTEN_BINDINGS(python) {
emscripten::function("runPython", &runPython); emscripten::function("runPython", &runPython);
emscripten::function("pyimport", &pyimport); emscripten::function("pyimport", &pyimport);
emscripten::function("repr", &repr);
emscripten::class_<Py>("Py") emscripten::class_<Py>("Py")
.function<val>("call", &Py::call) .function<val>("call", &Py::call)
.function<val>("getattr", &Py::getattr) .function<val>("getattr", &Py::getattr)
......
...@@ -10,9 +10,9 @@ ...@@ -10,9 +10,9 @@
#include "python2js.hpp" #include "python2js.hpp"
class Py { class Py {
public:
PyObject *x; PyObject *x;
public:
Py(PyObject *obj); Py(PyObject *obj);
Py(const Py& o); Py(const Py& o);
~Py(); ~Py();
......
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