Skip to content

Commit 5790c55

Browse files
authored
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).
1 parent b7659db commit 5790c55

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

napi-inl.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,8 +2607,8 @@ inline Error::Error(napi_env env, napi_value value) : ObjectReference(env, nullp
26072607

26082608
// property flag that we attach to show the error object is wrapped
26092609
napi_property_descriptor wrapObjFlag = {
2610-
ERROR_WRAP_VALUE, // Unique GUID identifier since Symbol isn't a
2611-
// viable option
2610+
ERROR_WRAP_VALUE(), // Unique GUID identifier since Symbol isn't a
2611+
// viable option
26122612
nullptr,
26132613
nullptr,
26142614
nullptr,
@@ -2648,15 +2648,17 @@ inline Object Error::Value() const {
26482648
// We are checking if the object is wrapped
26492649
bool isWrappedObject = false;
26502650

2651-
status = napi_has_property(
2652-
_env, refValue, String::From(_env, ERROR_WRAP_VALUE), &isWrappedObject);
2651+
status = napi_has_property(_env,
2652+
refValue,
2653+
String::From(_env, ERROR_WRAP_VALUE()),
2654+
&isWrappedObject);
26532655

26542656
// Don't care about status
26552657
if (isWrappedObject) {
26562658
napi_value unwrappedValue;
26572659
status = napi_get_property(_env,
26582660
refValue,
2659-
String::From(_env, ERROR_WRAP_VALUE),
2661+
String::From(_env, ERROR_WRAP_VALUE()),
26602662
&unwrappedValue);
26612663
NAPI_THROW_IF_FAILED(_env, status, Object());
26622664

@@ -2772,6 +2774,10 @@ inline const char* Error::what() const NAPI_NOEXCEPT {
27722774

27732775
#endif // NAPI_CPP_EXCEPTIONS
27742776

2777+
inline const char* Error::ERROR_WRAP_VALUE() NAPI_NOEXCEPT {
2778+
return "4bda9e7e-4913-4dbc-95de-891cbf66598e-errorVal";
2779+
}
2780+
27752781
template <typename TError>
27762782
inline TError Error::New(napi_env env,
27772783
const char* message,

napi.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,8 +1720,7 @@ namespace Napi {
17201720
/// !endcond
17211721

17221722
private:
1723-
const char* ERROR_WRAP_VALUE =
1724-
"4bda9e7e-4913-4dbc-95de-891cbf66598e-errorVal";
1723+
static inline const char* ERROR_WRAP_VALUE() NAPI_NOEXCEPT;
17251724
mutable std::string _message;
17261725
};
17271726

0 commit comments

Comments
 (0)