pyodide/src/pyimport.c

39 lines
757 B
C
Raw Normal View History

2018-05-23 11:23:49 +00:00
#include "pyimport.h"
#include <Python.h>
#include <emscripten.h>
#include "python2js.h"
2018-06-14 18:19:08 +00:00
extern PyObject* globals;
2018-05-23 11:23:49 +00:00
2018-06-14 18:19:08 +00:00
int
_pyimport(char* name)
{
PyObject* pyname = PyUnicode_FromString(name);
PyObject* pyval = PyDict_GetItem(globals, pyname);
2018-05-23 11:23:49 +00:00
if (pyval == NULL) {
Py_DECREF(pyname);
2018-05-31 00:27:18 +00:00
return pythonexc2js();
2018-05-23 11:23:49 +00:00
}
Py_DECREF(pyname);
2018-05-31 00:27:18 +00:00
int idval = python2js(pyval);
Py_DECREF(pyval);
return idval;
2018-05-23 11:23:49 +00:00
}
2018-05-31 00:27:18 +00:00
EM_JS(int, pyimport_init, (), {
2018-06-14 18:19:08 +00:00
Module.pyimport = function(name)
{
var pyname = allocate(intArrayFromString(name), 'i8', ALLOC_NORMAL);
2018-05-31 00:27:18 +00:00
var idresult = Module.__pyimport(pyname);
jsresult = Module.hiwire_get_value(idresult);
Module.hiwire_decref(idresult);
_free(pyname);
return jsresult;
2018-05-23 11:23:49 +00:00
};
return 0;
});