summaryrefslogtreecommitdiffstats
path: root/js/src/jsapi-tests/testException.cpp
blob: dbf5afbda10b7e4f4cee73a97f55237985edea62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "js/CallAndConstruct.h"    // JS_CallFunctionValue
#include "js/ColumnNumber.h"        // JS::ColumnNumberOneOrigin
#include "js/PropertyAndElement.h"  // JS_GetProperty
#include "jsapi-tests/tests.h"

BEGIN_TEST(testException_bug860435) {
  JS::RootedValue fun(cx);

  EVAL("ReferenceError", &fun);
  CHECK(fun.isObject());

  JS::RootedValue v(cx);
  CHECK(
      JS_CallFunctionValue(cx, global, fun, JS::HandleValueArray::empty(), &v));
  CHECK(v.isObject());
  JS::RootedObject obj(cx, &v.toObject());

  CHECK(JS_GetProperty(cx, obj, "stack", &v));
  CHECK(v.isString());
  return true;
}
END_TEST(testException_bug860435)

BEGIN_TEST(testException_getCause) {
  JS::RootedValue err(cx);
  EVAL("new Error('message', { cause: new Error('message 2') })", &err);
  CHECK(err.isObject());

  JS::RootedString msg(cx, JS::ToString(cx, err));
  CHECK(msg);
  // Check that we have the outer error
  bool match;
  CHECK(JS_StringEqualsLiteral(cx, msg, "Error: message", &match));
  CHECK(match);

  JS::Rooted<mozilla::Maybe<JS::Value>> maybeCause(
      cx, JS::GetExceptionCause(&err.toObject()));
  CHECK(maybeCause.isSome());
  JS::RootedValue cause(cx, *maybeCause);
  CHECK(cause.isObject());

  msg = JS::ToString(cx, cause);
  CHECK(msg);
  // Check that we have the inner error
  CHECK(JS_StringEqualsLiteral(cx, msg, "Error: message 2", &match));
  CHECK(match);

  maybeCause = JS::GetExceptionCause(&cause.toObject());
  CHECK(maybeCause.isNothing());

  return true;
}
END_TEST(testException_getCause)

BEGIN_TEST(testException_getCausePlainObject) {
  JS::RootedObject plain(cx, JS_NewPlainObject(cx));
  CHECK(plain);
  JS::Rooted<mozilla::Maybe<JS::Value>> maybeCause(
      cx, JS::GetExceptionCause(plain));
  CHECK(maybeCause.isNothing());
  return true;
}
END_TEST(testException_getCausePlainObject)

BEGIN_TEST(testException_createErrorWithCause) {
  JS::RootedString empty(cx, JS_GetEmptyString(cx));
  JS::Rooted<mozilla::Maybe<JS::Value>> cause(
      cx, mozilla::Some(JS::Int32Value(-1)));
  JS::RootedValue err(cx);
  CHECK(JS::CreateError(cx, JSEXN_ERR, nullptr, empty, 1,
                        JS::ColumnNumberOneOrigin(1), nullptr, empty, cause,
                        &err));
  CHECK(err.isObject());
  JS::Rooted<mozilla::Maybe<JS::Value>> maybeCause(
      cx, JS::GetExceptionCause(&err.toObject()));
  CHECK(maybeCause.isSome());
  CHECK_SAME(*cause, *maybeCause);

  CHECK(JS::CreateError(cx, JSEXN_ERR, nullptr, empty, 1,
                        JS::ColumnNumberOneOrigin(1), nullptr, empty,
                        JS::NothingHandleValue, &err));
  CHECK(err.isObject());
  maybeCause = JS::GetExceptionCause(&err.toObject());
  CHECK(maybeCause.isNothing());

  return true;
}
END_TEST(testException_createErrorWithCause)