Skip to content

Commit 1a48126

Browse files
committed
If the value stack is empty, return null value instead of trying to index into an empty collection.
From: MeteorDevelopment/starscript#34
1 parent f2b8f3b commit 1a48126

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/Lib/Public/Hypervisor/Abstraction/AbstractHypervisor.Stack.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,15 @@ public partial class AbstractHypervisor<TSelf>
1919

2020
protected void ClearStack() => _stack.Clear();
2121

22-
public Value Peek() => _stack.Peek();
23-
public Value Peek(int offset) => _stack.Peek(offset);
22+
public Value Peek()
23+
=> _stack.Count is 0
24+
? Value.Null
25+
: _stack.Peek();
26+
27+
public Value Peek(int offset)
28+
=> _stack.Count <= offset
29+
? Value.Null
30+
: _stack.Peek(offset);
2431

2532
public bool PopBoolean(string error)
2633
{

src/Lib/Public/Util/TransparentStack.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ public T Pop()
3737
public T Peek() => _buffer[_buffer.CurrentSize - 1];
3838

3939
public T Peek(int offset) => _buffer[_buffer.CurrentSize - 1 - offset];
40+
41+
public int Count => _buffer.CurrentSize;
4042
}

0 commit comments

Comments
 (0)