Bug 1542120 - Add js/public/Warnings.h for warning-related JSAPI. r=arai

Differential Revision: https://phabricator.services.mozilla.com/D26273

--HG--
rename : js/src/jsapi.h => js/public/Warnings.h
extra : moz-landing-system : lando
This commit is contained in:
Jeff Walden 2019-04-05 22:52:04 +00:00
parent 512a56bf3c
commit 5327fe7f3e
23 changed files with 208 additions and 117 deletions

View File

@ -73,6 +73,7 @@
// Helper Classes
#include "nsJSUtils.h"
#include "jsapi.h"
#include "js/Warnings.h" // JS::WarnASCII
#include "js/Wrapper.h"
#include "nsCharSeparatedTokenizer.h"
#include "nsReadableUtils.h"
@ -4549,7 +4550,7 @@ nsGlobalWindowInner::ShowSlowScriptDialog(JSContext* aCx,
// (since that spins the event loop). In that (rare) case, we just kill the
// script and report a warning.
if (!nsContentUtils::IsSafeToRunScript()) {
JS_ReportWarningASCII(aCx, "A long running script was terminated");
JS::WarnASCII(aCx, "A long running script was terminated");
return KillSlowScript;
}

View File

@ -70,6 +70,7 @@
#include "jsfriendapi.h"
#include "js/Conversions.h"
#include "js/HeapAPI.h"
#include "js/Warnings.h" // JS::WarnASCII
#include "mozilla/Alignment.h"
#include "mozilla/Assertions.h"
@ -2023,9 +2024,9 @@ already_AddRefed<CanvasPattern> CanvasRenderingContext2D::CreatePattern(
if (!srcSurf) {
JSContext* context = nsContentUtils::GetCurrentJSContext();
if (context) {
JS_ReportWarningASCII(context,
"CanvasRenderingContext2D.createPattern()"
" failed to snapshot source canvas.");
JS::WarnASCII(context,
"CanvasRenderingContext2D.createPattern() failed to "
"snapshot source canvas.");
}
aError.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
return nullptr;
@ -2070,9 +2071,9 @@ already_AddRefed<CanvasPattern> CanvasRenderingContext2D::CreatePattern(
if (!srcSurf) {
JSContext* context = nsContentUtils::GetCurrentJSContext();
if (context) {
JS_ReportWarningASCII(context,
"CanvasRenderingContext2D.createPattern()"
" failed to prepare source ImageBitmap.");
JS::WarnASCII(context,
"CanvasRenderingContext2D.createPattern() failed to "
"prepare source ImageBitmap.");
}
aError.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
return nullptr;

View File

@ -8,6 +8,7 @@
#include "GLContext.h"
#include "jsapi.h"
#include "js/Warnings.h" // JS::WarnASCII
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/gfx/Logging.h"
#include "mozilla/Preferences.h"
@ -68,7 +69,7 @@ void WebGLContext::GenerateWarning(const char* fmt, va_list ap) const {
char buf[1024];
VsprintfLiteral(buf, fmt, ap);
// no need to print to stderr, as JS_ReportWarning takes care of this for us.
// JS::WarnASCII will print to stderr for us.
if (!mCanvasElement) {
return;
@ -81,13 +82,12 @@ void WebGLContext::GenerateWarning(const char* fmt, va_list ap) const {
JSContext* cx = api.cx();
const auto funcName = FuncName();
JS_ReportWarningASCII(cx, "WebGL warning: %s: %s", funcName, buf);
JS::WarnASCII(cx, "WebGL warning: %s: %s", funcName, buf);
if (!ShouldGenerateWarnings()) {
JS_ReportWarningASCII(cx,
"WebGL: No further warnings will be reported for"
" this WebGL context."
" (already reported %d warnings)",
mAlreadyGeneratedWarnings);
JS::WarnASCII(cx,
"WebGL: No further warnings will be reported for this WebGL "
"context. (already reported %d warnings)",
mAlreadyGeneratedWarnings);
}
}
@ -119,15 +119,14 @@ void WebGLContext::GeneratePerfWarning(const char* fmt, ...) const {
////
const auto funcName = FuncName();
JS_ReportWarningASCII(cx, "WebGL perf warning: %s: %s", funcName, buf);
JS::WarnASCII(cx, "WebGL perf warning: %s: %s", funcName, buf);
mNumPerfWarnings++;
if (!ShouldGeneratePerfWarnings()) {
JS_ReportWarningASCII(
cx,
"WebGL: After reporting %u, no further perf warnings will"
" be reported for this WebGL context.",
uint32_t(mNumPerfWarnings));
JS::WarnASCII(cx,
"WebGL: After reporting %u, no further perf warnings will be "
"reported for this WebGL context.",
uint32_t(mNumPerfWarnings));
}
}

View File

@ -13,6 +13,7 @@
#include "nsContentUtils.h"
#include "nsIScriptError.h"
#include "jsapi.h"
#include "js/Warnings.h" // JS::WarnASCII
namespace mozilla {
namespace dom {
@ -55,9 +56,9 @@ void MediaError::GetMessage(nsAString& aResult) const {
if (api.Init(ownerDoc->GetScopeObject())) {
// We prefer this API because it can also print to our debug log and
// try server's log viewer.
JS_ReportWarningASCII(api.cx(), "%s", message.get());
JS::WarnASCII(api.cx(), "%s", message.get());
} else {
// If failed to use JS_ReportWarningASCII, fall back to
// If failed to use JS::WarnASCII, fall back to
// nsContentUtils::ReportToConsoleNonLocalized, which can only print to
// JavaScript console.
nsContentUtils::ReportToConsoleNonLocalized(

View File

@ -12,6 +12,7 @@
#include "jsapi.h"
#include "js/StableStringChars.h"
#include "js/Warnings.h" // JS::{Get,}WarningReporter
#include "xpcpublic.h"
#include "nsIGlobalObject.h"
#include "nsIDocShell.h"

View File

@ -18,6 +18,7 @@
#include "jsapi.h"
#include "js/Debug.h"
#include "js/Warnings.h" // JS::WarningReporter
class nsPIDOMWindowInner;
class nsGlobalWindowInner;

View File

@ -261,7 +261,7 @@ class JSErrorReport : public JSErrorBase {
* JSErrorReport flag values. These may be freely composed.
*/
#define JSREPORT_ERROR 0x0 /* pseudo-flag for default case */
#define JSREPORT_WARNING 0x1 /* reported via JS_ReportWarning */
#define JSREPORT_WARNING 0x1 /* reported via JS::Warn* */
#define JSREPORT_EXCEPTION 0x2 /* exception was thrown */
#define JSREPORT_STRICT 0x4 /* error or warning due to strict option */

101
js/public/Warnings.h Normal file
View File

@ -0,0 +1,101 @@
/* -*- 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/. */
/*
* Functionality for issuing and handling warnings.
*
* Warnings are situations that aren't inherently full-blown errors (and perhaps
* for spec compliance *can't* be), but that may represent dubious programming
* practice that embeddings may wish to know about. (That said, SpiderMonkey
* exposes various options that, when active, automatically upgrade warnings to
* errors. See |JS::TransitiveCompileOptions::werrorOptions| and
* |JS::ContextOptions::werror()|.)
*
* SpiderMonkey recognizes an unspecified set of syntactic patterns and runtime
* behaviors as triggering a warning. Embeddings may also recognize and report
* additional warnings.
*/
#ifndef js_Warnings_h
#define js_Warnings_h
#include "mozilla/Assertions.h" // MOZ_ASSERT
#include "mozilla/Attributes.h" // MOZ_FORMAT_PRINTF, MOZ_RAII
#include "jstypes.h" // JS_PUBLIC_API
struct JSContext;
class JSErrorReport;
namespace JS {
/**
* Report a warning represented by the sprintf-like conversion of ASCII format
* filled from trailing ASCII arguments.
*
* Return true iff the warning was successfully reported without reporting an
* error (or being upgraded into one).
*/
extern JS_PUBLIC_API bool WarnASCII(JSContext* cx, const char* format, ...)
MOZ_FORMAT_PRINTF(2, 3);
/**
* Report a warning represented by the sprintf-like conversion of Latin-1 format
* filled from trailing Latin-1 arguments.
*
* Return true iff the warning was successfully reported without reporting an
* error (or being upgraded into one).
*/
extern JS_PUBLIC_API bool WarnLatin1(JSContext* cx, const char* format, ...)
MOZ_FORMAT_PRINTF(2, 3);
/**
* Report a warning represented by the sprintf-like conversion of UTF-8 format
* filled from trailing UTF-8 arguments.
*
* Return true iff the warning was successfully reported without reporting an
* error (or being upgraded into one).
*/
extern JS_PUBLIC_API bool WarnUTF8(JSContext* cx, const char* format, ...)
MOZ_FORMAT_PRINTF(2, 3);
using WarningReporter = void (*)(JSContext* cx, JSErrorReport* report);
extern JS_PUBLIC_API WarningReporter GetWarningReporter(JSContext* cx);
extern JS_PUBLIC_API WarningReporter
SetWarningReporter(JSContext* cx, WarningReporter reporter);
/**
* A simple RAII class that clears the registered warning reporter on
* construction and restores it on destruction.
*
* A fresh warning reporter *may* be set while an instance of this class is
* live, but it must be unset in LIFO fashion by the time that instance is
* destroyed.
*/
class MOZ_RAII JS_PUBLIC_API AutoSuppressWarningReporter {
JSContext* context_;
WarningReporter prevReporter_;
public:
explicit AutoSuppressWarningReporter(JSContext* cx) : context_(cx) {
prevReporter_ = SetWarningReporter(context_, nullptr);
}
~AutoSuppressWarningReporter() {
#ifdef DEBUG
WarningReporter reporter =
#endif
SetWarningReporter(context_, prevReporter_);
MOZ_ASSERT(reporter == nullptr, "Unexpected WarningReporter active");
SetWarningReporter(context_, prevReporter_);
}
};
} // namespace JS
#endif // js_Warnings_h

View File

@ -25,6 +25,7 @@ typedef uint32_t HashNumber;
#include "js/PropertySpec.h"
#include "js/SourceText.h"
#include "js/StructuredClone.h"
#include "js/Warnings.h"
// Replacements for types that are too difficult for rust-bindgen.

View File

@ -10,6 +10,7 @@
#include "jsapi.h"
#include "jsfriendapi.h"
#include "js/Initialization.h"
#include "js/Warnings.h" // JS::SetWarningReporter
using namespace JS;

View File

@ -22,6 +22,7 @@
#include "js/Equality.h" // JS::SameValue
#include "js/RegExpFlags.h" // JS::RegExpFlags
#include "js/Vector.h"
#include "js/Warnings.h" // JS::SetWarningReporter
#include "vm/JSContext.h"
/* Note: Aborts on OOM. */

View File

@ -4860,42 +4860,6 @@ JS_PUBLIC_API void JS_ReportErrorNumberUCArray(JSContext* cx,
errorNumber, args);
}
JS_PUBLIC_API bool JS_ReportWarningASCII(JSContext* cx, const char* format,
...) {
va_list ap;
bool ok;
AssertHeapIsIdle();
va_start(ap, format);
ok = ReportErrorVA(cx, JSREPORT_WARNING, format, ArgumentsAreASCII, ap);
va_end(ap);
return ok;
}
JS_PUBLIC_API bool JS_ReportWarningLatin1(JSContext* cx, const char* format,
...) {
va_list ap;
bool ok;
AssertHeapIsIdle();
va_start(ap, format);
ok = ReportErrorVA(cx, JSREPORT_WARNING, format, ArgumentsAreLatin1, ap);
va_end(ap);
return ok;
}
JS_PUBLIC_API bool JS_ReportWarningUTF8(JSContext* cx, const char* format,
...) {
va_list ap;
bool ok;
AssertHeapIsIdle();
va_start(ap, format);
ok = ReportErrorVA(cx, JSREPORT_WARNING, format, ArgumentsAreUTF8, ap);
va_end(ap);
return ok;
}
JS_PUBLIC_API bool JS_ReportErrorFlagsAndNumberASCII(
JSContext* cx, unsigned flags, JSErrorCallback errorCallback, void* userRef,
const unsigned errorNumber, ...) {
@ -4962,17 +4926,6 @@ JS_PUBLIC_API void JS_ReportAllocationOverflow(JSContext* cx) {
ReportAllocationOverflow(cx);
}
JS_PUBLIC_API JS::WarningReporter JS::GetWarningReporter(JSContext* cx) {
return cx->runtime()->warningReporter;
}
JS_PUBLIC_API JS::WarningReporter JS::SetWarningReporter(
JSContext* cx, JS::WarningReporter reporter) {
WarningReporter older = cx->runtime()->warningReporter;
cx->runtime()->warningReporter = reporter;
return older;
}
/************************************************************************/
JS_PUBLIC_API bool JS_SetDefaultLocale(JSRuntime* rt, const char* locale) {

View File

@ -2652,24 +2652,6 @@ extern JS_PUBLIC_API void JS_ReportErrorNumberUCArray(
JSContext* cx, JSErrorCallback errorCallback, void* userRef,
const unsigned errorNumber, const char16_t** args);
/**
* As above, but report a warning instead (JSREPORT_IS_WARNING(report.flags)).
* Return true if there was no error trying to issue the warning, and if the
* warning was not converted into an error due to the JSOPTION_WERROR option
* being set, false otherwise.
*/
extern JS_PUBLIC_API bool JS_ReportWarningASCII(JSContext* cx,
const char* format, ...)
MOZ_FORMAT_PRINTF(2, 3);
extern JS_PUBLIC_API bool JS_ReportWarningLatin1(JSContext* cx,
const char* format, ...)
MOZ_FORMAT_PRINTF(2, 3);
extern JS_PUBLIC_API bool JS_ReportWarningUTF8(JSContext* cx,
const char* format, ...)
MOZ_FORMAT_PRINTF(2, 3);
extern JS_PUBLIC_API bool JS_ReportErrorFlagsAndNumberASCII(
JSContext* cx, unsigned flags, JSErrorCallback errorCallback, void* userRef,
const unsigned errorNumber, ...);
@ -2698,33 +2680,6 @@ extern JS_PUBLIC_API void JS_ReportAllocationOverflow(JSContext* cx);
namespace JS {
using WarningReporter = void (*)(JSContext* cx, JSErrorReport* report);
extern JS_PUBLIC_API WarningReporter
SetWarningReporter(JSContext* cx, WarningReporter reporter);
extern JS_PUBLIC_API WarningReporter GetWarningReporter(JSContext* cx);
// Suppress the Warning Reporter callback temporarily.
class MOZ_RAII JS_PUBLIC_API AutoSuppressWarningReporter {
JSContext* context_;
WarningReporter prevReporter_;
public:
explicit AutoSuppressWarningReporter(JSContext* cx) : context_(cx) {
prevReporter_ = SetWarningReporter(context_, nullptr);
}
~AutoSuppressWarningReporter() {
#ifdef DEBUG
WarningReporter reporter =
#endif
SetWarningReporter(context_, prevReporter_);
MOZ_ASSERT(reporter == nullptr, "Unexpected WarningReporter active");
SetWarningReporter(context_, prevReporter_);
}
};
extern JS_PUBLIC_API bool CreateError(
JSContext* cx, JSExnType type, HandleObject stack, HandleString fileName,
uint32_t lineNumber, uint32_t columnNumber, JSErrorReport* report,

View File

@ -26,6 +26,7 @@
#include "js/CharacterEncoding.h"
#include "js/PropertySpec.h"
#include "js/UniquePtr.h"
#include "js/Warnings.h" // JS::{,Set}WarningReporter
#include "js/Wrapper.h"
#include "util/StringBuffer.h"
#include "vm/ErrorObject.h"

View File

@ -191,6 +191,7 @@ EXPORTS.js += [
'../public/Utility.h',
'../public/Value.h',
'../public/Vector.h',
'../public/Warnings.h',
'../public/WeakMapPtr.h',
'../public/Wrapper.h',
]
@ -328,6 +329,7 @@ UNIFIED_SOURCES += [
'vm/UbiNodeCensus.cpp',
'vm/UbiNodeShortestPaths.cpp',
'vm/Value.cpp',
'vm/Warnings.cpp',
'vm/Xdr.cpp',
]

View File

@ -104,6 +104,7 @@
#include "js/StableStringChars.h"
#include "js/StructuredClone.h"
#include "js/SweepingAPI.h"
#include "js/Warnings.h" // JS::SetWarningReporter
#include "js/Wrapper.h"
#include "perf/jsperf.h"
#include "shell/jsoptparse.h"

View File

@ -13,6 +13,7 @@
#include "jsexn.h"
#include "jsfriendapi.h"
#include "js/Warnings.h" // JS::WarningReporter
#include "vm/GlobalObject.h"
#include "vm/JSContext.h"

View File

@ -41,6 +41,7 @@
#include "js/UniquePtr.h"
#include "js/Utility.h"
#include "js/Vector.h"
#include "js/Warnings.h" // JS::WarningReporter
#include "threading/Thread.h"
#include "vm/Caches.h"
#include "vm/CodeCoverage.h"

View File

@ -44,6 +44,7 @@
#include "js/Date.h"
#include "js/PropertySpec.h"
#include "js/StableStringChars.h"
#include "js/Warnings.h" // JS::{,Set}WarningReporter
#include "js/Wrapper.h"
#include "util/StringBuffer.h"
#include "vm/ArgumentsObject.h"

65
js/src/vm/Warnings.cpp Normal file
View File

@ -0,0 +1,65 @@
/* -*- 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/Warnings.h"
#include <stdarg.h> // va_{list,start,end}
#include "jsapi.h" // js::AssertHeapIsIdle
#include "jstypes.h" // JS_PUBLIC_API
#include "js/ErrorReport.h" // JSREPORT_WARNING
#include "vm/JSContext.h" // js::ArgumentsAre{ASCII,Latin1,UTF8}, js::ReportErrorVA
using js::ArgumentsAreASCII;
using js::ArgumentsAreLatin1;
using js::ArgumentsAreUTF8;
using js::AssertHeapIsIdle;
using js::ReportErrorVA;
JS_PUBLIC_API bool JS::WarnASCII(JSContext* cx, const char* format, ...) {
va_list ap;
bool ok;
AssertHeapIsIdle();
va_start(ap, format);
ok = ReportErrorVA(cx, JSREPORT_WARNING, format, ArgumentsAreASCII, ap);
va_end(ap);
return ok;
}
JS_PUBLIC_API bool JS::WarnLatin1(JSContext* cx, const char* format, ...) {
va_list ap;
bool ok;
AssertHeapIsIdle();
va_start(ap, format);
ok = ReportErrorVA(cx, JSREPORT_WARNING, format, ArgumentsAreLatin1, ap);
va_end(ap);
return ok;
}
JS_PUBLIC_API bool JS::WarnUTF8(JSContext* cx, const char* format, ...) {
va_list ap;
bool ok;
AssertHeapIsIdle();
va_start(ap, format);
ok = ReportErrorVA(cx, JSREPORT_WARNING, format, ArgumentsAreUTF8, ap);
va_end(ap);
return ok;
}
JS_PUBLIC_API JS::WarningReporter JS::GetWarningReporter(JSContext* cx) {
return cx->runtime()->warningReporter;
}
JS_PUBLIC_API JS::WarningReporter JS::SetWarningReporter(
JSContext* cx, WarningReporter reporter) {
WarningReporter older = cx->runtime()->warningReporter;
cx->runtime()->warningReporter = reporter;
return older;
}

View File

@ -19,6 +19,7 @@
#include "js/PropertySpec.h"
#include "js/SourceText.h"
#include "js/Utility.h"
#include "js/Warnings.h" // JS::SetWarningReporter
#include "prnetdb.h"
#include "nsITimer.h"
#include "mozilla/net/DNS.h"

View File

@ -10,6 +10,7 @@
#include "nsAppShell.h"
#include "nsIXPConnect.h"
#include "nsJSUtils.h"
#include "js/Warnings.h" // JS::WarnUTF8
#include "xpcpublic.h"
#include "mozilla/ScopeExit.h"
@ -296,7 +297,7 @@ nsresult BoxData(const nsAString& aEvent, JSContext* aCx, JS::HandleValue aData,
NS_ConvertUTF16toUTF8 event(aEvent);
if (JS_IsExceptionPending(aCx)) {
JS_ReportWarningUTF8(aCx, "Error dispatching %s", event.get());
JS::WarnUTF8(aCx, "Error dispatching %s", event.get());
} else {
JS_ReportErrorUTF8(aCx, "Invalid event data for %s", event.get());
}
@ -566,7 +567,7 @@ nsresult UnboxData(jni::String::Param aEvent, JSContext* aCx,
nsCString event = aEvent->ToCString();
if (JS_IsExceptionPending(aCx)) {
JS_ReportWarningUTF8(aCx, "Error dispatching %s", event.get());
JS::WarnUTF8(aCx, "Error dispatching %s", event.get());
} else {
JS_ReportErrorUTF8(aCx, "Invalid event data for %s", event.get());
}

View File

@ -75,6 +75,7 @@
#include "mozilla/dom/ScriptSettings.h"
#include "js/Debug.h"
#include "js/GCAPI.h"
#include "js/Warnings.h" // JS::SetWarningReporter
#include "jsfriendapi.h"
#include "nsContentUtils.h"
#include "nsCycleCollectionNoteRootCallback.h"