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());
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());
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
);
59 args
.rval().setObject(*obj
);
61 MOZ_RELEASE_ASSERT(args
.isConstructing());
63 args
.rval().setUndefined();
65 MOZ_RELEASE_ASSERT(!args
.isConstructing());
71 BEGIN_TEST(testCallArgs_isConstructing_constructor
) {
72 CHECK(JS_DefineFunction(cx
, global
, "customConstructor", CustomConstructor
, 0,
75 JS::RootedValue
result(cx
);
77 EVAL("new customConstructor();", &result
);
78 CHECK(result
.isObject());
80 EVAL("customConstructor();", &result
);
81 CHECK(result
.isUndefined());
85 END_TEST(testCallArgs_isConstructing_constructor
)