hpi-swa / trufflesqueak

A Squeak/Smalltalk VM and Polyglot Programming Environment for the GraalVM.
MIT License
286 stars 14 forks source link

Empty ArrayObject stores ObjectStorage as int #152

Closed janehmueller closed 3 years ago

janehmueller commented 3 years ago

If I have an empty ArrayObject and call getObjectStorage on it, I get the following exception:

java.lang.ClassCastException: class java.lang.Integer cannot be cast to class [Ljava.lang.Object; (java.lang.Integer and [Ljava.lang.Object; are in module java.base of loader 'bootstrap')
    at de.hpi.swa.trufflesqueak.model.ArrayObject.getObjectStorage(ArrayObject.java:207)
    at de.hpi.swa.trufflesqueak.model.ArrayObject.getObjectLength(ArrayObject.java:202)
    at de.hpi.swa.trufflesqueak.morphicswing.commands.MorphNodeCommand.redrawDamagedRectJava(MorphNodeCommand.java:437)

This seems to be caused by the storage object being an integer instead of an array. One has to use size first to check if the ArrayObject contains any elements. getObjectLength can not be used, since it also uses getObjectStorage

fniephaus commented 3 years ago

This is expected behavior as TruffleSqueak implements storage strategies to optimize guest language arrays. You are directly accessing the actual Java object representing the Smalltalk object without considering the optimization. The correct way to access ArrayObjects is to use the ArrayObjectNodes but that can be a bit tricky via interop.

If you want to directly access the object, you can only use getObjectStorage() if the object returns true for isObjectType(). Otherwise it can be isEmptyType(), isLongType(), and so on.

TruffleSqueak's VM introspection capabilities are quite powerful, but you know what comes with great power. 😉