Helper function: perform ToString on an exception (which may not even be an object), except if it is an InternalError, which would throw in ToString.
| 230 | // Helper function: perform ToString on an exception (which may not even be an |
| 231 | // object), except if it is an InternalError, which would throw in ToString. |
| 232 | GJS_JSAPI_RETURN_CONVENTION |
| 233 | static JSString* exception_to_string(JSContext* cx, JS::HandleValue exc) { |
| 234 | if (exc.isObject()) { |
| 235 | JS::RootedObject exc_obj(cx, &exc.toObject()); |
| 236 | const JSClass* internal_error = |
| 237 | js::ProtoKeyToClass(JSProto_InternalError); |
| 238 | if (JS_InstanceOf(cx, exc_obj, internal_error, nullptr)) { |
| 239 | JSErrorReport* report = JS_ErrorFromException(cx, exc_obj); |
| 240 | if (!report->message()) |
| 241 | return JS_NewStringCopyZ(cx, "(unknown internal error)"); |
| 242 | return JS_NewStringCopyUTF8Z(cx, report->message()); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | return JS::ToString(cx, exc); |
| 247 | } |
| 248 | |
| 249 | // Helper function: log and clear the pending exception, without calling into |
| 250 | // any JS APIs that might cause more exceptions to be thrown. |
no test coverage detected