js/src/vm/ErrorObject.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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/. */
     8 #include "vm/ErrorObject-inl.h"
    10 #include "jsexn.h"
    12 #include "vm/GlobalObject.h"
    14 #include "jsobjinlines.h"
    16 #include "vm/Shape-inl.h"
    18 using namespace js;
    19 using mozilla::PodZero;
    21 /* static */ Shape *
    22 js::ErrorObject::assignInitialShape(ExclusiveContext *cx, Handle<ErrorObject*> obj)
    23 {
    24     MOZ_ASSERT(obj->nativeEmpty());
    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 }
    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));
    44     if (!EmptyShape::ensureInitialCustomShape<ErrorObject>(cx, obj))
    45         return false;
    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     }
    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);
    67     MOZ_ASSERT(JSEXN_ERR <= type && type < JSEXN_LIMIT);
    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));
    79     if (report && report->originPrincipals)
    80         JS_HoldPrincipals(report->originPrincipals);
    82     return true;
    83 }
    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;
    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     }
   102     if (!ErrorObject::init(cx, errObject, errorType, report, fileName, stack,
   103                            lineNumber, columnNumber, message))
   104     {
   105         return nullptr;
   106     }
   108     return errObject;
   109 }
   111 JSErrorReport *
   112 js::ErrorObject::getOrCreateErrorReport(JSContext *cx)
   113 {
   114     if (JSErrorReport *r = getErrorReport())
   115         return r;
   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);
   122     // Type.
   123     JSExnType type_ = type();
   124     report.exnType = type_;
   126     // Filename.
   127     JSAutoByteString filenameStr;
   128     if (!filenameStr.encodeLatin1(cx, fileName(cx)))
   129         return nullptr;
   130     report.filename = filenameStr.ptr();
   132     // Coordinates.
   133     report.lineno = lineNumber();
   134     report.column = columnNumber();
   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();
   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 }

mercurial