Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames M Snell <jasnell@gmail.com>2020-02-26 01:37:33 +0300
committerJames M Snell <jasnell@gmail.com>2020-03-02 21:58:36 +0300
commit0fac393d263fc7e2f4f054c9d4aab0c1c3cf00c8 (patch)
treef11f3380bc282709feed38b957e38ad3adf2a4a5 /src/base_object-inl.h
parent68e36ade3de2205c583f2cc6a2d2ec192b75cc95 (diff)
src: improve handling of internal field counting
Change suggested by bnoordhuis. Improve handing of internal field counting by using enums. Helps protect against future possible breakage if field indexes are ever changed or added to. Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: https://github.com/nodejs/node/pull/31960 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Diffstat (limited to 'src/base_object-inl.h')
-rw-r--r--src/base_object-inl.h14
1 files changed, 9 insertions, 5 deletions
diff --git a/src/base_object-inl.h b/src/base_object-inl.h
index 8b2b30021a8..c9b4e1491fc 100644
--- a/src/base_object-inl.h
+++ b/src/base_object-inl.h
@@ -43,7 +43,9 @@ BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> object)
: persistent_handle_(env->isolate(), object), env_(env) {
CHECK_EQ(false, object.IsEmpty());
CHECK_GT(object->InternalFieldCount(), 0);
- object->SetAlignedPointerInInternalField(0, static_cast<void*>(this));
+ object->SetAlignedPointerInInternalField(
+ BaseObject::kSlot,
+ static_cast<void*>(this));
env->AddCleanupHook(DeleteMe, static_cast<void*>(this));
env->modify_base_object_count(1);
}
@@ -67,7 +69,7 @@ BaseObject::~BaseObject() {
{
v8::HandleScope handle_scope(env()->isolate());
- object()->SetAlignedPointerInInternalField(0, nullptr);
+ object()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
}
}
@@ -100,7 +102,8 @@ Environment* BaseObject::env() const {
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Object> obj) {
CHECK_GT(obj->InternalFieldCount(), 0);
- return static_cast<BaseObject*>(obj->GetAlignedPointerFromInternalField(0));
+ return static_cast<BaseObject*>(
+ obj->GetAlignedPointerFromInternalField(BaseObject::kSlot));
}
@@ -148,11 +151,12 @@ BaseObject::MakeLazilyInitializedJSTemplate(Environment* env) {
auto constructor = [](const v8::FunctionCallbackInfo<v8::Value>& args) {
DCHECK(args.IsConstructCall());
DCHECK_GT(args.This()->InternalFieldCount(), 0);
- args.This()->SetAlignedPointerInInternalField(0, nullptr);
+ args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
};
v8::Local<v8::FunctionTemplate> t = env->NewFunctionTemplate(constructor);
- t->InstanceTemplate()->SetInternalFieldCount(1);
+ t->InstanceTemplate()->SetInternalFieldCount(
+ BaseObject::kInternalFieldCount);
return t;
}