简体   繁体   中英

XPCOM object method is unaccessible from JavaScript

I'm trying to build an extension for Firefox. This extension uses an XPCOM component (a C++ dll). I'm compiling the DLL, compilation is OK.

I also succeeded in building a JS code which instanciates the object from XPCOM:

try {
   greenfox;
   return true;
} catch( e ) {
   alert( e );
   return false;
}

The object returned is this one:

QueryInterface
    QueryInterface()

__proto__
    [xpconnect wrapped native prototype] { QueryInterface=QueryInterface()}

QueryInterface
    QueryInterface()

Everything is fine, except I can't call the function which are supposed to be in my XPCOM component.

Here is my IDL file:

[scriptable, uuid(ec8030f7-c20a-464f-9b0e-13a3a9e97384)]
interface nsISample : nsISupports
{
    attribute string value;
    void writeValue(in string aPrefix);
    void poke(in string aValue);

    void start();
    double stop();
};

When callingstart() function, I get the Javascript error: "is not a function"

greenfox.start();

Do you have any idea? It seems like no function is exposed in my XPCOM.

You seem to be looking at an object exposing only the nsISupports interface. Your interface ( nsISample ) won't get exposed by default, you have to explicitly request it. You can do it for example by instantiating your component like this:

var greenfox = Components.classes["..."].getService(Components.interfaces.nsISample);
greenfox.start();

Alternatively, you can also call QueryInterface on an object you already have:

greenfox.QueryInterface(Components.interfaces.nsISample);
greenfox.start();

Generally, I wouldn't recommend using a binary XPCOM component for reasons outlined here , maintaining them requires way too much effort. I would rather suggest compiling a regular DLL and using it via js-ctypes . Reference a binary-component to js-ctypes mentions how you would locate a DLL inside your add-on to use it via js-ctypes.

Do you call QueryInterface with your uuid? It is necessary to call it before using the component instance created. Does your usage match what's in here ?

If you don't want to deal with XPCOM you can use js-ctypes .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM