js/xpconnect/src/XPCException.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/xpconnect/src/XPCException.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,80 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* vim: set ts=8 sts=4 et sw=4 tw=99: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/* An implementaion of nsIException. */
    1.11 +
    1.12 +#include "xpcprivate.h"
    1.13 +#include "nsError.h"
    1.14 +
    1.15 +/***************************************************************************/
    1.16 +/* Quick and dirty mapping of well known result codes to strings. We only
    1.17 +*  call this when building an exception object, so iterating the short array
    1.18 +*  is not too bad.
    1.19 +*
    1.20 +*  It sure would be nice to have exceptions declared in idl and available
    1.21 +*  in some more global way at runtime.
    1.22 +*/
    1.23 +
    1.24 +static const struct ResultMap
    1.25 +{nsresult rv; const char* name; const char* format;} map[] = {
    1.26 +#define XPC_MSG_DEF(val, format) \
    1.27 +    {(val), #val, format},
    1.28 +#include "xpc.msg"
    1.29 +#undef XPC_MSG_DEF
    1.30 +    {NS_OK,0,0}   // sentinel to mark end of array
    1.31 +};
    1.32 +
    1.33 +#define RESULT_COUNT ((sizeof(map) / sizeof(map[0]))-1)
    1.34 +
    1.35 +// static
    1.36 +bool
    1.37 +nsXPCException::NameAndFormatForNSResult(nsresult rv,
    1.38 +                                         const char** name,
    1.39 +                                         const char** format)
    1.40 +{
    1.41 +
    1.42 +    for (const ResultMap* p = map; p->name; p++) {
    1.43 +        if (rv == p->rv) {
    1.44 +            if (name) *name = p->name;
    1.45 +            if (format) *format = p->format;
    1.46 +            return true;
    1.47 +        }
    1.48 +    }
    1.49 +    return false;
    1.50 +}
    1.51 +
    1.52 +// static
    1.53 +const void*
    1.54 +nsXPCException::IterateNSResults(nsresult* rv,
    1.55 +                                 const char** name,
    1.56 +                                 const char** format,
    1.57 +                                 const void** iterp)
    1.58 +{
    1.59 +    const ResultMap* p = (const ResultMap*) *iterp;
    1.60 +    if (!p)
    1.61 +        p = map;
    1.62 +    else
    1.63 +        p++;
    1.64 +    if (!p->name)
    1.65 +        p = nullptr;
    1.66 +    else {
    1.67 +        if (rv)
    1.68 +            *rv = p->rv;
    1.69 +        if (name)
    1.70 +            *name = p->name;
    1.71 +        if (format)
    1.72 +            *format = p->format;
    1.73 +    }
    1.74 +    *iterp = p;
    1.75 +    return p;
    1.76 +}
    1.77 +
    1.78 +// static
    1.79 +uint32_t
    1.80 +nsXPCException::GetNSResultCount()
    1.81 +{
    1.82 +    return RESULT_COUNT;
    1.83 +}

mercurial