2

我正在将 JS 的侦听器注册到 NPAPI 插件。为了不多次注册同一个侦听器,我需要一种方法来将传递的 NPVariant 对象与列表中已经存在的对象进行比较。

这就是我从 JS 注册监听器的方式:

    PluginObject.registerListener("event", listener);

然后在插件源中:

    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;
      } 
    }
4

2 回答 2

0

当您谈论 javascript 函数时,NPVariant 只是一个 NPObject。

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

比较 val.type 和 val.objectValue。这通常会起作用,但如果不起作用,则没有其他方法,所以您最好还是尝试一下。我想另一种可能性是创建一个 javascript 函数来比较它们,用 NPN_Evaluate 注入它并用两个对象调用它。

于 2012-10-18T18:33:37.883 回答
0

我认为您不能依赖 objectValue。例如,如果您执行以下操作:

foo={}; bar=foo; x={};
x.f=foo; x.b=bar;

Now, if you call NPN_Enumerate and pass x as the NPObject, you get two identifiers. Calling GetProperty for each of these returns NPVariants, but the value of variant->value.objectValue will be different for each, and different again in subsequent calls to NPN_Enumerate.

taxilian: is there significant overhead in calling NPN_Invoke with the two NPObjects, just to test for equality? This also involves some calls to GetProperty and the creation of identifiers and calling the NPVARIANT macros to test the results, etc.. I am wondering just how much logic I should be injecting and evaluating in Javascript.. this code injection seems to come up as a solution again and again. Is it costly?

于 2012-10-19T06:51:25.033 回答