Skip to content

Commit f2b8f3b

Browse files
committed
Added a special case for attempting to execute an ExecutableScript with an empty bytecode buffer
1 parent 6c674a2 commit f2b8f3b

File tree

5 files changed

+13
-2
lines changed

5 files changed

+13
-2
lines changed

src/Lib/Internal/ExecutableScript.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public abstract class ExecutableScript : IDisposable
99
public abstract ReadOnlySpan<Value> Constants { get; }
1010

1111
public abstract void Dispose();
12+
13+
public abstract long CodeSize { get; }
1214

1315
protected virtual ref readonly byte GetByteAt(int idx) => ref Code[idx];
1416

src/Lib/Internal/MutableScript.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public class MutableScript : ExecutableScript
1111

1212
public override ReadOnlySpan<Value> Constants => CollectionsMarshal.AsSpan(_constants);
1313

14+
public override long CodeSize => CodeBuffer.CurrentSize;
15+
1416
public ResizableBuffer<byte> CodeBuffer { get; } = new();
1517

1618
public override ReadOnlySpan<byte> Code => CodeBuffer.RoSpan;

src/Lib/Public/Entities/Script.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public class Script : ExecutableScript
1919
?? throw new ObjectDisposedException(nameof(Script),
2020
"Cannot access constants of a disposed Script.");
2121

22+
public override long CodeSize => _code?.LongLength
23+
?? throw new ObjectDisposedException(nameof(Script),
24+
"Cannot access bytecode size of a disposed Script.");
25+
2226
public Script(byte[] codeBuffer, Value[] constants)
2327
{
2428
_code = codeBuffer ?? throw new NullReferenceException($"Cannot initialize a {nameof(Script)} with a null bytecode buffer.");

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ protected virtual StringSegment RunInternal(ExecutableScript script, StringBuild
1111
if (script.IsDisposed)
1212
throw new ObjectDisposedException(script.GetType().FullName, "Cannot execute a disposed Script.");
1313

14+
if (script.CodeSize is 0)
15+
throw new StarscriptException($"Attempted to execute a {script.GetType().Name} with no bytecode.");
16+
1417
ClearStack();
1518

1619
sb.Length = 0;

src/Lib/Public/Util/ResizableBuffer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ public ResizableBuffer(int initialCapacity = 8)
3838
/// <summary>
3939
/// The amount of elements in the underlying buffer.
4040
/// </summary>
41-
public int BufferSize => _buffer.Length;
41+
public long BufferSize => _buffer.LongLength;
4242

4343
/// <summary>
4444
/// The amount of bytes currently consumed by this buffer.
4545
/// </summary>
46-
public long BufferByteSize => _buffer.Length * Unsafe.SizeOf<T>();
46+
public long BufferByteSize => BufferSize * Unsafe.SizeOf<T>();
4747

4848
/// <summary>
4949
/// Write a value directly to the buffer. If the buffer is too small, it is resized by 50% (Length * 1.5).

0 commit comments

Comments
 (0)