Skip to content

Commit

Permalink
Move call and new to JsMethod (pyodide#1138)
Browse files Browse the repository at this point in the history
The goal of this PR is to ensure that JsProxy is no longer callable, now only JsMethod will be callable. Many bugs were caused by differences in behavior between the JsProxy_Call and JsBoundMethod_Call. If we build a proxy of a callable javascript object, we will use the subclass JsMethod. This way, there is only one Call method and no bugs caused by divergent behaviors.
  • Loading branch information
Hood Chatham authored Jan 16, 2021
1 parent c533b07 commit 3fef124
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 189 deletions.
2 changes: 1 addition & 1 deletion src/core/error_handling.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ EM_JS_NUM(errcode, log_error_obj, (JsRef obj), {
void
PyodideErr_SetJsError(JsRef err)
{
PyObject* py_err = JsProxy_new_error(err);
PyObject* py_err = JsProxy_create(err);
PyErr_SetObject((PyObject*)(py_err->ob_type), py_err);
}

Expand Down
19 changes: 16 additions & 3 deletions src/core/hiwire.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ hiwire_undefined()
}

JsRef
hiwire_jsnull()
hiwire_null()
{
return Js_NULL;
}
Expand Down Expand Up @@ -53,7 +53,7 @@ EM_JS(int, hiwire_init, (), {
};
Module.hiwire = {};
Module.hiwire.UNDEFINED = _hiwire_undefined();
Module.hiwire.JSNULL = _hiwire_jsnull();
Module.hiwire.JSNULL = _hiwire_null();
Module.hiwire.TRUE = _hiwire_true();
Module.hiwire.FALSE = _hiwire_false();

Expand Down Expand Up @@ -339,7 +339,14 @@ EM_JS_REF(JsRef,
(JsRef idfunc, JsRef idthis, JsRef idargs),
{
let func = Module.hiwire.get_value(idfunc);
let this_ = Module.hiwire.get_value(idthis);
let this_;
// clang-format off
if (idthis === 0) {
// clang-format on
this_ = null;
} else {
this_ = Module.hiwire.get_value(idthis);
}
let args = Module.hiwire.get_value(idargs);
return Module.hiwire.new_value(func.apply(this_, args));
});
Expand Down Expand Up @@ -393,6 +400,12 @@ EM_JS_NUM(bool, hiwire_is_function, (JsRef idobj), {
// clang-format on
});

EM_JS_NUM(bool, hiwire_is_error, (JsRef idobj), {
// From https://stackoverflow.com/a/45496068
let value = Module.hiwire.get_value(idobj);
return !!(value && value.stack && value.message);
});

EM_JS_NUM(bool, hiwire_function_supports_kwargs, (JsRef idfunc), {
let funcstr = Module.hiwire.get_value(idfunc).toString();
return Module.function_supports_kwargs(funcstr);
Expand Down
3 changes: 3 additions & 0 deletions src/core/hiwire.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,9 @@ hiwire_is_pyproxy(JsRef idobj);
bool
hiwire_is_function(JsRef idobj);

bool
hiwire_is_error(JsRef idobj);

bool
hiwire_function_supports_kwargs(JsRef idfunc);

Expand Down
15 changes: 2 additions & 13 deletions src/core/js2python.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,14 @@ _js2python_pyproxy(PyObject* val)
PyObject*
_js2python_memoryview(JsRef id)
{
PyObject* jsproxy = JsProxy_cnew(id);
PyObject* jsproxy = JsProxy_create(id);
return PyMemoryView_FromObject(jsproxy);
}

PyObject*
_js2python_jsproxy(JsRef id)
{
return JsProxy_cnew(id);
}

PyObject*
_js2python_error(JsRef id)
{
return JsProxy_new_error(id);
return JsProxy_create(id);
}

// TODO: Add some meaningful order
Expand Down Expand Up @@ -135,9 +129,6 @@ EM_JS_REF(PyObject*, __js2python, (JsRef id), {
return result;
}

// From https://stackoverflow.com/a/45496068
function is_error(value) { return value && value.stack && value.message; }

// clang-format off
let value = Module.hiwire.get_value(id);
let type = typeof value;
Expand All @@ -155,8 +146,6 @@ EM_JS_REF(PyObject*, __js2python, (JsRef id), {
return __js2python_pyproxy(Module.PyProxy.getPtr(value));
} else if (value['byteLength'] !== undefined) {
return __js2python_memoryview(id);
} else if (is_error(value)) {
return __js2python_error(id);
} else {
return __js2python_jsproxy(id);
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/jsimport.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int
JsImport_init()
{
JsRef globalThis_ref = hiwire_get_global("globalThis");
globalThis = JsProxy_cnew(globalThis_ref);
globalThis = JsProxy_create(globalThis_ref);
hiwire_decref(globalThis_ref);

PyObject* module_dict = PyImport_GetModuleDict();
Expand Down
Loading

0 comments on commit 3fef124

Please sign in to comment.