This repository was archived by the owner on Dec 4, 2023. It is now read-only.
Allow registering Ruby callbacks for V8 objects.#387
Open
cowboyd wants to merge 5 commits intoupgrade-to-v8-4.5from
Open
Allow registering Ruby callbacks for V8 objects.#387cowboyd wants to merge 5 commits intoupgrade-to-v8-4.5from
cowboyd wants to merge 5 commits intoupgrade-to-v8-4.5from
Conversation
b817b29 to
5cebcd4
Compare
This allows Ruby code to listen directly for when V8 object is garbage
collected. This is done with the `__DefineFinalizer__` method which can
be invoked on any handle. E.g.
v8_object.__DefineFinalizer__(method_that_generates_callable)
Just like in Ruby, care should be taken to ensure that the finalizer
does not reference the V8 object at all, otherwise, it might prevent it
from being garbage collected. The later, once v8_object has been gc'd,
the finalizer will be enqueued into an internal data structure that can
be accessed via the isolate's `__EachV8Finalizer`
isolate.__EachV8Finalizer__ do |finalizer|
finalizer.call()
end
There was a question of whether to follow the strict V8 API for this,
and expose the `SetWeak` method, but this would mean actually making a
handle weak, which is fine, but then we would have to add a mechanism
to capture a strong reference from any reference which we don't have.
We may want to revisit this at some later date.
5cebcd4 to
750e6b4
Compare
That will ensure that at least one object triggers finalization.
…bjects Conflicts: ext/v8/isolate.h
This implementation hues very closesly to the actual V8 API, so is more desirable. It also adds the ability to capture a handle to an existing object.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This allows Ruby code to listen directly for when V8 object is garbage
collected. This is done with the
SetWeakmethod which canbe invoked on any handle. This handle is now weak, and the object that it references can be garbage collected by V8 even if the Ruby object is still in scope. E.g.
Later, once v8_object has been gc'd, the finalizer will be enqueued into an internal data structure that can
be accessed via the isolate's
__EachV8FinalizerThis also adds the ability to capture a new, persistent reference to an existing handle via the
V8::C::Handle::New()method. For example, suppose we have the following:The
objectvariable represents a persistent reference to this V8 object, and it cannot be garbage collected by V8 as long as this Ruby object is reachable. But we can add another reference:Notice that even though we are using the handle api, the type of the returned object is the same as the original reference.
This V8 object cannot be garbage collected until both of the Ruby objects are either no longer reachable, or are set to be weak.