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