diff --git a/doc/env.md b/doc/env.md index c04b72529..9842de6bb 100644 --- a/doc/env.md +++ b/doc/env.md @@ -57,7 +57,7 @@ Returns a `bool` indicating if an exception is pending in the environment. ### GetAndClearPendingException ```cpp -Napi::Error Napi::Env::GetAndClearPendingException(); +Napi::Error Napi::Env::GetAndClearPendingException() const; ``` Returns an `Napi::Error` object representing the environment's pending exception, if any. @@ -65,7 +65,7 @@ Returns an `Napi::Error` object representing the environment's pending exception ### RunScript ```cpp -Napi::Value Napi::Env::RunScript(____ script); +Napi::Value Napi::Env::RunScript(____ script) const; ``` - `[in] script`: A string containing JavaScript code to execute. @@ -78,7 +78,7 @@ The `script` can be any of the following types: ### GetInstanceData ```cpp -template T* GetInstanceData(); +template T* GetInstanceData() const; ``` Returns the instance data that was previously associated with the environment, @@ -89,7 +89,7 @@ or `nullptr` if none was associated. ```cpp template using Finalizer = void (*)(Env, T*); template fini = Env::DefaultFini> -void SetInstanceData(T* data); +void SetInstanceData(T* data) const; ``` - `[template] fini`: A function to call when the instance data is to be deleted. @@ -112,7 +112,7 @@ template fini = Env::DefaultFiniWithHint> -void SetInstanceData(DataType* data, HintType* hint); +void SetInstanceData(DataType* data, HintType* hint) const; ``` - `[template] fini`: A function to call when the instance data is to be deleted. diff --git a/doc/object.md b/doc/object.md index 677ff5532..28b409f2f 100644 --- a/doc/object.md +++ b/doc/object.md @@ -72,7 +72,7 @@ Creates a new `Napi::Object` value. ### Set() ```cpp -bool Napi::Object::Set (____ key, ____ value); +bool Napi::Object::Set (____ key, ____ value) const; ``` - `[in] key`: The name for the property being assigned. - `[in] value`: The value being assigned to the property. @@ -91,7 +91,7 @@ The `value` can be of any type that is accepted by [`Napi::Value::From`][]. ### Delete() ```cpp -bool Napi::Object::Delete(____ key); +bool Napi::Object::Delete(____ key) const; ``` - `[in] key`: The name of the property to delete. @@ -143,7 +143,7 @@ Note: This is equivalent to the JavaScript instanceof operator. ### AddFinalizer() ```cpp template -inline void AddFinalizer(Finalizer finalizeCallback, T* data); +inline void AddFinalizer(Finalizer finalizeCallback, T* data) const; ``` - `[in] finalizeCallback`: The function to call when the object is garbage-collected. @@ -161,7 +161,7 @@ where `data` is the pointer that was passed into the call to `AddFinalizer()`. template inline void AddFinalizer(Finalizer finalizeCallback, T* data, - Hint* finalizeHint); + Hint* finalizeHint) const; ``` - `[in] data`: The data to associate with the object. @@ -184,7 +184,7 @@ The properties whose key is a `Symbol` will not be included. ### HasOwnProperty() ```cpp -bool Napi::Object::HasOwnProperty(____ key); const +bool Napi::Object::HasOwnProperty(____ key) const; ``` - `[in] key` The name of the property to check. @@ -200,7 +200,7 @@ The key can be any of the following types: ### DefineProperty() ```cpp -bool Napi::Object::DefineProperty (const Napi::PropertyDescriptor& property); +bool Napi::Object::DefineProperty (const Napi::PropertyDescriptor& property) const; ``` - `[in] property`: A [`Napi::PropertyDescriptor`](property_descriptor.md). @@ -209,7 +209,7 @@ Define a property on the object. ### DefineProperties() ```cpp -bool Napi::Object::DefineProperties (____ properties) +bool Napi::Object::DefineProperties (____ properties) const; ``` - `[in] properties`: A list of [`Napi::PropertyDescriptor`](property_descriptor.md). Can be one of the following types: - const std::initializer_list& @@ -220,7 +220,7 @@ Defines properties on the object. ### Freeze() ```cpp -void Napi::Object::Freeze() +void Napi::Object::Freeze() const; ``` The `Napi::Object::Freeze()` method freezes an object. A frozen object can no @@ -233,7 +233,7 @@ freezing an object also prevents its prototype from being changed. ### Seal() ```cpp -void Napi::Object::Seal() +void Napi::Object::Seal() const; ``` The `Napi::Object::Seal()` method seals an object, preventing new properties @@ -244,7 +244,7 @@ writable. ### operator\[\]() ```cpp -Napi::PropertyLValue Napi::Object::operator[] (const char* utf8name); +Napi::PropertyLValue Napi::Object::operator[] (const char* utf8name) const; ``` - `[in] utf8name`: UTF-8 encoded null-terminated property name. @@ -252,7 +252,7 @@ Returns a [`Napi::Object::PropertyLValue`](propertylvalue.md) as the named property or sets the named property. ```cpp -Napi::PropertyLValue Napi::Object::operator[] (const std::string& utf8name); +Napi::PropertyLValue Napi::Object::operator[] (const std::string& utf8name) const; ``` - `[in] utf8name`: UTF-8 encoded property name. @@ -260,47 +260,20 @@ Returns a [`Napi::Object::PropertyLValue`](propertylvalue.md) as the named property or sets the named property. ```cpp -Napi::PropertyLValue Napi::Object::operator[] (uint32_t index); +Napi::PropertyLValue Napi::Object::operator[] (uint32_t index) const; ``` - `[in] index`: Element index. Returns a [`Napi::Object::PropertyLValue`](propertylvalue.md) or sets an indexed property or array element. -```cpp -Napi::Value Napi::Object::operator[] (const char* utf8name) const; -``` -- `[in] utf8name`: UTF-8 encoded null-terminated property name. - -Returns the named property as a [`Napi::Value`](value.md). - -```cpp -Napi::Value Napi::Object::operator[] (const std::string& utf8name) const; -``` -- `[in] utf8name`: UTF-8 encoded property name. - -Returns the named property as a [`Napi::Value`](value.md). - -```cpp -Napi::Value Napi::Object::operator[] (uint32_t index) const; -``` -- `[in] index`: Element index. - -Returns an indexed property or array element as a [`Napi::Value`](value.md). - ### begin() ```cpp Napi::Object::iterator Napi::Object::begin() const; ``` -Returns a constant iterator to the beginning of the object. - -```cpp -Napi::Object::iterator Napi::Object::begin(); -``` - -Returns a non constant iterator to the beginning of the object. +Returns an iterator to the beginning of the object. ### end() @@ -308,13 +281,7 @@ Returns a non constant iterator to the beginning of the object. Napi::Object::iterator Napi::Object::end() const; ``` -Returns a constant iterator to the end of the object. - -```cpp -Napi::Object::iterator Napi::Object::end(); -``` - -Returns a non constant iterator to the end of the object. +Returns an iterator to the end of the object. ## Iterator @@ -324,63 +291,7 @@ Iterators expose an `std::pair<...>`, where the `first` property is a holds the currently iterated value. Iterators are only available if C++ exceptions are enabled (by defining `NAPI_CPP_EXCEPTIONS` during the build). -### Constant Iterator - -In constant iterators, the iterated values are immutable. - -#### operator++() - -```cpp -inline Napi::Object::const_iterator& Napi::Object::const_iterator::operator++(); -``` - -Moves the iterator one step forward. - -#### operator== - -```cpp -inline bool Napi::Object::const_iterator::operator==(const Napi::Object::const_iterator& other) const; -``` -- `[in] other`: Another iterator to compare the current iterator to. - -Returns whether both iterators are at the same index. - -#### operator!= - -```cpp -inline bool Napi::Object::const_iterator::operator!=(const Napi::Object::const_iterator& other) const; -``` -- `[in] other`: Another iterator to compare the current iterator to. - -Returns whether both iterators are at different indices. - -#### operator*() - -```cpp -inline const std::pair> Napi::Object::const_iterator::operator*() const; -``` - -Returns the currently iterated key and value. - -#### Example -```cpp -Value Sum(const CallbackInfo& info) { - Object object = info[0].As(); - int64_t sum = 0; - - for (const auto& e : object) { - sum += static_cast(e.second).As().Int64Value(); - } - - return Number::New(info.Env(), sum); -} -``` - -### Non Constant Iterator - -In non constant iterators, the iterated values are mutable. - -#### operator++() +### operator++() ```cpp inline Napi::Object::iterator& Napi::Object::iterator::operator++(); @@ -388,7 +299,7 @@ inline Napi::Object::iterator& Napi::Object::iterator::operator++(); Moves the iterator one step forward. -#### operator== +### operator== ```cpp inline bool Napi::Object::iterator::operator==(const Napi::Object::iterator& other) const; @@ -397,7 +308,7 @@ inline bool Napi::Object::iterator::operator==(const Napi::Object::iterator& oth Returns whether both iterators are at the same index. -#### operator!= +### operator!= ```cpp inline bool Napi::Object::iterator::operator!=(const Napi::Object::iterator& other) const; @@ -406,15 +317,15 @@ inline bool Napi::Object::iterator::operator!=(const Napi::Object::iterator& oth Returns whether both iterators are at different indices. -#### operator*() +### operator*() ```cpp -inline std::pair> Napi::Object::iterator::operator*(); +inline std::pair> Napi::Object::iterator::operator*() const; ``` Returns the currently iterated key and value. -#### Example +### Example ```cpp void Increment(const CallbackInfo& info) { Env env = info.Env(); diff --git a/doc/object_reference.md b/doc/object_reference.md index 38d6acc0e..1ee697980 100644 --- a/doc/object_reference.md +++ b/doc/object_reference.md @@ -103,7 +103,7 @@ The `value` can be any of the following types: ### Get ```cpp -Napi::Value Napi::ObjectReference::Get(___ key); +Napi::Value Napi::ObjectReference::Get(___ key) const; ``` * `[in] key`: The name of the property to return the value for. diff --git a/doc/reference.md b/doc/reference.md index 42ddc0609..0420990e7 100644 --- a/doc/reference.md +++ b/doc/reference.md @@ -69,7 +69,7 @@ Returns the value held by the `Napi::Reference`. ### Ref ```cpp -uint32_t Napi::Reference::Ref(); +uint32_t Napi::Reference::Ref() const; ``` Increments the reference count for the `Napi::Reference` and returns the resulting reference count. Throws an error if the increment fails. @@ -77,7 +77,7 @@ Increments the reference count for the `Napi::Reference` and returns the resulti ### Unref ```cpp -uint32_t Napi::Reference::Unref(); +uint32_t Napi::Reference::Unref() const; ``` Decrements the reference count for the `Napi::Reference` and returns the resulting reference count. Throws an error if the decrement fails. diff --git a/doc/threadsafe_function.md b/doc/threadsafe_function.md index 6943bacf2..fcbc2dff1 100644 --- a/doc/threadsafe_function.md +++ b/doc/threadsafe_function.md @@ -83,7 +83,7 @@ Add a thread to this thread-safe function object, indicating that a new thread will start making use of the thread-safe function. ```cpp -napi_status Napi::ThreadSafeFunction::Acquire() +napi_status Napi::ThreadSafeFunction::Acquire() const ``` Returns one of: @@ -100,7 +100,7 @@ thread-safe function. Using any thread-safe APIs after having called this API has undefined results in the current thread, as it may have been destroyed. ```cpp -napi_status Napi::ThreadSafeFunction::Release() +napi_status Napi::ThreadSafeFunction::Release() const ``` Returns one of: @@ -122,7 +122,7 @@ make no further use of the thread-safe function because it is no longer guaranteed to be allocated. ```cpp -napi_status Napi::ThreadSafeFunction::Abort() +napi_status Napi::ThreadSafeFunction::Abort() const ``` Returns one of: diff --git a/doc/typed_threadsafe_function.md b/doc/typed_threadsafe_function.md index efd173482..74d3cc2ed 100644 --- a/doc/typed_threadsafe_function.md +++ b/doc/typed_threadsafe_function.md @@ -124,7 +124,7 @@ has undefined results in the current thread, as the thread-safe function may have been destroyed. ```cpp -napi_status Napi::TypedThreadSafeFunction::Release() +napi_status Napi::TypedThreadSafeFunction::Release() const ``` Returns one of: @@ -146,7 +146,7 @@ function call a thread must make no further use of the thread-safe function because it is no longer guaranteed to be allocated. ```cpp -napi_status Napi::TypedThreadSafeFunction::Abort() +napi_status Napi::TypedThreadSafeFunction::Abort() const ``` Returns one of: diff --git a/napi-inl.h b/napi-inl.h index bd54feb67..800cc6af4 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -483,7 +483,7 @@ inline bool Env::IsExceptionPending() const { return result; } -inline Error Env::GetAndClearPendingException() { +inline Error Env::GetAndClearPendingException() const { napi_value value; napi_status status = napi_get_and_clear_last_exception(_env, &value); if (status != napi_ok) { @@ -493,16 +493,16 @@ inline Error Env::GetAndClearPendingException() { return Error(_env, value); } -inline MaybeOrValue Env::RunScript(const char* utf8script) { +inline MaybeOrValue Env::RunScript(const char* utf8script) const { String script = String::New(_env, utf8script); return RunScript(script); } -inline MaybeOrValue Env::RunScript(const std::string& utf8script) { +inline MaybeOrValue Env::RunScript(const std::string& utf8script) const { return RunScript(utf8script.c_str()); } -inline MaybeOrValue Env::RunScript(String script) { +inline MaybeOrValue Env::RunScript(String script) const { napi_value result; napi_status status = napi_run_script(_env, script, &result); NAPI_RETURN_OR_THROW_IF_FAILED( @@ -531,7 +531,7 @@ void Env::CleanupHook::WrapperWithArg(void* data) NAPI_NOEXCEPT { #if NAPI_VERSION > 5 template fini> -inline void Env::SetInstanceData(T* data) { +inline void Env::SetInstanceData(T* data) const { napi_status status = napi_set_instance_data(_env, data, [](napi_env env, void* data, void*) { fini(env, static_cast(data)); @@ -542,7 +542,7 @@ inline void Env::SetInstanceData(T* data) { template fini> -inline void Env::SetInstanceData(DataType* data, HintType* hint) { +inline void Env::SetInstanceData(DataType* data, HintType* hint) const { napi_status status = napi_set_instance_data(_env, data, [](napi_env env, void* data, void* hint) { @@ -552,7 +552,7 @@ inline void Env::SetInstanceData(DataType* data, HintType* hint) { } template -inline T* Env::GetInstanceData() { +inline T* Env::GetInstanceData() const { void* data = nullptr; napi_status status = napi_get_instance_data(_env, &data); @@ -1294,39 +1294,25 @@ inline Object::Object() : Value() { inline Object::Object(napi_env env, napi_value value) : Value(env, value) { } -inline Object::PropertyLValue Object::operator [](const char* utf8name) { +inline Object::PropertyLValue Object::operator[]( + const char* utf8name) const { return PropertyLValue(*this, utf8name); } -inline Object::PropertyLValue Object::operator [](const std::string& utf8name) { +inline Object::PropertyLValue Object::operator[]( + const std::string& utf8name) const { return PropertyLValue(*this, utf8name); } -inline Object::PropertyLValue Object::operator [](uint32_t index) { +inline Object::PropertyLValue Object::operator[]( + uint32_t index) const { return PropertyLValue(*this, index); } -inline Object::PropertyLValue Object::operator[](Value index) { - return PropertyLValue(*this, index); -} - inline Object::PropertyLValue Object::operator[](Value index) const { return PropertyLValue(*this, index); } -inline MaybeOrValue Object::operator[](const char* utf8name) const { - return Get(utf8name); -} - -inline MaybeOrValue Object::operator[]( - const std::string& utf8name) const { - return Get(utf8name); -} - -inline MaybeOrValue Object::operator[](uint32_t index) const { - return Get(index); -} - inline MaybeOrValue Object::Has(napi_value key) const { bool result; napi_status status = napi_has_property(_env, _value, key, &result); @@ -1396,14 +1382,15 @@ inline MaybeOrValue Object::Get(const std::string& utf8name) const { } template -inline MaybeOrValue Object::Set(napi_value key, const ValueType& value) { +inline MaybeOrValue Object::Set(napi_value key, + const ValueType& value) const { napi_status status = napi_set_property(_env, _value, key, Value::From(_env, value)); NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool); } template -inline MaybeOrValue Object::Set(Value key, const ValueType& value) { +inline MaybeOrValue Object::Set(Value key, const ValueType& value) const { napi_status status = napi_set_property(_env, _value, key, Value::From(_env, value)); NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool); @@ -1411,7 +1398,7 @@ inline MaybeOrValue Object::Set(Value key, const ValueType& value) { template inline MaybeOrValue Object::Set(const char* utf8name, - const ValueType& value) { + const ValueType& value) const { napi_status status = napi_set_named_property(_env, _value, utf8name, Value::From(_env, value)); NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool); @@ -1419,27 +1406,27 @@ inline MaybeOrValue Object::Set(const char* utf8name, template inline MaybeOrValue Object::Set(const std::string& utf8name, - const ValueType& value) { + const ValueType& value) const { return Set(utf8name.c_str(), value); } -inline MaybeOrValue Object::Delete(napi_value key) { +inline MaybeOrValue Object::Delete(napi_value key) const { bool result; napi_status status = napi_delete_property(_env, _value, key, &result); NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, result, bool); } -inline MaybeOrValue Object::Delete(Value key) { +inline MaybeOrValue Object::Delete(Value key) const { bool result; napi_status status = napi_delete_property(_env, _value, key, &result); NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, result, bool); } -inline MaybeOrValue Object::Delete(const char* utf8name) { +inline MaybeOrValue Object::Delete(const char* utf8name) const { return Delete(String::New(_env, utf8name)); } -inline MaybeOrValue Object::Delete(const std::string& utf8name) { +inline MaybeOrValue Object::Delete(const std::string& utf8name) const { return Delete(String::New(_env, utf8name)); } @@ -1456,13 +1443,14 @@ inline MaybeOrValue Object::Get(uint32_t index) const { } template -inline MaybeOrValue Object::Set(uint32_t index, const ValueType& value) { +inline MaybeOrValue Object::Set(uint32_t index, + const ValueType& value) const { napi_status status = napi_set_element(_env, _value, index, Value::From(_env, value)); NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool); } -inline MaybeOrValue Object::Delete(uint32_t index) { +inline MaybeOrValue Object::Delete(uint32_t index) const { bool result; napi_status status = napi_delete_element(_env, _value, index, &result); NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, result, bool); @@ -1475,21 +1463,21 @@ inline MaybeOrValue Object::GetPropertyNames() const { } inline MaybeOrValue Object::DefineProperty( - const PropertyDescriptor& property) { + const PropertyDescriptor& property) const { napi_status status = napi_define_properties(_env, _value, 1, reinterpret_cast(&property)); NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool); } inline MaybeOrValue Object::DefineProperties( - const std::initializer_list& properties) { + const std::initializer_list& properties) const { napi_status status = napi_define_properties(_env, _value, properties.size(), reinterpret_cast(properties.begin())); NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool); } inline MaybeOrValue Object::DefineProperties( - const std::vector& properties) { + const std::vector& properties) const { napi_status status = napi_define_properties(_env, _value, properties.size(), reinterpret_cast(properties.data())); NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool); @@ -1503,7 +1491,7 @@ inline MaybeOrValue Object::InstanceOf( } template -inline void Object::AddFinalizer(Finalizer finalizeCallback, T* data) { +inline void Object::AddFinalizer(Finalizer finalizeCallback, T* data) const { details::FinalizeData* finalizeData = new details::FinalizeData( {std::move(finalizeCallback), nullptr}); @@ -1522,7 +1510,7 @@ inline void Object::AddFinalizer(Finalizer finalizeCallback, T* data) { template inline void Object::AddFinalizer(Finalizer finalizeCallback, T* data, - Hint* finalizeHint) { + Hint* finalizeHint) const { details::FinalizeData* finalizeData = new details::FinalizeData( {std::move(finalizeCallback), finalizeHint}); @@ -1539,57 +1527,18 @@ inline void Object::AddFinalizer(Finalizer finalizeCallback, } #ifdef NAPI_CPP_EXCEPTIONS -inline Object::const_iterator::const_iterator(const Object* object, - const Type type) { - _object = object; - _keys = object->GetPropertyNames(); - _index = type == Type::BEGIN ? 0 : _keys.Length(); -} - -inline Object::const_iterator Napi::Object::begin() const { - const_iterator it(this, Object::const_iterator::Type::BEGIN); - return it; -} - -inline Object::const_iterator Napi::Object::end() const { - const_iterator it(this, Object::const_iterator::Type::END); - return it; -} - -inline Object::const_iterator& Object::const_iterator::operator++() { - ++_index; - return *this; -} - -inline bool Object::const_iterator::operator==( - const const_iterator& other) const { - return _index == other._index; -} - -inline bool Object::const_iterator::operator!=( - const const_iterator& other) const { - return _index != other._index; -} - -inline const std::pair> -Object::const_iterator::operator*() const { - const Value key = _keys[_index]; - const PropertyLValue value = (*_object)[key]; - return {key, value}; -} - -inline Object::iterator::iterator(Object* object, const Type type) { +inline Object::iterator::iterator(const Object* object, const Type type) { _object = object; _keys = object->GetPropertyNames(); _index = type == Type::BEGIN ? 0 : _keys.Length(); } -inline Object::iterator Napi::Object::begin() { +inline Object::iterator Napi::Object::begin() const { iterator it(this, Object::iterator::Type::BEGIN); return it; } -inline Object::iterator Napi::Object::end() { +inline Object::iterator Napi::Object::end() const { iterator it(this, Object::iterator::Type::END); return it; } @@ -1608,7 +1557,7 @@ inline bool Object::iterator::operator!=(const iterator& other) const { } inline std::pair> -Object::iterator::operator*() { +Object::iterator::operator*() const { Value key = _keys[_index]; PropertyLValue value = (*_object)[key]; return {key, value}; @@ -1616,12 +1565,12 @@ Object::iterator::operator*() { #endif // NAPI_CPP_EXCEPTIONS #if NAPI_VERSION >= 8 -inline MaybeOrValue Object::Freeze() { +inline MaybeOrValue Object::Freeze() const { napi_status status = napi_object_freeze(_env, _value); NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool); } -inline MaybeOrValue Object::Seal() { +inline MaybeOrValue Object::Seal() const { napi_status status = napi_object_seal(_env, _value); NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool); } @@ -2930,7 +2879,7 @@ inline T Reference::Value() const { } template -inline uint32_t Reference::Ref() { +inline uint32_t Reference::Ref() const { uint32_t result; napi_status status = napi_reference_ref(_env, _ref, &result); NAPI_THROW_IF_FAILED(_env, status, 0); @@ -2938,7 +2887,7 @@ inline uint32_t Reference::Ref() { } template -inline uint32_t Reference::Unref() { +inline uint32_t Reference::Unref() const { uint32_t result; napi_status status = napi_reference_unref(_env, _ref, &result); NAPI_THROW_IF_FAILED(_env, status, 0); @@ -3064,61 +3013,61 @@ inline MaybeOrValue ObjectReference::Get( } inline MaybeOrValue ObjectReference::Set(const char* utf8name, - napi_value value) { + napi_value value) const { HandleScope scope(_env); return Value().Set(utf8name, value); } inline MaybeOrValue ObjectReference::Set(const char* utf8name, - Napi::Value value) { + Napi::Value value) const { HandleScope scope(_env); return Value().Set(utf8name, value); } inline MaybeOrValue ObjectReference::Set(const char* utf8name, - const char* utf8value) { + const char* utf8value) const { HandleScope scope(_env); return Value().Set(utf8name, utf8value); } inline MaybeOrValue ObjectReference::Set(const char* utf8name, - bool boolValue) { + bool boolValue) const { HandleScope scope(_env); return Value().Set(utf8name, boolValue); } inline MaybeOrValue ObjectReference::Set(const char* utf8name, - double numberValue) { + double numberValue) const { HandleScope scope(_env); return Value().Set(utf8name, numberValue); } inline MaybeOrValue ObjectReference::Set(const std::string& utf8name, - napi_value value) { + napi_value value) const { HandleScope scope(_env); return Value().Set(utf8name, value); } inline MaybeOrValue ObjectReference::Set(const std::string& utf8name, - Napi::Value value) { + Napi::Value value) const { HandleScope scope(_env); return Value().Set(utf8name, value); } inline MaybeOrValue ObjectReference::Set(const std::string& utf8name, - std::string& utf8value) { + std::string& utf8value) const { HandleScope scope(_env); return Value().Set(utf8name, utf8value); } inline MaybeOrValue ObjectReference::Set(const std::string& utf8name, - bool boolValue) { + bool boolValue) const { HandleScope scope(_env); return Value().Set(utf8name, boolValue); } inline MaybeOrValue ObjectReference::Set(const std::string& utf8name, - double numberValue) { + double numberValue) const { HandleScope scope(_env); return Value().Set(utf8name, numberValue); } @@ -3140,36 +3089,37 @@ inline MaybeOrValue ObjectReference::Get(uint32_t index) const { } inline MaybeOrValue ObjectReference::Set(uint32_t index, - napi_value value) { + napi_value value) const { HandleScope scope(_env); return Value().Set(index, value); } inline MaybeOrValue ObjectReference::Set(uint32_t index, - Napi::Value value) { + Napi::Value value) const { HandleScope scope(_env); return Value().Set(index, value); } inline MaybeOrValue ObjectReference::Set(uint32_t index, - const char* utf8value) { + const char* utf8value) const { HandleScope scope(_env); return Value().Set(index, utf8value); } -inline MaybeOrValue ObjectReference::Set(uint32_t index, - const std::string& utf8value) { +inline MaybeOrValue ObjectReference::Set( + uint32_t index, const std::string& utf8value) const { HandleScope scope(_env); return Value().Set(index, utf8value); } -inline MaybeOrValue ObjectReference::Set(uint32_t index, bool boolValue) { +inline MaybeOrValue ObjectReference::Set(uint32_t index, + bool boolValue) const { HandleScope scope(_env); return Value().Set(index, boolValue); } inline MaybeOrValue ObjectReference::Set(uint32_t index, - double numberValue) { + double numberValue) const { HandleScope scope(_env); return Value().Set(index, numberValue); } @@ -5323,7 +5273,7 @@ template inline napi_status -TypedThreadSafeFunction::Release() { +TypedThreadSafeFunction::Release() const { return napi_release_threadsafe_function(_tsfn, napi_tsfn_release); } @@ -5331,7 +5281,7 @@ template inline napi_status -TypedThreadSafeFunction::Abort() { +TypedThreadSafeFunction::Abort() const { return napi_release_threadsafe_function(_tsfn, napi_tsfn_abort); } @@ -5661,11 +5611,11 @@ inline napi_status ThreadSafeFunction::Acquire() const { return napi_acquire_threadsafe_function(_tsfn); } -inline napi_status ThreadSafeFunction::Release() { +inline napi_status ThreadSafeFunction::Release() const { return napi_release_threadsafe_function(_tsfn, napi_tsfn_release); } -inline napi_status ThreadSafeFunction::Abort() { +inline napi_status ThreadSafeFunction::Abort() const { return napi_release_threadsafe_function(_tsfn, napi_tsfn_abort); } diff --git a/napi.h b/napi.h index 286430643..697f2c96a 100644 --- a/napi.h +++ b/napi.h @@ -286,11 +286,11 @@ namespace Napi { Value Null() const; bool IsExceptionPending() const; - Error GetAndClearPendingException(); + Error GetAndClearPendingException() const; - MaybeOrValue RunScript(const char* utf8script); - MaybeOrValue RunScript(const std::string& utf8script); - MaybeOrValue RunScript(String script); + MaybeOrValue RunScript(const char* utf8script) const; + MaybeOrValue RunScript(const std::string& utf8script) const; + MaybeOrValue RunScript(String script) const; #if NAPI_VERSION > 2 template @@ -301,19 +301,20 @@ namespace Napi { #endif // NAPI_VERSION > 2 #if NAPI_VERSION > 5 - template T* GetInstanceData(); + template + T* GetInstanceData() const; template using Finalizer = void (*)(Env, T*); template fini = Env::DefaultFini> - void SetInstanceData(T* data); + void SetInstanceData(T* data) const; template using FinalizerWithHint = void (*)(Env, DataType*, HintType*); template fini = - Env::DefaultFiniWithHint> - void SetInstanceData(DataType* data, HintType* hint); + Env::DefaultFiniWithHint> + void SetInstanceData(DataType* data, HintType* hint) const; #endif // NAPI_VERSION > 5 private: @@ -727,42 +728,24 @@ namespace Napi { napi_value value); ///< Wraps a Node-API value primitive. /// Gets or sets a named property. - PropertyLValue operator []( - const char* utf8name ///< UTF-8 encoded null-terminated property name - ); + PropertyLValue operator[]( + const char* utf8name ///< UTF-8 encoded null-terminated property name + ) const; /// Gets or sets a named property. - PropertyLValue operator []( - const std::string& utf8name ///< UTF-8 encoded property name - ); - - /// Gets or sets an indexed property or array element. - PropertyLValue operator []( - uint32_t index /// Property / element index - ); + PropertyLValue operator[]( + const std::string& utf8name ///< UTF-8 encoded property name + ) const; /// Gets or sets an indexed property or array element. - PropertyLValue operator[](Value index /// Property / element index - ); + PropertyLValue operator[]( + uint32_t index /// Property / element index + ) const; /// Gets or sets an indexed property or array element. PropertyLValue operator[](Value index /// Property / element index ) const; - /// Gets a named property. - MaybeOrValue operator[]( - const char* utf8name ///< UTF-8 encoded null-terminated property name - ) const; - - /// Gets a named property. - MaybeOrValue operator[]( - const std::string& utf8name ///< UTF-8 encoded property name - ) const; - - /// Gets an indexed property or array element. - MaybeOrValue operator[](uint32_t index ///< Property / element index - ) const; - /// Checks whether a property is present. MaybeOrValue Has(napi_value key ///< Property key primitive ) const; @@ -822,44 +805,44 @@ namespace Napi { template MaybeOrValue Set(napi_value key, ///< Property key primitive const ValueType& value ///< Property value primitive - ); + ) const; /// Sets a property. template MaybeOrValue Set(Value key, ///< Property key const ValueType& value ///< Property value - ); + ) const; /// Sets a named property. template MaybeOrValue Set( const char* utf8name, ///< UTF-8 encoded null-terminated property name - const ValueType& value); + const ValueType& value) const; /// Sets a named property. template MaybeOrValue Set( const std::string& utf8name, ///< UTF-8 encoded property name const ValueType& value ///< Property value primitive - ); + ) const; /// Delete property. MaybeOrValue Delete(napi_value key ///< Property key primitive - ); + ) const; /// Delete property. MaybeOrValue Delete(Value key ///< Property key - ); + ) const; /// Delete property. MaybeOrValue Delete( const char* utf8name ///< UTF-8 encoded null-terminated property name - ); + ) const; /// Delete property. MaybeOrValue Delete( const std::string& utf8name ///< UTF-8 encoded property name - ); + ) const; /// Checks whether an indexed property is present. MaybeOrValue Has(uint32_t index ///< Property / element index @@ -873,11 +856,11 @@ namespace Napi { template MaybeOrValue Set(uint32_t index, ///< Property / element index const ValueType& value ///< Property value primitive - ); + ) const; /// Deletes an indexed property or array element. MaybeOrValue Delete(uint32_t index ///< Property / element index - ); + ) const; /// This operation can fail in case of Proxy.[[OwnPropertyKeys]] and /// Proxy.[[GetOwnProperty]] calling into JavaScript. See: @@ -895,7 +878,7 @@ namespace Napi { MaybeOrValue DefineProperty( const PropertyDescriptor& property ///< Descriptor for the property to be defined - ); + ) const; /// Defines properties on the object. /// @@ -905,7 +888,7 @@ namespace Napi { MaybeOrValue DefineProperties( const std::initializer_list& properties ///< List of descriptors for the properties to be defined - ); + ) const; /// Defines properties on the object. /// @@ -915,7 +898,7 @@ namespace Napi { MaybeOrValue DefineProperties( const std::vector& properties ///< Vector of descriptors for the properties to be defined - ); + ) const; /// Checks if an object is an instance created by a constructor function. /// @@ -930,25 +913,19 @@ namespace Napi { ) const; template - inline void AddFinalizer(Finalizer finalizeCallback, T* data); + inline void AddFinalizer(Finalizer finalizeCallback, T* data) const; template inline void AddFinalizer(Finalizer finalizeCallback, T* data, - Hint* finalizeHint); + Hint* finalizeHint) const; #ifdef NAPI_CPP_EXCEPTIONS - class const_iterator; - - inline const_iterator begin() const; - - inline const_iterator end() const; - class iterator; - inline iterator begin(); + inline iterator begin() const; - inline iterator end(); + inline iterator end() const; #endif // NAPI_CPP_EXCEPTIONS #if NAPI_VERSION >= 8 @@ -956,12 +933,12 @@ namespace Napi { /// JavaScript. /// See /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-getprototypeof - MaybeOrValue Freeze(); + MaybeOrValue Freeze() const; /// This operation can fail in case of Proxy.[[GetPrototypeOf]] calling into /// JavaScript. /// See /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-getprototypeof - MaybeOrValue Seal(); + MaybeOrValue Seal() const; #endif // NAPI_VERSION >= 8 }; @@ -1000,35 +977,11 @@ namespace Napi { }; #ifdef NAPI_CPP_EXCEPTIONS - class Object::const_iterator { - private: - enum class Type { BEGIN, END }; - - inline const_iterator(const Object* object, const Type type); - - public: - inline const_iterator& operator++(); - - inline bool operator==(const const_iterator& other) const; - - inline bool operator!=(const const_iterator& other) const; - - inline const std::pair> operator*() - const; - - private: - const Napi::Object* _object; - Array _keys; - uint32_t _index; - - friend class Object; - }; - class Object::iterator { private: enum class Type { BEGIN, END }; - inline iterator(Object* object, const Type type); + inline iterator(const Object* object, const Type type); public: inline iterator& operator++(); @@ -1037,10 +990,10 @@ namespace Napi { inline bool operator!=(const iterator& other) const; - inline std::pair> operator*(); + inline std::pair> operator*() const; private: - Napi::Object* _object; + const Napi::Object* _object; Array _keys; uint32_t _index; @@ -1462,8 +1415,8 @@ namespace Napi { // within a HandleScope so that the value handle gets cleaned up efficiently. T Value() const; - uint32_t Ref(); - uint32_t Unref(); + uint32_t Ref() const; + uint32_t Unref() const; void Reset(); void Reset(const T& value, uint32_t refcount = 0); @@ -1500,24 +1453,27 @@ namespace Napi { MaybeOrValue Get(const char* utf8name) const; MaybeOrValue Get(const std::string& utf8name) const; - MaybeOrValue Set(const char* utf8name, napi_value value); - MaybeOrValue Set(const char* utf8name, Napi::Value value); - MaybeOrValue Set(const char* utf8name, const char* utf8value); - MaybeOrValue Set(const char* utf8name, bool boolValue); - MaybeOrValue Set(const char* utf8name, double numberValue); - MaybeOrValue Set(const std::string& utf8name, napi_value value); - MaybeOrValue Set(const std::string& utf8name, Napi::Value value); - MaybeOrValue Set(const std::string& utf8name, std::string& utf8value); - MaybeOrValue Set(const std::string& utf8name, bool boolValue); - MaybeOrValue Set(const std::string& utf8name, double numberValue); + MaybeOrValue Set(const char* utf8name, napi_value value) const; + MaybeOrValue Set(const char* utf8name, Napi::Value value) const; + MaybeOrValue Set(const char* utf8name, const char* utf8value) const; + MaybeOrValue Set(const char* utf8name, bool boolValue) const; + MaybeOrValue Set(const char* utf8name, double numberValue) const; + MaybeOrValue Set(const std::string& utf8name, napi_value value) const; + MaybeOrValue Set(const std::string& utf8name, + Napi::Value value) const; + MaybeOrValue Set(const std::string& utf8name, + std::string& utf8value) const; + MaybeOrValue Set(const std::string& utf8name, bool boolValue) const; + MaybeOrValue Set(const std::string& utf8name, + double numberValue) const; MaybeOrValue Get(uint32_t index) const; - MaybeOrValue Set(uint32_t index, const napi_value value); - MaybeOrValue Set(uint32_t index, const Napi::Value value); - MaybeOrValue Set(uint32_t index, const char* utf8value); - MaybeOrValue Set(uint32_t index, const std::string& utf8value); - MaybeOrValue Set(uint32_t index, bool boolValue); - MaybeOrValue Set(uint32_t index, double numberValue); + MaybeOrValue Set(uint32_t index, const napi_value value) const; + MaybeOrValue Set(uint32_t index, const Napi::Value value) const; + MaybeOrValue Set(uint32_t index, const char* utf8value) const; + MaybeOrValue Set(uint32_t index, const std::string& utf8value) const; + MaybeOrValue Set(uint32_t index, bool boolValue) const; + MaybeOrValue Set(uint32_t index, double numberValue) const; protected: ObjectReference(const ObjectReference&); @@ -2553,10 +2509,10 @@ namespace Napi { napi_status Acquire() const; // This API may be called from any thread. - napi_status Release(); + napi_status Release() const; // This API may be called from any thread. - napi_status Abort(); + napi_status Abort() const; struct ConvertibleContext { @@ -2752,10 +2708,10 @@ namespace Napi { napi_status Acquire() const; // This API may be called from any thread. - napi_status Release(); + napi_status Release() const; // This API may be called from any thread. - napi_status Abort(); + napi_status Abort() const; // This API may be called from any thread. ContextType* GetContext() const;