Subj : Re: exceptions To : Jeroen de Borst From : Brendan Eich Date : Fri Mar 05 2004 11:09 am Jeroen de Borst wrote: >Hi, > >I would like my C code to throw an exception that I can then catch from the >script that invoked it. Ideally the exception I throw would be subclassed >from "Error" (so I'll inherit the toString() method and stack property; the >message property would be overloaded). > >So I'd need to create a prototype object with the Error class defined in >jsexn.c (ExceptionClass), and then I can use that in a call to JS_InitClass. >But ExceptionClass is a static structure. So I don't see how to do this >without modifying the SpiderMonkey source (which I don't want to do for >licensing reasons). > >Is there another (intended) way to do this? > Sure, you can JS_GetProperty(cx, global, "Error", &v) to get the constructor, then JS_GetProperty(cx, JSVAL_TO_OBJECT(v), "prototype", &v), then JS_GET_CLASS(cx, JSVAL_TO_OBJECT(v)). Error and type checking elided, as usual. If this is a startup only cost, static const char Ep[] = "Error.prototype"; JS_EvaluateScript(cx, global, Ep, sizeof Ep - 1, NULL, 0, &v); is a little easier to read. /be .