From 4697e1b0d792f50863bbbcad25a95b84e6746501 Mon Sep 17 00:00:00 2001 From: Jon Moss Date: Tue, 18 Dec 2018 15:52:09 -0500 Subject: src: remove templating from StreamBase PR-URL: https://github.com/nodejs/node/pull/25142 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- src/stream_base-inl.h | 142 +++----------------------------------------------- 1 file changed, 7 insertions(+), 135 deletions(-) (limited to 'src/stream_base-inl.h') diff --git a/src/stream_base-inl.h b/src/stream_base-inl.h index 9cff67cd9f..f9a67872f9 100644 --- a/src/stream_base-inl.h +++ b/src/stream_base-inl.h @@ -273,144 +273,16 @@ inline WriteWrap* StreamBase::CreateWriteWrap( return new SimpleWriteWrap(this, object); } -template -void StreamBase::AddMethods(Environment* env, Local t) { - HandleScope scope(env->isolate()); - - enum PropertyAttribute attributes = - static_cast( - v8::ReadOnly | v8::DontDelete | v8::DontEnum); - - Local signature = Signature::New(env->isolate(), t); - - Local get_fd_templ = - env->NewFunctionTemplate(GetFD, - signature, - v8::ConstructorBehavior::kThrow, - v8::SideEffectType::kHasNoSideEffect); - - Local get_external_templ = - env->NewFunctionTemplate(GetExternal, - signature, - v8::ConstructorBehavior::kThrow, - v8::SideEffectType::kHasNoSideEffect); - - Local get_bytes_read_templ = - env->NewFunctionTemplate(GetBytesRead, - signature, - v8::ConstructorBehavior::kThrow, - v8::SideEffectType::kHasNoSideEffect); - - Local get_bytes_written_templ = - env->NewFunctionTemplate(GetBytesWritten, - signature, - v8::ConstructorBehavior::kThrow, - v8::SideEffectType::kHasNoSideEffect); - - t->PrototypeTemplate()->SetAccessorProperty(env->fd_string(), - get_fd_templ, - Local(), - attributes); - - t->PrototypeTemplate()->SetAccessorProperty(env->external_stream_string(), - get_external_templ, - Local(), - attributes); - - t->PrototypeTemplate()->SetAccessorProperty(env->bytes_read_string(), - get_bytes_read_templ, - Local(), - attributes); - - t->PrototypeTemplate()->SetAccessorProperty(env->bytes_written_string(), - get_bytes_written_templ, - Local(), - attributes); - - env->SetProtoMethod(t, "readStart", JSMethod); - env->SetProtoMethod(t, "readStop", JSMethod); - env->SetProtoMethod(t, "shutdown", JSMethod); - env->SetProtoMethod(t, "writev", JSMethod); - env->SetProtoMethod(t, - "writeBuffer", - JSMethod); - env->SetProtoMethod(t, - "writeAsciiString", - JSMethod >); - env->SetProtoMethod(t, - "writeUtf8String", - JSMethod >); - env->SetProtoMethod(t, - "writeUcs2String", - JSMethod >); - env->SetProtoMethod(t, - "writeLatin1String", - JSMethod >); +inline void StreamBase::AttachToObject(v8::Local obj) { + obj->SetAlignedPointerInInternalField(kStreamBaseField, this); } +inline StreamBase* StreamBase::FromObject(v8::Local obj) { + if (obj->GetAlignedPointerFromInternalField(0) == nullptr) + return nullptr; -template -void StreamBase::GetFD(const FunctionCallbackInfo& 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(handle); - if (!wrap->IsAlive()) - return args.GetReturnValue().Set(UV_EINVAL); - - args.GetReturnValue().Set(wrap->GetFD()); -} - -template -void StreamBase::GetBytesRead(const FunctionCallbackInfo& args) { - Base* handle; - ASSIGN_OR_RETURN_UNWRAP(&handle, - args.This(), - args.GetReturnValue().Set(0)); - - StreamBase* wrap = static_cast(handle); - // uint64_t -> double. 53bits is enough for all real cases. - args.GetReturnValue().Set(static_cast(wrap->bytes_read_)); -} - -template -void StreamBase::GetBytesWritten(const FunctionCallbackInfo& args) { - Base* handle; - ASSIGN_OR_RETURN_UNWRAP(&handle, - args.This(), - args.GetReturnValue().Set(0)); - - StreamBase* wrap = static_cast(handle); - // uint64_t -> double. 53bits is enough for all real cases. - args.GetReturnValue().Set(static_cast(wrap->bytes_written_)); -} - -template -void StreamBase::GetExternal(const FunctionCallbackInfo& args) { - Base* handle; - ASSIGN_OR_RETURN_UNWRAP(&handle, args.This()); - - StreamBase* wrap = static_cast(handle); - Local ext = External::New(args.GetIsolate(), wrap); - args.GetReturnValue().Set(ext); -} - - -template & args)> -void StreamBase::JSMethod(const FunctionCallbackInfo& args) { - Base* handle; - ASSIGN_OR_RETURN_UNWRAP(&handle, args.Holder()); - - StreamBase* wrap = static_cast(handle); - if (!wrap->IsAlive()) - return args.GetReturnValue().Set(UV_EINVAL); - - AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(handle); - args.GetReturnValue().Set((wrap->*Method)(args)); + return static_cast( + obj->GetAlignedPointerFromInternalField(kStreamBaseField)); } -- cgit v1.2.3