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"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
2001-11-13 07:09:56 +03:00
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsXBLProtoImplMethod.h"
|
|
|
|
#include "nsIScriptContext.h"
|
2013-01-17 06:50:25 +04:00
|
|
|
#include "nsJSUtils.h"
|
2004-09-12 05:41:34 +04:00
|
|
|
#include "nsContentUtils.h"
|
2013-05-22 20:05:26 +04:00
|
|
|
#include "nsCxPusher.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"
|
2001-11-13 07:09:56 +03:00
|
|
|
|
2013-02-26 23:04:13 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-04-05 17:21:02 +04:00
|
|
|
JS::Rooted<JSObject*> globalObject(aCx, JS_GetGlobalForObject(aCx, aTargetClassObject));
|
2013-11-01 18:31:57 +04:00
|
|
|
MOZ_ASSERT(xpc::IsInXBLScope(globalObject) ||
|
|
|
|
globalObject == xpc::GetXBLScope(aCx, globalObject));
|
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
|
|
|
|
2013-11-01 18:31:57 +04:00
|
|
|
JS::Rooted<JSObject*> method(aCx, JS_CloneFunctionObject(aCx, jsMethodObject, globalObject));
|
|
|
|
NS_ENSURE_TRUE(method, NS_ERROR_OUT_OF_MEMORY);
|
2013-04-05 17:21:02 +04:00
|
|
|
|
|
|
|
JS::Rooted<JS::Value> value(aCx, JS::ObjectValue(*method));
|
|
|
|
if (!::JS_DefineUCProperty(aCx, aTargetClassObject,
|
2012-05-05 13:00:06 +04:00
|
|
|
static_cast<const jschar*>(mName),
|
2013-04-05 17:21:02 +04:00
|
|
|
name.Length(), value,
|
2013-04-03 05:14:24 +04:00
|
|
|
nullptr, nullptr, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-08-09 20:25:13 +04:00
|
|
|
nsXBLProtoImplMethod::CompileMember(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.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t argPos = 0;
|
2009-08-12 13:49:52 +04:00
|
|
|
for (nsXBLParameter* curr = uncompiledMethod->mParameters;
|
|
|
|
curr;
|
|
|
|
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
|
|
|
|
2013-08-09 20:25:13 +04:00
|
|
|
AutoJSContext cx;
|
2013-01-17 06:50:25 +04:00
|
|
|
JSAutoCompartment ac(cx, aClassObject);
|
|
|
|
JS::CompileOptions options(cx);
|
|
|
|
options.setFileAndLine(functionUri.get(),
|
|
|
|
uncompiledMethod->mBodyText.GetLineNumber())
|
2013-05-21 08:34:17 +04:00
|
|
|
.setVersion(JSVERSION_LATEST);
|
2013-11-11 12:04:41 +04:00
|
|
|
JS::Rooted<JSObject*> methodObject(cx);
|
|
|
|
nsresult rv = nsJSUtils::CompileFunction(cx, JS::NullPtr(), 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
|
|
|
|
nsXBLProtoImplAnonymousMethod::Execute(nsIContent* aBoundElement)
|
|
|
|
{
|
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
|
|
|
|
2013-06-21 18:25:20 +04:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> global =
|
|
|
|
do_QueryInterface(document->GetWindow());
|
2004-09-10 19:29:19 +04:00
|
|
|
if (!global) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptContext> context = global->GetContext();
|
|
|
|
if (!context) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-03-31 20:30:13 +04:00
|
|
|
|
|
|
|
nsAutoMicroTask mt;
|
|
|
|
|
2013-02-26 23:04:13 +04:00
|
|
|
AutoPushJSContext cx(context->GetNativeContext());
|
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
|
|
|
|
2013-04-05 17:21:02 +04:00
|
|
|
JS::Rooted<JS::Value> v(cx);
|
2014-01-06 23:54:43 +04:00
|
|
|
nsresult rv = nsContentUtils::WrapNative(cx, globalObject, aBoundElement, &v);
|
|
|
|
|
2004-09-10 19:29:19 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-02-08 18:24:22 +04: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 23:52:24 +04:00
|
|
|
if (!pusher.Push(aBoundElement))
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2013-02-08 18:24:22 +04:00
|
|
|
MOZ_ASSERT(cx == nsContentUtils::GetCurrentJSContext());
|
|
|
|
|
2013-04-05 17:21:02 +04:00
|
|
|
JS::Rooted<JSObject*> thisObject(cx, &v.toObject());
|
|
|
|
JS::Rooted<JSObject*> scopeObject(cx, xpc::GetXBLScope(cx, globalObject));
|
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);
|
2013-10-16 04:02:23 +04:00
|
|
|
if (!JS_WrapObject(cx, &thisObject))
|
2013-02-08 18:24:22 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2006-06-13 02:39:55 +04:00
|
|
|
|
2004-09-12 05:41:34 +04: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 22:09:38 +04:00
|
|
|
JS::Rooted<JSObject*> jsMethodObject(cx, GetCompiledMethod());
|
|
|
|
JS::Rooted<JSObject*> method(cx, ::JS_CloneFunctionObject(cx, jsMethodObject, thisObject));
|
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);
|
2005-01-05 04:02:13 +03:00
|
|
|
ok = ::JS_CallFunctionValue(cx, thisObject, OBJECT_TO_JSVAL(method),
|
2013-04-05 17:21:02 +04:00
|
|
|
0 /* argc */, nullptr /* argv */, retval.address());
|
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;
|
|
|
|
}
|