Skip to content

Commit 6918138

Browse files
committed
Refactor code. Using hard coded string instead of using symbol
1 parent a0b3fe9 commit 6918138

File tree

2 files changed

+26
-56
lines changed

2 files changed

+26
-56
lines changed

napi-inl.h

Lines changed: 24 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,60 +2593,38 @@ inline Error::Error() : ObjectReference() {
25932593

25942594
inline Error::Error(napi_env env, napi_value value) : ObjectReference(env, nullptr) {
25952595
if (value != nullptr) {
2596+
// Attempting to create a reference on the error object.
2597+
// If it's not a Object/Function/Symbol, this call will return an error
2598+
// status.
25962599
napi_status status = napi_create_reference(env, value, 1, &_ref);
25972600

2598-
// Creates a wrapper object containg the error value (primitive types) and
2599-
// create a reference to this wrapper
26002601
if (status != napi_ok) {
26012602
napi_value wrappedErrorObj;
2602-
status = napi_create_object(env, &wrappedErrorObj);
26032603

2604+
// Create an error object
2605+
status = napi_create_object(env, &wrappedErrorObj);
26042606
NAPI_FATAL_IF_FAILED(status, "Error::Error", "napi_create_object");
26052607

2606-
napi_property_descriptor errValDesc = {
2607-
"errorVal", // const char* utf8Name
2608-
String::From(env, "errorVal"), // napi_value name
2609-
nullptr, // Method
2610-
nullptr, // getter
2611-
nullptr, // setter
2612-
Value::From(env, value), // napi_value value
2608+
// property flag that we attach to show the error object is wrapped
2609+
napi_property_descriptor wrapObjFlag = {
2610+
ERROR_WRAP_VALUE, // Unique GUID identifier since Symbol isn't a
2611+
// viable option
2612+
nullptr,
2613+
nullptr,
2614+
nullptr,
2615+
nullptr,
2616+
Value::From(env, value),
26132617
napi_enumerable,
26142618
nullptr};
26152619

2616-
status = napi_define_properties(env, wrappedErrorObj, 1, &errValDesc);
2617-
NAPI_FATAL_IF_FAILED(status, "Error::Error", "napi_define_properties");
2618-
2619-
MaybeOrValue<Symbol> result = Symbol::For(env, "isWrapObject");
2620-
napi_property_descriptor wrapObjFlag = {nullptr,
2621-
nullptr,
2622-
nullptr,
2623-
nullptr,
2624-
nullptr,
2625-
Value::From(env, value),
2626-
napi_enumerable,
2627-
nullptr};
2628-
2629-
#ifdef NODE_ADDON_API_ENABLE_MAYBE
2630-
Symbol uniqueSymb;
2631-
if (result.IsJust()) {
2632-
uniqueSymb = result.Unwrap();
2633-
}
2634-
2635-
wrapObjFlag.name = uniqueSymb;
26362620
status = napi_define_properties(env, wrappedErrorObj, 1, &wrapObjFlag);
26372621
NAPI_FATAL_IF_FAILED(status, "Error::Error", "napi_define_properties");
2638-
#else
26392622

2640-
wrapObjFlag.name = result;
2641-
status = napi_define_properties(env, wrappedErrorObj, 1, &wrapObjFlag);
2642-
NAPI_FATAL_IF_FAILED(status, "Error::Error", "napi_define_properties");
2643-
2644-
#endif
2623+
// Create a reference on the newly wrapped object
26452624
status = napi_create_reference(env, wrappedErrorObj, 1, &_ref);
26462625
}
26472626

26482627
// Avoid infinite recursion in the failure case.
2649-
// Don't try to construct & throw another Error instance.
26502628
NAPI_FATAL_IF_FAILED(status, "Error::Error", "napi_create_reference");
26512629
}
26522630
}
@@ -2664,32 +2642,22 @@ inline Object Error::Value() const {
26642642
status = napi_typeof(_env, refValue, &type);
26652643
NAPI_THROW_IF_FAILED(_env, status, Object());
26662644

2645+
// If refValue isn't a symbol, then we proceed to whether the refValue has the
2646+
// wrapped error flag
26672647
if (type != napi_symbol) {
26682648
// We are checking if the object is wrapped
26692649
bool isWrappedObject = false;
26702650

2671-
MaybeOrValue<Symbol> result = Symbol::For(_env, "isWrapObject");
2672-
2673-
#ifdef NODE_ADDON_API_ENABLE_MAYBE
2674-
Symbol uniqueSymb;
2675-
if (result.IsJust()) {
2676-
uniqueSymb = result.Unwrap();
2677-
}
2678-
status = napi_has_property(_env, refValue, uniqueSymb, &isWrappedObject);
2679-
NAPI_FATAL_IF_FAILED(status, "Error::Error", "napi_set_property");
2680-
2681-
#else
2682-
2683-
status = napi_has_property(_env, refValue, result, &isWrappedObject);
2684-
NAPI_FATAL_IF_FAILED(status, "Error::Error", "napi_set_property");
2685-
#endif
2651+
status = napi_has_property(
2652+
_env, refValue, String::From(_env, ERROR_WRAP_VALUE), &isWrappedObject);
26862653

26872654
// Don't care about status
2688-
2689-
if (isWrappedObject == true) {
2655+
if (isWrappedObject) {
26902656
napi_value unwrappedValue;
2691-
status = napi_get_property(
2692-
_env, refValue, String::From(_env, "errorVal"), &unwrappedValue);
2657+
status = napi_get_property(_env,
2658+
refValue,
2659+
String::From(_env, ERROR_WRAP_VALUE),
2660+
&unwrappedValue);
26932661
NAPI_THROW_IF_FAILED(_env, status, Object());
26942662

26952663
return Object(_env, unwrappedValue);

napi.h

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

17221722
private:
1723+
const char* ERROR_WRAP_VALUE =
1724+
"4bda9e7e-4913-4dbc-95de-891cbf66598e-errorVal";
17231725
mutable std::string _message;
17241726
};
17251727

0 commit comments

Comments
 (0)