From:  "Berserker" <raffaele.romito@gmail.com>
Date:  28 Jun 2006 19:51:39 Hong Kong Time
Newsgroup:  news.mozilla.org/netscape.public.mozilla.jseng
Subject:  

Exceptions handling

NNTP-Posting-Host:  62.101.126.235

Hi all, plz consider this code:

void alfa()
{
	throw std::exception("alfa error");
}

JSBool js_alfa(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
jsval *rval)
{
	try
	{
		alfa();
	}
	catch(std::exception &e)
	{
		JS_SetPendingException(cx, ...);	// converts e.what() to jsval
		return JS_FALSE;
	}

	return JS_TRUE;
}

void beta()
{
	throw std::exception("beta error");
}

JSBool js_beta(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
jsval *rval)
{
	try
	{
		beta();
	}
	catch(std::exception &e)
	{
		JS_SetPendingException(cx, ...);
		return JS_FALSE;
	}

	return JS_TRUE;
}

void register_functions(JSContext *cx, JSObject *obj)
{
	JSFunctionSpec methods[] =										{
		{ "alfa", js_alfa, 0, 0, 0 },
		{ "beta", js_beta, 0, 0, 0 },
		{ 0, 0, 0, 0, 0 }
	};
	JS_DefineFunctions(cx, obj, methods);
}

In this sample I'm rising exceptions in js by catching them in every
function that act as wrapper of the native counterpart, but I'd like to
write a global one to do this job, for example:

JSBool js_dispatcher(JSNative func, JSContext *cx, JSObject *obj, uintN
argc, jsval *argv, jsval *rval)
{
	try
	{
		return func(cx, obj, argc, argv, rval);
	}
	catch(std::exception &e)
	{
		JS_SetPendingException(cx, ...);
	}

	return JS_FALSE;
}

where "func" could be "js_alfa" or "js_beta". Is that possible? How can
I change the call to JS_DefineFunctions?
Plz help

P.S.: sorry for my english :)