24 class ArrayDelegateRef
27 readonly Lazy<Delegate> volatileGetter;
28 readonly Lazy<Delegate> volatilePutter;
29 readonly Lazy<Delegate> compareExchange;
35 public ArrayDelegateRef(RuntimeJavaType type)
37 volatileGetter =
new Lazy<Delegate>(() => CreateGetArrayVolatileDelegate(type),
true);
38 volatilePutter =
new Lazy<Delegate>(() => CreatePutArrayVolatileDelegate(type),
true);
39 compareExchange =
new Lazy<Delegate>(() => CreateCompareExchangeArrayDelegate(type),
true);
45 public Delegate VolatileGetter => volatileGetter.Value;
50 public Delegate VolatilePutter => volatilePutter.Value;
55 public Delegate CompareExchange => compareExchange.Value;
62 static readonly ConditionalWeakTable<RuntimeJavaType, ArrayDelegateRef> arrayRefCache =
new ConditionalWeakTable<RuntimeJavaType, ArrayDelegateRef>();
67 static readonly
MethodInfo compareAndSwapArrayMethodInfo = typeof(
Unsafe).GetMethods(BindingFlags.NonPublic | BindingFlags.Static)
68 .Where(i => i.Name == nameof(CompareAndSwapArray) && i.IsGenericMethodDefinition && i.GetGenericArguments().Length == 1)
77 static void EmitLdind(ILGenerator il,
Type t)
79 if (t == typeof(
bool))
80 il.Emit(OpCodes.Ldind_U1);
81 else if (t == typeof(
byte))
82 il.Emit(OpCodes.Ldind_U1);
83 else if (t == typeof(
char))
84 il.Emit(OpCodes.Ldind_U2);
85 else if (t == typeof(
short))
86 il.Emit(OpCodes.Ldind_I2);
87 else if (t == typeof(
int))
88 il.Emit(OpCodes.Ldind_I4);
89 else if (t == typeof(
long))
90 il.Emit(OpCodes.Ldind_I8);
91 else if (t == typeof(
float))
92 il.Emit(OpCodes.Ldind_R4);
93 else if (t == typeof(
double))
94 il.Emit(OpCodes.Ldind_R8);
96 il.Emit(OpCodes.Ldind_Ref);
105 static void EmitStind(ILGenerator il,
Type t)
107 if (t == typeof(
bool))
108 il.Emit(OpCodes.Stind_I1);
109 else if (t == typeof(
byte))
110 il.Emit(OpCodes.Stind_I1);
111 else if (t == typeof(
char))
112 il.Emit(OpCodes.Stind_I2);
113 else if (t == typeof(
short))
114 il.Emit(OpCodes.Stind_I2);
115 else if (t == typeof(
int))
116 il.Emit(OpCodes.Stind_I4);
117 else if (t == typeof(
long))
118 il.Emit(OpCodes.Stind_I8);
119 else if (t == typeof(
float))
120 il.Emit(OpCodes.Stind_R4);
121 else if (t == typeof(
double))
122 il.Emit(OpCodes.Stind_R8);
124 il.Emit(OpCodes.Stind_Ref);
143 static T GetField<T>(
object o,
long offset)
146 throw new NotImplementedException();
149 if (o is RuntimeJavaType w)
151 if (w != f.DeclaringType)
152 throw new global::java.lang.IllegalArgumentException();
154 return f.UnsafeGetValue<T>(
null);
157 return f.UnsafeGetValue<T>(o);
169 static void PutField<T>(
object o,
long offset, T value)
172 throw new NotImplementedException();
175 if (o is RuntimeJavaType w)
177 if (w != f.DeclaringType)
178 throw new global::java.lang.IllegalArgumentException();
180 f.UnsafeSetValue<T>(
null, value);
183 f.UnsafeSetValue<T>(o, value);
194 public static object getObject(
object self,
object o,
long offset)
197 throw new NotImplementedException();
203 RuntimeJavaType w => GetField<object>(
null, offset),
204 object[] array => array[offset / IntPtr.Size],
205 object obj => GetField<object>(obj, offset),
206 _ =>
throw new global::java.lang.IllegalArgumentException(),
209 catch (Exception e) when (e is not global::java.lang.Throwable)
211 throw new global::java.lang.InternalError(e);
223 public static void putObject(
object self,
object o,
long offset,
object x)
226 throw new NotImplementedException();
232 case RuntimeJavaType w:
233 PutField(w, offset, x);
236 array[offset / IntPtr.Size] = x;
239 PutField(obj, offset, x);
242 throw new global::java.lang.IllegalArgumentException();
245 catch (Exception e) when (e is not global::java.lang.Throwable)
247 throw new global::java.lang.InternalError(e);
259 public static bool getBoolean(
object self,
object o,
long offset)
262 throw new NotImplementedException();
268 null => Marshal.ReadByte((IntPtr)offset) != 0,
269 RuntimeJavaType w => GetField<bool>(
null, offset),
270 Array array => Buffer.GetByte(array, (
int)offset) != 0,
271 _ => GetField<bool>(o, offset)
274 catch (Exception e) when (e is not global::java.lang.Throwable)
276 throw new global::java.lang.InternalError(e);
288 public static void putBoolean(
object self,
object o,
long offset,
bool x)
291 throw new NotImplementedException();
298 Marshal.WriteByte((IntPtr)offset, x ? (
byte)1 : (
byte)0);
300 case RuntimeJavaType w:
301 PutField(w, offset, x);
304 Buffer.SetByte(array, (
int)offset, x ? (
byte)1 : (
byte)0);
307 PutField(o, offset, x);
311 catch (Exception e) when (e is not global::java.lang.Throwable)
313 throw new global::java.lang.InternalError(e);
325 public static byte getByte(
object self,
object o,
long offset)
328 throw new NotImplementedException();
334 null => Marshal.ReadByte((IntPtr)offset),
335 RuntimeJavaType w => GetField<byte>(
null, offset),
336 Array array => Buffer.GetByte(array, (
int)offset),
337 _ => GetField<byte>(o, offset)
340 catch (Exception e) when (e is not global::java.lang.Throwable)
342 throw new global::java.lang.InternalError(e);
354 public static void putByte(
object self,
object o,
long offset,
byte x)
357 throw new NotImplementedException();
364 Marshal.WriteByte((IntPtr)offset, x);
366 case RuntimeJavaType w:
367 PutField(w, offset, x);
370 Buffer.SetByte(array, (
int)offset, x);
373 PutField(o, offset, x);
377 catch (Exception e) when (e is not global::java.lang.Throwable)
379 throw new global::java.lang.InternalError(e);
391 public static short getShort(
object self,
object o,
long offset)
394 throw new NotImplementedException();
400 null => Marshal.ReadInt16((IntPtr)offset),
401 RuntimeJavaType w => GetField<short>(
null, offset),
402 Array array => ReadInt16(array, offset),
403 _ => GetField<short>(o, offset)
406 catch (Exception e) when (e is not global::java.lang.Throwable)
408 throw new global::java.lang.InternalError(e);
420 public static void putShort(
object self,
object o,
long offset,
short x)
423 throw new NotImplementedException();
430 Marshal.WriteInt16((IntPtr)offset, x);
432 case RuntimeJavaType w:
433 PutField(w, offset, x);
436 WriteInt16(array, offset, x);
439 PutField(o, offset, x);
443 catch (Exception e) when (e is not global::java.lang.Throwable)
445 throw new global::java.lang.InternalError(e);
457 public static char getChar(
object self,
object o,
long offset)
460 throw new NotImplementedException();
466 null => (char)Marshal.ReadInt16((IntPtr)offset),
467 RuntimeJavaType w => GetField<char>(
null, offset),
468 Array array => (char)ReadInt16(array, offset),
469 _ => GetField<char>(o, offset)
472 catch (Exception e) when (e is not global::java.lang.Throwable)
474 throw new global::java.lang.InternalError(e);
486 public static void putChar(
object self,
object o,
long offset,
char x)
489 throw new NotImplementedException();
496 Marshal.WriteInt16((IntPtr)offset, (
short)x);
498 case RuntimeJavaType w:
499 PutField(w, offset, x);
502 WriteInt16(array, offset, (
short)x);
505 PutField(o, offset, x);
509 catch (Exception e) when (e is not global::java.lang.Throwable)
511 throw new global::java.lang.InternalError(e);
523 public static int getInt(
object self,
object o,
long offset)
526 throw new NotImplementedException();
532 null => Marshal.ReadInt32((IntPtr)offset),
533 RuntimeJavaType w => GetField<int>(
null, offset),
534 Array array => ReadInt32(array, offset),
535 _ => GetField<int>(o, offset)
538 catch (Exception e) when (e is not global::java.lang.Throwable)
540 throw new global::java.lang.InternalError(e);
552 public static void putInt(
object self,
object o,
long offset,
int x)
555 throw new NotImplementedException();
562 Marshal.WriteInt32((IntPtr)offset, x);
564 case RuntimeJavaType w:
565 PutField(w, offset, x);
568 WriteInt32(array, offset, x);
571 PutField(o, offset, x);
575 catch (Exception e) when (e is not global::java.lang.Throwable)
577 throw new global::java.lang.InternalError(e);
589 public static long getLong(
object self,
object o,
long offset)
592 throw new NotImplementedException();
598 null => Marshal.ReadInt64((IntPtr)offset),
599 RuntimeJavaType w => GetField<long>(
null, offset),
600 Array array => ReadInt64(array, offset),
601 _ => GetField<long>(o, offset)
604 catch (Exception e) when (e is not global::java.lang.Throwable)
606 throw new global::java.lang.InternalError(e);
618 public static void putLong(
object self,
object o,
long offset,
long x)
621 throw new NotImplementedException();
628 Marshal.WriteInt64((IntPtr)offset, x);
630 case RuntimeJavaType w:
631 PutField(w, offset, x);
634 WriteInt64(array, offset, x);
637 PutField(o, offset, x);
641 catch (Exception e) when (e is not global::java.lang.Throwable)
643 throw new global::java.lang.InternalError(e);
655 public static float getFloat(
object self,
object o,
long offset)
658 throw new NotImplementedException();
664 null => global::java.lang.Float.intBitsToFloat(Marshal.ReadInt32((IntPtr)offset)),
665 RuntimeJavaType w => GetField<float>(
null, offset),
666 Array array => global::java.lang.Float.intBitsToFloat(ReadInt32(array, offset)),
667 _ => GetField<float>(o, offset)
670 catch (Exception e) when (e is not global::java.lang.Throwable)
672 throw new global::java.lang.InternalError(e);
684 public static void putFloat(
object self,
object o,
long offset,
float x)
687 throw new NotImplementedException();
694 Marshal.WriteInt32((IntPtr)offset, global::java.lang.Float.floatToRawIntBits(x));
696 case RuntimeJavaType w:
697 PutField(w, offset, x);
700 WriteInt32(array, offset, global::java.lang.Float.floatToRawIntBits(x));
703 PutField(o, offset, x);
707 catch (Exception e) when (e is not global::java.lang.Throwable)
709 throw new global::java.lang.InternalError(e);
721 public static double getDouble(
object self,
object o,
long offset)
724 throw new NotImplementedException();
730 null => global::java.lang.Double.longBitsToDouble(Marshal.ReadInt64((IntPtr)offset)),
731 RuntimeJavaType w => GetField<double>(
null, offset),
732 Array array => global::java.lang.Double.longBitsToDouble(ReadInt64(array, offset)),
733 _ => GetField<double>(o, offset)
736 catch (Exception e) when (e is not global::java.lang.Throwable)
738 throw new global::java.lang.InternalError(e);
750 public static void putDouble(
object self,
object o,
long offset,
double x)
753 throw new NotImplementedException();
760 Marshal.WriteInt64((IntPtr)offset, global::java.lang.Double.doubleToRawLongBits(x));
762 case RuntimeJavaType w:
763 PutField(w, offset, x);
766 WriteInt64(array, offset, global::java.lang.Double.doubleToRawLongBits(x));
769 PutField(o, offset, x);
773 catch (Exception e) when (e is not global::java.lang.Throwable)
775 throw new global::java.lang.InternalError(e);
786 public static byte getByte(
object self,
long address)
789 throw new NotImplementedException();
793 return Marshal.ReadByte((IntPtr)address);
795 catch (Exception e) when (e is not global::java.lang.Throwable)
797 throw new global::java.lang.InternalError(e);
808 public static void putByte(
object self,
long address,
byte x)
811 throw new NotImplementedException();
815 Marshal.WriteByte((IntPtr)address, x);
817 catch (Exception e) when (e is not global::java.lang.Throwable)
819 throw new global::java.lang.InternalError(e);
830 public static short getShort(
object self,
long address)
833 throw new NotImplementedException();
837 return Marshal.ReadInt16((IntPtr)address);
839 catch (Exception e) when (e is not global::java.lang.Throwable)
841 throw new global::java.lang.InternalError(e);
852 public static void putShort(
object self,
long address,
short x)
855 throw new NotImplementedException();
859 Marshal.WriteInt16((IntPtr)address, x);
861 catch (Exception e) when (e is not global::java.lang.Throwable)
863 throw new global::java.lang.InternalError(e);
874 public static char getChar(
object self,
long address)
877 throw new NotImplementedException();
881 return (
char)Marshal.ReadInt16((IntPtr)address);
883 catch (Exception e) when (e is not global::java.lang.Throwable)
885 throw new global::java.lang.InternalError(e);
896 public static void putChar(
object self,
long address,
char x)
899 throw new NotImplementedException();
903 Marshal.WriteInt16((IntPtr)address, (
short)x);
905 catch (Exception e) when (e is not global::java.lang.Throwable)
907 throw new global::java.lang.InternalError(e);
918 public static int getInt(
object self,
long address)
921 throw new NotImplementedException();
925 return Marshal.ReadInt32((IntPtr)address);
927 catch (Exception e) when (e is not global::java.lang.Throwable)
929 throw new global::java.lang.InternalError(e);
940 public static void putInt(
object self,
long address,
int x)
943 throw new NotImplementedException();
947 Marshal.WriteInt32((IntPtr)address, x);
949 catch (Exception e) when (e is not global::java.lang.Throwable)
951 throw new global::java.lang.InternalError(e);
962 public static long getLong(
object self,
long address)
965 throw new NotImplementedException();
969 return Marshal.ReadInt64((IntPtr)address);
971 catch (Exception e) when (e is not global::java.lang.Throwable)
973 throw new global::java.lang.InternalError(e);
984 public static void putLong(
object self,
long address,
long x)
987 throw new NotImplementedException();
991 Marshal.WriteInt64((IntPtr)address, x);
993 catch (Exception e) when (e is not global::java.lang.Throwable)
995 throw new global::java.lang.InternalError(e);
1006 public static float getFloat(
object self,
long address)
1009 throw new NotImplementedException();
1013 return global::java.lang.Float.intBitsToFloat(
getInt(
self, address));
1015 catch (Exception e) when (e is not global::java.lang.Throwable)
1017 throw new global::java.lang.InternalError(e);
1028 public static void putFloat(
object self,
long address,
float x)
1031 throw new NotImplementedException();
1033 putInt(
self, address, global::java.lang.Float.floatToIntBits(x));
1046 throw new NotImplementedException();
1050 return global::java.lang.Double.longBitsToDouble(
getLong(
self, address));
1052 catch (Exception e) when (e is not global::java.lang.Throwable)
1054 throw new global::java.lang.InternalError(e);
1065 public static void putDouble(
object self,
long address,
double x)
1068 throw new NotImplementedException();
1070 putLong(
self, address, global::java.lang.Double.doubleToLongBits(x));
1083 throw new NotImplementedException();
1087 return Marshal.ReadIntPtr((IntPtr)address).ToInt64();
1089 catch (Exception e) when (e is not global::java.lang.Throwable)
1091 throw new global::java.lang.InternalError(e);
1105 throw new NotImplementedException();
1109 Marshal.WriteIntPtr((IntPtr)address, (IntPtr)x);
1111 catch (Exception e) when (e is not global::java.lang.Throwable)
1113 throw new global::java.lang.InternalError(e);
1128 throw new NotImplementedException();
1135 var len = (IntPtr)bytes +
sizeof(IntPtr);
1136 var ptr = Marshal.AllocHGlobal(len);
1137 Marshal.WriteIntPtr(ptr, len);
1138 System.GC.AddMemoryPressure((
long)len);
1139 return (
long)ptr +
sizeof(IntPtr);
1141 catch (OutOfMemoryException e)
1143 throw new global::java.lang.OutOfMemoryError(e.Message).initCause(e);
1145 catch (Exception e) when (e is not global::java.lang.Throwable)
1147 throw new global::java.lang.InternalError(e);
1163 throw new NotImplementedException();
1174 var ptr = (IntPtr)address -
sizeof(IntPtr);
1175 var len = Marshal.ReadIntPtr(ptr);
1176 System.GC.RemoveMemoryPressure((
long)len);
1179 len = (IntPtr)bytes +
sizeof(IntPtr);
1180 ptr = Marshal.ReAllocHGlobal(ptr, len);
1181 Marshal.WriteIntPtr(ptr, len);
1182 System.GC.AddMemoryPressure((
long)len);
1183 return (
long)ptr +
sizeof(IntPtr);
1185 catch (OutOfMemoryException e)
1187 throw new global::java.lang.OutOfMemoryError(e.Message).initCause(e);
1189 catch (Exception e) when (e is not global::java.lang.Throwable)
1191 throw new global::java.lang.InternalError(e);
1205 public static unsafe
void setMemory(
object self,
object o,
long offset,
long bytes,
byte value)
1208 throw new NotImplementedException();
1213 new Span<byte>((
void*)(IntPtr)offset, (
int)bytes).Fill(value);
1214 else if (o is
byte[] array)
1215 ((Span<byte>)array).Slice((
int)offset, (
int)bytes).Fill(value);
1216 else if (o is Array array2)
1222 h = GCHandle.Alloc(array2, GCHandleType.Pinned);
1223 new Span<byte>((
byte*)h.AddrOfPinnedObject(), Buffer.ByteLength(array2)).Slice((
int)offset, (
int)bytes).Fill(value);
1232 throw new global::java.lang.IllegalArgumentException();
1234 catch (Exception e) when (e is not global::java.lang.Throwable)
1236 throw new global::java.lang.InternalError(e);
1251 public static unsafe
void copyMemory(
object self,
object srcBase,
long srcOffset,
object destBase,
long destOffset,
long bytes)
1254 throw new NotImplementedException();
1258 if (srcBase ==
null)
1260 if (destBase is
byte[] byteArray)
1262 Marshal.Copy((IntPtr)srcOffset, byteArray, (
int)destOffset, (
int)bytes);
1264 else if (destBase is
bool[])
1266 var tmp =
new byte[(int)bytes];
1267 copyMemory(
self, srcBase, srcOffset, tmp, 0, bytes);
1268 copyMemory(
self, tmp, 0, destBase, destOffset, bytes);
1270 else if (destBase is
short[] shortArray)
1272 Marshal.Copy((IntPtr)srcOffset, shortArray, (
int)(destOffset >> 1), (
int)(bytes >> 1));
1274 else if (destBase is
char[] charArray)
1276 Marshal.Copy((IntPtr)srcOffset, charArray, (
int)(destOffset >> 1), (
int)(bytes >> 1));
1278 else if (destBase is
int[] intArray)
1280 Marshal.Copy((IntPtr)srcOffset, intArray, (
int)(destOffset >> 2), (
int)(bytes >> 2));
1282 else if (destBase is
float[] floatArray)
1284 Marshal.Copy((IntPtr)srcOffset, floatArray, (
int)(destOffset >> 2), (
int)(bytes >> 2));
1286 else if (destBase is
long[] longArray)
1288 Marshal.Copy((IntPtr)srcOffset, longArray, (
int)(destOffset >> 3), (
int)(bytes >> 3));
1290 else if (destBase is
double[] doubleArray)
1292 Marshal.Copy((IntPtr)srcOffset, doubleArray, (
int)(destOffset >> 3), (
int)(bytes >> 3));
1294 else if (destBase ==
null)
1296 Buffer.MemoryCopy((
void*)(IntPtr)srcOffset, (
void*)(IntPtr)destOffset,
long.MaxValue, bytes);
1300 throw new global::java.lang.IllegalArgumentException();
1303 else if (srcBase is Array && destBase is Array)
1305 Buffer.BlockCopy((Array)srcBase, (
int)srcOffset, (Array)destBase, (
int)destOffset, (
int)bytes);
1309 if (srcBase is
byte[] byteArray)
1311 Marshal.Copy(byteArray, (
int)srcOffset, (IntPtr)destOffset, (
int)bytes);
1313 else if (srcBase is
bool[])
1315 var tmp =
new byte[(int)bytes];
1316 copyMemory(
self, srcBase, srcOffset, tmp, 0, bytes);
1317 copyMemory(
self, tmp, 0, destBase, destOffset, bytes);
1319 else if (srcBase is
short[] shortArray)
1321 Marshal.Copy(shortArray, (
int)(srcOffset >> 1), (IntPtr)(destOffset), (
int)(bytes >> 1));
1323 else if (srcBase is
char[] charArray)
1325 Marshal.Copy(charArray, (
int)(srcOffset >> 1), (IntPtr)(destOffset), (
int)(bytes >> 1));
1327 else if (srcBase is
int[] intArray)
1329 Marshal.Copy(intArray, (
int)(srcOffset >> 2), (IntPtr)(destOffset), (
int)(bytes >> 2));
1331 else if (srcBase is
float[] floatArray)
1333 Marshal.Copy(floatArray, (
int)(srcOffset >> 2), (IntPtr)(destOffset), (
int)(bytes >> 2));
1335 else if (srcBase is
long[] longArray)
1337 Marshal.Copy(longArray, (
int)(srcOffset >> 3), (IntPtr)(destOffset), (
int)(bytes >> 3));
1339 else if (srcBase is
double[] doubleArray)
1341 Marshal.Copy(doubleArray, (
int)(srcOffset >> 3), (IntPtr)(destOffset), (
int)(bytes >> 3));
1345 throw new global::java.lang.IllegalArgumentException();
1349 catch (Exception e) when (e is not global::java.lang.Throwable)
1351 throw new global::java.lang.InternalError(e);
1363 var adr = (nint)address;
1367 var ptr = adr -
sizeof(nint);
1368 var len = Marshal.ReadIntPtr(ptr);
1369 Marshal.FreeHGlobal(ptr);
1370 System.GC.RemoveMemoryPressure((
long)len);
1382 if (w.IsStatic ==
false)
1383 throw new global::java.lang.IllegalArgumentException();
1385 return w.DeclaringType;
1397 if (w.IsStatic ==
false)
1398 throw new global::java.lang.IllegalArgumentException();
1400 return (
long)w.Cookie;
1413 throw new global::java.lang.IllegalArgumentException();
1415 return (
long)w.Cookie;
1426 return RuntimeJavaType.FromClass(c).HasStaticInitializer;
1436 var tw = RuntimeJavaType.FromClass(c);
1437 if (tw.IsArray ==
false)
1468 static int ArrayIndexScale(RuntimeJavaType tw)
1471 throw new NotImplementedException();
1473 var context = tw.Context;
1475 var et = tw.ElementTypeWrapper;
1476 if (et == context.PrimitiveJavaTypeFactory.BYTE || et == context.PrimitiveJavaTypeFactory.BOOLEAN)
1478 else if (et == context.PrimitiveJavaTypeFactory.CHAR || et == context.PrimitiveJavaTypeFactory.SHORT)
1480 else if (et == context.PrimitiveJavaTypeFactory.INT || et == context.PrimitiveJavaTypeFactory.FLOAT)
1482 else if (et == context.PrimitiveJavaTypeFactory.LONG || et == context.PrimitiveJavaTypeFactory.DOUBLE)
1484 else if (et.IsPrimitive ==
false && et.IsNonPrimitiveValueType)
1485 return Marshal.SizeOf(et.TypeAsTBD);
1486 else if (et.IsPrimitive ==
false && et.IsNonPrimitiveValueType ==
false)
1501 return ArrayIndexScale(RuntimeJavaType.FromClass(arrayClass));
1521 return Environment.SystemPageSize;
1535 public static global::java.lang.Class
defineClass(
object self,
string name,
byte[] b,
int off,
int len, global::java.lang.ClassLoader loader, global::java.security.ProtectionDomain protectionDomain)
1549 public static global::java.lang.Class
defineAnonymousClass(
object self, global::java.lang.Class hostClass,
byte[] data,
object[] cpPatches)
1552 throw new NotImplementedException();
1556 var tw = RuntimeJavaType.FromClass(hostClass);
1557 var cl = tw.ClassLoader;
1562 if (cf.IKVMAssemblyAttribute !=
null)
1563 throw new global::java.lang.ClassFormatError(
"Trying to define anonymous class based on stub class: " + cf.Name);
1565 return cl.GetTypeWrapperFactory().DefineClassImpl(
null, tw, cf, cl, hostClass.pd).ClassObject;
1574#if FIRST_PASS == false
1581 static IKVM.ByteCode.Decoding.ClassFile ReadClass(
byte[] buffer)
1585 return IKVM.ByteCode.Decoding.ClassFile.Read(buffer);
1587 catch (InvalidClassMagicException)
1589 throw new global::java.lang.ClassFormatError(
"Incompatible magic value");
1591 catch (ByteCodeException e)
1593 throw new global::java.lang.ClassFormatError(e.Message);
1607 var wrapper = RuntimeJavaType.FromClass(cls);
1618 return FormatterServices.GetUninitializedObject(wrapper.TypeAsBaseType);
1620 return RuntimeHelpers.GetUninitializedObject(wrapper.TypeAsBaseType);
1652 return Monitor.TryEnter(o);
1673 static T GetFieldVolatile<T>(
object o,
long offset)
1676 throw new NotImplementedException();
1679 if (o is RuntimeJavaType w)
1681 if (w != f.DeclaringType)
1682 throw new global::java.lang.IllegalArgumentException();
1684 return f.UnsafeVolatileGet<T>(
null);
1687 return f.UnsafeVolatileGet<T>(o);
1699 static void PutFieldVolatile<T>(
object o,
long offset, T value)
1702 throw new NotImplementedException();
1705 if (o is RuntimeJavaType w)
1707 if (w != f.DeclaringType)
1708 throw new global::java.lang.IllegalArgumentException();
1710 f.UnsafeVolatileSet<T>(
null, value);
1713 f.UnsafeVolatileSet<T>(o, value);
1722 static Delegate CreateGetArrayVolatileDelegate(RuntimeJavaType tw)
1724 var et = tw.IsPrimitive ? tw.TypeAsTBD : typeof(
object);
1725 var dm =
DynamicMethodUtil.Create($
"__<UnsafeGetArrayVolatile>__{tw.Name.Replace(".
", "_
")}", tw.TypeAsTBD,
true, et,
new[] { typeof(object[]), typeof(long) });
1726 var il = dm.GetILGenerator();
1729 il.Emit(OpCodes.Ldarg_0);
1730 il.Emit(OpCodes.Ldarg_1);
1731 il.Emit(OpCodes.Conv_Ovf_I);
1732 il.Emit(OpCodes.Ldc_I4, ArrayIndexScale(tw.MakeArrayType(1)));
1733 il.Emit(OpCodes.Div);
1734 il.Emit(OpCodes.Conv_Ovf_I);
1735 il.Emit(OpCodes.Ldelema, tw.TypeAsLocalOrStackType);
1737 if (tw.IsWidePrimitive ==
false)
1739 il.Emit(OpCodes.Volatile);
1740 EmitLdind(il, tw.TypeAsLocalOrStackType);
1745 var mi = typeof(Unsafe).GetMethod(nameof(InterlockedRead), BindingFlags.NonPublic | BindingFlags.Static,
null,
new[] { tw.TypeAsTBD.MakeByRefType() },
null);
1746 il.Emit(OpCodes.Call, mi);
1749 il.Emit(OpCodes.Ret);
1750 return dm.CreateDelegate(typeof(Func<,,>).MakeGenericType(typeof(
object[]), typeof(
long), et));
1759 static object GetArrayObjectVolatile(
object[] array,
long offset)
1762 throw new NotImplementedException();
1764 return ((Func<
object[],
long,
object>)arrayRefCache.GetValue(
JVM.Context.
ClassLoaderFactory.GetJavaTypeFromType(array.GetType().GetElementType()), _ =>
new ArrayDelegateRef(_)).VolatileGetter)(array, offset);
1773 static Delegate CreatePutArrayVolatileDelegate(RuntimeJavaType tw)
1775 var et = tw.IsPrimitive ? tw.TypeAsTBD : typeof(
object);
1776 var dm =
DynamicMethodUtil.Create($
"__<UnsafePutArrayVolatile>__{tw.Name.Replace(".
", "_
")}", tw.TypeAsTBD,
true, typeof(
void),
new[] { typeof(
object[]), typeof(
long), et });
1777 var il = dm.GetILGenerator();
1780 il.Emit(OpCodes.Ldarg_0);
1781 il.Emit(OpCodes.Ldarg_1);
1782 il.Emit(OpCodes.Conv_Ovf_I);
1783 il.Emit(OpCodes.Ldc_I4, ArrayIndexScale(tw.MakeArrayType(1)));
1784 il.Emit(OpCodes.Div);
1785 il.Emit(OpCodes.Conv_Ovf_I);
1786 il.Emit(OpCodes.Ldelema, tw.TypeAsLocalOrStackType);
1788 il.Emit(OpCodes.Ldarg_2);
1790 if (tw.IsWidePrimitive ==
false)
1792 il.Emit(OpCodes.Volatile);
1793 EmitStind(il, tw.TypeAsLocalOrStackType);
1798 var mi = typeof(
Interlocked).GetMethod(nameof(
Interlocked.Exchange),
new[] { tw.TypeAsTBD.MakeByRefType() });
1799 il.Emit(OpCodes.Call, mi);
1802 il.Emit(OpCodes.Ret);
1803 return dm.CreateDelegate(typeof(Action<,,>).MakeGenericType(typeof(
object[]), typeof(
long), et));
1813 static void PutArrayObjectVolatile(
object[] array,
long offset,
object value)
1816 throw new NotImplementedException();
1818 ((Action<object[], long, object>)arrayRefCache.GetValue(
JVM.Context.
ClassLoaderFactory.GetJavaTypeFromType(array.GetType().GetElementType()), _ =>
new ArrayDelegateRef(_)).VolatilePutter)(array, offset, value);
1832 throw new NotImplementedException();
1838 RuntimeJavaType w => GetField<object>(
null, offset),
1839 object[] array when array.GetType() == typeof(
object[]) => Volatile.Read(
ref array[offset / IntPtr.Size]),
1840 object[] array => GetArrayObjectVolatile(array, offset),
1841 object obj => GetFieldVolatile<object>(obj, offset),
1842 _ =>
throw new global::java.lang.IllegalArgumentException(),
1845 catch (Exception e) when (e is not global::java.lang.Throwable)
1847 throw new global::java.lang.InternalError(e);
1862 throw new NotImplementedException();
1868 case RuntimeJavaType w:
1869 PutField(w, offset, x);
1871 case object[] array when array.GetType() == typeof(
object[]):
1872 Volatile.Write(
ref array[offset / IntPtr.Size], x);
1874 case object[] array:
1875 PutArrayObjectVolatile(array, offset, x);
1878 PutFieldVolatile(o, offset, x);
1881 throw new global::java.lang.IllegalArgumentException();
1884 catch (Exception e) when (e is not global::java.lang.Throwable)
1886 throw new global::java.lang.InternalError(e);
1901 throw new NotImplementedException();
1908 RuntimeJavaType w => GetFieldVolatile<int>(
null, offset),
1909 Array array => ReadInt32Volatile(array, offset),
1910 _ => GetFieldVolatile<int>(o, offset)
1913 catch (Exception e) when (e is not global::java.lang.Throwable)
1915 throw new global::java.lang.InternalError(e);
1930 throw new NotImplementedException();
1939 case RuntimeJavaType w:
1940 PutFieldVolatile(w, offset, x);
1943 WriteInt32Volatile(array, offset, x);
1946 PutFieldVolatile(o, offset, x);
1950 catch (Exception e) when (e is not global::java.lang.Throwable)
1952 throw new global::java.lang.InternalError(e);
1967 throw new NotImplementedException();
1974 RuntimeJavaType w => GetFieldVolatile<bool>(
null, offset),
1975 Array array => ReadByteVolatile(array, offset) != 0,
1976 _ => GetFieldVolatile<bool>(o, offset)
1979 catch (Exception e) when (e is not global::java.lang.Throwable)
1981 throw new global::java.lang.InternalError(e);
1996 throw new NotImplementedException();
2005 case RuntimeJavaType w:
2006 PutFieldVolatile(w, offset, x);
2009 WriteByteVolatile(array, offset, x ? (
byte)1 : (
byte)0);
2012 PutFieldVolatile(o, offset, x);
2016 catch (Exception e) when (e is not global::java.lang.Throwable)
2018 throw new global::java.lang.InternalError(e);
2033 throw new NotImplementedException();
2040 RuntimeJavaType w => GetFieldVolatile<byte>(
null, offset),
2041 Array array => ReadByteVolatile(array, offset),
2042 _ => GetFieldVolatile<byte>(o, offset)
2045 catch (Exception e) when (e is not global::java.lang.Throwable)
2047 throw new global::java.lang.InternalError(e);
2062 throw new NotImplementedException();
2071 case RuntimeJavaType w:
2072 PutFieldVolatile(w, offset, x);
2075 WriteByteVolatile(array, offset, x);
2078 PutFieldVolatile(o, offset, x);
2082 catch (Exception e) when (e is not global::java.lang.Throwable)
2084 throw new global::java.lang.InternalError(e);
2099 throw new NotImplementedException();
2105 Array array => ReadInt16Volatile(array, offset),
2106 _ => GetFieldVolatile<short>(o, offset)
2109 catch (Exception e) when (e is not global::java.lang.Throwable)
2111 throw new global::java.lang.InternalError(e);
2126 throw new NotImplementedException();
2135 case RuntimeJavaType w:
2136 PutFieldVolatile(w, offset, x);
2139 WriteInt16Volatile(array, offset, x);
2142 PutFieldVolatile(o, offset, x);
2146 catch (Exception e) when (e is not global::java.lang.Throwable)
2148 throw new global::java.lang.InternalError(e);
2163 throw new NotImplementedException();
2169 Array array => (char)ReadInt16Volatile(array, offset),
2170 _ => GetFieldVolatile<char>(o, offset)
2173 catch (Exception e) when (e is not global::java.lang.Throwable)
2175 throw new global::java.lang.InternalError(e);
2190 throw new NotImplementedException();
2199 case RuntimeJavaType w:
2200 PutFieldVolatile(w, offset, x);
2203 WriteInt16Volatile(array, offset, (
short)x);
2206 PutFieldVolatile(o, offset, x);
2210 catch (Exception e) when (e is not global::java.lang.Throwable)
2212 throw new global::java.lang.InternalError(e);
2227 throw new NotImplementedException();
2233 Array array => ReadInt64Volatile(array, offset),
2234 _ => GetFieldVolatile<long>(o, offset)
2237 catch (Exception e) when (e is not global::java.lang.Throwable)
2239 throw new global::java.lang.InternalError(e);
2254 throw new NotImplementedException();
2263 case RuntimeJavaType w:
2264 PutFieldVolatile(w, offset, x);
2267 WriteInt64Volatile(array, offset, x);
2270 PutFieldVolatile(o, offset, x);
2274 catch (Exception e) when (e is not global::java.lang.Throwable)
2276 throw new global::java.lang.InternalError(e);
2292 throw new NotImplementedException();
2298 Array array => global::java.lang.Float.intBitsToFloat(ReadInt32Volatile(array, offset)),
2299 _ => GetFieldVolatile<float>(o, offset)
2302 catch (Exception e) when (e is not global::java.lang.Throwable)
2304 throw new global::java.lang.InternalError(e);
2319 throw new NotImplementedException();
2328 case RuntimeJavaType w:
2329 PutFieldVolatile(w, offset, x);
2332 WriteInt32Volatile(array, offset, global::java.lang.Float.floatToRawIntBits(x));
2335 PutFieldVolatile(o, offset, x);
2339 catch (Exception e) when (e is not global::java.lang.Throwable)
2341 throw new global::java.lang.InternalError(e);
2357 throw new NotImplementedException();
2363 Array array => global::java.lang.Double.longBitsToDouble(ReadInt64Volatile(array, offset)),
2364 _ => GetFieldVolatile<double>(o, offset)
2367 catch (Exception e) when (e is not global::java.lang.Throwable)
2369 throw new global::java.lang.InternalError(e);
2384 throw new NotImplementedException();
2393 case RuntimeJavaType w:
2394 PutFieldVolatile(w, offset, x);
2397 WriteInt64Volatile(array, offset, global::java.lang.Double.doubleToRawLongBits(x));
2400 PutFieldVolatile(o, offset, x);
2404 catch (Exception e) when (e is not global::java.lang.Throwable)
2406 throw new global::java.lang.InternalError(e);
2436 static bool CompareAndSwapField<T>(
object o,
long offset, T expected, T value)
2439 throw new NotImplementedException();
2441 return RuntimeJavaField.FromCookie((IntPtr)offset).UnsafeCompareAndSwap(o, expected, value);
2450 static Delegate CreateCompareExchangeArrayDelegate(RuntimeJavaType tw)
2452 var p = Expression.Parameter(typeof(
object[]));
2453 var i = Expression.Parameter(typeof(
long));
2454 var v = Expression.Parameter(typeof(
object));
2455 var e = Expression.Parameter(typeof(
object));
2456 return Expression.Lambda<Func<object[], long, object, object, object>>(
2458 compareAndSwapArrayMethodInfo.MakeGenericMethod(tw.TypeAsTBD),
2459 Expression.Convert(p, tw.MakeArrayType(1).TypeAsTBD),
2461 Expression.Convert(v, tw.TypeAsTBD),
2462 Expression.Convert(e, tw.TypeAsTBD)),
2476 static object CompareAndSwapArray<T>(T[] o,
long offset, T value, T comparand)
2480 throw new NotImplementedException();
2496 static object CompareAndSwapObjectArray(
object[] o,
long offset,
object value,
object expected)
2499 throw new NotImplementedException();
2501 return ((Func<
object[],
long,
object,
object,
object>)arrayRefCache.GetValue(
JVM.Context.
ClassLoaderFactory.GetJavaTypeFromType(o.GetType().GetElementType()), _ =>
new ArrayDelegateRef(_)).CompareExchange)(o, offset, value, expected);
2517 throw new NotImplementedException();
2523 object[] array when array.GetType() == typeof(
object[]) =>
Interlocked.CompareExchange(
ref array[offset / IntPtr.Size], x, expected) == expected,
2524 object[] array => CompareAndSwapObjectArray(array, offset, x, expected) == expected,
2525 _ => CompareAndSwapField(o, offset, expected, x)
2528 catch (Exception e) when (e is not global::java.lang.Throwable)
2530 throw new global::java.lang.InternalError(e);
2547 throw new NotImplementedException();
2551 if (o is
int[] array && (offset %
sizeof(
int)) == 0)
2553 return Interlocked.CompareExchange(
ref array[offset /
sizeof(
int)], x, expected) == expected;
2555 else if (o is Array array1 && (offset %
sizeof(
int)) == 0)
2557 return CompareExchangeInt32(array1, offset, x, expected) == expected;
2559 else if (o is Array array2)
2561 return CompareExchangeInt32Unaligned(array2, offset, x, expected) == expected;
2565 return CompareAndSwapField(o, offset, expected, x);
2568 catch (Exception e) when (e is not global::java.lang.Throwable)
2570 throw new global::java.lang.InternalError(e);
2587 throw new NotImplementedException();
2591 if (o is
long[] array && (offset %
sizeof(
long)) == 0)
2593 return Interlocked.CompareExchange(
ref array[offset /
sizeof(
long)], x, expected) == expected;
2595 else if (o is Array array1 && (offset %
sizeof(
long)) == 0)
2597 return CompareExchangeInt64(array1, offset, x, expected) == expected;
2599 else if (o is Array array2)
2601 return CompareExchangeInt64Unaligned(array2, offset, x, expected) == expected;
2605 return CompareAndSwapField(o, offset, expected, x);
2608 catch (Exception e) when (e is not global::java.lang.Throwable)
2610 throw new global::java.lang.InternalError(e);
2615 const int PARK_STATE_RUNNING = 0;
2616 const int PARK_STATE_PERMIT = 1;
2617 const int PARK_STATE_PARKED = 2;
2624 public static void unpark(
object self,
object thread)
2627 throw new NotImplementedException();
2629 var currentThread = (global::java.lang.Thread)thread;
2630 if (currentThread ==
null)
2631 throw new global::java.lang.IllegalStateException();
2633 if (currentThread !=
null)
2635 if (
Interlocked.CompareExchange(
ref currentThread.parkState, PARK_STATE_PERMIT, PARK_STATE_RUNNING) == PARK_STATE_PARKED)
2637 if (
Interlocked.CompareExchange(
ref currentThread.parkState, PARK_STATE_RUNNING, PARK_STATE_PARKED) == PARK_STATE_PARKED)
2640 Interlocked.CompareExchange(
ref currentThread.parkLock,
new global::java.lang.Object(),
null);
2643 lock (currentThread.parkLock)
2644 Monitor.Pulse(currentThread.parkLock);
2657 public static void park(
object self,
bool isAbsolute,
long time)
2660 throw new NotImplementedException();
2662 var currentThread = global::java.lang.Thread.currentThread();
2663 if (currentThread ==
null)
2664 throw new global::java.lang.IllegalStateException();
2666 if (
Interlocked.CompareExchange(
ref currentThread.parkState, PARK_STATE_RUNNING, PARK_STATE_PERMIT) == PARK_STATE_PERMIT)
2670 Interlocked.CompareExchange(
ref currentThread.parkLock,
new global::java.lang.Object(),
null);
2673 lock (currentThread.parkLock)
2675 if (
Interlocked.CompareExchange(
ref currentThread.parkState, PARK_STATE_PARKED, PARK_STATE_RUNNING) == PARK_STATE_PERMIT)
2677 Interlocked.CompareExchange(
ref currentThread.parkState, PARK_STATE_RUNNING, PARK_STATE_PERMIT);
2684 time -= global::java.lang.System.currentTimeMillis() * 1000000;
2691 ((global::java.lang.Object)currentThread.parkLock).wait(time / 1000000, (
int)(time % 1000000));
2693 catch (global::java.lang.InterruptedException)
2695 currentThread.interrupt();
2699 Interlocked.CompareExchange(
ref currentThread.parkState, PARK_STATE_RUNNING, PARK_STATE_PARKED);
2711 Thread.MemoryBarrier();
2716 Thread.MemoryBarrier();
2721 Thread.MemoryBarrier();
2730 static unsafe
byte ReadByte(Array obj,
long offset)
2733 throw new NotImplementedException();
2739 h = GCHandle.Alloc(obj, GCHandleType.Pinned);
2757 static unsafe
byte ReadByteVolatile(Array obj,
long offset)
2760 throw new NotImplementedException();
2766 h = GCHandle.Alloc(obj, GCHandleType.Pinned);
2768 var v = Volatile.Read(
ref r);
2785 static unsafe
short ReadInt16(Array obj,
long offset)
2788 throw new NotImplementedException();
2794 h = GCHandle.Alloc(obj, GCHandleType.Pinned);
2812 static unsafe
short ReadInt16Volatile(Array obj,
long offset)
2815 throw new NotImplementedException();
2821 h = GCHandle.Alloc(obj, GCHandleType.Pinned);
2840 static unsafe
int ReadInt32(Array obj,
long offset)
2843 throw new NotImplementedException();
2849 h = GCHandle.Alloc(obj, GCHandleType.Pinned);
2867 static unsafe
int ReadInt32Volatile(Array obj,
long offset)
2870 throw new NotImplementedException();
2876 h = GCHandle.Alloc(obj, GCHandleType.Pinned);
2895 static unsafe
long ReadInt64(Array obj,
long offset)
2898 throw new NotImplementedException();
2904 h = GCHandle.Alloc(obj, GCHandleType.Pinned);
2922 static unsafe
long ReadInt64Volatile(Array obj,
long offset)
2925 throw new NotImplementedException();
2931 h = GCHandle.Alloc(obj, GCHandleType.Pinned);
2950 static unsafe
void WriteByte(Array obj,
long offset,
byte value)
2953 throw new NotImplementedException();
2959 h = GCHandle.Alloc(obj, GCHandleType.Pinned);
2976 static unsafe
void WriteByteVolatile(Array obj,
long offset,
byte value)
2979 throw new NotImplementedException();
2985 h = GCHandle.Alloc(obj, GCHandleType.Pinned);
3002 static unsafe
void WriteInt16(Array obj,
long offset,
short value)
3005 throw new NotImplementedException();
3011 h = GCHandle.Alloc(obj, GCHandleType.Pinned);
3028 static unsafe
void WriteInt16Volatile(Array obj,
long offset,
short value)
3031 throw new NotImplementedException();
3037 h = GCHandle.Alloc(obj, GCHandleType.Pinned);
3054 static unsafe
void WriteInt32(Array obj,
long offset,
int value)
3057 throw new NotImplementedException();
3063 h = GCHandle.Alloc(obj, GCHandleType.Pinned);
3080 static unsafe
void WriteInt32Volatile(Array obj,
long offset,
int value)
3083 throw new NotImplementedException();
3089 h = GCHandle.Alloc(obj, GCHandleType.Pinned);
3106 static unsafe
void WriteInt64(Array obj,
long offset,
long value)
3109 throw new NotImplementedException();
3115 h = GCHandle.Alloc(obj, GCHandleType.Pinned);
3132 static unsafe
void WriteInt64Volatile(Array obj,
long offset,
long value)
3135 throw new NotImplementedException();
3141 h = GCHandle.Alloc(obj, GCHandleType.Pinned);
3160 static unsafe
int CompareExchangeInt32(Array obj,
long offset,
int value,
int expected)
3163 throw new NotImplementedException();
3169 h = GCHandle.Alloc(obj, GCHandleType.Pinned);
3189 static unsafe
int CompareExchangeInt32Unaligned(Array obj,
long offset,
int value,
int expected)
3192 throw new NotImplementedException();
3198 h = GCHandle.Alloc(obj, GCHandleType.Pinned);
3218 static unsafe
long CompareExchangeInt64(Array obj,
long offset,
long value,
long expected)
3221 throw new NotImplementedException();
3227 h = GCHandle.Alloc(obj, GCHandleType.Pinned);
3247 static unsafe
long CompareExchangeInt64Unaligned(Array obj,
long offset,
long value,
long expected)
3250 throw new NotImplementedException();
3256 h = GCHandle.Alloc(obj, GCHandleType.Pinned);
3273 static long InterlockedRead(
ref long location)
3276 throw new NotImplementedException();
3284 throw new global::java.lang.InternalError(e);
3294 static double InterlockedRead(
ref double location)
3297 throw new NotImplementedException();
3305 throw new global::java.lang.InternalError(e);
3315 static void InterlockedWrite(
ref long location,
long value)
3318 throw new NotImplementedException();
3326 throw new global::java.lang.InternalError(e);
3336 static void InterlockedWrite(
ref double location,
double value)
3339 throw new NotImplementedException();
3347 throw new global::java.lang.InternalError(e);