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