Skip to content

Commit 6387ead

Browse files
committed
Refactor constant array class hierarchy.
(cherry picked from commit a451d88)
1 parent 3b5a6a1 commit 6387ead

12 files changed

+158
-195
lines changed

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/array/dyn/AbstractConstantArray.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,6 @@ public final Object getElementInBounds(JSDynamicObject object, long index) {
8585

8686
public abstract Object getElementInBounds(JSDynamicObject object, int index);
8787

88-
@Override
89-
public long length(JSDynamicObject object) {
90-
return lengthInt(object);
91-
}
92-
9388
@Override
9489
public long firstElementIndex(JSDynamicObject object) {
9590
return 0;

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/array/dyn/AbstractConstantEmptyArray.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,24 @@ private AbstractObjectArray createWritableObjectContiguous(JSDynamicObject objec
251251
return ContiguousObjectArray.makeContiguousObjectArray(object, capacity, initialArray, indexOffset, arrayOffset, 0, integrityLevel);
252252
}
253253

254+
@Override
255+
public ScriptArray setLengthImpl(JSDynamicObject object, long length, Node node, SetLengthProfileAccess profile) {
256+
setCapacity(object, length);
257+
return this;
258+
}
259+
260+
@Override
261+
public ScriptArray removeRangeImpl(JSDynamicObject object, long start, long end) {
262+
setCapacity(object, getCapacity(object) - (end - start));
263+
return this;
264+
}
265+
266+
@Override
267+
public ScriptArray addRangeImpl(JSDynamicObject object, long offset, int size) {
268+
setCapacity(object, getCapacity(object) + size);
269+
return this;
270+
}
271+
254272
@Override
255273
public boolean isHolesType() {
256274
return true;
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (c) 2025, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
42+
package com.oracle.truffle.js.runtime.array.dyn;
43+
44+
import static com.oracle.truffle.js.runtime.builtins.JSAbstractArray.arrayGetLength;
45+
46+
import com.oracle.truffle.api.nodes.Node;
47+
import com.oracle.truffle.js.runtime.array.ScriptArray;
48+
import com.oracle.truffle.js.runtime.objects.JSDynamicObject;
49+
50+
/**
51+
* Common base class for lazy array strategies.
52+
*/
53+
public abstract class AbstractConstantLazyArray extends AbstractConstantArray {
54+
55+
protected AbstractConstantLazyArray(int integrityLevel, DynamicArrayCache cache) {
56+
super(integrityLevel, cache);
57+
}
58+
59+
@Override
60+
public final boolean hasElement(JSDynamicObject object, long index) {
61+
return index >= 0 && index < lengthInt(object);
62+
}
63+
64+
@Override
65+
public final long length(JSDynamicObject object) {
66+
return lengthInt(object);
67+
}
68+
69+
@Override
70+
public final int lengthInt(JSDynamicObject object) {
71+
return (int) arrayGetLength(object);
72+
}
73+
74+
@Override
75+
public final AbstractWritableArray createWriteableDouble(JSDynamicObject object, long index, double value, Node node, CreateWritableProfileAccess profile) {
76+
return createWriteableObject(object, index, value, node, profile);
77+
}
78+
79+
@Override
80+
public final AbstractWritableArray createWriteableInt(JSDynamicObject object, long index, int value, Node node, CreateWritableProfileAccess profile) {
81+
return createWriteableObject(object, index, value, node, profile);
82+
}
83+
84+
@Override
85+
public final AbstractWritableArray createWriteableJSObject(JSDynamicObject object, long index, JSDynamicObject value, Node node, CreateWritableProfileAccess profile) {
86+
return createWriteableObject(object, index, value, node, profile);
87+
}
88+
89+
@Override
90+
public final ScriptArray deleteElementImpl(JSDynamicObject object, long index, boolean strict) {
91+
return createWriteableObject(object, index, null, null, CreateWritableProfileAccess.getUncached()).deleteElementImpl(object, index, strict);
92+
}
93+
94+
@Override
95+
public final ScriptArray setLengthImpl(JSDynamicObject object, long len, Node node, SetLengthProfileAccess profile) {
96+
return createWriteableObject(object, len - 1, null, node, profile).setLengthImpl(object, len, node, profile);
97+
}
98+
99+
@Override
100+
public final ScriptArray removeRangeImpl(JSDynamicObject object, long start, long end) {
101+
return createWriteableObject(object, start, null, null, CreateWritableProfileAccess.getUncached()).removeRangeImpl(object, start, end);
102+
}
103+
104+
@Override
105+
public final ScriptArray addRangeImpl(JSDynamicObject object, long offset, int size) {
106+
return createWriteableObject(object, offset, null, null, CreateWritableProfileAccess.getUncached()).addRangeImpl(object, offset, size);
107+
}
108+
}

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/array/dyn/ConstantByteArray.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -73,6 +73,11 @@ public static int getElementByte(JSDynamicObject object, int index) {
7373
return getArray(object)[index];
7474
}
7575

76+
@Override
77+
public long length(JSDynamicObject object) {
78+
return lengthInt(object);
79+
}
80+
7681
@Override
7782
public int lengthInt(JSDynamicObject object) {
7883
return getArray(object).length;

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/array/dyn/ConstantDoubleArray.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -73,6 +73,11 @@ private static double[] getArray(JSDynamicObject object) {
7373
return (double[]) arrayGetArray(object);
7474
}
7575

76+
@Override
77+
public long length(JSDynamicObject object) {
78+
return lengthInt(object);
79+
}
80+
7681
@Override
7782
public int lengthInt(JSDynamicObject object) {
7883
return getArray(object).length;

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/array/dyn/ConstantEmptyArray.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -40,10 +40,7 @@
4040
*/
4141
package com.oracle.truffle.js.runtime.array.dyn;
4242

43-
import com.oracle.truffle.api.nodes.Node;
4443
import com.oracle.truffle.js.runtime.array.DynamicArray;
45-
import com.oracle.truffle.js.runtime.array.ScriptArray;
46-
import com.oracle.truffle.js.runtime.objects.JSDynamicObject;
4744

4845
public final class ConstantEmptyArray extends AbstractConstantEmptyArray {
4946
private static final ConstantEmptyArray EMPTY_ARRAY = new ConstantEmptyArray(INTEGRITY_LEVEL_NONE, createCache()).maybePreinitializeCache();
@@ -56,24 +53,6 @@ private ConstantEmptyArray(int integrityLevel, DynamicArrayCache cache) {
5653
super(integrityLevel, cache);
5754
}
5855

59-
@Override
60-
public ScriptArray setLengthImpl(JSDynamicObject object, long length, Node node, SetLengthProfileAccess profile) {
61-
setCapacity(object, length);
62-
return this;
63-
}
64-
65-
@Override
66-
public ScriptArray removeRangeImpl(JSDynamicObject object, long start, long end) {
67-
setCapacity(object, getCapacity(object) - (end - start));
68-
return this;
69-
}
70-
71-
@Override
72-
public ScriptArray addRangeImpl(JSDynamicObject object, long offset, int size) {
73-
setCapacity(object, getCapacity(object) + size);
74-
return this;
75-
}
76-
7756
@Override
7857
protected DynamicArray withIntegrityLevel(int newIntegrityLevel) {
7958
return new ConstantEmptyArray(newIntegrityLevel, cache);

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/array/dyn/ConstantEmptyPrototypeArray.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -68,12 +68,6 @@ private static Assumption getArrayPrototypeNoElementsAssumption(JSDynamicObject
6868
return JSObject.getJSContext(object).getArrayPrototypeNoElementsAssumption();
6969
}
7070

71-
@Override
72-
public ScriptArray setLengthImpl(JSDynamicObject object, long length, Node node, SetLengthProfileAccess profile) {
73-
setCapacity(object, length);
74-
return this;
75-
}
76-
7771
@Override
7872
public AbstractIntArray createWriteableInt(JSDynamicObject object, long index, int value, Node node, CreateWritableProfileAccess profile) {
7973
getArrayPrototypeNoElementsAssumption(object).invalidate(JSAbstractArray.ARRAY_PROTOTYPE_NO_ELEMENTS_INVALIDATION);
@@ -98,18 +92,6 @@ public AbstractObjectArray createWriteableObject(JSDynamicObject object, long in
9892
return super.createWriteableObject(object, index, value, node, profile);
9993
}
10094

101-
@Override
102-
public ScriptArray removeRangeImpl(JSDynamicObject object, long start, long end) {
103-
setCapacity(object, getCapacity(object) - (end - start));
104-
return this;
105-
}
106-
107-
@Override
108-
public ScriptArray addRangeImpl(JSDynamicObject object, long offset, int size) {
109-
setCapacity(object, getCapacity(object) + size);
110-
return this;
111-
}
112-
11395
@Override
11496
protected DynamicArray withIntegrityLevel(int newIntegrityLevel) {
11597
return new ConstantEmptyPrototypeArray(newIntegrityLevel, cache);

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/array/dyn/ConstantIntArray.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -73,6 +73,11 @@ private static int[] getArray(JSDynamicObject object) {
7373
return (int[]) arrayGetArray(object);
7474
}
7575

76+
@Override
77+
public long length(JSDynamicObject object) {
78+
return lengthInt(object);
79+
}
80+
7681
@Override
7782
public int lengthInt(JSDynamicObject object) {
7883
return getArray(object).length;

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/array/dyn/ConstantObjectArray.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -105,6 +105,11 @@ public boolean hasHoles(JSDynamicObject object) {
105105
return holes;
106106
}
107107

108+
@Override
109+
public long length(JSDynamicObject object) {
110+
return lengthInt(object);
111+
}
112+
108113
@Override
109114
public int lengthInt(JSDynamicObject object) {
110115
return getArray(object).length;

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/array/dyn/LazyArray.java

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -42,7 +42,6 @@
4242
package com.oracle.truffle.js.runtime.array.dyn;
4343

4444
import static com.oracle.truffle.js.runtime.builtins.JSAbstractArray.arrayGetArray;
45-
import static com.oracle.truffle.js.runtime.builtins.JSAbstractArray.arrayGetLength;
4645

4746
import java.util.List;
4847

@@ -51,7 +50,6 @@
5150
import com.oracle.truffle.js.runtime.Boundaries;
5251
import com.oracle.truffle.js.runtime.JSConfig;
5352
import com.oracle.truffle.js.runtime.array.DynamicArray;
54-
import com.oracle.truffle.js.runtime.array.ScriptArray;
5553
import com.oracle.truffle.js.runtime.objects.JSDynamicObject;
5654

5755
/**
@@ -61,7 +59,7 @@
6159
* Whenever a lazy array is written to, the entire lazy array is enumerated and copied and loses its
6260
* lazy lookup property.
6361
*/
64-
public class LazyArray extends AbstractConstantArray {
62+
public class LazyArray extends AbstractConstantLazyArray {
6563

6664
private static final LazyArray LAZY_ARRAY = new LazyArray(INTEGRITY_LEVEL_NONE, createCache()).maybePreinitializeCache();
6765

@@ -91,16 +89,6 @@ public Object getElementInBounds(JSDynamicObject object, int index, ListGetNode
9189
return listGetNode.execute(arrayGetArray(object), index);
9290
}
9391

94-
@Override
95-
public boolean hasElement(JSDynamicObject object, long index) {
96-
return index >= 0 && index < lengthInt(object);
97-
}
98-
99-
@Override
100-
public int lengthInt(JSDynamicObject object) {
101-
return (int) arrayGetLength(object);
102-
}
103-
10492
@Override
10593
public AbstractWritableArray createWriteableObject(JSDynamicObject object, long index, Object value, Node node, CreateWritableProfileAccess profile) {
10694
// enumerate the whole array
@@ -117,41 +105,6 @@ public AbstractWritableArray createWriteableObject(JSDynamicObject object, long
117105
return newArray;
118106
}
119107

120-
@Override
121-
public AbstractWritableArray createWriteableDouble(JSDynamicObject object, long index, double value, Node node, CreateWritableProfileAccess profile) {
122-
return createWriteableObject(object, index, value, node, profile);
123-
}
124-
125-
@Override
126-
public AbstractWritableArray createWriteableInt(JSDynamicObject object, long index, int value, Node node, CreateWritableProfileAccess profile) {
127-
return createWriteableObject(object, index, value, node, profile);
128-
}
129-
130-
@Override
131-
public AbstractWritableArray createWriteableJSObject(JSDynamicObject object, long index, JSDynamicObject value, Node node, CreateWritableProfileAccess profile) {
132-
return createWriteableObject(object, index, value, node, profile);
133-
}
134-
135-
@Override
136-
public ScriptArray deleteElementImpl(JSDynamicObject object, long index, boolean strict) {
137-
return createWriteableObject(object, index, null, null, CreateWritableProfileAccess.getUncached()).deleteElementImpl(object, index, strict);
138-
}
139-
140-
@Override
141-
public ScriptArray setLengthImpl(JSDynamicObject object, long len, Node node, SetLengthProfileAccess profile) {
142-
return createWriteableObject(object, len - 1, null, node, profile).setLengthImpl(object, len, node, profile);
143-
}
144-
145-
@Override
146-
public ScriptArray removeRangeImpl(JSDynamicObject object, long start, long end) {
147-
return createWriteableObject(object, start, null, null, CreateWritableProfileAccess.getUncached()).removeRangeImpl(object, start, end);
148-
}
149-
150-
@Override
151-
public ScriptArray addRangeImpl(JSDynamicObject object, long offset, int size) {
152-
return createWriteableObject(object, offset, null, null, CreateWritableProfileAccess.getUncached()).addRangeImpl(object, offset, size);
153-
}
154-
155108
@Override
156109
public Object cloneArray(JSDynamicObject object) {
157110
return arrayGetLazyList(object);

0 commit comments

Comments
 (0)