Skip to content

Support FinalizationRegistry  #31072

Open

Description

@gterzian

Came-up in #30880

Spec: https://tc39.es/ecma262/multipage/managing-memory.html#sec-finalization-registry-objects

This looks like something the JS engine would implement, although we could add a "fake" one using webidl, and simply leaking any resources observed.

Perhaps this will be solved with a future version of https://github.com/servo/mozjs

Activity

  1. added
    L-javascript
    on Jan 12, 2024
  2. jdm commented on Jan 12, 2024

    @jdm
    Member

    This already present in Spidermonkey. We just haven't initializes the callbacks required to expose it and implement the DOM integration: https://searchfox.org/mozilla-central/source/js/src/shell/js.cpp#11041

  3. jdm commented on Jan 12, 2024

    @jdm
    Member
  4. added
    A-content/script
    on Jan 15, 2024
  5. gterzian commented on Jan 15, 2024

    @gterzian
    MemberAuthor

    The current use of JS_AddInterruptCallback can provide a loose example of what the Rust code callback could look like. In the light of #30862, we should hide the use of jsapi inside dom/bindings, unlike what we did previously with JS_AddInterruptCallback.

  6. gterzian commented on Jan 15, 2024

    @gterzian
    MemberAuthor

    What the DOM integration would look like is not completely clear to me yet, but it would seem that the callback should, when called, queue a task(or rather just send a message to the script or worker thread) that would then run something like the equivalent steps found here in Gecko

    See also https://github.com/servo/mozjs/blob/191a3caafb7fb46e681fc45e07c94c290907abe5/mozjs-sys/mozjs/js/src/gc/FinalizationObservers.cpp#L296 (the part of the implementation in Spidermonkey that calls into the callback).

  7. added
    B-interesting-project
    on Jan 15, 2024
  8. sebsebmc commented on Feb 14, 2024

    @sebsebmc
    Contributor

    @gterzian Is this something that would be done with defining a new DOM binding via WebIDL and integrating the jsapi code in dom/bindings? I've been looking at FF/Chrome/Webkit and none of them seem to have IDL for FinalizationRegistry. (FF does but it just defines the callback if I understand correctly)

  9. sebsebmc commented on Feb 14, 2024

    @sebsebmc
    Contributor

    Re-reading the comments after pondering on this a bit I realize that there really isn't anything that needs to be exposed with WebIDL because this should be in the JS engine (and the comments above point that out).

  10. gterzian commented on Feb 15, 2024

    @gterzian
    MemberAuthor

    Right, this is more about using the JS_API to integrate with Servo, and also figuring out what Gecko does and whether we need to do anything similar.

  11. sebsebmc commented on Feb 15, 2024

    @sebsebmc
    Contributor

    I think I might have an idea of how this should go, and it looks like someone recently submitted a Spidermonkey-embedding-example for enabling FinalizationRegistry as well.

  12. gterzian commented on Feb 16, 2024

    @gterzian
    MemberAuthor

    @sebsebmc I would say go for it!

    By the way in the light of #30862 it would be good to hide the JS_API stuff inside dom/bindings. See for example ongoing work with dom/bindings/typedarrays.rs.

  13. sebsebmc commented on Feb 19, 2024

    @sebsebmc
    Contributor

    I believe I have figured out how to set the realm options and callback in order for FinalizationRegistry to be enabled inside of SM.

    I think I'm going to need a bit of mentoring on this one, I've got a few questions that I'm struggling with:

    1. The example and FF both keep a vector of callbacks that they process. I'm struggling to understand what the type for this vec of JSFunctions should be for rooting and tracing purposes.
    2. I think that using the Promise callback microtask queue would work for running the cleanup job but if there's another job queue that's better, it'd be good to know.
    3. I'm not sure what the servo equivalent of ExposeObjectToActiveJS and JS_CallFunction is for running the callbacks.
  14. gterzian commented on Feb 20, 2024

    @gterzian
    MemberAuthor

    I believe I have figured out how to set the realm options and callback in order for FinalizationRegistry to be enabled inside of SM.

    Awesome!

    1. Look into rooted_vec!
    2. I think we can follow the example in your link and use the JobQueue
    3. You probably will have to add manual code for your usecase, but the generated code can provide a useful example. For example if you build Servo locally, and search for DecodeErrorCallback, you can see some callback code generated in a file in target/(...)/Bindings/BaseAudioContextBinding.rs.

    Hope this helps, let me know if you have other questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-content/scriptB-interesting-projectE-candidate-for-mentoringL-javascript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions