Right now, the received wisdom as I understand it is that Promises are the hot, hip new way to do asynchronous programming. So, over here in $WE_WANT_TO_REMOVE_CRUFT, I proposed adding a new API that returns a Promise for all the JS consumers. This new API, naturally, is to be implemented in JS. However, we also have a large body of code that may want to use this API who are presently in C++ and can't really move to JS (doing so would require moving tens or hundreds of thousands of line of code more or less simultaneously).

Now, creating a Promise (via mozilla::dom::Promise) from C++ and passing it to JS is possible but kind of difficult, since I need to materialize an nsIGlobalObject which requires too much code. It's not really possible to cast to this method from a JS implemented Promise (since the notion of "promise" in JS is "I have a callable property named then"). It's also the only way you can react to a promise from C++ without stabbing yourself. Since I need C++ to be able to react to JS-implemented stuff, this kind of means I have to use XPIDL instead of WebIDL.

Can promises be specified in XPIDL in such a way that they are usable from C++? Not quite: while [function] automatically converts a function to an nsISupports-derived interface, it doesn't allow the same nsISupports-derived object to be called as a function pointer. e.g., if I defined an interface:

[function]
interface AnyAnyCallback {
  jsval doCallback(in jsval arg);
};

interface nsIPromise {
  nsIPromise then(in AnyAnyCallback resolve, in AnyAnyCallback reject);
};

Then an implementation of a JS .then method couldn't call its resolve argument with resolve(val) but would have to do resolve.doCallback(val).

I've thought about ways to fix this, but they all require modifying xpconnect to some degree, so I wondered if anyone had better solutions to this problem space.

--
Joshua Cranmer
Thunderbird and DXR developer
Source code archæologist

_______________________________________________
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to