7777import com .oracle .truffle .api .profiles .ValueProfile ;
7878import com .oracle .truffle .js .builtins .ArrayPrototypeBuiltinsFactory .DeleteAndSetLengthNodeGen ;
7979import com .oracle .truffle .js .builtins .ArrayPrototypeBuiltinsFactory .FlattenIntoArrayNodeGen ;
80+ import com .oracle .truffle .js .builtins .ArrayPrototypeBuiltinsFactory .JSArrayAtNodeGen ;
8081import com .oracle .truffle .js .builtins .ArrayPrototypeBuiltinsFactory .JSArrayConcatNodeGen ;
8182import com .oracle .truffle .js .builtins .ArrayPrototypeBuiltinsFactory .JSArrayCopyWithinNodeGen ;
8283import com .oracle .truffle .js .builtins .ArrayPrototypeBuiltinsFactory .JSArrayEveryNodeGen ;
@@ -209,8 +210,6 @@ public enum ArrayPrototype implements BuiltinEnum<ArrayPrototype> {
209210 splice (2 ),
210211 every (1 ),
211212 filter (1 ),
212- flat (0 ),
213- flatMap (1 ),
214213 forEach (1 ),
215214 some (1 ),
216215 map (1 ),
@@ -219,7 +218,7 @@ public enum ArrayPrototype implements BuiltinEnum<ArrayPrototype> {
219218 reduceRight (1 ),
220219 reverse (0 ),
221220
222- // ES6
221+ // ES6 / ES2015
223222 find (1 ),
224223 findIndex (1 ),
225224 fill (1 ),
@@ -228,8 +227,15 @@ public enum ArrayPrototype implements BuiltinEnum<ArrayPrototype> {
228227 values (0 ),
229228 entries (0 ),
230229
231- // ES7
232- includes (1 );
230+ // ES2016
231+ includes (1 ),
232+
233+ // ES2019
234+ flat (0 ),
235+ flatMap (1 ),
236+
237+ // ES2022
238+ at (1 );
233239
234240 private final int length ;
235241
@@ -245,11 +251,13 @@ public int getLength() {
245251 @ Override
246252 public int getECMAScriptVersion () {
247253 if (EnumSet .of (find , findIndex , fill , copyWithin , keys , values , entries ).contains (this )) {
248- return 6 ;
254+ return JSConfig . ECMAScript2015 ;
249255 } else if (this == includes ) {
250- return 7 ;
256+ return JSConfig . ECMAScript2016 ;
251257 } else if (EnumSet .of (flat , flatMap ).contains (this )) {
252- return 10 ;
258+ return JSConfig .ECMAScript2019 ;
259+ } else if (this == at ) {
260+ return JSConfig .ECMAScript2022 ;
253261 }
254262 return BuiltinEnum .super .getECMAScriptVersion ();
255263 }
@@ -323,6 +331,9 @@ protected Object createNode(JSContext context, JSBuiltin builtin, boolean constr
323331 return JSArrayFlatMapNodeGen .create (context , builtin , args ().withThis ().fixedArgs (2 ).createArgumentNodes (context ));
324332 case flat :
325333 return JSArrayFlatNodeGen .create (context , builtin , args ().withThis ().fixedArgs (3 ).createArgumentNodes (context ));
334+
335+ case at :
336+ return JSArrayAtNodeGen .create (context , builtin , args ().withThis ().fixedArgs (1 ).createArgumentNodes (context ));
326337 }
327338 return null ;
328339 }
@@ -3197,4 +3208,26 @@ protected DynamicObject doNotJSObject(VirtualFrame frame, Object thisObj,
31973208 }
31983209 }
31993210
3211+ public abstract static class JSArrayAtNode extends JSArrayOperationWithToInt {
3212+ public JSArrayAtNode (JSContext context , JSBuiltin builtin ) {
3213+ super (context , builtin , false );
3214+ }
3215+
3216+ @ Specialization
3217+ protected Object at (Object thisObj , Object index ) {
3218+ final Object o = toObject (thisObj );
3219+ final long length = getLength (o );
3220+ long relativeIndex = toIntegerAsLong (index );
3221+ long k ;
3222+ if (relativeIndex >= 0 ) {
3223+ k = relativeIndex ;
3224+ } else {
3225+ k = length + relativeIndex ;
3226+ }
3227+ if (k < 0 || k >= length ) {
3228+ return Undefined .instance ;
3229+ }
3230+ return read (o , k );
3231+ }
3232+ }
32003233}
0 commit comments