2013-07-03 11:24:32 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=4 sw=4 et tw=80:
|
|
|
|
*
|
|
|
|
* 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 "JavaScriptShared.h"
|
2013-11-18 21:21:43 +04:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2014-05-17 03:40:37 +04:00
|
|
|
#include "mozilla/dom/TabChild.h"
|
2013-07-03 11:24:32 +04:00
|
|
|
#include "jsfriendapi.h"
|
|
|
|
#include "xpcprivate.h"
|
2014-05-29 05:05:22 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-07-03 11:24:32 +04:00
|
|
|
|
|
|
|
using namespace js;
|
|
|
|
using namespace JS;
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::jsipc;
|
|
|
|
|
2014-05-17 03:40:35 +04:00
|
|
|
IdToObjectMap::IdToObjectMap()
|
2013-07-03 11:24:32 +04:00
|
|
|
: table_(SystemAllocPolicy())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-05-17 03:40:35 +04:00
|
|
|
IdToObjectMap::init()
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2014-05-17 03:40:36 +04:00
|
|
|
if (table_.initialized())
|
|
|
|
return true;
|
2013-07-03 11:24:32 +04:00
|
|
|
return table_.init(32);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-17 03:40:35 +04:00
|
|
|
IdToObjectMap::trace(JSTracer *trc)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2014-05-17 03:40:35 +04:00
|
|
|
for (Table::Range r(table_.all()); !r.empty(); r.popFront()) {
|
2013-11-26 05:35:09 +04:00
|
|
|
DebugOnly<JSObject *> prior = r.front().value().get();
|
|
|
|
JS_CallHeapObjectTracer(trc, &r.front().value(), "ipc-object");
|
|
|
|
MOZ_ASSERT(r.front().value() == prior);
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-17 03:40:37 +04:00
|
|
|
void
|
|
|
|
IdToObjectMap::finalize(JSFreeOp *fop)
|
|
|
|
{
|
|
|
|
for (Table::Enum e(table_); !e.empty(); e.popFront()) {
|
|
|
|
DebugOnly<JSObject *> prior = e.front().value().get();
|
|
|
|
if (JS_IsAboutToBeFinalized(&e.front().value()))
|
|
|
|
e.removeFront();
|
|
|
|
else
|
|
|
|
MOZ_ASSERT(e.front().value() == prior);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-03 11:24:32 +04:00
|
|
|
JSObject *
|
2014-05-17 03:40:35 +04:00
|
|
|
IdToObjectMap::find(ObjectId id)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2014-05-17 03:40:35 +04:00
|
|
|
Table::Ptr p = table_.lookup(id);
|
2013-07-03 11:24:32 +04:00
|
|
|
if (!p)
|
2013-09-19 23:24:53 +04:00
|
|
|
return nullptr;
|
2013-11-26 05:35:09 +04:00
|
|
|
return p->value();
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-05-17 03:40:35 +04:00
|
|
|
IdToObjectMap::add(ObjectId id, JSObject *obj)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
|
|
|
return table_.put(id, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-17 03:40:35 +04:00
|
|
|
IdToObjectMap::remove(ObjectId id)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
|
|
|
table_.remove(id);
|
|
|
|
}
|
|
|
|
|
2014-05-17 03:40:35 +04:00
|
|
|
ObjectToIdMap::ObjectToIdMap()
|
2013-11-18 21:21:43 +04:00
|
|
|
: table_(nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-17 03:40:35 +04:00
|
|
|
ObjectToIdMap::~ObjectToIdMap()
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2013-11-18 21:21:43 +04:00
|
|
|
if (table_) {
|
2014-05-17 03:40:35 +04:00
|
|
|
dom::AddForDeferredFinalization<Table, nsAutoPtr>(table_);
|
2013-11-18 21:21:43 +04:00
|
|
|
table_ = nullptr;
|
|
|
|
}
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-05-17 03:40:35 +04:00
|
|
|
ObjectToIdMap::init()
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2014-05-17 03:40:36 +04:00
|
|
|
if (table_)
|
|
|
|
return true;
|
|
|
|
|
2014-05-17 03:40:35 +04:00
|
|
|
table_ = new Table(SystemAllocPolicy());
|
2013-11-18 21:21:43 +04:00
|
|
|
return table_ && table_->init(32);
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-17 03:40:37 +04:00
|
|
|
ObjectToIdMap::finalize(JSFreeOp *fop)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2014-05-17 03:40:37 +04:00
|
|
|
for (Table::Enum e(*table_); !e.empty(); e.popFront()) {
|
|
|
|
JSObject *obj = e.front().key();
|
|
|
|
if (JS_IsAboutToBeFinalizedUnbarriered(&obj))
|
|
|
|
e.removeFront();
|
|
|
|
else
|
|
|
|
MOZ_ASSERT(obj == e.front().key());
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectId
|
2014-05-17 03:40:35 +04:00
|
|
|
ObjectToIdMap::find(JSObject *obj)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2014-05-17 03:40:35 +04:00
|
|
|
Table::Ptr p = table_->lookup(obj);
|
2013-07-03 11:24:32 +04:00
|
|
|
if (!p)
|
|
|
|
return 0;
|
2013-11-26 05:35:09 +04:00
|
|
|
return p->value();
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-05-17 03:40:35 +04:00
|
|
|
ObjectToIdMap::add(JSContext *cx, JSObject *obj, ObjectId id)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2013-11-18 21:21:43 +04:00
|
|
|
if (!table_->put(obj, id))
|
2013-08-01 14:21:44 +04:00
|
|
|
return false;
|
2013-11-18 21:21:43 +04:00
|
|
|
JS_StoreObjectPostBarrierCallback(cx, keyMarkCallback, obj, table_);
|
2013-08-01 14:21:44 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is called during minor GCs for each key in the HashMap that has
|
|
|
|
* been moved.
|
|
|
|
*/
|
|
|
|
/* static */ void
|
2014-05-17 03:40:35 +04:00
|
|
|
ObjectToIdMap::keyMarkCallback(JSTracer *trc, JSObject *key, void *data)
|
|
|
|
{
|
|
|
|
Table *table = static_cast<Table*>(data);
|
2013-08-01 14:21:44 +04:00
|
|
|
JSObject *prior = key;
|
|
|
|
JS_CallObjectTracer(trc, &key, "ObjectIdCache::table_ key");
|
2013-11-18 21:21:43 +04:00
|
|
|
table->rekeyIfMoved(prior, key);
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-17 03:40:35 +04:00
|
|
|
ObjectToIdMap::remove(JSObject *obj)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2013-11-18 21:21:43 +04:00
|
|
|
table_->remove(obj);
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
2014-05-29 05:05:22 +04:00
|
|
|
bool JavaScriptShared::sLoggingInitialized;
|
|
|
|
bool JavaScriptShared::sLoggingEnabled;
|
|
|
|
bool JavaScriptShared::sStackLoggingEnabled;
|
|
|
|
|
2014-05-17 03:40:37 +04:00
|
|
|
JavaScriptShared::JavaScriptShared(JSRuntime *rt)
|
|
|
|
: rt_(rt),
|
|
|
|
refcount_(1),
|
|
|
|
lastId_(0)
|
|
|
|
{
|
2014-05-29 05:05:22 +04:00
|
|
|
if (!sLoggingInitialized) {
|
|
|
|
sLoggingInitialized = true;
|
|
|
|
Preferences::AddBoolVarCache(&sLoggingEnabled,
|
|
|
|
"dom.ipc.cpows.log.enabled", false);
|
|
|
|
Preferences::AddBoolVarCache(&sStackLoggingEnabled,
|
|
|
|
"dom.ipc.cpows.log.stack", false);
|
|
|
|
}
|
2014-05-17 03:40:37 +04:00
|
|
|
}
|
|
|
|
|
2013-07-03 11:24:32 +04:00
|
|
|
bool
|
|
|
|
JavaScriptShared::init()
|
|
|
|
{
|
|
|
|
if (!objects_.init())
|
|
|
|
return false;
|
2014-05-17 03:40:36 +04:00
|
|
|
if (!cpows_.init())
|
|
|
|
return false;
|
2014-05-17 03:40:37 +04:00
|
|
|
if (!objectIds_.init())
|
|
|
|
return false;
|
|
|
|
|
2013-07-03 11:24:32 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-17 03:40:37 +04:00
|
|
|
void
|
|
|
|
JavaScriptShared::decref()
|
|
|
|
{
|
|
|
|
refcount_--;
|
|
|
|
if (!refcount_)
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JavaScriptShared::incref()
|
|
|
|
{
|
|
|
|
refcount_++;
|
|
|
|
}
|
|
|
|
|
2013-07-03 11:24:32 +04:00
|
|
|
bool
|
|
|
|
JavaScriptShared::convertIdToGeckoString(JSContext *cx, JS::HandleId id, nsString *to)
|
|
|
|
{
|
|
|
|
RootedValue idval(cx);
|
2014-01-17 22:08:51 +04:00
|
|
|
if (!JS_IdToValue(cx, id, &idval))
|
2013-07-03 11:24:32 +04:00
|
|
|
return false;
|
|
|
|
|
2013-11-16 16:31:36 +04:00
|
|
|
RootedString str(cx, ToString(cx, idval));
|
2013-07-03 11:24:32 +04:00
|
|
|
if (!str)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const jschar *chars = JS_GetStringCharsZ(cx, str);
|
|
|
|
if (!chars)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*to = chars;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
JavaScriptShared::convertGeckoStringToId(JSContext *cx, const nsString &from, JS::MutableHandleId to)
|
|
|
|
{
|
|
|
|
RootedString str(cx, JS_NewUCStringCopyN(cx, from.BeginReading(), from.Length()));
|
|
|
|
if (!str)
|
|
|
|
return false;
|
|
|
|
|
2014-04-02 18:28:03 +04:00
|
|
|
return JS_StringToId(cx, str, to);
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-10-12 09:02:24 +04:00
|
|
|
JavaScriptShared::toVariant(JSContext *cx, JS::HandleValue from, JSVariant *to)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
|
|
|
switch (JS_TypeOfValue(cx, from)) {
|
|
|
|
case JSTYPE_VOID:
|
2014-05-17 03:40:36 +04:00
|
|
|
*to = UndefinedVariant();
|
2013-07-03 11:24:32 +04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case JSTYPE_OBJECT:
|
|
|
|
case JSTYPE_FUNCTION:
|
|
|
|
{
|
2013-08-24 02:53:46 +04:00
|
|
|
RootedObject obj(cx, from.toObjectOrNull());
|
2013-07-03 11:24:32 +04:00
|
|
|
if (!obj) {
|
2014-02-18 10:24:15 +04:00
|
|
|
MOZ_ASSERT(from == JSVAL_NULL);
|
2014-05-17 03:40:36 +04:00
|
|
|
*to = NullVariant();
|
2013-07-03 11:24:32 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xpc_JSObjectIsID(cx, obj)) {
|
|
|
|
JSIID iid;
|
|
|
|
const nsID *id = xpc_JSObjectToID(cx, obj);
|
|
|
|
ConvertID(*id, &iid);
|
|
|
|
*to = iid;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-17 03:40:36 +04:00
|
|
|
ObjectVariant objVar;
|
|
|
|
if (!toObjectVariant(cx, obj, &objVar))
|
2013-07-03 11:24:32 +04:00
|
|
|
return false;
|
2014-05-17 03:40:36 +04:00
|
|
|
*to = objVar;
|
2013-07-03 11:24:32 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case JSTYPE_STRING:
|
|
|
|
{
|
|
|
|
nsDependentJSString dep;
|
|
|
|
if (!dep.init(cx, from))
|
|
|
|
return false;
|
|
|
|
*to = dep;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case JSTYPE_NUMBER:
|
2014-04-28 06:47:02 +04:00
|
|
|
if (from.isInt32())
|
2013-07-03 11:24:32 +04:00
|
|
|
*to = double(from.toInt32());
|
|
|
|
else
|
|
|
|
*to = from.toDouble();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case JSTYPE_BOOLEAN:
|
|
|
|
*to = from.toBoolean();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-05-17 03:40:35 +04:00
|
|
|
JavaScriptShared::fromVariant(JSContext *cx, const JSVariant &from, MutableHandleValue to)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
|
|
|
switch (from.type()) {
|
2014-05-17 03:40:36 +04:00
|
|
|
case JSVariant::TUndefinedVariant:
|
2013-07-03 11:24:32 +04:00
|
|
|
to.set(UndefinedValue());
|
|
|
|
return true;
|
|
|
|
|
2014-05-17 03:40:36 +04:00
|
|
|
case JSVariant::TNullVariant:
|
|
|
|
to.set(NullValue());
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case JSVariant::TObjectVariant:
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2014-05-17 03:40:36 +04:00
|
|
|
JSObject *obj = fromObjectVariant(cx, from.get_ObjectVariant());
|
2014-05-17 03:40:36 +04:00
|
|
|
if (!obj)
|
|
|
|
return false;
|
|
|
|
to.set(ObjectValue(*obj));
|
2013-07-03 11:24:32 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case JSVariant::Tdouble:
|
|
|
|
to.set(JS_NumberValue(from.get_double()));
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case JSVariant::Tbool:
|
|
|
|
to.set(BOOLEAN_TO_JSVAL(from.get_bool()));
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case JSVariant::TnsString:
|
|
|
|
{
|
|
|
|
const nsString &old = from.get_nsString();
|
|
|
|
JSString *str = JS_NewUCStringCopyN(cx, old.BeginReading(), old.Length());
|
|
|
|
if (!str)
|
|
|
|
return false;
|
|
|
|
to.set(StringValue(str));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case JSVariant::TJSIID:
|
|
|
|
{
|
|
|
|
nsID iid;
|
|
|
|
const JSIID &id = from.get_JSIID();
|
|
|
|
ConvertID(id, &iid);
|
|
|
|
|
|
|
|
JSCompartment *compartment = GetContextCompartment(cx);
|
|
|
|
RootedObject global(cx, JS_GetGlobalForCompartmentOrNull(cx, compartment));
|
|
|
|
JSObject *obj = xpc_NewIDObject(cx, global, iid);
|
|
|
|
if (!obj)
|
|
|
|
return false;
|
|
|
|
to.set(ObjectValue(*obj));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
JavaScriptShared::ConvertID(const nsID &from, JSIID *to)
|
|
|
|
{
|
|
|
|
to->m0() = from.m0;
|
|
|
|
to->m1() = from.m1;
|
|
|
|
to->m2() = from.m2;
|
|
|
|
to->m3_0() = from.m3[0];
|
|
|
|
to->m3_1() = from.m3[1];
|
|
|
|
to->m3_2() = from.m3[2];
|
|
|
|
to->m3_3() = from.m3[3];
|
|
|
|
to->m3_4() = from.m3[4];
|
|
|
|
to->m3_5() = from.m3[5];
|
|
|
|
to->m3_6() = from.m3[6];
|
|
|
|
to->m3_7() = from.m3[7];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
JavaScriptShared::ConvertID(const JSIID &from, nsID *to)
|
|
|
|
{
|
|
|
|
to->m0 = from.m0();
|
|
|
|
to->m1 = from.m1();
|
|
|
|
to->m2 = from.m2();
|
|
|
|
to->m3[0] = from.m3_0();
|
|
|
|
to->m3[1] = from.m3_1();
|
|
|
|
to->m3[2] = from.m3_2();
|
|
|
|
to->m3[3] = from.m3_3();
|
|
|
|
to->m3[4] = from.m3_4();
|
|
|
|
to->m3[5] = from.m3_5();
|
|
|
|
to->m3[6] = from.m3_6();
|
|
|
|
to->m3[7] = from.m3_7();
|
|
|
|
}
|
|
|
|
|
2014-05-17 03:40:37 +04:00
|
|
|
JSObject *
|
|
|
|
JavaScriptShared::findObjectById(JSContext *cx, uint32_t objId)
|
2014-05-17 03:40:37 +04:00
|
|
|
{
|
2014-05-17 03:40:37 +04:00
|
|
|
RootedObject obj(cx, findObjectById(objId));
|
|
|
|
if (!obj) {
|
|
|
|
JS_ReportError(cx, "operation not possible on dead CPOW");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Objects are stored in objects_ unwrapped. We want to wrap the object
|
|
|
|
// before returning it so that all operations happen on Xray wrappers. If
|
|
|
|
// the object is a DOM element, we try to obtain the corresponding
|
|
|
|
// TabChildGlobal and wrap in that.
|
|
|
|
RootedObject global(cx, GetGlobalForObjectCrossCompartment(obj));
|
|
|
|
nsCOMPtr<nsIGlobalObject> nativeGlobal = xpc::GetNativeForGlobal(global);
|
|
|
|
nsCOMPtr<nsIDOMWindow> window = do_QueryInterface(nativeGlobal);
|
|
|
|
if (window) {
|
|
|
|
dom::TabChild *tabChild = dom::TabChild::GetFrom(window);
|
|
|
|
if (tabChild) {
|
|
|
|
nsCOMPtr<nsIContentFrameMessageManager> mm;
|
|
|
|
tabChild->GetMessageManager(getter_AddRefs(mm));
|
|
|
|
nsCOMPtr<nsIGlobalObject> tabChildNativeGlobal = do_QueryInterface(mm);
|
|
|
|
RootedObject tabChildGlobal(cx, tabChildNativeGlobal->GetGlobalJSObject());
|
|
|
|
JSAutoCompartment ac(cx, tabChildGlobal);
|
|
|
|
if (!JS_WrapObject(cx, &obj))
|
|
|
|
return nullptr;
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's no TabChildGlobal, we use the junk scope.
|
|
|
|
JSAutoCompartment ac(cx, xpc::GetJunkScope());
|
|
|
|
if (!JS_WrapObject(cx, &obj))
|
|
|
|
return nullptr;
|
|
|
|
return obj;
|
2014-05-17 03:40:37 +04:00
|
|
|
}
|
|
|
|
|
2014-05-17 03:40:36 +04:00
|
|
|
static const uint64_t DefaultPropertyOp = 1;
|
|
|
|
static const uint64_t GetterOnlyPropertyStub = 2;
|
|
|
|
static const uint64_t UnknownPropertyOp = 3;
|
2013-07-03 11:24:32 +04:00
|
|
|
|
|
|
|
bool
|
2013-04-30 21:29:40 +04:00
|
|
|
JavaScriptShared::fromDescriptor(JSContext *cx, Handle<JSPropertyDescriptor> desc,
|
|
|
|
PPropertyDescriptor *out)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2013-04-30 21:29:40 +04:00
|
|
|
out->attrs() = desc.attributes();
|
|
|
|
if (!toVariant(cx, desc.value(), &out->value()))
|
2013-07-03 11:24:32 +04:00
|
|
|
return false;
|
|
|
|
|
2014-05-17 03:40:36 +04:00
|
|
|
JS_ASSERT(desc.object());
|
|
|
|
if (!toObjectVariant(cx, desc.object(), &out->obj()))
|
2013-07-03 11:24:32 +04:00
|
|
|
return false;
|
|
|
|
|
2013-04-30 21:29:40 +04:00
|
|
|
if (!desc.getter()) {
|
2013-07-03 11:24:32 +04:00
|
|
|
out->getter() = 0;
|
2013-04-30 21:29:40 +04:00
|
|
|
} else if (desc.hasGetterObject()) {
|
|
|
|
JSObject *getter = desc.getterObject();
|
2014-05-17 03:40:36 +04:00
|
|
|
ObjectVariant objVar;
|
|
|
|
if (!toObjectVariant(cx, getter, &objVar))
|
2013-07-03 11:24:32 +04:00
|
|
|
return false;
|
2014-05-17 03:40:36 +04:00
|
|
|
out->getter() = objVar;
|
2013-07-03 11:24:32 +04:00
|
|
|
} else {
|
2013-04-30 21:29:40 +04:00
|
|
|
if (desc.getter() == JS_PropertyStub)
|
2013-07-03 11:24:32 +04:00
|
|
|
out->getter() = DefaultPropertyOp;
|
|
|
|
else
|
|
|
|
out->getter() = UnknownPropertyOp;
|
|
|
|
}
|
|
|
|
|
2013-04-30 21:29:40 +04:00
|
|
|
if (!desc.setter()) {
|
2013-07-03 11:24:32 +04:00
|
|
|
out->setter() = 0;
|
2013-04-30 21:29:40 +04:00
|
|
|
} else if (desc.hasSetterObject()) {
|
|
|
|
JSObject *setter = desc.setterObject();
|
2014-05-17 03:40:36 +04:00
|
|
|
ObjectVariant objVar;
|
|
|
|
if (!toObjectVariant(cx, setter, &objVar))
|
2013-07-03 11:24:32 +04:00
|
|
|
return false;
|
2014-05-17 03:40:36 +04:00
|
|
|
out->setter() = objVar;
|
2013-07-03 11:24:32 +04:00
|
|
|
} else {
|
2013-04-30 21:29:40 +04:00
|
|
|
if (desc.setter() == JS_StrictPropertyStub)
|
2013-07-03 11:24:32 +04:00
|
|
|
out->setter() = DefaultPropertyOp;
|
2013-04-30 21:29:40 +04:00
|
|
|
else if (desc.setter() == js_GetterOnlyPropertyStub)
|
2013-07-03 11:24:32 +04:00
|
|
|
out->setter() = GetterOnlyPropertyStub;
|
|
|
|
else
|
|
|
|
out->setter() = UnknownPropertyOp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
bool
|
2013-07-03 11:24:32 +04:00
|
|
|
UnknownPropertyStub(JSContext *cx, HandleObject obj, HandleId id, MutableHandleValue vp)
|
|
|
|
{
|
|
|
|
JS_ReportError(cx, "getter could not be wrapped via CPOWs");
|
2013-08-07 10:59:54 +04:00
|
|
|
return false;
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
bool
|
|
|
|
UnknownStrictPropertyStub(JSContext *cx, HandleObject obj, HandleId id, bool strict, MutableHandleValue vp)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
|
|
|
JS_ReportError(cx, "setter could not be wrapped via CPOWs");
|
2013-08-07 10:59:54 +04:00
|
|
|
return false;
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-04-30 21:29:40 +04:00
|
|
|
JavaScriptShared::toDescriptor(JSContext *cx, const PPropertyDescriptor &in,
|
|
|
|
MutableHandle<JSPropertyDescriptor> out)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2013-04-30 21:29:40 +04:00
|
|
|
out.setAttributes(in.attrs());
|
2014-05-17 03:40:35 +04:00
|
|
|
if (!fromVariant(cx, in.value(), out.value()))
|
2013-07-03 11:24:32 +04:00
|
|
|
return false;
|
2013-08-08 03:15:57 +04:00
|
|
|
Rooted<JSObject*> obj(cx);
|
2014-05-17 03:40:36 +04:00
|
|
|
obj = fromObjectVariant(cx, in.obj());
|
|
|
|
if (!obj)
|
2013-07-03 11:24:32 +04:00
|
|
|
return false;
|
2013-04-30 21:29:40 +04:00
|
|
|
out.object().set(obj);
|
2013-07-03 11:24:32 +04:00
|
|
|
|
2014-05-17 03:40:36 +04:00
|
|
|
if (in.getter().type() == GetterSetter::Tuint64_t && !in.getter().get_uint64_t()) {
|
2013-04-30 21:29:40 +04:00
|
|
|
out.setGetter(nullptr);
|
2013-07-03 11:24:32 +04:00
|
|
|
} else if (in.attrs() & JSPROP_GETTER) {
|
2013-08-08 03:15:57 +04:00
|
|
|
Rooted<JSObject*> getter(cx);
|
2014-05-17 03:40:36 +04:00
|
|
|
getter = fromObjectVariant(cx, in.getter().get_ObjectVariant());
|
|
|
|
if (!getter)
|
2013-07-03 11:24:32 +04:00
|
|
|
return false;
|
2013-04-30 21:29:40 +04:00
|
|
|
out.setGetter(JS_DATA_TO_FUNC_PTR(JSPropertyOp, getter.get()));
|
2013-07-03 11:24:32 +04:00
|
|
|
} else {
|
2014-05-17 03:40:36 +04:00
|
|
|
if (in.getter().get_uint64_t() == DefaultPropertyOp)
|
2013-04-30 21:29:40 +04:00
|
|
|
out.setGetter(JS_PropertyStub);
|
2013-07-03 11:24:32 +04:00
|
|
|
else
|
2013-04-30 21:29:40 +04:00
|
|
|
out.setGetter(UnknownPropertyStub);
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
2014-05-17 03:40:36 +04:00
|
|
|
if (in.setter().type() == GetterSetter::Tuint64_t && !in.setter().get_uint64_t()) {
|
2013-04-30 21:29:40 +04:00
|
|
|
out.setSetter(nullptr);
|
2013-07-03 11:24:32 +04:00
|
|
|
} else if (in.attrs() & JSPROP_SETTER) {
|
2013-08-08 03:15:57 +04:00
|
|
|
Rooted<JSObject*> setter(cx);
|
2014-05-17 03:40:36 +04:00
|
|
|
setter = fromObjectVariant(cx, in.setter().get_ObjectVariant());
|
|
|
|
if (!setter)
|
2013-07-03 11:24:32 +04:00
|
|
|
return false;
|
2013-04-30 21:29:40 +04:00
|
|
|
out.setSetter(JS_DATA_TO_FUNC_PTR(JSStrictPropertyOp, setter.get()));
|
2013-07-03 11:24:32 +04:00
|
|
|
} else {
|
2014-05-17 03:40:36 +04:00
|
|
|
if (in.setter().get_uint64_t() == DefaultPropertyOp)
|
2013-04-30 21:29:40 +04:00
|
|
|
out.setSetter(JS_StrictPropertyStub);
|
2014-05-17 03:40:36 +04:00
|
|
|
else if (in.setter().get_uint64_t() == GetterOnlyPropertyStub)
|
2013-04-30 21:29:40 +04:00
|
|
|
out.setSetter(js_GetterOnlyPropertyStub);
|
2013-07-03 11:24:32 +04:00
|
|
|
else
|
2013-04-30 21:29:40 +04:00
|
|
|
out.setSetter(UnknownStrictPropertyStub);
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-11 02:05:39 +04:00
|
|
|
bool
|
2013-10-16 04:02:23 +04:00
|
|
|
CpowIdHolder::ToObject(JSContext *cx, JS::MutableHandleObject objp)
|
2013-07-11 02:05:39 +04:00
|
|
|
{
|
|
|
|
return js_->Unwrap(cx, cpows_, objp);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-10-16 04:02:23 +04:00
|
|
|
JavaScriptShared::Unwrap(JSContext *cx, const InfallibleTArray<CpowEntry> &aCpows,
|
|
|
|
JS::MutableHandleObject objp)
|
2013-07-11 02:05:39 +04:00
|
|
|
{
|
2013-10-16 04:02:23 +04:00
|
|
|
objp.set(nullptr);
|
2013-07-11 02:05:39 +04:00
|
|
|
|
|
|
|
if (!aCpows.Length())
|
|
|
|
return true;
|
|
|
|
|
2014-01-16 21:48:58 +04:00
|
|
|
RootedObject obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), JS::NullPtr()));
|
2013-07-11 02:05:39 +04:00
|
|
|
if (!obj)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
RootedValue v(cx);
|
|
|
|
RootedString str(cx);
|
|
|
|
for (size_t i = 0; i < aCpows.Length(); i++) {
|
|
|
|
const nsString &name = aCpows[i].name();
|
|
|
|
|
2014-05-17 03:40:35 +04:00
|
|
|
if (!fromVariant(cx, aCpows[i].value(), &v))
|
2013-07-11 02:05:39 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!JS_DefineUCProperty(cx,
|
|
|
|
obj,
|
|
|
|
name.BeginReading(),
|
|
|
|
name.Length(),
|
|
|
|
v,
|
|
|
|
JSPROP_ENUMERATE))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-16 04:02:23 +04:00
|
|
|
objp.set(obj);
|
2013-07-11 02:05:39 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
JavaScriptShared::Wrap(JSContext *cx, HandleObject aObj, InfallibleTArray<CpowEntry> *outCpows)
|
|
|
|
{
|
|
|
|
if (!aObj)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
AutoIdArray ids(cx, JS_Enumerate(cx, aObj));
|
|
|
|
if (!ids)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
RootedId id(cx);
|
|
|
|
RootedValue v(cx);
|
|
|
|
for (size_t i = 0; i < ids.length(); i++) {
|
|
|
|
id = ids[i];
|
|
|
|
|
|
|
|
nsString str;
|
|
|
|
if (!convertIdToGeckoString(cx, id, &str))
|
|
|
|
return false;
|
|
|
|
|
2013-07-26 13:00:38 +04:00
|
|
|
if (!JS_GetPropertyById(cx, aObj, id, &v))
|
2013-07-11 02:05:39 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
JSVariant var;
|
|
|
|
if (!toVariant(cx, v, &var))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
outCpows->AppendElement(CpowEntry(str, var));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|