summaryrefslogtreecommitdiffstats
path: root/js/src/shell/jsshell.cpp
blob: 7f8e53b9a4bcd4d00c66264a99ce3cd662aa6f83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=8 sts=4 et sw=4 tw=99:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// jsshell.cpp - Utilities for the JS shell

#include "shell/jsshell.h"

#include "jsapi.h"
#include "jsfriendapi.h"

#include "vm/StringBuffer.h"

using namespace JS;

namespace js {
namespace shell {

bool
GenerateInterfaceHelp(JSContext* cx, HandleObject obj, const char* name)
{
    AutoIdVector idv(cx);
    if (!GetPropertyKeys(cx, obj, JSITER_OWNONLY | JSITER_HIDDEN, &idv))
        return false;

    StringBuffer buf(cx);
    if (!buf.append(' '))
        return false;

    for (size_t i = 0; i < idv.length(); i++) {
        RootedValue v(cx);
        RootedId id(cx, idv[i]);
        if (!JS_GetPropertyById(cx, obj, id, &v))
            return false;
        if (!v.isObject())
            continue;
        bool hasHelp = false;
        RootedObject prop(cx, &v.toObject());
        if (!JS_GetProperty(cx, prop, "usage", &v))
            return false;
        if (v.isString())
            hasHelp = true;
        if (!JS_GetProperty(cx, prop, "help", &v))
            return false;
        if (v.isString())
            hasHelp = true;
        if (hasHelp) {
            if (!buf.append(' ') ||
                !buf.append(name, strlen(name)) ||
                !buf.append('.') ||
                !buf.append(JSID_TO_FLAT_STRING(id)))
            {
                return false;
            }
        }
    }

    RootedString s(cx, buf.finishString());
    if (!s || !JS_DefineProperty(cx, obj, "help", s, 0))
        return false;

    if (!buf.append(name, strlen(name)) || !buf.append(" - interface object", 20))
        return false;
    s = buf.finishString();
    if (!s || !JS_DefineProperty(cx, obj, "usage", s, 0))
        return false;

    return true;
}

} // namespace shell
} // namespace js