summaryrefslogtreecommitdiffstats
path: root/js/src/builtin/Reflect.cpp
blob: 4c2e3f17422506dccc2da9a9ef728dcc47e7df49 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* -*- 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 "builtin/Reflect.h"

#include "jsapi.h"

#include "builtin/Object.h"
#include "jit/InlinableNatives.h"
#include "js/friend/ErrorMessages.h"  // js::GetErrorMessage, JSMSG_NOT_EXPECTED_TYPE
#include "js/PropertySpec.h"
#include "vm/GlobalObject.h"
#include "vm/JSContext.h"
#include "vm/PlainObject.h"

#include "vm/GeckoProfiler-inl.h"
#include "vm/JSObject-inl.h"
#include "vm/ObjectOperations-inl.h"

using namespace js;

/*** Reflect methods ********************************************************/

/* ES6 26.1.4 Reflect.deleteProperty (target, propertyKey) */
static bool Reflect_deleteProperty(JSContext* cx, unsigned argc, Value* vp) {
  CallArgs args = CallArgsFromVp(argc, vp);

  // Step 1.
  RootedObject target(
      cx,
      RequireObjectArg(cx, "`target`", "Reflect.deleteProperty", args.get(0)));
  if (!target) {
    return false;
  }

  // Steps 2-3.
  RootedValue propertyKey(cx, args.get(1));
  RootedId key(cx);
  if (!ToPropertyKey(cx, propertyKey, &key)) {
    return false;
  }

  // Step 4.
  ObjectOpResult result;
  if (!DeleteProperty(cx, target, key, result)) {
    return false;
  }
  args.rval().setBoolean(result.ok());
  return true;
}

/* ES6 26.1.8 Reflect.getPrototypeOf(target) */
bool js::Reflect_getPrototypeOf(JSContext* cx, unsigned argc, Value* vp) {
  CallArgs args = CallArgsFromVp(argc, vp);

  // Step 1.
  RootedObject target(
      cx,
      RequireObjectArg(cx, "`target`", "Reflect.getPrototypeOf", args.get(0)));
  if (!target) {
    return false;
  }

  // Step 2.
  RootedObject proto(cx);
  if (!GetPrototype(cx, target, &proto)) {
    return false;
  }
  args.rval().setObjectOrNull(proto);
  return true;
}

/* ES6 draft 26.1.10 Reflect.isExtensible(target) */
bool js::Reflect_isExtensible(JSContext* cx, unsigned argc, Value* vp) {
  CallArgs args = CallArgsFromVp(argc, vp);

  // Step 1.
  RootedObject target(
      cx,
      RequireObjectArg(cx, "`target`", "Reflect.isExtensible", args.get(0)));
  if (!target) {
    return false;
  }

  // Step 2.
  bool extensible;
  if (!IsExtensible(cx, target, &extensible)) {
    return false;
  }
  args.rval().setBoolean(extensible);
  return true;
}

// ES2018 draft rev c164be80f7ea91de5526b33d54e5c9321ed03d3f
// 26.1.10 Reflect.ownKeys ( target )
bool js::Reflect_ownKeys(JSContext* cx, unsigned argc, Value* vp) {
  AutoJSMethodProfilerEntry pseudoFrame(cx, "Reflect", "ownKeys");
  CallArgs args = CallArgsFromVp(argc, vp);

  // Step 1.
  RootedObject target(
      cx, RequireObjectArg(cx, "`target`", "Reflect.ownKeys", args.get(0)));
  if (!target) {
    return false;
  }

  // Steps 2-3.
  return GetOwnPropertyKeys(
      cx, target, JSITER_OWNONLY | JSITER_HIDDEN | JSITER_SYMBOLS, args.rval());
}

/* ES6 26.1.12 Reflect.preventExtensions(target) */
static bool Reflect_preventExtensions(JSContext* cx, unsigned argc, Value* vp) {
  CallArgs args = CallArgsFromVp(argc, vp);

  // Step 1.
  RootedObject target(
      cx, RequireObjectArg(cx, "`target`", "Reflect.preventExtensions",
                           args.get(0)));
  if (!target) {
    return false;
  }

  // Step 2.
  ObjectOpResult result;
  if (!PreventExtensions(cx, target, result)) {
    return false;
  }
  args.rval().setBoolean(result.ok());
  return true;
}

/* ES6 26.1.13 Reflect.set(target, propertyKey, V [, receiver]) */
static bool Reflect_set(JSContext* cx, unsigned argc, Value* vp) {
  CallArgs args = CallArgsFromVp(argc, vp);

  // Step 1.
  RootedObject target(
      cx, RequireObjectArg(cx, "`target`", "Reflect.set", args.get(0)));
  if (!target) {
    return false;
  }

  // Steps 2-3.
  RootedValue propertyKey(cx, args.get(1));
  RootedId key(cx);
  if (!ToPropertyKey(cx, propertyKey, &key)) {
    return false;
  }

  // Step 4.
  RootedValue receiver(cx, args.length() > 3 ? args[3] : args.get(0));

  // Step 5.
  ObjectOpResult result;
  RootedValue value(cx, args.get(2));
  if (!SetProperty(cx, target, key, value, receiver, result)) {
    return false;
  }
  args.rval().setBoolean(result.ok());
  return true;
}

/*
 * ES6 26.1.3 Reflect.setPrototypeOf(target, proto)
 *
 * The specification is not quite similar enough to Object.setPrototypeOf to
 * share code.
 */
static bool Reflect_setPrototypeOf(JSContext* cx, unsigned argc, Value* vp) {
  CallArgs args = CallArgsFromVp(argc, vp);

  // Step 1.
  RootedObject obj(cx, RequireObjectArg(cx, "`target`",
                                        "Reflect.setPrototypeOf", args.get(0)));
  if (!obj) {
    return false;
  }

  // Step 2.
  if (!args.get(1).isObjectOrNull()) {
    JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
                              JSMSG_NOT_EXPECTED_TYPE, "Reflect.setPrototypeOf",
                              "an object or null",
                              InformalValueTypeName(args.get(1)));
    return false;
  }
  RootedObject proto(cx, args.get(1).toObjectOrNull());

  // Step 4.
  ObjectOpResult result;
  if (!SetPrototype(cx, obj, proto, result)) {
    return false;
  }
  args.rval().setBoolean(result.ok());
  return true;
}

static const JSFunctionSpec reflect_methods[] = {
    JS_SELF_HOSTED_FN("apply", "Reflect_apply", 3, 0),
    JS_SELF_HOSTED_FN("construct", "Reflect_construct", 2, 0),
    JS_SELF_HOSTED_FN("defineProperty", "Reflect_defineProperty", 3, 0),
    JS_FN("deleteProperty", Reflect_deleteProperty, 2, 0),
    JS_SELF_HOSTED_FN("get", "Reflect_get", 2, 0),
    JS_SELF_HOSTED_FN("getOwnPropertyDescriptor",
                      "Reflect_getOwnPropertyDescriptor", 2, 0),
    JS_INLINABLE_FN("getPrototypeOf", Reflect_getPrototypeOf, 1, 0,
                    ReflectGetPrototypeOf),
    JS_SELF_HOSTED_FN("has", "Reflect_has", 2, 0),
    JS_FN("isExtensible", Reflect_isExtensible, 1, 0),
    JS_FN("ownKeys", Reflect_ownKeys, 1, 0),
    JS_FN("preventExtensions", Reflect_preventExtensions, 1, 0),
    JS_FN("set", Reflect_set, 3, 0),
    JS_FN("setPrototypeOf", Reflect_setPrototypeOf, 2, 0),
    JS_FS_END};

static const JSPropertySpec reflect_properties[] = {
    JS_STRING_SYM_PS(toStringTag, "Reflect", JSPROP_READONLY), JS_PS_END};

/*** Setup ******************************************************************/

static JSObject* CreateReflectObject(JSContext* cx, JSProtoKey key) {
  RootedObject proto(cx, &cx->global()->getObjectPrototype());
  return NewPlainObjectWithProto(cx, proto, TenuredObject);
}

static const ClassSpec ReflectClassSpec = {CreateReflectObject, nullptr,
                                           reflect_methods, reflect_properties};

const JSClass js::ReflectClass = {"Reflect", 0, JS_NULL_CLASS_OPS,
                                  &ReflectClassSpec};