|
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/. */ |
|
5 |
|
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" |
|
13 |
|
14 #define JSPERF_CONTRACTID \ |
|
15 "@mozilla.org/jsperf;1" |
|
16 |
|
17 #define JSPERF_CID \ |
|
18 { 0x421c38e6, 0xaee0, 0x4509, \ |
|
19 { 0xa0, 0x25, 0x13, 0x0f, 0x43, 0x78, 0x03, 0x5a } } |
|
20 |
|
21 namespace mozilla { |
|
22 namespace jsperf { |
|
23 |
|
24 NS_GENERIC_FACTORY_CONSTRUCTOR(Module) |
|
25 |
|
26 NS_IMPL_ISUPPORTS(Module, nsIXPCScriptable) |
|
27 |
|
28 Module::Module() |
|
29 { |
|
30 } |
|
31 |
|
32 Module::~Module() |
|
33 { |
|
34 } |
|
35 |
|
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" |
|
41 |
|
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; |
|
48 |
|
49 if (prop.isUndefined()) { |
|
50 // Pretend we sealed the object. |
|
51 return true; |
|
52 } |
|
53 |
|
54 JS::Rooted<JSObject*> obj(cx, prop.toObjectOrNull()); |
|
55 if (!JS_GetProperty(cx, obj, "prototype", &prop)) |
|
56 return false; |
|
57 |
|
58 JS::Rooted<JSObject*> prototype(cx, prop.toObjectOrNull()); |
|
59 return JS_FreezeObject(cx, obj) && JS_FreezeObject(cx, prototype); |
|
60 } |
|
61 |
|
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; |
|
68 |
|
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; |
|
75 |
|
76 // Finally, seal the global object, for good measure. (But not recursively; |
|
77 // this breaks things.) |
|
78 return JS_FreezeObject(cx, global); |
|
79 } |
|
80 |
|
81 NS_IMETHODIMP |
|
82 Module::Call(nsIXPConnectWrappedNative* wrapper, |
|
83 JSContext* cx, |
|
84 JSObject* obj, |
|
85 const JS::CallArgs& args, |
|
86 bool* _retval) |
|
87 { |
|
88 |
|
89 mozJSComponentLoader* loader = mozJSComponentLoader::Get(); |
|
90 JS::Rooted<JSObject*> targetObj(cx); |
|
91 nsresult rv = loader->FindTargetObject(cx, &targetObj); |
|
92 NS_ENSURE_SUCCESS(rv, rv); |
|
93 |
|
94 *_retval = InitAndSealPerfMeasurementClass(cx, targetObj); |
|
95 return NS_OK; |
|
96 } |
|
97 |
|
98 } |
|
99 } |
|
100 |
|
101 NS_DEFINE_NAMED_CID(JSPERF_CID); |
|
102 |
|
103 static const mozilla::Module::CIDEntry kPerfCIDs[] = { |
|
104 { &kJSPERF_CID, false, nullptr, mozilla::jsperf::ModuleConstructor }, |
|
105 { nullptr } |
|
106 }; |
|
107 |
|
108 static const mozilla::Module::ContractIDEntry kPerfContracts[] = { |
|
109 { JSPERF_CONTRACTID, &kJSPERF_CID }, |
|
110 { nullptr } |
|
111 }; |
|
112 |
|
113 static const mozilla::Module kPerfModule = { |
|
114 mozilla::Module::kVersion, |
|
115 kPerfCIDs, |
|
116 kPerfContracts |
|
117 }; |
|
118 |
|
119 NSMODULE_DEFN(jsperf) = &kPerfModule; |