1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/perf/PerfMeasurement.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,119 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "PerfMeasurement.h" 1.10 +#include "jsperf.h" 1.11 +#include "mozilla/ModuleUtils.h" 1.12 +#include "nsMemory.h" 1.13 +#include "mozilla/Preferences.h" 1.14 +#include "mozJSComponentLoader.h" 1.15 +#include "nsZipArchive.h" 1.16 + 1.17 +#define JSPERF_CONTRACTID \ 1.18 + "@mozilla.org/jsperf;1" 1.19 + 1.20 +#define JSPERF_CID \ 1.21 +{ 0x421c38e6, 0xaee0, 0x4509, \ 1.22 + { 0xa0, 0x25, 0x13, 0x0f, 0x43, 0x78, 0x03, 0x5a } } 1.23 + 1.24 +namespace mozilla { 1.25 +namespace jsperf { 1.26 + 1.27 +NS_GENERIC_FACTORY_CONSTRUCTOR(Module) 1.28 + 1.29 +NS_IMPL_ISUPPORTS(Module, nsIXPCScriptable) 1.30 + 1.31 +Module::Module() 1.32 +{ 1.33 +} 1.34 + 1.35 +Module::~Module() 1.36 +{ 1.37 +} 1.38 + 1.39 +#define XPC_MAP_CLASSNAME Module 1.40 +#define XPC_MAP_QUOTED_CLASSNAME "Module" 1.41 +#define XPC_MAP_WANT_CALL 1.42 +#define XPC_MAP_FLAGS nsIXPCScriptable::WANT_CALL 1.43 +#include "xpc_map_end.h" 1.44 + 1.45 +static bool 1.46 +SealObjectAndPrototype(JSContext* cx, JS::Handle<JSObject *> parent, const char* name) 1.47 +{ 1.48 + JS::Rooted<JS::Value> prop(cx); 1.49 + if (!JS_GetProperty(cx, parent, name, &prop)) 1.50 + return false; 1.51 + 1.52 + if (prop.isUndefined()) { 1.53 + // Pretend we sealed the object. 1.54 + return true; 1.55 + } 1.56 + 1.57 + JS::Rooted<JSObject*> obj(cx, prop.toObjectOrNull()); 1.58 + if (!JS_GetProperty(cx, obj, "prototype", &prop)) 1.59 + return false; 1.60 + 1.61 + JS::Rooted<JSObject*> prototype(cx, prop.toObjectOrNull()); 1.62 + return JS_FreezeObject(cx, obj) && JS_FreezeObject(cx, prototype); 1.63 +} 1.64 + 1.65 +static bool 1.66 +InitAndSealPerfMeasurementClass(JSContext* cx, JS::Handle<JSObject*> global) 1.67 +{ 1.68 + // Init the PerfMeasurement class 1.69 + if (!JS::RegisterPerfMeasurement(cx, global)) 1.70 + return false; 1.71 + 1.72 + // Seal up Object, Function, and Array and their prototypes. (This single 1.73 + // object instance is shared amongst everyone who imports the jsperf module.) 1.74 + if (!SealObjectAndPrototype(cx, global, "Object") || 1.75 + !SealObjectAndPrototype(cx, global, "Function") || 1.76 + !SealObjectAndPrototype(cx, global, "Array")) 1.77 + return false; 1.78 + 1.79 + // Finally, seal the global object, for good measure. (But not recursively; 1.80 + // this breaks things.) 1.81 + return JS_FreezeObject(cx, global); 1.82 +} 1.83 + 1.84 +NS_IMETHODIMP 1.85 +Module::Call(nsIXPConnectWrappedNative* wrapper, 1.86 + JSContext* cx, 1.87 + JSObject* obj, 1.88 + const JS::CallArgs& args, 1.89 + bool* _retval) 1.90 +{ 1.91 + 1.92 + mozJSComponentLoader* loader = mozJSComponentLoader::Get(); 1.93 + JS::Rooted<JSObject*> targetObj(cx); 1.94 + nsresult rv = loader->FindTargetObject(cx, &targetObj); 1.95 + NS_ENSURE_SUCCESS(rv, rv); 1.96 + 1.97 + *_retval = InitAndSealPerfMeasurementClass(cx, targetObj); 1.98 + return NS_OK; 1.99 +} 1.100 + 1.101 +} 1.102 +} 1.103 + 1.104 +NS_DEFINE_NAMED_CID(JSPERF_CID); 1.105 + 1.106 +static const mozilla::Module::CIDEntry kPerfCIDs[] = { 1.107 + { &kJSPERF_CID, false, nullptr, mozilla::jsperf::ModuleConstructor }, 1.108 + { nullptr } 1.109 +}; 1.110 + 1.111 +static const mozilla::Module::ContractIDEntry kPerfContracts[] = { 1.112 + { JSPERF_CONTRACTID, &kJSPERF_CID }, 1.113 + { nullptr } 1.114 +}; 1.115 + 1.116 +static const mozilla::Module kPerfModule = { 1.117 + mozilla::Module::kVersion, 1.118 + kPerfCIDs, 1.119 + kPerfContracts 1.120 +}; 1.121 + 1.122 +NSMODULE_DEFN(jsperf) = &kPerfModule;