michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "PerfMeasurement.h" michael@0: #include "jsperf.h" michael@0: #include "mozilla/ModuleUtils.h" michael@0: #include "nsMemory.h" michael@0: #include "mozilla/Preferences.h" michael@0: #include "mozJSComponentLoader.h" michael@0: #include "nsZipArchive.h" michael@0: michael@0: #define JSPERF_CONTRACTID \ michael@0: "@mozilla.org/jsperf;1" michael@0: michael@0: #define JSPERF_CID \ michael@0: { 0x421c38e6, 0xaee0, 0x4509, \ michael@0: { 0xa0, 0x25, 0x13, 0x0f, 0x43, 0x78, 0x03, 0x5a } } michael@0: michael@0: namespace mozilla { michael@0: namespace jsperf { michael@0: michael@0: NS_GENERIC_FACTORY_CONSTRUCTOR(Module) michael@0: michael@0: NS_IMPL_ISUPPORTS(Module, nsIXPCScriptable) michael@0: michael@0: Module::Module() michael@0: { michael@0: } michael@0: michael@0: Module::~Module() michael@0: { michael@0: } michael@0: michael@0: #define XPC_MAP_CLASSNAME Module michael@0: #define XPC_MAP_QUOTED_CLASSNAME "Module" michael@0: #define XPC_MAP_WANT_CALL michael@0: #define XPC_MAP_FLAGS nsIXPCScriptable::WANT_CALL michael@0: #include "xpc_map_end.h" michael@0: michael@0: static bool michael@0: SealObjectAndPrototype(JSContext* cx, JS::Handle parent, const char* name) michael@0: { michael@0: JS::Rooted prop(cx); michael@0: if (!JS_GetProperty(cx, parent, name, &prop)) michael@0: return false; michael@0: michael@0: if (prop.isUndefined()) { michael@0: // Pretend we sealed the object. michael@0: return true; michael@0: } michael@0: michael@0: JS::Rooted obj(cx, prop.toObjectOrNull()); michael@0: if (!JS_GetProperty(cx, obj, "prototype", &prop)) michael@0: return false; michael@0: michael@0: JS::Rooted prototype(cx, prop.toObjectOrNull()); michael@0: return JS_FreezeObject(cx, obj) && JS_FreezeObject(cx, prototype); michael@0: } michael@0: michael@0: static bool michael@0: InitAndSealPerfMeasurementClass(JSContext* cx, JS::Handle global) michael@0: { michael@0: // Init the PerfMeasurement class michael@0: if (!JS::RegisterPerfMeasurement(cx, global)) michael@0: return false; michael@0: michael@0: // Seal up Object, Function, and Array and their prototypes. (This single michael@0: // object instance is shared amongst everyone who imports the jsperf module.) michael@0: if (!SealObjectAndPrototype(cx, global, "Object") || michael@0: !SealObjectAndPrototype(cx, global, "Function") || michael@0: !SealObjectAndPrototype(cx, global, "Array")) michael@0: return false; michael@0: michael@0: // Finally, seal the global object, for good measure. (But not recursively; michael@0: // this breaks things.) michael@0: return JS_FreezeObject(cx, global); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: Module::Call(nsIXPConnectWrappedNative* wrapper, michael@0: JSContext* cx, michael@0: JSObject* obj, michael@0: const JS::CallArgs& args, michael@0: bool* _retval) michael@0: { michael@0: michael@0: mozJSComponentLoader* loader = mozJSComponentLoader::Get(); michael@0: JS::Rooted targetObj(cx); michael@0: nsresult rv = loader->FindTargetObject(cx, &targetObj); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: *_retval = InitAndSealPerfMeasurementClass(cx, targetObj); michael@0: return NS_OK; michael@0: } michael@0: michael@0: } michael@0: } michael@0: michael@0: NS_DEFINE_NAMED_CID(JSPERF_CID); michael@0: michael@0: static const mozilla::Module::CIDEntry kPerfCIDs[] = { michael@0: { &kJSPERF_CID, false, nullptr, mozilla::jsperf::ModuleConstructor }, michael@0: { nullptr } michael@0: }; michael@0: michael@0: static const mozilla::Module::ContractIDEntry kPerfContracts[] = { michael@0: { JSPERF_CONTRACTID, &kJSPERF_CID }, michael@0: { nullptr } michael@0: }; michael@0: michael@0: static const mozilla::Module kPerfModule = { michael@0: mozilla::Module::kVersion, michael@0: kPerfCIDs, michael@0: kPerfContracts michael@0: }; michael@0: michael@0: NSMODULE_DEFN(jsperf) = &kPerfModule;