Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / js / src / jsapi-tests / testCallArgs.cpp
blob07a1a83cfc86019e31c6d66831acece71085d907
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 "mozilla/Utf8.h" // mozilla::Utf8Unit
7 #include "js/CompilationAndEvaluation.h" // JS::Evaluate
8 #include "js/PropertyAndElement.h" // JS_DefineFunction
9 #include "js/SourceText.h" // JS::Source{Ownership,Text}
10 #include "jsapi-tests/tests.h"
12 static bool CustomNative(JSContext* cx, unsigned argc, JS::Value* vp) {
13 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
15 MOZ_RELEASE_ASSERT(!JS_IsExceptionPending(cx));
17 MOZ_RELEASE_ASSERT(!args.isConstructing());
18 args.rval().setUndefined();
19 MOZ_RELEASE_ASSERT(!args.isConstructing());
21 return true;
24 BEGIN_TEST(testCallArgs_isConstructing_native) {
25 CHECK(JS_DefineFunction(cx, global, "customNative", CustomNative, 0, 0));
27 JS::CompileOptions opts(cx);
28 opts.setFileAndLine(__FILE__, __LINE__ + 4);
30 JS::RootedValue result(cx);
32 static const char code[] = "new customNative();";
33 JS::SourceText<mozilla::Utf8Unit> srcBuf;
34 CHECK(srcBuf.init(cx, code, strlen(code), JS::SourceOwnership::Borrowed));
36 CHECK(!JS::Evaluate(cx, opts, srcBuf, &result));
38 CHECK(JS_IsExceptionPending(cx));
39 JS_ClearPendingException(cx);
41 EVAL("customNative();", &result);
42 CHECK(result.isUndefined());
44 return true;
46 END_TEST(testCallArgs_isConstructing_native)
48 static bool CustomConstructor(JSContext* cx, unsigned argc, JS::Value* vp) {
49 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
51 MOZ_RELEASE_ASSERT(!JS_IsExceptionPending(cx));
53 if (args.isConstructing()) {
54 JSObject* obj = JS_NewPlainObject(cx);
55 if (!obj) {
56 return false;
59 args.rval().setObject(*obj);
61 MOZ_RELEASE_ASSERT(args.isConstructing());
62 } else {
63 args.rval().setUndefined();
65 MOZ_RELEASE_ASSERT(!args.isConstructing());
68 return true;
71 BEGIN_TEST(testCallArgs_isConstructing_constructor) {
72 CHECK(JS_DefineFunction(cx, global, "customConstructor", CustomConstructor, 0,
73 JSFUN_CONSTRUCTOR));
75 JS::RootedValue result(cx);
77 EVAL("new customConstructor();", &result);
78 CHECK(result.isObject());
80 EVAL("customConstructor();", &result);
81 CHECK(result.isUndefined());
83 return true;
85 END_TEST(testCallArgs_isConstructing_constructor)