An arrays of wstrings works and is easy (well, XPCOM Easy at least :-).
eg, nsILoginManager has an interface that returns an array of strings:
void getAllDisabledHosts(out unsigned long count, [retval, array, size_is(count)] out wstring hostnames);
(From Alex: Don't I feel really, really stupid right about now. :-) I thought wstring was deprecated.)
Posted by Justin Dolske at December 29, 2008 9:10 PMI just ran into this with one of the extensions I work on. I ended up changing the method on the interface to return a string enumerator. I then wrapped my string array in a javascript implementation of a string enumerator.
function StringEnumerator(strings) {
//setup key array
this._strings = strings;
this._index = 0;
}
StringEnumerator.prototype = {
_index: null,
_strings: null,
QueryInterface: function stringenumerator_QI(iid) {
if (iid.equals(Components.interfaces.nsIStringEnumerator) ||
iid.equals(Components.interfaces.nsISupports))
return this;
throw Components.results.NS_ERROR_NO_INTERFACE;
},
hasMore: function stringenumerator_hasMore() {
return this._index
},
getNext: function stringenumerator_getNext() {
var rv = this._strings[this._index];
this._index++;
return rv;
}
};
According to http://benjamin.smedbergs.us/blog/2008-09-26/allocated-memory-and-shared-library-boundaries/ wstring should no longer be used in new code, indeed.
Posted by mcsmurf at December 30, 2008 11:56 AMAnother alternative if you want to return an array from C++ to JavaScript is to provide an object with a length attribute and item method. This results in an object that can be also read from JavaScript as if it was an array. A few of the interfaces also provide the getNamedItem method which simulates a hash (but only for names that failed an interface lookup). In rare cases the methods are renamed (but strangely not the length attribute, see the comment for nsITreeColumn).
(From Alex: My nsIDOMStringArray object indeed provides a length method and an "item" method - getStringAtIndex. Does it really work for JS array indexes if I simply rename it to item? I thought you needed help from someone for that, like nsArraySH.)
Posted by Neil Rashbrook at December 30, 2008 12:54 PMOf course, an array of wstrings is easy to leak, especially if you need to return an nsresult in the middle of constructing your array. And returning an array of wstrings is mildly confusing for those people unused to writing *** in their type names.
Posted by Neil Rashbrook at December 30, 2008 12:56 PM