mozilla / positron

a experimental, Electron-compatible runtime on top of Gecko
Other
564 stars 64 forks source link

support Electron's JavaScript destructor callbacks #46

Open mykmelez opened 8 years ago

mykmelez commented 8 years ago

Electron uses a native API to register JavaScript callbacks on destruction of certain objects:

https://github.com/mozilla/positron/search?l=javascript&q=setDestructor&utf8=%E2%9C%93

Electron uses these callbacks to destroy objects in another process that are proxies or callbacks for the objects being destroyed in this process. We'll need to support these callbacks too, or find another way to ensure that the objects in the other process are destroyed, or else we'll leak the objects in the other process.

Ryuno-Ki commented 8 years ago

"this process", "other process" … I'm confused.

Could you rephrase using "process A" and "process B", @mykmelez?

mykmelez commented 8 years ago

Could you rephrase using "process A" and "process B", @mykmelez?

I can, but there are actually two (related) use cases, one in process A and the other in process B, so perhaps it would be better to get more specific:

Use Case 1:

When a "renderer" process creates a "delegate object" to represent an object in the "main" (a.k.a. "browser") process, it calls v8Utils.setDestructor to register a JavaScript callback on the delegate:

https://github.com/mozilla/positron/blob/6f0f638402d5d71751c2225272dece28f58ad5f9/positron/electron/lib/renderer/api/remote.js#L204-L208

Upon destruction of the delegate, that callback sends a message to the main process, which removes the object from a registry of objects:

https://github.com/mozilla/positron/blob/6f0f638402d5d71751c2225272dece28f58ad5f9/positron/electron/lib/browser/rpc-server.js#L344-L346

Use Case 2:

When the main process creates a "callIntoRenderer function" to represent a function in a renderer process, it calls v8Utils.setDestructor to register a JavaScript callback on the function:

https://github.com/mozilla/positron/blob/6f0f638402d5d71751c2225272dece28f58ad5f9/positron/electron/lib/browser/rpc-server.js#L203-L206

Upon destruction of the function, that callback sends a message to the renderer process, which removes the object from a registry of callbacks:

https://github.com/mozilla/positron/blob/6f0f638402d5d71751c2225272dece28f58ad5f9/positron/electron/lib/renderer/api/remote.js#L241-L244

Ryuno-Ki commented 8 years ago

Thanks for clarifying!

brendandahl commented 8 years ago

As discussed in IRC, setDestructor has been removed from electron https://github.com/electron/electron/commit/06cf0406fe8ddcef3a500a689952c77fdfb9c9a8#diff-695530a8eced1fbb2c784d6a52a4290f

Though we'll likely need to support setRemoteCallbackFreer and setRemoteObjectFreer