/********************************************************************* * NAN - Native Abstractions for Node.js * * Copyright (c) 2017 NAN contributors * * MIT License ********************************************************************/ #ifndef NAN_MAYBE_PRE_43_INL_H_ #define NAN_MAYBE_PRE_43_INL_H_ template class MaybeLocal { public: inline MaybeLocal() : val_(v8::Local()) {} template # if NODE_MODULE_VERSION >= NODE_0_12_MODULE_VERSION inline MaybeLocal(v8::Local that) : val_(that) {} // NOLINT(runtime/explicit) # else inline MaybeLocal(v8::Local that) : // NOLINT(runtime/explicit) val_(*reinterpret_cast*>(&that)) {} # endif inline bool IsEmpty() const { return val_.IsEmpty(); } template inline bool ToLocal(v8::Local *out) const { *out = val_; return !IsEmpty(); } inline v8::Local ToLocalChecked() const { #if defined(V8_ENABLE_CHECKS) assert(!IsEmpty() && "ToLocalChecked is Empty"); #endif // V8_ENABLE_CHECKS return val_; } template inline v8::Local FromMaybe(v8::Local default_value) const { return IsEmpty() ? default_value : v8::Local(val_); } private: v8::Local val_; }; template class Maybe { public: inline bool IsNothing() const { return !has_value_; } inline bool IsJust() const { return has_value_; } inline T FromJust() const { #if defined(V8_ENABLE_CHECKS) assert(IsJust() && "FromJust is Nothing"); #endif // V8_ENABLE_CHECKS return value_; } inline T FromMaybe(const T& default_value) const { return has_value_ ? value_ : default_value; } inline bool operator==(const Maybe &other) const { return (IsJust() == other.IsJust()) && (!IsJust() || FromJust() == other.FromJust()); } inline bool operator!=(const Maybe &other) const { return !operator==(other); } private: Maybe() : has_value_(false) {} explicit Maybe(const T& t) : has_value_(true), value_(t) {} bool has_value_; T value_; template friend Maybe Nothing(); template friend Maybe Just(const U& u); }; template inline Maybe Nothing() { return Maybe(); } template inline Maybe Just(const T& t) { return Maybe(t); } inline MaybeLocal ToDetailString(v8::Handle val) { return MaybeLocal(val->ToDetailString()); } inline MaybeLocal ToArrayIndex(v8::Handle val) { return MaybeLocal(val->ToArrayIndex()); } inline Maybe Equals(v8::Handle a, v8::Handle(b)) { return Just(a->Equals(b)); } inline MaybeLocal NewInstance(v8::Handle h) { return MaybeLocal(h->NewInstance()); } inline MaybeLocal NewInstance( v8::Local h , int argc , v8::Local argv[]) { return MaybeLocal(h->NewInstance(argc, argv)); } inline MaybeLocal NewInstance(v8::Handle h) { return MaybeLocal(h->NewInstance()); } inline MaybeLocal GetFunction(v8::Handle t) { return MaybeLocal(t->GetFunction()); } inline Maybe Set( v8::Handle obj , v8::Handle key , v8::Handle value) { return Just(obj->Set(key, value)); } inline Maybe Set( v8::Handle obj , uint32_t index , v8::Handle value) { return Just(obj->Set(index, value)); } inline Maybe ForceSet( v8::Handle obj , v8::Handle key , v8::Handle value , v8::PropertyAttribute attribs = v8::None) { return Just(obj->ForceSet(key, value, attribs)); } inline MaybeLocal Get( v8::Handle obj , v8::Handle key) { return MaybeLocal(obj->Get(key)); } inline MaybeLocal Get( v8::Handle obj , uint32_t index) { return MaybeLocal(obj->Get(index)); } inline Maybe GetPropertyAttributes( v8::Handle obj , v8::Handle key) { return Just(obj->GetPropertyAttributes(key)); } inline Maybe Has( v8::Handle obj , v8::Handle key) { return Just(obj->Has(key)); } inline Maybe Has( v8::Handle obj , uint32_t index) { return Just(obj->Has(index)); } inline Maybe Delete( v8::Handle obj , v8::Handle key) { return Just(obj->Delete(key)); } inline Maybe Delete( v8::Handle obj , uint32_t index) { return Just(obj->Delete(index)); } inline MaybeLocal GetPropertyNames(v8::Handle obj) { return MaybeLocal(obj->GetPropertyNames()); } inline MaybeLocal GetOwnPropertyNames(v8::Handle obj) { return MaybeLocal(obj->GetOwnPropertyNames()); } inline Maybe SetPrototype( v8::Handle obj , v8::Handle prototype) { return Just(obj->SetPrototype(prototype)); } inline MaybeLocal ObjectProtoToString( v8::Handle obj) { return MaybeLocal(obj->ObjectProtoToString()); } inline Maybe HasOwnProperty( v8::Handle obj , v8::Handle key) { return Just(obj->HasOwnProperty(key)); } inline Maybe HasRealNamedProperty( v8::Handle obj , v8::Handle key) { return Just(obj->HasRealNamedProperty(key)); } inline Maybe HasRealIndexedProperty( v8::Handle obj , uint32_t index) { return Just(obj->HasRealIndexedProperty(index)); } inline Maybe HasRealNamedCallbackProperty( v8::Handle obj , v8::Handle key) { return Just(obj->HasRealNamedCallbackProperty(key)); } inline MaybeLocal GetRealNamedPropertyInPrototypeChain( v8::Handle obj , v8::Handle key) { return MaybeLocal( obj->GetRealNamedPropertyInPrototypeChain(key)); } inline MaybeLocal GetRealNamedProperty( v8::Handle obj , v8::Handle key) { return MaybeLocal(obj->GetRealNamedProperty(key)); } inline MaybeLocal CallAsFunction( v8::Handle obj , v8::Handle recv , int argc , v8::Handle argv[]) { return MaybeLocal(obj->CallAsFunction(recv, argc, argv)); } inline MaybeLocal CallAsConstructor( v8::Handle obj , int argc , v8::Local argv[]) { return MaybeLocal(obj->CallAsConstructor(argc, argv)); } inline MaybeLocal GetSourceLine(v8::Handle msg) { return MaybeLocal(msg->GetSourceLine()); } inline Maybe GetLineNumber(v8::Handle msg) { return Just(msg->GetLineNumber()); } inline Maybe GetStartColumn(v8::Handle msg) { return Just(msg->GetStartColumn()); } inline Maybe GetEndColumn(v8::Handle msg) { return Just(msg->GetEndColumn()); } inline MaybeLocal CloneElementAt( v8::Handle array , uint32_t index) { return MaybeLocal(array->CloneElementAt(index)); } inline MaybeLocal Call( v8::Local fun , v8::Local recv , int argc , v8::Local argv[]) { return MaybeLocal(fun->Call(recv, argc, argv)); } #endif // NAN_MAYBE_PRE_43_INL_H_