How to create a new JS object using NPAPI?

馋奶兔 提交于 2019-12-25 03:46:31

问题


I'm writing an NPAPI plugin. In C++ code, I can't create a JS object using NPAPI. I tried this method:

Foo.js

function Foo(p)
{

   this.a=p;
}

Foo.prototype.get=function()
{
   return this.a;
}

C++ code (I want to create the Foo object. Just link what we can do in JS . var s=new Foo(2);)

    int newJSObject(const NPP instance,const NPNetscapeFuncs* npnfuncs,const string    objectName,
      const NPVariant* args,const int argsCount,NPVariant& result)
   {

       int status;

       NPObject *winobj;
       npnfuncs->getvalue(instance,NPNVWindowNPObject,&winobj);

       NPVariant npJS;
       NPIdentifier npJSId=npnfuncs->getstringidentifier(objectName.c_str());

       status=npnfuncs->getproperty(instance,winobj,npJSId,&npJS);

       NPObject* npObjJS = NPVARIANT_TO_OBJECT(npJS);

       NPVariant npPrototype;
       NPIdentifier npPrototypeId=npnfuncs->getstringidentifier("prototype");
       status=npnfuncs->getproperty(instance,npObjJS,npPrototypeId,&npPrototype);

       NPObject* npObjPrototype = NPVARIANT_TO_OBJECT(npPrototype);


       NPVariant npJSConstructor;
       NPIdentifier npJSConstructorId=npnfuncs->getstringidentifier("constructor");

       status=npnfuncs->invoke(instance,npObjPrototype,npJSConstructorId,
         args,argsCount,&npJSConstructor);

    /*
    *   destroy memory
    * */
       npnfuncs->releaseobject(winobj);
       npnfuncs->releasevariantvalue(&npJS);
       npnfuncs->releasevariantvalue(&npJSConstructor);

       result=npJS;

       return status;
    }




    NPVariant jsFoo1;
    NPVariant arg;
    DOUBLE_TO_NPVARIANT(2,arg);
    NPVariant prams[]={arg};
    newJSObject(instance,npnfuncs,"Foo",prams,sizeof(prams)/sizeof(prams[0]),jsFoo);

    NPVariant jsFoo2;
    NPVariant arg;
    DOUBLE_TO_NPVARIANT(3,arg);
    NPVariant prams[]={arg};
    newJSObject(instance,npnfuncs,"Foo",prams,sizeof(prams)/sizeof(prams[0]),jsFoo);

But if we return jsFoo1 and jsFoo2 to JS code, then we call Fooxxx.get(). Both jsFoo1 and jsFoo2 is 3. I know the problem is "constructor"

Can anyone give me another method the create the JS object in C++ using NPAPI?


回答1:


NPN_Construct doesn't seem to work on browser objects, so there is no direct way to do what you want. What you could do, however, is make a javascript function to create the object for you and return it; you could even use NPN_Evaluate to inject it automatically into the page.

something like this:

function __create_object_2_params(obj, param1, param2) {
    return new obj(param1, param2);
}

FireBreath uses some similar tricks in places.




回答2:


I don't know what's wrong with your code, but I do know that when I needed a NPAPI plugin, the following projects have made it really easy:

FireBreath - "a framework that allows easy creation of powerful browser plugins"

nixysa - "A glue code generation framework for NPAPI plugins"

Both projects do all the hard work for you, so you can create simple C++ classes on one side, and easily use them as JS objects on the other side. If you're not too heavily invested in the NPAPI stuff, I suggest you give them a try. Could save you a lot of hassle.



来源:https://stackoverflow.com/questions/6838567/how-to-create-a-new-js-object-using-npapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!