nan_maybe_43_inl.h 12.3 KB
Newer Older
1 2 3
/*********************************************************************
 * NAN - Native Abstractions for Node.js
 *
4
 * Copyright (c) 2018 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
  return v8::Just<T>(t);
}

28
inline
29
MaybeLocal<v8::String> ToDetailString(v8::Local<v8::Value> val) {
30 31 32 33
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::EscapableHandleScope scope(isolate);
  return scope.Escape(val->ToDetailString(isolate->GetCurrentContext())
                          .FromMaybe(v8::Local<v8::String>()));
34 35
}

36
inline
37
MaybeLocal<v8::Uint32> ToArrayIndex(v8::Local<v8::Value> val) {
38 39 40 41
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::EscapableHandleScope scope(isolate);
  return scope.Escape(val->ToArrayIndex(isolate->GetCurrentContext())
                          .FromMaybe(v8::Local<v8::Uint32>()));
42 43
}

44
inline
45
Maybe<bool> Equals(v8::Local<v8::Value> a, v8::Local<v8::Value>(b)) {
46 47 48
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::HandleScope scope(isolate);
  return a->Equals(isolate->GetCurrentContext(), b);
49 50
}

51
inline
52
MaybeLocal<v8::Object> NewInstance(v8::Local<v8::Function> h) {
53 54 55 56
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::EscapableHandleScope scope(isolate);
  return scope.Escape(h->NewInstance(isolate->GetCurrentContext())
                          .FromMaybe(v8::Local<v8::Object>()));
57 58
}

59
inline
60 61 62 63
MaybeLocal<v8::Object> NewInstance(
      v8::Local<v8::Function> h
    , int argc
    , v8::Local<v8::Value> argv[]) {
64 65 66 67
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::EscapableHandleScope scope(isolate);
  return scope.Escape(h->NewInstance(isolate->GetCurrentContext(), argc, argv)
                          .FromMaybe(v8::Local<v8::Object>()));
68 69
}

70
inline
71
MaybeLocal<v8::Object> NewInstance(v8::Local<v8::ObjectTemplate> h) {
72 73 74 75
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::EscapableHandleScope scope(isolate);
  return scope.Escape(h->NewInstance(isolate->GetCurrentContext())
                          .FromMaybe(v8::Local<v8::Object>()));
76 77 78
}


79
inline MaybeLocal<v8::Function> GetFunction(
80
    v8::Local<v8::FunctionTemplate> t) {
81 82 83 84
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::EscapableHandleScope scope(isolate);
  return scope.Escape(t->GetFunction(isolate->GetCurrentContext())
                          .FromMaybe(v8::Local<v8::Function>()));
85 86
}

87
inline Maybe<bool> Set(
88 89 90
    v8::Local<v8::Object> obj
  , v8::Local<v8::Value> key
  , v8::Local<v8::Value> value) {
91 92 93
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::HandleScope scope(isolate);
  return obj->Set(isolate->GetCurrentContext(), key, value);
94 95
}

96
inline Maybe<bool> Set(
97 98 99
    v8::Local<v8::Object> obj
  , uint32_t index
  , v8::Local<v8::Value> value) {
100 101 102
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::HandleScope scope(isolate);
  return obj->Set(isolate->GetCurrentContext(), index, value);
103 104
}

105 106 107 108
#if NODE_MODULE_VERSION < NODE_4_0_MODULE_VERSION
#include "nan_define_own_property_helper.h"  // NOLINT(build/include)
#endif

109 110 111 112 113 114 115
inline Maybe<bool> DefineOwnProperty(
    v8::Local<v8::Object> obj
  , v8::Local<v8::String> key
  , v8::Local<v8::Value> value
  , v8::PropertyAttribute attribs = v8::None) {
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::HandleScope scope(isolate);
116
#if NODE_MODULE_VERSION >= NODE_4_0_MODULE_VERSION
117 118
  return obj->DefineOwnProperty(isolate->GetCurrentContext(), key, value,
                                attribs);
119 120 121 122 123 124 125
#else
  Maybe<v8::PropertyAttribute> maybeCurrent =
      obj->GetPropertyAttributes(isolate->GetCurrentContext(), key);
  if (maybeCurrent.IsNothing()) {
    return Nothing<bool>();
  }
  v8::PropertyAttribute current = maybeCurrent.FromJust();
126
  return imp::DefineOwnPropertyHelper(current, obj, key, value, attribs);
127
#endif
128 129 130
}

NAN_DEPRECATED inline Maybe<bool> ForceSet(
131 132 133 134
    v8::Local<v8::Object> obj
  , v8::Local<v8::Value> key
  , v8::Local<v8::Value> value
  , v8::PropertyAttribute attribs = v8::None) {
135 136
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::HandleScope scope(isolate);
137 138 139 140 141 142
#if NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  return key->IsName()
             ? obj->DefineOwnProperty(isolate->GetCurrentContext(),
                                      key.As<v8::Name>(), value, attribs)
             : Nothing<bool>();
#else
143
  return obj->ForceSet(isolate->GetCurrentContext(), key, value, attribs);
144
#endif
145 146
}

147
inline MaybeLocal<v8::Value> Get(
148 149
    v8::Local<v8::Object> obj
  , v8::Local<v8::Value> key) {
150 151 152 153
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::EscapableHandleScope scope(isolate);
  return scope.Escape(obj->Get(isolate->GetCurrentContext(), key)
                          .FromMaybe(v8::Local<v8::Value>()));
154 155
}

156
inline
157
MaybeLocal<v8::Value> Get(v8::Local<v8::Object> obj, uint32_t index) {
158 159 160 161
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::EscapableHandleScope scope(isolate);
  return scope.Escape(obj->Get(isolate->GetCurrentContext(), index)
                          .FromMaybe(v8::Local<v8::Value>()));
162 163
}

164
inline v8::PropertyAttribute GetPropertyAttributes(
165 166
    v8::Local<v8::Object> obj
  , v8::Local<v8::Value> key) {
167 168 169 170
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::HandleScope scope(isolate);
  return obj->GetPropertyAttributes(isolate->GetCurrentContext(), key)
      .FromJust();
171 172
}

173
inline Maybe<bool> Has(
174 175
    v8::Local<v8::Object> obj
  , v8::Local<v8::String> key) {
176 177 178
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::HandleScope scope(isolate);
  return obj->Has(isolate->GetCurrentContext(), key);
179 180
}

181
inline Maybe<bool> Has(v8::Local<v8::Object> obj, uint32_t index) {
182 183 184
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::HandleScope scope(isolate);
  return obj->Has(isolate->GetCurrentContext(), index);
185 186
}

187
inline Maybe<bool> Delete(
188 189
    v8::Local<v8::Object> obj
  , v8::Local<v8::String> key) {
190 191 192
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::HandleScope scope(isolate);
  return obj->Delete(isolate->GetCurrentContext(), key);
193 194
}

195
inline
196
Maybe<bool> Delete(v8::Local<v8::Object> obj, uint32_t index) {
197 198 199
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::HandleScope scope(isolate);
  return obj->Delete(isolate->GetCurrentContext(), index);
200 201
}

202
inline
203
MaybeLocal<v8::Array> GetPropertyNames(v8::Local<v8::Object> obj) {
204 205 206 207
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::EscapableHandleScope scope(isolate);
  return scope.Escape(obj->GetPropertyNames(isolate->GetCurrentContext())
                          .FromMaybe(v8::Local<v8::Array>()));
208 209
}

210
inline
211
MaybeLocal<v8::Array> GetOwnPropertyNames(v8::Local<v8::Object> obj) {
212 213 214 215
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::EscapableHandleScope scope(isolate);
  return scope.Escape(obj->GetOwnPropertyNames(isolate->GetCurrentContext())
                          .FromMaybe(v8::Local<v8::Array>()));
216 217
}

218
inline Maybe<bool> SetPrototype(
219 220
    v8::Local<v8::Object> obj
  , v8::Local<v8::Value> prototype) {
221 222 223
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::HandleScope scope(isolate);
  return obj->SetPrototype(isolate->GetCurrentContext(), prototype);
224 225
}

226
inline MaybeLocal<v8::String> ObjectProtoToString(
227
    v8::Local<v8::Object> obj) {
228 229 230 231
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::EscapableHandleScope scope(isolate);
  return scope.Escape(obj->ObjectProtoToString(isolate->GetCurrentContext())
                          .FromMaybe(v8::Local<v8::String>()));
232 233
}

234
inline Maybe<bool> HasOwnProperty(
235 236
    v8::Local<v8::Object> obj
  , v8::Local<v8::String> key) {
237 238 239
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::HandleScope scope(isolate);
  return obj->HasOwnProperty(isolate->GetCurrentContext(), key);
240 241
}

242
inline Maybe<bool> HasRealNamedProperty(
243 244
    v8::Local<v8::Object> obj
  , v8::Local<v8::String> key) {
245 246 247
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::HandleScope scope(isolate);
  return obj->HasRealNamedProperty(isolate->GetCurrentContext(), key);
248 249
}

250
inline Maybe<bool> HasRealIndexedProperty(
251 252
    v8::Local<v8::Object> obj
  , uint32_t index) {
253 254 255
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::HandleScope scope(isolate);
  return obj->HasRealIndexedProperty(isolate->GetCurrentContext(), index);
256 257
}

258
inline Maybe<bool> HasRealNamedCallbackProperty(
259 260
    v8::Local<v8::Object> obj
  , v8::Local<v8::String> key) {
261 262 263
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::HandleScope scope(isolate);
  return obj->HasRealNamedCallbackProperty(isolate->GetCurrentContext(), key);
264 265
}

266
inline MaybeLocal<v8::Value> GetRealNamedPropertyInPrototypeChain(
267 268
    v8::Local<v8::Object> obj
  , v8::Local<v8::String> key) {
269 270 271 272 273
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::EscapableHandleScope scope(isolate);
  return scope.Escape(obj->GetRealNamedPropertyInPrototypeChain(
                             isolate->GetCurrentContext(), key)
                          .FromMaybe(v8::Local<v8::Value>()));
274 275
}

276
inline MaybeLocal<v8::Value> GetRealNamedProperty(
277 278
    v8::Local<v8::Object> obj
  , v8::Local<v8::String> key) {
279 280 281 282 283
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::EscapableHandleScope scope(isolate);
  return scope.Escape(
      obj->GetRealNamedProperty(isolate->GetCurrentContext(), key)
          .FromMaybe(v8::Local<v8::Value>()));
284 285
}

286
inline MaybeLocal<v8::Value> CallAsFunction(
287 288 289 290
    v8::Local<v8::Object> obj
  , v8::Local<v8::Object> recv
  , int argc
  , v8::Local<v8::Value> argv[]) {
291 292 293 294 295
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::EscapableHandleScope scope(isolate);
  return scope.Escape(
      obj->CallAsFunction(isolate->GetCurrentContext(), recv, argc, argv)
          .FromMaybe(v8::Local<v8::Value>()));
296 297
}

298
inline MaybeLocal<v8::Value> CallAsConstructor(
299 300
    v8::Local<v8::Object> obj
  , int argc, v8::Local<v8::Value> argv[]) {
301 302 303 304 305
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::EscapableHandleScope scope(isolate);
  return scope.Escape(
      obj->CallAsConstructor(isolate->GetCurrentContext(), argc, argv)
          .FromMaybe(v8::Local<v8::Value>()));
306 307
}

308
inline
309
MaybeLocal<v8::String> GetSourceLine(v8::Local<v8::Message> msg) {
310 311 312 313
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::EscapableHandleScope scope(isolate);
  return scope.Escape(msg->GetSourceLine(isolate->GetCurrentContext())
                          .FromMaybe(v8::Local<v8::String>()));
314 315
}

316
inline Maybe<int> GetLineNumber(v8::Local<v8::Message> msg) {
317 318 319
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::HandleScope scope(isolate);
  return msg->GetLineNumber(isolate->GetCurrentContext());
320 321
}

322
inline Maybe<int> GetStartColumn(v8::Local<v8::Message> msg) {
323 324 325
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::HandleScope scope(isolate);
  return msg->GetStartColumn(isolate->GetCurrentContext());
326 327
}

328
inline Maybe<int> GetEndColumn(v8::Local<v8::Message> msg) {
329 330 331
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::HandleScope scope(isolate);
  return msg->GetEndColumn(isolate->GetCurrentContext());
332 333
}

334
inline MaybeLocal<v8::Object> CloneElementAt(
335 336
    v8::Local<v8::Array> array
  , uint32_t index) {
337
#if (NODE_MODULE_VERSION >= NODE_6_0_MODULE_VERSION)
338 339 340
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::EscapableHandleScope scope(isolate);
  v8::Local<v8::Context> context = isolate->GetCurrentContext();
341
  v8::Local<v8::Value> elem;
342
  v8::Local<v8::Object> obj;
343
  if (!array->Get(context, index).ToLocal(&elem)) {
344
    return scope.Escape(obj);
345 346
  }
  if (!elem->ToObject(context).ToLocal(&obj)) {
347
    return scope.Escape(v8::Local<v8::Object>());
348
  }
349
  return scope.Escape(obj->Clone());
350
#else
351 352 353 354
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::EscapableHandleScope scope(isolate);
  return scope.Escape(array->CloneElementAt(isolate->GetCurrentContext(), index)
                          .FromMaybe(v8::Local<v8::Object>()));
355
#endif
356 357
}

358
inline MaybeLocal<v8::Value> Call(
359 360 361 362
    v8::Local<v8::Function> fun
  , v8::Local<v8::Object> recv
  , int argc
  , v8::Local<v8::Value> argv[]) {
363 364 365 366
  v8::Isolate *isolate = v8::Isolate::GetCurrent();
  v8::EscapableHandleScope scope(isolate);
  return scope.Escape(fun->Call(isolate->GetCurrentContext(), recv, argc, argv)
                          .FromMaybe(v8::Local<v8::Value>()));
367 368
}

369
#endif  // NAN_MAYBE_43_INL_H_