-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
src: create BaseObject with node::Realm #44348
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
#include "base_object.h" | ||
#include "env-inl.h" | ||
#include "node_realm-inl.h" | ||
|
||
namespace node { | ||
|
||
using v8::FunctionCallbackInfo; | ||
using v8::FunctionTemplate; | ||
using v8::HandleScope; | ||
using v8::Local; | ||
using v8::Object; | ||
using v8::Value; | ||
using v8::WeakCallbackInfo; | ||
using v8::WeakCallbackType; | ||
|
||
BaseObject::BaseObject(Realm* realm, Local<Object> object) | ||
: persistent_handle_(realm->isolate(), object), realm_(realm) { | ||
CHECK_EQ(false, object.IsEmpty()); | ||
CHECK_GE(object->InternalFieldCount(), BaseObject::kInternalFieldCount); | ||
object->SetAlignedPointerInInternalField(BaseObject::kEmbedderType, | ||
&kNodeEmbedderId); | ||
object->SetAlignedPointerInInternalField(BaseObject::kSlot, | ||
static_cast<void*>(this)); | ||
realm->AddCleanupHook(DeleteMe, static_cast<void*>(this)); | ||
realm->modify_base_object_count(1); | ||
} | ||
|
||
BaseObject::~BaseObject() { | ||
realm()->modify_base_object_count(-1); | ||
realm()->RemoveCleanupHook(DeleteMe, static_cast<void*>(this)); | ||
|
||
if (UNLIKELY(has_pointer_data())) { | ||
PointerData* metadata = pointer_data(); | ||
CHECK_EQ(metadata->strong_ptr_count, 0); | ||
metadata->self = nullptr; | ||
if (metadata->weak_ptr_count == 0) delete metadata; | ||
} | ||
|
||
if (persistent_handle_.IsEmpty()) { | ||
// This most likely happened because the weak callback below cleared it. | ||
return; | ||
} | ||
|
||
{ | ||
HandleScope handle_scope(env()->isolate()); | ||
legendecas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
object()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr); | ||
} | ||
} | ||
|
||
void BaseObject::MakeWeak() { | ||
if (has_pointer_data()) { | ||
pointer_data()->wants_weak_jsobj = true; | ||
if (pointer_data()->strong_ptr_count > 0) return; | ||
} | ||
|
||
persistent_handle_.SetWeak( | ||
this, | ||
[](const WeakCallbackInfo<BaseObject>& data) { | ||
BaseObject* obj = data.GetParameter(); | ||
// Clear the persistent handle so that ~BaseObject() doesn't attempt | ||
// to mess with internal fields, since the JS object may have | ||
// transitioned into an invalid state. | ||
// Refs: https://github.com/nodejs/node/issues/18897 | ||
obj->persistent_handle_.Reset(); | ||
CHECK_IMPLIES(obj->has_pointer_data(), | ||
obj->pointer_data()->strong_ptr_count == 0); | ||
obj->OnGCCollect(); | ||
}, | ||
WeakCallbackType::kParameter); | ||
} | ||
|
||
// This just has to be different from the Chromium ones: | ||
// https://source.chromium.org/chromium/chromium/src/+/main:gin/public/gin_embedders.h;l=18-23;drc=5a758a97032f0b656c3c36a3497560762495501a | ||
// Otherwise, when Node is loaded in an isolate which uses cppgc, cppgc will | ||
// misinterpret the data stored in the embedder fields and try to garbage | ||
// collect them. | ||
uint16_t kNodeEmbedderId = 0x90de; | ||
|
||
void BaseObject::LazilyInitializedJSTemplateConstructor( | ||
const FunctionCallbackInfo<Value>& args) { | ||
DCHECK(args.IsConstructCall()); | ||
CHECK_GE(args.This()->InternalFieldCount(), BaseObject::kInternalFieldCount); | ||
args.This()->SetAlignedPointerInInternalField(BaseObject::kEmbedderType, | ||
&kNodeEmbedderId); | ||
args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr); | ||
} | ||
|
||
Local<FunctionTemplate> BaseObject::MakeLazilyInitializedJSTemplate( | ||
Environment* env) { | ||
Local<FunctionTemplate> t = NewFunctionTemplate( | ||
env->isolate(), LazilyInitializedJSTemplateConstructor); | ||
t->Inherit(BaseObject::GetConstructorTemplate(env)); | ||
t->InstanceTemplate()->SetInternalFieldCount(BaseObject::kInternalFieldCount); | ||
return t; | ||
} | ||
|
||
BaseObject::PointerData* BaseObject::pointer_data() { | ||
if (!has_pointer_data()) { | ||
PointerData* metadata = new PointerData(); | ||
metadata->wants_weak_jsobj = persistent_handle_.IsWeak(); | ||
metadata->self = this; | ||
pointer_data_ = metadata; | ||
} | ||
CHECK(has_pointer_data()); | ||
return pointer_data_; | ||
} | ||
|
||
void BaseObject::decrease_refcount() { | ||
CHECK(has_pointer_data()); | ||
PointerData* metadata = pointer_data(); | ||
CHECK_GT(metadata->strong_ptr_count, 0); | ||
unsigned int new_refcount = --metadata->strong_ptr_count; | ||
if (new_refcount == 0) { | ||
if (metadata->is_detached) { | ||
OnGCCollect(); | ||
} else if (metadata->wants_weak_jsobj && !persistent_handle_.IsEmpty()) { | ||
MakeWeak(); | ||
} | ||
} | ||
} | ||
|
||
void BaseObject::increase_refcount() { | ||
unsigned int prev_refcount = pointer_data()->strong_ptr_count++; | ||
if (prev_refcount == 0 && !persistent_handle_.IsEmpty()) | ||
persistent_handle_.ClearWeak(); | ||
} | ||
|
||
void BaseObject::DeleteMe(void* data) { | ||
BaseObject* self = static_cast<BaseObject*>(data); | ||
if (self->has_pointer_data() && self->pointer_data()->strong_ptr_count > 0) { | ||
return self->Detach(); | ||
} | ||
delete self; | ||
} | ||
|
||
bool BaseObject::IsDoneInitializing() const { | ||
return true; | ||
} | ||
|
||
Local<Object> BaseObject::WrappedObject() const { | ||
return object(); | ||
} | ||
|
||
bool BaseObject::IsRootNode() const { | ||
return !persistent_handle_.IsWeak(); | ||
} | ||
|
||
Local<FunctionTemplate> BaseObject::GetConstructorTemplate( | ||
IsolateData* isolate_data) { | ||
Local<FunctionTemplate> tmpl = isolate_data->base_object_ctor_template(); | ||
if (tmpl.IsEmpty()) { | ||
tmpl = NewFunctionTemplate(isolate_data->isolate(), nullptr); | ||
tmpl->SetClassName( | ||
FIXED_ONE_BYTE_STRING(isolate_data->isolate(), "BaseObject")); | ||
isolate_data->set_base_object_ctor_template(tmpl); | ||
} | ||
return tmpl; | ||
} | ||
|
||
bool BaseObject::IsNotIndicativeOfMemoryLeakAtExit() const { | ||
return IsWeakOrDetached(); | ||
} | ||
|
||
} // namespace node |
This file contains 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
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it's a good time now to introduce a list of realms in the env and do a
env->ForEachRealm()
in places like this, so we don't have to update everything again later other than adding more realms to the list..There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, thank you for the suggestion, it makes sense to me. Updated.