js/src/vm/ErrorObject.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/vm/ErrorObject.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,101 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99:
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef vm_ErrorObject_h_
    1.11 +#define vm_ErrorObject_h_
    1.12 +
    1.13 +#include "jsobj.h"
    1.14 +
    1.15 +#include "vm/Shape.h"
    1.16 +
    1.17 +struct JSExnPrivate;
    1.18 +
    1.19 +/*
    1.20 + * Initialize the exception constructor/prototype hierarchy.
    1.21 + */
    1.22 +extern JSObject *
    1.23 +js_InitExceptionClasses(JSContext *cx, JS::HandleObject obj);
    1.24 +
    1.25 +namespace js {
    1.26 +
    1.27 +class ErrorObject : public JSObject
    1.28 +{
    1.29 +    static ErrorObject *
    1.30 +    createProto(JSContext *cx, JS::Handle<GlobalObject*> global, JSExnType type,
    1.31 +                JS::HandleObject proto);
    1.32 +
    1.33 +    /* For access to createProto. */
    1.34 +    friend JSObject *
    1.35 +    ::js_InitExceptionClasses(JSContext *cx, JS::HandleObject global);
    1.36 +
    1.37 +    /* For access to assignInitialShape. */
    1.38 +    friend bool
    1.39 +    EmptyShape::ensureInitialCustomShape<ErrorObject>(ExclusiveContext *cx,
    1.40 +                                                      Handle<ErrorObject*> obj);
    1.41 +
    1.42 +    /*
    1.43 +     * Assign the initial error shape to the empty object.  (This shape does
    1.44 +     * *not* include .message, which must be added separately if needed; see
    1.45 +     * ErrorObject::init.)
    1.46 +     */
    1.47 +    static Shape *
    1.48 +    assignInitialShape(ExclusiveContext *cx, Handle<ErrorObject*> obj);
    1.49 +
    1.50 +    static bool
    1.51 +    init(JSContext *cx, Handle<ErrorObject*> obj, JSExnType type,
    1.52 +         ScopedJSFreePtr<JSErrorReport> *errorReport, HandleString fileName, HandleString stack,
    1.53 +         uint32_t lineNumber, uint32_t columnNumber, HandleString message);
    1.54 +
    1.55 +  protected:
    1.56 +    static const uint32_t EXNTYPE_SLOT      = 0;
    1.57 +    static const uint32_t ERROR_REPORT_SLOT = EXNTYPE_SLOT + 1;
    1.58 +    static const uint32_t FILENAME_SLOT     = ERROR_REPORT_SLOT + 1;
    1.59 +    static const uint32_t LINENUMBER_SLOT   = FILENAME_SLOT + 1;
    1.60 +    static const uint32_t COLUMNNUMBER_SLOT = LINENUMBER_SLOT + 1;
    1.61 +    static const uint32_t STACK_SLOT        = COLUMNNUMBER_SLOT + 1;
    1.62 +    static const uint32_t MESSAGE_SLOT      = STACK_SLOT + 1;
    1.63 +
    1.64 +    static const uint32_t RESERVED_SLOTS = MESSAGE_SLOT + 1;
    1.65 +
    1.66 +  public:
    1.67 +    static const Class class_;
    1.68 +
    1.69 +    // Create an error of the given type corresponding to the provided location
    1.70 +    // info.  If |message| is non-null, then the error will have a .message
    1.71 +    // property with that value; otherwise the error will have no .message
    1.72 +    // property.
    1.73 +    static ErrorObject *
    1.74 +    create(JSContext *cx, JSExnType type, HandleString stack, HandleString fileName,
    1.75 +           uint32_t lineNumber, uint32_t columnNumber, ScopedJSFreePtr<JSErrorReport> *report,
    1.76 +           HandleString message);
    1.77 +
    1.78 +    JSExnType type() const {
    1.79 +        return JSExnType(getReservedSlot(EXNTYPE_SLOT).toInt32());
    1.80 +    }
    1.81 +
    1.82 +    JSErrorReport * getErrorReport() const {
    1.83 +        const Value &slot = getReservedSlot(ERROR_REPORT_SLOT);
    1.84 +        if (slot.isUndefined())
    1.85 +            return nullptr;
    1.86 +        return static_cast<JSErrorReport*>(slot.toPrivate());
    1.87 +    }
    1.88 +
    1.89 +    JSErrorReport * getOrCreateErrorReport(JSContext *cx);
    1.90 +
    1.91 +    inline JSString * fileName(JSContext *cx) const;
    1.92 +    inline uint32_t lineNumber() const;
    1.93 +    inline uint32_t columnNumber() const;
    1.94 +    inline JSString * stack(JSContext *cx) const;
    1.95 +
    1.96 +    JSString * getMessage() const {
    1.97 +        const HeapSlot &slot = getReservedSlotRef(MESSAGE_SLOT);
    1.98 +        return slot.isString() ? slot.toString() : nullptr;
    1.99 +    }
   1.100 +};
   1.101 +
   1.102 +} // namespace js
   1.103 +
   1.104 +#endif // vm_ErrorObject_h_

mercurial