@@ -5,10 +5,18 @@ use std::pin::Pin;
5
5
6
6
pub ( super ) const VM_STACK_SIZE : usize = 8192 ;
7
7
8
+ ///
9
+ /// Ruruby exection stack.
10
+ ///
11
+ /// Ruruby stack is implemented as a fixed and pinned heap array of `Value`s.
12
+ /// You can do operations like push, pop, or extend, as if it was a Vec.
13
+ ///
14
+ /// Stack size is VM_STACK_SIZE(currently, 8192 `Value`s).
8
15
#[ derive( Clone ) ]
9
16
pub ( super ) struct RubyStack {
10
- //len: usize,
17
+ /// Stack pointer.
11
18
pub sp : StackPtr ,
19
+ /// Pinned Buffer.
12
20
buf : Pin < Box < [ Value ] > > ,
13
21
}
14
22
@@ -49,6 +57,7 @@ impl IndexMut<Range<usize>> for RubyStack {
49
57
}
50
58
51
59
impl RubyStack {
60
+ /// Allocate new `RubyStack`.
52
61
pub ( super ) fn new ( ) -> Self {
53
62
let mut inner = unsafe { Box :: new_uninit_slice ( VM_STACK_SIZE ) . assume_init ( ) } ;
54
63
let sp = StackPtr :: from ( inner. as_mut_ptr ( ) ) ;
@@ -58,33 +67,43 @@ impl RubyStack {
58
67
}
59
68
}
60
69
70
+ /// Set SP to `new_len`.
61
71
unsafe fn set_len ( & mut self , new_len : usize ) {
62
72
self . sp = StackPtr :: from ( self . buf . as_mut_ptr ( ) ) + new_len;
63
73
}
64
74
75
+ /// Increment SP.
65
76
unsafe fn inc_len ( & mut self , offset : usize ) {
66
77
self . sp = self . sp + offset;
67
78
}
68
79
80
+ /// Decrement SP.
69
81
unsafe fn dec_len ( & mut self , offset : usize ) {
70
82
self . sp = self . sp - offset;
71
83
}
72
84
85
+ /// Get length of stack.
86
+ /// This is as same as the index of SP in the stack.
73
87
pub ( super ) fn len ( & self ) -> usize {
74
88
let len = unsafe { self . sp . as_ptr ( ) . offset_from ( self . buf . as_ptr ( ) ) } ;
75
89
assert ! ( len >= 0 ) ;
76
90
len as usize
77
91
}
78
92
93
+ /// Shortens the stack.
94
+ /// If len is greater than the current length, this has no effect.
79
95
pub ( super ) fn truncate ( & mut self , len : usize ) {
80
96
if len >= self . len ( ) {
81
97
return ;
82
98
}
83
99
unsafe { self . set_len ( len) } ;
84
100
}
85
101
102
+ /// Resize the stack so that len is equal to new_len.
103
+ /// If new_len is greater than len, the stack is extended by the difference, with each additional slot filled with Nil.
104
+ /// If new_len is less than len, the stack is simply truncated.
86
105
pub ( super ) fn resize ( & mut self , new_len : usize ) {
87
- debug_assert ! ( new_len <= VM_STACK_SIZE ) ;
106
+ assert ! ( new_len <= VM_STACK_SIZE ) ;
88
107
let len = self . len ( ) ;
89
108
if new_len > len {
90
109
self . buf [ len..new_len] . fill ( Value :: nil ( ) ) ;
0 commit comments