Bug 723206 - Constructors implemented in JS from IDL should be allowed to have arguments [r=mrbkap]

This commit is contained in:
Fabrice Desré 2012-06-01 14:40:14 -07:00
parent 4cbec42f85
commit 70c05cb278
3 changed files with 79 additions and 4 deletions

View File

@ -518,6 +518,8 @@ using mozilla::dom::indexedDB::IDBWrapperCache;
#undef None // something included above defines this preprocessor symbol, maybe Xlib headers
#include "WebGLContext.h"
#include "nsIDOMGlobalObjectConstructor.h"
using namespace mozilla;
using namespace mozilla::dom;
@ -5634,7 +5636,8 @@ BaseStubConstructor(nsIWeakReference* aWeakOwner,
}
nsCOMPtr<nsIJSNativeInitializer> initializer(do_QueryInterface(native));
if (initializer) {
nsCOMPtr<nsIDOMGlobalObjectConstructor> constructor(do_QueryInterface(native));
if (initializer || constructor) {
// Initialize object using the current inner window, but only if
// the caller can access it.
nsCOMPtr<nsPIDOMWindow> owner = do_QueryReferent(aWeakOwner);
@ -5647,9 +5650,61 @@ BaseStubConstructor(nsIWeakReference* aWeakOwner,
return NS_ERROR_DOM_SECURITY_ERR;
}
rv = initializer->Initialize(currentInner, cx, obj, argc, argv);
if (NS_FAILED(rv)) {
return rv;
if (initializer) {
rv = initializer->Initialize(currentInner, cx, obj, argc, argv);
if (NS_FAILED(rv)) {
return rv;
}
} else {
nsCOMPtr<nsIXPConnectWrappedJS> wrappedJS = do_QueryInterface(native);
JSObject* object = nsnull;
wrappedJS->GetJSObject(&object);
if (!object) {
return NS_ERROR_UNEXPECTED;
}
nsCxPusher pusher;
NS_ENSURE_STATE(pusher.Push(cx, false));
JSAutoRequest ar(cx);
JSAutoEnterCompartment ac;
if (!ac.enter(cx, object)) {
return NS_ERROR_FAILURE;
}
JS::Value thisValue = JSVAL_VOID;
JS::Value funval;
if (!JS_GetProperty(cx, object, "constructor", &funval) || !funval.isObject()) {
return NS_ERROR_UNEXPECTED;
}
// Check if the object is even callable.
NS_ENSURE_STATE(JS_ObjectIsCallable(cx, &funval.toObject()));
thisValue.setObject(*object);
{
JSObject* thisObject = &thisValue.toObject();
// wrap parameters in the target compartment
nsAutoArrayPtr<JS::Value> args(new JS::Value[argc]);
JS::AutoArrayRooter rooter(cx, 0, args);
for (size_t i = 0; i < argc; ++i) {
args[i] = argv[i];
if (!JS_WrapValue(cx, &args[i]))
return NS_ERROR_FAILURE;
rooter.changeLength(i + 1);
}
JS::Value frval;
bool ret = JS_CallFunctionValue(cx, thisObject, funval, argc, args, &frval);
if (!ret) {
return NS_ERROR_FAILURE;
}
}
}
}

View File

@ -52,6 +52,7 @@ XPIDLSRCS = \
nsITabChild.idl \
nsITabParent.idl \
nsIDOMGlobalPropertyInitializer.idl \
nsIDOMGlobalObjectConstructor.idl \
nsIStructuredCloneContainer.idl \
nsIDOMPerformance.idl \
nsIDOMPerformanceTiming.idl \

View File

@ -0,0 +1,19 @@
/* -*- Mode: IDL; 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 "domstubs.idl"
[scriptable, uuid(cb439c73-0129-4289-a349-c5216e6b912a)]
interface nsIDOMGlobalObjectConstructor : nsISupports
{
/*
* JS use only
*
* The constructor() method will be called with any parameters passed
* to the object constructor.
* If the JS implementation returns a value, it will be ignored.
*/
void constructor();
};