LCOV - app.info - toolkit/components/ctypes/ctypes.cpp

LCOV - code coverage report
| Current view: | directory - toolkit/components/ctypes - ctypes.cpp (source / functions) |  | Found | Hit | Coverage |
| Test: | app.info | Lines: | 44 | 35 | 79.5 %
| Date: | 2012-04-21 | Functions: | 10 | 10 | 100.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 js-ctypes.
          16                 :  *
          17                 :  * The Initial Developer of the Original Code is
          18                 :  * The Mozilla Foundation <http://www.mozilla.org/>.
          19                 :  * Portions created by the Initial Developer are Copyright (C) 2009
          20                 :  * the Initial Developer. All Rights Reserved.
          21                 :  *
          22                 :  * Contributor(s):
          23                 :  *  Mark Finkle <mark.finkle@gmail.com>, <mfinkle@mozilla.com>
          24                 :  *  Dan Witte <dwitte@mozilla.com>
          25                 :  *
          26                 :  * Alternatively, the contents of this file may be used under the terms of
          27                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
          28                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
          29                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
          30                 :  * of those above. If you wish to allow use of your version of this file only
          31                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
          32                 :  * use your version of this file under the terms of the MPL, indicate your
          33                 :  * decision by deleting the provisions above and replace them with the notice
          34                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
          35                 :  * the provisions above, a recipient may use your version of this file under
          36                 :  * the terms of any one of the MPL, the GPL or the LGPL.
          37                 :  *
          38                 :  * ***** END LICENSE BLOCK ***** */
          39                 :
          40                 : #include "ctypes.h"
          41                 : #include "jsapi.h"
          42                 : #include "mozilla/ModuleUtils.h"
          43                 : #include "nsMemory.h"
          44                 : #include "nsString.h"
          45                 : #include "nsNativeCharsetUtils.h"
          46                 :
          47                 : #define JSCTYPES_CONTRACTID \
          48                 :   "@mozilla.org/jsctypes;1"
          49                 :
          50                 :
          51                 : #define JSCTYPES_CID \
          52                 : { 0xc797702, 0x1c60, 0x4051, { 0x9d, 0xd7, 0x4d, 0x74, 0x5, 0x60, 0x56, 0x42 } }
          53                 :
          54                 : namespace mozilla {
          55                 : namespace ctypes {
          56                 :
          57                 : static char*
          58              56 : UnicodeToNative(JSContext *cx, const jschar *source, size_t slen)
          59                 : {
          60             112 :   nsCAutoString native;
          61             112 :   nsDependentString unicode(reinterpret_cast<const PRUnichar*>(source), slen);
          62              56 :   nsresult rv = NS_CopyUnicodeToNative(unicode, native);
          63              56 :   if (NS_FAILED(rv)) {
          64               0 :     JS_ReportError(cx, "could not convert string to native charset");
          65               0 :     return NULL;
          66                 :   }
          67                 :
          68              56 :   char* result = static_cast<char*>(JS_malloc(cx, native.Length() + 1));
          69              56 :   if (!result)
          70               0 :     return NULL;
          71                 :
          72              56 :   memcpy(result, native.get(), native.Length() + 1);
          73              56 :   return result;
          74                 : }
          75                 :
          76                 : static JSCTypesCallbacks sCallbacks = {
          77                 :   UnicodeToNative
          78                 : };
          79                 :
          80             546 : NS_GENERIC_FACTORY_CONSTRUCTOR(Module)
          81                 :
          82            7644 : NS_IMPL_ISUPPORTS1(Module, nsIXPCScriptable)
          83                 :
          84             273 : Module::Module()
          85                 : {
          86             273 : }
          87                 :
          88             273 : Module::~Module()
          89                 : {
          90             273 : }
          91                 :
          92                 : #define XPC_MAP_CLASSNAME Module
          93                 : #define XPC_MAP_QUOTED_CLASSNAME "Module"
          94                 : #define XPC_MAP_WANT_CALL
          95                 : #define XPC_MAP_FLAGS nsIXPCScriptable::WANT_CALL
          96                 : #include "xpc_map_end.h"
          97                 :
          98                 : static JSBool
          99            1092 : SealObjectAndPrototype(JSContext* cx, JSObject* parent, const char* name)
         100                 : {
         101                 :   jsval prop;
         102            1092 :   if (!JS_GetProperty(cx, parent, name, &prop))
         103               0 :     return false;
         104                 :
         105            1092 :   JSObject* obj = JSVAL_TO_OBJECT(prop);
         106            1092 :   if (!JS_GetProperty(cx, obj, "prototype", &prop))
         107               0 :     return false;
         108                 :
         109            1092 :   JSObject* prototype = JSVAL_TO_OBJECT(prop);
         110            1092 :   return JS_FreezeObject(cx, obj) && JS_FreezeObject(cx, prototype);
         111                 : }
         112                 :
         113                 : static JSBool
         114             273 : InitAndSealCTypesClass(JSContext* cx, JSObject* global)
         115                 : {
         116                 :   // Init the ctypes object.
         117             273 :   if (!JS_InitCTypesClass(cx, global))
         118               0 :     return false;
         119                 :
         120                 :   // Set callbacks for charset conversion and such.
         121                 :   jsval ctypes;
         122             273 :   if (!JS_GetProperty(cx, global, "ctypes", &ctypes))
         123               0 :     return false;
         124                 :
         125             273 :   JS_SetCTypesCallbacks(JSVAL_TO_OBJECT(ctypes), &sCallbacks);
         126                 :
         127                 :   // Seal up Object, Function, Array and Error and their prototypes.  (This
         128                 :   // single object instance is shared amongst everyone who imports the ctypes
         129                 :   // module.)
         130            1092 :   if (!SealObjectAndPrototype(cx, global, "Object") ||
         131             273 :       !SealObjectAndPrototype(cx, global, "Function") ||
         132             273 :       !SealObjectAndPrototype(cx, global, "Array") ||
         133             273 :       !SealObjectAndPrototype(cx, global, "Error"))
         134               0 :     return false;
         135                 :
         136                 :   // Finally, seal the global object, for good measure. (But not recursively;
         137                 :   // this breaks things.)
         138             273 :   return JS_FreezeObject(cx, global);
         139                 : }
         140                 :
         141                 : NS_IMETHODIMP
         142             273 : Module::Call(nsIXPConnectWrappedNative* wrapper,
         143                 :              JSContext* cx,
         144                 :              JSObject* obj,
         145                 :              PRUint32 argc,
         146                 :              jsval* argv,
         147                 :              jsval* vp,
         148                 :              bool* _retval)
         149                 : {
         150             273 :   JSObject* global = JS_GetGlobalForScopeChain(cx);
         151             273 :   if (!global)
         152               0 :     return NS_ERROR_NOT_AVAILABLE;
         153                 :
         154             273 :   *_retval = InitAndSealCTypesClass(cx, global);
         155             273 :   return NS_OK;
         156                 : }
         157                 :
         158                 : }
         159                 : }
         160                 :
         161                 : NS_DEFINE_NAMED_CID(JSCTYPES_CID);
         162                 :
         163                 : static const mozilla::Module::CIDEntry kCTypesCIDs[] = {
         164                 :   { &kJSCTYPES_CID, false, NULL, mozilla::ctypes::ModuleConstructor },
         165                 :   { NULL }
         166                 : };
         167                 :
         168                 : static const mozilla::Module::ContractIDEntry kCTypesContracts[] = {
         169                 :   { JSCTYPES_CONTRACTID, &kJSCTYPES_CID },
         170                 :   { NULL }
         171                 : };
         172                 :
         173                 : static const mozilla::Module kCTypesModule = {
         174                 :   mozilla::Module::kVersion,
         175                 :   kCTypesCIDs,
         176                 :   kCTypesContracts
         177                 : };
         178                 :
         179                 : NSMODULE_DEFN(jsctypes) = &kCTypesModule;

Generated by: LCOV version 1.7