2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* David Hyatt <hyatt@netscape.com> (Original Author)
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsIAtom.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsString.h"
|
2010-04-30 12:40:59 -07:00
|
|
|
#include "mozilla/FunctionTimer.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsXBLProtoImplMethod.h"
|
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
|
|
|
|
nsXBLProtoImplMethod::nsXBLProtoImplMethod(const PRUnichar* aName) :
|
|
|
|
nsXBLProtoImplMember(aName),
|
2008-02-12 08:02:41 -08:00
|
|
|
mUncompiledMethod(BIT_UNCOMPILED)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsXBLProtoImplMethod);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsXBLProtoImplMethod::~nsXBLProtoImplMethod()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(nsXBLProtoImplMethod);
|
|
|
|
|
2008-02-12 08:02:41 -08:00
|
|
|
if (!IsCompiled()) {
|
|
|
|
delete GetUncompiledMethod();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsXBLProtoImplMethod::AppendBodyText(const nsAString& aText)
|
|
|
|
{
|
2008-02-12 08:02:41 -08:00
|
|
|
NS_PRECONDITION(!IsCompiled(),
|
2007-03-22 10:30:00 -07:00
|
|
|
"Must not be compiled when accessing uncompiled method");
|
2008-02-12 08:02:41 -08:00
|
|
|
|
|
|
|
nsXBLUncompiledMethod* uncompiledMethod = GetUncompiledMethod();
|
|
|
|
if (!uncompiledMethod) {
|
|
|
|
uncompiledMethod = new nsXBLUncompiledMethod();
|
|
|
|
if (!uncompiledMethod)
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
2008-02-12 08:02:41 -08:00
|
|
|
SetUncompiledMethod(uncompiledMethod);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-02-12 08:02:41 -08:00
|
|
|
uncompiledMethod->AppendBodyText(aText);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsXBLProtoImplMethod::AddParameter(const nsAString& aText)
|
|
|
|
{
|
2008-02-12 08:02:41 -08:00
|
|
|
NS_PRECONDITION(!IsCompiled(),
|
2007-03-22 10:30:00 -07:00
|
|
|
"Must not be compiled when accessing uncompiled method");
|
2008-02-12 08:02:41 -08:00
|
|
|
|
|
|
|
nsXBLUncompiledMethod* uncompiledMethod = GetUncompiledMethod();
|
|
|
|
if (!uncompiledMethod) {
|
|
|
|
uncompiledMethod = new nsXBLUncompiledMethod();
|
|
|
|
if (!uncompiledMethod)
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
2008-02-12 08:02:41 -08:00
|
|
|
SetUncompiledMethod(uncompiledMethod);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-02-12 08:02:41 -08:00
|
|
|
uncompiledMethod->AddParameter(aText);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsXBLProtoImplMethod::SetLineNumber(PRUint32 aLineNumber)
|
|
|
|
{
|
2008-02-12 08:02:41 -08:00
|
|
|
NS_PRECONDITION(!IsCompiled(),
|
2007-03-22 10:30:00 -07:00
|
|
|
"Must not be compiled when accessing uncompiled method");
|
2008-02-12 08:02:41 -08:00
|
|
|
|
|
|
|
nsXBLUncompiledMethod* uncompiledMethod = GetUncompiledMethod();
|
|
|
|
if (!uncompiledMethod) {
|
|
|
|
uncompiledMethod = new nsXBLUncompiledMethod();
|
|
|
|
if (!uncompiledMethod)
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
2008-02-12 08:02:41 -08:00
|
|
|
SetUncompiledMethod(uncompiledMethod);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-02-12 08:02:41 -08:00
|
|
|
uncompiledMethod->SetLineNumber(aLineNumber);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsXBLProtoImplMethod::InstallMember(nsIScriptContext* aContext,
|
|
|
|
nsIContent* aBoundElement,
|
|
|
|
void* aScriptObject,
|
|
|
|
void* aTargetClassObject,
|
|
|
|
const nsCString& aClassStr)
|
|
|
|
{
|
2008-02-12 08:02:41 -08:00
|
|
|
NS_PRECONDITION(IsCompiled(),
|
2007-03-22 10:30:00 -07:00
|
|
|
"Should not be installing an uncompiled method");
|
|
|
|
JSContext* cx = (JSContext*) aContext->GetNativeContext();
|
|
|
|
|
|
|
|
nsIDocument *ownerDoc = aBoundElement->GetOwnerDoc();
|
|
|
|
nsIScriptGlobalObject *sgo;
|
|
|
|
|
2008-11-06 18:06:49 -08:00
|
|
|
if (!ownerDoc || !(sgo = ownerDoc->GetScopeObject())) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject * scriptObject = (JSObject *) aScriptObject;
|
|
|
|
NS_ASSERTION(scriptObject, "uh-oh, script Object should NOT be null or bad things will happen");
|
|
|
|
if (!scriptObject)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
JSObject * targetClassObject = (JSObject *) aTargetClassObject;
|
|
|
|
JSObject * globalObject = sgo->GetGlobalJSObject();
|
|
|
|
|
|
|
|
// now we want to reevaluate our property using aContext and the script object for this window...
|
|
|
|
if (mJSMethodObject && targetClassObject) {
|
|
|
|
nsDependentString name(mName);
|
|
|
|
JSAutoRequest ar(cx);
|
2010-09-22 17:34:20 -07:00
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
|
|
|
|
if (!ac.enter(cx, mJSMethodObject)) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject * method = ::JS_CloneFunctionObject(cx, mJSMethodObject, globalObject);
|
|
|
|
if (!method) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsAutoGCRoot root(&method, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!::JS_DefineUCProperty(cx, targetClassObject,
|
2007-07-08 00:08:04 -07:00
|
|
|
reinterpret_cast<const jschar*>(mName),
|
2007-03-22 10:30:00 -07:00
|
|
|
name.Length(), OBJECT_TO_JSVAL(method),
|
|
|
|
NULL, NULL, JSPROP_ENUMERATE)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsXBLProtoImplMethod::CompileMember(nsIScriptContext* aContext, const nsCString& aClassStr,
|
|
|
|
void* aClassObject)
|
|
|
|
{
|
2010-04-30 12:40:59 -07:00
|
|
|
NS_TIME_FUNCTION_MIN(5);
|
2008-02-12 08:02:41 -08:00
|
|
|
NS_PRECONDITION(!IsCompiled(),
|
2007-03-22 10:30:00 -07:00
|
|
|
"Trying to compile an already-compiled method");
|
|
|
|
NS_PRECONDITION(aClassObject,
|
|
|
|
"Must have class object to compile");
|
|
|
|
|
2008-02-12 08:02:41 -08:00
|
|
|
nsXBLUncompiledMethod* uncompiledMethod = GetUncompiledMethod();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// No parameters or body was supplied, so don't install method.
|
2008-02-12 08:02:41 -08:00
|
|
|
if (!uncompiledMethod) {
|
|
|
|
// Early return after which we consider ourselves compiled.
|
|
|
|
mJSMethodObject = nsnull;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2008-02-12 08:02:41 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Don't install method if no name was supplied.
|
|
|
|
if (!mName) {
|
2008-02-12 08:02:41 -08:00
|
|
|
delete uncompiledMethod;
|
|
|
|
|
|
|
|
// Early return after which we consider ourselves compiled.
|
|
|
|
mJSMethodObject = nsnull;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have a method.
|
|
|
|
// Allocate an array for our arguments.
|
2008-02-12 08:02:41 -08:00
|
|
|
PRInt32 paramCount = uncompiledMethod->GetParameterCount();
|
2007-03-22 10:30:00 -07:00
|
|
|
char** args = nsnull;
|
|
|
|
if (paramCount > 0) {
|
|
|
|
args = new char*[paramCount];
|
|
|
|
if (!args)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2009-08-12 02:49:52 -07:00
|
|
|
// Add our parameters to our args array.
|
|
|
|
PRInt32 argPos = 0;
|
|
|
|
for (nsXBLParameter* curr = uncompiledMethod->mParameters;
|
|
|
|
curr;
|
|
|
|
curr = curr->mNext) {
|
|
|
|
args[argPos] = curr->mName;
|
|
|
|
argPos++;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the body
|
|
|
|
nsDependentString body;
|
2008-02-12 08:02:41 -08:00
|
|
|
PRUnichar *bodyText = uncompiledMethod->mBodyText.GetText();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (bodyText)
|
|
|
|
body.Rebind(bodyText);
|
|
|
|
|
|
|
|
// Now that we have a body and args, compile the function
|
|
|
|
// and then define it.
|
|
|
|
NS_ConvertUTF16toUTF8 cname(mName);
|
|
|
|
nsCAutoString functionUri(aClassStr);
|
|
|
|
PRInt32 hash = functionUri.RFindChar('#');
|
|
|
|
if (hash != kNotFound) {
|
|
|
|
functionUri.Truncate(hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* methodObject = nsnull;
|
|
|
|
nsresult rv = aContext->CompileFunction(aClassObject,
|
|
|
|
cname,
|
|
|
|
paramCount,
|
|
|
|
(const char**)args,
|
|
|
|
body,
|
|
|
|
functionUri.get(),
|
2008-02-12 08:02:41 -08:00
|
|
|
uncompiledMethod->mBodyText.GetLineNumber(),
|
2008-02-15 21:13:16 -08:00
|
|
|
JSVERSION_LATEST,
|
2007-03-22 10:30:00 -07:00
|
|
|
PR_TRUE,
|
|
|
|
(void **) &methodObject);
|
|
|
|
|
|
|
|
// Destroy our uncompiled method and delete our arg list.
|
2008-02-12 08:02:41 -08:00
|
|
|
delete uncompiledMethod;
|
2007-03-22 10:30:00 -07:00
|
|
|
delete [] args;
|
|
|
|
if (NS_FAILED(rv)) {
|
2008-02-12 08:02:41 -08:00
|
|
|
SetUncompiledMethod(nsnull);
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mJSMethodObject = methodObject;
|
|
|
|
|
2008-02-12 08:02:41 -08:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-05-24 07:10:02 -07:00
|
|
|
void
|
2007-10-29 06:45:07 -07:00
|
|
|
nsXBLProtoImplMethod::Trace(TraceCallback aCallback, void *aClosure) const
|
2007-05-24 07:10:02 -07:00
|
|
|
{
|
2008-02-12 08:02:41 -08:00
|
|
|
if (IsCompiled() && mJSMethodObject) {
|
2007-10-29 06:45:07 -07:00
|
|
|
aCallback(nsIProgrammingLanguage::JAVASCRIPT, mJSMethodObject, aClosure);
|
|
|
|
}
|
2007-05-24 07:10:02 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
|
|
|
nsXBLProtoImplAnonymousMethod::Execute(nsIContent* aBoundElement)
|
|
|
|
{
|
2008-02-12 08:02:41 -08:00
|
|
|
NS_PRECONDITION(IsCompiled(), "Can't execute uncompiled method");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!mJSMethodObject) {
|
|
|
|
// Nothing to do here
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the script context the same way
|
|
|
|
// nsXBLProtoImpl::InstallImplementation does.
|
|
|
|
nsIDocument* document = aBoundElement->GetOwnerDoc();
|
|
|
|
if (!document) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIScriptGlobalObject* global = document->GetScriptGlobalObject();
|
|
|
|
if (!global) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptContext> context = global->GetContext();
|
|
|
|
if (!context) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext* cx = (JSContext*) context->GetNativeContext();
|
|
|
|
|
|
|
|
JSObject* globalObject = global->GetGlobalJSObject();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> wrapper;
|
2009-08-14 12:00:24 -07:00
|
|
|
jsval v;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv =
|
2009-08-14 12:00:24 -07:00
|
|
|
nsContentUtils::WrapNative(cx, globalObject, aBoundElement, &v,
|
|
|
|
getter_AddRefs(wrapper));
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2009-08-14 12:00:24 -07:00
|
|
|
JSObject* thisObject = JSVAL_TO_OBJECT(v);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
|
|
|
|
// 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).
|
|
|
|
JSObject* method = ::JS_CloneFunctionObject(cx, mJSMethodObject, thisObject);
|
|
|
|
if (!method)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
// Now call the method
|
|
|
|
|
|
|
|
// Use nsCxPusher to make sure we call ScriptEvaluated when we're done.
|
2007-10-01 03:02:32 -07:00
|
|
|
nsCxPusher pusher;
|
|
|
|
NS_ENSURE_STATE(pusher.Push(aBoundElement));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Check whether it's OK to call the method.
|
|
|
|
rv = nsContentUtils::GetSecurityManager()->CheckFunctionAccess(cx, method,
|
|
|
|
thisObject);
|
|
|
|
|
|
|
|
JSBool ok = JS_TRUE;
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
jsval retval;
|
|
|
|
ok = ::JS_CallFunctionValue(cx, thisObject, OBJECT_TO_JSVAL(method),
|
|
|
|
0 /* argc */, nsnull /* argv */, &retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok) {
|
2009-05-19 19:11:01 -07: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.
|
|
|
|
JSStackFrame* frame = JS_SaveFrameChain(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
::JS_ReportPendingException(cx);
|
2009-05-19 19:11:01 -07:00
|
|
|
JS_RestoreFrameChain(cx, frame);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|