New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use JsVal instead of JsRef in more places (nr. 3) #4236
Conversation
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! LGTM with a question.
This also fixes JsArray_pop which leaked memory and returned a singleton list with the element removed rather than the element itself.
Nice!
@@ -105,16 +105,16 @@ def test_jsproxy_document(selenium): | |||
@pytest.mark.parametrize( | |||
"js,result", | |||
[ | |||
("{}", False), | |||
("{}", True), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Were we treating an empty JS object as a falsy value before?
I got a different result when I tried this.
a = {}
pyodide.runPython(`
from js import a
print(bool(a))
`)
True
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty object was always truthy, but the test was written incorrectly. If you do eval("{}")
, {}
is parsed as an empty block not as an object. Thus, it returns undefined
which is Falsey
. I adjusted the test so it calls run_js(f"({js})")
which calls eval("({})")
which returns empty object with is truthy.
I think empty object is most comparable to:
class A:
pass
bool(A())
We could do something like:
function isTruthy(obj) {
if (obj.constructor !== Object && obj.constructor !== null) {
return true;
}
for (const prop in obj) {
if (Object.hasOwn(obj, prop)) {
return true;
}
}
// object with default or null constructor and no ownProperties
return false;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyways I am open to switching to this in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Thanks for the explanation!
I added a lot of methods in the same PR with |
This also fixes
JsArray_pop
which leaked memory and returned a singleton list with the element removed rather than the element itself.