Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[987e5d]: content / xbl / src / nsXBLProtoImplMethod.cpp Maximize Restore History

Download this file

nsXBLProtoImplMethod.cpp    376 lines (310 with data), 11.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsIAtom.h"
#include "nsString.h"
#include "jsapi.h"
#include "nsIContent.h"
#include "nsIDocument.h"
#include "nsIScriptGlobalObject.h"
#include "nsUnicharUtils.h"
#include "nsReadableUtils.h"
#include "nsXBLProtoImplMethod.h"
#include "nsIScriptContext.h"
#include "nsJSUtils.h"
#include "nsContentUtils.h"
#include "nsCxPusher.h"
#include "nsIScriptSecurityManager.h"
#include "nsIXPConnect.h"
#include "xpcpublic.h"
#include "nsXBLPrototypeBinding.h"
using namespace mozilla;
nsXBLProtoImplMethod::nsXBLProtoImplMethod(const PRUnichar* aName) :
nsXBLProtoImplMember(aName),
mMethod()
{
MOZ_COUNT_CTOR(nsXBLProtoImplMethod);
}
nsXBLProtoImplMethod::~nsXBLProtoImplMethod()
{
MOZ_COUNT_DTOR(nsXBLProtoImplMethod);
if (!IsCompiled()) {
delete GetUncompiledMethod();
}
}
void
nsXBLProtoImplMethod::AppendBodyText(const nsAString& aText)
{
NS_PRECONDITION(!IsCompiled(),
"Must not be compiled when accessing uncompiled method");
nsXBLUncompiledMethod* uncompiledMethod = GetUncompiledMethod();
if (!uncompiledMethod) {
uncompiledMethod = new nsXBLUncompiledMethod();
if (!uncompiledMethod)
return;
SetUncompiledMethod(uncompiledMethod);
}
uncompiledMethod->AppendBodyText(aText);
}
void
nsXBLProtoImplMethod::AddParameter(const nsAString& aText)
{
NS_PRECONDITION(!IsCompiled(),
"Must not be compiled when accessing uncompiled method");
if (aText.IsEmpty()) {
NS_WARNING("Empty name attribute in xbl:parameter!");
return;
}
nsXBLUncompiledMethod* uncompiledMethod = GetUncompiledMethod();
if (!uncompiledMethod) {
uncompiledMethod = new nsXBLUncompiledMethod();
if (!uncompiledMethod)
return;
SetUncompiledMethod(uncompiledMethod);
}
uncompiledMethod->AddParameter(aText);
}
void
nsXBLProtoImplMethod::SetLineNumber(uint32_t aLineNumber)
{
NS_PRECONDITION(!IsCompiled(),
"Must not be compiled when accessing uncompiled method");
nsXBLUncompiledMethod* uncompiledMethod = GetUncompiledMethod();
if (!uncompiledMethod) {
uncompiledMethod = new nsXBLUncompiledMethod();
if (!uncompiledMethod)
return;
SetUncompiledMethod(uncompiledMethod);
}
uncompiledMethod->SetLineNumber(aLineNumber);
}
nsresult
nsXBLProtoImplMethod::InstallMember(JSContext* aCx,
JS::Handle<JSObject*> aTargetClassObject)
{
NS_PRECONDITION(IsCompiled(),
"Should not be installing an uncompiled method");
MOZ_ASSERT(js::IsObjectInContextCompartment(aTargetClassObject, aCx));
JS::Rooted<JSObject*> globalObject(aCx, JS_GetGlobalForObject(aCx, aTargetClassObject));
JS::Rooted<JSObject*> scopeObject(aCx, xpc::GetXBLScope(aCx, globalObject));
NS_ENSURE_TRUE(scopeObject, NS_ERROR_OUT_OF_MEMORY);
// now we want to reevaluate our property using aContext and the script object for this window...
JS::Rooted<JSObject*> jsMethodObject(aCx, GetCompiledMethod());
if (jsMethodObject) {
nsDependentString name(mName);
// First, make the function in the compartment of the scope object.
JSAutoCompartment ac(aCx, scopeObject);
JS::Rooted<JSObject*> method(aCx, ::JS_CloneFunctionObject(aCx, jsMethodObject, scopeObject));
if (!method) {
return NS_ERROR_OUT_OF_MEMORY;
}
// Then, enter the content compartment, wrap the method pointer, and define
// the wrapped version on the class object.
JSAutoCompartment ac2(aCx, aTargetClassObject);
if (!JS_WrapObject(aCx, method.address()))
return NS_ERROR_OUT_OF_MEMORY;
JS::Rooted<JS::Value> value(aCx, JS::ObjectValue(*method));
if (!::JS_DefineUCProperty(aCx, aTargetClassObject,
static_cast<const jschar*>(mName),
name.Length(), value,
nullptr, nullptr, JSPROP_ENUMERATE)) {
return NS_ERROR_OUT_OF_MEMORY;
}
}
return NS_OK;
}
nsresult
nsXBLProtoImplMethod::CompileMember(nsIScriptContext* aContext, const nsCString& aClassStr,
JS::Handle<JSObject*> aClassObject)
{
NS_PRECONDITION(!IsCompiled(),
"Trying to compile an already-compiled method");
NS_PRECONDITION(aClassObject,
"Must have class object to compile");
nsXBLUncompiledMethod* uncompiledMethod = GetUncompiledMethod();
// No parameters or body was supplied, so don't install method.
if (!uncompiledMethod) {
// Early return after which we consider ourselves compiled.
SetCompiledMethod(nullptr);
return NS_OK;
}
// Don't install method if no name was supplied.
if (!mName) {
delete uncompiledMethod;
// Early return after which we consider ourselves compiled.
SetCompiledMethod(nullptr);
return NS_OK;
}
// We have a method.
// Allocate an array for our arguments.
int32_t paramCount = uncompiledMethod->GetParameterCount();
char** args = nullptr;
if (paramCount > 0) {
args = new char*[paramCount];
if (!args)
return NS_ERROR_OUT_OF_MEMORY;
// Add our parameters to our args array.
int32_t argPos = 0;
for (nsXBLParameter* curr = uncompiledMethod->mParameters;
curr;
curr = curr->mNext) {
args[argPos] = curr->mName;
argPos++;
}
}
// Get the body
nsDependentString body;
PRUnichar *bodyText = uncompiledMethod->mBodyText.GetText();
if (bodyText)
body.Rebind(bodyText);
// Now that we have a body and args, compile the function
// and then define it.
NS_ConvertUTF16toUTF8 cname(mName);
nsAutoCString functionUri(aClassStr);
int32_t hash = functionUri.RFindChar('#');
if (hash != kNotFound) {
functionUri.Truncate(hash);
}
AutoPushJSContext cx(aContext->GetNativeContext());
JSAutoCompartment ac(cx, aClassObject);
JS::CompileOptions options(cx);
options.setFileAndLine(functionUri.get(),
uncompiledMethod->mBodyText.GetLineNumber())
.setVersion(JSVERSION_LATEST);
JS::RootedObject rootedNull(cx, nullptr); // See bug 781070.
JS::RootedObject methodObject(cx);
nsresult rv = nsJSUtils::CompileFunction(cx, rootedNull, options, cname,
paramCount,
const_cast<const char**>(args),
body, methodObject.address());
// Destroy our uncompiled method and delete our arg list.
delete uncompiledMethod;
delete [] args;
if (NS_FAILED(rv)) {
SetUncompiledMethod(nullptr);
return rv;
}
SetCompiledMethod(methodObject);
return NS_OK;
}
void
nsXBLProtoImplMethod::Trace(const TraceCallbacks& aCallbacks, void *aClosure)
{
if (IsCompiled() && GetCompiledMethod()) {
aCallbacks.Trace(&mMethod.AsHeapObject(), "mMethod", aClosure);
}
}
nsresult
nsXBLProtoImplMethod::Read(nsIScriptContext* aContext,
nsIObjectInputStream* aStream)
{
JS::Rooted<JSObject*> methodObject(aContext->GetNativeContext());
nsresult rv = XBL_DeserializeFunction(aContext, aStream, &methodObject);
if (NS_FAILED(rv)) {
SetUncompiledMethod(nullptr);
return rv;
}
SetCompiledMethod(methodObject);
return NS_OK;
}
nsresult
nsXBLProtoImplMethod::Write(nsIScriptContext* aContext,
nsIObjectOutputStream* aStream)
{
MOZ_ASSERT(IsCompiled());
if (GetCompiledMethod()) {
nsresult rv = aStream->Write8(XBLBinding_Serialize_Method);
NS_ENSURE_SUCCESS(rv, rv);
rv = aStream->WriteWStringZ(mName);
NS_ENSURE_SUCCESS(rv, rv);
return XBL_SerializeFunction(aContext, aStream, mMethod.AsHeapObject());
}
return NS_OK;
}
nsresult
nsXBLProtoImplAnonymousMethod::Execute(nsIContent* aBoundElement)
{
NS_PRECONDITION(IsCompiled(), "Can't execute uncompiled method");
if (!GetCompiledMethod()) {
// Nothing to do here
return NS_OK;
}
// Get the script context the same way
// nsXBLProtoImpl::InstallImplementation does.
nsIDocument* document = aBoundElement->OwnerDoc();
nsCOMPtr<nsIScriptGlobalObject> global =
do_QueryInterface(document->GetWindow());
if (!global) {
return NS_OK;
}
nsCOMPtr<nsIScriptContext> context = global->GetContext();
if (!context) {
return NS_OK;
}
nsAutoMicroTask mt;
AutoPushJSContext cx(context->GetNativeContext());
JS::Rooted<JSObject*> globalObject(cx, global->GetGlobalJSObject());
nsCOMPtr<nsIXPConnectJSObjectHolder> wrapper;
JS::Rooted<JS::Value> v(cx);
nsresult rv =
nsContentUtils::WrapNative(cx, globalObject, aBoundElement, v.address(),
getter_AddRefs(wrapper));
NS_ENSURE_SUCCESS(rv, rv);
// Use nsCxPusher to make sure we call ScriptEvaluated when we're done.
//
// Make sure to do this before entering the compartment, since pushing Push()
// may call JS_SaveFrameChain(), which puts us back in an unentered state.
nsCxPusher pusher;
if (!pusher.Push(aBoundElement))
return NS_ERROR_UNEXPECTED;
MOZ_ASSERT(cx == nsContentUtils::GetCurrentJSContext());
JS::Rooted<JSObject*> thisObject(cx, &v.toObject());
JS::Rooted<JSObject*> scopeObject(cx, xpc::GetXBLScope(cx, globalObject));
NS_ENSURE_TRUE(scopeObject, NS_ERROR_OUT_OF_MEMORY);
JSAutoCompartment ac(cx, scopeObject);
if (!JS_WrapObject(cx, thisObject.address()))
return NS_ERROR_OUT_OF_MEMORY;
// Clone the function object, using thisObject as the parent so "this" is in
// the scope chain of the resulting function (for backwards compat to the
// days when this was an event handler).
JS::Rooted<JSObject*> method(cx, ::JS_CloneFunctionObject(cx, GetCompiledMethod(), thisObject));
if (!method)
return NS_ERROR_OUT_OF_MEMORY;
// Now call the method
// Check whether it's OK to call the method.
rv = nsContentUtils::GetSecurityManager()->CheckFunctionAccess(cx, method,
thisObject);
JSBool ok = JS_TRUE;
if (NS_SUCCEEDED(rv)) {
JS::Rooted<JS::Value> retval(cx);
ok = ::JS_CallFunctionValue(cx, thisObject, OBJECT_TO_JSVAL(method),
0 /* argc */, nullptr /* argv */, retval.address());
}
if (!ok) {
// If a constructor or destructor threw an exception, it doesn't stop
// anything else. We just report it. Note that we need to set aside the
// frame chain here, since the constructor invocation is not related to
// whatever is on the stack right now, really.
JSBool saved = JS_SaveFrameChain(cx);
JS_ReportPendingException(cx);
if (saved)
JS_RestoreFrameChain(cx);
return NS_ERROR_FAILURE;
}
return NS_OK;
}
nsresult
nsXBLProtoImplAnonymousMethod::Write(nsIScriptContext* aContext,
nsIObjectOutputStream* aStream,
XBLBindingSerializeDetails aType)
{
MOZ_ASSERT(IsCompiled());
if (GetCompiledMethod()) {
nsresult rv = aStream->Write8(aType);
NS_ENSURE_SUCCESS(rv, rv);
rv = XBL_SerializeFunction(aContext, aStream, mMethod.AsHeapObject());
NS_ENSURE_SUCCESS(rv, rv);
}
return NS_OK;
}