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>
This commit is contained in:
James M Snell committed 2020-03-02 10:58:36 -08:00
1 parent 68e36ade3d
commit 0fac393d26
40 files changed
+151 -92

No files matched your search

+12 -10
View File
@@ -176,6 +176,10 @@ void AsyncWrap::EmitAfter(Environment* env, double async_id) {
class PromiseWrap : public AsyncWrap { class PromiseWrap : public AsyncWrap {
public: public:
enum InternalFields {
kIsChainedPromiseField = AsyncWrap::kInternalFieldCount,
kInternalFieldCount
};
PromiseWrap(Environment* env, Local<Object> object, bool silent) PromiseWrap(Environment* env, Local<Object> object, bool silent)
: AsyncWrap(env, object, PROVIDER_PROMISE, kInvalidAsyncId, silent) { : AsyncWrap(env, object, PROVIDER_PROMISE, kInvalidAsyncId, silent) {
MakeWeak(); MakeWeak();
@@ -185,9 +189,6 @@ class PromiseWrap : public AsyncWrap {
SET_MEMORY_INFO_NAME(PromiseWrap) SET_MEMORY_INFO_NAME(PromiseWrap)
SET_SELF_SIZE(PromiseWrap) SET_SELF_SIZE(PromiseWrap)
static constexpr int kIsChainedPromiseField = 1;
static constexpr int kInternalFieldCount = 2;
static PromiseWrap* New(Environment* env, static PromiseWrap* New(Environment* env,
Local<Promise> promise, Local<Promise> promise,
PromiseWrap* parent_wrap, PromiseWrap* parent_wrap,
@@ -214,15 +215,16 @@ PromiseWrap* PromiseWrap::New(Environment* env,
void PromiseWrap::getIsChainedPromise(Local<String> property, void PromiseWrap::getIsChainedPromise(Local<String> property,
const PropertyCallbackInfo<Value>& info) { const PropertyCallbackInfo<Value>& info) {
info.GetReturnValue().Set( info.GetReturnValue().Set(
info.Holder()->GetInternalField(kIsChainedPromiseField)); info.Holder()->GetInternalField(PromiseWrap::kIsChainedPromiseField));
} }
static PromiseWrap* extractPromiseWrap(Local<Promise> promise) { static PromiseWrap* extractPromiseWrap(Local<Promise> promise) {
Local<Value> resource_object_value = promise->GetInternalField(0); // This check is imperfect. If the internal field is set, it should
if (resource_object_value->IsObject()) { // be an object. If it's not, we just ignore it. Ideally v8 would
return Unwrap<PromiseWrap>(resource_object_value.As<Object>()); // have had GetInternalField returning a MaybeLocal but this works
} // for now.
return nullptr; Local<Value> obj = promise->GetInternalField(0);
return obj->IsObject() ? Unwrap<PromiseWrap>(obj.As<Object>()) : nullptr;
} }
static void PromiseHook(PromiseHookType type, Local<Promise> promise, static void PromiseHook(PromiseHookType type, Local<Promise> promise,
@@ -560,7 +562,7 @@ void AsyncWrap::Initialize(Local<Object> target,
function_template->SetClassName(class_name); function_template->SetClassName(class_name);
function_template->Inherit(AsyncWrap::GetConstructorTemplate(env)); function_template->Inherit(AsyncWrap::GetConstructorTemplate(env));
auto instance_template = function_template->InstanceTemplate(); auto instance_template = function_template->InstanceTemplate();
instance_template->SetInternalFieldCount(1); instance_template->SetInternalFieldCount(AsyncWrap::kInternalFieldCount);
auto function = auto function =
function_template->GetFunction(env->context()).ToLocalChecked(); function_template->GetFunction(env->context()).ToLocalChecked();
target->Set(env->context(), class_name, function).Check(); target->Set(env->context(), class_name, function).Check();
+9 -5
View File
@@ -43,7 +43,9 @@ BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> object)
: persistent_handle_(env->isolate(), object), env_(env) { : persistent_handle_(env->isolate(), object), env_(env) {
CHECK_EQ(false, object.IsEmpty()); CHECK_EQ(false, object.IsEmpty());
CHECK_GT(object->InternalFieldCount(), 0); 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->AddCleanupHook(DeleteMe, static_cast<void*>(this));
env->modify_base_object_count(1); env->modify_base_object_count(1);
} }
@@ -67,7 +69,7 @@ BaseObject::~BaseObject() {
{ {
v8::HandleScope handle_scope(env()->isolate()); 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) { BaseObject* BaseObject::FromJSObject(v8::Local<v8::Object> obj) {
CHECK_GT(obj->InternalFieldCount(), 0); 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) { auto constructor = [](const v8::FunctionCallbackInfo<v8::Value>& args) {
DCHECK(args.IsConstructCall()); DCHECK(args.IsConstructCall());
DCHECK_GT(args.This()->InternalFieldCount(), 0); 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); v8::Local<v8::FunctionTemplate> t = env->NewFunctionTemplate(constructor);
t->InstanceTemplate()->SetInternalFieldCount(1); t->InstanceTemplate()->SetInternalFieldCount(
BaseObject::kInternalFieldCount);
return t; return t;
} }
+2
View File
@@ -36,6 +36,8 @@ class BaseObjectPtrImpl;
class BaseObject : public MemoryRetainer { class BaseObject : public MemoryRetainer {
public: public:
enum InternalFields { kSlot, kInternalFieldCount };
// Associates this object with `object`. It uses the 0th internal field for // Associates this object with `object`. It uses the 0th internal field for
// that, and in particular aborts if there is no such field. // that, and in particular aborts if there is no such field.
inline BaseObject(Environment* env, v8::Local<v8::Object> object); inline BaseObject(Environment* env, v8::Local<v8::Object> object);
+2 -1
View File
@@ -2223,7 +2223,8 @@ void Initialize(Local<Object> target,
Local<FunctionTemplate> channel_wrap = Local<FunctionTemplate> channel_wrap =
env->NewFunctionTemplate(ChannelWrap::New); env->NewFunctionTemplate(ChannelWrap::New);
channel_wrap->InstanceTemplate()->SetInternalFieldCount(1); channel_wrap->InstanceTemplate()->SetInternalFieldCount(
ChannelWrap::kInternalFieldCount);
channel_wrap->Inherit(AsyncWrap::GetConstructorTemplate(env)); channel_wrap->Inherit(AsyncWrap::GetConstructorTemplate(env));
env->SetProtoMethod(channel_wrap, "queryAny", Query<QueryAnyWrap>); env->SetProtoMethod(channel_wrap, "queryAny", Query<QueryAnyWrap>);
+2 -1
View File
@@ -97,7 +97,8 @@ void FSEventWrap::Initialize(Local<Object> target,
auto fsevent_string = FIXED_ONE_BYTE_STRING(env->isolate(), "FSEvent"); auto fsevent_string = FIXED_ONE_BYTE_STRING(env->isolate(), "FSEvent");
Local<FunctionTemplate> t = env->NewFunctionTemplate(New); Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
t->InstanceTemplate()->SetInternalFieldCount(1); t->InstanceTemplate()->SetInternalFieldCount(
FSEventWrap::kInternalFieldCount);
t->SetClassName(fsevent_string); t->SetClassName(fsevent_string);
t->Inherit(AsyncWrap::GetConstructorTemplate(env)); t->Inherit(AsyncWrap::GetConstructorTemplate(env));
+1 -1
View File
@@ -341,7 +341,7 @@ BaseObjectPtr<AsyncWrap> CreateHeapSnapshotStream(
Local<FunctionTemplate> os = FunctionTemplate::New(env->isolate()); Local<FunctionTemplate> os = FunctionTemplate::New(env->isolate());
os->Inherit(AsyncWrap::GetConstructorTemplate(env)); os->Inherit(AsyncWrap::GetConstructorTemplate(env));
Local<ObjectTemplate> ost = os->InstanceTemplate(); Local<ObjectTemplate> ost = os->InstanceTemplate();
ost->SetInternalFieldCount(StreamBase::kStreamBaseFieldCount); ost->SetInternalFieldCount(StreamBase::kInternalFieldCount);
os->SetClassName( os->SetClassName(
FIXED_ONE_BYTE_STRING(env->isolate(), "HeapSnapshotStream")); FIXED_ONE_BYTE_STRING(env->isolate(), "HeapSnapshotStream"));
StreamBase::AddMethods(env, os); StreamBase::AddMethods(env, os);
+2 -1
View File
@@ -105,7 +105,8 @@ class JSBindingsConnection : public AsyncWrap {
Local<String> class_name = ConnectionType::GetClassName(env); Local<String> class_name = ConnectionType::GetClassName(env);
Local<FunctionTemplate> tmpl = Local<FunctionTemplate> tmpl =
env->NewFunctionTemplate(JSBindingsConnection::New); env->NewFunctionTemplate(JSBindingsConnection::New);
tmpl->InstanceTemplate()->SetInternalFieldCount(1); tmpl->InstanceTemplate()->SetInternalFieldCount(
JSBindingsConnection::kInternalFieldCount);
tmpl->SetClassName(class_name); tmpl->SetClassName(class_name);
tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env)); tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env));
env->SetProtoMethod(tmpl, "dispatch", JSBindingsConnection::Dispatch); env->SetProtoMethod(tmpl, "dispatch", JSBindingsConnection::Dispatch);
+1 -1
View File
@@ -204,7 +204,7 @@ void JSStream::Initialize(Local<Object> target,
FIXED_ONE_BYTE_STRING(env->isolate(), "JSStream"); FIXED_ONE_BYTE_STRING(env->isolate(), "JSStream");
t->SetClassName(jsStreamString); t->SetClassName(jsStreamString);
t->InstanceTemplate() t->InstanceTemplate()
->SetInternalFieldCount(StreamBase::kStreamBaseFieldCount); ->SetInternalFieldCount(StreamBase::kInternalFieldCount);
t->Inherit(AsyncWrap::GetConstructorTemplate(env)); t->Inherit(AsyncWrap::GetConstructorTemplate(env));
env->SetProtoMethod(t, "finishWrite", Finish<WriteWrap>); env->SetProtoMethod(t, "finishWrite", Finish<WriteWrap>);
+2 -1
View File
@@ -1636,7 +1636,8 @@ void ModuleWrap::Initialize(Local<Object> target,
Local<FunctionTemplate> tpl = env->NewFunctionTemplate(New); Local<FunctionTemplate> tpl = env->NewFunctionTemplate(New);
tpl->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "ModuleWrap")); tpl->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "ModuleWrap"));
tpl->InstanceTemplate()->SetInternalFieldCount(1); tpl->InstanceTemplate()->SetInternalFieldCount(
ModuleWrap::kInternalFieldCount);
env->SetProtoMethod(tpl, "link", Link); env->SetProtoMethod(tpl, "link", Link);
env->SetProtoMethod(tpl, "instantiate", Instantiate); env->SetProtoMethod(tpl, "instantiate", Instantiate);
+9 -5
View File
@@ -145,7 +145,7 @@ MaybeLocal<Object> ContextifyContext::CreateDataWrapper(Environment* env) {
return MaybeLocal<Object>(); return MaybeLocal<Object>();
} }
wrapper->SetAlignedPointerInInternalField(0, this); wrapper->SetAlignedPointerInInternalField(ContextifyContext::kSlot, this);
return wrapper; return wrapper;
} }
@@ -232,7 +232,8 @@ MaybeLocal<Context> ContextifyContext::CreateV8Context(
void ContextifyContext::Init(Environment* env, Local<Object> target) { void ContextifyContext::Init(Environment* env, Local<Object> target) {
Local<FunctionTemplate> function_template = Local<FunctionTemplate> function_template =
FunctionTemplate::New(env->isolate()); FunctionTemplate::New(env->isolate());
function_template->InstanceTemplate()->SetInternalFieldCount(1); function_template->InstanceTemplate()->SetInternalFieldCount(
ContextifyContext::kInternalFieldCount);
env->set_script_data_constructor_function( env->set_script_data_constructor_function(
function_template->GetFunction(env->context()).ToLocalChecked()); function_template->GetFunction(env->context()).ToLocalChecked());
@@ -331,7 +332,8 @@ template <typename T>
ContextifyContext* ContextifyContext::Get(const PropertyCallbackInfo<T>& args) { ContextifyContext* ContextifyContext::Get(const PropertyCallbackInfo<T>& args) {
Local<Value> data = args.Data(); Local<Value> data = args.Data();
return static_cast<ContextifyContext*>( return static_cast<ContextifyContext*>(
data.As<Object>()->GetAlignedPointerFromInternalField(0)); data.As<Object>()->GetAlignedPointerFromInternalField(
ContextifyContext::kSlot));
} }
// static // static
@@ -628,7 +630,8 @@ void ContextifyScript::Init(Environment* env, Local<Object> target) {
FIXED_ONE_BYTE_STRING(env->isolate(), "ContextifyScript"); FIXED_ONE_BYTE_STRING(env->isolate(), "ContextifyScript");
Local<FunctionTemplate> script_tmpl = env->NewFunctionTemplate(New); Local<FunctionTemplate> script_tmpl = env->NewFunctionTemplate(New);
script_tmpl->InstanceTemplate()->SetInternalFieldCount(1); script_tmpl->InstanceTemplate()->SetInternalFieldCount(
ContextifyScript::kInternalFieldCount);
script_tmpl->SetClassName(class_name); script_tmpl->SetClassName(class_name);
env->SetProtoMethod(script_tmpl, "createCachedData", CreateCachedData); env->SetProtoMethod(script_tmpl, "createCachedData", CreateCachedData);
env->SetProtoMethod(script_tmpl, "runInContext", RunInContext); env->SetProtoMethod(script_tmpl, "runInContext", RunInContext);
@@ -1251,7 +1254,8 @@ void Initialize(Local<Object> target,
{ {
Local<FunctionTemplate> tpl = FunctionTemplate::New(env->isolate()); Local<FunctionTemplate> tpl = FunctionTemplate::New(env->isolate());
tpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "CompiledFnEntry")); tpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "CompiledFnEntry"));
tpl->InstanceTemplate()->SetInternalFieldCount(1); tpl->InstanceTemplate()->SetInternalFieldCount(
CompiledFnEntry::kInternalFieldCount);
env->set_compiled_fn_entry_template(tpl->InstanceTemplate()); env->set_compiled_fn_entry_template(tpl->InstanceTemplate());
} }
+1
View File
@@ -19,6 +19,7 @@ struct ContextOptions {
class ContextifyContext { class ContextifyContext {
public: public:
enum InternalFields { kSlot, kInternalFieldCount };
ContextifyContext(Environment* env, ContextifyContext(Environment* env,
v8::Local<v8::Object> sandbox_obj, v8::Local<v8::Object> sandbox_obj,
const ContextOptions& options); const ContextOptions& options);
+17 -9
View File
@@ -464,7 +464,8 @@ static T* MallocOpenSSL(size_t count) {
void SecureContext::Initialize(Environment* env, Local<Object> target) { void SecureContext::Initialize(Environment* env, Local<Object> target) {
Local<FunctionTemplate> t = env->NewFunctionTemplate(New); Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
t->InstanceTemplate()->SetInternalFieldCount(1); t->InstanceTemplate()->SetInternalFieldCount(
SecureContext::kInternalFieldCount);
Local<String> secureContextString = Local<String> secureContextString =
FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext"); FIXED_ONE_BYTE_STRING(env->isolate(), "SecureContext");
t->SetClassName(secureContextString); t->SetClassName(secureContextString);
@@ -3803,7 +3804,8 @@ EVP_PKEY* ManagedEVPPKey::get() const {
Local<Function> KeyObject::Initialize(Environment* env, Local<Object> target) { Local<Function> KeyObject::Initialize(Environment* env, Local<Object> target) {
Local<FunctionTemplate> t = env->NewFunctionTemplate(New); Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
t->InstanceTemplate()->SetInternalFieldCount(1); t->InstanceTemplate()->SetInternalFieldCount(
KeyObject::kInternalFieldCount);
env->SetProtoMethod(t, "init", Init); env->SetProtoMethod(t, "init", Init);
env->SetProtoMethodNoSideEffect(t, "getSymmetricKeySize", env->SetProtoMethodNoSideEffect(t, "getSymmetricKeySize",
@@ -4036,7 +4038,8 @@ CipherBase::CipherBase(Environment* env,
void CipherBase::Initialize(Environment* env, Local<Object> target) { void CipherBase::Initialize(Environment* env, Local<Object> target) {
Local<FunctionTemplate> t = env->NewFunctionTemplate(New); Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
t->InstanceTemplate()->SetInternalFieldCount(1); t->InstanceTemplate()->SetInternalFieldCount(
CipherBase::kInternalFieldCount);
env->SetProtoMethod(t, "init", Init); env->SetProtoMethod(t, "init", Init);
env->SetProtoMethod(t, "initiv", InitIv); env->SetProtoMethod(t, "initiv", InitIv);
@@ -4663,7 +4666,8 @@ Hmac::Hmac(Environment* env, v8::Local<v8::Object> wrap)
void Hmac::Initialize(Environment* env, Local<Object> target) { void Hmac::Initialize(Environment* env, Local<Object> target) {
Local<FunctionTemplate> t = env->NewFunctionTemplate(New); Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
t->InstanceTemplate()->SetInternalFieldCount(1); t->InstanceTemplate()->SetInternalFieldCount(
Hmac::kInternalFieldCount);
env->SetProtoMethod(t, "init", HmacInit); env->SetProtoMethod(t, "init", HmacInit);
env->SetProtoMethod(t, "update", HmacUpdate); env->SetProtoMethod(t, "update", HmacUpdate);
@@ -4789,7 +4793,8 @@ Hash::Hash(Environment* env, v8::Local<v8::Object> wrap)
void Hash::Initialize(Environment* env, Local<Object> target) { void Hash::Initialize(Environment* env, Local<Object> target) {
Local<FunctionTemplate> t = env->NewFunctionTemplate(New); Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
t->InstanceTemplate()->SetInternalFieldCount(1); t->InstanceTemplate()->SetInternalFieldCount(
Hash::kInternalFieldCount);
env->SetProtoMethod(t, "update", HashUpdate); env->SetProtoMethod(t, "update", HashUpdate);
env->SetProtoMethod(t, "digest", HashDigest); env->SetProtoMethod(t, "digest", HashDigest);
@@ -5061,7 +5066,8 @@ Sign::Sign(Environment* env, v8::Local<v8::Object> wrap) : SignBase(env, wrap) {
void Sign::Initialize(Environment* env, Local<Object> target) { void Sign::Initialize(Environment* env, Local<Object> target) {
Local<FunctionTemplate> t = env->NewFunctionTemplate(New); Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
t->InstanceTemplate()->SetInternalFieldCount(1); t->InstanceTemplate()->SetInternalFieldCount(
SignBase::kInternalFieldCount);
env->SetProtoMethod(t, "init", SignInit); env->SetProtoMethod(t, "init", SignInit);
env->SetProtoMethod(t, "update", SignUpdate); env->SetProtoMethod(t, "update", SignUpdate);
@@ -5385,7 +5391,8 @@ Verify::Verify(Environment* env, v8::Local<v8::Object> wrap) :
void Verify::Initialize(Environment* env, Local<Object> target) { void Verify::Initialize(Environment* env, Local<Object> target) {
Local<FunctionTemplate> t = env->NewFunctionTemplate(New); Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
t->InstanceTemplate()->SetInternalFieldCount(1); t->InstanceTemplate()->SetInternalFieldCount(
SignBase::kInternalFieldCount);
env->SetProtoMethod(t, "init", VerifyInit); env->SetProtoMethod(t, "init", VerifyInit);
env->SetProtoMethod(t, "update", VerifyUpdate); env->SetProtoMethod(t, "update", VerifyUpdate);
@@ -5697,7 +5704,8 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
const PropertyAttribute attributes = const PropertyAttribute attributes =
static_cast<PropertyAttribute>(ReadOnly | DontDelete); static_cast<PropertyAttribute>(ReadOnly | DontDelete);
t->InstanceTemplate()->SetInternalFieldCount(1); t->InstanceTemplate()->SetInternalFieldCount(
DiffieHellman::kInternalFieldCount);
env->SetProtoMethod(t, "generateKeys", GenerateKeys); env->SetProtoMethod(t, "generateKeys", GenerateKeys);
env->SetProtoMethod(t, "computeSecret", ComputeSecret); env->SetProtoMethod(t, "computeSecret", ComputeSecret);
@@ -6036,7 +6044,7 @@ void ECDH::Initialize(Environment* env, Local<Object> target) {
Local<FunctionTemplate> t = env->NewFunctionTemplate(New); Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
t->InstanceTemplate()->SetInternalFieldCount(1); t->InstanceTemplate()->SetInternalFieldCount(ECDH::kInternalFieldCount);
env->SetProtoMethod(t, "generateKeys", GenerateKeys); env->SetProtoMethod(t, "generateKeys", GenerateKeys);
env->SetProtoMethod(t, "computeSecret", ComputeSecret); env->SetProtoMethod(t, "computeSecret", ComputeSecret);
+1 -1
View File
@@ -358,7 +358,7 @@ void Initialize(Local<Object> target,
env->SetProtoMethod(dir, "read", DirHandle::Read); env->SetProtoMethod(dir, "read", DirHandle::Read);
env->SetProtoMethod(dir, "close", DirHandle::Close); env->SetProtoMethod(dir, "close", DirHandle::Close);
Local<ObjectTemplate> dirt = dir->InstanceTemplate(); Local<ObjectTemplate> dirt = dir->InstanceTemplate();
dirt->SetInternalFieldCount(DirHandle::kDirHandleFieldCount); dirt->SetInternalFieldCount(DirHandle::kInternalFieldCount);
Local<String> handleString = Local<String> handleString =
FIXED_ONE_BYTE_STRING(isolate, "DirHandle"); FIXED_ONE_BYTE_STRING(isolate, "DirHandle");
dir->SetClassName(handleString); dir->SetClassName(handleString);
-2
View File
@@ -12,8 +12,6 @@ namespace fs_dir {
// Needed to propagate `uv_dir_t`. // Needed to propagate `uv_dir_t`.
class DirHandle : public AsyncWrap { class DirHandle : public AsyncWrap {
public: public:
static constexpr int kDirHandleFieldCount = 1;
static DirHandle* New(Environment* env, uv_dir_t* dir); static DirHandle* New(Environment* env, uv_dir_t* dir);
~DirHandle() override; ~DirHandle() override;
+7 -5
View File
@@ -2290,7 +2290,8 @@ void Initialize(Local<Object> target,
// Create FunctionTemplate for FSReqCallback // Create FunctionTemplate for FSReqCallback
Local<FunctionTemplate> fst = env->NewFunctionTemplate(NewFSReqCallback); Local<FunctionTemplate> fst = env->NewFunctionTemplate(NewFSReqCallback);
fst->InstanceTemplate()->SetInternalFieldCount(1); fst->InstanceTemplate()->SetInternalFieldCount(
FSReqBase::kInternalFieldCount);
fst->Inherit(AsyncWrap::GetConstructorTemplate(env)); fst->Inherit(AsyncWrap::GetConstructorTemplate(env));
Local<String> wrapString = Local<String> wrapString =
FIXED_ONE_BYTE_STRING(isolate, "FSReqCallback"); FIXED_ONE_BYTE_STRING(isolate, "FSReqCallback");
@@ -2303,7 +2304,8 @@ void Initialize(Local<Object> target,
// Create FunctionTemplate for FileHandleReadWrap. There’s no need // Create FunctionTemplate for FileHandleReadWrap. There’s no need
// to do anything in the constructor, so we only store the instance template. // to do anything in the constructor, so we only store the instance template.
Local<FunctionTemplate> fh_rw = FunctionTemplate::New(isolate); Local<FunctionTemplate> fh_rw = FunctionTemplate::New(isolate);
fh_rw->InstanceTemplate()->SetInternalFieldCount(1); fh_rw->InstanceTemplate()->SetInternalFieldCount(
FSReqBase::kInternalFieldCount);
fh_rw->Inherit(AsyncWrap::GetConstructorTemplate(env)); fh_rw->Inherit(AsyncWrap::GetConstructorTemplate(env));
Local<String> fhWrapString = Local<String> fhWrapString =
FIXED_ONE_BYTE_STRING(isolate, "FileHandleReqWrap"); FIXED_ONE_BYTE_STRING(isolate, "FileHandleReqWrap");
@@ -2318,7 +2320,7 @@ void Initialize(Local<Object> target,
FIXED_ONE_BYTE_STRING(isolate, "FSReqPromise"); FIXED_ONE_BYTE_STRING(isolate, "FSReqPromise");
fpt->SetClassName(promiseString); fpt->SetClassName(promiseString);
Local<ObjectTemplate> fpo = fpt->InstanceTemplate(); Local<ObjectTemplate> fpo = fpt->InstanceTemplate();
fpo->SetInternalFieldCount(1); fpo->SetInternalFieldCount(FSReqBase::kInternalFieldCount);
env->set_fsreqpromise_constructor_template(fpo); env->set_fsreqpromise_constructor_template(fpo);
// Create FunctionTemplate for FileHandle // Create FunctionTemplate for FileHandle
@@ -2327,7 +2329,7 @@ void Initialize(Local<Object> target,
env->SetProtoMethod(fd, "close", FileHandle::Close); env->SetProtoMethod(fd, "close", FileHandle::Close);
env->SetProtoMethod(fd, "releaseFD", FileHandle::ReleaseFD); env->SetProtoMethod(fd, "releaseFD", FileHandle::ReleaseFD);
Local<ObjectTemplate> fdt = fd->InstanceTemplate(); Local<ObjectTemplate> fdt = fd->InstanceTemplate();
fdt->SetInternalFieldCount(StreamBase::kStreamBaseFieldCount); fdt->SetInternalFieldCount(StreamBase::kInternalFieldCount);
Local<String> handleString = Local<String> handleString =
FIXED_ONE_BYTE_STRING(isolate, "FileHandle"); FIXED_ONE_BYTE_STRING(isolate, "FileHandle");
fd->SetClassName(handleString); fd->SetClassName(handleString);
@@ -2344,7 +2346,7 @@ void Initialize(Local<Object> target,
"FileHandleCloseReq")); "FileHandleCloseReq"));
fdclose->Inherit(AsyncWrap::GetConstructorTemplate(env)); fdclose->Inherit(AsyncWrap::GetConstructorTemplate(env));
Local<ObjectTemplate> fdcloset = fdclose->InstanceTemplate(); Local<ObjectTemplate> fdcloset = fdclose->InstanceTemplate();
fdcloset->SetInternalFieldCount(1); fdcloset->SetInternalFieldCount(FSReqBase::kInternalFieldCount);
env->set_fdclose_constructor_template(fdcloset); env->set_fdclose_constructor_template(fdcloset);
Local<Symbol> use_promises_symbol = Local<Symbol> use_promises_symbol =
+5 -4
View File
@@ -3025,14 +3025,14 @@ void Initialize(Local<Object> target,
ping->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Ping")); ping->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Ping"));
ping->Inherit(AsyncWrap::GetConstructorTemplate(env)); ping->Inherit(AsyncWrap::GetConstructorTemplate(env));
Local<ObjectTemplate> pingt = ping->InstanceTemplate(); Local<ObjectTemplate> pingt = ping->InstanceTemplate();
pingt->SetInternalFieldCount(1); pingt->SetInternalFieldCount(Http2Session::Http2Ping::kInternalFieldCount);
env->set_http2ping_constructor_template(pingt); env->set_http2ping_constructor_template(pingt);
Local<FunctionTemplate> setting = FunctionTemplate::New(env->isolate()); Local<FunctionTemplate> setting = FunctionTemplate::New(env->isolate());
setting->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Setting")); setting->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Setting"));
setting->Inherit(AsyncWrap::GetConstructorTemplate(env)); setting->Inherit(AsyncWrap::GetConstructorTemplate(env));
Local<ObjectTemplate> settingt = setting->InstanceTemplate(); Local<ObjectTemplate> settingt = setting->InstanceTemplate();
settingt->SetInternalFieldCount(1); settingt->SetInternalFieldCount(AsyncWrap::kInternalFieldCount);
env->set_http2settings_constructor_template(settingt); env->set_http2settings_constructor_template(settingt);
Local<FunctionTemplate> stream = FunctionTemplate::New(env->isolate()); Local<FunctionTemplate> stream = FunctionTemplate::New(env->isolate());
@@ -3049,7 +3049,7 @@ void Initialize(Local<Object> target,
stream->Inherit(AsyncWrap::GetConstructorTemplate(env)); stream->Inherit(AsyncWrap::GetConstructorTemplate(env));
StreamBase::AddMethods(env, stream); StreamBase::AddMethods(env, stream);
Local<ObjectTemplate> streamt = stream->InstanceTemplate(); Local<ObjectTemplate> streamt = stream->InstanceTemplate();
streamt->SetInternalFieldCount(StreamBase::kStreamBaseFieldCount); streamt->SetInternalFieldCount(StreamBase::kInternalFieldCount);
env->set_http2stream_constructor_template(streamt); env->set_http2stream_constructor_template(streamt);
target->Set(context, target->Set(context,
FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Stream"), FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Stream"),
@@ -3058,7 +3058,8 @@ void Initialize(Local<Object> target,
Local<FunctionTemplate> session = Local<FunctionTemplate> session =
env->NewFunctionTemplate(Http2Session::New); env->NewFunctionTemplate(Http2Session::New);
session->SetClassName(http2SessionClassName); session->SetClassName(http2SessionClassName);
session->InstanceTemplate()->SetInternalFieldCount(1); session->InstanceTemplate()->SetInternalFieldCount(
Http2Session::kInternalFieldCount);
session->Inherit(AsyncWrap::GetConstructorTemplate(env)); session->Inherit(AsyncWrap::GetConstructorTemplate(env));
env->SetProtoMethod(session, "origin", Http2Session::Origin); env->SetProtoMethod(session, "origin", Http2Session::Origin);
env->SetProtoMethod(session, "altsvc", Http2Session::AltSvc); env->SetProtoMethod(session, "altsvc", Http2Session::AltSvc);
+1 -1
View File
@@ -873,7 +873,7 @@ void InitializeHttpParser(Local<Object> target,
void* priv) { void* priv) {
Environment* env = Environment::GetCurrent(context); Environment* env = Environment::GetCurrent(context);
Local<FunctionTemplate> t = env->NewFunctionTemplate(Parser::New); Local<FunctionTemplate> t = env->NewFunctionTemplate(Parser::New);
t->InstanceTemplate()->SetInternalFieldCount(1); t->InstanceTemplate()->SetInternalFieldCount(Parser::kInternalFieldCount);
t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "HTTPParser")); t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "HTTPParser"));
t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "REQUEST"), t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "REQUEST"),
+1 -1
View File
@@ -172,7 +172,7 @@ class ConverterObject : public BaseObject, Converter {
HandleScope scope(env->isolate()); HandleScope scope(env->isolate());
Local<ObjectTemplate> t = ObjectTemplate::New(env->isolate()); Local<ObjectTemplate> t = ObjectTemplate::New(env->isolate());
t->SetInternalFieldCount(1); t->SetInternalFieldCount(ConverterObject::kInternalFieldCount);
Local<Object> obj; Local<Object> obj;
if (!t->NewInstance(env->context()).ToLocal(&obj)) return; if (!t->NewInstance(env->context()).ToLocal(&obj)) return;
+2 -1
View File
@@ -935,7 +935,8 @@ Local<FunctionTemplate> GetMessagePortConstructorTemplate(Environment* env) {
{ {
Local<FunctionTemplate> m = env->NewFunctionTemplate(MessagePort::New); Local<FunctionTemplate> m = env->NewFunctionTemplate(MessagePort::New);
m->SetClassName(env->message_port_constructor_string()); m->SetClassName(env->message_port_constructor_string());
m->InstanceTemplate()->SetInternalFieldCount(1); m->InstanceTemplate()->SetInternalFieldCount(
MessagePort::kInternalFieldCount);
m->Inherit(HandleWrap::GetConstructorTemplate(env)); m->Inherit(HandleWrap::GetConstructorTemplate(env));
env->SetProtoMethod(m, "postMessage", MessagePort::PostMessage); env->SetProtoMethod(m, "postMessage", MessagePort::PostMessage);
+2 -1
View File
@@ -640,7 +640,8 @@ void Initialize(Local<Object> target,
Local<FunctionTemplate> eldh = Local<FunctionTemplate> eldh =
env->NewFunctionTemplate(ELDHistogramNew); env->NewFunctionTemplate(ELDHistogramNew);
eldh->SetClassName(eldh_classname); eldh->SetClassName(eldh_classname);
eldh->InstanceTemplate()->SetInternalFieldCount(1); eldh->InstanceTemplate()->SetInternalFieldCount(
ELDHistogram::kInternalFieldCount);
env->SetProtoMethod(eldh, "exceeds", ELDHistogramExceeds); env->SetProtoMethod(eldh, "exceeds", ELDHistogramExceeds);
env->SetProtoMethod(eldh, "min", ELDHistogramMin); env->SetProtoMethod(eldh, "min", ELDHistogramMin);
env->SetProtoMethod(eldh, "max", ELDHistogramMax); env->SetProtoMethod(eldh, "max", ELDHistogramMax);
+4 -2
View File
@@ -451,7 +451,8 @@ void Initialize(Local<Object> target,
Local<FunctionTemplate> ser = Local<FunctionTemplate> ser =
env->NewFunctionTemplate(SerializerContext::New); env->NewFunctionTemplate(SerializerContext::New);
ser->InstanceTemplate()->SetInternalFieldCount(1); ser->InstanceTemplate()->SetInternalFieldCount(
SerializerContext::kInternalFieldCount);
env->SetProtoMethod(ser, "writeHeader", SerializerContext::WriteHeader); env->SetProtoMethod(ser, "writeHeader", SerializerContext::WriteHeader);
env->SetProtoMethod(ser, "writeValue", SerializerContext::WriteValue); env->SetProtoMethod(ser, "writeValue", SerializerContext::WriteValue);
@@ -477,7 +478,8 @@ void Initialize(Local<Object> target,
Local<FunctionTemplate> des = Local<FunctionTemplate> des =
env->NewFunctionTemplate(DeserializerContext::New); env->NewFunctionTemplate(DeserializerContext::New);
des->InstanceTemplate()->SetInternalFieldCount(1); des->InstanceTemplate()->SetInternalFieldCount(
DeserializerContext::kInternalFieldCount);
env->SetProtoMethod(des, "readHeader", DeserializerContext::ReadHeader); env->SetProtoMethod(des, "readHeader", DeserializerContext::ReadHeader);
env->SetProtoMethod(des, "readValue", DeserializerContext::ReadValue); env->SetProtoMethod(des, "readValue", DeserializerContext::ReadValue);
+2 -1
View File
@@ -47,7 +47,8 @@ void StatWatcher::Initialize(Environment* env, Local<Object> target) {
HandleScope scope(env->isolate()); HandleScope scope(env->isolate());
Local<FunctionTemplate> t = env->NewFunctionTemplate(StatWatcher::New); Local<FunctionTemplate> t = env->NewFunctionTemplate(StatWatcher::New);
t->InstanceTemplate()->SetInternalFieldCount(1); t->InstanceTemplate()->SetInternalFieldCount(
StatWatcher::kInternalFieldCount);
Local<String> statWatcherString = Local<String> statWatcherString =
FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher"); FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher");
t->SetClassName(statWatcherString); t->SetClassName(statWatcherString);
+2 -1
View File
@@ -129,7 +129,8 @@ void NodeCategorySet::Initialize(Local<Object> target,
Local<FunctionTemplate> category_set = Local<FunctionTemplate> category_set =
env->NewFunctionTemplate(NodeCategorySet::New); env->NewFunctionTemplate(NodeCategorySet::New);
category_set->InstanceTemplate()->SetInternalFieldCount(1); category_set->InstanceTemplate()->SetInternalFieldCount(
NodeCategorySet::kInternalFieldCount);
env->SetProtoMethod(category_set, "enable", NodeCategorySet::Enable); env->SetProtoMethod(category_set, "enable", NodeCategorySet::Enable);
env->SetProtoMethod(category_set, "disable", NodeCategorySet::Disable); env->SetProtoMethod(category_set, "disable", NodeCategorySet::Disable);
+2 -1
View File
@@ -323,7 +323,8 @@ void Initialize(Local<Object> target,
FIXED_ONE_BYTE_STRING(env->isolate(), "WeakReference"); FIXED_ONE_BYTE_STRING(env->isolate(), "WeakReference");
Local<FunctionTemplate> weak_ref = Local<FunctionTemplate> weak_ref =
env->NewFunctionTemplate(WeakReference::New); env->NewFunctionTemplate(WeakReference::New);
weak_ref->InstanceTemplate()->SetInternalFieldCount(1); weak_ref->InstanceTemplate()->SetInternalFieldCount(
WeakReference::kInternalFieldCount);
weak_ref->SetClassName(weak_ref_string); weak_ref->SetClassName(weak_ref_string);
env->SetProtoMethod(weak_ref, "get", WeakReference::Get); env->SetProtoMethod(weak_ref, "get", WeakReference::Get);
env->SetProtoMethod(weak_ref, "incRef", WeakReference::IncRef); env->SetProtoMethod(weak_ref, "incRef", WeakReference::IncRef);
+1 -1
View File
@@ -1810,7 +1810,7 @@ static void Initialize(Local<Object> target,
Local<FunctionTemplate> tmpl = env->NewFunctionTemplate(WASI::New); Local<FunctionTemplate> tmpl = env->NewFunctionTemplate(WASI::New);
auto wasi_wrap_string = FIXED_ONE_BYTE_STRING(env->isolate(), "WASI"); auto wasi_wrap_string = FIXED_ONE_BYTE_STRING(env->isolate(), "WASI");
tmpl->InstanceTemplate()->SetInternalFieldCount(1); tmpl->InstanceTemplate()->SetInternalFieldCount(WASI::kInternalFieldCount);
tmpl->SetClassName(wasi_wrap_string); tmpl->SetClassName(wasi_wrap_string);
env->SetProtoMethod(tmpl, "args_get", WASI::ArgsGet); env->SetProtoMethod(tmpl, "args_get", WASI::ArgsGet);
+2 -1
View File
@@ -121,7 +121,8 @@ SignalPropagation SigintWatchdog::HandleSigint() {
void TraceSigintWatchdog::Init(Environment* env, Local<Object> target) { void TraceSigintWatchdog::Init(Environment* env, Local<Object> target) {
Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New); Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
constructor->InstanceTemplate()->SetInternalFieldCount(1); constructor->InstanceTemplate()->SetInternalFieldCount(
TraceSigintWatchdog::kInternalFieldCount);
Local<v8::String> js_sigint_watch_dog = Local<v8::String> js_sigint_watch_dog =
FIXED_ONE_BYTE_STRING(env->isolate(), "TraceSigintWatchdog"); FIXED_ONE_BYTE_STRING(env->isolate(), "TraceSigintWatchdog");
constructor->SetClassName(js_sigint_watch_dog); constructor->SetClassName(js_sigint_watch_dog);
+4 -2
View File
@@ -776,7 +776,8 @@ void InitWorker(Local<Object> target,
{ {
Local<FunctionTemplate> w = env->NewFunctionTemplate(Worker::New); Local<FunctionTemplate> w = env->NewFunctionTemplate(Worker::New);
w->InstanceTemplate()->SetInternalFieldCount(1); w->InstanceTemplate()->SetInternalFieldCount(
Worker::kInternalFieldCount);
w->Inherit(AsyncWrap::GetConstructorTemplate(env)); w->Inherit(AsyncWrap::GetConstructorTemplate(env));
env->SetProtoMethod(w, "startThread", Worker::StartThread); env->SetProtoMethod(w, "startThread", Worker::StartThread);
@@ -797,7 +798,8 @@ void InitWorker(Local<Object> target,
{ {
Local<FunctionTemplate> wst = FunctionTemplate::New(env->isolate()); Local<FunctionTemplate> wst = FunctionTemplate::New(env->isolate());
wst->InstanceTemplate()->SetInternalFieldCount(1); wst->InstanceTemplate()->SetInternalFieldCount(
WorkerHeapSnapshotTaker::kInternalFieldCount);
wst->Inherit(AsyncWrap::GetConstructorTemplate(env)); wst->Inherit(AsyncWrap::GetConstructorTemplate(env));
Local<String> wst_string = Local<String> wst_string =
+2 -1
View File
@@ -1216,7 +1216,8 @@ struct MakeClass {
static void Make(Environment* env, Local<Object> target, const char* name) { static void Make(Environment* env, Local<Object> target, const char* name) {
Local<FunctionTemplate> z = env->NewFunctionTemplate(Stream::New); Local<FunctionTemplate> z = env->NewFunctionTemplate(Stream::New);
z->InstanceTemplate()->SetInternalFieldCount(1); z->InstanceTemplate()->SetInternalFieldCount(
Stream::kInternalFieldCount);
z->Inherit(AsyncWrap::GetConstructorTemplate(env)); z->Inherit(AsyncWrap::GetConstructorTemplate(env));
env->SetProtoMethod(z, "write", Stream::template Write<true>); env->SetProtoMethod(z, "write", Stream::template Write<true>);
+1 -1
View File
@@ -74,7 +74,7 @@ void PipeWrap::Initialize(Local<Object> target,
Local<String> pipeString = FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe"); Local<String> pipeString = FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe");
t->SetClassName(pipeString); t->SetClassName(pipeString);
t->InstanceTemplate() t->InstanceTemplate()
->SetInternalFieldCount(StreamBase::kStreamBaseFieldCount); ->SetInternalFieldCount(StreamBase::kInternalFieldCount);
t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env)); t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
+2 -1
View File
@@ -52,7 +52,8 @@ class ProcessWrap : public HandleWrap {
void* priv) { void* priv) {
Environment* env = Environment::GetCurrent(context); Environment* env = Environment::GetCurrent(context);
Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New); Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
constructor->InstanceTemplate()->SetInternalFieldCount(1); constructor->InstanceTemplate()->SetInternalFieldCount(
ProcessWrap::kInternalFieldCount);
Local<String> processString = Local<String> processString =
FIXED_ONE_BYTE_STRING(env->isolate(), "Process"); FIXED_ONE_BYTE_STRING(env->isolate(), "Process");
constructor->SetClassName(processString); constructor->SetClassName(processString);
+2 -1
View File
@@ -53,7 +53,8 @@ class SignalWrap : public HandleWrap {
void* priv) { void* priv) {
Environment* env = Environment::GetCurrent(context); Environment* env = Environment::GetCurrent(context);
Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New); Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
constructor->InstanceTemplate()->SetInternalFieldCount(1); constructor->InstanceTemplate()->SetInternalFieldCount(
SignalWrap::kInternalFieldCount);
Local<String> signalString = Local<String> signalString =
FIXED_ONE_BYTE_STRING(env->isolate(), "Signal"); FIXED_ONE_BYTE_STRING(env->isolate(), "Signal");
constructor->SetClassName(signalString); constructor->SetClassName(signalString);
+14 -8
View File
@@ -23,18 +23,22 @@ using v8::String;
using v8::Value; using v8::Value;
inline void StreamReq::AttachToObject(v8::Local<v8::Object> req_wrap_obj) { inline void StreamReq::AttachToObject(v8::Local<v8::Object> req_wrap_obj) {
CHECK_EQ(req_wrap_obj->GetAlignedPointerFromInternalField(kStreamReqField), CHECK_EQ(req_wrap_obj->GetAlignedPointerFromInternalField(
StreamReq::kStreamReqField),
nullptr); nullptr);
req_wrap_obj->SetAlignedPointerInInternalField(kStreamReqField, this); req_wrap_obj->SetAlignedPointerInInternalField(
StreamReq::kStreamReqField, this);
} }
inline StreamReq* StreamReq::FromObject(v8::Local<v8::Object> req_wrap_obj) { inline StreamReq* StreamReq::FromObject(v8::Local<v8::Object> req_wrap_obj) {
return static_cast<StreamReq*>( return static_cast<StreamReq*>(
req_wrap_obj->GetAlignedPointerFromInternalField(kStreamReqField)); req_wrap_obj->GetAlignedPointerFromInternalField(
StreamReq::kStreamReqField));
} }
inline void StreamReq::Dispose() { inline void StreamReq::Dispose() {
object()->SetAlignedPointerInInternalField(kStreamReqField, nullptr); object()->SetAlignedPointerInInternalField(
StreamReq::kStreamReqField, nullptr);
delete this; delete this;
} }
@@ -261,15 +265,17 @@ inline WriteWrap* StreamBase::CreateWriteWrap(
} }
inline void StreamBase::AttachToObject(v8::Local<v8::Object> obj) { inline void StreamBase::AttachToObject(v8::Local<v8::Object> obj) {
obj->SetAlignedPointerInInternalField(kStreamBaseField, this); obj->SetAlignedPointerInInternalField(
StreamBase::kStreamBaseField, this);
} }
inline StreamBase* StreamBase::FromObject(v8::Local<v8::Object> obj) { inline StreamBase* StreamBase::FromObject(v8::Local<v8::Object> obj) {
if (obj->GetAlignedPointerFromInternalField(0) == nullptr) if (obj->GetAlignedPointerFromInternalField(StreamBase::kSlot) == nullptr)
return nullptr; return nullptr;
return static_cast<StreamBase*>( return static_cast<StreamBase*>(
obj->GetAlignedPointerFromInternalField(kStreamBaseField)); obj->GetAlignedPointerFromInternalField(
StreamBase::kStreamBaseField));
} }
@@ -304,7 +310,7 @@ inline void StreamReq::Done(int status, const char* error_str) {
inline void StreamReq::ResetObject(v8::Local<v8::Object> obj) { inline void StreamReq::ResetObject(v8::Local<v8::Object> obj) {
DCHECK_GT(obj->InternalFieldCount(), StreamReq::kStreamReqField); DCHECK_GT(obj->InternalFieldCount(), StreamReq::kStreamReqField);
obj->SetAlignedPointerInInternalField(0, nullptr); // BaseObject field. obj->SetAlignedPointerInInternalField(StreamReq::kSlot, nullptr);
obj->SetAlignedPointerInInternalField(StreamReq::kStreamReqField, nullptr); obj->SetAlignedPointerInInternalField(StreamReq::kStreamReqField, nullptr);
} }
+7 -3
View File
@@ -340,7 +340,8 @@ MaybeLocal<Value> StreamBase::CallJSOnreadMethod(ssize_t nread,
AsyncWrap* wrap = GetAsyncWrap(); AsyncWrap* wrap = GetAsyncWrap();
CHECK_NOT_NULL(wrap); CHECK_NOT_NULL(wrap);
Local<Value> onread = wrap->object()->GetInternalField(kOnReadFunctionField); Local<Value> onread = wrap->object()->GetInternalField(
StreamBase::kOnReadFunctionField);
CHECK(onread->IsFunction()); CHECK(onread->IsFunction());
return wrap->MakeCallback(onread.As<Function>(), arraysize(argv), argv); return wrap->MakeCallback(onread.As<Function>(), arraysize(argv), argv);
} }
@@ -409,8 +410,11 @@ void StreamBase::AddMethods(Environment* env, Local<FunctionTemplate> t) {
True(env->isolate())); True(env->isolate()));
t->PrototypeTemplate()->SetAccessor( t->PrototypeTemplate()->SetAccessor(
FIXED_ONE_BYTE_STRING(env->isolate(), "onread"), FIXED_ONE_BYTE_STRING(env->isolate(), "onread"),
BaseObject::InternalFieldGet<kOnReadFunctionField>, BaseObject::InternalFieldGet<
BaseObject::InternalFieldSet<kOnReadFunctionField, &Value::IsFunction>); StreamBase::kOnReadFunctionField>,
BaseObject::InternalFieldSet<
StreamBase::kOnReadFunctionField,
&Value::IsFunction>);
} }
void StreamBase::GetFD(const FunctionCallbackInfo<Value>& args) { void StreamBase::GetFD(const FunctionCallbackInfo<Value>& args) {
+17 -5
View File
@@ -29,7 +29,14 @@ using JSMethodFunction = void(const v8::FunctionCallbackInfo<v8::Value>& args);
class StreamReq { class StreamReq {
public: public:
static constexpr int kStreamReqField = 1; // The kSlot internal field here mirrors BaseObject::InternalFields::kSlot
// here because instances derived from StreamReq will also derive from
// BaseObject, and the slots are used for the identical purpose.
enum InternalFields {
kSlot = BaseObject::kSlot,
kStreamReqField = BaseObject::kInternalFieldCount,
kInternalFieldCount
};
explicit StreamReq(StreamBase* stream, explicit StreamReq(StreamBase* stream,
v8::Local<v8::Object> req_wrap_obj) : stream_(stream) { v8::Local<v8::Object> req_wrap_obj) : stream_(stream) {
@@ -275,10 +282,15 @@ class StreamResource {
class StreamBase : public StreamResource { class StreamBase : public StreamResource {
public: public:
// 0 is reserved for the BaseObject pointer. // The kSlot field here mirrors that of BaseObject::InternalFields::kSlot
static constexpr int kStreamBaseField = 1; // because instances deriving from StreamBase generally also derived from
static constexpr int kOnReadFunctionField = 2; // BaseObject (it's possible for it not to, however).
static constexpr int kStreamBaseFieldCount = 3; enum InternalFields {
kSlot = BaseObject::kSlot,
kStreamBaseField = BaseObject::kInternalFieldCount,
kOnReadFunctionField,
kInternalFieldCount
};
static void AddMethods(Environment* env, static void AddMethods(Environment* env,
v8::Local<v8::FunctionTemplate> target); v8::Local<v8::FunctionTemplate> target);
+2 -1
View File
@@ -298,7 +298,8 @@ void InitializeStreamPipe(Local<Object> target,
env->SetProtoMethod(pipe, "pendingWrites", StreamPipe::PendingWrites); env->SetProtoMethod(pipe, "pendingWrites", StreamPipe::PendingWrites);
pipe->Inherit(AsyncWrap::GetConstructorTemplate(env)); pipe->Inherit(AsyncWrap::GetConstructorTemplate(env));
pipe->SetClassName(stream_pipe_string); pipe->SetClassName(stream_pipe_string);
pipe->InstanceTemplate()->SetInternalFieldCount(1); pipe->InstanceTemplate()->SetInternalFieldCount(
StreamPipe::kInternalFieldCount);
target target
->Set(context, stream_pipe_string, ->Set(context, stream_pipe_string,
pipe->GetFunction(context).ToLocalChecked()) pipe->GetFunction(context).ToLocalChecked())
+4 -4
View File
@@ -64,8 +64,7 @@ void LibuvStreamWrap::Initialize(Local<Object> target,
}; };
Local<FunctionTemplate> sw = Local<FunctionTemplate> sw =
FunctionTemplate::New(env->isolate(), is_construct_call_callback); FunctionTemplate::New(env->isolate(), is_construct_call_callback);
sw->InstanceTemplate()->SetInternalFieldCount( sw->InstanceTemplate()->SetInternalFieldCount(StreamReq::kInternalFieldCount);
StreamReq::kStreamReqField + 1 + 3);
Local<String> wrapString = Local<String> wrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap"); FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap");
sw->SetClassName(wrapString); sw->SetClassName(wrapString);
@@ -94,7 +93,8 @@ void LibuvStreamWrap::Initialize(Local<Object> target,
Local<FunctionTemplate> ww = Local<FunctionTemplate> ww =
FunctionTemplate::New(env->isolate(), is_construct_call_callback); FunctionTemplate::New(env->isolate(), is_construct_call_callback);
ww->InstanceTemplate()->SetInternalFieldCount(StreamReq::kStreamReqField + 1); ww->InstanceTemplate()->SetInternalFieldCount(
StreamReq::kInternalFieldCount);
Local<String> writeWrapString = Local<String> writeWrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap"); FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap");
ww->SetClassName(writeWrapString); ww->SetClassName(writeWrapString);
@@ -136,7 +136,7 @@ Local<FunctionTemplate> LibuvStreamWrap::GetConstructorTemplate(
FIXED_ONE_BYTE_STRING(env->isolate(), "LibuvStreamWrap")); FIXED_ONE_BYTE_STRING(env->isolate(), "LibuvStreamWrap"));
tmpl->Inherit(HandleWrap::GetConstructorTemplate(env)); tmpl->Inherit(HandleWrap::GetConstructorTemplate(env));
tmpl->InstanceTemplate()->SetInternalFieldCount( tmpl->InstanceTemplate()->SetInternalFieldCount(
StreamBase::kStreamBaseFieldCount); StreamBase::kInternalFieldCount);
Local<FunctionTemplate> get_write_queue_size = Local<FunctionTemplate> get_write_queue_size =
FunctionTemplate::New(env->isolate(), FunctionTemplate::New(env->isolate(),
GetWriteQueueSize, GetWriteQueueSize,
+1 -2
View File
@@ -77,8 +77,7 @@ void TCPWrap::Initialize(Local<Object> target,
Local<FunctionTemplate> t = env->NewFunctionTemplate(New); Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
Local<String> tcpString = FIXED_ONE_BYTE_STRING(env->isolate(), "TCP"); Local<String> tcpString = FIXED_ONE_BYTE_STRING(env->isolate(), "TCP");
t->SetClassName(tcpString); t->SetClassName(tcpString);
t->InstanceTemplate() t->InstanceTemplate()->SetInternalFieldCount(StreamBase::kInternalFieldCount);
->SetInternalFieldCount(StreamBase::kStreamBaseFieldCount);
// Init properties // Init properties
t->InstanceTemplate()->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "reading"), t->InstanceTemplate()->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "reading"),
+1 -2
View File
@@ -1269,8 +1269,7 @@ void TLSWrap::Initialize(Local<Object> target,
Local<String> tlsWrapString = Local<String> tlsWrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "TLSWrap"); FIXED_ONE_BYTE_STRING(env->isolate(), "TLSWrap");
t->SetClassName(tlsWrapString); t->SetClassName(tlsWrapString);
t->InstanceTemplate() t->InstanceTemplate()->SetInternalFieldCount(StreamBase::kInternalFieldCount);
->SetInternalFieldCount(StreamBase::kStreamBaseFieldCount);
Local<FunctionTemplate> get_write_queue_size = Local<FunctionTemplate> get_write_queue_size =
FunctionTemplate::New(env->isolate(), FunctionTemplate::New(env->isolate(),
+1 -2
View File
@@ -51,8 +51,7 @@ void TTYWrap::Initialize(Local<Object> target,
Local<FunctionTemplate> t = env->NewFunctionTemplate(New); Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
t->SetClassName(ttyString); t->SetClassName(ttyString);
t->InstanceTemplate() t->InstanceTemplate()->SetInternalFieldCount(StreamBase::kInternalFieldCount);
->SetInternalFieldCount(StreamBase::kStreamBaseFieldCount);
t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env)); t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
env->SetProtoMethodNoSideEffect(t, "getWindowSize", TTYWrap::GetWindowSize); env->SetProtoMethodNoSideEffect(t, "getWindowSize", TTYWrap::GetWindowSize);
+1 -1
View File
@@ -91,7 +91,7 @@ void UDPWrap::Initialize(Local<Object> target,
Environment* env = Environment::GetCurrent(context); Environment* env = Environment::GetCurrent(context);
Local<FunctionTemplate> t = env->NewFunctionTemplate(New); Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
t->InstanceTemplate()->SetInternalFieldCount(1); t->InstanceTemplate()->SetInternalFieldCount(UDPWrap::kInternalFieldCount);
Local<String> udpString = Local<String> udpString =
FIXED_ONE_BYTE_STRING(env->isolate(), "UDP"); FIXED_ONE_BYTE_STRING(env->isolate(), "UDP");
t->SetClassName(udpString); t->SetClassName(udpString);