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