2012-05-22 06:46:20 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: set ts=2 sw=2 et tw=99 ft=cpp: */
|
|
|
|
/* 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 "mozilla/Util.h"
|
|
|
|
|
|
|
|
#include "DOMJSProxyHandler.h"
|
|
|
|
#include "xpcpublic.h"
|
|
|
|
#include "xpcprivate.h"
|
|
|
|
#include "XPCQuickStubs.h"
|
|
|
|
#include "XPCWrapper.h"
|
|
|
|
#include "WrapperFactory.h"
|
|
|
|
#include "nsDOMClassInfo.h"
|
|
|
|
#include "nsGlobalWindow.h"
|
|
|
|
#include "nsWrapperCacheInlines.h"
|
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
|
|
|
|
|
|
|
#include "jsapi.h"
|
|
|
|
|
|
|
|
using namespace JS;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
jsid s_length_id = JSID_VOID;
|
|
|
|
|
|
|
|
bool
|
|
|
|
DefineStaticJSVals(JSContext* cx)
|
|
|
|
{
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
|
|
|
|
return InternJSString(cx, s_length_id, "length");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int HandlerFamily;
|
|
|
|
|
2012-08-24 09:32:26 -07:00
|
|
|
// Store the information for the specialized ICs.
|
|
|
|
struct SetListBaseInformation
|
|
|
|
{
|
|
|
|
SetListBaseInformation() {
|
|
|
|
js::SetListBaseInformation((void*) &HandlerFamily, js::JSSLOT_PROXY_EXTRA + JSPROXYSLOT_EXPANDO);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
SetListBaseInformation gSetListBaseInformation;
|
|
|
|
|
|
|
|
|
2012-05-22 06:46:20 -07:00
|
|
|
// static
|
|
|
|
JSObject*
|
|
|
|
DOMProxyHandler::EnsureExpandoObject(JSContext* cx, JSObject* obj)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(IsDOMProxy(obj), "expected a DOM proxy object");
|
|
|
|
JSObject* expando = GetExpandoObject(obj);
|
|
|
|
if (!expando) {
|
|
|
|
expando = JS_NewObjectWithGivenProto(cx, nullptr, nullptr,
|
|
|
|
js::GetObjectParent(obj));
|
|
|
|
if (!expando) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-10-25 08:01:09 -07:00
|
|
|
XPCWrappedNativeScope* scope = xpc::GetObjectScope(obj);
|
2012-10-25 08:01:09 -07:00
|
|
|
if (!scope->RegisterDOMExpandoObject(obj)) {
|
2012-05-22 06:46:20 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsWrapperCache* cache;
|
2012-12-06 12:21:19 -08:00
|
|
|
CallQueryInterface(UnwrapDOMObject<nsISupports>(obj), &cache);
|
2012-05-22 06:46:20 -07:00
|
|
|
cache->SetPreservingWrapper(true);
|
|
|
|
|
|
|
|
js::SetProxyExtra(obj, JSPROXYSLOT_EXPANDO, ObjectValue(*expando));
|
|
|
|
}
|
|
|
|
return expando;
|
|
|
|
}
|
|
|
|
|
2013-03-22 19:43:03 -07:00
|
|
|
bool
|
|
|
|
DOMProxyHandler::isExtensible(JSObject *proxy)
|
|
|
|
{
|
|
|
|
return true; // always extensible per WebIDL
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DOMProxyHandler::preventExtensions(JSContext *cx, JS::Handle<JSObject*> proxy)
|
|
|
|
{
|
|
|
|
// Throw a TypeError, per WebIDL.
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_CANT_CHANGE_EXTENSIBILITY);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-22 06:46:20 -07:00
|
|
|
bool
|
2013-03-21 15:23:48 -07:00
|
|
|
DOMProxyHandler::getPropertyDescriptor(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id,
|
2013-01-03 13:31:36 -08:00
|
|
|
JSPropertyDescriptor* desc, unsigned flags)
|
2012-05-22 06:46:20 -07:00
|
|
|
{
|
2013-01-03 13:31:36 -08:00
|
|
|
if (!getOwnPropertyDescriptor(cx, proxy, id, desc, flags)) {
|
2012-05-22 06:46:20 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (desc->obj) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-03 16:42:10 -07:00
|
|
|
JSObject* proto;
|
|
|
|
if (!js::GetObjectProto(cx, proxy, &proto)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-22 06:46:20 -07:00
|
|
|
if (!proto) {
|
|
|
|
desc->obj = NULL;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-15 11:19:55 -07:00
|
|
|
return JS_GetPropertyDescriptorById(cx, proto, id, 0, desc);
|
2012-05-22 06:46:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-03-21 15:23:48 -07:00
|
|
|
DOMProxyHandler::defineProperty(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id,
|
2012-05-22 06:46:20 -07:00
|
|
|
JSPropertyDescriptor* desc)
|
|
|
|
{
|
|
|
|
if ((desc->attrs & JSPROP_GETTER) && desc->setter == JS_StrictPropertyStub) {
|
|
|
|
return JS_ReportErrorFlagsAndNumber(cx,
|
|
|
|
JSREPORT_WARNING | JSREPORT_STRICT |
|
|
|
|
JSREPORT_STRICT_MODE_ERROR,
|
|
|
|
js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_GETTER_ONLY);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xpc::WrapperFactory::IsXrayWrapper(proxy)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* expando = EnsureExpandoObject(cx, proxy);
|
|
|
|
if (!expando) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_DefinePropertyById(cx, expando, id, desc->value, desc->getter, desc->setter,
|
|
|
|
desc->attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-03-21 15:23:48 -07:00
|
|
|
DOMProxyHandler::delete_(JSContext* cx, JS::Handle<JSObject*> proxy,
|
2013-03-21 15:23:48 -07:00
|
|
|
JS::Handle<jsid> id, bool* bp)
|
2012-05-22 06:46:20 -07:00
|
|
|
{
|
|
|
|
JSBool b = true;
|
|
|
|
|
|
|
|
JSObject* expando;
|
|
|
|
if (!xpc::WrapperFactory::IsXrayWrapper(proxy) && (expando = GetExpandoObject(proxy))) {
|
|
|
|
Value v;
|
|
|
|
if (!JS_DeletePropertyById2(cx, expando, id, &v) || !JS_ValueToBoolean(cx, v, &b)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*bp = !!b;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-03-21 15:23:48 -07:00
|
|
|
DOMProxyHandler::enumerate(JSContext* cx, JS::Handle<JSObject*> proxy, AutoIdVector& props)
|
2012-05-22 06:46:20 -07:00
|
|
|
{
|
2012-09-03 16:42:10 -07:00
|
|
|
JSObject* proto;
|
|
|
|
if (!JS_GetPrototype(cx, proxy, &proto)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-22 06:46:20 -07:00
|
|
|
return getOwnPropertyNames(cx, proxy, props) &&
|
|
|
|
(!proto || js::GetPropertyNames(cx, proto, 0, &props));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-03-21 15:23:48 -07:00
|
|
|
DOMProxyHandler::has(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id, bool* bp)
|
2012-05-22 06:46:20 -07:00
|
|
|
{
|
|
|
|
if (!hasOwn(cx, proxy, id, bp)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*bp) {
|
|
|
|
// We have the property ourselves; no need to worry about our prototype
|
|
|
|
// chain.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// OK, now we have to look at the proto
|
2012-09-03 16:42:10 -07:00
|
|
|
JSObject *proto;
|
|
|
|
if (!js::GetObjectProto(cx, proxy, &proto)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-22 06:46:20 -07:00
|
|
|
if (!proto) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
JSBool protoHasProp;
|
|
|
|
bool ok = JS_HasPropertyById(cx, proto, id, &protoHasProp);
|
|
|
|
if (ok) {
|
|
|
|
*bp = protoHasProp;
|
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
JSString*
|
|
|
|
DOMProxyHandler::obj_toString(JSContext* cx, const char* className)
|
|
|
|
{
|
|
|
|
size_t nchars = sizeof("[object ]") - 1 + strlen(className);
|
|
|
|
jschar* chars = static_cast<jschar*>(JS_malloc(cx, (nchars + 1) * sizeof(jschar)));
|
|
|
|
if (!chars) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* prefix = "[object ";
|
|
|
|
nchars = 0;
|
|
|
|
while ((chars[nchars] = (jschar)*prefix) != 0) {
|
|
|
|
nchars++, prefix++;
|
|
|
|
}
|
|
|
|
while ((chars[nchars] = (jschar)*className) != 0) {
|
|
|
|
nchars++, className++;
|
|
|
|
}
|
|
|
|
chars[nchars++] = ']';
|
|
|
|
chars[nchars] = 0;
|
|
|
|
|
|
|
|
JSString* str = JS_NewUCString(cx, chars, nchars);
|
|
|
|
if (!str) {
|
|
|
|
JS_free(cx, chars);
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2012-11-05 08:58:03 -08:00
|
|
|
bool
|
|
|
|
DOMProxyHandler::AppendNamedPropertyIds(JSContext* cx, JSObject* proxy,
|
|
|
|
nsTArray<nsString>& names,
|
|
|
|
JS::AutoIdVector& props)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < names.Length(); ++i) {
|
|
|
|
JS::Value v;
|
|
|
|
if (!xpc::NonVoidStringToJsval(cx, names[i], &v)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsid id;
|
|
|
|
if (!JS_ValueToId(cx, v, &id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HasPropertyOnPrototype(cx, proxy, this, id)) {
|
|
|
|
if (!props.append(id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-22 06:46:20 -07:00
|
|
|
int32_t
|
|
|
|
IdToInt32(JSContext* cx, jsid id)
|
|
|
|
{
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
|
2013-04-02 16:05:37 -07:00
|
|
|
JS::Value idval;
|
2012-05-22 06:46:20 -07:00
|
|
|
double array_index;
|
|
|
|
int32_t i;
|
|
|
|
if (!::JS_IdToValue(cx, id, &idval) ||
|
|
|
|
!::JS_ValueToNumber(cx, idval, &array_index) ||
|
|
|
|
!::JS_DoubleIsInt32(array_index, &i)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|