Plugins:NPN SetExceptionVariant

From MozillaWiki
Jump to: navigation, search


Under consideration.


Unlike JavaScript the current NPN_SetException[1] call does not allow to throw custom objects or plain JS types as exceptions from a plugin. Hence the NPN_SetException limits scriptable objects to just throwing errors with a defined message property.

Current Proposal

  • Last modified: July 14, 2009
  • Author: Anselm R Garbe, Aplix Corporation
  • Contributors:


One possible solution to this limitation is the introduction of a new function call to trigger script exceptions as follows:

void NPN_SetExceptionVariant(NPObject *npobj, const NPVariant *ex);

The semantics of NPN_SetExceptionVariant remain similiar to NPN_SetException, though the NPN_SetExceptionVariant call triggers a script exception upon return from entry points into 'npobj' where the given object thrown is the value of 'ex'. Such a variant value may be a custom object exposing properties, methods and type information that extends the typical 'message' only approach and enables more fine-grain and language-independent exception handling in JavaScript.

An Example

bool SomeClass::invokeDefault(NPObject *npObj, const NPVariant *args, uint32_t argCount, NPVariant *result) {
        static NPVariant v;
        NPObject *ob;

        if(createCustomErrorObject(&ob) == NoError) {
                OBJECT_TO_NPVARIANT(ob, v); 
                NPN_SetExceptionVariant(npObj, &v);
                return false;
        INT32_TO_NPVARIANT(0, *result);
        return true;

Legacy APIs

The proposed NPAPI extension works well with legacy APIs, because it doesn't attempt to change NPN_SetException but introduces an alternative call instead.