4141package com .oracle .truffle .object ;
4242
4343import static com .oracle .truffle .object .LayoutImpl .ACCESS ;
44+ import static com .oracle .truffle .object .UnsafeAccess .ARRAY_INT_BASE_OFFSET ;
45+ import static com .oracle .truffle .object .UnsafeAccess .ARRAY_INT_INDEX_SCALE ;
46+ import static com .oracle .truffle .object .UnsafeAccess .unsafeGetLong ;
47+ import static com .oracle .truffle .object .UnsafeAccess .unsafeGetObject ;
48+ import static com .oracle .truffle .object .UnsafeAccess .unsafePutLong ;
49+ import static com .oracle .truffle .object .UnsafeAccess .unsafePutObject ;
4450
4551import java .lang .reflect .Field ;
4652import java .util .Objects ;
5359import com .oracle .truffle .api .object .Property ;
5460import com .oracle .truffle .api .object .Shape ;
5561
56- import sun .misc .Unsafe ;
57-
5862/**
5963 * Property location.
6064 *
@@ -490,10 +494,7 @@ public final void accept(LocationVisitor locationVisitor) {
490494 }
491495
492496 static class LongArrayLocation extends ArrayLocation implements LongLocation {
493- private static final Unsafe UNSAFE = getUnsafe ();
494497 private static final int ALIGN = LONG_ARRAY_SLOT_SIZE - 1 ;
495- private static final long ARRAY_INT_BASE_OFFSET = UNSAFE .arrayBaseOffset (int [].class );
496- private static final long ARRAY_INT_INDEX_SCALE = UNSAFE .arrayIndexScale (int [].class );
497498
498499 protected final boolean allowInt ;
499500
@@ -535,13 +536,30 @@ protected static final int[] getArray(DynamicObject store) {
535536 @ Override
536537 public long getLong (DynamicObject store , boolean guard ) {
537538 int [] array = getArray (store );
538- return UNSAFE .getLong (array , getOffset (array ));
539+ int idx = index ;
540+ boolean boundsCheck = idx >= 0 && idx < array .length - ALIGN ;
541+ if (boundsCheck ) {
542+ long offset = ARRAY_INT_BASE_OFFSET + ARRAY_INT_INDEX_SCALE * idx ;
543+ return unsafeGetLong (array , offset , boundsCheck , null );
544+ } else {
545+ throw arrayIndexOutOfBounds (idx );
546+ }
539547 }
540548
541549 public final void setLongInternal (DynamicObject store , long value ) {
542550 int [] array = getArray (store );
543- long offset = getOffset (array );
544- UNSAFE .putLong (array , offset , value );
551+ int idx = index ;
552+ if (idx >= 0 && idx < array .length - ALIGN ) {
553+ long offset = ARRAY_INT_BASE_OFFSET + ARRAY_INT_INDEX_SCALE * idx ;
554+ unsafePutLong (array , offset , value , null );
555+ } else {
556+ throw arrayIndexOutOfBounds (idx );
557+ }
558+ }
559+
560+ private static ArrayIndexOutOfBoundsException arrayIndexOutOfBounds (int idx ) {
561+ CompilerDirectives .transferToInterpreterAndInvalidate ();
562+ throw new ArrayIndexOutOfBoundsException (idx );
545563 }
546564
547565 @ Override
@@ -578,15 +596,6 @@ public boolean equals(Object obj) {
578596 public boolean isImplicitCastIntToLong () {
579597 return allowInt ;
580598 }
581-
582- protected final long getOffset (int [] array ) {
583- int idx = index ;
584- if (idx < 0 || idx >= array .length - ALIGN ) {
585- CompilerDirectives .transferToInterpreterAndInvalidate ();
586- throw new ArrayIndexOutOfBoundsException (idx );
587- }
588- return ARRAY_INT_BASE_OFFSET + ARRAY_INT_INDEX_SCALE * idx ;
589- }
590599 }
591600
592601 public static LongLocation createLongLocation (LongLocation longLocation , boolean allowInt ) {
@@ -941,7 +950,6 @@ static int upperInt(long value) {
941950 static final class DynamicObjectFieldLocation extends SimpleObjectFieldLocation {
942951 private final long offset ;
943952 private final Class <? extends DynamicObject > tclass ;
944- private static final Unsafe UNSAFE = getUnsafe ();
945953
946954 private DynamicObjectFieldLocation (int index , long offset , Class <? extends DynamicObject > declaringClass ) {
947955 super (index );
@@ -950,20 +958,20 @@ private DynamicObjectFieldLocation(int index, long offset, Class<? extends Dynam
950958 }
951959
952960 DynamicObjectFieldLocation (int index , Field objectField ) {
953- this (index , UNSAFE .objectFieldOffset (objectField ), objectField .getDeclaringClass ().asSubclass (DynamicObject .class ));
961+ this (index , UnsafeAccess .objectFieldOffset (objectField ), objectField .getDeclaringClass ().asSubclass (DynamicObject .class ));
954962 if (objectField .getType () != Object .class ) {
955963 throw new IllegalArgumentException ();
956964 }
957965 }
958966
959967 @ Override
960968 public Object get (DynamicObject store , boolean guard ) {
961- return UNSAFE . getObject (receiverCast (store , tclass ), offset );
969+ return unsafeGetObject (receiverCast (store , tclass ), offset );
962970 }
963971
964972 @ Override
965973 public void set (DynamicObject store , Object value , boolean guard , boolean init ) {
966- UNSAFE . putObject (receiverCast (store , tclass ), offset , value );
974+ unsafePutObject (receiverCast (store , tclass ), offset , value );
967975 }
968976
969977 @ Override
@@ -978,8 +986,6 @@ static final class DynamicLongFieldLocation extends SimpleLongFieldLocation {
978986 /** {@link DynamicObject} subclass holding field. */
979987 private final Class <? extends DynamicObject > tclass ;
980988
981- private static final Unsafe UNSAFE = getUnsafe ();
982-
983989 DynamicLongFieldLocation (int index , long offset , Class <? extends DynamicObject > declaringClass ) {
984990 super (index );
985991 this .offset = offset ;
@@ -989,12 +995,12 @@ static final class DynamicLongFieldLocation extends SimpleLongFieldLocation {
989995
990996 @ Override
991997 public long getLong (DynamicObject store , boolean guard ) {
992- return UNSAFE . getLong (receiverCast (store , tclass ), offset );
998+ return unsafeGetLong (receiverCast (store , tclass ), offset );
993999 }
9941000
9951001 @ Override
9961002 public void setLong (DynamicObject store , long value , boolean guard , boolean init ) {
997- UNSAFE . putLong (receiverCast (store , tclass ), offset , value );
1003+ unsafePutLong (receiverCast (store , tclass ), offset , value );
9981004 }
9991005
10001006 @ Override
@@ -1014,18 +1020,4 @@ static int getLocationOrdinal(CoreLocation loc) {
10141020 throw new IllegalArgumentException (internal .getClass ().getName ());
10151021 }
10161022 }
1017-
1018- static Unsafe getUnsafe () {
1019- try {
1020- return Unsafe .getUnsafe ();
1021- } catch (SecurityException e ) {
1022- }
1023- try {
1024- Field theUnsafeInstance = Unsafe .class .getDeclaredField ("theUnsafe" );
1025- theUnsafeInstance .setAccessible (true );
1026- return (Unsafe ) theUnsafeInstance .get (Unsafe .class );
1027- } catch (Exception e ) {
1028- throw new RuntimeException ("exception while trying to get Unsafe.theUnsafe via reflection:" , e );
1029- }
1030- }
10311023}
0 commit comments