Sergej Marsnjak wrote:

> Thanks for answer, but it's not enough for me.
> 
> The .js sample just calls C++ methods from javaScript. It does have a
> definition for some interface, but a problem is that, if I want to call
> javaScript methods, I must get a pointer to an interface representing java;
> so where to get this pointer?


Okay, I'm confused ... Java or JavaScript? To deal with any
XPCOM component whether implemented in C++ or JS, you must
have a pointer to it. The sample HTML code shows how to
create an instance of the component from JS like so ...

var sample = Components.classes["@mozilla.org/sample;1"].createInstance();
sample = sample.QueryInterface(Components.interfaces.nsISample);

Now, when you run the xpconnect-sample.html, you will be loading
either the C++ implementation or the JS implementation depending
upon which version has been registered. You could rename the
contract id of one to run them side by side.


> Is it possible to implement some C++ function, which is called by JS at
> start of HTML and gives a pointer of an interface to C++ (something like
> "SetJavaScriptInterface(nsSomeJavaInterface *ptr)")? How can I write this in
> JS?


To repeat, you do it like this (using nsSample as example) ...

// First, instantiate the object and return a pointer to nsISupports.
var sample = Components.classes["@mozilla.org/sample;1"].createInstance();
// You don't really have pointers in JS, but you can access the
// methods and properties of the underlying interface.

// Use the QI method to get to the nSISample interface.
// Place the result into the same JS variable.
sample = sample.QueryInterface(Components.interfaces.nsISample);

So ... to recap, "@mozilla.org/sample;1" is the contract ID of the
component to be created. If you want a different component, use
a different contract id. "nsISample" is the name of the interface
implemented by the component. To use a different interface, substitute
the other interface name in place of "nsISample".

> Thanks,
> Sergej


Good luck,
Rick


Reply via email to