summaryrefslogtreecommitdiffstats
path: root/js/src/shell/jsshell.cpp
blob: fe4b7488dca59857659b31c851b10be4f8e663c6 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * 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 "mozilla/Sprintf.h"

#include "jsfriendapi.h"

#include "js/GlobalObject.h"
#include "js/PropertyAndElement.h"  // JS_DefineProperty, JS_GetProperty, JS_GetPropertyById
#include "util/StringBuffer.h"

using namespace JS;

namespace js {
namespace shell {

// Generate 'usage' and 'help' properties for the given object.
// JS_DefineFunctionsWithHelp will define individual function objects with both
// of those properties (eg getpid.usage = "getpid()" and getpid.help = "return
// the process id"). This function will generate strings for an "interface
// object", eg os.file, which contains some number of functions.
//
// .usage will be set to "<name> - interface object".
//
// .help will be set to a newline-separated list of functions that have either
// 'help' or 'usage' properties. Functions are described with their usage
// strings, if they have them, else with just their names.
//
bool GenerateInterfaceHelp(JSContext* cx, HandleObject obj, const char* name) {
  RootedIdVector idv(cx);
  if (!GetPropertyKeys(cx, obj, JSITER_OWNONLY | JSITER_HIDDEN, &idv)) {
    return false;
  }

  JSStringBuilder buf(cx);
  int numEntries = 0;
  for (size_t i = 0; i < idv.length(); i++) {
    RootedId id(cx, idv[i]);
    RootedValue v(cx);
    if (!JS_GetPropertyById(cx, obj, id, &v)) {
      return false;
    }
    if (!v.isObject()) {
      continue;
    }
    RootedObject prop(cx, &v.toObject());

    RootedValue usage(cx);
    RootedValue help(cx);
    if (!JS_GetProperty(cx, prop, "usage", &usage)) {
      return false;
    }
    if (!JS_GetProperty(cx, prop, "help", &help)) {
      return false;
    }
    if (!usage.isString() && !help.isString()) {
      continue;
    }

    if (numEntries && !buf.append("\n")) {
      return false;
    }
    numEntries++;

    if (!buf.append("  ")) {
      return false;
    }

    if (!buf.append(usage.isString() ? usage.toString() : id.toString())) {
      return false;
    }
  }

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

  buf.clear();
  if (!buf.append(name, strlen(name)) ||
      !buf.append(" - interface object with ")) {
    return false;
  }
  char cbuf[100];
  SprintfLiteral(cbuf, "%d %s", numEntries,
                 numEntries == 1 ? "entry" : "entries");
  if (!buf.append(cbuf, strlen(cbuf))) {
    return false;
  }
  s = buf.finishString();
  if (!s || !JS_DefineProperty(cx, obj, "usage", s, 0)) {
    return false;
  }

  return true;
}

bool CreateAlias(JSContext* cx, const char* dstName,
                 JS::HandleObject namespaceObj, const char* srcName) {
  RootedObject global(cx, JS::GetNonCCWObjectGlobal(namespaceObj));

  RootedValue val(cx);
  if (!JS_GetProperty(cx, namespaceObj, srcName, &val)) {
    return false;
  }

  if (!val.isObject()) {
    JS_ReportErrorASCII(cx, "attempted to alias nonexistent function");
    return false;
  }

  RootedObject function(cx, &val.toObject());
  if (!JS_DefineProperty(cx, global, dstName, function, 0)) {
    return false;
  }

  return true;
}

}  // namespace shell
}  // namespace js