Blame js/src/jsexn.h

Packit f0b94e
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
Packit f0b94e
 * vim: set ts=8 sts=4 et sw=4 tw=99:
Packit f0b94e
 * This Source Code Form is subject to the terms of the Mozilla Public
Packit f0b94e
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit f0b94e
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit f0b94e
Packit f0b94e
/*
Packit f0b94e
 * JS runtime exception classes.
Packit f0b94e
 */
Packit f0b94e
Packit f0b94e
#ifndef jsexn_h
Packit f0b94e
#define jsexn_h
Packit f0b94e
Packit f0b94e
#include "jsapi.h"
Packit f0b94e
#include "NamespaceImports.h"
Packit f0b94e
Packit f0b94e
#include "vm/JSContext.h"
Packit f0b94e
Packit f0b94e
namespace js {
Packit f0b94e
class ErrorObject;
Packit f0b94e
Packit f0b94e
JSErrorNotes::Note* CopyErrorNote(JSContext* cx, JSErrorNotes::Note* note);
Packit f0b94e
Packit f0b94e
JSErrorReport* CopyErrorReport(JSContext* cx, JSErrorReport* report);
Packit f0b94e
Packit f0b94e
JSString* ComputeStackString(JSContext* cx);
Packit f0b94e
Packit f0b94e
/*
Packit f0b94e
 * Given a JSErrorReport, check to see if there is an exception associated with
Packit f0b94e
 * the error number.  If there is, then create an appropriate exception object,
Packit f0b94e
 * set it as the pending exception, and set the JSREPORT_EXCEPTION flag on the
Packit f0b94e
 * error report.
Packit f0b94e
 *
Packit f0b94e
 * It's possible we fail (due to OOM or some other error) and end up setting
Packit f0b94e
 * cx->exception to a different exception. The original error described by
Packit f0b94e
 * *reportp typically won't be reported anywhere in this case.
Packit f0b94e
 *
Packit f0b94e
 * If the error code is unrecognized, or if we decided to do nothing in order to
Packit f0b94e
 * avoid recursion, we simply return and this error is just being swept under
Packit f0b94e
 * the rug.
Packit f0b94e
 */
Packit f0b94e
extern void ErrorToException(JSContext* cx, JSErrorReport* reportp,
Packit f0b94e
                             JSErrorCallback callback, void* userRef);
Packit f0b94e
Packit f0b94e
extern JSErrorReport* ErrorFromException(JSContext* cx, HandleObject obj);
Packit f0b94e
Packit f0b94e
/*
Packit f0b94e
 * Make a copy of errobj parented to cx's compartment's global.
Packit f0b94e
 *
Packit f0b94e
 * errobj may be in a different compartment than cx, but it must be an Error
Packit f0b94e
 * object (not a wrapper of one) and it must not be one of the standard error
Packit f0b94e
 * prototype objects (errobj->getPrivate() must not be nullptr).
Packit f0b94e
 */
Packit f0b94e
extern JSObject* CopyErrorObject(JSContext* cx,
Packit f0b94e
                                 JS::Handle<ErrorObject*> errobj);
Packit f0b94e
Packit f0b94e
static_assert(
Packit f0b94e
    JSEXN_ERR == 0 &&
Packit f0b94e
        JSProto_Error + JSEXN_INTERNALERR == JSProto_InternalError &&
Packit f0b94e
        JSProto_Error + JSEXN_EVALERR == JSProto_EvalError &&
Packit f0b94e
        JSProto_Error + JSEXN_RANGEERR == JSProto_RangeError &&
Packit f0b94e
        JSProto_Error + JSEXN_REFERENCEERR == JSProto_ReferenceError &&
Packit f0b94e
        JSProto_Error + JSEXN_SYNTAXERR == JSProto_SyntaxError &&
Packit f0b94e
        JSProto_Error + JSEXN_TYPEERR == JSProto_TypeError &&
Packit f0b94e
        JSProto_Error + JSEXN_URIERR == JSProto_URIError &&
Packit f0b94e
        JSProto_Error + JSEXN_DEBUGGEEWOULDRUN == JSProto_DebuggeeWouldRun &&
Packit f0b94e
        JSProto_Error + JSEXN_WASMCOMPILEERROR == JSProto_CompileError &&
Packit f0b94e
        JSProto_Error + JSEXN_WASMLINKERROR == JSProto_LinkError &&
Packit f0b94e
        JSProto_Error + JSEXN_WASMRUNTIMEERROR == JSProto_RuntimeError &&
Packit f0b94e
        JSEXN_WASMRUNTIMEERROR + 1 == JSEXN_WARN &&
Packit f0b94e
        JSEXN_WARN + 1 == JSEXN_NOTE && JSEXN_NOTE + 1 == JSEXN_LIMIT,
Packit f0b94e
    "GetExceptionProtoKey and ExnTypeFromProtoKey require that "
Packit f0b94e
    "each corresponding JSExnType and JSProtoKey value be separated "
Packit f0b94e
    "by the same constant value");
Packit f0b94e
Packit f0b94e
static inline JSProtoKey GetExceptionProtoKey(JSExnType exn) {
Packit f0b94e
  MOZ_ASSERT(JSEXN_ERR <= exn);
Packit f0b94e
  MOZ_ASSERT(exn < JSEXN_WARN);
Packit f0b94e
  return JSProtoKey(JSProto_Error + int(exn));
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
static inline JSExnType ExnTypeFromProtoKey(JSProtoKey key) {
Packit f0b94e
  JSExnType type = static_cast<JSExnType>(key - JSProto_Error);
Packit f0b94e
  MOZ_ASSERT(type >= JSEXN_ERR);
Packit f0b94e
  MOZ_ASSERT(type < JSEXN_ERROR_LIMIT);
Packit f0b94e
  return type;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
static inline bool IsErrorProtoKey(JSProtoKey key) {
Packit f0b94e
  int type = key - JSProto_Error;
Packit f0b94e
  return type >= JSEXN_ERR && type < JSEXN_ERROR_LIMIT;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
class AutoClearPendingException {
Packit f0b94e
  JSContext* cx;
Packit f0b94e
Packit f0b94e
 public:
Packit f0b94e
  explicit AutoClearPendingException(JSContext* cxArg) : cx(cxArg) {}
Packit f0b94e
Packit f0b94e
  ~AutoClearPendingException() { JS_ClearPendingException(cx); }
Packit f0b94e
};
Packit f0b94e
Packit f0b94e
class AutoAssertNoPendingException {
Packit f0b94e
  mozilla::DebugOnly<JSContext*> cx;
Packit f0b94e
Packit f0b94e
 public:
Packit f0b94e
  explicit AutoAssertNoPendingException(JSContext* cxArg) : cx(cxArg) {}
Packit f0b94e
Packit f0b94e
  ~AutoAssertNoPendingException() { MOZ_ASSERT(!JS_IsExceptionPending(cx)); }
Packit f0b94e
};
Packit f0b94e
Packit f0b94e
extern const char* ValueToSourceForError(JSContext* cx, HandleValue val,
Packit f0b94e
                                         JSAutoByteString& bytes);
Packit f0b94e
Packit f0b94e
bool GetInternalError(JSContext* cx, unsigned errorNumber,
Packit f0b94e
                      MutableHandleValue error);
Packit f0b94e
bool GetTypeError(JSContext* cx, unsigned errorNumber,
Packit f0b94e
                  MutableHandleValue error);
Packit f0b94e
Packit f0b94e
}  // namespace js
Packit f0b94e
Packit f0b94e
#endif /* jsexn_h */