Permalink
Browse files

JavaScriptCore: Improved code that uses JSObjectMakeError instead of …

…JSValueToObject to create the exception object.

JSObjectMakeError automatically populates the "message" field, and possibly other fields I don't know about. This seems to be the most robust way to create an exception object.

Thanks to Brian Barnes again for the tip on JSObjectMakeError.
  • Loading branch information...
1 parent e7b2062 commit 1766e67a1af127f42c908090e8d46b49de30ce19 Eric Wing committed with oliver---- May 21, 2014
Showing with 17 additions and 23 deletions.
  1. +17 −23 Lib/javascript/jsc/javascriptrun.swg
@@ -8,30 +8,24 @@
#define SWIG_fail goto fail
SWIGRUNTIME void SWIG_Javascript_Raise(JSContextRef context, JSValueRef *exception, const char* type) {
- JSStringRef message = JSStringCreateWithUTF8CString(type);
- JSStringRef message_property_key;
- JSObjectRef exception_object;
- JSValueRef exception_value;
- JSValueRef message_value;
- exception_value = JSValueMakeString(context, message);
- /* Converting the result to an object will let JavascriptCore add "sourceURL" (file) and "line" (number) to the exception,
- instead of just returning a raw string. This is extremely important for debugging your errors.
- */
- exception_object = JSValueToObject(context, exception_value, NULL);
-
- /* Additionally, JSCore uses "message" which contains the error description.
- But it seems that unlike "sourceURL" and "line", converting to an object is not automatically doing this.
- So we can add it ourselves.
+ JSStringRef message = JSStringCreateWithUTF8CString(type);
+ JSValueRef error_arguments[1];
+ JSObjectRef exception_object;
+ JSValueRef exception_value;
+ exception_value = JSValueMakeString(context, message);
+ /* Converting the result to an object will let JavascriptCore add
+ "sourceURL" (file) and "line" (number) and "message" to the exception,
+ instead of just returning a raw string. This is extremely important for debugging your errors.
+ Using JSObjectMakeError is better than JSValueToObject because the latter only populates
+ "sourceURL" and "line", but not "message" or any others I don't know about.
*/
- message_property_key = JSStringCreateWithUTF8CString("message");
- message_value = JSValueMakeString(context, message);
- JSObjectSetProperty(context, exception_object, message_property_key, message_value, kJSClassAttributeNone, NULL);
-
- /* Return the exception_object */
- *exception = exception_object;
-
- JSStringRelease(message_property_key);
- JSStringRelease(message);
+ error_arguments[0] = exception_value;
+ exception_object = JSObjectMakeError(context, 1, error_arguments, NULL);
+
+ /* Return the exception_object */
+ *exception = exception_object;
+
+ JSStringRelease(message);
}
SWIGRUNTIME void SWIG_JSC_exception(JSContextRef context, JSValueRef *exception, int code, const char* msg) {

0 comments on commit 1766e67

Please sign in to comment.