LCOV - code coverage report
Current view: directory - tools/profiler - JSObjectBuilder.h (source / functions) Found Hit Coverage
Test: app.info Lines: 37 0 0.0 %
Date: 2012-06-02 Functions: 7 0 0.0 %

       1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2                 : /* ***** BEGIN LICENSE BLOCK *****
       3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       4                 :  *
       5                 :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :  * the License. You may obtain a copy of the License at
       8                 :  * http://www.mozilla.org/MPL/
       9                 :  *
      10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12                 :  * for the specific language governing rights and limitations under the
      13                 :  * License.
      14                 :  *
      15                 :  * The Original Code is mozilla.org code.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is
      18                 :  * Mozilla Foundation.
      19                 :  * Portions created by the Initial Developer are Copyright (C) 2012
      20                 :  * the Initial Developer. All Rights Reserved.
      21                 :  *
      22                 :  * Contributor(s):
      23                 :  *   Jeff Muizelaar <jmuizelaar@mozilla.com>
      24                 :  *
      25                 :  * Alternatively, the contents of this file may be used under the terms of
      26                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      27                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      28                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      29                 :  * of those above. If you wish to allow use of your version of this file only
      30                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      31                 :  * use your version of this file under the terms of the MPL, indicate your
      32                 :  * decision by deleting the provisions above and replace them with the notice
      33                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      34                 :  * the provisions above, a recipient may use your version of this file under
      35                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      36                 :  *
      37                 :  * ***** END LICENSE BLOCK ***** */
      38                 : 
      39                 : #include "jsapi.h"
      40                 : 
      41                 : /* this is handy wrapper around JSAPI to make it more pleasant to use.
      42                 :  * We collect the JSAPI errors and so that callers don't need to */
      43                 : class JSObjectBuilder
      44                 : {
      45                 :   public:
      46                 : 
      47               0 :   void DefineProperty(JSObject *aObject, const char *name, JSObject *aValue)
      48                 :   {
      49               0 :     if (!mOk)
      50               0 :       return;
      51                 : 
      52               0 :     mOk = JS_DefineProperty(mCx, aObject, name, OBJECT_TO_JSVAL(aValue), NULL, NULL, JSPROP_ENUMERATE);
      53                 :   }
      54                 : 
      55                 :   void DefineProperty(JSObject *aObject, const char *name, int value)
      56                 :   {
      57                 :     if (!mOk)
      58                 :       return;
      59                 : 
      60                 :     mOk = JS_DefineProperty(mCx, aObject, name, INT_TO_JSVAL(value), NULL, NULL, JSPROP_ENUMERATE);
      61                 :   }
      62                 : 
      63                 :   void DefineProperty(JSObject *aObject, const char *name, double value)
      64                 :   {
      65                 :     if (!mOk)
      66                 :       return;
      67                 : 
      68                 :     mOk = JS_DefineProperty(mCx, aObject, name, DOUBLE_TO_JSVAL(value), NULL, NULL, JSPROP_ENUMERATE);
      69                 :   }
      70                 : 
      71               0 :   void DefineProperty(JSObject *aObject, const char *name, nsAString &value)
      72                 :   {
      73               0 :     if (!mOk)
      74               0 :       return;
      75                 : 
      76               0 :     const nsString &flat = PromiseFlatString(value);
      77               0 :     JSString *string = JS_NewUCStringCopyN(mCx, static_cast<const jschar*>(flat.get()), flat.Length());
      78               0 :     if (!string)
      79               0 :       mOk = JS_FALSE;
      80                 : 
      81               0 :     if (!mOk)
      82                 :       return;
      83                 : 
      84               0 :     mOk = JS_DefineProperty(mCx, aObject, name, STRING_TO_JSVAL(string), NULL, NULL, JSPROP_ENUMERATE);
      85                 :   }
      86                 : 
      87               0 :   void DefineProperty(JSObject *aObject, const char *name, const char *value)
      88                 :   {
      89               0 :     nsAutoString string = NS_ConvertASCIItoUTF16(value);
      90               0 :     DefineProperty(aObject, name, string);
      91               0 :   }
      92                 : 
      93                 :   void ArrayPush(JSObject *aArray, int value)
      94                 :   {
      95                 :     if (!mOk)
      96                 :       return;
      97                 : 
      98                 :     jsval objval = INT_TO_JSVAL(value);
      99                 :     uint32_t length;
     100                 :     mOk = JS_GetArrayLength(mCx, aArray, &length);
     101                 : 
     102                 :     if (!mOk)
     103                 :       return;
     104                 : 
     105                 :     mOk = JS_SetElement(mCx, aArray, length, &objval);
     106                 :   }
     107                 : 
     108                 : 
     109               0 :   void ArrayPush(JSObject *aArray, JSObject *aObject)
     110                 :   {
     111               0 :     if (!mOk)
     112               0 :       return;
     113                 : 
     114               0 :     jsval objval = OBJECT_TO_JSVAL(aObject);
     115                 :     uint32_t length;
     116               0 :     mOk = JS_GetArrayLength(mCx, aArray, &length);
     117                 : 
     118               0 :     if (!mOk)
     119               0 :       return;
     120                 : 
     121               0 :     mOk = JS_SetElement(mCx, aArray, length, &objval);
     122                 :   }
     123                 : 
     124               0 :   JSObject *CreateArray() {
     125               0 :     JSObject *array = JS_NewArrayObject(mCx, 0, NULL);
     126               0 :     if (!array)
     127               0 :       mOk = JS_FALSE;
     128                 : 
     129               0 :     return array;
     130                 :   }
     131                 : 
     132               0 :   JSObject *CreateObject() {
     133               0 :     JSObject *obj = JS_NewObject(mCx, NULL, NULL, NULL);
     134               0 :     if (!obj)
     135               0 :       mOk = JS_FALSE;
     136                 : 
     137               0 :     return obj;
     138                 :   }
     139                 : 
     140                 : 
     141                 :   // We need to ensure that this object lives on the stack so that GC sees it properly
     142               0 :   JSObjectBuilder(JSContext *aCx) : mCx(aCx), mOk(JS_TRUE)
     143                 :   {
     144               0 :   }
     145                 :   private:
     146                 :   JSObjectBuilder(JSObjectBuilder&);
     147                 : 
     148                 :   JSContext *mCx;
     149                 :   JSObject *mObj;
     150                 :   JSBool mOk;
     151                 : };
     152                 : 
     153                 : 

Generated by: LCOV version 1.7