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