forked from neo-project/neo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJumpTable.Compound.cs
553 lines (527 loc) · 24 KB
/
JumpTable.Compound.cs
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
// Copyright (C) 2015-2024 The Neo Project.
//
// JumpTable.Compound.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.
using Neo.VM.Types;
using System;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using VMArray = Neo.VM.Types.Array;
namespace Neo.VM
{
/// <summary>
/// Partial class for manipulating compound types like maps, arrays, and structs within a jump table.
/// </summary>
public partial class JumpTable
{
/// <summary>
/// Packs a map from the evaluation stack.
/// <see cref="OpCode.PACKMAP"/>
/// </summary>
/// <param name="engine">The execution engine.</param>
/// <param name="instruction">The instruction being executed.</param>
/// <remarks>Pop 2n+1, Push 1</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void PackMap(ExecutionEngine engine, Instruction instruction)
{
var size = (int)engine.Pop().GetInteger();
if (size < 0 || size * 2 > engine.CurrentContext!.EvaluationStack.Count)
throw new InvalidOperationException($"The value {size} is out of range.");
Map map = new(engine.ReferenceCounter);
for (var i = 0; i < size; i++)
{
var key = engine.Pop<PrimitiveType>();
var value = engine.Pop();
map[key] = value;
}
engine.Push(map);
}
/// <summary>
/// Packs a struct from the evaluation stack.
/// <see cref="OpCode.PACKSTRUCT"/>
/// </summary>
/// <param name="engine">The execution engine.</param>
/// <param name="instruction">The instruction being executed.</param>
/// <remarks>Pop n+1, Push 1</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void PackStruct(ExecutionEngine engine, Instruction instruction)
{
var size = (int)engine.Pop().GetInteger();
if (size < 0 || size > engine.CurrentContext!.EvaluationStack.Count)
throw new InvalidOperationException($"The value {size} is out of range.");
Struct @struct = new(engine.ReferenceCounter);
for (var i = 0; i < size; i++)
{
var item = engine.Pop();
@struct.Add(item);
}
engine.Push(@struct);
}
/// <summary>
/// Packs an array from the evaluation stack.
/// <see cref="OpCode.PACK"/>
/// </summary>
/// <param name="engine">The execution engine.</param>
/// <param name="instruction">The instruction being executed.</param>
/// <remarks>Pop n+1, Push 1</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void Pack(ExecutionEngine engine, Instruction instruction)
{
var size = (int)engine.Pop().GetInteger();
if (size < 0 || size > engine.CurrentContext!.EvaluationStack.Count)
throw new InvalidOperationException($"The value {size} is out of range.");
VMArray array = new(engine.ReferenceCounter);
for (var i = 0; i < size; i++)
{
var item = engine.Pop();
array.Add(item);
}
engine.Push(array);
}
/// <summary>
/// Unpacks a compound type from the evaluation stack.
/// <see cref="OpCode.UNPACK"/>
/// </summary>
/// <param name="engine">The execution engine.</param>
/// <param name="instruction">The instruction being executed.</param>
/// <remarks>Pop 1, Push 2n+1 or n+1</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void Unpack(ExecutionEngine engine, Instruction instruction)
{
var compound = engine.Pop<CompoundType>();
switch (compound)
{
case Map map:
foreach (var (key, value) in map.Reverse())
{
engine.Push(value);
engine.Push(key);
}
break;
case VMArray array:
for (var i = array.Count - 1; i >= 0; i--)
{
engine.Push(array[i]);
}
break;
default:
throw new InvalidOperationException($"Invalid type for {instruction.OpCode}: {compound.Type}");
}
engine.Push(compound.Count);
}
/// <summary>
/// Creates a new empty array with zero elements on the evaluation stack.
/// <see cref="OpCode.NEWARRAY0"/>
/// </summary>
/// <param name="engine">The execution engine.</param>
/// <param name="instruction">The instruction being executed.</param>
/// <remarks>
/// Pop 0, Push 1
/// TODO: Change to NewNullArray method or add it?
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void NewArray0(ExecutionEngine engine, Instruction instruction)
{
engine.Push(new VMArray(engine.ReferenceCounter));
}
/// <summary>
/// Creates a new array with a specified number of elements on the evaluation stack.
/// <see cref="OpCode.NEWARRAY"/>
/// </summary>
/// <param name="engine">The execution engine.</param>
/// <param name="instruction">The instruction being executed.</param>
/// <remarks>Pop 1, Push 1</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void NewArray(ExecutionEngine engine, Instruction instruction)
{
var n = (int)engine.Pop().GetInteger();
if (n < 0 || n > engine.Limits.MaxStackSize)
throw new InvalidOperationException($"MaxStackSize exceed: {n}");
engine.Push(new VMArray(engine.ReferenceCounter, Enumerable.Repeat(StackItem.Null, n)));
}
/// <summary>
/// Creates a new array with a specified number of elements and a specified type on the evaluation stack.
/// <see cref="OpCode.NEWARRAY_T"/>
/// </summary>
/// <param name="engine">The execution engine.</param>
/// <param name="instruction">The instruction being executed.</param>
/// <remarks>Pop 1, Push 1</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void NewArray_T(ExecutionEngine engine, Instruction instruction)
{
var n = (int)engine.Pop().GetInteger();
if (n < 0 || n > engine.Limits.MaxStackSize)
throw new InvalidOperationException($"MaxStackSize exceed: {n}");
var type = (StackItemType)instruction.TokenU8;
if (!Enum.IsDefined(typeof(StackItemType), type))
throw new InvalidOperationException($"Invalid type for {instruction.OpCode}: {instruction.TokenU8}");
var item = instruction.TokenU8 switch
{
(byte)StackItemType.Boolean => StackItem.False,
(byte)StackItemType.Integer => Integer.Zero,
(byte)StackItemType.ByteString => ByteString.Empty,
_ => StackItem.Null
};
engine.Push(new VMArray(engine.ReferenceCounter, Enumerable.Repeat(item, n)));
}
/// <summary>
/// Creates a new empty struct with zero elements on the evaluation stack.
/// <see cref="OpCode.NEWSTRUCT0"/>
/// </summary>
/// <param name="engine">The execution engine.</param>
/// <param name="instruction">The instruction being executed.</param>
/// <remarks>Pop 0, Push 1</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void NewStruct0(ExecutionEngine engine, Instruction instruction)
{
engine.Push(new Struct(engine.ReferenceCounter));
}
/// <summary>
/// Creates a new struct with a specified number of elements on the evaluation stack.
/// <see cref="OpCode.NEWSTRUCT"/>
/// </summary>
/// <param name="engine">The execution engine.</param>
/// <param name="instruction">The instruction being executed.</param>
/// <remarks>Pop 1, Push 1</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void NewStruct(ExecutionEngine engine, Instruction instruction)
{
var n = (int)engine.Pop().GetInteger();
if (n < 0 || n > engine.Limits.MaxStackSize)
throw new InvalidOperationException($"MaxStackSize exceed: {n}");
Struct result = new(engine.ReferenceCounter);
for (var i = 0; i < n; i++)
result.Add(StackItem.Null);
engine.Push(result);
}
/// <summary>
/// Creates a new empty map on the evaluation stack.
/// <see cref="OpCode.NEWMAP"/>
/// </summary>
/// <param name="engine">The execution engine.</param>
/// <param name="instruction">The instruction being executed.</param>
/// <remarks>Pop 0, Push 1</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void NewMap(ExecutionEngine engine, Instruction instruction)
{
engine.Push(new Map(engine.ReferenceCounter));
}
/// <summary>
/// Gets the size of the top item on the evaluation stack and pushes it onto the stack.
/// <see cref="OpCode.SIZE"/>
/// </summary>
/// <param name="engine">The execution engine.</param>
/// <param name="instruction">The instruction being executed.</param>
/// <remarks>Pop 1, Push 1</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void Size(ExecutionEngine engine, Instruction instruction)
{
// TODO: we should be able to optimize by using peek instead of dup and pop
var x = engine.Pop();
switch (x)
{
case CompoundType compound:
engine.Push(compound.Count);
break;
case PrimitiveType primitive:
engine.Push(primitive.Size);
break;
case Types.Buffer buffer:
engine.Push(buffer.Size);
break;
default:
throw new InvalidOperationException($"Invalid type for {instruction.OpCode}: {x.Type}");
}
}
/// <summary>
/// Checks whether the top item on the evaluation stack has the specified key.
/// <see cref="OpCode.HASKEY"/>
/// </summary>
/// <param name="engine">The execution engine.</param>
/// <param name="instruction">The instruction being executed.</param>
/// <remarks>Pop 2, Push 1</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void HasKey(ExecutionEngine engine, Instruction instruction)
{
var key = engine.Pop<PrimitiveType>();
var x = engine.Pop();
// Check the type of the top item and perform the corresponding action.
switch (x)
{
// For arrays, check if the index is within bounds and push the result onto the stack.
case VMArray array:
{
// TODO: Overflow and underflow checking needs to be done.
var index = (int)key.GetInteger();
if (index < 0)
throw new InvalidOperationException($"The negative value {index} is invalid for OpCode.{instruction.OpCode}.");
engine.Push(index < array.Count);
break;
}
// For maps, check if the key exists and push the result onto the stack.
case Map map:
{
engine.Push(map.ContainsKey(key));
break;
}
// For buffers, check if the index is within bounds and push the result onto the stack.
case Types.Buffer buffer:
{
// TODO: Overflow and underflow checking needs to be done.
var index = (int)key.GetInteger();
if (index < 0)
throw new InvalidOperationException($"The negative value {index} is invalid for OpCode.{instruction.OpCode}.");
engine.Push(index < buffer.Size);
break;
}
// For byte strings, check if the index is within bounds and push the result onto the stack.
case ByteString array:
{
// TODO: Overflow and underflow checking needs to be done.
var index = (int)key.GetInteger();
if (index < 0)
throw new InvalidOperationException($"The negative value {index} is invalid for OpCode.{instruction.OpCode}.");
engine.Push(index < array.Size);
break;
}
default:
throw new InvalidOperationException($"Invalid type for {instruction.OpCode}: {x.Type}");
}
}
/// <summary>
/// Retrieves the keys of a map and pushes them onto the evaluation stack as an array.
/// <see cref="OpCode.KEYS"/>
/// </summary>
/// <param name="engine">The execution engine.</param>
/// <param name="instruction">The instruction being executed.</param>
/// <remarks>Pop 1, Push 1</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void Keys(ExecutionEngine engine, Instruction instruction)
{
var map = engine.Pop<Map>();
engine.Push(new VMArray(engine.ReferenceCounter, map.Keys));
}
/// <summary>
/// Retrieves the values of a compound type and pushes them onto the evaluation stack as an array.
/// <see cref="OpCode.VALUES"/>
/// </summary>
/// <param name="engine">The execution engine.</param>
/// <param name="instruction">The instruction being executed.</param>
/// <remarks>Pop 1, Push 1</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void Values(ExecutionEngine engine, Instruction instruction)
{
var x = engine.Pop();
var values = x switch
{
VMArray array => array,
Map map => map.Values,
_ => throw new InvalidOperationException($"Invalid type for {instruction.OpCode}: {x.Type}"),
};
VMArray newArray = new(engine.ReferenceCounter);
foreach (var item in values)
if (item is Struct s)
newArray.Add(s.Clone(engine.Limits));
else
newArray.Add(item);
engine.Push(newArray);
}
/// <summary>
/// Retrieves the item from an array, map, buffer, or byte string based on the specified key,
/// and pushes it onto the evaluation stack.
/// <see cref="OpCode.PICKITEM"/>
/// </summary>
/// <param name="engine">The execution engine.</param>
/// <param name="instruction">The instruction being executed.</param>
/// <remarks>Pop 2, Push 1</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void PickItem(ExecutionEngine engine, Instruction instruction)
{
var key = engine.Pop<PrimitiveType>();
var x = engine.Pop();
switch (x)
{
case VMArray array:
{
var index = (int)key.GetInteger();
if (index < 0 || index >= array.Count)
throw new CatchableException($"The value {index} is out of range.");
engine.Push(array[index]);
break;
}
case Map map:
{
if (!map.TryGetValue(key, out var value))
throw new CatchableException($"Key not found in {nameof(Map)}");
engine.Push(value);
break;
}
case PrimitiveType primitive:
{
var byteArray = primitive.GetSpan();
var index = (int)key.GetInteger();
if (index < 0 || index >= byteArray.Length)
throw new CatchableException($"The value {index} is out of range.");
engine.Push((BigInteger)byteArray[index]);
break;
}
case Types.Buffer buffer:
{
var index = (int)key.GetInteger();
if (index < 0 || index >= buffer.Size)
throw new CatchableException($"The value {index} is out of range.");
engine.Push((BigInteger)buffer.InnerBuffer.Span[index]);
break;
}
default:
throw new InvalidOperationException($"Invalid type for {instruction.OpCode}: {x.Type}");
}
}
/// <summary>
/// Appends an item to the end of the specified array.
/// <see cref="OpCode.APPEND"/>
/// </summary>
/// <param name="engine">The execution engine.</param>
/// <param name="instruction">The instruction being executed.</param>
/// <remarks>Pop 2, Push 0</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void Append(ExecutionEngine engine, Instruction instruction)
{
var newItem = engine.Pop();
var array = engine.Pop<VMArray>();
if (newItem is Struct s) newItem = s.Clone(engine.Limits);
array.Add(newItem);
}
/// <summary>
/// A value v, index n (or key) and an array (or map) are taken from main stack. Attribution array[n]=v (or map[n]=v) is performed.
/// <see cref="OpCode.SETITEM"/>
/// </summary>
/// <param name="engine">The execution engine.</param>
/// <param name="instruction">The instruction being executed.</param>
/// <remarks>Pop 3, Push 0</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void SetItem(ExecutionEngine engine, Instruction instruction)
{
var value = engine.Pop();
if (value is Struct s) value = s.Clone(engine.Limits);
var key = engine.Pop<PrimitiveType>();
var x = engine.Pop();
switch (x)
{
case VMArray array:
{
var index = (int)key.GetInteger();
if (index < 0 || index >= array.Count)
throw new CatchableException($"The value {index} is out of range.");
array[index] = value;
break;
}
case Map map:
{
map[key] = value;
break;
}
case Types.Buffer buffer:
{
var index = (int)key.GetInteger();
if (index < 0 || index >= buffer.Size)
throw new CatchableException($"The value {index} is out of range.");
if (value is not PrimitiveType p)
throw new InvalidOperationException($"Value must be a primitive type in {instruction.OpCode}");
var b = (int)p.GetInteger();
if (b < sbyte.MinValue || b > byte.MaxValue)
throw new InvalidOperationException($"Overflow in {instruction.OpCode}, {b} is not a byte type.");
buffer.InnerBuffer.Span[index] = (byte)b;
break;
}
default:
throw new InvalidOperationException($"Invalid type for {instruction.OpCode}: {x.Type}");
}
}
/// <summary>
/// Reverses the order of items in the specified array or buffer.
/// <see cref="OpCode.REVERSEITEMS"/>
/// </summary>
/// <param name="engine">The execution engine.</param>
/// <param name="instruction">The instruction being executed.</param>
/// <remarks>Pop 1, Push 0</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void ReverseItems(ExecutionEngine engine, Instruction instruction)
{
var x = engine.Pop();
switch (x)
{
case VMArray array:
array.Reverse();
break;
case Types.Buffer buffer:
buffer.InnerBuffer.Span.Reverse();
break;
default:
throw new InvalidOperationException($"Invalid type for {instruction.OpCode}: {x.Type}");
}
}
/// <summary>
/// Removes the item at the specified index from the array or map.
/// <see cref="OpCode.REMOVE"/>
/// </summary>
/// <param name="engine">The execution engine.</param>
/// <param name="instruction">The instruction being executed.</param>
/// <remarks>Pop 2, Push 0</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void Remove(ExecutionEngine engine, Instruction instruction)
{
var key = engine.Pop<PrimitiveType>();
var x = engine.Pop();
switch (x)
{
case VMArray array:
var index = (int)key.GetInteger();
if (index < 0 || index >= array.Count)
throw new InvalidOperationException($"The value {index} is out of range.");
array.RemoveAt(index);
break;
case Map map:
map.Remove(key);
break;
default:
throw new InvalidOperationException($"Invalid type for {instruction.OpCode}: {x.Type}");
}
}
/// <summary>
/// Clears all items from the compound type.
/// <see cref="OpCode.CLEARITEMS"/>
/// </summary>
/// <param name="engine">The execution engine.</param>
/// <param name="instruction">The instruction being executed.</param>
/// <remarks>Pop 1, Push 0</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void ClearItems(ExecutionEngine engine, Instruction instruction)
{
var x = engine.Pop<CompoundType>();
x.Clear();
}
/// <summary>
/// Removes and returns the item at the top of the specified array.
/// <see cref="OpCode.POPITEM"/>
/// </summary>
/// <param name="engine">The execution engine.</param>
/// <param name="instruction">The instruction being executed.</param>
/// <remarks>Pop 1, Push 1</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void PopItem(ExecutionEngine engine, Instruction instruction)
{
var x = engine.Pop<VMArray>();
var index = x.Count - 1;
engine.Push(x[index]);
x.RemoveAt(index);
}
}
}