From 5790c5578455106b6bfea3e255db824692a95743 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 21 Feb 2022 16:37:15 +0100 Subject: [PATCH] src: do not use non-static class member for constant value (#1134) Non-static class members need to be set for each instantiated object and take up extra space, which is why constant data is generally provided through static members or static functions (and static functions are a bit easier to use in header-only C++11). --- napi-inl.h | 16 +++++++++++----- napi.h | 3 +-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/napi-inl.h b/napi-inl.h index 64d4f5aa0..1cbf444c2 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -2607,8 +2607,8 @@ inline Error::Error(napi_env env, napi_value value) : ObjectReference(env, nullp // property flag that we attach to show the error object is wrapped napi_property_descriptor wrapObjFlag = { - ERROR_WRAP_VALUE, // Unique GUID identifier since Symbol isn't a - // viable option + ERROR_WRAP_VALUE(), // Unique GUID identifier since Symbol isn't a + // viable option nullptr, nullptr, nullptr, @@ -2648,15 +2648,17 @@ inline Object Error::Value() const { // We are checking if the object is wrapped bool isWrappedObject = false; - status = napi_has_property( - _env, refValue, String::From(_env, ERROR_WRAP_VALUE), &isWrappedObject); + status = napi_has_property(_env, + refValue, + String::From(_env, ERROR_WRAP_VALUE()), + &isWrappedObject); // Don't care about status if (isWrappedObject) { napi_value unwrappedValue; status = napi_get_property(_env, refValue, - String::From(_env, ERROR_WRAP_VALUE), + String::From(_env, ERROR_WRAP_VALUE()), &unwrappedValue); NAPI_THROW_IF_FAILED(_env, status, Object()); @@ -2772,6 +2774,10 @@ inline const char* Error::what() const NAPI_NOEXCEPT { #endif // NAPI_CPP_EXCEPTIONS +inline const char* Error::ERROR_WRAP_VALUE() NAPI_NOEXCEPT { + return "4bda9e7e-4913-4dbc-95de-891cbf66598e-errorVal"; +} + template inline TError Error::New(napi_env env, const char* message, diff --git a/napi.h b/napi.h index 429e6a622..c0d0d6ef4 100644 --- a/napi.h +++ b/napi.h @@ -1720,8 +1720,7 @@ namespace Napi { /// !endcond private: - const char* ERROR_WRAP_VALUE = - "4bda9e7e-4913-4dbc-95de-891cbf66598e-errorVal"; + static inline const char* ERROR_WRAP_VALUE() NAPI_NOEXCEPT; mutable std::string _message; };