我想我是在回答我自己的问题…
我发现的解决方案(我仍然很感激对其有效性的评论,特别是如果你认为有更好的方法可以做到这一点)是分配一个NPObject派生的结构,它在我从插件中向firefox公开的allocate()函数中有一个指向我的实现类的指针。然后,我在npp的pdata成员npp_new()中存储一个指向该np对象的指针。
在invoke()中,我将得到的npObject指针转换为派生结构的附加成员,这样我就可以得到指向实现类实例的指针。
据我所知,这就是设计的目的——npObject对象是它们指向的npClass的实例,它们通过处理这些实体的npClass函数指针实现方法和属性,并且任何私有数据都应该由实现分配和释放,其格式是未指定的。
它看起来像这样:
static NPClass refObject = {
NP_CLASS_STRUCT_VERSION,
My_Allocate,
My_Deallocate,
NULL,
My_HasMethod,
My_Invoke,
My_InvokeDefault,
My_HasProperty,
My_GetProperty,
NULL,
NULL,
};
class MyImplClass {
// Implementation goes here
};
struct MyNPObject : public NPObject {
MyImplClass *my_impl_instance;
};
// This is just a bit of memory management - Mozilla wants us to allocate our own memory:
NPObject *My_Allocate(NPP inst, NPClass *)
{
// We initialize the structure in NPP_New() below
return (NPObject *)malloc(sizeof(MyNPObject));
}
NPError NPP_New( NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
char* argn[], char* argv[], NPSavedData* saved )
{
NPObject *scriptable_object = npnfuncs->createobject(instance, &refObject);
npnfuncs->retainobject(scriptable_object);
MyImplClass *new_player = new MyImplClass();
instance->pdata = scriptable_object;
((MyNPObject*)instance->pdata)->my_impl_instance = new_player;
return NPERR_NO_ERROR;
}