Fix #388: Support dir(x) on JsProxies (#393)

This commit is contained in:
Michael Droettboom 2019-04-22 14:08:29 -04:00 committed by GitHub
parent f1c05058b0
commit 67ae415f09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 0 deletions

View File

@ -241,6 +241,15 @@ EM_JS(void, hiwire_delete_member_obj, (int idobj, int ididx), {
delete jsobj[jsidx];
});
EM_JS(int, hiwire_dir, (int idobj), {
var jsobj = Module.hiwire_get_value(idobj);
var result = [];
do {
result.push.apply(result, Object.getOwnPropertyNames(jsobj));
} while ((jsobj = Object.getPrototypeOf(jsobj)));
return Module.hiwire_new_value(result);
});
EM_JS(int, hiwire_call, (int idfunc, int idargs), {
var jsfunc = Module.hiwire_get_value(idfunc);
var jsargs = Module.hiwire_get_value(idargs);

View File

@ -346,6 +346,13 @@ hiwire_set_member_obj(int idobj, int ididx, int idval);
void
hiwire_delete_member_obj(int idobj, int ididx);
/**
* Get the methods on an object, both on itself and what it inherits.
*
*/
int
hiwire_dir(int idobj);
/**
* Call a function
*

View File

@ -383,6 +383,17 @@ JsProxy_HasBytes(PyObject* o)
}
}
static PyObject*
JsProxy_Dir(PyObject* o)
{
JsProxy* self = (JsProxy*)o;
int iddir = hiwire_dir(self->js);
PyObject* pydir = js2python(iddir);
hiwire_decref(iddir);
return pydir;
}
// clang-format off
static PyMappingMethods JsProxy_MappingMethods = {
JsProxy_length,
@ -408,6 +419,10 @@ static PyMethodDef JsProxy_Methods[] = {
(PyCFunction)JsProxy_HasBytes,
METH_NOARGS,
"Returns true if instance has buffer memory. For testing only." },
{ "__dir__",
(PyCFunction)JsProxy_Dir,
METH_NOARGS,
"Returns a list of the members and methods on the object." },
{ NULL }
};
// clang-format on

View File

@ -443,6 +443,13 @@ def test_jsproxy(selenium):
dict(TEST) == {'foo': 'bar', 'baz': 'bap'}
"""
) is True
assert selenium.run(
"""
from js import document
el = document.createElement('div')
len(dir(el)) >= 200 and 'appendChild' in dir(el)
"""
) is True
def test_jsproxy_iter(selenium):