toolkit/components/perf/PerfMeasurement.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 /* -*-  Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "PerfMeasurement.h"
     7 #include "jsperf.h"
     8 #include "mozilla/ModuleUtils.h"
     9 #include "nsMemory.h"
    10 #include "mozilla/Preferences.h"
    11 #include "mozJSComponentLoader.h"
    12 #include "nsZipArchive.h"
    14 #define JSPERF_CONTRACTID \
    15   "@mozilla.org/jsperf;1"
    17 #define JSPERF_CID            \
    18 { 0x421c38e6, 0xaee0, 0x4509, \
    19   { 0xa0, 0x25, 0x13, 0x0f, 0x43, 0x78, 0x03, 0x5a } }
    21 namespace mozilla {
    22 namespace jsperf {
    24 NS_GENERIC_FACTORY_CONSTRUCTOR(Module)
    26 NS_IMPL_ISUPPORTS(Module, nsIXPCScriptable)
    28 Module::Module()
    29 {
    30 }
    32 Module::~Module()
    33 {
    34 }
    36 #define XPC_MAP_CLASSNAME Module
    37 #define XPC_MAP_QUOTED_CLASSNAME "Module"
    38 #define XPC_MAP_WANT_CALL
    39 #define XPC_MAP_FLAGS nsIXPCScriptable::WANT_CALL
    40 #include "xpc_map_end.h"
    42 static bool
    43 SealObjectAndPrototype(JSContext* cx, JS::Handle<JSObject *> parent, const char* name)
    44 {
    45   JS::Rooted<JS::Value> prop(cx);
    46   if (!JS_GetProperty(cx, parent, name, &prop))
    47     return false;
    49   if (prop.isUndefined()) {
    50     // Pretend we sealed the object.
    51     return true;
    52   }
    54   JS::Rooted<JSObject*> obj(cx, prop.toObjectOrNull());
    55   if (!JS_GetProperty(cx, obj, "prototype", &prop))
    56     return false;
    58   JS::Rooted<JSObject*> prototype(cx, prop.toObjectOrNull());
    59   return JS_FreezeObject(cx, obj) && JS_FreezeObject(cx, prototype);
    60 }
    62 static bool
    63 InitAndSealPerfMeasurementClass(JSContext* cx, JS::Handle<JSObject*> global)
    64 {
    65   // Init the PerfMeasurement class
    66   if (!JS::RegisterPerfMeasurement(cx, global))
    67     return false;
    69   // Seal up Object, Function, and Array and their prototypes.  (This single
    70   // object instance is shared amongst everyone who imports the jsperf module.)
    71   if (!SealObjectAndPrototype(cx, global, "Object") ||
    72       !SealObjectAndPrototype(cx, global, "Function") ||
    73       !SealObjectAndPrototype(cx, global, "Array"))
    74     return false;
    76   // Finally, seal the global object, for good measure. (But not recursively;
    77   // this breaks things.)
    78   return JS_FreezeObject(cx, global);
    79 }
    81 NS_IMETHODIMP
    82 Module::Call(nsIXPConnectWrappedNative* wrapper,
    83              JSContext* cx,
    84              JSObject* obj,
    85              const JS::CallArgs& args,
    86              bool* _retval)
    87 {
    89   mozJSComponentLoader* loader = mozJSComponentLoader::Get();
    90   JS::Rooted<JSObject*> targetObj(cx);
    91   nsresult rv = loader->FindTargetObject(cx, &targetObj);
    92   NS_ENSURE_SUCCESS(rv, rv);
    94   *_retval = InitAndSealPerfMeasurementClass(cx, targetObj);
    95   return NS_OK;
    96 }
    98 }
    99 }
   101 NS_DEFINE_NAMED_CID(JSPERF_CID);
   103 static const mozilla::Module::CIDEntry kPerfCIDs[] = {
   104   { &kJSPERF_CID, false, nullptr, mozilla::jsperf::ModuleConstructor },
   105   { nullptr }
   106 };
   108 static const mozilla::Module::ContractIDEntry kPerfContracts[] = {
   109   { JSPERF_CONTRACTID, &kJSPERF_CID },
   110   { nullptr }
   111 };
   113 static const mozilla::Module kPerfModule = {
   114   mozilla::Module::kVersion,
   115   kPerfCIDs,
   116   kPerfContracts
   117 };
   119 NSMODULE_DEFN(jsperf) = &kPerfModule;

mercurial