2001-11-13 07:09:56 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2001-11-13 07:09:56 +03:00
|
|
|
|
|
|
|
#include "nsIAtom.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "nsIContent.h"
|
2004-09-10 19:29:19 +04:00
|
|
|
#include "nsIDocument.h"
|
2014-07-30 21:47:50 +04:00
|
|
|
#include "nsIGlobalObject.h"
|
2001-11-13 07:09:56 +03:00
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsXBLProtoImplMethod.h"
|
2013-01-17 06:50:25 +04:00
|
|
|
#include "nsJSUtils.h"
|
2004-09-12 05:41:34 +04:00
|
|
|
#include "nsContentUtils.h"
|
2005-01-05 04:02:13 +03:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2006-03-22 21:36:36 +03:00
|
|
|
#include "nsIXPConnect.h"
|
2013-02-08 18:24:22 +04:00
|
|
|
#include "xpcpublic.h"
|
2011-11-04 00:39:08 +04:00
|
|
|
#include "nsXBLPrototypeBinding.h"
|
2014-10-31 00:40:17 +03:00
|
|
|
#include "mozilla/dom/Element.h"
|
2014-07-30 21:47:50 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2001-11-13 07:09:56 +03:00
|
|
|
|
2013-02-26 23:04:13 +04:00
|
|
|
using namespace mozilla;
|
2014-09-29 17:34:21 +04:00
|
|
|
using namespace mozilla::dom;
|
2013-02-26 23:04:13 +04:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
nsXBLProtoImplMethod::nsXBLProtoImplMethod(const char16_t* aName) :
|
2013-06-18 14:00:38 +04:00
|
|
|
nsXBLProtoImplMember(aName),
|
|
|
|
mMethod()
|
2001-11-13 07:09:56 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsXBLProtoImplMethod);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsXBLProtoImplMethod::~nsXBLProtoImplMethod()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(nsXBLProtoImplMethod);
|
|
|
|
|
2008-02-12 19:02:41 +03:00
|
|
|
if (!IsCompiled()) {
|
|
|
|
delete GetUncompiledMethod();
|
2001-11-13 07:09:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-13 11:38:36 +04:00
|
|
|
void
|
2002-03-24 01:46:13 +03:00
|
|
|
nsXBLProtoImplMethod::AppendBodyText(const nsAString& aText)
|
2001-11-13 07:09:56 +03:00
|
|
|
{
|
2008-02-12 19:02:41 +03:00
|
|
|
NS_PRECONDITION(!IsCompiled(),
|
2005-02-06 23:34:15 +03:00
|
|
|
"Must not be compiled when accessing uncompiled method");
|
2008-02-12 19:02:41 +03:00
|
|
|
|
|
|
|
nsXBLUncompiledMethod* uncompiledMethod = GetUncompiledMethod();
|
|
|
|
if (!uncompiledMethod) {
|
|
|
|
uncompiledMethod = new nsXBLUncompiledMethod();
|
|
|
|
if (!uncompiledMethod)
|
2001-11-13 07:09:56 +03:00
|
|
|
return;
|
2008-02-12 19:02:41 +03:00
|
|
|
SetUncompiledMethod(uncompiledMethod);
|
2001-11-13 07:09:56 +03:00
|
|
|
}
|
|
|
|
|
2008-02-12 19:02:41 +03:00
|
|
|
uncompiledMethod->AppendBodyText(aText);
|
2001-11-13 07:09:56 +03:00
|
|
|
}
|
|
|
|
|
2014-02-13 11:38:36 +04:00
|
|
|
void
|
2002-03-24 01:46:13 +03:00
|
|
|
nsXBLProtoImplMethod::AddParameter(const nsAString& aText)
|
2001-11-13 07:09:56 +03:00
|
|
|
{
|
2008-02-12 19:02:41 +03:00
|
|
|
NS_PRECONDITION(!IsCompiled(),
|
2005-02-06 23:34:15 +03:00
|
|
|
"Must not be compiled when accessing uncompiled method");
|
2008-02-12 19:02:41 +03:00
|
|
|
|
2012-08-02 10:12:34 +04:00
|
|
|
if (aText.IsEmpty()) {
|
|
|
|
NS_WARNING("Empty name attribute in xbl:parameter!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-12 19:02:41 +03:00
|
|
|
nsXBLUncompiledMethod* uncompiledMethod = GetUncompiledMethod();
|
|
|
|
if (!uncompiledMethod) {
|
|
|
|
uncompiledMethod = new nsXBLUncompiledMethod();
|
|
|
|
if (!uncompiledMethod)
|
2001-11-13 07:09:56 +03:00
|
|
|
return;
|
2008-02-12 19:02:41 +03:00
|
|
|
SetUncompiledMethod(uncompiledMethod);
|
2001-11-13 07:09:56 +03:00
|
|
|
}
|
|
|
|
|
2008-02-12 19:02:41 +03:00
|
|
|
uncompiledMethod->AddParameter(aText);
|
2001-11-13 07:09:56 +03:00
|
|
|
}
|
|
|
|
|
2003-06-24 05:20:40 +04:00
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
nsXBLProtoImplMethod::SetLineNumber(uint32_t aLineNumber)
|
2003-06-24 05:20:40 +04:00
|
|
|
{
|
2008-02-12 19:02:41 +03:00
|
|
|
NS_PRECONDITION(!IsCompiled(),
|
2005-02-06 23:34:15 +03:00
|
|
|
"Must not be compiled when accessing uncompiled method");
|
2008-02-12 19:02:41 +03:00
|
|
|
|
|
|
|
nsXBLUncompiledMethod* uncompiledMethod = GetUncompiledMethod();
|
|
|
|
if (!uncompiledMethod) {
|
|
|
|
uncompiledMethod = new nsXBLUncompiledMethod();
|
|
|
|
if (!uncompiledMethod)
|
2003-06-24 05:20:40 +04:00
|
|
|
return;
|
2008-02-12 19:02:41 +03:00
|
|
|
SetUncompiledMethod(uncompiledMethod);
|
2003-06-24 05:20:40 +04:00
|
|
|
}
|
|
|
|
|
2008-02-12 19:02:41 +03:00
|
|
|
uncompiledMethod->SetLineNumber(aLineNumber);
|
2003-06-24 05:20:40 +04:00
|
|
|
}
|
|
|
|
|
2001-11-13 07:09:56 +03:00
|
|
|
nsresult
|
2013-02-08 18:24:21 +04:00
|
|
|
nsXBLProtoImplMethod::InstallMember(JSContext* aCx,
|
2013-04-05 17:21:02 +04:00
|
|
|
JS::Handle<JSObject*> aTargetClassObject)
|
2001-11-13 07:09:56 +03:00
|
|
|
{
|
2008-02-12 19:02:41 +03:00
|
|
|
NS_PRECONDITION(IsCompiled(),
|
2005-02-06 23:34:15 +03:00
|
|
|
"Should not be installing an uncompiled method");
|
2013-02-08 18:24:21 +04:00
|
|
|
MOZ_ASSERT(js::IsObjectInContextCompartment(aTargetClassObject, aCx));
|
2005-07-31 00:57:07 +04:00
|
|
|
|
2014-10-31 02:40:28 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> globalObject(aCx, JS_GetGlobalForObject(aCx, aTargetClassObject));
|
|
|
|
MOZ_ASSERT(xpc::IsInContentXBLScope(globalObject) ||
|
|
|
|
xpc::IsInAddonScope(globalObject) ||
|
|
|
|
globalObject == xpc::GetXBLScope(aCx, globalObject));
|
|
|
|
MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx) == globalObject);
|
|
|
|
}
|
|
|
|
#endif
|
2001-11-13 07:09:56 +03:00
|
|
|
|
2013-06-18 14:00:38 +04:00
|
|
|
JS::Rooted<JSObject*> jsMethodObject(aCx, GetCompiledMethod());
|
|
|
|
if (jsMethodObject) {
|
2001-11-13 07:09:56 +03:00
|
|
|
nsDependentString name(mName);
|
2010-09-23 04:34:20 +04:00
|
|
|
|
2014-10-31 02:40:28 +03:00
|
|
|
JS::Rooted<JSObject*> method(aCx, JS::CloneFunctionObject(aCx, jsMethodObject));
|
2013-11-01 18:31:57 +04:00
|
|
|
NS_ENSURE_TRUE(method, NS_ERROR_OUT_OF_MEMORY);
|
2013-04-05 17:21:02 +04:00
|
|
|
|
|
|
|
if (!::JS_DefineUCProperty(aCx, aTargetClassObject,
|
2014-07-22 08:43:21 +04:00
|
|
|
static_cast<const char16_t*>(mName),
|
2014-04-30 13:10:33 +04:00
|
|
|
name.Length(), method,
|
|
|
|
JSPROP_ENUMERATE)) {
|
2003-09-11 08:10:20 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2006-04-04 06:39:12 +04:00
|
|
|
}
|
2001-11-13 07:09:56 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-13 11:38:36 +04:00
|
|
|
nsresult
|
2014-09-29 17:34:21 +04:00
|
|
|
nsXBLProtoImplMethod::CompileMember(AutoJSAPI& jsapi, const nsCString& aClassStr,
|
2013-04-05 17:21:02 +04:00
|
|
|
JS::Handle<JSObject*> aClassObject)
|
2001-11-13 07:09:56 +03:00
|
|
|
{
|
2013-08-09 20:25:13 +04:00
|
|
|
AssertInCompilationScope();
|
2008-02-12 19:02:41 +03:00
|
|
|
NS_PRECONDITION(!IsCompiled(),
|
2005-02-06 23:34:15 +03:00
|
|
|
"Trying to compile an already-compiled method");
|
|
|
|
NS_PRECONDITION(aClassObject,
|
|
|
|
"Must have class object to compile");
|
2001-11-13 07:09:56 +03:00
|
|
|
|
2008-02-12 19:02:41 +03:00
|
|
|
nsXBLUncompiledMethod* uncompiledMethod = GetUncompiledMethod();
|
|
|
|
|
2003-07-13 11:19:28 +04:00
|
|
|
// No parameters or body was supplied, so don't install method.
|
2008-02-12 19:02:41 +03:00
|
|
|
if (!uncompiledMethod) {
|
|
|
|
// Early return after which we consider ourselves compiled.
|
2013-06-18 14:00:38 +04:00
|
|
|
SetCompiledMethod(nullptr);
|
2008-02-12 19:02:41 +03:00
|
|
|
|
2004-01-19 02:38:31 +03:00
|
|
|
return NS_OK;
|
2008-02-12 19:02:41 +03:00
|
|
|
}
|
2003-07-13 11:19:28 +04:00
|
|
|
|
2007-01-04 21:56:00 +03:00
|
|
|
// Don't install method if no name was supplied.
|
|
|
|
if (!mName) {
|
2008-02-12 19:02:41 +03:00
|
|
|
delete uncompiledMethod;
|
|
|
|
|
|
|
|
// Early return after which we consider ourselves compiled.
|
2013-06-18 14:00:38 +04:00
|
|
|
SetCompiledMethod(nullptr);
|
2008-02-12 19:02:41 +03:00
|
|
|
|
2003-09-11 08:10:20 +04:00
|
|
|
return NS_OK;
|
2004-09-10 19:29:19 +04:00
|
|
|
}
|
2003-09-11 08:10:20 +04:00
|
|
|
|
2001-11-13 07:09:56 +03:00
|
|
|
// We have a method.
|
|
|
|
// Allocate an array for our arguments.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t paramCount = uncompiledMethod->GetParameterCount();
|
2012-07-30 18:20:58 +04:00
|
|
|
char** args = nullptr;
|
2001-11-13 07:09:56 +03:00
|
|
|
if (paramCount > 0) {
|
|
|
|
args = new char*[paramCount];
|
|
|
|
if (!args)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2009-08-12 13:49:52 +04:00
|
|
|
// Add our parameters to our args array.
|
2014-02-13 11:38:36 +04:00
|
|
|
int32_t argPos = 0;
|
|
|
|
for (nsXBLParameter* curr = uncompiledMethod->mParameters;
|
|
|
|
curr;
|
2009-08-12 13:49:52 +04:00
|
|
|
curr = curr->mNext) {
|
|
|
|
args[argPos] = curr->mName;
|
|
|
|
argPos++;
|
|
|
|
}
|
2001-11-13 07:09:56 +03:00
|
|
|
}
|
|
|
|
|
2007-01-04 21:56:00 +03:00
|
|
|
// Get the body
|
|
|
|
nsDependentString body;
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t *bodyText = uncompiledMethod->mBodyText.GetText();
|
2007-01-04 21:56:00 +03:00
|
|
|
if (bodyText)
|
|
|
|
body.Rebind(bodyText);
|
|
|
|
|
2001-11-13 07:09:56 +03:00
|
|
|
// Now that we have a body and args, compile the function
|
|
|
|
// and then define it.
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_ConvertUTF16toUTF8 cname(mName);
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString functionUri(aClassStr);
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t hash = functionUri.RFindChar('#');
|
2003-09-11 08:10:20 +04:00
|
|
|
if (hash != kNotFound) {
|
|
|
|
functionUri.Truncate(hash);
|
|
|
|
}
|
2003-06-24 05:20:40 +04:00
|
|
|
|
2014-09-29 17:34:21 +04:00
|
|
|
JSContext *cx = jsapi.cx();
|
2013-01-17 06:50:25 +04:00
|
|
|
JSAutoCompartment ac(cx, aClassObject);
|
|
|
|
JS::CompileOptions options(cx);
|
|
|
|
options.setFileAndLine(functionUri.get(),
|
|
|
|
uncompiledMethod->mBodyText.GetLineNumber())
|
2014-10-31 02:40:29 +03:00
|
|
|
.setVersion(JSVERSION_LATEST);
|
2013-11-11 12:04:41 +04:00
|
|
|
JS::Rooted<JSObject*> methodObject(cx);
|
2014-10-25 08:50:29 +04:00
|
|
|
JS::AutoObjectVector emptyVector(cx);
|
|
|
|
nsresult rv = nsJSUtils::CompileFunction(jsapi, emptyVector, options, cname,
|
2013-01-17 06:50:25 +04:00
|
|
|
paramCount,
|
|
|
|
const_cast<const char**>(args),
|
2013-04-05 17:21:02 +04:00
|
|
|
body, methodObject.address());
|
2003-09-11 08:10:20 +04:00
|
|
|
|
|
|
|
// Destroy our uncompiled method and delete our arg list.
|
2008-02-12 19:02:41 +03:00
|
|
|
delete uncompiledMethod;
|
2003-09-11 08:10:20 +04:00
|
|
|
delete [] args;
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
SetUncompiledMethod(nullptr);
|
2003-09-11 08:10:20 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-06-18 14:00:38 +04:00
|
|
|
SetCompiledMethod(methodObject);
|
2003-09-11 08:10:20 +04:00
|
|
|
|
2008-02-12 19:02:41 +03:00
|
|
|
return NS_OK;
|
2001-11-13 07:09:56 +03:00
|
|
|
}
|
2004-09-10 19:29:19 +04:00
|
|
|
|
2007-05-24 18:10:02 +04:00
|
|
|
void
|
2013-05-27 15:50:49 +04:00
|
|
|
nsXBLProtoImplMethod::Trace(const TraceCallbacks& aCallbacks, void *aClosure)
|
2007-05-24 18:10:02 +04:00
|
|
|
{
|
2013-11-22 01:19:46 +04:00
|
|
|
if (IsCompiled() && GetCompiledMethodPreserveColor()) {
|
2013-06-18 14:00:38 +04:00
|
|
|
aCallbacks.Trace(&mMethod.AsHeapObject(), "mMethod", aClosure);
|
2007-10-29 16:45:07 +03:00
|
|
|
}
|
2007-05-24 18:10:02 +04:00
|
|
|
}
|
|
|
|
|
2011-11-04 00:39:08 +04:00
|
|
|
nsresult
|
2013-08-09 20:25:13 +04:00
|
|
|
nsXBLProtoImplMethod::Read(nsIObjectInputStream* aStream)
|
2011-11-04 00:39:08 +04:00
|
|
|
{
|
2013-08-09 20:25:13 +04:00
|
|
|
AssertInCompilationScope();
|
2013-07-23 13:58:28 +04:00
|
|
|
MOZ_ASSERT(!IsCompiled() && !GetUncompiledMethod());
|
|
|
|
|
2013-08-09 20:25:13 +04:00
|
|
|
AutoJSContext cx;
|
2013-08-09 20:25:13 +04:00
|
|
|
JS::Rooted<JSObject*> methodObject(cx);
|
|
|
|
nsresult rv = XBL_DeserializeFunction(aStream, &methodObject);
|
2011-11-04 00:39:08 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
SetUncompiledMethod(nullptr);
|
2011-11-04 00:39:08 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-06-18 14:00:38 +04:00
|
|
|
SetCompiledMethod(methodObject);
|
2011-11-04 00:39:08 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-08-09 20:25:13 +04:00
|
|
|
nsXBLProtoImplMethod::Write(nsIObjectOutputStream* aStream)
|
2011-11-04 00:39:08 +04:00
|
|
|
{
|
2013-08-09 20:25:13 +04:00
|
|
|
AssertInCompilationScope();
|
2013-06-18 14:00:38 +04:00
|
|
|
MOZ_ASSERT(IsCompiled());
|
2013-11-22 01:19:46 +04:00
|
|
|
if (GetCompiledMethodPreserveColor()) {
|
2012-02-13 23:24:28 +04:00
|
|
|
nsresult rv = aStream->Write8(XBLBinding_Serialize_Method);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-11-04 00:39:08 +04:00
|
|
|
|
2012-02-13 23:24:28 +04:00
|
|
|
rv = aStream->WriteWStringZ(mName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-11-04 00:39:08 +04:00
|
|
|
|
2013-07-23 13:58:28 +04:00
|
|
|
// Calling fromMarkedLocation() is safe because mMethod is traced by the
|
|
|
|
// Trace() method above, and because its value is never changed after it has
|
|
|
|
// been set to a compiled method.
|
2013-07-23 13:58:27 +04:00
|
|
|
JS::Handle<JSObject*> method =
|
|
|
|
JS::Handle<JSObject*>::fromMarkedLocation(mMethod.AsHeapObject().address());
|
2013-08-09 20:25:13 +04:00
|
|
|
return XBL_SerializeFunction(aStream, method);
|
2012-02-13 23:24:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2011-11-04 00:39:08 +04:00
|
|
|
}
|
|
|
|
|
2004-09-10 19:29:19 +04:00
|
|
|
nsresult
|
2014-06-24 03:33:37 +04:00
|
|
|
nsXBLProtoImplAnonymousMethod::Execute(nsIContent* aBoundElement, JSAddonId* aAddonId)
|
2004-09-10 19:29:19 +04:00
|
|
|
{
|
2014-10-31 00:40:17 +03:00
|
|
|
MOZ_ASSERT(aBoundElement->IsElement());
|
2008-02-12 19:02:41 +03:00
|
|
|
NS_PRECONDITION(IsCompiled(), "Can't execute uncompiled method");
|
2013-07-23 13:58:28 +04:00
|
|
|
|
2013-06-18 14:00:38 +04:00
|
|
|
if (!GetCompiledMethod()) {
|
2004-09-10 19:29:19 +04:00
|
|
|
// Nothing to do here
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the script context the same way
|
|
|
|
// nsXBLProtoImpl::InstallImplementation does.
|
2011-10-18 14:53:36 +04:00
|
|
|
nsIDocument* document = aBoundElement->OwnerDoc();
|
2004-09-10 19:29:19 +04:00
|
|
|
|
2014-07-30 21:47:50 +04:00
|
|
|
nsCOMPtr<nsIGlobalObject> global =
|
|
|
|
do_QueryInterface(document->GetInnerWindow());
|
2004-09-10 19:29:19 +04:00
|
|
|
if (!global) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-30 21:47:50 +04:00
|
|
|
// We are going to run script via JS::Call, so we need a script entry point,
|
|
|
|
// but as this is XBL related it does not appear in the HTML spec.
|
|
|
|
dom::AutoEntryScript aes(global);
|
|
|
|
JSContext* cx = aes.cx();
|
2004-09-10 19:29:19 +04:00
|
|
|
|
2013-04-05 17:21:02 +04:00
|
|
|
JS::Rooted<JSObject*> globalObject(cx, global->GetGlobalJSObject());
|
2004-09-10 19:29:19 +04:00
|
|
|
|
2014-06-24 03:33:37 +04:00
|
|
|
JS::Rooted<JSObject*> scopeObject(cx, xpc::GetScopeForXBLExecution(cx, globalObject, aAddonId));
|
2013-04-05 23:04:09 +04:00
|
|
|
NS_ENSURE_TRUE(scopeObject, NS_ERROR_OUT_OF_MEMORY);
|
2004-09-10 19:29:19 +04:00
|
|
|
|
2013-02-08 18:24:22 +04:00
|
|
|
JSAutoCompartment ac(cx, scopeObject);
|
2014-10-31 00:40:17 +03:00
|
|
|
JS::AutoObjectVector scopeChain(cx);
|
|
|
|
if (!nsJSUtils::GetScopeChainForElement(cx, aBoundElement->AsElement(),
|
|
|
|
scopeChain)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(scopeChain.length() != 0);
|
2006-06-13 02:39:55 +04:00
|
|
|
|
2014-10-31 00:40:17 +03:00
|
|
|
// Clone the function object, using our scope chain (for backwards
|
|
|
|
// compat to the days when this was an event handler).
|
2014-01-17 22:09:38 +04:00
|
|
|
JS::Rooted<JSObject*> jsMethodObject(cx, GetCompiledMethod());
|
2014-10-31 00:40:17 +03:00
|
|
|
JS::Rooted<JSObject*> method(cx, JS::CloneFunctionObject(cx, jsMethodObject,
|
|
|
|
scopeChain));
|
2006-06-13 02:39:55 +04:00
|
|
|
if (!method)
|
2004-09-12 05:41:34 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2004-09-10 19:29:19 +04:00
|
|
|
// Now call the method
|
2004-11-05 18:39:58 +03:00
|
|
|
|
2013-11-13 04:43:32 +04:00
|
|
|
// Check whether script is enabled.
|
|
|
|
bool scriptAllowed = nsContentUtils::GetSecurityManager()->
|
|
|
|
ScriptAllowed(js::GetGlobalForObjectCrossCompartment(method));
|
2005-01-05 04:02:13 +03:00
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
bool ok = true;
|
2013-11-13 04:43:32 +04:00
|
|
|
if (scriptAllowed) {
|
2013-04-05 17:21:02 +04:00
|
|
|
JS::Rooted<JS::Value> retval(cx);
|
2014-02-13 19:33:04 +04:00
|
|
|
JS::Rooted<JS::Value> methodVal(cx, JS::ObjectValue(*method));
|
2014-10-31 00:40:17 +03:00
|
|
|
ok = ::JS::Call(cx, scopeChain[0], methodVal, JS::HandleValueArray::empty(), &retval);
|
2005-01-05 04:02:13 +03:00
|
|
|
}
|
2004-11-05 18:39:58 +03:00
|
|
|
|
|
|
|
if (!ok) {
|
2009-05-20 06:11:01 +04:00
|
|
|
// 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.
|
2013-07-17 22:53:52 +04:00
|
|
|
nsJSUtils::ReportPendingException(cx);
|
2004-09-10 19:29:19 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-11-04 00:39:08 +04:00
|
|
|
|
|
|
|
nsresult
|
2013-08-09 20:25:13 +04:00
|
|
|
nsXBLProtoImplAnonymousMethod::Write(nsIObjectOutputStream* aStream,
|
2011-11-04 00:39:08 +04:00
|
|
|
XBLBindingSerializeDetails aType)
|
|
|
|
{
|
2013-08-09 20:25:13 +04:00
|
|
|
AssertInCompilationScope();
|
2013-06-18 14:00:38 +04:00
|
|
|
MOZ_ASSERT(IsCompiled());
|
2013-11-22 01:19:46 +04:00
|
|
|
if (GetCompiledMethodPreserveColor()) {
|
2011-11-04 00:39:08 +04:00
|
|
|
nsresult rv = aStream->Write8(aType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-08-22 03:45:52 +04:00
|
|
|
rv = aStream->WriteWStringZ(mName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-07-23 13:58:28 +04:00
|
|
|
// Calling fromMarkedLocation() is safe because mMethod is traced by the
|
|
|
|
// Trace() method above, and because its value is never changed after it has
|
|
|
|
// been set to a compiled method.
|
2013-07-23 13:58:27 +04:00
|
|
|
JS::Handle<JSObject*> method =
|
|
|
|
JS::Handle<JSObject*>::fromMarkedLocation(mMethod.AsHeapObject().address());
|
2013-08-09 20:25:13 +04:00
|
|
|
rv = XBL_SerializeFunction(aStream, method);
|
2011-11-04 00:39:08 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|