diff options
author | Jon Moss <me@jonathanmoss.me> | 2018-12-18 23:52:09 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-03-08 11:25:30 +0300 |
commit | 4697e1b0d792f50863bbbcad25a95b84e6746501 (patch) | |
tree | ec352361e56956176ca5831c092703d36cd929c6 /src/stream_base-inl.h | |
parent | 254635198ab1e6afed357c1884c1ef75eb3b8486 (diff) |
src: remove templating from StreamBase
PR-URL: https://github.com/nodejs/node/pull/25142
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src/stream_base-inl.h')
-rw-r--r-- | src/stream_base-inl.h | 142 |
1 files changed, 7 insertions, 135 deletions
diff --git a/src/stream_base-inl.h b/src/stream_base-inl.h index 9cff67cd9fe..f9a67872f90 100644 --- a/src/stream_base-inl.h +++ b/src/stream_base-inl.h @@ -273,144 +273,16 @@ inline WriteWrap* StreamBase::CreateWriteWrap( return new SimpleWriteWrap<AsyncWrap>(this, object); } -template <class Base> -void StreamBase::AddMethods(Environment* env, Local<FunctionTemplate> t) { - HandleScope scope(env->isolate()); - - enum PropertyAttribute attributes = - static_cast<PropertyAttribute>( - v8::ReadOnly | v8::DontDelete | v8::DontEnum); - - Local<Signature> signature = Signature::New(env->isolate(), t); - - Local<FunctionTemplate> get_fd_templ = - env->NewFunctionTemplate(GetFD<Base>, - signature, - v8::ConstructorBehavior::kThrow, - v8::SideEffectType::kHasNoSideEffect); - - Local<FunctionTemplate> get_external_templ = - env->NewFunctionTemplate(GetExternal<Base>, - signature, - v8::ConstructorBehavior::kThrow, - v8::SideEffectType::kHasNoSideEffect); - - Local<FunctionTemplate> get_bytes_read_templ = - env->NewFunctionTemplate(GetBytesRead<Base>, - signature, - v8::ConstructorBehavior::kThrow, - v8::SideEffectType::kHasNoSideEffect); - - Local<FunctionTemplate> get_bytes_written_templ = - env->NewFunctionTemplate(GetBytesWritten<Base>, - signature, - v8::ConstructorBehavior::kThrow, - v8::SideEffectType::kHasNoSideEffect); - - t->PrototypeTemplate()->SetAccessorProperty(env->fd_string(), - get_fd_templ, - Local<FunctionTemplate>(), - attributes); - - t->PrototypeTemplate()->SetAccessorProperty(env->external_stream_string(), - get_external_templ, - Local<FunctionTemplate>(), - attributes); - - t->PrototypeTemplate()->SetAccessorProperty(env->bytes_read_string(), - get_bytes_read_templ, - Local<FunctionTemplate>(), - attributes); - - t->PrototypeTemplate()->SetAccessorProperty(env->bytes_written_string(), - get_bytes_written_templ, - Local<FunctionTemplate>(), - attributes); - - env->SetProtoMethod(t, "readStart", JSMethod<Base, &StreamBase::ReadStartJS>); - env->SetProtoMethod(t, "readStop", JSMethod<Base, &StreamBase::ReadStopJS>); - env->SetProtoMethod(t, "shutdown", JSMethod<Base, &StreamBase::Shutdown>); - env->SetProtoMethod(t, "writev", JSMethod<Base, &StreamBase::Writev>); - env->SetProtoMethod(t, - "writeBuffer", - JSMethod<Base, &StreamBase::WriteBuffer>); - env->SetProtoMethod(t, - "writeAsciiString", - JSMethod<Base, &StreamBase::WriteString<ASCII> >); - env->SetProtoMethod(t, - "writeUtf8String", - JSMethod<Base, &StreamBase::WriteString<UTF8> >); - env->SetProtoMethod(t, - "writeUcs2String", - JSMethod<Base, &StreamBase::WriteString<UCS2> >); - env->SetProtoMethod(t, - "writeLatin1String", - JSMethod<Base, &StreamBase::WriteString<LATIN1> >); +inline void StreamBase::AttachToObject(v8::Local<v8::Object> obj) { + obj->SetAlignedPointerInInternalField(kStreamBaseField, this); } +inline StreamBase* StreamBase::FromObject(v8::Local<v8::Object> obj) { + if (obj->GetAlignedPointerFromInternalField(0) == nullptr) + return nullptr; -template <class Base> -void StreamBase::GetFD(const FunctionCallbackInfo<Value>& args) { - // Mimic implementation of StreamBase::GetFD() and UDPWrap::GetFD(). - Base* handle; - ASSIGN_OR_RETURN_UNWRAP(&handle, - args.This(), - args.GetReturnValue().Set(UV_EINVAL)); - - StreamBase* wrap = static_cast<StreamBase*>(handle); - if (!wrap->IsAlive()) - return args.GetReturnValue().Set(UV_EINVAL); - - args.GetReturnValue().Set(wrap->GetFD()); -} - -template <class Base> -void StreamBase::GetBytesRead(const FunctionCallbackInfo<Value>& args) { - Base* handle; - ASSIGN_OR_RETURN_UNWRAP(&handle, - args.This(), - args.GetReturnValue().Set(0)); - - StreamBase* wrap = static_cast<StreamBase*>(handle); - // uint64_t -> double. 53bits is enough for all real cases. - args.GetReturnValue().Set(static_cast<double>(wrap->bytes_read_)); -} - -template <class Base> -void StreamBase::GetBytesWritten(const FunctionCallbackInfo<Value>& args) { - Base* handle; - ASSIGN_OR_RETURN_UNWRAP(&handle, - args.This(), - args.GetReturnValue().Set(0)); - - StreamBase* wrap = static_cast<StreamBase*>(handle); - // uint64_t -> double. 53bits is enough for all real cases. - args.GetReturnValue().Set(static_cast<double>(wrap->bytes_written_)); -} - -template <class Base> -void StreamBase::GetExternal(const FunctionCallbackInfo<Value>& args) { - Base* handle; - ASSIGN_OR_RETURN_UNWRAP(&handle, args.This()); - - StreamBase* wrap = static_cast<StreamBase*>(handle); - Local<External> ext = External::New(args.GetIsolate(), wrap); - args.GetReturnValue().Set(ext); -} - - -template <class Base, - int (StreamBase::*Method)(const FunctionCallbackInfo<Value>& args)> -void StreamBase::JSMethod(const FunctionCallbackInfo<Value>& args) { - Base* handle; - ASSIGN_OR_RETURN_UNWRAP(&handle, args.Holder()); - - StreamBase* wrap = static_cast<StreamBase*>(handle); - if (!wrap->IsAlive()) - return args.GetReturnValue().Set(UV_EINVAL); - - AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(handle); - args.GetReturnValue().Set((wrap->*Method)(args)); + return static_cast<StreamBase*>( + obj->GetAlignedPointerFromInternalField(kStreamBaseField)); } |