summaryrefslogtreecommitdiff
path: root/src/stream_base-inl.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/stream_base-inl.h')
-rw-r--r--src/stream_base-inl.h142
1 files changed, 7 insertions, 135 deletions
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<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));
}