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  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "js/Warnings.h"
8 #include "vm/Warnings.h"
9 
10 #include <stdarg.h>  // va_{list,start,end}
11 
12 #include "jsapi.h"    // js::AssertHeapIsIdle
13 #include "jstypes.h"  // JS_PUBLIC_API
14 
15 #include "js/friend/ErrorMessages.h"  // js::GetErrorMessage
16 #include "vm/ErrorReporting.h"        // IsWarning
17 #include "vm/JSContext.h"  // js::ArgumentsAre{ASCII,Latin1,UTF8}, js::ReportError{Number}VA
18 
19 using js::ArgumentsAreASCII;
20 using js::ArgumentsAreLatin1;
21 using js::ArgumentsAreUTF8;
22 using js::AssertHeapIsIdle;
23 using js::GetErrorMessage;
24 using js::IsWarning;
25 using js::ReportErrorVA;
26 
WarnASCII(JSContext * cx,const char * format,...)27 JS_PUBLIC_API bool JS::WarnASCII(JSContext* cx, const char* format, ...) {
28   va_list ap;
29   bool ok;
30 
31   AssertHeapIsIdle();
32   va_start(ap, format);
33   ok = ReportErrorVA(cx, IsWarning::Yes, format, ArgumentsAreASCII, ap);
34   va_end(ap);
35   return ok;
36 }
37 
WarnLatin1(JSContext * cx,const char * format,...)38 JS_PUBLIC_API bool JS::WarnLatin1(JSContext* cx, const char* format, ...) {
39   va_list ap;
40   bool ok;
41 
42   AssertHeapIsIdle();
43   va_start(ap, format);
44   ok = ReportErrorVA(cx, IsWarning::Yes, format, ArgumentsAreLatin1, ap);
45   va_end(ap);
46   return ok;
47 }
48 
WarnUTF8(JSContext * cx,const char * format,...)49 JS_PUBLIC_API bool JS::WarnUTF8(JSContext* cx, const char* format, ...) {
50   va_list ap;
51   bool ok;
52 
53   AssertHeapIsIdle();
54   va_start(ap, format);
55   ok = ReportErrorVA(cx, IsWarning::Yes, format, ArgumentsAreUTF8, ap);
56   va_end(ap);
57   return ok;
58 }
59 
GetWarningReporter(JSContext * cx)60 JS_PUBLIC_API JS::WarningReporter JS::GetWarningReporter(JSContext* cx) {
61   return cx->runtime()->warningReporter;
62 }
63 
SetWarningReporter(JSContext * cx,WarningReporter reporter)64 JS_PUBLIC_API JS::WarningReporter JS::SetWarningReporter(
65     JSContext* cx, WarningReporter reporter) {
66   WarningReporter older = cx->runtime()->warningReporter;
67   cx->runtime()->warningReporter = reporter;
68   return older;
69 }
70 
WarnNumberASCII(JSContext * cx,const unsigned errorNumber,...)71 bool js::WarnNumberASCII(JSContext* cx, const unsigned errorNumber, ...) {
72   va_list ap;
73   va_start(ap, errorNumber);
74   bool ok = ReportErrorNumberVA(cx, IsWarning::Yes, GetErrorMessage, nullptr,
75                                 errorNumber, ArgumentsAreASCII, ap);
76   va_end(ap);
77   return ok;
78 }
79 
WarnNumberLatin1(JSContext * cx,const unsigned errorNumber,...)80 bool js::WarnNumberLatin1(JSContext* cx, const unsigned errorNumber, ...) {
81   va_list ap;
82   va_start(ap, errorNumber);
83   bool ok = ReportErrorNumberVA(cx, IsWarning::Yes, GetErrorMessage, nullptr,
84                                 errorNumber, ArgumentsAreLatin1, ap);
85   va_end(ap);
86   return ok;
87 }
88 
WarnNumberUTF8(JSContext * cx,const unsigned errorNumber,...)89 bool js::WarnNumberUTF8(JSContext* cx, const unsigned errorNumber, ...) {
90   va_list ap;
91   va_start(ap, errorNumber);
92   bool ok = ReportErrorNumberVA(cx, IsWarning::Yes, GetErrorMessage, nullptr,
93                                 errorNumber, ArgumentsAreUTF8, ap);
94   va_end(ap);
95   return ok;
96 }
97 
WarnNumberUC(JSContext * cx,const unsigned errorNumber,...)98 bool js::WarnNumberUC(JSContext* cx, const unsigned errorNumber, ...) {
99   va_list ap;
100   va_start(ap, errorNumber);
101   bool ok = ReportErrorNumberVA(cx, IsWarning::Yes, GetErrorMessage, nullptr,
102                                 errorNumber, ArgumentsAreUnicode, ap);
103   va_end(ap);
104   return ok;
105 }
106