## Maybe Types The `Nan::MaybeLocal` and `Nan::Maybe` types are monads that encapsulate `v8::Local` handles that _may be empty_. * **Maybe Types** - Nan::MaybeLocal - Nan::Maybe - Nan::Nothing - Nan::Just * **Maybe Helpers** - Nan::Call() - Nan::ToDetailString() - Nan::ToArrayIndex() - Nan::Equals() - Nan::NewInstance() - Nan::GetFunction() - Nan::Set() - Nan::DefineOwnProperty() - Nan::ForceSet() - Nan::Get() - Nan::GetPropertyAttributes() - Nan::Has() - Nan::Delete() - Nan::GetPropertyNames() - Nan::GetOwnPropertyNames() - Nan::SetPrototype() - Nan::ObjectProtoToString() - Nan::HasOwnProperty() - Nan::HasRealNamedProperty() - Nan::HasRealIndexedProperty() - Nan::HasRealNamedCallbackProperty() - Nan::GetRealNamedPropertyInPrototypeChain() - Nan::GetRealNamedProperty() - Nan::CallAsFunction() - Nan::CallAsConstructor() - Nan::GetSourceLine() - Nan::GetLineNumber() - Nan::GetStartColumn() - Nan::GetEndColumn() - Nan::CloneElementAt() - Nan::HasPrivate() - Nan::GetPrivate() - Nan::SetPrivate() - Nan::DeletePrivate() - Nan::MakeMaybe() ### Nan::MaybeLocal A `Nan::MaybeLocal` is a wrapper around [`v8::Local`](https://v8docs.nodesource.com/io.js-3.3/de/deb/classv8_1_1_local.html) that enforces a check that determines whether the `v8::Local` is empty before it can be used. If an API method returns a `Nan::MaybeLocal`, the API method can potentially fail either because an exception is thrown, or because an exception is pending, e.g. because a previous API call threw an exception that hasn't been caught yet, or because a `v8::TerminateExecution` exception was thrown. In that case, an empty `Nan::MaybeLocal` is returned. Definition: ```c++ template class Nan::MaybeLocal { public: MaybeLocal(); template MaybeLocal(v8::Local that); bool IsEmpty() const; template bool ToLocal(v8::Local *out); // Will crash if the MaybeLocal<> is empty. v8::Local ToLocalChecked(); template v8::Local FromMaybe(v8::Local default_value) const; }; ``` See the documentation for [`v8::MaybeLocal`](https://v8docs.nodesource.com/io.js-3.3/d8/d7d/classv8_1_1_maybe_local.html) for further details. ### Nan::Maybe A simple `Nan::Maybe` type, representing an object which may or may not have a value, see https://hackage.haskell.org/package/base/docs/Data-Maybe.html. If an API method returns a `Nan::Maybe<>`, the API method can potentially fail either because an exception is thrown, or because an exception is pending, e.g. because a previous API call threw an exception that hasn't been caught yet, or because a `v8::TerminateExecution` exception was thrown. In that case, a "Nothing" value is returned. Definition: ```c++ template class Nan::Maybe { public: bool IsNothing() const; bool IsJust() const; // Will crash if the Maybe<> is nothing. T FromJust(); T FromMaybe(const T& default_value); bool operator==(const Maybe &other); bool operator!=(const Maybe &other); }; ``` See the documentation for [`v8::Maybe`](https://v8docs.nodesource.com/io.js-3.3/d9/d4b/classv8_1_1_maybe.html) for further details. ### Nan::Nothing Construct an empty `Nan::Maybe` type representing _nothing_. ```c++ template Nan::Maybe Nan::Nothing(); ``` ### Nan::Just Construct a `Nan::Maybe` type representing _just_ a value. ```c++ template Nan::Maybe Nan::Just(const T &t); ``` ### Nan::Call() A helper method for calling [`v8::Function#Call()`](https://v8docs.nodesource.com/io.js-3.3/d5/d54/classv8_1_1_function.html#a468a89f737af0612db10132799c827c0) in a way compatible across supported versions of V8. Signature: ```c++ Nan::MaybeLocal Nan::Call(v8::Local fun, v8::Local recv, int argc, v8::Local argv[]); ``` ### Nan::ToDetailString() A helper method for calling [`v8::Value#ToDetailString()`](https://v8docs.nodesource.com/io.js-3.3/dc/d0a/classv8_1_1_value.html#a2f9770296dc2c8d274bc8cc0dca243e5) in a way compatible across supported versions of V8. Signature: ```c++ Nan::MaybeLocal Nan::ToDetailString(v8::Local val); ``` ### Nan::ToArrayIndex() A helper method for calling [`v8::Value#ToArrayIndex()`](https://v8docs.nodesource.com/io.js-3.3/dc/d0a/classv8_1_1_value.html#acc5bbef3c805ec458470c0fcd6f13493) in a way compatible across supported versions of V8. Signature: ```c++ Nan::MaybeLocal Nan::ToArrayIndex(v8::Local val); ``` ### Nan::Equals() A helper method for calling [`v8::Value#Equals()`](https://v8docs.nodesource.com/io.js-3.3/dc/d0a/classv8_1_1_value.html#a0d9616ab2de899d4e3047c30a10c9285) in a way compatible across supported versions of V8. Signature: ```c++ Nan::Maybe Nan::Equals(v8::Local a, v8::Local(b)); ``` ### Nan::NewInstance() A helper method for calling [`v8::Function#NewInstance()`](https://v8docs.nodesource.com/io.js-3.3/d5/d54/classv8_1_1_function.html#a691b13f7a553069732cbacf5ac8c62ec) and [`v8::ObjectTemplate#NewInstance()`](https://v8docs.nodesource.com/io.js-3.3/db/d5f/classv8_1_1_object_template.html#ad605a7543cfbc5dab54cdb0883d14ae4) in a way compatible across supported versions of V8. Signature: ```c++ Nan::MaybeLocal Nan::NewInstance(v8::Local h); Nan::MaybeLocal Nan::NewInstance(v8::Local h, int argc, v8::Local argv[]); Nan::MaybeLocal Nan::NewInstance(v8::Local h); ``` ### Nan::GetFunction() A helper method for calling [`v8::FunctionTemplate#GetFunction()`](https://v8docs.nodesource.com/io.js-3.3/d8/d83/classv8_1_1_function_template.html#a56d904662a86eca78da37d9bb0ed3705) in a way compatible across supported versions of V8. Signature: ```c++ Nan::MaybeLocal Nan::GetFunction(v8::Local t); ``` ### Nan::Set() A helper method for calling [`v8::Object#Set()`](https://v8docs.nodesource.com/io.js-3.3/db/d85/classv8_1_1_object.html#a67604ea3734f170c66026064ea808f20) in a way compatible across supported versions of V8. Signature: ```c++ Nan::Maybe Nan::Set(v8::Local obj, v8::Local key, v8::Local value) Nan::Maybe Nan::Set(v8::Local obj, uint32_t index, v8::Local value); ``` ### Nan::DefineOwnProperty() A helper method for calling [`v8::Object#DefineOwnProperty()`](https://v8docs.nodesource.com/node-4.8/db/d85/classv8_1_1_object.html#a6f76b2ed605cb8f9185b92de0033a820) in a way compatible across supported versions of V8. Signature: ```c++ Nan::Maybe Nan::DefineOwnProperty(v8::Local obj, v8::Local key, v8::Local value, v8::PropertyAttribute attribs = v8::None); ``` ### Nan::ForceSet() Deprecated, use Nan::DefineOwnProperty(). A helper method for calling [`v8::Object#ForceSet()`](https://v8docs.nodesource.com/io.js-3.3/db/d85/classv8_1_1_object.html#a796b7b682896fb64bf1872747734e836) in a way compatible across supported versions of V8. Signature: ```c++ NAN_DEPRECATED Nan::Maybe Nan::ForceSet(v8::Local obj, v8::Local key, v8::Local value, v8::PropertyAttribute attribs = v8::None); ``` ### Nan::Get() A helper method for calling [`v8::Object#Get()`](https://v8docs.nodesource.com/io.js-3.3/db/d85/classv8_1_1_object.html#a2565f03e736694f6b1e1cf22a0b4eac2) in a way compatible across supported versions of V8. Signature: ```c++ Nan::MaybeLocal Nan::Get(v8::Local obj, v8::Local key); Nan::MaybeLocal Nan::Get(v8::Local obj, uint32_t index); ``` ### Nan::GetPropertyAttributes() A helper method for calling [`v8::Object#GetPropertyAttributes()`](https://v8docs.nodesource.com/io.js-3.3/db/d85/classv8_1_1_object.html#a9b898894da3d1db2714fd9325a54fe57) in a way compatible across supported versions of V8. Signature: ```c++ Nan::Maybe Nan::GetPropertyAttributes( v8::Local obj, v8::Local key); ``` ### Nan::Has() A helper method for calling [`v8::Object#Has()`](https://v8docs.nodesource.com/io.js-3.3/db/d85/classv8_1_1_object.html#ab3c3d89ea7c2f9afd08965bd7299a41d) in a way compatible across supported versions of V8. Signature: ```c++ Nan::Maybe Nan::Has(v8::Local obj, v8::Local key); Nan::Maybe Nan::Has(v8::Local obj, uint32_t index); ``` ### Nan::Delete() A helper method for calling [`v8::Object#Delete()`](https://v8docs.nodesource.com/io.js-3.3/db/d85/classv8_1_1_object.html#a2fa0f5a592582434ed1ceceff7d891ef) in a way compatible across supported versions of V8. Signature: ```c++ Nan::Maybe Nan::Delete(v8::Local obj, v8::Local key); Nan::Maybe Nan::Delete(v8::Local obj, uint32_t index); ``` ### Nan::GetPropertyNames() A helper method for calling [`v8::Object#GetPropertyNames()`](https://v8docs.nodesource.com/io.js-3.3/db/d85/classv8_1_1_object.html#aced885270cfd2c956367b5eedc7fbfe8) in a way compatible across supported versions of V8. Signature: ```c++ Nan::MaybeLocal Nan::GetPropertyNames(v8::Local obj); ``` ### Nan::GetOwnPropertyNames() A helper method for calling [`v8::Object#GetOwnPropertyNames()`](https://v8docs.nodesource.com/io.js-3.3/db/d85/classv8_1_1_object.html#a79a6e4d66049b9aa648ed4dfdb23e6eb) in a way compatible across supported versions of V8. Signature: ```c++ Nan::MaybeLocal Nan::GetOwnPropertyNames(v8::Local obj); ``` ### Nan::SetPrototype() A helper method for calling [`v8::Object#SetPrototype()`](https://v8docs.nodesource.com/io.js-3.3/db/d85/classv8_1_1_object.html#a442706b22fceda6e6d1f632122a9a9f4) in a way compatible across supported versions of V8. Signature: ```c++ Nan::Maybe Nan::SetPrototype(v8::Local obj, v8::Local prototype); ``` ### Nan::ObjectProtoToString() A helper method for calling [`v8::Object#ObjectProtoToString()`](https://v8docs.nodesource.com/io.js-3.3/db/d85/classv8_1_1_object.html#ab7a92b4dcf822bef72f6c0ac6fea1f0b) in a way compatible across supported versions of V8. Signature: ```c++ Nan::MaybeLocal Nan::ObjectProtoToString(v8::Local obj); ``` ### Nan::HasOwnProperty() A helper method for calling [`v8::Object#HasOwnProperty()`](https://v8docs.nodesource.com/io.js-3.3/db/d85/classv8_1_1_object.html#ab7b7245442ca6de1e1c145ea3fd653ff) in a way compatible across supported versions of V8. Signature: ```c++ Nan::Maybe Nan::HasOwnProperty(v8::Local obj, v8::Local key); ``` ### Nan::HasRealNamedProperty() A helper method for calling [`v8::Object#HasRealNamedProperty()`](https://v8docs.nodesource.com/io.js-3.3/db/d85/classv8_1_1_object.html#ad8b80a59c9eb3c1e6c3cd6c84571f767) in a way compatible across supported versions of V8. Signature: ```c++ Nan::Maybe Nan::HasRealNamedProperty(v8::Local obj, v8::Local key); ``` ### Nan::HasRealIndexedProperty() A helper method for calling [`v8::Object#HasRealIndexedProperty()`](https://v8docs.nodesource.com/io.js-3.3/db/d85/classv8_1_1_object.html#af94fc1135a5e74a2193fb72c3a1b9855) in a way compatible across supported versions of V8. Signature: ```c++ Nan::Maybe Nan::HasRealIndexedProperty(v8::Local obj, uint32_t index); ``` ### Nan::HasRealNamedCallbackProperty() A helper method for calling [`v8::Object#HasRealNamedCallbackProperty()`](https://v8docs.nodesource.com/io.js-3.3/db/d85/classv8_1_1_object.html#af743b7ea132b89f84d34d164d0668811) in a way compatible across supported versions of V8. Signature: ```c++ Nan::Maybe Nan::HasRealNamedCallbackProperty( v8::Local obj, v8::Local key); ``` ### Nan::GetRealNamedPropertyInPrototypeChain() A helper method for calling [`v8::Object#GetRealNamedPropertyInPrototypeChain()`](https://v8docs.nodesource.com/io.js-3.3/db/d85/classv8_1_1_object.html#a8700b1862e6b4783716964ba4d5e6172) in a way compatible across supported versions of V8. Signature: ```c++ Nan::MaybeLocal Nan::GetRealNamedPropertyInPrototypeChain( v8::Local obj, v8::Local key); ``` ### Nan::GetRealNamedProperty() A helper method for calling [`v8::Object#GetRealNamedProperty()`](https://v8docs.nodesource.com/io.js-3.3/db/d85/classv8_1_1_object.html#a84471a824576a5994fdd0ffcbf99ccc0) in a way compatible across supported versions of V8. Signature: ```c++ Nan::MaybeLocal Nan::GetRealNamedProperty(v8::Local obj, v8::Local key); ``` ### Nan::CallAsFunction() A helper method for calling [`v8::Object#CallAsFunction()`](https://v8docs.nodesource.com/io.js-3.3/db/d85/classv8_1_1_object.html#a9ef18be634e79b4f0cdffa1667a29f58) in a way compatible across supported versions of V8. Signature: ```c++ Nan::MaybeLocal Nan::CallAsFunction(v8::Local obj, v8::Local recv, int argc, v8::Local argv[]); ``` ### Nan::CallAsConstructor() A helper method for calling [`v8::Object#CallAsConstructor()`](https://v8docs.nodesource.com/io.js-3.3/db/d85/classv8_1_1_object.html#a50d571de50d0b0dfb28795619d07a01b) in a way compatible across supported versions of V8. Signature: ```c++ Nan::MaybeLocal Nan::CallAsConstructor(v8::Local obj, int argc, v8::Local argv[]); ``` ### Nan::GetSourceLine() A helper method for calling [`v8::Message#GetSourceLine()`](https://v8docs.nodesource.com/io.js-3.3/d9/d28/classv8_1_1_message.html#a849f7a6c41549d83d8159825efccd23a) in a way compatible across supported versions of V8. Signature: ```c++ Nan::MaybeLocal Nan::GetSourceLine(v8::Local msg); ``` ### Nan::GetLineNumber() A helper method for calling [`v8::Message#GetLineNumber()`](https://v8docs.nodesource.com/io.js-3.3/d9/d28/classv8_1_1_message.html#adbe46c10a88a6565f2732a2d2adf99b9) in a way compatible across supported versions of V8. Signature: ```c++ Nan::Maybe Nan::GetLineNumber(v8::Local msg); ``` ### Nan::GetStartColumn() A helper method for calling [`v8::Message#GetStartColumn()`](https://v8docs.nodesource.com/io.js-3.3/d9/d28/classv8_1_1_message.html#a60ede616ba3822d712e44c7a74487ba6) in a way compatible across supported versions of V8. Signature: ```c++ Nan::Maybe Nan::GetStartColumn(v8::Local msg); ``` ### Nan::GetEndColumn() A helper method for calling [`v8::Message#GetEndColumn()`](https://v8docs.nodesource.com/io.js-3.3/d9/d28/classv8_1_1_message.html#aaa004cf19e529da980bc19fcb76d93be) in a way compatible across supported versions of V8. Signature: ```c++ Nan::Maybe Nan::GetEndColumn(v8::Local msg); ``` ### Nan::CloneElementAt() A helper method for calling [`v8::Array#CloneElementAt()`](https://v8docs.nodesource.com/io.js-3.3/d3/d32/classv8_1_1_array.html#a1d3a878d4c1c7cae974dd50a1639245e) in a way compatible across supported versions of V8. Signature: ```c++ Nan::MaybeLocal Nan::CloneElementAt(v8::Local array, uint32_t index); ``` ### Nan::HasPrivate() A helper method for calling [`v8::Object#HasPrivate()`](https://v8docs.nodesource.com/node-7.2/db/d85/classv8_1_1_object.html#af68a0b98066cfdeb8f943e98a40ba08d) in a way compatible across supported versions of V8. Signature: ```c++ Nan::Maybe Nan::HasPrivate(v8::Local object, v8::Local key); ``` ### Nan::GetPrivate() A helper method for calling [`v8::Object#GetPrivate()`](https://v8docs.nodesource.com/node-7.2/db/d85/classv8_1_1_object.html#a169f2da506acbec34deadd9149a1925a) in a way compatible across supported versions of V8. Signature: ```c++ Nan::MaybeLocal Nan::GetPrivate(v8::Local object, v8::Local key); ``` ### Nan::SetPrivate() A helper method for calling [`v8::Object#SetPrivate()`](https://v8docs.nodesource.com/node-7.2/db/d85/classv8_1_1_object.html#ace1769b0f3b86bfe9fda1010916360ee) in a way compatible across supported versions of V8. Signature: ```c++ Nan::Maybe Nan::SetPrivate(v8::Local object, v8::Local key, v8::Local value); ``` ### Nan::DeletePrivate() A helper method for calling [`v8::Object#DeletePrivate()`](https://v8docs.nodesource.com/node-7.2/db/d85/classv8_1_1_object.html#a138bb32a304f3982be02ad499693b8fd) in a way compatible across supported versions of V8. Signature: ```c++ Nan::Maybe Nan::DeletePrivate(v8::Local object, v8::Local key); ``` ### Nan::MakeMaybe() Wraps a `v8::Local<>` in a `Nan::MaybeLocal<>`. When called with a `Nan::MaybeLocal<>` it just returns its argument. This is useful in generic template code that builds on NAN. Synopsis: ```c++ MaybeLocal someNumber = MakeMaybe(New(3.141592654)); MaybeLocal someString = MakeMaybe(New("probably")); ``` Signature: ```c++ template class MaybeMaybe> Nan::MaybeLocal Nan::MakeMaybe(MaybeMaybe v); ```