Mozdev
mozilla/verbosio/core/modules/ArrayConverter.jsm
author Alex Vincent@SKYFIREDEMO1
Mon Oct 20 22:25:52 2008 -0700 (15 months ago)
changeset 46 9db1ccb62e4d
permissions -rw-r--r--
Remove more JSLib cruft.
     1 /* ***** BEGIN LICENSE BLOCK *****
     2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
     3  *
     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/
     8  *
     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
    12  * License.
    13  *
    14  * The Original Code is Code modules: JavaScript array converter.
    15  *
    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.
    20  *
    21  * Contributor(s):
    22  *
    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.
    34  *
    35  * ***** END LICENSE BLOCK ***** */
    36 
    37 /**
    38  * Utilities for JavaScript components loaded by the JS component
    39  * loader.
    40  *
    41  * Import into a JS component using
    42  * 'Components.utils.import("resource://app/modules/ArrayConverter.jsm");'
    43  *
    44  */
    45 
    46 EXPORTED_SYMBOLS = [ "ArrayConverter" ];
    47 
    48 debug("*** loading ArrayConverter\n");
    49 
    50 /**
    51  * Get a simple enumerator.
    52  *
    53  * @param aItemsList JavaScript array to enumerate.
    54  */
    55 function Enumerator(aItemsList) {
    56   this._itemsList = aItemsList;
    57   this._iteratorPosition = 0;
    58 }
    59 Enumerator.prototype = {
    60   // nsISimpleEnumerator
    61   hasMoreElements: function hasMoreElements() {
    62     return (this._iteratorPosition < this._itemsList.length);
    63   },
    64 
    65   // nsISimpleEnumerator
    66   getNext: function getNext() {
    67     if (!(this.hasMoreElements())) {
    68       throw Components.results.NS_ERROR_FAILURE;
    69     }
    70     var type = this._itemsList[this._iteratorPosition];
    71     this._iteratorPosition++;
    72     return type;
    73   },
    74 
    75   // nsISupports
    76   QueryInterface: function QueryInterface(aIID)
    77   {
    78     if (aIID.equals(Components.interfaces.nsISimpleEnumerator) ||
    79         aIID.equals(Components.interfaces.nsISupports))
    80       return this;
    81 
    82     throw Components.results.NS_ERROR_NO_INTERFACE;
    83   }
    84 }
    85 
    86 function NSArray(aItemsList) {
    87   this._itemsList = aItemsList;
    88 }
    89 NSArray.prototype = {
    90   // nsIArray
    91   get length() {
    92     return this._itemsList.length;
    93   },
    94 
    95   // nsIArray
    96   queryElementAt: function queryElementAt(aIndex, aIID) {
    97     if (aIndex > this.length - 1) {
    98       throw Components.results.NS_ERROR_ILLEGAL_VALUE;
    99     }
   100 
   101     return this._itemsList[aIndex].QueryInterface(aIID);
   102   },
   103 
   104   // nsIArray
   105   indexOf: function indexOf(aIndex, aElement) {
   106     for (var i = aIndex; i < this._itemsList.length; i++) {
   107       if (this._itemsList[i] == aElement) {
   108         return i;
   109       }
   110     }
   111     throw Components.results.NS_ERROR_NOT_FOUND;
   112   },
   113 
   114   // nsIArray
   115   enumerate: function enumerate() {
   116     return new Enumerator(this._itemsList);
   117   },
   118 
   119   // nsISupports
   120   QueryInterface: function QueryInterface(aIID)
   121   {
   122     if (aIID.equals(Components.interfaces.nsIArray) ||
   123         aIID.equals(Components.interfaces.nsISupports))
   124       return this;
   125 
   126     throw Components.results.NS_ERROR_NO_INTERFACE;
   127   }
   128 }
   129 
   130 var ArrayConverter = {
   131   /**
   132    * Get a JavaScript array for a nsIArray or nsISimpleEnumerator.
   133    *
   134    * @param aObject nsIArray or nsISimpleEnumerator to convert.
   135    *
   136    * @throws NS_ERROR_INVALID_ARG.
   137    */
   138   JSArray: function getJSArray(aObject) {
   139     if (aObject instanceof Components.interfaces.nsIArray) {
   140       aObject = aObject.enumerate();
   141     }
   142 
   143     if (!(aObject instanceof Components.interfaces.nsISimpleEnumerator)) {
   144       throw Components.results.NS_ERROR_INVALID_ARG;
   145     }
   146 
   147     var array = [];
   148     while (aObject.hasMoreElements()) {
   149       array[array.length] = aObject.getNext();
   150     }
   151     return array;
   152   },
   153 
   154   /**
   155    * Return a nsIArray for a JavaScript array.
   156    *
   157    * @param aArray JavaScript array to convert.
   158    */
   159   nsIArray: function get_nsIArray(aArray) {
   160     return new NSArray(aArray);
   161   },
   162 
   163   /**
   164    * Return a nsISimpleEnumerator for a JavaScript array.
   165    *
   166    * @param aArray JavaScript array to convert.
   167    */
   168   enumerator: function get_enumerator(/* in JSArray */ aArray) {
   169     return new Enumerator(aArray);
   170   },
   171 
   172   /**
   173    * Return a method to convert a JavaScript array into a XPCOM array.
   174    *
   175    * @param aArrayName Name of JavaScript array property in "this" to convert.
   176    */
   177   xpcomArrayMethod: function getXPCOMMethod(aArrayName) {
   178     /**
   179      * Return a XPCOM array and count.
   180      *
   181      * @param (out) aCount The length of the array returned.
   182      *
   183      * @return The XPCOM array.
   184      */
   185     return function xpcomArray(aCount) {
   186       var array = this[aArrayName];
   187       aCount.value = array.length;
   188       var rv = [];
   189       for (var i = 0; i < array.length; i++) {
   190         rv[i] = array[i];
   191       }
   192       return rv;
   193     }
   194   }
   195 };