Search code examples
npapi

How to compare NPVariant objects?


I am registering listeners from JS to NPAPI plugin. In order not to register same listener multiple times I need a way to compare passed NPVariant object to those already in the list.

This is how I'm registering listeners from JS :

    PluginObject.registerListener("event", listener);

and then in plugin source :

    for (l=head; l!=NULL; l=l->next) {
      // somehow compare the listeners
      // l->listener holds NPVariant object
      if (l->listener-> ??? == new_lle->listener-> ???) 
      {
        found = 1;
        DBG("listener is a duplicate, not adding.");
        NPN_MemFree(new_lle->listener);
        free(new_lle);
        break;
      } 
    }

Solution

  • when you're talking about a javascript function the NPVariant is just an NPObject.

    typedef struct _NPVariant {
      NPVariantType type;
      union {
        bool boolValue;
        int32_t intValue;
        double_t doubleValue;
        NPString stringValue;
        NPObject *objectValue;
      } value;
    } NPVariant;
    

    compare the val.type and val.objectValue. This will usually work, but if it doesn't there isn't another way so you're still better off trying it. I guess one other possibility would be to create a javascript function to compare them, inject it with NPN_Evaluate and call it with the two objects.