@@ -27,9 +27,52 @@ This Source Code Form is subject to the terms of the
2727
2828namespace ScriptEngine . Machine
2929{
30+ public class OpStackChecked
31+ {
32+ private readonly List < IValue > _stack = new List < IValue > ( ) ;
33+ private int _size = 0 ;
34+ public OpStackChecked ( ) { }
35+
36+ public int Count => _size ;
37+ public IValue Pop ( )
38+ { return _size > 0 ? _stack [ -- _size ] : throw new RuntimeException ( "Stack is empty" ) ; }
39+ public IValue Peek ( )
40+ { return _size > 0 ? _stack [ _size - 1 ] : throw new RuntimeException ( "Stack is empty" ) ; }
41+ public void Drop ( )
42+ { if ( _size > 0 ) _size -= 1 ; else throw new RuntimeException ( "Stack is empty" ) ; }
43+ public void Push ( IValue val )
44+ { if ( _size < _stack . Count ) _stack [ _size ] = val ; else _stack . Add ( val ) ; _size ++ ; }
45+ public IValue this [ int index ]
46+ {
47+ get => index < _size ? _stack [ _size - index - 1 ] : throw RuntimeException . IndexOutOfRange ( ) ;
48+ set { if ( index < _size ) _stack [ _size - index - 1 ] = value ; else throw RuntimeException . IndexOutOfRange ( ) ; }
49+ }
50+ }
51+
52+ public class OpStack
53+ {
54+ private IValue [ ] _stack = new IValue [ 64 ] ;
55+ private int _size = 0 ;
56+ public OpStack ( ) { }
57+
58+ public int Count => _size ;
59+ public IValue Pop ( ) { return _stack [ -- _size ] ; }
60+ public IValue Peek ( ) { return _stack [ _size - 1 ] ; }
61+ public void Drop ( ) { _size -= 1 ; }
62+ public void Push ( IValue val )
63+ { if ( _size >= _stack . Length ) Array . Resize ( ref _stack , _size * 3 / 2 ) ; _stack [ _size ] = val ; _size ++ ; }
64+ public IValue this [ int index ]
65+ {
66+ get => _stack [ _size - index - 1 ] ;
67+ set { _stack [ _size - index - 1 ] = value ; }
68+ }
69+ }
70+
3071 public class MachineInstance
3172 {
32- private Stack < IValue > _operationStack ;
73+ //OpStack _opStack = new OpStack();
74+ //private Stack<IValue> _operationStack;
75+ private OpStack _operationStack ;
3376 private Stack < ExecutionFrame > _callStack ;
3477 private ExecutionFrame _currentFrame ;
3578 private Action < int > [ ] _commands ;
@@ -346,7 +389,8 @@ private void SetFrame(ExecutionFrame frame)
346389
347390 private void Reset ( )
348391 {
349- _operationStack = new Stack < IValue > ( ) ;
392+ //_operationStack = new Stack<IValue>();
393+ _operationStack = new OpStack ( ) ;
350394 _callStack = new Stack < ExecutionFrame > ( ) ;
351395 _exceptionsStack = new Stack < ExceptionJumpInfo > ( ) ;
352396 _module = null ;
@@ -707,7 +751,7 @@ private void PushNull(int arg)
707751 }
708752
709753 private void PushLoc ( int arg )
710- {
754+ {
711755 _operationStack . Push ( _currentFrame . Locals [ arg ] ) ;
712756 NextInstruction ( ) ;
713757 }
@@ -757,9 +801,13 @@ private void AssignRef(int arg)
757801
758802 private void Add ( int arg )
759803 {
760- var op2 = PopRawValue ( ) ;
761- var op1 = PopRawValue ( ) ;
762- _operationStack . Push ( ValueFactory . Add ( op1 , op2 , _process ) ) ;
804+ //var op2 = PopRawValue();
805+ //var op1 = PopRawValue();
806+ //_operationStack.Push(ValueFactory.Add(op1, op2, _process));
807+ var op2 = RawValue ( _operationStack [ 0 ] ) ;
808+ var op1 = RawValue ( _operationStack [ 1 ] ) ;
809+ _operationStack [ 1 ] = ValueFactory . Add ( op1 , op2 , _process ) ;
810+ _operationStack . Drop ( ) ;
763811 NextInstruction ( ) ;
764812 }
765813
0 commit comments