Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

XPCOM -> JavaScript

2 views
Skip to first unread message

MATTHEWS,HEATHER , HP-Vancouver,ex1

unread,
Aug 15, 2000, 3:00:00 AM8/15/00
to mozill...@mozilla.org
Is there a way to call a function in JavaScript from an XPCOM component?


Brendan Eich

unread,
Aug 15, 2000, 3:00:00 AM8/15/00
to MATTHEWS,HEATHER , HP-Vancouver,ex1
"MATTHEWS,HEATHER , HP-Vancouver,ex1" wrote:
Is there a way to call a function in JavaScript from an XPCOM component?
The new way: have that JS function be a method, i.e. a function-valued property of an object, where the object containing it has other methods that together implement an XPIDL-defined interface.

[scriptable, uuid(...)]
interface myFoo : nsISupports {
  void bar(in string blah);
}

in myFoo.idl, and

var myFoo = {
  bar: function (blah) {
    dump(blah);
    do whatever else with blah you like
  }
};

in a .js file, call it myFoo.js.  Then you just need to register the JS object with your XPCOM (presumably, you mean C++) component.  To do that, your component would need to supply a factory or service by which you could pass myFoo in.  Suppose it does a service, myFooRegistry:

const MY_FOO_REGISTRY_PROGID = "...";
const MY_FOO_INTERFACE = Components.interfaces.myFoo;
var myFooService = Components.classes[MY_FOO_REGISTRY_PROGID].getService();
var myFooRegistry = myFooService.QueryInterface(MY_FOO_INTERFACE);
myFooRegistry.registerFoo(myFoo);

Your C++ component will have to implement the interface method I show being called immediately above:

nsresult myFooRegistry::RegisterFoo(myFoo *aFoo);

(C++ method spelling, NB).  It can hold onto the passed-in interface and cal its blah method later.

The old way to do this involves the JS API (http://lxr.mozilla.org/mozilla/source/js/src/jsapi.h, http://www.mozilla.org/js/spidermonkey/), which is not as good as the new way, because it doesn't use XPIDL to separate interface from implementation (and from implementation language).

/be
 

0 new messages