summaryrefslogtreecommitdiffstats
path: root/js/src/vm/ErrorReporting.cpp
blob: fc974b0bc38de490fa89ae0c75cf73594fdf5d13 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * 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 "vm/ErrorReporting.h"

#include "mozilla/Move.h"

#include <stdarg.h>

#include "jscntxt.h"
#include "jsexn.h"

using mozilla::Move;

using JS::UniqueTwoByteChars;

void
CallWarningReporter(JSContext* cx, JSErrorReport* reportp)
{
    MOZ_ASSERT(reportp);
    MOZ_ASSERT(JSREPORT_IS_WARNING(reportp->flags));

    if (JS::WarningReporter warningReporter = cx->runtime()->warningReporter)
        warningReporter(cx, reportp);
}

void
CompileError::throwError(JSContext* cx)
{
    if (JSREPORT_IS_WARNING(flags)) {
        CallWarningReporter(cx, this);
        return;
    }

    // If there's a runtime exception type associated with this error
    // number, set that as the pending exception.  For errors occuring at
    // compile time, this is very likely to be a JSEXN_SYNTAXERR.
    //
    // If an exception is thrown but not caught, the JSREPORT_EXCEPTION
    // flag will be set in report.flags.  Proper behavior for an error
    // reporter is to ignore a report with this flag for all but top-level
    // compilation errors.  The exception will remain pending, and so long
    // as the non-top-level "load", "eval", or "compile" native function
    // returns false, the top-level reporter will eventually receive the
    // uncaught exception report.
    ErrorToException(cx, this, nullptr, nullptr);
}

bool
ReportCompileWarning(JSContext* cx, ErrorMetadata&& metadata, UniquePtr<JSErrorNotes> notes,
                         unsigned flags, unsigned errorNumber, va_list args)
{
    // On the main thread, report the error immediately. When compiling off
    // thread, save the error so that the thread finishing the parse can report
    // it later.
    CompileError tempErr;
    CompileError* err = &tempErr;
    if (!cx->isJSContext() && !cx->addPendingCompileError(&err)) {
        return false;
    }

    err->notes = Move(notes);
    err->flags = flags;
    err->errorNumber = errorNumber;

    err->filename = metadata.filename;
    err->lineno = metadata.lineNumber;
    err->column = metadata.columnNumber;
    err->isMuted = metadata.isMuted;

    if (UniqueTwoByteChars lineOfContext = Move(metadata.lineOfContext))
        err->initOwnedLinebuf(lineOfContext.release(), metadata.lineLength, metadata.tokenOffset);

    if (!ExpandErrorArgumentsVA(cx, GetErrorMessage, nullptr, errorNumber,
                                nullptr, ArgumentsAreLatin1, err, args))
    {
        return false;
    }

    if (cx->isJSContext()) {
        err->throwError(cx->asJSContext());
    }

    return true;
}

void
ReportCompileError(JSContext* cx, ErrorMetadata&& metadata, UniquePtr<JSErrorNotes> notes,
                       unsigned flags, unsigned errorNumber, va_list args)
{
    // On the main thread, report the error immediately. When compiling off
    // thread, save the error so that the thread finishing the parse can report
    // it later.
    CompileError tempErr;
    CompileError* err = &tempErr;
    if (!cx->isJSContext() && !cx->addPendingCompileError(&err)) {
        return;
    }

    err->notes = Move(notes);
    err->flags = flags;
    err->errorNumber = errorNumber;

    err->filename = metadata.filename;
    err->lineno = metadata.lineNumber;
    err->column = metadata.columnNumber;
    err->isMuted = metadata.isMuted;

    if (UniqueTwoByteChars lineOfContext = Move(metadata.lineOfContext))
        err->initOwnedLinebuf(lineOfContext.release(), metadata.lineLength, metadata.tokenOffset);

    if (!ExpandErrorArgumentsVA(cx, GetErrorMessage, nullptr, errorNumber,
                                nullptr, ArgumentsAreLatin1, err, args))
    {
        return;
    }

    if (cx->isJSContext()) {
        err->throwError(cx->asJSContext());
    }
}