@@ -2595,12 +2595,73 @@ inline Error::Error(napi_env env, napi_value value) : ObjectReference(env, nullp
25952595 if (value != nullptr ) {
25962596 napi_status status = napi_create_reference (env, value, 1 , &_ref);
25972597
2598+ // Creates a wrapper object containg the error value (primitive types) and
2599+ // create a reference to this wrapper
2600+ if (status != napi_ok) {
2601+ napi_value wrappedErrorObj;
2602+ status = napi_create_object (env, &wrappedErrorObj);
2603+
2604+ NAPI_FATAL_IF_FAILED (status, " Error::Error" , " napi_create_object" );
2605+
2606+ status = napi_set_property (env,
2607+ wrappedErrorObj,
2608+ String::From (env, " errorVal" ),
2609+ Value::From (env, value));
2610+ NAPI_FATAL_IF_FAILED (status, " Error::Error" , " napi_set_property" );
2611+
2612+ status = napi_set_property (env,
2613+ wrappedErrorObj,
2614+ String::From (env, " isWrapObject" ),
2615+ Value::From (env, value));
2616+ NAPI_FATAL_IF_FAILED (status, " Error::Error" , " napi_set_property" );
2617+
2618+ status = napi_create_reference (env, wrappedErrorObj, 1 , &_ref);
2619+ }
2620+
25982621 // Avoid infinite recursion in the failure case.
25992622 // Don't try to construct & throw another Error instance.
26002623 NAPI_FATAL_IF_FAILED (status, " Error::Error" , " napi_create_reference" );
26012624 }
26022625}
26032626
2627+ inline Object Error::Value () const {
2628+ if (_ref == nullptr ) {
2629+ return Object (_env, nullptr );
2630+ }
2631+ // Most likely will mess up thread execution
2632+
2633+ napi_value refValue;
2634+ napi_status status = napi_get_reference_value (_env, _ref, &refValue);
2635+ NAPI_THROW_IF_FAILED (_env, status, Object ());
2636+
2637+ // We are wrapping this object
2638+ bool isWrappedObject = false ;
2639+ napi_has_property (
2640+ _env, refValue, String::From (_env, " isWrapObject" ), &isWrappedObject);
2641+ // Don't care about status
2642+
2643+ if (isWrappedObject == true ) {
2644+ napi_value unwrappedValue;
2645+ status = napi_get_property (
2646+ _env, refValue, String::From (_env, " errorVal" ), &unwrappedValue);
2647+ NAPI_THROW_IF_FAILED (_env, status, Object ());
2648+ return Object (_env, unwrappedValue);
2649+ }
2650+
2651+ return Object (_env, refValue);
2652+ }
2653+ // template<typename T>
2654+ // inline T Error::Value() const {
2655+ // // if (_ref == nullptr) {
2656+ // // return T(_env, nullptr);
2657+ // // }
2658+
2659+ // // napi_value value;
2660+ // // napi_status status = napi_get_reference_value(_env, _ref, &value);
2661+ // // NAPI_THROW_IF_FAILED(_env, status, T());
2662+ // return nullptr;
2663+ // }
2664+
26042665inline Error::Error (Error&& other) : ObjectReference(std::move(other)) {
26052666}
26062667
@@ -2651,6 +2712,7 @@ inline const std::string& Error::Message() const NAPI_NOEXCEPT {
26512712 return _message;
26522713}
26532714
2715+ // we created an object on the &_ref
26542716inline void Error::ThrowAsJavaScriptException () const {
26552717 HandleScope scope (_env);
26562718 if (!IsEmpty ()) {
0 commit comments