-
Notifications
You must be signed in to change notification settings - Fork 428
Expand file tree
/
Copy pathStackOperation.cs
More file actions
810 lines (739 loc) · 30.7 KB
/
StackOperation.cs
File metadata and controls
810 lines (739 loc) · 30.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/*
* Tencent is pleased to support the open source community by making InjectFix available.
* Copyright (C) 2019 Tencent. All rights reserved.
* InjectFix is licensed under the MIT License, except for the third-party components listed in the file 'LICENSE' which may be subject to their corresponding license terms.
* This file is subject to the terms and conditions defined in file 'LICENSE', which is part of this source code package.
*/
namespace IFix.Core
{
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Collections.Generic;
[StructLayout(LayoutKind.Sequential)]
unsafe struct UnmanagedStack
{
public Value* Base;
public Value* Top;
}
unsafe class ThreadStackInfo
{
public UnmanagedStack* UnmanagedStack;
public object[] ManagedStack;
IntPtr evaluationStackHandler;
IntPtr unmanagedStackHandler;
//int index;
public ThreadStackInfo()
{
//index = idx;
evaluationStackHandler = Marshal.AllocHGlobal(sizeof(Value) * VirtualMachine.MAX_EVALUATION_STACK_SIZE);
unmanagedStackHandler = Marshal.AllocHGlobal(sizeof(UnmanagedStack));
UnmanagedStack = (UnmanagedStack*)unmanagedStackHandler.ToPointer();
UnmanagedStack->Base = UnmanagedStack->Top = (Value*)evaluationStackHandler.ToPointer();
ManagedStack = new object[VirtualMachine.MAX_EVALUATION_STACK_SIZE];
}
//去掉析构,正常而言,静态变量不会析构,如果整个虚拟机释放的话,通过Marshal.AllocHGlobal分配的非托管
//内存应该也会自动释放吧?
//~ThreadStackInfo()
//{
// //VirtualMachine._Info("~ThreadStackInfo");
// lock(stackListGuard)
// {
// stackList[index] = null;
// }
// UnmanagedStack = null;
// ManagedStack = null;
// Marshal.FreeHGlobal(evaluationStackHandler);
// Marshal.FreeHGlobal(unmanagedStackHandler);
//}
//本来ThreadStatic是很合适的方案,但据说Unity下的ThreadStatic会Crash,
//Unity文档:https://docs.unity3d.com/Manual/Attributes.html
//相关issue链接:https://issuetracker.unity3d.com/issues/
// e-document-threadstatic-attribute-must-not-be-used-i-will-cause-crashes
//issue内容:
//This is a known limitation of the liveness check, as the we don't handle thread static or
//context static variables as roots when performing the collection.
//The crash will happen in mono_unity_liveness_calculation_from_statics
//[ThreadStatic]
//internal static ThreadStackInfo Stack = null;
static LocalDataStoreSlot localSlot = Thread.AllocateDataSlot();
internal static ThreadStackInfo Stack
{
get
{
var stack = Thread.GetData(localSlot) as ThreadStackInfo;
if (stack == null)
{
VirtualMachine._Info("create thread stack");
stack = new ThreadStackInfo();
Thread.SetData(localSlot, stack);
}
return stack;
}
}
}
unsafe internal static class EvaluationStackOperation
{
internal static void UnboxPrimitive(Value* evaluationStackPointer, object obj, Type type)
{
if (obj.GetType().IsEnum)
{
obj = Convert.ChangeType(obj, type);
}
if (obj is int)
{
evaluationStackPointer->Type = ValueType.Integer;
evaluationStackPointer->Value1 = (int)obj;
}
else if (obj is float)
{
evaluationStackPointer->Type = ValueType.Float;
*(float*)(&evaluationStackPointer->Value1) = (float)obj;
}
else if (obj is bool)
{
evaluationStackPointer->Type = ValueType.Integer;
evaluationStackPointer->Value1 = (bool)(obj) ? 1 : 0;
}
else if (obj is double)
{
evaluationStackPointer->Type = ValueType.Double;
*(double*)(&evaluationStackPointer->Value1) = (double)obj;
}
else if (obj is long)
{
evaluationStackPointer->Type = ValueType.Long;
*(long*)(&evaluationStackPointer->Value1) = (long)obj;
}
else if (obj is byte)
{
evaluationStackPointer->Type = ValueType.Integer;
evaluationStackPointer->Value1 = (byte)obj;
}
else if (obj is uint)
{
evaluationStackPointer->Type = ValueType.Integer;
evaluationStackPointer->Value1 = (int)(uint)obj;
}
else if (obj is ushort)
{
evaluationStackPointer->Type = ValueType.Integer;
evaluationStackPointer->Value1 = (int)(ushort)obj;
}
else if (obj is short)
{
evaluationStackPointer->Type = ValueType.Integer;
evaluationStackPointer->Value1 = (short)obj;
}
else if (obj is char)
{
evaluationStackPointer->Type = ValueType.Integer;
evaluationStackPointer->Value1 = (int)(char)obj;
}
else if (obj is ulong)
{
evaluationStackPointer->Type = ValueType.Long;
*(ulong*)(&evaluationStackPointer->Value1) = (ulong)obj;
}
else if (obj is sbyte)
{
evaluationStackPointer->Type = ValueType.Integer;
evaluationStackPointer->Value1 = (sbyte)obj;
}
else if (obj is IntPtr)
{
evaluationStackPointer->Type = ValueType.Long;
*(long*)(&evaluationStackPointer->Value1) = ((IntPtr)obj).ToInt64();
}
else if (obj is UIntPtr)
{
evaluationStackPointer->Type = ValueType.Long;
*(ulong*)(&evaluationStackPointer->Value1) = ((UIntPtr)obj).ToUInt64();
}
else
throw new NotImplementedException("Unbox a " + obj.GetType() + " to " + type);
}
internal static object mGet(bool isArray, object root, int layer, int[] fieldIdList, FieldInfo[] fieldInfos, Dictionary<int, NewFieldInfo> newFieldInfos)
{
//Console.WriteLine("mGet " + root);
var fieldId = fieldIdList[layer];
if (layer == 0)
{
if (isArray)
{
return (root as Array).GetValue(fieldId);
}
else
{
var fieldInfo = fieldInfos[fieldId];
if(fieldInfo == null)
{
return newFieldInfos[fieldId].GetValue(root);
}
return fieldInfo.GetValue(root);
}
}
else
{
var fieldInfo = fieldInfos[fieldId];
if(fieldInfo == null)
{
return newFieldInfos[fieldId].GetValue(mGet(isArray, root, layer - 1, fieldIdList, fieldInfos, newFieldInfos));
}
//VirtualMachine._Info("before --- " + fieldInfo);
var ret = fieldInfo.GetValue(mGet(isArray, root, layer - 1, fieldIdList, fieldInfos, newFieldInfos));
//VirtualMachine._Info("after --- " + fieldInfo);
return ret;
}
}
internal static void mSet(bool isArray, object root, object val, int layer, int[] fieldIdList,
FieldInfo[] fieldInfos, Dictionary<int, NewFieldInfo> newFieldInfos)
{
var fieldId = fieldIdList[layer];
if (layer == 0)
{
if (isArray)
{
(root as Array).SetValue(val, fieldId);
}
else
{
var fieldInfo = fieldInfos[fieldId];
if(fieldInfo == null)
{
newFieldInfos[fieldId].SetValue(root, val);
}
else
{
//VirtualMachine._Info("set1 " + val.GetType() + " to " + fieldInfo + " of " + root.GetType()
// + ", root.hc = " + root.GetHashCode());
fieldInfo.SetValue(root, val);
}
}
}
else
{
var fieldInfo = fieldInfos[fieldId];
//VirtualMachine._Info("before get " + fieldInfo);
var parent = mGet(isArray, root, layer - 1, fieldIdList, fieldInfos, newFieldInfos);
//VirtualMachine._Info("after get " + fieldInfo);
//VirtualMachine._Info("before set " + fieldInfo);
if(fieldInfo == null)
{
newFieldInfos[fieldId].SetValue(parent, val);
}
else
{
fieldInfo.SetValue(parent, val);
}
//VirtualMachine._Info("set2 " + val.GetType() + " to " + fieldInfo + " of " + parent.GetType());
//VirtualMachine._Info("after set " + fieldInfo);
mSet(isArray, root, parent, layer - 1, fieldIdList, fieldInfos, newFieldInfos);
}
}
// #lizard forgives
internal static unsafe object ToObject(Value* evaluationStackBase, Value* evaluationStackPointer,
object[] managedStack, Type type, VirtualMachine virtualMachine, bool valueTypeClone = true)
{
//未初始化的local引用可能作为out参数反射调用
//TODO: 验证值类型out参数,对应参数位置是否可以是null?
switch (evaluationStackPointer->Type)
{
case ValueType.Integer:
{
int i = evaluationStackPointer->Value1;
if (type == typeof(int))
return i;
else if (type == typeof(bool))
return i == 1;
else if (type == typeof(sbyte))
return (sbyte)i;
else if (type == typeof(byte))
return (byte)i;
else if (type == typeof(char))
return (char)i;
else if (type == typeof(short))
return (short)i;
else if (type == typeof(ushort))
return (ushort)i;
else if (type == typeof(uint))
return (uint)i;
else if (type.IsEnum)
{
return Enum.ToObject(type, i);
}
else
return null;
}
case ValueType.Long:
{
long l = *(long*)&evaluationStackPointer->Value1;
if (type == typeof(long))
{
return l;
}
else if (type == typeof(ulong))
{
return (ulong)l;
}
else if (type == typeof(IntPtr))
{
return new IntPtr(l);
}
else if (type == typeof(UIntPtr))
{
return new UIntPtr((ulong)l);
}
else if (type.IsEnum)
{
return Enum.ToObject(type, l);
}
else
{
return null;
}
}
case ValueType.Float:
{
if (type == typeof(float))
{
return *(float*)&evaluationStackPointer->Value1;
}
else
{
return null;
}
}
case ValueType.Double:
{
if (type == typeof(double))
{
return *(double*)&evaluationStackPointer->Value1;
}
else
{
return null;
}
}
case ValueType.Object:
return managedStack[evaluationStackPointer->Value1];
case ValueType.ValueType:
if (valueTypeClone && managedStack[evaluationStackPointer->Value1] != null)
{
return virtualMachine.objectClone.Clone(managedStack[evaluationStackPointer->Value1]);
}
else
{
return managedStack[evaluationStackPointer->Value1];
}
case ValueType.StackReference:
{
return ToObject(evaluationStackBase, (*(Value**)&evaluationStackPointer->Value1),
managedStack, type, virtualMachine, valueTypeClone);
}
case ValueType.FieldReference:
case ValueType.ChainFieldReference:
{
//VirtualMachine._Info("ToObject FieldReference:" + evaluationStackPointer->Value2
// + "," + evaluationStackPointer->Value1);
if (evaluationStackPointer->Type == ValueType.ChainFieldReference)
{
var fieldAddr = managedStack[evaluationStackPointer - evaluationStackBase] as FieldAddr;
var fieldIdList = fieldAddr.FieldIdList;
return mGet(evaluationStackPointer->Value2 != -1,
fieldAddr.Object, fieldIdList.Length - 1,
fieldIdList, virtualMachine.fieldInfos, virtualMachine.newFieldInfos);
}
else
{
if (evaluationStackPointer->Value2 >= 0)
{
var fieldInfo = virtualMachine.fieldInfos[evaluationStackPointer->Value2];
var obj = managedStack[evaluationStackPointer->Value1];
if(fieldInfo == null)
{
virtualMachine.newFieldInfos[evaluationStackPointer->Value2].CheckInit(virtualMachine, obj);
return virtualMachine.newFieldInfos[evaluationStackPointer->Value2].GetValue(obj);
}
return fieldInfo.GetValue(obj);
}
else
{
var obj = managedStack[evaluationStackPointer->Value1] as AnonymousStorey;
return obj.Get(-(evaluationStackPointer->Value2 + 1), type,
virtualMachine, valueTypeClone);
}
}
}
case ValueType.ArrayReference:
var arr = managedStack[evaluationStackPointer->Value1] as Array;
return arr.GetValue(evaluationStackPointer->Value2);
case ValueType.StaticFieldReference:
{
var fieldIndex = evaluationStackPointer->Value1;
if (fieldIndex >= 0)
{
var fieldInfo = virtualMachine.fieldInfos[fieldIndex];
if(fieldInfo == null)
{
virtualMachine.newFieldInfos[fieldIndex].CheckInit(virtualMachine, null);
return virtualMachine.newFieldInfos[fieldIndex].GetValue(null);
}
return fieldInfo.GetValue(null);
}
else
{
fieldIndex = -(fieldIndex + 1);
return virtualMachine.staticFields[fieldIndex];
}
}
default:
throw new NotImplementedException("get obj of " + evaluationStackPointer->Type);
}
}
public static void PushObject(Value* evaluationStackBase, Value* evaluationStackPointer,
object[] managedStack, object obj, Type type)
{
if (obj != null)
{
if (type.IsPrimitive)
{
UnboxPrimitive(evaluationStackPointer, obj, type);
return;
}
else if (type.IsEnum)
{
var underlyingType = Enum.GetUnderlyingType(type);
if (underlyingType == typeof(long) || underlyingType == typeof(ulong))
{
evaluationStackPointer->Type = ValueType.Long;
*(long*)(&evaluationStackPointer->Value1) = underlyingType == typeof(long) ?
Convert.ToInt64(obj) : (long)Convert.ToUInt64(obj) ;
}
else if (underlyingType == typeof(uint))
{
evaluationStackPointer->Type = ValueType.Integer;
evaluationStackPointer->Value1 = unchecked((int) Convert.ToUInt32(obj));
}
else
{
evaluationStackPointer->Type = ValueType.Integer;
evaluationStackPointer->Value1 = Convert.ToInt32(obj);
}
return;
}
}
int pos = (int)(evaluationStackPointer - evaluationStackBase);
evaluationStackPointer->Value1 = pos;
managedStack[pos] = obj;
evaluationStackPointer->Type = (obj != null && type.IsValueType) ?
ValueType.ValueType : ValueType.Object;
}
public static void UpdateReference(Value* evaluationStackBase, Value* evaluationStackPointer,
object[] managedStack, object obj, VirtualMachine virtualMachine, Type type) //反射专用
{
switch (evaluationStackPointer->Type)
{
case ValueType.StackReference:
var des = *(Value**)&evaluationStackPointer->Value1;
//VirtualMachine._Info("UpdateReference des->Type:" + des->Type + ", des->Value1:"
// + des->Value1 + ", des:" + new IntPtr(des) + ", offset:" + (des - evaluationStackBase) );
PushObject(evaluationStackBase, des, managedStack, obj, type);
break;
case ValueType.ArrayReference:
var arr = managedStack[evaluationStackPointer->Value1] as Array;
arr.SetValue(obj, evaluationStackPointer->Value2);
break;
case ValueType.FieldReference:
case ValueType.ChainFieldReference:
{
if (evaluationStackPointer->Type == ValueType.ChainFieldReference)
{
var fieldAddr = managedStack[evaluationStackPointer - evaluationStackBase] as FieldAddr;
var fieldIdList = fieldAddr.FieldIdList;
//for(int i = 0; i < fieldIdList.Length; i++)
//{
// VirtualMachine._Info("fid " + i + ": " + fieldIdList[i] + ", "
// + virtualMachine.fieldInfos[fieldIdList[i]]);
//}
mSet(evaluationStackPointer->Value2 != -1,
fieldAddr.Object, obj, fieldIdList.Length - 1,
fieldIdList, virtualMachine.fieldInfos, virtualMachine.newFieldInfos);
}
else
{
if (evaluationStackPointer->Value2 >= 0)
{
var fieldInfo = virtualMachine.fieldInfos[evaluationStackPointer->Value2];
if(fieldInfo == null)
{
virtualMachine.newFieldInfos[evaluationStackPointer->Value2].SetValue(managedStack[evaluationStackPointer->Value1], obj);;
}
else
{
//VirtualMachine._Info("update field: " + fieldInfo);
//VirtualMachine._Info("update field of: " + fieldInfo.DeclaringType);
//VirtualMachine._Info("update ref obj: "
// + managedStack[evaluationStackPointer->Value1]);
//VirtualMachine._Info("update ref obj idx: " + evaluationStackPointer->Value1);
fieldInfo.SetValue(managedStack[evaluationStackPointer->Value1], obj);
}
}
else
{
var anonymousStorey = managedStack[evaluationStackPointer->Value1]
as AnonymousStorey;
anonymousStorey.Set(-(evaluationStackPointer->Value2 + 1), obj, type, virtualMachine);
}
}
break;
}
case ValueType.StaticFieldReference://更新完毕,直接return
{
var fieldIndex = evaluationStackPointer->Value1;
if (fieldIndex >= 0)
{
var fieldInfo = virtualMachine.fieldInfos[evaluationStackPointer->Value1];
if(fieldInfo == null)
{
virtualMachine.newFieldInfos[evaluationStackPointer->Value1].SetValue(null, obj);;
}
else
{
fieldInfo.SetValue(null, obj);
}
}
else
{
fieldIndex = -(fieldIndex + 1);
virtualMachine.staticFields[fieldIndex] = obj;
}
break;
}
}
}
}
unsafe public struct Call
{
internal Value* argumentBase;
internal Value* evaluationStackBase;
internal object[] managedStack;
internal Value* currentTop;//用于push状态
internal Value** topWriteBack;
public static Call Begin()
{
var stack = ThreadStackInfo.Stack;
return new Call()
{
managedStack = stack.ManagedStack,
currentTop = stack.UnmanagedStack->Top,
argumentBase = stack.UnmanagedStack->Top,
evaluationStackBase = stack.UnmanagedStack->Base,
topWriteBack = &(stack.UnmanagedStack->Top)
};
}
internal static Call BeginForStack(ThreadStackInfo stack)
{
return new Call()
{
managedStack = stack.ManagedStack,
currentTop = stack.UnmanagedStack->Top,
argumentBase = stack.UnmanagedStack->Top,
evaluationStackBase = stack.UnmanagedStack->Base,
topWriteBack = &(stack.UnmanagedStack->Top)
};
}
public void PushBoolean(bool b)
{
currentTop->Value1 = b ? 1 : 0;
currentTop->Type = ValueType.Integer;
currentTop++;
}
public bool GetBoolean(int offset = 0)
{
return (argumentBase + offset)->Value1 == 0 ? false : true;
}
public void PushByte(byte b)
{
PushInt32(b);
}
public byte GetByte(int offset = 0)
{
return (byte)GetInt32(offset);
}
public void PushSByte(sbyte sb)
{
PushInt32(sb);
}
public sbyte GetSByte(int offset = 0)
{
return (sbyte)GetInt32(offset);
}
public void PushInt16(short s)
{
PushInt32(s);
}
public short GetInt16(int offset = 0)
{
return (short)GetInt32(offset);
}
public void PushChar(char c)
{
PushInt32(c);
}
public char GetChar(int offset = 0)
{
return (char)GetInt32(offset);
}
public void PushUInt16(ushort us)
{
PushInt32(us);
}
public ushort GetUInt16(int offset = 0)
{
return (ushort)GetInt32(offset);
}
public void PushInt32(int i)
{
currentTop->Value1 = i;
currentTop->Type = ValueType.Integer;
currentTop++;
}
public int GetInt32(int offset = 0)
{
return (argumentBase + offset)->Value1;
}
public void PushUInt32(uint ui)
{
PushInt32((int)ui);
}
public uint GetUInt32(int offset = 0)
{
return (uint)GetInt32(offset);
}
public void PushInt64(long i)
{
*(long*)¤tTop->Value1 = i;
currentTop->Type = ValueType.Long;
currentTop++;
}
public long GetInt64(int offset = 0)
{
return *((long*)&((argumentBase + offset)->Value1));
}
public void PushUInt64(ulong i)
{
PushInt64((long)i);
}
public ulong GetUInt64(int offset = 0)
{
return (ulong)GetInt64(offset);
}
public void PushSingle(float f)
{
*(float*)(¤tTop->Value1) = f;
currentTop->Type = ValueType.Float;
currentTop++;
}
public float GetSingle(int offset = 0)
{
return *((float*)&((argumentBase + offset)->Value1));
}
public void PushDouble(double d)
{
*(double*)(¤tTop->Value1) = d;
currentTop->Type = ValueType.Double;
currentTop++;
}
public double GetDouble(int offset = 0)
{
return *((double*)&((argumentBase + offset)->Value1));
}
public void PushIntPtr(IntPtr i)
{
PushInt64(i.ToInt64());
}
public IntPtr GetIntPtr(int offset = 0)
{
return new IntPtr(GetInt64(offset));
}
public void PushUIntPtr(UIntPtr i)
{
PushUInt64(i.ToUInt64());
}
public UIntPtr GetUIntPtr(int offset = 0)
{
return new UIntPtr(GetUInt64(offset));
}
public void PushObject(object o)
{
int pos = (int)(currentTop - evaluationStackBase);
currentTop->Type = ValueType.Object;
currentTop->Value1 = pos;
managedStack[pos] = o;
currentTop++;
}
public void PushValueType(object o)
{
int pos = (int)(currentTop - evaluationStackBase);
currentTop->Type = ValueType.ValueType;
currentTop->Value1 = pos;
managedStack[pos] = o;
currentTop++;
}
public object GetObject(int offset = 0)
{
var ptr = argumentBase + offset;
object ret = managedStack[ptr->Value1];
managedStack[ptr - evaluationStackBase] = null;
return ret;
}
public T GetAsType<T>(int offset = 0)
{
//if (typeof(T).IsEnum)
//{
// var obj = GetObject(offset);
// var ptr = argumentBase + offset;
// VirtualMachine._Info("ptr =" + new IntPtr(ptr) + ", offset=" + (ptr - evaluationStackBase)
// + ",ptr->Value1=" + ptr->Value1 + ",ptr->Type=" + ptr->Type);
// if (obj != null)
// {
// VirtualMachine._Info("obj = " + obj + ", type = " + obj.GetType());
// }
// else
// {
// VirtualMachine._Info("obj = null");
// }
// return (T)Enum.ToObject(typeof(T), obj);
//}
//else
//{
// return (T)GetObject(offset);
//}
return (T)GetObject(offset);
}
public void PushObjectAsResult(object obj, Type type) //反射专用
{
EvaluationStackOperation.PushObject(evaluationStackBase, argumentBase, managedStack, obj, type);
currentTop = argumentBase + 1;
}
public void PushRef(int offset)
{
//Console.WriteLine("PushRef:" + offset + " address:" + new IntPtr(argumentBase + offset));
*(Value**)¤tTop->Value1 = argumentBase + offset;
currentTop->Type = ValueType.StackReference;
currentTop++;
}
public void UpdateReference(int offset, object obj, VirtualMachine virtualMachine, Type type) //反射专用
{
EvaluationStackOperation.UpdateReference(ThreadStackInfo.Stack.UnmanagedStack->Base,
argumentBase + offset, managedStack, obj, virtualMachine, type);
}
public static void End(ref Call call)
{
//Top的维护
//ThreadStackInfo.Stack.UnmanagedStack->Top = call.argumentBase;
}
}
}