1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * The Mozilla Foundation <http://www.mozilla.org/>.
19 : * Portions created by the Initial Developer are Copyright (C) 2010
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Zack Weinberg <zweinberg@mozilla.com> (original author)
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "PerfMeasurement.h"
40 : #include "jsperf.h"
41 : #include "mozilla/ModuleUtils.h"
42 : #include "nsMemory.h"
43 :
44 : #define JSPERF_CONTRACTID \
45 : "@mozilla.org/jsperf;1"
46 :
47 : #define JSPERF_CID \
48 : { 0x421c38e6, 0xaee0, 0x4509, \
49 : { 0xa0, 0x25, 0x13, 0x0f, 0x43, 0x78, 0x03, 0x5a } }
50 :
51 : namespace mozilla {
52 : namespace jsperf {
53 :
54 0 : NS_GENERIC_FACTORY_CONSTRUCTOR(Module)
55 :
56 0 : NS_IMPL_ISUPPORTS1(Module, nsIXPCScriptable)
57 :
58 0 : Module::Module()
59 : {
60 0 : }
61 :
62 0 : Module::~Module()
63 : {
64 0 : }
65 :
66 : #define XPC_MAP_CLASSNAME Module
67 : #define XPC_MAP_QUOTED_CLASSNAME "Module"
68 : #define XPC_MAP_WANT_CALL
69 : #define XPC_MAP_FLAGS nsIXPCScriptable::WANT_CALL
70 : #include "xpc_map_end.h"
71 :
72 : static JSBool
73 0 : SealObjectAndPrototype(JSContext* cx, JSObject* parent, const char* name)
74 : {
75 : jsval prop;
76 0 : if (!JS_GetProperty(cx, parent, name, &prop))
77 0 : return false;
78 :
79 0 : JSObject* obj = JSVAL_TO_OBJECT(prop);
80 0 : if (!JS_GetProperty(cx, obj, "prototype", &prop))
81 0 : return false;
82 :
83 0 : JSObject* prototype = JSVAL_TO_OBJECT(prop);
84 0 : return JS_FreezeObject(cx, obj) && JS_FreezeObject(cx, prototype);
85 : }
86 :
87 : static JSBool
88 0 : InitAndSealPerfMeasurementClass(JSContext* cx, JSObject* global)
89 : {
90 : // Init the PerfMeasurement class
91 0 : if (!JS::RegisterPerfMeasurement(cx, global))
92 0 : return false;
93 :
94 : // Seal up Object, Function, and Array and their prototypes. (This single
95 : // object instance is shared amongst everyone who imports the jsperf module.)
96 0 : if (!SealObjectAndPrototype(cx, global, "Object") ||
97 0 : !SealObjectAndPrototype(cx, global, "Function") ||
98 0 : !SealObjectAndPrototype(cx, global, "Array"))
99 0 : return false;
100 :
101 : // Finally, seal the global object, for good measure. (But not recursively;
102 : // this breaks things.)
103 0 : return JS_FreezeObject(cx, global);
104 : }
105 :
106 : NS_IMETHODIMP
107 0 : Module::Call(nsIXPConnectWrappedNative* wrapper,
108 : JSContext* cx,
109 : JSObject* obj,
110 : PRUint32 argc,
111 : jsval* argv,
112 : jsval* vp,
113 : bool* _retval)
114 : {
115 0 : JSObject* global = JS_GetGlobalForScopeChain(cx);
116 0 : if (!global)
117 0 : return NS_ERROR_NOT_AVAILABLE;
118 :
119 0 : *_retval = InitAndSealPerfMeasurementClass(cx, global);
120 0 : return NS_OK;
121 : }
122 :
123 : }
124 : }
125 :
126 : NS_DEFINE_NAMED_CID(JSPERF_CID);
127 :
128 : static const mozilla::Module::CIDEntry kPerfCIDs[] = {
129 : { &kJSPERF_CID, false, NULL, mozilla::jsperf::ModuleConstructor },
130 : { NULL }
131 : };
132 :
133 : static const mozilla::Module::ContractIDEntry kPerfContracts[] = {
134 : { JSPERF_CONTRACTID, &kJSPERF_CID },
135 : { NULL }
136 : };
137 :
138 : static const mozilla::Module kPerfModule = {
139 : mozilla::Module::kVersion,
140 : kPerfCIDs,
141 : kPerfContracts
142 : };
143 :
144 : NSMODULE_DEFN(jsperf) = &kPerfModule;
|