Remove more JSLib cruft.
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is Code modules: JavaScript array converter.
16 * The Initial Developer of the Original Code is
17 * Alexander J. Vincent <ajvincent@gmail.com>.
18 * Portions created by the Initial Developer are Copyright (C) 2007
19 * the Initial Developer. All Rights Reserved.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
38 * Utilities for JavaScript components loaded by the JS component
41 * Import into a JS component using
42 * 'Components.utils.import("resource://app/modules/ArrayConverter.jsm");'
46 EXPORTED_SYMBOLS = [ "ArrayConverter" ];
48 debug("*** loading ArrayConverter\n");
51 * Get a simple enumerator.
53 * @param aItemsList JavaScript array to enumerate.
55 function Enumerator(aItemsList) {
56 this._itemsList = aItemsList;
57 this._iteratorPosition = 0;
59 Enumerator.prototype = {
60 // nsISimpleEnumerator
61 hasMoreElements: function hasMoreElements() {
62 return (this._iteratorPosition < this._itemsList.length);
65 // nsISimpleEnumerator
66 getNext: function getNext() {
67 if (!(this.hasMoreElements())) {
68 throw Components.results.NS_ERROR_FAILURE;
70 var type = this._itemsList[this._iteratorPosition];
71 this._iteratorPosition++;
76 QueryInterface: function QueryInterface(aIID)
78 if (aIID.equals(Components.interfaces.nsISimpleEnumerator) ||
79 aIID.equals(Components.interfaces.nsISupports))
82 throw Components.results.NS_ERROR_NO_INTERFACE;
86 function NSArray(aItemsList) {
87 this._itemsList = aItemsList;
92 return this._itemsList.length;
96 queryElementAt: function queryElementAt(aIndex, aIID) {
97 if (aIndex > this.length - 1) {
98 throw Components.results.NS_ERROR_ILLEGAL_VALUE;
101 return this._itemsList[aIndex].QueryInterface(aIID);
105 indexOf: function indexOf(aIndex, aElement) {
106 for (var i = aIndex; i < this._itemsList.length; i++) {
107 if (this._itemsList[i] == aElement) {
111 throw Components.results.NS_ERROR_NOT_FOUND;
115 enumerate: function enumerate() {
116 return new Enumerator(this._itemsList);
120 QueryInterface: function QueryInterface(aIID)
122 if (aIID.equals(Components.interfaces.nsIArray) ||
123 aIID.equals(Components.interfaces.nsISupports))
126 throw Components.results.NS_ERROR_NO_INTERFACE;
130 var ArrayConverter = {
132 * Get a JavaScript array for a nsIArray or nsISimpleEnumerator.
134 * @param aObject nsIArray or nsISimpleEnumerator to convert.
136 * @throws NS_ERROR_INVALID_ARG.
138 JSArray: function getJSArray(aObject) {
139 if (aObject instanceof Components.interfaces.nsIArray) {
140 aObject = aObject.enumerate();
143 if (!(aObject instanceof Components.interfaces.nsISimpleEnumerator)) {
144 throw Components.results.NS_ERROR_INVALID_ARG;
148 while (aObject.hasMoreElements()) {
149 array[array.length] = aObject.getNext();
155 * Return a nsIArray for a JavaScript array.
157 * @param aArray JavaScript array to convert.
159 nsIArray: function get_nsIArray(aArray) {
160 return new NSArray(aArray);
164 * Return a nsISimpleEnumerator for a JavaScript array.
166 * @param aArray JavaScript array to convert.
168 enumerator: function get_enumerator(/* in JSArray */ aArray) {
169 return new Enumerator(aArray);
173 * Return a method to convert a JavaScript array into a XPCOM array.
175 * @param aArrayName Name of JavaScript array property in "this" to convert.
177 xpcomArrayMethod: function getXPCOMMethod(aArrayName) {
179 * Return a XPCOM array and count.
181 * @param (out) aCount The length of the array returned.
183 * @return The XPCOM array.
185 return function xpcomArray(aCount) {
186 var array = this[aArrayName];
187 aCount.value = array.length;
189 for (var i = 0; i < array.length; i++) {