Bug 32283 - It seems that Exceptions aren't handled properly in C callback functions when embedding Javascriptcore.
Summary: It seems that Exceptions aren't handled properly in C callback functions when...
Status: RESOLVED WORKSFORME
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 528+ (Nightly build)
Hardware: Mac OS X 10.5
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-12-08 12:50 PST by martin
Modified: 2011-06-14 16:31 PDT (History)
2 users (show)

See Also:


Attachments
Sample app which shows the problem. (35.85 KB, application/zip)
2009-12-08 13:13 PST, martin
no flags Details
Fixed version of sample application (42.73 KB, application/zip)
2009-12-08 16:01 PST, Darin Adler
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description martin 2009-12-08 12:50:01 PST
Throwing exceptions in a C callback function when embedding the Javascript core doesn't seem to work properly. When an exception is thrown in a C callback function the execution of the script is stopped and an exception is generated in

     JSEvaluateScript()

with the line number where the exception was caused. But the exception message gets lost.  Below is an example how I generate my exception messages. But I've also attached a small example app which shows the problem.

Many thanks for your help.

Bye,
Martin

static JSValueRef JSVec3D_add(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
    if(argumentCount==1){
	JSObjectRef	newobj=JSObjectMake(ctx,vec3D_class,NULL);
	Vec3f		*erg = (Vec3f*)JSObjectGetPrivate(newobj);
	Vec3f		*vec = (Vec3f*)JSObjectGetPrivate(thisObject);
	Vec3f		*vec0 = (Vec3f*)JSObjectGetPrivate(JSValueToObject(ctx,arguments[0],NULL));
	
	*erg=(*vec)+(*vec0);
	return newobj;
	}
    else{
	// Throw an exception
	JSStringRef string = JSStringCreateWithUTF8CString("Wrong number of arguments");
	JSValueRef exceptionString =JSValueMakeString(ctx, string);
	JSStringRelease(string);

	*exception = JSValueToObject(ctx, exceptionString, NULL);
	return NULL;
	}
}
Comment 1 Darin Adler 2009-12-08 13:07:45 PST
You mentioned you have a test application. Could you attach it to this bug?
Comment 2 martin 2009-12-08 13:13:37 PST
Created attachment 44481 [details]
Sample app which shows the problem.
Comment 3 martin 2009-12-08 13:14:53 PST
(In reply to comment #1)
> You mentioned you have a test application. Could you attach it to this bug?

Sorry, my fault. I've attached it now.
Comment 4 Darin Adler 2009-12-08 16:01:11 PST
Created attachment 44494 [details]
Fixed version of sample application

I fixed the sample application. The code to create the exception was making a string object, but the code to receive the exception expected an object with a property named message.

Note there is also a storage leak in the test application. There are calls to create strings without corresponding release calls.
Comment 5 martin 2009-12-08 16:51:43 PST
(In reply to comment #4)
> I fixed the sample application. The code to create the exception was making a
> string object, but the code to receive the exception expected an object with a
> property named message.

Thank you very much for that fast help. That works great. I already assumed that I was doing something wrong. 

Maybe it would be worth to mention that behavior i the documentation of the JavascriptCore framework.
Comment 6 Gavin Barraclough 2011-06-14 16:31:14 PDT
Looks fine with Darin's fixes.