greatsea
greatsea

Reputation: 87

How to create a new JS object using NPAPI?

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?

Upvotes: 0

Views: 1561

Answers (2)

taxilian
taxilian

Reputation: 14324

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.

Upvotes: 1

Eran
Eran

Reputation: 22020

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.

Upvotes: 1

Related Questions