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