Skip to content

Commit

Permalink
src: do not use non-static class member for constant value (#1134)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
kevindavies8 committed Feb 21, 2022
1 parent b1fc193 commit 066af58
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
16 changes: 11 additions & 5 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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 <typename TError>
inline TError Error::New(napi_env env,
const char* message,
Expand Down
3 changes: 1 addition & 2 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down

0 comments on commit 066af58

Please sign in to comment.