Skip to content

Commit 62ce0fe

Browse files
committed
handle more types in ByteSequenceStorage
1 parent 3204b58 commit 62ce0fe

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/ByteSequenceStorage.java

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.Arrays;
3232

3333
import com.oracle.graal.python.PythonLanguage;
34+
import com.oracle.graal.python.builtins.objects.ints.PInt;
3435
import com.oracle.graal.python.runtime.sequence.SequenceUtil;
3536
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
3637

@@ -377,19 +378,36 @@ public int indexOfInt(int value) {
377378
public void append(Object value) throws SequenceStoreException {
378379
if (value instanceof Integer) {
379380
appendInt((int) value);
381+
} else if (value instanceof Long) {
382+
appendLong((long) value);
383+
} else if (value instanceof PInt) {
384+
try {
385+
appendInt(((PInt) value).intValueExact());
386+
} catch (ArithmeticException e) {
387+
throw SequenceStoreException.INSTANCE;
388+
}
380389
} else if (value instanceof Byte) {
381390
appendByte((byte) value);
382391
} else {
383392
throw SequenceStoreException.INSTANCE;
384393
}
385394
}
386395

396+
public void appendLong(long value) {
397+
if (value < 0 || value >= 256) {
398+
throw SequenceStoreException.INSTANCE;
399+
}
400+
ensureCapacity(length + 1);
401+
values[length] = (byte) value;
402+
length++;
403+
}
404+
387405
public void appendInt(int value) {
388406
if (value < 0 || value >= 256) {
389407
throw SequenceStoreException.INSTANCE;
390408
}
391409
ensureCapacity(length + 1);
392-
values[length] = ((Integer) value).byteValue();
410+
values[length] = (byte) value;
393411
length++;
394412
}
395413

@@ -405,6 +423,8 @@ public void extend(SequenceStorage other) throws SequenceStoreException {
405423
extendWithByteStorage((ByteSequenceStorage) other);
406424
} else if (other instanceof IntSequenceStorage) {
407425
extendWithIntStorage((IntSequenceStorage) other);
426+
} else if (other instanceof ObjectSequenceStorage) {
427+
extendWithObjectStorage((ObjectSequenceStorage) other);
408428
} else {
409429
throw SequenceStoreException.INSTANCE;
410430
}
@@ -432,7 +452,35 @@ private void extendWithIntStorage(IntSequenceStorage other) {
432452
if (otherValue < 0 || otherValue >= 256) {
433453
throw SequenceStoreException.INSTANCE;
434454
}
435-
values[i] = ((Integer) otherValue).byteValue();
455+
values[i] = (byte) otherValue;
456+
}
457+
458+
length = extendedLength;
459+
}
460+
461+
private void extendWithObjectStorage(ObjectSequenceStorage other) {
462+
int extendedLength = length + other.length();
463+
ensureCapacity(extendedLength);
464+
Object[] otherValues = other.getInternalArray();
465+
466+
for (int i = length, j = 0; i < extendedLength; i++, j++) {
467+
Object otherValue = otherValues[j];
468+
long value = 0;
469+
if (otherValue instanceof Integer) {
470+
value = (int) otherValue;
471+
} else if (otherValue instanceof Long) {
472+
value = (long) otherValue;
473+
} else if (otherValue instanceof PInt) {
474+
try {
475+
value = ((PInt) otherValue).intValueExact();
476+
} catch (ArithmeticException e) {
477+
throw SequenceStoreException.INSTANCE;
478+
}
479+
}
480+
if (value < 0 || value >= 256) {
481+
throw SequenceStoreException.INSTANCE;
482+
}
483+
values[i] = (byte) value;
436484
}
437485

438486
length = extendedLength;

0 commit comments

Comments
 (0)