From 4c6c27dfd407d3267e3f48f5ada409a36a2f0b92 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Sat, 13 Mar 2021 02:54:21 -0800 Subject: [PATCH] Filter integers out of dir(js_array) (#1321) --- src/core/hiwire.c | 9 ++++++++- src/tests/test_jsproxy.py | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/core/hiwire.c b/src/core/hiwire.c index 56f344b00..83f53e4ee 100644 --- a/src/core/hiwire.c +++ b/src/core/hiwire.c @@ -349,7 +349,14 @@ EM_JS_REF(JsRef, hiwire_dir, (JsRef idobj), { let jsobj = Module.hiwire.get_value(idobj); let result = []; 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)); return Module.hiwire.new_value(result); }); diff --git a/src/tests/test_jsproxy.py b/src/tests/test_jsproxy.py index 27845313e..948eb9256 100644 --- a/src/tests/test_jsproxy.py +++ b/src/tests/test_jsproxy.py @@ -38,6 +38,28 @@ def test_jsproxy_dir(selenium): assert set1.issuperset(jsproxy_items) assert set1.issuperset(callable_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):