Filter integers out of dir(js_array) (#1321)

This commit is contained in:
Hood Chatham 2021-03-13 02:54:21 -08:00 committed by GitHub
parent de7c342010
commit 4c6c27dfd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions

View File

@ -349,7 +349,14 @@ EM_JS_REF(JsRef, hiwire_dir, (JsRef idobj), {
let jsobj = Module.hiwire.get_value(idobj); let jsobj = Module.hiwire.get_value(idobj);
let result = []; let result = [];
do { do {
result.push(... Object.getOwnPropertyNames(jsobj)); // clang-format off
result.push(... Object.getOwnPropertyNames(jsobj).filter(
s => {
let c = s.charCodeAt(0);
return c < 48 || c > 57; /* Filter out integer array indices */
}
));
// clang-format on
} while (jsobj = Object.getPrototypeOf(jsobj)); } while (jsobj = Object.getPrototypeOf(jsobj));
return Module.hiwire.new_value(result); return Module.hiwire.new_value(result);
}); });

View File

@ -38,6 +38,28 @@ def test_jsproxy_dir(selenium):
assert set1.issuperset(jsproxy_items) assert set1.issuperset(jsproxy_items)
assert set1.issuperset(callable_items) assert set1.issuperset(callable_items)
assert set1.isdisjoint(a_items) assert set1.isdisjoint(a_items)
selenium.run_js(
"""
window.a = [0,1,2,3,4,5,6,7,8,9];
a[27] = 0;
a[":"] = 0;
a["/"] = 0;
a.abcd = 0;
a.α = 0;
pyodide.runPython(`
from js import a
d = dir(a)
assert '0' not in d
assert '9' not in d
assert '27' not in d
assert ':' in d
assert '/' in d
assert 'abcd' in d
assert 'α' in d
`);
"""
)
def test_jsproxy_getattr(selenium): def test_jsproxy_getattr(selenium):