Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / js / src / jsapi-tests / testCallNonGenericMethodOnProxy.cpp
blob54244ea768c0918600d77648071480e57a4c7aab
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/. */
5 #include "js/CallAndConstruct.h"
6 #include "js/GlobalObject.h"
7 #include "js/Object.h" // JS::GetClass, JS::GetReservedSlot
8 #include "jsapi-tests/tests.h"
10 using namespace JS;
12 static const JSClass CustomClass = {"CustomClass",
13 JSCLASS_HAS_RESERVED_SLOTS(1)};
15 static const uint32_t CUSTOM_SLOT = 0;
17 static bool IsCustomClass(JS::Handle<JS::Value> v) {
18 return v.isObject() && JS::GetClass(&v.toObject()) == &CustomClass;
21 static bool CustomMethodImpl(JSContext* cx, const CallArgs& args) {
22 MOZ_RELEASE_ASSERT(IsCustomClass(args.thisv()));
23 args.rval().set(JS::GetReservedSlot(&args.thisv().toObject(), CUSTOM_SLOT));
24 return true;
27 static bool CustomMethod(JSContext* cx, unsigned argc, Value* vp) {
28 CallArgs args = CallArgsFromVp(argc, vp);
29 return CallNonGenericMethod(cx, IsCustomClass, CustomMethodImpl, args);
32 BEGIN_TEST(test_CallNonGenericMethodOnProxy) {
33 // Create the first global object and compartment
34 JS::RealmOptions options;
35 JS::RootedObject globalA(
36 cx, JS_NewGlobalObject(cx, getGlobalClass(), nullptr,
37 JS::FireOnNewGlobalHook, options));
38 CHECK(globalA);
40 JS::RootedObject customA(cx, JS_NewObject(cx, &CustomClass));
41 CHECK(customA);
42 JS_SetReservedSlot(customA, CUSTOM_SLOT, Int32Value(17));
44 JS::RootedFunction customMethodA(
45 cx, JS_NewFunction(cx, CustomMethod, 0, 0, "customMethodA"));
46 CHECK(customMethodA);
48 JS::RootedValue rval(cx);
49 CHECK(JS_CallFunction(cx, customA, customMethodA,
50 JS::HandleValueArray::empty(), &rval));
51 CHECK_SAME(rval, Int32Value(17));
53 // Now create the second global object and compartment...
55 JS::RealmOptions options;
56 JS::RootedObject globalB(
57 cx, JS_NewGlobalObject(cx, getGlobalClass(), nullptr,
58 JS::FireOnNewGlobalHook, options));
59 CHECK(globalB);
61 // ...and enter it.
62 JSAutoRealm enter(cx, globalB);
63 JS::RootedObject customB(cx, JS_NewObject(cx, &CustomClass));
64 CHECK(customB);
65 JS_SetReservedSlot(customB, CUSTOM_SLOT, Int32Value(42));
67 JS::RootedFunction customMethodB(
68 cx, JS_NewFunction(cx, CustomMethod, 0, 0, "customMethodB"));
69 CHECK(customMethodB);
71 JS::RootedValue rval(cx);
72 CHECK(JS_CallFunction(cx, customB, customMethodB,
73 JS::HandleValueArray::empty(), &rval));
74 CHECK_SAME(rval, Int32Value(42));
76 JS::RootedObject wrappedCustomA(cx, customA);
77 CHECK(JS_WrapObject(cx, &wrappedCustomA));
79 JS::RootedValue rval2(cx);
80 CHECK(JS_CallFunction(cx, wrappedCustomA, customMethodB,
81 JS::HandleValueArray::empty(), &rval2));
82 CHECK_SAME(rval, Int32Value(42));
85 return true;
87 END_TEST(test_CallNonGenericMethodOnProxy)