|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * vim: set ts=8 sw=4 et tw=78: |
|
3 * |
|
4 * This Source Code Form is subject to the terms of the Mozilla Public |
|
5 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
7 |
|
8 #include "vm/ErrorObject-inl.h" |
|
9 |
|
10 #include "jsexn.h" |
|
11 |
|
12 #include "vm/GlobalObject.h" |
|
13 |
|
14 #include "jsobjinlines.h" |
|
15 |
|
16 #include "vm/Shape-inl.h" |
|
17 |
|
18 using namespace js; |
|
19 using mozilla::PodZero; |
|
20 |
|
21 /* static */ Shape * |
|
22 js::ErrorObject::assignInitialShape(ExclusiveContext *cx, Handle<ErrorObject*> obj) |
|
23 { |
|
24 MOZ_ASSERT(obj->nativeEmpty()); |
|
25 |
|
26 if (!obj->addDataProperty(cx, cx->names().fileName, FILENAME_SLOT, 0)) |
|
27 return nullptr; |
|
28 if (!obj->addDataProperty(cx, cx->names().lineNumber, LINENUMBER_SLOT, 0)) |
|
29 return nullptr; |
|
30 if (!obj->addDataProperty(cx, cx->names().columnNumber, COLUMNNUMBER_SLOT, 0)) |
|
31 return nullptr; |
|
32 return obj->addDataProperty(cx, cx->names().stack, STACK_SLOT, 0); |
|
33 } |
|
34 |
|
35 /* static */ bool |
|
36 js::ErrorObject::init(JSContext *cx, Handle<ErrorObject*> obj, JSExnType type, |
|
37 ScopedJSFreePtr<JSErrorReport> *errorReport, HandleString fileName, |
|
38 HandleString stack, uint32_t lineNumber, uint32_t columnNumber, |
|
39 HandleString message) |
|
40 { |
|
41 // Null out early in case of error, for exn_finalize's sake. |
|
42 obj->initReservedSlot(ERROR_REPORT_SLOT, PrivateValue(nullptr)); |
|
43 |
|
44 if (!EmptyShape::ensureInitialCustomShape<ErrorObject>(cx, obj)) |
|
45 return false; |
|
46 |
|
47 // The .message property isn't part of the initial shape because it's |
|
48 // present in some error objects -- |Error.prototype|, |new Error("f")|, |
|
49 // |new Error("")| -- but not in others -- |new Error(undefined)|, |
|
50 // |new Error()|. |
|
51 RootedShape messageShape(cx); |
|
52 if (message) { |
|
53 messageShape = obj->addDataProperty(cx, cx->names().message, MESSAGE_SLOT, 0); |
|
54 if (!messageShape) |
|
55 return false; |
|
56 MOZ_ASSERT(messageShape->slot() == MESSAGE_SLOT); |
|
57 } |
|
58 |
|
59 MOZ_ASSERT(obj->nativeLookupPure(NameToId(cx->names().fileName))->slot() == FILENAME_SLOT); |
|
60 MOZ_ASSERT(obj->nativeLookupPure(NameToId(cx->names().lineNumber))->slot() == LINENUMBER_SLOT); |
|
61 MOZ_ASSERT(obj->nativeLookupPure(NameToId(cx->names().columnNumber))->slot() == |
|
62 COLUMNNUMBER_SLOT); |
|
63 MOZ_ASSERT(obj->nativeLookupPure(NameToId(cx->names().stack))->slot() == STACK_SLOT); |
|
64 MOZ_ASSERT_IF(message, |
|
65 obj->nativeLookupPure(NameToId(cx->names().message))->slot() == MESSAGE_SLOT); |
|
66 |
|
67 MOZ_ASSERT(JSEXN_ERR <= type && type < JSEXN_LIMIT); |
|
68 |
|
69 JSErrorReport *report = errorReport ? errorReport->forget() : nullptr; |
|
70 obj->initReservedSlot(EXNTYPE_SLOT, Int32Value(type)); |
|
71 obj->setReservedSlot(ERROR_REPORT_SLOT, PrivateValue(report)); |
|
72 obj->initReservedSlot(FILENAME_SLOT, StringValue(fileName)); |
|
73 obj->initReservedSlot(LINENUMBER_SLOT, Int32Value(lineNumber)); |
|
74 obj->initReservedSlot(COLUMNNUMBER_SLOT, Int32Value(columnNumber)); |
|
75 obj->initReservedSlot(STACK_SLOT, StringValue(stack)); |
|
76 if (message) |
|
77 obj->nativeSetSlotWithType(cx, messageShape, StringValue(message)); |
|
78 |
|
79 if (report && report->originPrincipals) |
|
80 JS_HoldPrincipals(report->originPrincipals); |
|
81 |
|
82 return true; |
|
83 } |
|
84 |
|
85 /* static */ ErrorObject * |
|
86 js::ErrorObject::create(JSContext *cx, JSExnType errorType, HandleString stack, |
|
87 HandleString fileName, uint32_t lineNumber, uint32_t columnNumber, |
|
88 ScopedJSFreePtr<JSErrorReport> *report, HandleString message) |
|
89 { |
|
90 Rooted<JSObject*> proto(cx, GlobalObject::getOrCreateCustomErrorPrototype(cx, cx->global(), errorType)); |
|
91 if (!proto) |
|
92 return nullptr; |
|
93 |
|
94 Rooted<ErrorObject*> errObject(cx); |
|
95 { |
|
96 JSObject* obj = NewObjectWithGivenProto(cx, &ErrorObject::class_, proto, nullptr); |
|
97 if (!obj) |
|
98 return nullptr; |
|
99 errObject = &obj->as<ErrorObject>(); |
|
100 } |
|
101 |
|
102 if (!ErrorObject::init(cx, errObject, errorType, report, fileName, stack, |
|
103 lineNumber, columnNumber, message)) |
|
104 { |
|
105 return nullptr; |
|
106 } |
|
107 |
|
108 return errObject; |
|
109 } |
|
110 |
|
111 JSErrorReport * |
|
112 js::ErrorObject::getOrCreateErrorReport(JSContext *cx) |
|
113 { |
|
114 if (JSErrorReport *r = getErrorReport()) |
|
115 return r; |
|
116 |
|
117 // We build an error report on the stack and then use CopyErrorReport to do |
|
118 // the nitty-gritty malloc stuff. |
|
119 JSErrorReport report; |
|
120 PodZero(&report); |
|
121 |
|
122 // Type. |
|
123 JSExnType type_ = type(); |
|
124 report.exnType = type_; |
|
125 |
|
126 // Filename. |
|
127 JSAutoByteString filenameStr; |
|
128 if (!filenameStr.encodeLatin1(cx, fileName(cx))) |
|
129 return nullptr; |
|
130 report.filename = filenameStr.ptr(); |
|
131 |
|
132 // Coordinates. |
|
133 report.lineno = lineNumber(); |
|
134 report.column = columnNumber(); |
|
135 |
|
136 // Message. Note that |new Error()| will result in an undefined |message| |
|
137 // slot, so we need to explicitly substitute the empty string in that case. |
|
138 RootedString message(cx, getMessage()); |
|
139 if (!message) |
|
140 message = cx->runtime()->emptyString; |
|
141 if (!message->ensureFlat(cx)) |
|
142 return nullptr; |
|
143 report.ucmessage = message->asFlat().chars(); |
|
144 |
|
145 // Cache and return. |
|
146 JSErrorReport *copy = CopyErrorReport(cx, &report); |
|
147 if (!copy) |
|
148 return nullptr; |
|
149 setReservedSlot(ERROR_REPORT_SLOT, PrivateValue(copy)); |
|
150 return copy; |
|
151 } |