Debian Codegraph @

js/src/jsapi-tests/testException.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7
8 #include "js/CallAndConstruct.h" // JS_CallFunctionValue
9 #include "js/ColumnNumber.h" // JS::ColumnNumberOneOrigin
10 #include "js/PropertyAndElement.h" // JS_GetProperty
11 #include "jsapi-tests/tests.h"
12
13 BEGIN_TEST(testException_bug860435) {
14 JS::RootedValue fun(cx);
15
16 EVAL("ReferenceError", &fun);
17 CHECK(fun.isObject());
18
19 JS::RootedValue v(cx);
20 CHECK(
21 JS_CallFunctionValue(cx, global, fun, JS::HandleValueArray::empty(), &v));
22 CHECK(v.isObject());
23 JS::RootedObject obj(cx, &v.toObject());
24
25 CHECK(JS_GetProperty(cx, obj, "stack", &v));
26 CHECK(v.isString());
27 return true;
28 }
29 END_TEST(testException_bug860435)
30
31 BEGIN_TEST(testException_getCause) {
32 JS::RootedValue err(cx);
33 EVAL("new Error('message', { cause: new Error('message 2') })", &err);
34 CHECK(err.isObject());
35
36 JS::RootedString msg(cx, JS::ToString(cx, err));
37 CHECK(msg);
38 // Check that we have the outer error
39 bool match;
40 CHECK(JS_StringEqualsLiteral(cx, msg, "Error: message", &match));
41 CHECK(match);
42
43 JS::Rooted<mozilla::Maybe<JS::Value>> maybeCause(
44 cx, JS::GetExceptionCause(&err.toObject()));
45 CHECK(maybeCause.isSome());
46 JS::RootedValue cause(cx, *maybeCause);
47 CHECK(cause.isObject());
48
49 msg = JS::ToString(cx, cause);
50 CHECK(msg);
51 // Check that we have the inner error
52 CHECK(JS_StringEqualsLiteral(cx, msg, "Error: message 2", &match));
53 CHECK(match);
54
55 maybeCause = JS::GetExceptionCause(&cause.toObject());
56 CHECK(maybeCause.isNothing());
57
58 return true;
59 }
60 END_TEST(testException_getCause)
61
62 BEGIN_TEST(testException_getCausePlainObject) {
63 JS::RootedObject plain(cx, JS_NewPlainObject(cx));
64 CHECK(plain);
65 JS::Rooted<mozilla::Maybe<JS::Value>> maybeCause(
66 cx, JS::GetExceptionCause(plain));
67 CHECK(maybeCause.isNothing());
68 return true;
69 }
70 END_TEST(testException_getCausePlainObject)
71
72 BEGIN_TEST(testException_createErrorWithCause) {
73 JS::RootedString empty(cx, JS_GetEmptyString(cx));
74 JS::Rooted<mozilla::Maybe<JS::Value>> cause(
75 cx, mozilla::Some(JS::Int32Value(-1)));
76 JS::RootedValue err(cx);
77 CHECK(JS::CreateError(cx, JSEXN_ERR, nullptr, empty, 1,
78 JS::ColumnNumberOneOrigin(1), nullptr, empty, cause,
79 &err));
80 CHECK(err.isObject());
81 JS::Rooted<mozilla::Maybe<JS::Value>> maybeCause(
82 cx, JS::GetExceptionCause(&err.toObject()));
83 CHECK(maybeCause.isSome());
84 CHECK_SAME(*cause, *maybeCause);
85
86 CHECK(JS::CreateError(cx, JSEXN_ERR, nullptr, empty, 1,
87 JS::ColumnNumberOneOrigin(1), nullptr, empty,
88 JS::NothingHandleValue, &err));
89 CHECK(err.isObject());
90 maybeCause = JS::GetExceptionCause(&err.toObject());
91 CHECK(maybeCause.isNothing());
92
93 return true;
94 }
95 END_TEST(testException_createErrorWithCause)