swig/Lib/javascript/jsc
Eric Wing 6f69555225 JavaScriptCore: Fixed exception object so sourceURL (file name), line (number), and message can be recovered.
The current implementation only returns an error string. But this is insufficient for debugging (what file and line number did it fail at?).

As documented here:
http://parmanoir.com/Taming_JavascriptCore_within_and_without_WebView
converting the JSValueRef of string to an JSObjectRef (via JSValueToObject) will trigger JSCore into filling the "sourceURL" and "line" properties into the object so they can be inspected by the caller.

Additionally, JavaScriptCore has a "message" property which contains the message string. JSCore doesn't seem to be filling this in for us automatically, unlike "sourceURL" and "line". So this patch also fills that property in too.

Thanks to Brian Barnes for the detailed information about "sourceURL", "line", and "message".

Below is an example (derived from Brian Barnes's information) on how you typically use/extract these exception details.

void script_exception_to_string(JSContextRef js_context,JSValueRef exception_value_ref,char* return_error_string, int return_error_string_max_length)
{
	JSObjectRef exception_object;
	JSValueRef value_ref;
	JSStringRef jsstring_property_name = NULL;
	JSValueRef temporary_exception = NULL;
	JSStringRef js_return_string = NULL;
	size_t bytes_needed;
	char* c_result_string = NULL;
	exception_object = JSValueToObject(js_context, exception_value_ref, NULL);
	// source and line numbers

	strcpy(return_error_string,"[");

	jsstring_property_name = JSStringCreateWithUTF8CString("sourceURL");
	value_ref = JSObjectGetProperty(js_context, exception_object, jsstring_property_name, &temporary_exception);
	JSStringRelease(jsstring_property_name);
	js_return_string = JSValueToStringCopy(js_context, value_ref, NULL);
	bytes_needed = JSStringGetMaximumUTF8CStringSize(js_return_string);
	c_result_string = (char*)calloc(bytes_needed, sizeof(char));
	JSStringGetUTF8CString(js_return_string, c_result_string, bytes_needed);
	SDL_Log("c_result_string: %s\n", c_result_string);
	JSStringRelease(js_return_string);
	strncat(return_error_string, c_result_string, return_error_string_max_length-1);
	free(c_result_string);

	strncat(return_error_string, ":", return_error_string_max_length-1);

	jsstring_property_name = JSStringCreateWithUTF8CString("line");
	value_ref = JSObjectGetProperty(js_context, exception_object, jsstring_property_name, &temporary_exception);
	JSStringRelease(jsstring_property_name);
	js_return_string = JSValueToStringCopy(js_context, value_ref, NULL);
	bytes_needed = JSStringGetMaximumUTF8CStringSize(js_return_string);
	c_result_string = (char*)calloc(bytes_needed, sizeof(char));
	JSStringGetUTF8CString(js_return_string, c_result_string, bytes_needed);
	SDL_Log("c_result_string: %s\n", c_result_string);
	JSStringRelease(js_return_string);
	strncat(return_error_string, c_result_string, return_error_string_max_length-1);
	//SDL_Log("c_result_string: %s\n", c_result_string);
	free(c_result_string);

	strncat(return_error_string, "]", return_error_string_max_length-1);

	/* get message */
	jsstring_property_name = JSStringCreateWithUTF8CString("message");
	value_ref = JSObjectGetProperty(js_context, exception_object, jsstring_property_name, &temporary_exception);
	JSStringRelease(jsstring_property_name);
	if(NULL == value_ref)
	{
		strncat(return_error_string, "Unknown Error", return_error_string_max_length-1);
	}
	else
	{
		js_return_string = JSValueToStringCopy(js_context, value_ref, NULL);
		bytes_needed = JSStringGetMaximumUTF8CStringSize(js_return_string);
		c_result_string = (char*)calloc(bytes_needed, sizeof(char));
		JSStringGetUTF8CString(js_return_string, c_result_string, bytes_needed);
		SDL_Log("c_result_string: %s\n", c_result_string);
		JSStringRelease(js_return_string);
		strncat(return_error_string, c_result_string, return_error_string_max_length-1);
		//SDL_Log("c_result_string: %s\n", c_result_string);
		free(c_result_string);
	}
}

To use:
if(js_exception)
{
	char return_error_string[256];
	script_exception_to_string(js_context, js_exception, return_error_string, 256);
	SDL_Log("Compile error is %s", return_error_string);
}
2014-05-26 22:35:27 +02:00
..
arrays_javascript.i Typemap for natural support for arrays 2013-09-03 13:58:09 +02:00
ccomplex.i Add swig configuration files for v8. 2012-09-08 00:57:42 +00:00
cdata.i Add cdata.i typemaps. 2013-09-16 04:12:06 +02:00
complex.i Merge branch 'devel' of https://github.com/Neha03/gsoc2012-javascript into devel 2012-09-08 00:56:48 +00:00
exception.i Some fixes for the Javascript generator. 2013-09-09 22:25:51 +03:00
javascript.swg Merge branch 'devel' of https://github.com/Neha03/gsoc2012-javascript into devel 2012-09-08 00:56:48 +00:00
javascriptcode.swg JavascriptCore: fix cleanup code. 2014-05-18 21:27:54 +02:00
javascriptcomplex.swg Merge branch 'devel' of https://github.com/Neha03/gsoc2012-javascript into devel 2012-09-08 00:56:48 +00:00
javascriptfragments.swg Clean up in javascripttypemaps.swg. 2013-09-09 17:34:53 +03:00
javascripthelpers.swg JavaScript: Added missing static modifiers to avoid duplicate symbol problems with multiple SWIG modules. 2014-05-18 21:27:53 +02:00
javascriptinit.swg Rearrange generation of init block to have custom init code within the initializer body. 2013-09-16 03:53:00 +02:00
javascriptkw.swg Merge branch 'devel' of https://github.com/Neha03/gsoc2012-javascript into devel 2012-09-08 00:56:48 +00:00
javascriptprimtypes.swg Fix typemap declarations for (unsigned) long long. 2013-09-16 01:23:03 +02:00
javascriptrun.swg JavaScriptCore: Fixed exception object so sourceURL (file name), line (number), and message can be recovered. 2014-05-26 22:35:27 +02:00
javascriptruntime.swg Add support for PackedData to Javascript generator. 2013-09-10 11:53:12 +03:00
javascriptstrings.swg Javascript warnings for c++98 - remove vla 2014-05-24 14:13:24 +01:00
javascripttypemaps.swg Add support for IN/OUTPUT typemaps. 2013-09-16 00:55:43 +02:00
std_common.i Remove execute permissions from various non-executable files 2014-05-02 20:06:11 +02:00
std_complex.i Merge branch 'devel' of https://github.com/Neha03/gsoc2012-javascript into devel 2012-09-08 00:56:48 +00:00
std_deque.i Add stub std_deque.i files. 2013-09-24 03:56:19 +02:00
std_except.i Merge branch 'devel' of https://github.com/Neha03/gsoc2012-javascript into devel 2012-09-08 00:56:48 +00:00
std_map.i Remove execute permissions from various non-executable files 2014-05-02 20:06:11 +02:00
std_pair.i Remove execute permissions from various non-executable files 2014-05-02 20:06:11 +02:00
std_string.i Javascript: fix warnings in li_std_string test. 2014-05-18 22:40:22 +02:00
std_vector.i Remove execute permissions from various non-executable files 2014-05-02 20:06:11 +02:00
stl.i Remove execute permissions from various non-executable files 2014-05-02 20:06:11 +02:00
typemaps.i Add support for IN/OUTPUT typemaps. 2013-09-16 00:55:43 +02:00