src: replace naive search in Buffer::IndexOf
[platform/upstream/nodejs.git] / src / stream_base-inl.h
1 #ifndef SRC_STREAM_BASE_INL_H_
2 #define SRC_STREAM_BASE_INL_H_
3
4 #include "stream_base.h"
5
6 #include "node.h"
7 #include "env.h"
8 #include "env-inl.h"
9 #include "v8.h"
10
11 namespace node {
12
13 using v8::External;
14 using v8::FunctionCallbackInfo;
15 using v8::FunctionTemplate;
16 using v8::HandleScope;
17 using v8::Local;
18 using v8::Object;
19 using v8::PropertyAttribute;
20 using v8::PropertyCallbackInfo;
21 using v8::String;
22 using v8::Value;
23
24 template <class Base>
25 void StreamBase::AddMethods(Environment* env,
26                             Local<FunctionTemplate> t,
27                             int flags) {
28   HandleScope scope(env->isolate());
29
30   enum PropertyAttribute attributes =
31       static_cast<PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
32   t->InstanceTemplate()->SetAccessor(env->fd_string(),
33                                      GetFD<Base>,
34                                      nullptr,
35                                      env->as_external(),
36                                      v8::DEFAULT,
37                                      attributes);
38
39   t->InstanceTemplate()->SetAccessor(env->external_stream_string(),
40                                      GetExternal<Base>,
41                                      nullptr,
42                                      env->as_external(),
43                                      v8::DEFAULT,
44                                      attributes);
45
46   env->SetProtoMethod(t, "readStart", JSMethod<Base, &StreamBase::ReadStart>);
47   env->SetProtoMethod(t, "readStop", JSMethod<Base, &StreamBase::ReadStop>);
48   if ((flags & kFlagNoShutdown) == 0)
49     env->SetProtoMethod(t, "shutdown", JSMethod<Base, &StreamBase::Shutdown>);
50   if ((flags & kFlagHasWritev) != 0)
51     env->SetProtoMethod(t, "writev", JSMethod<Base, &StreamBase::Writev>);
52   env->SetProtoMethod(t,
53                       "writeBuffer",
54                       JSMethod<Base, &StreamBase::WriteBuffer>);
55   env->SetProtoMethod(t,
56                       "writeAsciiString",
57                       JSMethod<Base, &StreamBase::WriteString<ASCII> >);
58   env->SetProtoMethod(t,
59                       "writeUtf8String",
60                       JSMethod<Base, &StreamBase::WriteString<UTF8> >);
61   env->SetProtoMethod(t,
62                       "writeUcs2String",
63                       JSMethod<Base, &StreamBase::WriteString<UCS2> >);
64   env->SetProtoMethod(t,
65                       "writeBinaryString",
66                       JSMethod<Base, &StreamBase::WriteString<BINARY> >);
67 }
68
69
70 template <class Base>
71 void StreamBase::GetFD(Local<String> key,
72                        const PropertyCallbackInfo<Value>& args) {
73   StreamBase* wrap = Unwrap<Base>(args.Holder());
74
75   if (!wrap->IsAlive())
76     return args.GetReturnValue().Set(UV_EINVAL);
77
78   args.GetReturnValue().Set(wrap->GetFD());
79 }
80
81
82 template <class Base>
83 void StreamBase::GetExternal(Local<String> key,
84                              const PropertyCallbackInfo<Value>& args) {
85   StreamBase* wrap = Unwrap<Base>(args.Holder());
86
87   Local<External> ext = External::New(args.GetIsolate(), wrap);
88   args.GetReturnValue().Set(ext);
89 }
90
91
92 template <class Base,
93           int (StreamBase::*Method)(const FunctionCallbackInfo<Value>& args)>
94 void StreamBase::JSMethod(const FunctionCallbackInfo<Value>& args) {
95   StreamBase* wrap = Unwrap<Base>(args.Holder());
96
97   if (!wrap->IsAlive())
98     return args.GetReturnValue().Set(UV_EINVAL);
99
100   args.GetReturnValue().Set((wrap->*Method)(args));
101 }
102
103
104 WriteWrap* WriteWrap::New(Environment* env,
105                           Local<Object> obj,
106                           StreamBase* wrap,
107                           DoneCb cb,
108                           size_t extra) {
109   size_t storage_size = ROUND_UP(sizeof(WriteWrap), kAlignSize) + extra;
110   char* storage = new char[storage_size];
111
112   return new(storage) WriteWrap(env, obj, wrap, cb, storage_size);
113 }
114
115
116 void WriteWrap::Dispose() {
117   this->~WriteWrap();
118   delete[] reinterpret_cast<char*>(this);
119 }
120
121
122 char* WriteWrap::Extra(size_t offset) {
123   return reinterpret_cast<char*>(this) +
124          ROUND_UP(sizeof(*this), kAlignSize) +
125          offset;
126 }
127
128 }  // namespace node
129
130 #endif  // SRC_STREAM_BASE_INL_H_