Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=4 sw=4 et tw=80: |
michael@0 | 3 | * |
michael@0 | 4 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 7 | |
michael@0 | 8 | #include "JavaScriptChild.h" |
michael@0 | 9 | #include "mozilla/dom/ContentChild.h" |
michael@0 | 10 | #include "mozilla/dom/BindingUtils.h" |
michael@0 | 11 | #include "nsContentUtils.h" |
michael@0 | 12 | #include "xpcprivate.h" |
michael@0 | 13 | #include "jsfriendapi.h" |
michael@0 | 14 | #include "nsCxPusher.h" |
michael@0 | 15 | |
michael@0 | 16 | using namespace JS; |
michael@0 | 17 | using namespace mozilla; |
michael@0 | 18 | using namespace mozilla::jsipc; |
michael@0 | 19 | |
michael@0 | 20 | using mozilla::AutoSafeJSContext; |
michael@0 | 21 | |
michael@0 | 22 | JavaScriptChild::JavaScriptChild(JSRuntime *rt) |
michael@0 | 23 | : lastId_(0), |
michael@0 | 24 | rt_(rt) |
michael@0 | 25 | { |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | static void |
michael@0 | 29 | Trace(JSTracer *trc, void *data) |
michael@0 | 30 | { |
michael@0 | 31 | reinterpret_cast<JavaScriptChild *>(data)->trace(trc); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | JavaScriptChild::~JavaScriptChild() |
michael@0 | 35 | { |
michael@0 | 36 | JS_RemoveExtraGCRootsTracer(rt_, Trace, this); |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | void |
michael@0 | 40 | JavaScriptChild::trace(JSTracer *trc) |
michael@0 | 41 | { |
michael@0 | 42 | objects_.trace(trc); |
michael@0 | 43 | ids_.trace(trc); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | bool |
michael@0 | 47 | JavaScriptChild::init() |
michael@0 | 48 | { |
michael@0 | 49 | if (!JavaScriptShared::init()) |
michael@0 | 50 | return false; |
michael@0 | 51 | if (!ids_.init()) |
michael@0 | 52 | return false; |
michael@0 | 53 | |
michael@0 | 54 | JS_AddExtraGCRootsTracer(rt_, Trace, this); |
michael@0 | 55 | return true; |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | bool |
michael@0 | 59 | JavaScriptChild::RecvDropObject(const ObjectId &objId) |
michael@0 | 60 | { |
michael@0 | 61 | JSObject *obj = findObject(objId); |
michael@0 | 62 | if (obj) { |
michael@0 | 63 | ids_.remove(obj); |
michael@0 | 64 | objects_.remove(objId); |
michael@0 | 65 | } |
michael@0 | 66 | return true; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | bool |
michael@0 | 70 | JavaScriptChild::makeId(JSContext *cx, JSObject *obj, ObjectId *idp) |
michael@0 | 71 | { |
michael@0 | 72 | if (!obj) { |
michael@0 | 73 | *idp = 0; |
michael@0 | 74 | return true; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | ObjectId id = ids_.find(obj); |
michael@0 | 78 | if (id) { |
michael@0 | 79 | *idp = id; |
michael@0 | 80 | return true; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | id = ++lastId_; |
michael@0 | 84 | if (id > MAX_CPOW_IDS) { |
michael@0 | 85 | JS_ReportError(cx, "CPOW id limit reached"); |
michael@0 | 86 | return false; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | id <<= OBJECT_EXTRA_BITS; |
michael@0 | 90 | if (JS_ObjectIsCallable(cx, obj)) |
michael@0 | 91 | id |= OBJECT_IS_CALLABLE; |
michael@0 | 92 | |
michael@0 | 93 | if (!objects_.add(id, obj)) |
michael@0 | 94 | return false; |
michael@0 | 95 | if (!ids_.add(cx, obj, id)) |
michael@0 | 96 | return false; |
michael@0 | 97 | |
michael@0 | 98 | *idp = id; |
michael@0 | 99 | return true; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | JSObject * |
michael@0 | 103 | JavaScriptChild::unwrap(JSContext *cx, ObjectId id) |
michael@0 | 104 | { |
michael@0 | 105 | JSObject *obj = findObject(id); |
michael@0 | 106 | MOZ_ASSERT(obj); |
michael@0 | 107 | return obj; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | bool |
michael@0 | 111 | JavaScriptChild::fail(JSContext *cx, ReturnStatus *rs) |
michael@0 | 112 | { |
michael@0 | 113 | // By default, we set |undefined| unless we can get a more meaningful |
michael@0 | 114 | // exception. |
michael@0 | 115 | *rs = ReturnStatus(ReturnException(JSVariant(void_t()))); |
michael@0 | 116 | |
michael@0 | 117 | // Note we always return true from this function, since this propagates |
michael@0 | 118 | // to the IPC code, and we don't want a JS failure to cause the death |
michael@0 | 119 | // of the child process. |
michael@0 | 120 | |
michael@0 | 121 | RootedValue exn(cx); |
michael@0 | 122 | if (!JS_GetPendingException(cx, &exn)) |
michael@0 | 123 | return true; |
michael@0 | 124 | |
michael@0 | 125 | // If we don't clear the pending exception, JS will try to wrap it as it |
michael@0 | 126 | // leaves the current compartment. Since there is no previous compartment, |
michael@0 | 127 | // that would crash. |
michael@0 | 128 | JS_ClearPendingException(cx); |
michael@0 | 129 | |
michael@0 | 130 | if (JS_IsStopIteration(exn)) { |
michael@0 | 131 | *rs = ReturnStatus(ReturnStopIteration()); |
michael@0 | 132 | return true; |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | // If this fails, we still don't want to exit. Just return an invalid |
michael@0 | 136 | // exception. |
michael@0 | 137 | (void) toVariant(cx, exn, &rs->get_ReturnException().exn()); |
michael@0 | 138 | return true; |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | bool |
michael@0 | 142 | JavaScriptChild::ok(ReturnStatus *rs) |
michael@0 | 143 | { |
michael@0 | 144 | *rs = ReturnStatus(ReturnSuccess()); |
michael@0 | 145 | return true; |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | bool |
michael@0 | 149 | JavaScriptChild::AnswerPreventExtensions(const ObjectId &objId, ReturnStatus *rs) |
michael@0 | 150 | { |
michael@0 | 151 | AutoSafeJSContext cx; |
michael@0 | 152 | JSAutoRequest request(cx); |
michael@0 | 153 | |
michael@0 | 154 | RootedObject obj(cx, findObject(objId)); |
michael@0 | 155 | if (!obj) |
michael@0 | 156 | return false; |
michael@0 | 157 | |
michael@0 | 158 | JSAutoCompartment comp(cx, obj); |
michael@0 | 159 | if (!JS_PreventExtensions(cx, obj)) |
michael@0 | 160 | return fail(cx, rs); |
michael@0 | 161 | |
michael@0 | 162 | return ok(rs); |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | static void |
michael@0 | 166 | EmptyDesc(PPropertyDescriptor *desc) |
michael@0 | 167 | { |
michael@0 | 168 | desc->objId() = 0; |
michael@0 | 169 | desc->attrs() = 0; |
michael@0 | 170 | desc->value() = void_t(); |
michael@0 | 171 | desc->getter() = 0; |
michael@0 | 172 | desc->setter() = 0; |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | bool |
michael@0 | 176 | JavaScriptChild::AnswerGetPropertyDescriptor(const ObjectId &objId, const nsString &id, |
michael@0 | 177 | ReturnStatus *rs, PPropertyDescriptor *out) |
michael@0 | 178 | { |
michael@0 | 179 | AutoSafeJSContext cx; |
michael@0 | 180 | JSAutoRequest request(cx); |
michael@0 | 181 | |
michael@0 | 182 | EmptyDesc(out); |
michael@0 | 183 | |
michael@0 | 184 | RootedObject obj(cx, findObject(objId)); |
michael@0 | 185 | if (!obj) |
michael@0 | 186 | return false; |
michael@0 | 187 | |
michael@0 | 188 | JSAutoCompartment comp(cx, obj); |
michael@0 | 189 | |
michael@0 | 190 | RootedId internedId(cx); |
michael@0 | 191 | if (!convertGeckoStringToId(cx, id, &internedId)) |
michael@0 | 192 | return fail(cx, rs); |
michael@0 | 193 | |
michael@0 | 194 | Rooted<JSPropertyDescriptor> desc(cx); |
michael@0 | 195 | if (!JS_GetPropertyDescriptorById(cx, obj, internedId, &desc)) |
michael@0 | 196 | return fail(cx, rs); |
michael@0 | 197 | |
michael@0 | 198 | if (!desc.object()) |
michael@0 | 199 | return ok(rs); |
michael@0 | 200 | |
michael@0 | 201 | if (!fromDescriptor(cx, desc, out)) |
michael@0 | 202 | return fail(cx, rs); |
michael@0 | 203 | |
michael@0 | 204 | return ok(rs); |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | bool |
michael@0 | 208 | JavaScriptChild::AnswerGetOwnPropertyDescriptor(const ObjectId &objId, const nsString &id, |
michael@0 | 209 | ReturnStatus *rs, PPropertyDescriptor *out) |
michael@0 | 210 | { |
michael@0 | 211 | AutoSafeJSContext cx; |
michael@0 | 212 | JSAutoRequest request(cx); |
michael@0 | 213 | |
michael@0 | 214 | EmptyDesc(out); |
michael@0 | 215 | |
michael@0 | 216 | RootedObject obj(cx, findObject(objId)); |
michael@0 | 217 | if (!obj) |
michael@0 | 218 | return false; |
michael@0 | 219 | |
michael@0 | 220 | JSAutoCompartment comp(cx, obj); |
michael@0 | 221 | |
michael@0 | 222 | RootedId internedId(cx); |
michael@0 | 223 | if (!convertGeckoStringToId(cx, id, &internedId)) |
michael@0 | 224 | return fail(cx, rs); |
michael@0 | 225 | |
michael@0 | 226 | Rooted<JSPropertyDescriptor> desc(cx); |
michael@0 | 227 | if (!JS_GetPropertyDescriptorById(cx, obj, internedId, &desc)) |
michael@0 | 228 | return fail(cx, rs); |
michael@0 | 229 | |
michael@0 | 230 | if (desc.object() != obj) |
michael@0 | 231 | return ok(rs); |
michael@0 | 232 | |
michael@0 | 233 | if (!fromDescriptor(cx, desc, out)) |
michael@0 | 234 | return fail(cx, rs); |
michael@0 | 235 | |
michael@0 | 236 | return ok(rs); |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | bool |
michael@0 | 240 | JavaScriptChild::AnswerDefineProperty(const ObjectId &objId, const nsString &id, |
michael@0 | 241 | const PPropertyDescriptor &descriptor, ReturnStatus *rs) |
michael@0 | 242 | { |
michael@0 | 243 | AutoSafeJSContext cx; |
michael@0 | 244 | JSAutoRequest request(cx); |
michael@0 | 245 | |
michael@0 | 246 | RootedObject obj(cx, findObject(objId)); |
michael@0 | 247 | if (!obj) |
michael@0 | 248 | return false; |
michael@0 | 249 | |
michael@0 | 250 | JSAutoCompartment comp(cx, obj); |
michael@0 | 251 | |
michael@0 | 252 | RootedId internedId(cx); |
michael@0 | 253 | if (!convertGeckoStringToId(cx, id, &internedId)) |
michael@0 | 254 | return fail(cx, rs); |
michael@0 | 255 | |
michael@0 | 256 | Rooted<JSPropertyDescriptor> desc(cx); |
michael@0 | 257 | if (!toDescriptor(cx, descriptor, &desc)) |
michael@0 | 258 | return false; |
michael@0 | 259 | |
michael@0 | 260 | if (!js::CheckDefineProperty(cx, obj, internedId, desc.value(), desc.getter(), |
michael@0 | 261 | desc.setter(), desc.attributes())) |
michael@0 | 262 | { |
michael@0 | 263 | return fail(cx, rs); |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | if (!JS_DefinePropertyById(cx, obj, internedId, desc.value(), desc.getter(), |
michael@0 | 267 | desc.setter(), desc.attributes())) |
michael@0 | 268 | { |
michael@0 | 269 | return fail(cx, rs); |
michael@0 | 270 | } |
michael@0 | 271 | |
michael@0 | 272 | return ok(rs); |
michael@0 | 273 | } |
michael@0 | 274 | |
michael@0 | 275 | bool |
michael@0 | 276 | JavaScriptChild::AnswerDelete(const ObjectId &objId, const nsString &id, ReturnStatus *rs, |
michael@0 | 277 | bool *success) |
michael@0 | 278 | { |
michael@0 | 279 | AutoSafeJSContext cx; |
michael@0 | 280 | JSAutoRequest request(cx); |
michael@0 | 281 | |
michael@0 | 282 | *success = false; |
michael@0 | 283 | |
michael@0 | 284 | RootedObject obj(cx, findObject(objId)); |
michael@0 | 285 | if (!obj) |
michael@0 | 286 | return false; |
michael@0 | 287 | |
michael@0 | 288 | JSAutoCompartment comp(cx, obj); |
michael@0 | 289 | |
michael@0 | 290 | RootedId internedId(cx); |
michael@0 | 291 | if (!convertGeckoStringToId(cx, id, &internedId)) |
michael@0 | 292 | return fail(cx, rs); |
michael@0 | 293 | |
michael@0 | 294 | if (!JS_DeletePropertyById2(cx, obj, internedId, success)) |
michael@0 | 295 | return fail(cx, rs); |
michael@0 | 296 | |
michael@0 | 297 | return ok(rs); |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | bool |
michael@0 | 301 | JavaScriptChild::AnswerHas(const ObjectId &objId, const nsString &id, ReturnStatus *rs, bool *bp) |
michael@0 | 302 | { |
michael@0 | 303 | AutoSafeJSContext cx; |
michael@0 | 304 | JSAutoRequest request(cx); |
michael@0 | 305 | |
michael@0 | 306 | *bp = false; |
michael@0 | 307 | |
michael@0 | 308 | RootedObject obj(cx, findObject(objId)); |
michael@0 | 309 | if (!obj) |
michael@0 | 310 | return false; |
michael@0 | 311 | |
michael@0 | 312 | JSAutoCompartment comp(cx, obj); |
michael@0 | 313 | |
michael@0 | 314 | RootedId internedId(cx); |
michael@0 | 315 | if (!convertGeckoStringToId(cx, id, &internedId)) |
michael@0 | 316 | return fail(cx, rs); |
michael@0 | 317 | |
michael@0 | 318 | bool found; |
michael@0 | 319 | if (!JS_HasPropertyById(cx, obj, internedId, &found)) |
michael@0 | 320 | return fail(cx, rs); |
michael@0 | 321 | *bp = !!found; |
michael@0 | 322 | |
michael@0 | 323 | return ok(rs); |
michael@0 | 324 | } |
michael@0 | 325 | |
michael@0 | 326 | bool |
michael@0 | 327 | JavaScriptChild::AnswerHasOwn(const ObjectId &objId, const nsString &id, ReturnStatus *rs, bool *bp) |
michael@0 | 328 | { |
michael@0 | 329 | AutoSafeJSContext cx; |
michael@0 | 330 | JSAutoRequest request(cx); |
michael@0 | 331 | |
michael@0 | 332 | *bp = false; |
michael@0 | 333 | |
michael@0 | 334 | RootedObject obj(cx, findObject(objId)); |
michael@0 | 335 | if (!obj) |
michael@0 | 336 | return false; |
michael@0 | 337 | |
michael@0 | 338 | JSAutoCompartment comp(cx, obj); |
michael@0 | 339 | |
michael@0 | 340 | RootedId internedId(cx); |
michael@0 | 341 | if (!convertGeckoStringToId(cx, id, &internedId)) |
michael@0 | 342 | return fail(cx, rs); |
michael@0 | 343 | |
michael@0 | 344 | Rooted<JSPropertyDescriptor> desc(cx); |
michael@0 | 345 | if (!JS_GetPropertyDescriptorById(cx, obj, internedId, &desc)) |
michael@0 | 346 | return fail(cx, rs); |
michael@0 | 347 | *bp = (desc.object() == obj); |
michael@0 | 348 | |
michael@0 | 349 | return ok(rs); |
michael@0 | 350 | } |
michael@0 | 351 | |
michael@0 | 352 | bool |
michael@0 | 353 | JavaScriptChild::AnswerGet(const ObjectId &objId, const ObjectId &receiverId, const nsString &id, |
michael@0 | 354 | ReturnStatus *rs, JSVariant *result) |
michael@0 | 355 | { |
michael@0 | 356 | AutoSafeJSContext cx; |
michael@0 | 357 | JSAutoRequest request(cx); |
michael@0 | 358 | |
michael@0 | 359 | // The outparam will be written to the buffer, so it must be set even if |
michael@0 | 360 | // the parent won't read it. |
michael@0 | 361 | *result = void_t(); |
michael@0 | 362 | |
michael@0 | 363 | RootedObject obj(cx, findObject(objId)); |
michael@0 | 364 | if (!obj) |
michael@0 | 365 | return false; |
michael@0 | 366 | |
michael@0 | 367 | RootedObject receiver(cx, findObject(receiverId)); |
michael@0 | 368 | if (!receiver) |
michael@0 | 369 | return false; |
michael@0 | 370 | |
michael@0 | 371 | JSAutoCompartment comp(cx, obj); |
michael@0 | 372 | |
michael@0 | 373 | RootedId internedId(cx); |
michael@0 | 374 | if (!convertGeckoStringToId(cx, id, &internedId)) |
michael@0 | 375 | return fail(cx, rs); |
michael@0 | 376 | |
michael@0 | 377 | JS::RootedValue val(cx); |
michael@0 | 378 | if (!JS_ForwardGetPropertyTo(cx, obj, internedId, receiver, &val)) |
michael@0 | 379 | return fail(cx, rs); |
michael@0 | 380 | |
michael@0 | 381 | if (!toVariant(cx, val, result)) |
michael@0 | 382 | return fail(cx, rs); |
michael@0 | 383 | |
michael@0 | 384 | return ok(rs); |
michael@0 | 385 | } |
michael@0 | 386 | |
michael@0 | 387 | bool |
michael@0 | 388 | JavaScriptChild::AnswerSet(const ObjectId &objId, const ObjectId &receiverId, const nsString &id, |
michael@0 | 389 | const bool &strict, const JSVariant &value, ReturnStatus *rs, |
michael@0 | 390 | JSVariant *result) |
michael@0 | 391 | { |
michael@0 | 392 | AutoSafeJSContext cx; |
michael@0 | 393 | JSAutoRequest request(cx); |
michael@0 | 394 | |
michael@0 | 395 | // The outparam will be written to the buffer, so it must be set even if |
michael@0 | 396 | // the parent won't read it. |
michael@0 | 397 | *result = void_t(); |
michael@0 | 398 | |
michael@0 | 399 | RootedObject obj(cx, findObject(objId)); |
michael@0 | 400 | if (!obj) |
michael@0 | 401 | return false; |
michael@0 | 402 | |
michael@0 | 403 | RootedObject receiver(cx, findObject(receiverId)); |
michael@0 | 404 | if (!receiver) |
michael@0 | 405 | return false; |
michael@0 | 406 | |
michael@0 | 407 | JSAutoCompartment comp(cx, obj); |
michael@0 | 408 | |
michael@0 | 409 | RootedId internedId(cx); |
michael@0 | 410 | if (!convertGeckoStringToId(cx, id, &internedId)) |
michael@0 | 411 | return fail(cx, rs); |
michael@0 | 412 | |
michael@0 | 413 | MOZ_ASSERT(obj == receiver); |
michael@0 | 414 | |
michael@0 | 415 | RootedValue val(cx); |
michael@0 | 416 | if (!toValue(cx, value, &val)) |
michael@0 | 417 | return fail(cx, rs); |
michael@0 | 418 | |
michael@0 | 419 | if (!JS_SetPropertyById(cx, obj, internedId, val)) |
michael@0 | 420 | return fail(cx, rs); |
michael@0 | 421 | |
michael@0 | 422 | if (!toVariant(cx, val, result)) |
michael@0 | 423 | return fail(cx, rs); |
michael@0 | 424 | |
michael@0 | 425 | return ok(rs); |
michael@0 | 426 | } |
michael@0 | 427 | |
michael@0 | 428 | bool |
michael@0 | 429 | JavaScriptChild::AnswerIsExtensible(const ObjectId &objId, ReturnStatus *rs, bool *result) |
michael@0 | 430 | { |
michael@0 | 431 | AutoSafeJSContext cx; |
michael@0 | 432 | JSAutoRequest request(cx); |
michael@0 | 433 | |
michael@0 | 434 | *result = false; |
michael@0 | 435 | |
michael@0 | 436 | RootedObject obj(cx, findObject(objId)); |
michael@0 | 437 | if (!obj) |
michael@0 | 438 | return false; |
michael@0 | 439 | |
michael@0 | 440 | JSAutoCompartment comp(cx, obj); |
michael@0 | 441 | |
michael@0 | 442 | bool extensible; |
michael@0 | 443 | if (!JS_IsExtensible(cx, obj, &extensible)) |
michael@0 | 444 | return fail(cx, rs); |
michael@0 | 445 | |
michael@0 | 446 | *result = !!extensible; |
michael@0 | 447 | return ok(rs); |
michael@0 | 448 | } |
michael@0 | 449 | |
michael@0 | 450 | bool |
michael@0 | 451 | JavaScriptChild::AnswerCall(const ObjectId &objId, const nsTArray<JSParam> &argv, ReturnStatus *rs, |
michael@0 | 452 | JSVariant *result, nsTArray<JSParam> *outparams) |
michael@0 | 453 | { |
michael@0 | 454 | AutoSafeJSContext cx; |
michael@0 | 455 | JSAutoRequest request(cx); |
michael@0 | 456 | |
michael@0 | 457 | // The outparam will be written to the buffer, so it must be set even if |
michael@0 | 458 | // the parent won't read it. |
michael@0 | 459 | *result = void_t(); |
michael@0 | 460 | |
michael@0 | 461 | RootedObject obj(cx, findObject(objId)); |
michael@0 | 462 | if (!obj) |
michael@0 | 463 | return false; |
michael@0 | 464 | |
michael@0 | 465 | MOZ_ASSERT(argv.Length() >= 2); |
michael@0 | 466 | |
michael@0 | 467 | RootedValue objv(cx); |
michael@0 | 468 | if (!toValue(cx, argv[0], &objv)) |
michael@0 | 469 | return fail(cx, rs); |
michael@0 | 470 | |
michael@0 | 471 | JSAutoCompartment comp(cx, &objv.toObject()); |
michael@0 | 472 | |
michael@0 | 473 | *result = JSVariant(void_t()); |
michael@0 | 474 | |
michael@0 | 475 | AutoValueVector vals(cx); |
michael@0 | 476 | AutoValueVector outobjects(cx); |
michael@0 | 477 | for (size_t i = 0; i < argv.Length(); i++) { |
michael@0 | 478 | if (argv[i].type() == JSParam::Tvoid_t) { |
michael@0 | 479 | // This is an outparam. |
michael@0 | 480 | JSCompartment *compartment = js::GetContextCompartment(cx); |
michael@0 | 481 | RootedObject global(cx, JS_GetGlobalForCompartmentOrNull(cx, compartment)); |
michael@0 | 482 | RootedObject obj(cx, xpc::NewOutObject(cx, global)); |
michael@0 | 483 | if (!obj) |
michael@0 | 484 | return fail(cx, rs); |
michael@0 | 485 | if (!outobjects.append(ObjectValue(*obj))) |
michael@0 | 486 | return fail(cx, rs); |
michael@0 | 487 | if (!vals.append(ObjectValue(*obj))) |
michael@0 | 488 | return fail(cx, rs); |
michael@0 | 489 | } else { |
michael@0 | 490 | RootedValue v(cx); |
michael@0 | 491 | if (!toValue(cx, argv[i].get_JSVariant(), &v)) |
michael@0 | 492 | return fail(cx, rs); |
michael@0 | 493 | if (!vals.append(v)) |
michael@0 | 494 | return fail(cx, rs); |
michael@0 | 495 | } |
michael@0 | 496 | } |
michael@0 | 497 | |
michael@0 | 498 | RootedValue rval(cx); |
michael@0 | 499 | { |
michael@0 | 500 | AutoSaveContextOptions asco(cx); |
michael@0 | 501 | ContextOptionsRef(cx).setDontReportUncaught(true); |
michael@0 | 502 | |
michael@0 | 503 | HandleValueArray args = HandleValueArray::subarray(vals, 2, vals.length() - 2); |
michael@0 | 504 | bool success = JS::Call(cx, vals.handleAt(1), vals.handleAt(0), args, &rval); |
michael@0 | 505 | if (!success) |
michael@0 | 506 | return fail(cx, rs); |
michael@0 | 507 | } |
michael@0 | 508 | |
michael@0 | 509 | if (!toVariant(cx, rval, result)) |
michael@0 | 510 | return fail(cx, rs); |
michael@0 | 511 | |
michael@0 | 512 | // Prefill everything with a dummy jsval. |
michael@0 | 513 | for (size_t i = 0; i < outobjects.length(); i++) |
michael@0 | 514 | outparams->AppendElement(JSParam(void_t())); |
michael@0 | 515 | |
michael@0 | 516 | // Go through each argument that was an outparam, retrieve the "value" |
michael@0 | 517 | // field, and add it to a temporary list. We need to do this separately |
michael@0 | 518 | // because the outparams vector is not rooted. |
michael@0 | 519 | vals.clear(); |
michael@0 | 520 | for (size_t i = 0; i < outobjects.length(); i++) { |
michael@0 | 521 | RootedObject obj(cx, &outobjects[i].toObject()); |
michael@0 | 522 | |
michael@0 | 523 | RootedValue v(cx); |
michael@0 | 524 | bool found; |
michael@0 | 525 | if (JS_HasProperty(cx, obj, "value", &found)) { |
michael@0 | 526 | if (!JS_GetProperty(cx, obj, "value", &v)) |
michael@0 | 527 | return fail(cx, rs); |
michael@0 | 528 | } else { |
michael@0 | 529 | v = UndefinedValue(); |
michael@0 | 530 | } |
michael@0 | 531 | if (!vals.append(v)) |
michael@0 | 532 | return fail(cx, rs); |
michael@0 | 533 | } |
michael@0 | 534 | |
michael@0 | 535 | // Copy the outparams. If any outparam is already set to a void_t, we |
michael@0 | 536 | // treat this as the outparam never having been set. |
michael@0 | 537 | for (size_t i = 0; i < vals.length(); i++) { |
michael@0 | 538 | JSVariant variant; |
michael@0 | 539 | if (!toVariant(cx, vals.handleAt(i), &variant)) |
michael@0 | 540 | return fail(cx, rs); |
michael@0 | 541 | outparams->ReplaceElementAt(i, JSParam(variant)); |
michael@0 | 542 | } |
michael@0 | 543 | |
michael@0 | 544 | return ok(rs); |
michael@0 | 545 | } |
michael@0 | 546 | |
michael@0 | 547 | bool |
michael@0 | 548 | JavaScriptChild::AnswerObjectClassIs(const ObjectId &objId, const uint32_t &classValue, |
michael@0 | 549 | bool *result) |
michael@0 | 550 | { |
michael@0 | 551 | AutoSafeJSContext cx; |
michael@0 | 552 | JSAutoRequest request(cx); |
michael@0 | 553 | |
michael@0 | 554 | RootedObject obj(cx, findObject(objId)); |
michael@0 | 555 | if (!obj) |
michael@0 | 556 | return false; |
michael@0 | 557 | |
michael@0 | 558 | JSAutoCompartment comp(cx, obj); |
michael@0 | 559 | |
michael@0 | 560 | *result = js_ObjectClassIs(cx, obj, (js::ESClassValue)classValue); |
michael@0 | 561 | return true; |
michael@0 | 562 | } |
michael@0 | 563 | |
michael@0 | 564 | bool |
michael@0 | 565 | JavaScriptChild::AnswerClassName(const ObjectId &objId, nsString *name) |
michael@0 | 566 | { |
michael@0 | 567 | AutoSafeJSContext cx; |
michael@0 | 568 | JSAutoRequest request(cx); |
michael@0 | 569 | |
michael@0 | 570 | RootedObject obj(cx, findObject(objId)); |
michael@0 | 571 | if (!obj) |
michael@0 | 572 | return false; |
michael@0 | 573 | |
michael@0 | 574 | JSAutoCompartment comp(cx, obj); |
michael@0 | 575 | |
michael@0 | 576 | *name = NS_ConvertASCIItoUTF16(js_ObjectClassName(cx, obj)); |
michael@0 | 577 | return true; |
michael@0 | 578 | } |
michael@0 | 579 | |
michael@0 | 580 | bool |
michael@0 | 581 | JavaScriptChild::AnswerGetPropertyNames(const ObjectId &objId, const uint32_t &flags, |
michael@0 | 582 | ReturnStatus *rs, nsTArray<nsString> *names) |
michael@0 | 583 | { |
michael@0 | 584 | AutoSafeJSContext cx; |
michael@0 | 585 | JSAutoRequest request(cx); |
michael@0 | 586 | |
michael@0 | 587 | RootedObject obj(cx, findObject(objId)); |
michael@0 | 588 | if (!obj) |
michael@0 | 589 | return false; |
michael@0 | 590 | |
michael@0 | 591 | JSAutoCompartment comp(cx, obj); |
michael@0 | 592 | |
michael@0 | 593 | AutoIdVector props(cx); |
michael@0 | 594 | if (!js::GetPropertyNames(cx, obj, flags, &props)) |
michael@0 | 595 | return fail(cx, rs); |
michael@0 | 596 | |
michael@0 | 597 | for (size_t i = 0; i < props.length(); i++) { |
michael@0 | 598 | nsString name; |
michael@0 | 599 | if (!convertIdToGeckoString(cx, props.handleAt(i), &name)) |
michael@0 | 600 | return fail(cx, rs); |
michael@0 | 601 | |
michael@0 | 602 | names->AppendElement(name); |
michael@0 | 603 | } |
michael@0 | 604 | |
michael@0 | 605 | return ok(rs); |
michael@0 | 606 | } |
michael@0 | 607 | |
michael@0 | 608 | bool |
michael@0 | 609 | JavaScriptChild::AnswerInstanceOf(const ObjectId &objId, const JSIID &iid, ReturnStatus *rs, |
michael@0 | 610 | bool *instanceof) |
michael@0 | 611 | { |
michael@0 | 612 | AutoSafeJSContext cx; |
michael@0 | 613 | JSAutoRequest request(cx); |
michael@0 | 614 | |
michael@0 | 615 | *instanceof = false; |
michael@0 | 616 | |
michael@0 | 617 | RootedObject obj(cx, findObject(objId)); |
michael@0 | 618 | if (!obj) |
michael@0 | 619 | return false; |
michael@0 | 620 | |
michael@0 | 621 | JSAutoCompartment comp(cx, obj); |
michael@0 | 622 | |
michael@0 | 623 | nsID nsiid; |
michael@0 | 624 | ConvertID(iid, &nsiid); |
michael@0 | 625 | |
michael@0 | 626 | nsresult rv = xpc::HasInstance(cx, obj, &nsiid, instanceof); |
michael@0 | 627 | if (rv != NS_OK) |
michael@0 | 628 | return fail(cx, rs); |
michael@0 | 629 | |
michael@0 | 630 | return ok(rs); |
michael@0 | 631 | } |
michael@0 | 632 | |
michael@0 | 633 | bool |
michael@0 | 634 | JavaScriptChild::AnswerDOMInstanceOf(const ObjectId &objId, const int &prototypeID, |
michael@0 | 635 | const int &depth, |
michael@0 | 636 | ReturnStatus *rs, bool *instanceof) |
michael@0 | 637 | { |
michael@0 | 638 | AutoSafeJSContext cx; |
michael@0 | 639 | JSAutoRequest request(cx); |
michael@0 | 640 | |
michael@0 | 641 | *instanceof = false; |
michael@0 | 642 | |
michael@0 | 643 | RootedObject obj(cx, findObject(objId)); |
michael@0 | 644 | if (!obj) |
michael@0 | 645 | return false; |
michael@0 | 646 | |
michael@0 | 647 | JSAutoCompartment comp(cx, obj); |
michael@0 | 648 | |
michael@0 | 649 | bool tmp; |
michael@0 | 650 | if (!mozilla::dom::InterfaceHasInstance(cx, prototypeID, depth, obj, &tmp)) |
michael@0 | 651 | return fail(cx, rs); |
michael@0 | 652 | *instanceof = tmp; |
michael@0 | 653 | |
michael@0 | 654 | return ok(rs); |
michael@0 | 655 | } |