Debian Codegraph @

js/src/jsapi-tests/testCallArgs.cpp

raw
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 "mozilla/Utf8.h" // mozilla::Utf8Unit
6
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"
11
12 static bool CustomNative(JSContext* cx, unsigned argc, JS::Value* vp) {
13 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
14
15 MOZ_RELEASE_ASSERT(!JS_IsExceptionPending(cx));
16
17 MOZ_RELEASE_ASSERT(!args.isConstructing());
18 args.rval().setUndefined();
19 MOZ_RELEASE_ASSERT(!args.isConstructing());
20
21 return true;
22 }
23
24 BEGIN_TEST(testCallArgs_isConstructing_native) {
25 CHECK(JS_DefineFunction(cx, global, "customNative", CustomNative, 0, 0));
26
27 JS::CompileOptions opts(cx);
28 opts.setFileAndLine(__FILE__, __LINE__ + 4);
29
30 JS::RootedValue result(cx);
31
32 static const char code[] = "new customNative();";
33 JS::SourceText<mozilla::Utf8Unit> srcBuf;
34 CHECK(srcBuf.init(cx, code, strlen(code), JS::SourceOwnership::Borrowed));
35
36 CHECK(!JS::Evaluate(cx, opts, srcBuf, &result));
37
38 CHECK(JS_IsExceptionPending(cx));
39 JS_ClearPendingException(cx);
40
41 EVAL("customNative();", &result);
42 CHECK(result.isUndefined());
43
44 return true;
45 }
46 END_TEST(testCallArgs_isConstructing_native)
47
48 static bool CustomConstructor(JSContext* cx, unsigned argc, JS::Value* vp) {
49 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
50
51 MOZ_RELEASE_ASSERT(!JS_IsExceptionPending(cx));
52
53 if (args.isConstructing()) {
54 JSObject* obj = JS_NewPlainObject(cx);
55 if (!obj) {
56 return false;
57 }
58
59 args.rval().setObject(*obj);
60
61 MOZ_RELEASE_ASSERT(args.isConstructing());
62 } else {
63 args.rval().setUndefined();
64
65 MOZ_RELEASE_ASSERT(!args.isConstructing());
66 }
67
68 return true;
69 }
70
71 BEGIN_TEST(testCallArgs_isConstructing_constructor) {
72 CHECK(JS_DefineFunction(cx, global, "customConstructor", CustomConstructor, 0,
73 JSFUN_CONSTRUCTOR));
74
75 JS::RootedValue result(cx);
76
77 EVAL("new customConstructor();", &result);
78 CHECK(result.isObject());
79
80 EVAL("customConstructor();", &result);
81 CHECK(result.isUndefined());
82
83 return true;
84 }
85 END_TEST(testCallArgs_isConstructing_constructor)