|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * vim: set ts=8 sts=4 et sw=4 tw=99: |
|
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 #ifndef vm_ErrorObject_h_ |
|
8 #define vm_ErrorObject_h_ |
|
9 |
|
10 #include "jsobj.h" |
|
11 |
|
12 #include "vm/Shape.h" |
|
13 |
|
14 struct JSExnPrivate; |
|
15 |
|
16 /* |
|
17 * Initialize the exception constructor/prototype hierarchy. |
|
18 */ |
|
19 extern JSObject * |
|
20 js_InitExceptionClasses(JSContext *cx, JS::HandleObject obj); |
|
21 |
|
22 namespace js { |
|
23 |
|
24 class ErrorObject : public JSObject |
|
25 { |
|
26 static ErrorObject * |
|
27 createProto(JSContext *cx, JS::Handle<GlobalObject*> global, JSExnType type, |
|
28 JS::HandleObject proto); |
|
29 |
|
30 /* For access to createProto. */ |
|
31 friend JSObject * |
|
32 ::js_InitExceptionClasses(JSContext *cx, JS::HandleObject global); |
|
33 |
|
34 /* For access to assignInitialShape. */ |
|
35 friend bool |
|
36 EmptyShape::ensureInitialCustomShape<ErrorObject>(ExclusiveContext *cx, |
|
37 Handle<ErrorObject*> obj); |
|
38 |
|
39 /* |
|
40 * Assign the initial error shape to the empty object. (This shape does |
|
41 * *not* include .message, which must be added separately if needed; see |
|
42 * ErrorObject::init.) |
|
43 */ |
|
44 static Shape * |
|
45 assignInitialShape(ExclusiveContext *cx, Handle<ErrorObject*> obj); |
|
46 |
|
47 static bool |
|
48 init(JSContext *cx, Handle<ErrorObject*> obj, JSExnType type, |
|
49 ScopedJSFreePtr<JSErrorReport> *errorReport, HandleString fileName, HandleString stack, |
|
50 uint32_t lineNumber, uint32_t columnNumber, HandleString message); |
|
51 |
|
52 protected: |
|
53 static const uint32_t EXNTYPE_SLOT = 0; |
|
54 static const uint32_t ERROR_REPORT_SLOT = EXNTYPE_SLOT + 1; |
|
55 static const uint32_t FILENAME_SLOT = ERROR_REPORT_SLOT + 1; |
|
56 static const uint32_t LINENUMBER_SLOT = FILENAME_SLOT + 1; |
|
57 static const uint32_t COLUMNNUMBER_SLOT = LINENUMBER_SLOT + 1; |
|
58 static const uint32_t STACK_SLOT = COLUMNNUMBER_SLOT + 1; |
|
59 static const uint32_t MESSAGE_SLOT = STACK_SLOT + 1; |
|
60 |
|
61 static const uint32_t RESERVED_SLOTS = MESSAGE_SLOT + 1; |
|
62 |
|
63 public: |
|
64 static const Class class_; |
|
65 |
|
66 // Create an error of the given type corresponding to the provided location |
|
67 // info. If |message| is non-null, then the error will have a .message |
|
68 // property with that value; otherwise the error will have no .message |
|
69 // property. |
|
70 static ErrorObject * |
|
71 create(JSContext *cx, JSExnType type, HandleString stack, HandleString fileName, |
|
72 uint32_t lineNumber, uint32_t columnNumber, ScopedJSFreePtr<JSErrorReport> *report, |
|
73 HandleString message); |
|
74 |
|
75 JSExnType type() const { |
|
76 return JSExnType(getReservedSlot(EXNTYPE_SLOT).toInt32()); |
|
77 } |
|
78 |
|
79 JSErrorReport * getErrorReport() const { |
|
80 const Value &slot = getReservedSlot(ERROR_REPORT_SLOT); |
|
81 if (slot.isUndefined()) |
|
82 return nullptr; |
|
83 return static_cast<JSErrorReport*>(slot.toPrivate()); |
|
84 } |
|
85 |
|
86 JSErrorReport * getOrCreateErrorReport(JSContext *cx); |
|
87 |
|
88 inline JSString * fileName(JSContext *cx) const; |
|
89 inline uint32_t lineNumber() const; |
|
90 inline uint32_t columnNumber() const; |
|
91 inline JSString * stack(JSContext *cx) const; |
|
92 |
|
93 JSString * getMessage() const { |
|
94 const HeapSlot &slot = getReservedSlotRef(MESSAGE_SLOT); |
|
95 return slot.isString() ? slot.toString() : nullptr; |
|
96 } |
|
97 }; |
|
98 |
|
99 } // namespace js |
|
100 |
|
101 #endif // vm_ErrorObject_h_ |