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 #ifndef vm_ErrorObject_h_
8 #define vm_ErrorObject_h_
9 
10 #include "mozilla/Assertions.h"
11 #include "mozilla/Maybe.h"
12 
13 #include <iterator>
14 #include <stdint.h>
15 
16 #include "jspubtd.h"
17 #include "NamespaceImports.h"
18 
19 #include "gc/Barrier.h"
20 #include "js/Class.h"
21 #include "js/ErrorReport.h"
22 #include "js/RootingAPI.h"
23 #include "js/TypeDecls.h"
24 #include "js/UniquePtr.h"
25 #include "js/Value.h"
26 #include "vm/FunctionFlags.h"  // js::FunctionFlags
27 #include "vm/JSObject.h"
28 #include "vm/NativeObject.h"
29 #include "vm/Shape.h"
30 
31 namespace js {
32 class ArrayObject;
33 
34 class ErrorObject : public NativeObject {
35   static JSObject* createProto(JSContext* cx, JSProtoKey key);
36 
37   static JSObject* createConstructor(JSContext* cx, JSProtoKey key);
38 
39   static bool init(JSContext* cx, Handle<ErrorObject*> obj, JSExnType type,
40                    UniquePtr<JSErrorReport> errorReport, HandleString fileName,
41                    HandleObject stack, uint32_t sourceId, uint32_t lineNumber,
42                    uint32_t columnNumber, HandleString message,
43                    Handle<mozilla::Maybe<JS::Value>> cause);
44 
45   static const ClassSpec classSpecs[JSEXN_ERROR_LIMIT];
46   static const JSClass protoClasses[JSEXN_ERROR_LIMIT];
47 
48  protected:
49   static const uint32_t STACK_SLOT = 0;
50   static const uint32_t ERROR_REPORT_SLOT = STACK_SLOT + 1;
51   static const uint32_t FILENAME_SLOT = ERROR_REPORT_SLOT + 1;
52   static const uint32_t LINENUMBER_SLOT = FILENAME_SLOT + 1;
53   static const uint32_t COLUMNNUMBER_SLOT = LINENUMBER_SLOT + 1;
54   static const uint32_t MESSAGE_SLOT = COLUMNNUMBER_SLOT + 1;
55   static const uint32_t CAUSE_SLOT = MESSAGE_SLOT + 1;
56   static const uint32_t SOURCEID_SLOT = CAUSE_SLOT + 1;
57 
58   static const uint32_t RESERVED_SLOTS = SOURCEID_SLOT + 1;
59 
60   // This slot is only used for errors that could be Wasm traps.
61   static const uint32_t WASM_TRAP_SLOT = SOURCEID_SLOT + 1;
62   static const uint32_t RESERVED_SLOTS_MAYBE_WASM_TRAP = WASM_TRAP_SLOT + 1;
63 
64  public:
65   static const JSClass classes[JSEXN_ERROR_LIMIT];
66 
classForType(JSExnType type)67   static const JSClass* classForType(JSExnType type) {
68     MOZ_ASSERT(type < JSEXN_ERROR_LIMIT);
69     return &classes[type];
70   }
71 
isErrorClass(const JSClass * clasp)72   static bool isErrorClass(const JSClass* clasp) {
73     return &classes[0] <= clasp && clasp < &classes[0] + std::size(classes);
74   }
75 
76   // Create an error of the given type corresponding to the provided location
77   // info.  If |message| is non-null, then the error will have a .message
78   // property with that value; otherwise the error will have no .message
79   // property.
80   static ErrorObject* create(JSContext* cx, JSExnType type, HandleObject stack,
81                              HandleString fileName, uint32_t sourceId,
82                              uint32_t lineNumber, uint32_t columnNumber,
83                              UniquePtr<JSErrorReport> report,
84                              HandleString message,
85                              Handle<mozilla::Maybe<JS::Value>> cause,
86                              HandleObject proto = nullptr);
87 
88   /*
89    * Assign the initial error shape to the empty object.  (This shape does
90    * *not* include .message, which must be added separately if needed; see
91    * ErrorObject::init.)
92    */
93   static Shape* assignInitialShape(JSContext* cx, Handle<ErrorObject*> obj);
94 
type()95   JSExnType type() const {
96     MOZ_ASSERT(isErrorClass(getClass()));
97     return static_cast<JSExnType>(getClass() - &classes[0]);
98   }
99 
getErrorReport()100   JSErrorReport* getErrorReport() const {
101     const Value& slot = getReservedSlot(ERROR_REPORT_SLOT);
102     if (slot.isUndefined()) {
103       return nullptr;
104     }
105     return static_cast<JSErrorReport*>(slot.toPrivate());
106   }
107 
108   JSErrorReport* getOrCreateErrorReport(JSContext* cx);
109 
110   inline JSString* fileName(JSContext* cx) const;
111   inline uint32_t sourceId() const;
112   inline uint32_t lineNumber() const;
113   inline uint32_t columnNumber() const;
114   inline JSObject* stack() const;
115 
getMessage()116   JSString* getMessage() const {
117     const HeapSlot& slot = getReservedSlotRef(MESSAGE_SLOT);
118     return slot.isString() ? slot.toString() : nullptr;
119   }
120 
getCause()121   mozilla::Maybe<Value> getCause() const {
122     const auto& value = getReservedSlot(CAUSE_SLOT);
123     if (value.isMagic(JS_ERROR_WITHOUT_CAUSE)) {
124       return mozilla::Nothing();
125     }
126     return mozilla::Some(value);
127   }
128 
129   // Getter and setter for the Error.prototype.stack accessor.
130   static bool getStack(JSContext* cx, unsigned argc, Value* vp);
131   static bool getStack_impl(JSContext* cx, const CallArgs& args);
132   static bool setStack(JSContext* cx, unsigned argc, Value* vp);
133   static bool setStack_impl(JSContext* cx, const CallArgs& args);
134 
135   // Used to distinguish errors created from Wasm traps.
mightBeWasmTrap()136   bool mightBeWasmTrap() const {
137     return type() == JSEXN_WASMRUNTIMEERROR || type() == JSEXN_INTERNALERR;
138   }
fromWasmTrap()139   bool fromWasmTrap() const {
140     if (!mightBeWasmTrap()) {
141       return false;
142     } else {
143       MOZ_ASSERT(JSCLASS_RESERVED_SLOTS(getClass()) > WASM_TRAP_SLOT);
144       return getReservedSlot(WASM_TRAP_SLOT).toBoolean();
145     }
146   }
147   void setFromWasmTrap();
148 };
149 
150 JSString* ErrorToSource(JSContext* cx, HandleObject obj);
151 
152 }  // namespace js
153 
154 template <>
155 inline bool JSObject::is<js::ErrorObject>() const {
156   return js::ErrorObject::isErrorClass(getClass());
157 }
158 
159 #endif  // vm_ErrorObject_h_
160