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