This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
Allow registering Ruby callbacks for V8 objects. #387
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
SetWeak
method 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
__EachV8Finalizer
This 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
object
variable 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.