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 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "jsapi-tests/tests.h" |
michael@0 | 6 | |
michael@0 | 7 | using namespace JS; |
michael@0 | 8 | |
michael@0 | 9 | static const JSClass CustomClass = { |
michael@0 | 10 | "CustomClass", |
michael@0 | 11 | JSCLASS_HAS_RESERVED_SLOTS(1), |
michael@0 | 12 | JS_PropertyStub, |
michael@0 | 13 | JS_DeletePropertyStub, |
michael@0 | 14 | JS_PropertyStub, |
michael@0 | 15 | JS_StrictPropertyStub, |
michael@0 | 16 | JS_EnumerateStub, |
michael@0 | 17 | JS_ResolveStub, |
michael@0 | 18 | JS_ConvertStub |
michael@0 | 19 | }; |
michael@0 | 20 | |
michael@0 | 21 | static const uint32_t CUSTOM_SLOT = 0; |
michael@0 | 22 | |
michael@0 | 23 | static bool |
michael@0 | 24 | IsCustomClass(JS::Handle<JS::Value> v) |
michael@0 | 25 | { |
michael@0 | 26 | return v.isObject() && JS_GetClass(&v.toObject()) == &CustomClass; |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | static bool |
michael@0 | 30 | CustomMethodImpl(JSContext *cx, CallArgs args) |
michael@0 | 31 | { |
michael@0 | 32 | JS_ASSERT(IsCustomClass(args.thisv())); |
michael@0 | 33 | args.rval().set(JS_GetReservedSlot(&args.thisv().toObject(), CUSTOM_SLOT)); |
michael@0 | 34 | return true; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | static bool |
michael@0 | 38 | CustomMethod(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 39 | { |
michael@0 | 40 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 41 | return CallNonGenericMethod(cx, IsCustomClass, CustomMethodImpl, args); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | BEGIN_TEST(test_CallNonGenericMethodOnProxy) |
michael@0 | 45 | { |
michael@0 | 46 | // Create the first global object and compartment |
michael@0 | 47 | JS::RootedObject globalA(cx, JS_NewGlobalObject(cx, getGlobalClass(), nullptr, JS::FireOnNewGlobalHook)); |
michael@0 | 48 | CHECK(globalA); |
michael@0 | 49 | |
michael@0 | 50 | JS::RootedObject customA(cx, JS_NewObject(cx, &CustomClass, JS::NullPtr(), JS::NullPtr())); |
michael@0 | 51 | CHECK(customA); |
michael@0 | 52 | JS_SetReservedSlot(customA, CUSTOM_SLOT, Int32Value(17)); |
michael@0 | 53 | |
michael@0 | 54 | JS::RootedFunction customMethodA(cx, JS_NewFunction(cx, CustomMethod, 0, 0, |
michael@0 | 55 | customA, "customMethodA")); |
michael@0 | 56 | CHECK(customMethodA); |
michael@0 | 57 | |
michael@0 | 58 | JS::RootedValue rval(cx); |
michael@0 | 59 | CHECK(JS_CallFunction(cx, customA, customMethodA, JS::HandleValueArray::empty(), |
michael@0 | 60 | &rval)); |
michael@0 | 61 | CHECK_SAME(rval, Int32Value(17)); |
michael@0 | 62 | |
michael@0 | 63 | // Now create the second global object and compartment... |
michael@0 | 64 | { |
michael@0 | 65 | JS::RootedObject globalB(cx, JS_NewGlobalObject(cx, getGlobalClass(), nullptr, JS::FireOnNewGlobalHook)); |
michael@0 | 66 | CHECK(globalB); |
michael@0 | 67 | |
michael@0 | 68 | // ...and enter it. |
michael@0 | 69 | JSAutoCompartment enter(cx, globalB); |
michael@0 | 70 | JS::RootedObject customB(cx, JS_NewObject(cx, &CustomClass, JS::NullPtr(), JS::NullPtr())); |
michael@0 | 71 | CHECK(customB); |
michael@0 | 72 | JS_SetReservedSlot(customB, CUSTOM_SLOT, Int32Value(42)); |
michael@0 | 73 | |
michael@0 | 74 | JS::RootedFunction customMethodB(cx, JS_NewFunction(cx, CustomMethod, 0, 0, customB, "customMethodB")); |
michael@0 | 75 | CHECK(customMethodB); |
michael@0 | 76 | |
michael@0 | 77 | JS::RootedValue rval(cx); |
michael@0 | 78 | CHECK(JS_CallFunction(cx, customB, customMethodB, JS::HandleValueArray::empty(), |
michael@0 | 79 | &rval)); |
michael@0 | 80 | CHECK_SAME(rval, Int32Value(42)); |
michael@0 | 81 | |
michael@0 | 82 | JS::RootedObject wrappedCustomA(cx, customA); |
michael@0 | 83 | CHECK(JS_WrapObject(cx, &wrappedCustomA)); |
michael@0 | 84 | |
michael@0 | 85 | JS::RootedValue rval2(cx); |
michael@0 | 86 | CHECK(JS_CallFunction(cx, wrappedCustomA, customMethodB, JS::HandleValueArray::empty(), |
michael@0 | 87 | &rval2)); |
michael@0 | 88 | CHECK_SAME(rval, Int32Value(42)); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | return true; |
michael@0 | 92 | } |
michael@0 | 93 | END_TEST(test_CallNonGenericMethodOnProxy) |