Skip to content

Commit

Permalink
Combine pyproxy_new and pyproxy_use into pyproxy_new (pyodide#1050)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hood Chatham authored Jan 11, 2021
1 parent 65a9da0 commit e8e341f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 22 deletions.
20 changes: 8 additions & 12 deletions src/core/pyproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,27 +150,22 @@ _pyproxy_apply(PyObject* pyobj, JsRef idargs)

void
_pyproxy_destroy(PyObject* ptrobj)
{
{ // See bug #1049
Py_DECREF(ptrobj);
EM_ASM({ delete Module.PyProxies[$0]; }, ptrobj);
}

EM_JS_REF(JsRef, pyproxy_use, (PyObject * ptrobj), {
// Checks if there is already an existing proxy on ptrobj

if (Module.PyProxies.hasOwnProperty(ptrobj)) {
return Module.hiwire.new_value(Module.PyProxies[ptrobj]);
}

return Module.hiwire.ERROR;
})

EM_JS_REF(JsRef, pyproxy_new, (PyObject * ptrobj), {
// Technically, this leaks memory, since we're holding on to a reference
// to the proxy forever. But we have that problem anyway since we don't
// have a destructor in Javascript to free the Python object.
// _pyproxy_destroy, which is a way for users to manually delete the proxy,
// also deletes the proxy from this set.
if (Module.PyProxies.hasOwnProperty(ptrobj)) {
return Module.hiwire.new_value(Module.PyProxies[ptrobj]);
}

_Py_IncRef(ptrobj);

let target = function(){};
target['$$'] = { ptr : ptrobj, type : 'PyProxy' };
Expand All @@ -180,7 +175,7 @@ EM_JS_REF(JsRef, pyproxy_new, (PyObject * ptrobj), {
return Module.hiwire.new_value(proxy);
});

EM_JS_NUM(int, pyproxy_init, (), {
EM_JS(int, pyproxy_init, (), {
// clang-format off
Module.PyProxies = {};
Module.PyProxy = {
Expand Down Expand Up @@ -220,6 +215,7 @@ EM_JS_NUM(int, pyproxy_init, (), {
} else if (jskey === '$$') {
return jsobj['$$'];
} else if (jskey === 'destroy') {
// See bug #1049
return function() {
__pyproxy_destroy(ptrobj);
jsobj['$$']['ptr'] = null;
Expand Down
3 changes: 0 additions & 3 deletions src/core/pyproxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
// This implements the Javascript Proxy handler interface as defined here:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy

JsRef
pyproxy_use(PyObject* obj);

JsRef
pyproxy_new(PyObject* obj);

Expand Down
7 changes: 0 additions & 7 deletions src/core/python2js.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,6 @@ _python2js(PyObject* x, PyObject* map)
// Proxies we've already created are just returned again, so that the
// same object on the Python side is always the same object on the
// Javascript side.
ret = pyproxy_use(x);
if (ret != NULL) {
return ret;
}

// Reference counter is increased only once when a PyProxy is created.
Py_INCREF(x);
return pyproxy_new(x);
}
}
Expand Down

0 comments on commit e8e341f

Please sign in to comment.