Skip to content

Commit

Permalink
BUGFIX: Bind functions to window on from js import func (#1126)
Browse files Browse the repository at this point in the history
Fixes #461
  • Loading branch information
Hood Chatham committed Jan 16, 2021
1 parent 603d223 commit c533b07
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
28 changes: 9 additions & 19 deletions src/core/jsimport.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,26 @@
#include "Python.h"

#include "jsimport.h"

#include "jsproxy.h"
#include <emscripten.h>

#include "hiwire.h"
#include "js2python.h"

static PyObject* js_module = NULL;
static PyObject* globalThis = NULL;
_Py_IDENTIFIER(__dir__);

static PyObject*
JsImport_GetAttr(PyObject* self, PyObject* attr)
{
const char* c = PyUnicode_AsUTF8(attr);
if (c == NULL) {
return NULL;
}
JsRef idval = hiwire_get_global(c);
if (idval == NULL) {
PyErr_Format(PyExc_AttributeError, "Unknown attribute '%s'", c);
return NULL;
}
PyObject* result = js2python(idval);
hiwire_decref(idval);
return result;
return PyObject_GetAttr(globalThis, attr);
}

static PyObject*
JsImport_Dir()
{
JsRef idwindow = hiwire_get_global("self");
JsRef iddir = hiwire_dir(idwindow);
hiwire_decref(idwindow);
PyObject* pydir = js2python(iddir);
hiwire_decref(iddir);
return pydir;
return _PyObject_CallMethodIdObjArgs(globalThis, &PyId___dir__, NULL);
}

static PyMethodDef JsModule_Methods[] = {
Expand All @@ -61,6 +47,10 @@ static struct PyModuleDef JsModule = {
int
JsImport_init()
{
JsRef globalThis_ref = hiwire_get_global("globalThis");
globalThis = JsProxy_cnew(globalThis_ref);
hiwire_decref(globalThis_ref);

PyObject* module_dict = PyImport_GetModuleDict();
if (module_dict == NULL) {
return -1;
Expand Down
2 changes: 1 addition & 1 deletion src/core/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ main(int argc, char** argv)
TRY_INIT(hiwire);
TRY_INIT(error_handling);
TRY_INIT(js2python);
TRY_INIT_WITH_CORE_MODULE(JsProxy); // JsProxy needs to be before JsImport
TRY_INIT(JsImport);
TRY_INIT_WITH_CORE_MODULE(JsProxy);
TRY_INIT(pyproxy);
TRY_INIT(python2js);

Expand Down
2 changes: 1 addition & 1 deletion src/core/pyproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ EM_JS(int, pyproxy_init, (), {
return ptr;
},
isPyProxy: function(jsobj) {
return jsobj['$$'] !== undefined && jsobj['$$']['type'] === 'PyProxy';
return jsobj && jsobj['$$'] !== undefined && jsobj['$$']['type'] === 'PyProxy';
},
addExtraKeys: function(result) {
result.push('toString');
Expand Down
19 changes: 19 additions & 0 deletions src/tests/test_jsproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ def test_jsproxy_call(selenium):
)


@pytest.mark.xfail
def test_jsproxy_call_kwargs(selenium):
assert (
selenium.run_js(
Expand Down Expand Up @@ -423,6 +424,24 @@ async def test():
)


@run_in_pyodide
def test_import_invocation():
import js

def temp():
print("okay?")

js.setTimeout(temp, 100)
js.fetch("packages.json")


@run_in_pyodide
def test_import_bind():
from js import fetch

fetch("packages.json")


@run_in_pyodide
def test_nested_attribute_access():
import js
Expand Down

0 comments on commit c533b07

Please sign in to comment.