Commit graph

17 commits

Author SHA1 Message Date
Olly Betts
b138f054e5 [Javascript] Fix SWIG_exception() macro (#792)
Fix SWIG_exception() macro to return from the current function.
Fixes #789, reported by Julien Dutriaux.
2016-09-17 17:29:42 +12:00
Eric Wing
1766e67a1a 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.
2014-05-26 22:35:28 +02:00
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
Oliver Buchtala
61b7da1671 JavascriptCore: Bugfix for null-pointer support.
This solution must be considered as a preliminary workaround.
2014-05-19 16:04:22 +02:00
Oliver Buchtala
3f0f588891 Javascript: support null pointers.
We allow to set pointer types using JS null.
2014-05-19 16:04:22 +02:00
Eric Wing
d5ea32d760 JavaScriptCore: ConvertPtr should allow JavaScript null to be a valid value.
It is common in C to accept NULL to functions for pointer parameters.
extern void DoSomething(struct Foo* foo);
...
DoSomething(NULL);

Thus, JavaScript null should be allowed:
module.DoSomething(null);

But the current ConvertPtr definition accepts only objects. This modifies it to allow null.
2014-05-19 16:04:22 +02:00
Oliver Buchtala
d9bb3a8415 Revert "JavaScriptCore: ConvertPtr should allow JavaScript null to be a valid value."
This reverts commit 05ed0325dc.
2014-05-19 11:46:29 +02:00
Oliver Buchtala
7cc617a19d Revert "Javascript: support null pointers."
This reverts commit 11963788e0.
2014-05-19 11:46:21 +02:00
Oliver Buchtala
11963788e0 Javascript: support null pointers.
We allow to set pointer types using JS null.
2014-05-19 00:21:21 +02:00
Eric Wing
05ed0325dc JavaScriptCore: ConvertPtr should allow JavaScript null to be a valid value.
It is common in C to accept NULL to functions for pointer parameters.
extern void DoSomething(struct Foo* foo);
...
DoSomething(NULL);

Thus, JavaScript null should be allowed:
module.DoSomething(null);

But the current ConvertPtr definition accepts only objects. This modifies it to allow null.
2014-05-18 21:27:54 +02:00
Eric Wing
8498e4878d JavaScriptCore: More missing static modifiers. 2014-05-18 21:27:54 +02:00
Eric Wing
fade0bcbde JavaScriptCore: C89: declare variables at the top for antiquated compilers like Microsoft Visual Studio. 2014-05-18 21:27:54 +02:00
Eric Wing
486e903de1 JavaScriptCore: Bug fix for SWIGJSC_AppendOutput. This function requires a return value. I think it should be arr, but somebody should scrutinize this. 2014-05-18 21:27:54 +02:00
Oliver Buchtala
eb9523b5ca Add missing macros. 2013-09-16 04:12:55 +02:00
Oliver Buchtala
bb7bd50eab Add support for IN/OUTPUT typemaps. 2013-09-16 00:55:43 +02:00
Oliver Buchtala
001f38c6a9 Fix settings for building nodejs tests.
Removed the `-node` command line flag.
Instead one has to use `-v8 -DBUILDING_NODE_EXTENSION=1`.
2013-09-10 13:29:16 +03:00
Oliver Buchtala
be35d94fdb Add support for PackedData to Javascript generator. 2013-09-10 11:53:12 +03:00