summaryrefslogtreecommitdiffstats
path: root/js/src/vm/BuiltinObjectKind.cpp
blob: c9d17f180e0f1c39d53512ba44756e733515cf92 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* -*- 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/. */

#include "vm/BuiltinObjectKind.h"

#include "jspubtd.h"

#include "vm/GlobalObject.h"
#include "vm/JSContext.h"

using namespace js;

static JSProtoKey ToProtoKey(BuiltinObjectKind kind) {
  switch (kind) {
    case BuiltinObjectKind::Array:
      return JSProto_Array;
    case BuiltinObjectKind::ArrayBuffer:
      return JSProto_ArrayBuffer;
    case BuiltinObjectKind::Iterator:
      return JSProto_Iterator;
    case BuiltinObjectKind::Promise:
      return JSProto_Promise;
    case BuiltinObjectKind::RegExp:
      return JSProto_RegExp;
    case BuiltinObjectKind::SharedArrayBuffer:
      return JSProto_SharedArrayBuffer;

    case BuiltinObjectKind::FunctionPrototype:
      return JSProto_Function;
    case BuiltinObjectKind::ObjectPrototype:
      return JSProto_Object;
    case BuiltinObjectKind::RegExpPrototype:
      return JSProto_RegExp;
    case BuiltinObjectKind::StringPrototype:
      return JSProto_String;

    case BuiltinObjectKind::DateTimeFormat:
      return JSProto_DateTimeFormat;
    case BuiltinObjectKind::NumberFormat:
      return JSProto_NumberFormat;

    case BuiltinObjectKind::None:
      break;
  }
  MOZ_CRASH("Unexpected builtin object kind");
}

static bool IsPrototype(BuiltinObjectKind kind) {
  switch (kind) {
    case BuiltinObjectKind::Array:
    case BuiltinObjectKind::ArrayBuffer:
    case BuiltinObjectKind::Iterator:
    case BuiltinObjectKind::Promise:
    case BuiltinObjectKind::RegExp:
    case BuiltinObjectKind::SharedArrayBuffer:
      return false;

    case BuiltinObjectKind::FunctionPrototype:
    case BuiltinObjectKind::ObjectPrototype:
    case BuiltinObjectKind::RegExpPrototype:
    case BuiltinObjectKind::StringPrototype:
      return true;

    case BuiltinObjectKind::DateTimeFormat:
    case BuiltinObjectKind::NumberFormat:
      return false;

    case BuiltinObjectKind::None:
      break;
  }
  MOZ_CRASH("Unexpected builtin object kind");
}

using BuiltinName =
    const js::frontend::ParserName* js::frontend::WellKnownParserAtoms::*;

struct BuiltinObjectMap {
  BuiltinName name;
  BuiltinObjectKind kind;
};

BuiltinObjectKind js::BuiltinConstructorForName(
    JSContext* cx, const js::frontend::ParserAtom* name) {
  using WellKnownName = js::frontend::WellKnownParserAtoms;
  static constexpr BuiltinObjectMap constructors[] = {
      {&WellKnownName::Array, BuiltinObjectKind::Array},
      {&WellKnownName::ArrayBuffer, BuiltinObjectKind::ArrayBuffer},
      {&WellKnownName::Iterator, BuiltinObjectKind::Iterator},
      {&WellKnownName::Promise, BuiltinObjectKind::Promise},
      {&WellKnownName::RegExp, BuiltinObjectKind::RegExp},
      {&WellKnownName::SharedArrayBuffer, BuiltinObjectKind::SharedArrayBuffer},
      {&WellKnownName::DateTimeFormat, BuiltinObjectKind::DateTimeFormat},
      {&WellKnownName::NumberFormat, BuiltinObjectKind::NumberFormat},
  };

  for (auto& builtin : constructors) {
    if (name == cx->parserNames().*(builtin.name)) {
      return builtin.kind;
    }
  }
  return BuiltinObjectKind::None;
}

BuiltinObjectKind js::BuiltinPrototypeForName(
    JSContext* cx, const js::frontend::ParserAtom* name) {
  using WellKnownName = js::frontend::WellKnownParserAtoms;
  static constexpr BuiltinObjectMap prototypes[] = {
      {&WellKnownName::Function, BuiltinObjectKind::FunctionPrototype},
      {&WellKnownName::Object, BuiltinObjectKind::ObjectPrototype},
      {&WellKnownName::RegExp, BuiltinObjectKind::RegExpPrototype},
      {&WellKnownName::String, BuiltinObjectKind::StringPrototype},
  };

  for (auto& builtin : prototypes) {
    if (name == cx->parserNames().*(builtin.name)) {
      return builtin.kind;
    }
  }
  return BuiltinObjectKind::None;
}

JSObject* js::MaybeGetBuiltinObject(GlobalObject* global,
                                    BuiltinObjectKind kind) {
  JSProtoKey key = ToProtoKey(kind);
  if (IsPrototype(kind)) {
    return global->maybeGetPrototype(key);
  }
  return global->maybeGetConstructor(key);
}

JSObject* js::GetOrCreateBuiltinObject(JSContext* cx, BuiltinObjectKind kind) {
  JSProtoKey key = ToProtoKey(kind);
  if (IsPrototype(kind)) {
    return GlobalObject::getOrCreatePrototype(cx, key);
  }
  return GlobalObject::getOrCreateConstructor(cx, key);
}

const char* js::BuiltinObjectName(BuiltinObjectKind kind) {
  switch (kind) {
    case BuiltinObjectKind::Array:
      return "Array";
    case BuiltinObjectKind::ArrayBuffer:
      return "ArrayBuffer";
    case BuiltinObjectKind::Iterator:
      return "Iterator";
    case BuiltinObjectKind::Promise:
      return "Promise";
    case BuiltinObjectKind::RegExp:
      return "RegExp";
    case BuiltinObjectKind::SharedArrayBuffer:
      return "SharedArrayBuffer";

    case BuiltinObjectKind::FunctionPrototype:
      return "Function.prototype";
    case BuiltinObjectKind::ObjectPrototype:
      return "Object.prototype";
    case BuiltinObjectKind::RegExpPrototype:
      return "RegExp.prototype";
    case BuiltinObjectKind::StringPrototype:
      return "String.prototype";

    case BuiltinObjectKind::DateTimeFormat:
      return "DateTimeFormat";
    case BuiltinObjectKind::NumberFormat:
      return "NumberFormat";

    case BuiltinObjectKind::None:
      break;
  }
  MOZ_CRASH("Unexpected builtin object kind");
}