nan_maybe_43_inl.h 6.45 KB
Newer Older
1 2 3
/*********************************************************************
 * NAN - Native Abstractions for Node.js
 *
4
 * Copyright (c) 2016 NAN contributors
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
 ********************************************************************/

#ifndef NAN_MAYBE_43_INL_H_
#define NAN_MAYBE_43_INL_H_

template<typename T>
using MaybeLocal = v8::MaybeLocal<T>;

template<typename T>
using Maybe = v8::Maybe<T>;

template<typename T>
19
inline Maybe<T> Nothing() {
20 21 22 23
  return v8::Nothing<T>();
}

template<typename T>
24
inline Maybe<T> Just(const T& t) {
25 26 27 28 29
  return v8::Just<T>(t);
}

v8::Local<v8::Context> GetCurrentContext();

30
inline
31 32 33 34
MaybeLocal<v8::String> ToDetailString(v8::Local<v8::Value> val) {
  return val->ToDetailString(GetCurrentContext());
}

35
inline
36 37 38 39
MaybeLocal<v8::Uint32> ToArrayIndex(v8::Local<v8::Value> val) {
  return val->ToArrayIndex(GetCurrentContext());
}

40
inline
41 42 43 44
Maybe<bool> Equals(v8::Local<v8::Value> a, v8::Local<v8::Value>(b)) {
  return a->Equals(GetCurrentContext(), b);
}

45
inline
46
MaybeLocal<v8::Object> NewInstance(v8::Local<v8::Function> h) {
47
  return h->NewInstance(GetCurrentContext());
48 49
}

50
inline
51 52 53 54
MaybeLocal<v8::Object> NewInstance(
      v8::Local<v8::Function> h
    , int argc
    , v8::Local<v8::Value> argv[]) {
55
  return h->NewInstance(GetCurrentContext(), argc, argv);
56 57
}

58
inline
59
MaybeLocal<v8::Object> NewInstance(v8::Local<v8::ObjectTemplate> h) {
60
  return h->NewInstance(GetCurrentContext());
61 62 63
}


64
inline MaybeLocal<v8::Function> GetFunction(
65 66 67 68
    v8::Local<v8::FunctionTemplate> t) {
  return t->GetFunction(GetCurrentContext());
}

69
inline Maybe<bool> Set(
70 71 72 73 74 75
    v8::Local<v8::Object> obj
  , v8::Local<v8::Value> key
  , v8::Local<v8::Value> value) {
  return obj->Set(GetCurrentContext(), key, value);
}

76
inline Maybe<bool> Set(
77 78 79 80 81 82
    v8::Local<v8::Object> obj
  , uint32_t index
  , v8::Local<v8::Value> value) {
  return obj->Set(GetCurrentContext(), index, value);
}

83
inline Maybe<bool> ForceSet(
84 85 86 87 88 89 90
    v8::Local<v8::Object> obj
  , v8::Local<v8::Value> key
  , v8::Local<v8::Value> value
  , v8::PropertyAttribute attribs = v8::None) {
  return obj->ForceSet(GetCurrentContext(), key, value, attribs);
}

91
inline MaybeLocal<v8::Value> Get(
92 93 94 95 96
    v8::Local<v8::Object> obj
  , v8::Local<v8::Value> key) {
  return obj->Get(GetCurrentContext(), key);
}

97
inline
98 99 100 101
MaybeLocal<v8::Value> Get(v8::Local<v8::Object> obj, uint32_t index) {
  return obj->Get(GetCurrentContext(), index);
}

102
inline v8::PropertyAttribute GetPropertyAttributes(
103 104 105 106 107
    v8::Local<v8::Object> obj
  , v8::Local<v8::Value> key) {
  return obj->GetPropertyAttributes(GetCurrentContext(), key).FromJust();
}

108
inline Maybe<bool> Has(
109 110 111 112 113
    v8::Local<v8::Object> obj
  , v8::Local<v8::String> key) {
  return obj->Has(GetCurrentContext(), key);
}

114
inline Maybe<bool> Has(v8::Local<v8::Object> obj, uint32_t index) {
115 116 117
  return obj->Has(GetCurrentContext(), index);
}

118
inline Maybe<bool> Delete(
119 120 121 122 123
    v8::Local<v8::Object> obj
  , v8::Local<v8::String> key) {
  return obj->Delete(GetCurrentContext(), key);
}

124
inline
125 126 127 128
Maybe<bool> Delete(v8::Local<v8::Object> obj, uint32_t index) {
  return obj->Delete(GetCurrentContext(), index);
}

129
inline
130 131 132 133
MaybeLocal<v8::Array> GetPropertyNames(v8::Local<v8::Object> obj) {
  return obj->GetPropertyNames(GetCurrentContext());
}

134
inline
135 136 137 138
MaybeLocal<v8::Array> GetOwnPropertyNames(v8::Local<v8::Object> obj) {
  return obj->GetOwnPropertyNames(GetCurrentContext());
}

139
inline Maybe<bool> SetPrototype(
140 141 142 143 144
    v8::Local<v8::Object> obj
  , v8::Local<v8::Value> prototype) {
  return obj->SetPrototype(GetCurrentContext(), prototype);
}

145
inline MaybeLocal<v8::String> ObjectProtoToString(
146 147 148 149
    v8::Local<v8::Object> obj) {
  return obj->ObjectProtoToString(GetCurrentContext());
}

150
inline Maybe<bool> HasOwnProperty(
151 152 153 154 155
    v8::Local<v8::Object> obj
  , v8::Local<v8::String> key) {
  return obj->HasOwnProperty(GetCurrentContext(), key);
}

156
inline Maybe<bool> HasRealNamedProperty(
157 158 159 160 161
    v8::Local<v8::Object> obj
  , v8::Local<v8::String> key) {
  return obj->HasRealNamedProperty(GetCurrentContext(), key);
}

162
inline Maybe<bool> HasRealIndexedProperty(
163 164 165 166 167
    v8::Local<v8::Object> obj
  , uint32_t index) {
  return obj->HasRealIndexedProperty(GetCurrentContext(), index);
}

168
inline Maybe<bool> HasRealNamedCallbackProperty(
169 170 171 172 173
    v8::Local<v8::Object> obj
  , v8::Local<v8::String> key) {
  return obj->HasRealNamedCallbackProperty(GetCurrentContext(), key);
}

174
inline MaybeLocal<v8::Value> GetRealNamedPropertyInPrototypeChain(
175 176 177 178 179
    v8::Local<v8::Object> obj
  , v8::Local<v8::String> key) {
  return obj->GetRealNamedPropertyInPrototypeChain(GetCurrentContext(), key);
}

180
inline MaybeLocal<v8::Value> GetRealNamedProperty(
181 182 183 184 185
    v8::Local<v8::Object> obj
  , v8::Local<v8::String> key) {
  return obj->GetRealNamedProperty(GetCurrentContext(), key);
}

186
inline MaybeLocal<v8::Value> CallAsFunction(
187 188 189 190 191 192 193
    v8::Local<v8::Object> obj
  , v8::Local<v8::Object> recv
  , int argc
  , v8::Local<v8::Value> argv[]) {
  return obj->CallAsFunction(GetCurrentContext(), recv, argc, argv);
}

194
inline MaybeLocal<v8::Value> CallAsConstructor(
195 196 197 198 199
    v8::Local<v8::Object> obj
  , int argc, v8::Local<v8::Value> argv[]) {
  return obj->CallAsConstructor(GetCurrentContext(), argc, argv);
}

200
inline
201 202 203 204
MaybeLocal<v8::String> GetSourceLine(v8::Local<v8::Message> msg) {
  return msg->GetSourceLine(GetCurrentContext());
}

205
inline Maybe<int> GetLineNumber(v8::Local<v8::Message> msg) {
206 207 208
  return msg->GetLineNumber(GetCurrentContext());
}

209
inline Maybe<int> GetStartColumn(v8::Local<v8::Message> msg) {
210 211 212
  return msg->GetStartColumn(GetCurrentContext());
}

213
inline Maybe<int> GetEndColumn(v8::Local<v8::Message> msg) {
214 215 216
  return msg->GetEndColumn(GetCurrentContext());
}

217
inline MaybeLocal<v8::Object> CloneElementAt(
218 219
    v8::Local<v8::Array> array
  , uint32_t index) {
220 221 222 223 224 225 226 227 228 229 230 231 232
#if (NODE_MODULE_VERSION >= NODE_6_0_MODULE_VERSION)
  v8::EscapableHandleScope handle_scope(v8::Isolate::GetCurrent());
  v8::Local<v8::Context> context = GetCurrentContext();
  v8::Local<v8::Value> elem;
  if (!array->Get(context, index).ToLocal(&elem)) {
    return MaybeLocal<v8::Object>();
  }
  v8::Local<v8::Object> obj;
  if (!elem->ToObject(context).ToLocal(&obj)) {
    return MaybeLocal<v8::Object>();
  }
  return MaybeLocal<v8::Object>(handle_scope.Escape(obj->Clone()));
#else
233
  return array->CloneElementAt(GetCurrentContext(), index);
234
#endif
235 236
}

237
inline MaybeLocal<v8::Value> Call(
238 239 240 241 242 243 244
    v8::Local<v8::Function> fun
  , v8::Local<v8::Object> recv
  , int argc
  , v8::Local<v8::Value> argv[]) {
  return fun->Call(GetCurrentContext(), recv, argc, argv);
}

245
#endif  // NAN_MAYBE_43_INL_H_