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.cc | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) (limited to 'src/stream_base.cc') diff --git a/src/stream_base.cc b/src/stream_base.cc index 24210e1e26..bf7003e127 100644 --- a/src/stream_base.cc +++ b/src/stream_base.cc @@ -327,6 +327,93 @@ Local StreamBase::GetObject() { return GetAsyncWrap()->object(); } +void StreamBase::AddMethod(Environment* env, + Local signature, + enum PropertyAttribute attributes, + Local t, + JSMethodFunction* stream_method, + Local string) { + Local templ = + env->NewFunctionTemplate(stream_method, + signature, + v8::ConstructorBehavior::kThrow, + v8::SideEffectType::kHasNoSideEffect); + t->PrototypeTemplate()->SetAccessorProperty( + string, templ, Local(), attributes); +} + +void StreamBase::AddMethods(Environment* env, Local t) { + HandleScope scope(env->isolate()); + + enum PropertyAttribute attributes = static_cast( + v8::ReadOnly | v8::DontDelete | v8::DontEnum); + Local sig = Signature::New(env->isolate(), t); + + AddMethod(env, sig, attributes, t, GetFD, env->fd_string()); + AddMethod( + env, sig, attributes, t, GetExternal, env->external_stream_string()); + AddMethod(env, sig, attributes, t, GetBytesRead, env->bytes_read_string()); + AddMethod( + env, sig, attributes, t, GetBytesWritten, env->bytes_written_string()); + env->SetProtoMethod(t, "readStart", JSMethod<&StreamBase::ReadStartJS>); + env->SetProtoMethod(t, "readStop", JSMethod<&StreamBase::ReadStopJS>); + env->SetProtoMethod(t, "shutdown", JSMethod<&StreamBase::Shutdown>); + env->SetProtoMethod(t, "writev", JSMethod<&StreamBase::Writev>); + env->SetProtoMethod(t, "writeBuffer", JSMethod<&StreamBase::WriteBuffer>); + env->SetProtoMethod( + t, "writeAsciiString", JSMethod<&StreamBase::WriteString>); + env->SetProtoMethod( + t, "writeUtf8String", JSMethod<&StreamBase::WriteString>); + env->SetProtoMethod( + t, "writeUcs2String", JSMethod<&StreamBase::WriteString>); + env->SetProtoMethod( + t, "writeLatin1String", JSMethod<&StreamBase::WriteString>); +} + +void StreamBase::GetFD(const FunctionCallbackInfo& args) { + // Mimic implementation of StreamBase::GetFD() and UDPWrap::GetFD(). + StreamBase* wrap = StreamBase::FromObject(args.This().As()); + if (wrap == nullptr) return args.GetReturnValue().Set(UV_EINVAL); + + if (!wrap->IsAlive()) return args.GetReturnValue().Set(UV_EINVAL); + + args.GetReturnValue().Set(wrap->GetFD()); +} + +void StreamBase::GetBytesRead(const FunctionCallbackInfo& args) { + StreamBase* wrap = StreamBase::FromObject(args.This().As()); + if (wrap == nullptr) return args.GetReturnValue().Set(0); + + // uint64_t -> double. 53bits is enough for all real cases. + args.GetReturnValue().Set(static_cast(wrap->bytes_read_)); +} + +void StreamBase::GetBytesWritten(const FunctionCallbackInfo& args) { + StreamBase* wrap = StreamBase::FromObject(args.This().As()); + if (wrap == nullptr) return args.GetReturnValue().Set(0); + + // uint64_t -> double. 53bits is enough for all real cases. + args.GetReturnValue().Set(static_cast(wrap->bytes_written_)); +} + +void StreamBase::GetExternal(const FunctionCallbackInfo& args) { + StreamBase* wrap = StreamBase::FromObject(args.This().As()); + if (wrap == nullptr) return; + + Local ext = External::New(args.GetIsolate(), wrap); + args.GetReturnValue().Set(ext); +} + +template & args)> +void StreamBase::JSMethod(const FunctionCallbackInfo& args) { + StreamBase* wrap = StreamBase::FromObject(args.Holder().As()); + if (wrap == nullptr) return; + + if (!wrap->IsAlive()) return args.GetReturnValue().Set(UV_EINVAL); + + AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(wrap->GetAsyncWrap()); + args.GetReturnValue().Set((wrap->*Method)(args)); +} int StreamResource::DoTryWrite(uv_buf_t** bufs, size_t* count) { // No TryWrite by default -- cgit v1.2.3