Skip to content
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

Merged
merged 9 commits into from Oct 22, 2023

Conversation

hoodmane
Copy link
Member

@hoodmane hoodmane commented Oct 21, 2023

This also fixes JsArray_pop which leaked memory and returned a singleton list with the element removed rather than the element itself.

@hoodmane hoodmane changed the title Use JsVal instead of JsRef in more places Use JsVal instead of JsRef in more places (nr. 3) Oct 21, 2023
Copy link
Member

@ryanking13 ryanking13 left a 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),
Copy link
Member

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

Copy link
Member Author

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;
}

Copy link
Member Author

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.

Copy link
Member

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!

@hoodmane
Copy link
Member Author

I added a lot of methods in the same PR with JsArray_pop, so it stands to reason that I forgot to test one.

@hoodmane hoodmane merged commit a41e267 into pyodide:main Oct 22, 2023
39 of 40 checks passed
@hoodmane hoodmane deleted the libhiwire-4 branch October 22, 2023 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants