Skip to content

Commit 0c02a22

Browse files
aardvark179gbrail
authored andcommitted
Convert NativeReflect to descriptors.
1 parent 668c128 commit 0c02a22

1 file changed

Lines changed: 66 additions & 52 deletions

File tree

rhino/src/main/java/org/mozilla/javascript/NativeReflect.java

Lines changed: 66 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
package org.mozilla.javascript;
88

9+
import static org.mozilla.javascript.ClassDescriptor.Builder.value;
10+
import static org.mozilla.javascript.ClassDescriptor.Destination.CTOR;
11+
912
import java.io.Serial;
1013
import java.util.ArrayList;
1114
import java.util.List;
@@ -21,32 +24,37 @@ final class NativeReflect extends ScriptableObject {
2124

2225
private static final String REFLECT_TAG = "Reflect";
2326

27+
private static final ClassDescriptor DESCRIPTOR;
28+
29+
static {
30+
DESCRIPTOR =
31+
new ClassDescriptor.Builder(REFLECT_TAG)
32+
.withMethod(CTOR, "apply", 3, NativeReflect::apply)
33+
.withMethod(CTOR, "construct", 2, NativeReflect::construct)
34+
.withMethod(CTOR, "defineProperty", 3, NativeReflect::defineProperty)
35+
.withMethod(CTOR, "deleteProperty", 2, NativeReflect::deleteProperty)
36+
.withMethod(CTOR, "get", 2, NativeReflect::get)
37+
.withMethod(
38+
CTOR,
39+
"getOwnPropertyDescriptor",
40+
2,
41+
NativeReflect::getOwnPropertyDescriptor)
42+
.withMethod(CTOR, "getPrototypeOf", 1, NativeReflect::getPrototypeOf)
43+
.withMethod(CTOR, "has", 2, NativeReflect::has)
44+
.withMethod(CTOR, "isExtensible", 1, NativeReflect::isExtensible)
45+
.withMethod(CTOR, "ownKeys", 1, NativeReflect::ownKeys)
46+
.withMethod(CTOR, "preventExtensions", 1, NativeReflect::preventExtensions)
47+
.withMethod(CTOR, "set", 3, NativeReflect::set)
48+
.withMethod(CTOR, "setPrototypeOf", 2, NativeReflect::setPrototypeOf)
49+
.withProp(
50+
CTOR,
51+
SymbolKey.TO_STRING_TAG,
52+
value(REFLECT_TAG, DONTENUM | READONLY))
53+
.build();
54+
}
55+
2456
public static Object init(Context cx, VarScope scope, boolean sealed) {
25-
NativeReflect reflect = new NativeReflect();
26-
reflect.setPrototype(getObjectPrototype(scope));
27-
reflect.setParentScope(scope);
28-
29-
reflect.defineBuiltinProperty(scope, "apply", 3, NativeReflect::apply);
30-
reflect.defineBuiltinProperty(scope, "construct", 2, NativeReflect::construct);
31-
reflect.defineBuiltinProperty(scope, "defineProperty", 3, NativeReflect::defineProperty);
32-
reflect.defineBuiltinProperty(scope, "deleteProperty", 2, NativeReflect::deleteProperty);
33-
reflect.defineBuiltinProperty(scope, "get", 2, NativeReflect::get);
34-
reflect.defineBuiltinProperty(
35-
scope, "getOwnPropertyDescriptor", 2, NativeReflect::getOwnPropertyDescriptor);
36-
reflect.defineBuiltinProperty(scope, "getPrototypeOf", 1, NativeReflect::getPrototypeOf);
37-
reflect.defineBuiltinProperty(scope, "has", 2, NativeReflect::has);
38-
reflect.defineBuiltinProperty(scope, "isExtensible", 1, NativeReflect::isExtensible);
39-
reflect.defineBuiltinProperty(scope, "ownKeys", 1, NativeReflect::ownKeys);
40-
reflect.defineBuiltinProperty(
41-
scope, "preventExtensions", 1, NativeReflect::preventExtensions);
42-
reflect.defineBuiltinProperty(scope, "set", 3, NativeReflect::set);
43-
reflect.defineBuiltinProperty(scope, "setPrototypeOf", 2, NativeReflect::setPrototypeOf);
44-
45-
reflect.defineProperty(SymbolKey.TO_STRING_TAG, REFLECT_TAG, DONTENUM | READONLY);
46-
if (sealed) {
47-
reflect.sealObject();
48-
}
49-
return reflect;
57+
return DESCRIPTOR.populateGlobal(cx, scope, new NativeObject(), sealed);
5058
}
5159

5260
private NativeReflect() {}
@@ -56,7 +64,8 @@ public String getClassName() {
5664
return "Reflect";
5765
}
5866

59-
private static Object apply(Context cx, VarScope scope, Object thisObj, Object[] args) {
67+
private static Object apply(
68+
Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args) {
6069
if (args.length < 3) {
6170
throw ScriptRuntime.typeErrorById(
6271
"msg.method.missing.parameter",
@@ -70,7 +79,7 @@ private static Object apply(Context cx, VarScope scope, Object thisObj, Object[]
7079
if (args[1] instanceof Scriptable) {
7180
thisObj = (Scriptable) args[1];
7281
} else if (ScriptRuntime.isPrimitive(args[1])) {
73-
thisObj = cx.newObject(scope, "Object", new Object[] {args[1]});
82+
thisObj = cx.newObject(s, "Object", new Object[] {args[1]});
7483
}
7584

7685
if (ScriptRuntime.isSymbol(args[2])) {
@@ -79,15 +88,15 @@ private static Object apply(Context cx, VarScope scope, Object thisObj, Object[]
7988
ScriptableObject argumentsList = ScriptableObject.ensureScriptableObject(args[2]);
8089

8190
return ScriptRuntime.applyOrCall(
82-
true, cx, scope, callable, new Object[] {thisObj, argumentsList});
91+
true, cx, s, callable, new Object[] {thisObj, argumentsList});
8392
}
8493

8594
/**
8695
* see <a href="https://262.ecma-international.org/12.0/#sec-reflect.construct">28.1.2
8796
* Reflect.construct (target, argumentsList[, newTarget])</a>
8897
*/
8998
private static Scriptable construct(
90-
Context cx, VarScope scope, Scriptable thisObj, Object[] args) {
99+
Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args) {
91100
/*
92101
* 1. If IsConstructor(target) is false, throw a TypeError exception.
93102
* 2. If newTarget is not present, set newTarget to target.
@@ -109,7 +118,7 @@ private static Scriptable construct(
109118

110119
Constructable ctor = (Constructable) args[0];
111120
if (args.length < 2) {
112-
return ctor.construct(cx, scope, ScriptRuntime.emptyArgs);
121+
return ctor.construct(cx, s, ScriptRuntime.emptyArgs);
113122
}
114123

115124
if (args.length > 2 && !AbstractEcmaObjectOperations.isConstructor(cx, args[2])) {
@@ -140,11 +149,11 @@ private static Scriptable construct(
140149
// the prototype before executing call(..).
141150
if (ctor instanceof BaseFunction && newTargetPrototype != null) {
142151
BaseFunction ctorBaseFunction = (BaseFunction) ctor;
143-
Scriptable result = ctorBaseFunction.createObject(cx, scope);
152+
Scriptable result = ctorBaseFunction.createObject(cx, s);
144153
if (result != null) {
145154
result.setPrototype((Scriptable) newTargetPrototype);
146155

147-
Object val = ctorBaseFunction.call(cx, scope, result, callArgs);
156+
Object val = ctorBaseFunction.call(cx, s, result, callArgs);
148157
if (val instanceof Scriptable) {
149158
return (Scriptable) val;
150159
}
@@ -153,7 +162,7 @@ private static Scriptable construct(
153162
}
154163
}
155164

156-
Scriptable newScriptable = ctor.construct(cx, scope, callArgs);
165+
Scriptable newScriptable = ctor.construct(cx, s, callArgs);
157166
if (newTargetPrototype != null) {
158167
newScriptable.setPrototype((Scriptable) newTargetPrototype);
159168
}
@@ -162,7 +171,7 @@ private static Scriptable construct(
162171
}
163172

164173
private static Object defineProperty(
165-
Context cx, VarScope scope, Object thisObj, Object[] args) {
174+
Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args) {
166175
if (args.length < 3) {
167176
throw ScriptRuntime.typeErrorById(
168177
"msg.method.missing.parameter",
@@ -192,7 +201,7 @@ private static Object defineProperty(
192201
}
193202

194203
private static Object deleteProperty(
195-
Context cx, VarScope scope, Object thisObj, Object[] args) {
204+
Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args) {
196205
ScriptableObject target = checkTarget(args);
197206

198207
if (args.length > 1) {
@@ -205,7 +214,8 @@ private static Object deleteProperty(
205214
return false;
206215
}
207216

208-
private static Object get(Context cx, VarScope scope, Object thisObj, Object[] args) {
217+
private static Object get(
218+
Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args) {
209219
ScriptableObject target = checkTarget(args);
210220

211221
if (args.length > 1) {
@@ -225,29 +235,30 @@ private static Object get(Context cx, VarScope scope, Object thisObj, Object[] a
225235
}
226236

227237
private static Scriptable getOwnPropertyDescriptor(
228-
Context cx, VarScope scope, Object thisObj, Object[] args) {
238+
Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args) {
229239
ScriptableObject target = checkTarget(args);
230240

231241
if (args.length > 1) {
232242
if (ScriptRuntime.isSymbol(args[1])) {
233243
var desc = target.getOwnPropertyDescriptor(cx, args[1]);
234-
return desc == null ? Undefined.SCRIPTABLE_UNDEFINED : desc.toObject(scope);
244+
return desc == null ? Undefined.SCRIPTABLE_UNDEFINED : desc.toObject(s);
235245
}
236246

237247
var desc = target.getOwnPropertyDescriptor(cx, ScriptRuntime.toString(args[1]));
238-
return desc == null ? Undefined.SCRIPTABLE_UNDEFINED : desc.toObject(scope);
248+
return desc == null ? Undefined.SCRIPTABLE_UNDEFINED : desc.toObject(s);
239249
}
240250
return Undefined.SCRIPTABLE_UNDEFINED;
241251
}
242252

243253
private static Scriptable getPrototypeOf(
244-
Context cx, VarScope scope, Object thisObj, Object[] args) {
254+
Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args) {
245255
ScriptableObject target = checkTarget(args);
246256

247257
return target.getPrototype();
248258
}
249259

250-
private static Object has(Context cx, VarScope scope, Object thisObj, Object[] args) {
260+
private static Object has(
261+
Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args) {
251262
ScriptableObject target = checkTarget(args);
252263

253264
if (args.length > 1) {
@@ -260,12 +271,14 @@ private static Object has(Context cx, VarScope scope, Object thisObj, Object[] a
260271
return false;
261272
}
262273

263-
private static Object isExtensible(Context cx, VarScope scope, Object thisObj, Object[] args) {
274+
private static Object isExtensible(
275+
Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args) {
264276
ScriptableObject target = checkTarget(args);
265277
return target.isExtensible();
266278
}
267279

268-
private static Scriptable ownKeys(Context cx, VarScope scope, Object thisObj, Object[] args) {
280+
private static Scriptable ownKeys(
281+
Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args) {
269282
ScriptableObject target = checkTarget(args);
270283

271284
final List<Object> strings = new ArrayList<>();
@@ -287,17 +300,18 @@ private static Scriptable ownKeys(Context cx, VarScope scope, Object thisObj, Ob
287300
System.arraycopy(strings.toArray(), 0, keys, 0, strings.size());
288301
System.arraycopy(symbols.toArray(), 0, keys, strings.size(), symbols.size());
289302

290-
return cx.newArray(scope, keys);
303+
return cx.newArray(s, keys);
291304
}
292305

293306
private static Object preventExtensions(
294-
Context cx, VarScope scope, Object thisObj, Object[] args) {
307+
Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args) {
295308
ScriptableObject target = checkTarget(args);
296309

297310
return target.preventExtensions();
298311
}
299312

300-
private static Object set(Context cx, VarScope scope, Object thisObj, Object[] args) {
313+
private static Object set(
314+
Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args) {
301315
ScriptableObject target = checkTarget(args);
302316
if (args.length < 2) {
303317
return true;
@@ -310,7 +324,7 @@ private static Object set(Context cx, VarScope scope, Object thisObj, Object[] a
310324
if (descriptor != null) {
311325
Object setter = descriptor.setter;
312326
if (setter != null && setter != NOT_FOUND) {
313-
((Function) setter).call(cx, scope, receiver, new Object[] {args[2]});
327+
((Function) setter).call(cx, s, receiver, new Object[] {args[2]});
314328
return true;
315329
}
316330

@@ -323,19 +337,19 @@ private static Object set(Context cx, VarScope scope, Object thisObj, Object[] a
323337
if (ScriptRuntime.isSymbol(args[1])) {
324338
receiver.put((Symbol) args[1], receiver, args[2]);
325339
} else {
326-
StringIdOrIndex s = ScriptRuntime.toStringIdOrIndex(args[1]);
327-
if (s.stringId == null) {
328-
receiver.put(s.index, receiver, args[2]);
340+
StringIdOrIndex soi = ScriptRuntime.toStringIdOrIndex(args[1]);
341+
if (soi.stringId == null) {
342+
receiver.put(soi.index, receiver, args[2]);
329343
} else {
330-
receiver.put(s.stringId, receiver, args[2]);
344+
receiver.put(soi.stringId, receiver, args[2]);
331345
}
332346
}
333347

334348
return true;
335349
}
336350

337351
private static Object setPrototypeOf(
338-
Context cx, VarScope scope, Object thisObj, Object[] args) {
352+
Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args) {
339353
if (args.length < 2) {
340354
throw ScriptRuntime.typeErrorById(
341355
"msg.method.missing.parameter",

0 commit comments

Comments
 (0)