nan_maybe_pre_43_inl.h 8.04 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
 *
 * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
 ********************************************************************/

#ifndef NAN_MAYBE_PRE_43_INL_H_
#define NAN_MAYBE_PRE_43_INL_H_

template<typename T>
class MaybeLocal {
 public:
15
  inline MaybeLocal() : val_(v8::Local<T>()) {}
16 17 18

  template<typename S>
# if NODE_MODULE_VERSION >= NODE_0_12_MODULE_VERSION
19 20
  inline
  MaybeLocal(v8::Local<S> that) : val_(that) {}  // NOLINT(runtime/explicit)
21
# else
22 23
  inline
  MaybeLocal(v8::Local<S> that) :  // NOLINT(runtime/explicit)
24 25 26
      val_(*reinterpret_cast<v8::Local<T>*>(&that)) {}
# endif

27
  inline bool IsEmpty() const { return val_.IsEmpty(); }
28 29

  template<typename S>
30
  inline bool ToLocal(v8::Local<S> *out) const {
31 32 33 34
    *out = val_;
    return !IsEmpty();
  }

35
  inline v8::Local<T> ToLocalChecked() const {
36 37 38 39 40 41 42
#if defined(V8_ENABLE_CHECKS)
    assert(!IsEmpty() && "ToLocalChecked is Empty");
#endif  // V8_ENABLE_CHECKS
    return val_;
  }

  template<typename S>
43
  inline v8::Local<S> FromMaybe(v8::Local<S> default_value) const {
44
    return IsEmpty() ? default_value : v8::Local<S>(val_);
45 46 47 48 49 50 51 52 53
  }

 private:
  v8::Local<T> val_;
};

template<typename T>
class Maybe {
 public:
54 55
  inline bool IsNothing() const { return !has_value_; }
  inline bool IsJust() const { return has_value_; }
56

57
  inline T FromJust() const {
58 59 60 61 62 63
#if defined(V8_ENABLE_CHECKS)
    assert(IsJust() && "FromJust is Nothing");
#endif  // V8_ENABLE_CHECKS
    return value_;
  }

64
  inline T FromMaybe(const T& default_value) const {
65 66 67
    return has_value_ ? value_ : default_value;
  }

68
  inline bool operator==(const Maybe &other) const {
69 70 71 72
    return (IsJust() == other.IsJust()) &&
        (!IsJust() || FromJust() == other.FromJust());
  }

73
  inline bool operator!=(const Maybe &other) const {
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    return !operator==(other);
  }

 private:
  Maybe() : has_value_(false) {}
  explicit Maybe(const T& t) : has_value_(true), value_(t) {}
  bool has_value_;
  T value_;

  template<typename U>
  friend Maybe<U> Nothing();
  template<typename U>
  friend Maybe<U> Just(const U& u);
};

template<typename T>
inline Maybe<T> Nothing() {
  return Maybe<T>();
}

template<typename T>
inline Maybe<T> Just(const T& t) {
  return Maybe<T>(t);
}

99
inline
100 101 102 103
MaybeLocal<v8::String> ToDetailString(v8::Handle<v8::Value> val) {
  return MaybeLocal<v8::String>(val->ToDetailString());
}

104
inline
105 106 107 108
MaybeLocal<v8::Uint32> ToArrayIndex(v8::Handle<v8::Value> val) {
  return MaybeLocal<v8::Uint32>(val->ToArrayIndex());
}

109
inline
110 111 112 113
Maybe<bool> Equals(v8::Handle<v8::Value> a, v8::Handle<v8::Value>(b)) {
  return Just<bool>(a->Equals(b));
}

114
inline
115 116 117 118
MaybeLocal<v8::Object> NewInstance(v8::Handle<v8::Function> h) {
  return MaybeLocal<v8::Object>(h->NewInstance());
}

119
inline
120 121 122 123 124 125 126
MaybeLocal<v8::Object> NewInstance(
      v8::Local<v8::Function> h
    , int argc
    , v8::Local<v8::Value> argv[]) {
  return MaybeLocal<v8::Object>(h->NewInstance(argc, argv));
}

127
inline
128 129 130 131
MaybeLocal<v8::Object> NewInstance(v8::Handle<v8::ObjectTemplate> h) {
  return MaybeLocal<v8::Object>(h->NewInstance());
}

132
inline
133 134 135 136
MaybeLocal<v8::Function> GetFunction(v8::Handle<v8::FunctionTemplate> t) {
  return MaybeLocal<v8::Function>(t->GetFunction());
}

137
inline Maybe<bool> Set(
138 139 140 141 142 143
    v8::Handle<v8::Object> obj
  , v8::Handle<v8::Value> key
  , v8::Handle<v8::Value> value) {
  return Just<bool>(obj->Set(key, value));
}

144
inline Maybe<bool> Set(
145 146 147 148 149 150
    v8::Handle<v8::Object> obj
  , uint32_t index
  , v8::Handle<v8::Value> value) {
  return Just<bool>(obj->Set(index, value));
}

151 152
#include "nan_define_own_property_helper.h"  // NOLINT(build/include)

153 154 155 156 157 158
inline Maybe<bool> DefineOwnProperty(
    v8::Handle<v8::Object> obj
  , v8::Handle<v8::String> key
  , v8::Handle<v8::Value> value
  , v8::PropertyAttribute attribs = v8::None) {
  v8::PropertyAttribute current = obj->GetPropertyAttributes(key);
159
  return imp::DefineOwnPropertyHelper(current, obj, key, value, attribs);
160 161 162
}

NAN_DEPRECATED inline Maybe<bool> ForceSet(
163 164 165 166 167 168 169
    v8::Handle<v8::Object> obj
  , v8::Handle<v8::Value> key
  , v8::Handle<v8::Value> value
  , v8::PropertyAttribute attribs = v8::None) {
  return Just<bool>(obj->ForceSet(key, value, attribs));
}

170
inline MaybeLocal<v8::Value> Get(
171 172 173 174 175
    v8::Handle<v8::Object> obj
  , v8::Handle<v8::Value> key) {
  return MaybeLocal<v8::Value>(obj->Get(key));
}

176
inline MaybeLocal<v8::Value> Get(
177 178 179 180 181
    v8::Handle<v8::Object> obj
  , uint32_t index) {
  return MaybeLocal<v8::Value>(obj->Get(index));
}

182
inline Maybe<v8::PropertyAttribute> GetPropertyAttributes(
183 184 185 186 187
    v8::Handle<v8::Object> obj
  , v8::Handle<v8::Value> key) {
  return Just<v8::PropertyAttribute>(obj->GetPropertyAttributes(key));
}

188
inline Maybe<bool> Has(
189 190 191 192 193
    v8::Handle<v8::Object> obj
  , v8::Handle<v8::String> key) {
  return Just<bool>(obj->Has(key));
}

194
inline Maybe<bool> Has(
195 196 197 198 199
    v8::Handle<v8::Object> obj
  , uint32_t index) {
  return Just<bool>(obj->Has(index));
}

200
inline Maybe<bool> Delete(
201 202 203 204 205
    v8::Handle<v8::Object> obj
  , v8::Handle<v8::String> key) {
  return Just<bool>(obj->Delete(key));
}

206
inline Maybe<bool> Delete(
207 208 209 210 211
    v8::Handle<v8::Object> obj
  , uint32_t index) {
  return Just<bool>(obj->Delete(index));
}

212
inline
213 214 215 216
MaybeLocal<v8::Array> GetPropertyNames(v8::Handle<v8::Object> obj) {
  return MaybeLocal<v8::Array>(obj->GetPropertyNames());
}

217
inline
218 219 220 221
MaybeLocal<v8::Array> GetOwnPropertyNames(v8::Handle<v8::Object> obj) {
  return MaybeLocal<v8::Array>(obj->GetOwnPropertyNames());
}

222
inline Maybe<bool> SetPrototype(
223 224 225 226 227
    v8::Handle<v8::Object> obj
  , v8::Handle<v8::Value> prototype) {
  return Just<bool>(obj->SetPrototype(prototype));
}

228
inline MaybeLocal<v8::String> ObjectProtoToString(
229 230 231 232
    v8::Handle<v8::Object> obj) {
  return MaybeLocal<v8::String>(obj->ObjectProtoToString());
}

233
inline Maybe<bool> HasOwnProperty(
234 235 236 237 238
    v8::Handle<v8::Object> obj
  , v8::Handle<v8::String> key) {
  return Just<bool>(obj->HasOwnProperty(key));
}

239
inline Maybe<bool> HasRealNamedProperty(
240 241 242 243 244
    v8::Handle<v8::Object> obj
  , v8::Handle<v8::String> key) {
  return Just<bool>(obj->HasRealNamedProperty(key));
}

245
inline Maybe<bool> HasRealIndexedProperty(
246 247 248 249 250
    v8::Handle<v8::Object> obj
  , uint32_t index) {
  return Just<bool>(obj->HasRealIndexedProperty(index));
}

251
inline Maybe<bool> HasRealNamedCallbackProperty(
252 253 254 255 256
    v8::Handle<v8::Object> obj
  , v8::Handle<v8::String> key) {
  return Just<bool>(obj->HasRealNamedCallbackProperty(key));
}

257
inline MaybeLocal<v8::Value> GetRealNamedPropertyInPrototypeChain(
258 259 260 261 262 263
    v8::Handle<v8::Object> obj
  , v8::Handle<v8::String> key) {
  return MaybeLocal<v8::Value>(
      obj->GetRealNamedPropertyInPrototypeChain(key));
}

264
inline MaybeLocal<v8::Value> GetRealNamedProperty(
265 266 267 268 269
    v8::Handle<v8::Object> obj
  , v8::Handle<v8::String> key) {
  return MaybeLocal<v8::Value>(obj->GetRealNamedProperty(key));
}

270
inline MaybeLocal<v8::Value> CallAsFunction(
271 272 273 274 275 276 277
    v8::Handle<v8::Object> obj
  , v8::Handle<v8::Object> recv
  , int argc
  , v8::Handle<v8::Value> argv[]) {
  return MaybeLocal<v8::Value>(obj->CallAsFunction(recv, argc, argv));
}

278
inline MaybeLocal<v8::Value> CallAsConstructor(
279 280 281 282 283 284
    v8::Handle<v8::Object> obj
  , int argc
  , v8::Local<v8::Value> argv[]) {
  return MaybeLocal<v8::Value>(obj->CallAsConstructor(argc, argv));
}

285
inline
286 287 288 289
MaybeLocal<v8::String> GetSourceLine(v8::Handle<v8::Message> msg) {
  return MaybeLocal<v8::String>(msg->GetSourceLine());
}

290
inline Maybe<int> GetLineNumber(v8::Handle<v8::Message> msg) {
291 292 293
  return Just<int>(msg->GetLineNumber());
}

294
inline Maybe<int> GetStartColumn(v8::Handle<v8::Message> msg) {
295 296 297
  return Just<int>(msg->GetStartColumn());
}

298
inline Maybe<int> GetEndColumn(v8::Handle<v8::Message> msg) {
299 300 301
  return Just<int>(msg->GetEndColumn());
}

302
inline MaybeLocal<v8::Object> CloneElementAt(
303 304 305 306 307
    v8::Handle<v8::Array> array
  , uint32_t index) {
  return MaybeLocal<v8::Object>(array->CloneElementAt(index));
}

308
inline MaybeLocal<v8::Value> Call(
309 310 311 312 313 314 315
    v8::Local<v8::Function> fun
  , v8::Local<v8::Object> recv
  , int argc
  , v8::Local<v8::Value> argv[]) {
  return MaybeLocal<v8::Value>(fun->Call(recv, argc, argv));
}

316
#endif  // NAN_MAYBE_PRE_43_INL_H_