Skip to content

Commit f0a40e1

Browse files
committed
ref readonly modifiers for GetByteAt & indexer, add IsDisposed check to GetByteAt for MutableScript
1 parent ffc7a86 commit f0a40e1

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/Lib/Internal/ExecutableScript.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ public abstract class ExecutableScript : IDisposable
88

99
public abstract void Dispose();
1010

11-
protected virtual byte GetByteAt(int idx) => Code[idx];
12-
13-
public byte this[int idx] => GetByteAt(idx);
11+
protected virtual ref readonly byte GetByteAt(int idx) => ref Code[idx];
1412

13+
public ref readonly byte this[int idx] => ref GetByteAt(idx);
1514

1615
public bool IsDisposed { get; protected set; }
1716

src/Lib/Internal/MutableScript.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ public class MutableScript : ExecutableScript
1515

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

18+
protected override ref readonly byte GetByteAt(int idx)
19+
{
20+
if (IsDisposed)
21+
throw new ObjectDisposedException(nameof(MutableScript), "Cannot access bytecode of a disposed Script.");
22+
23+
return ref CodeBuffer[idx];
24+
}
25+
1826
public Script MoveToImmutable()
1927
{
2028
// Trim the excess (null) bytes before copying
@@ -112,7 +120,7 @@ public void PatchJump(int offset)
112120
}
113121

114122
#endregion
115-
123+
116124
public override void Dispose()
117125
{
118126
if (IsDisposed)

src/Lib/Public/Entities/Script.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ public Script(byte[] codeBuffer, Value[] constants)
1717
_constants = constants;
1818
}
1919

20-
protected override byte GetByteAt(int idx)
20+
protected override ref readonly byte GetByteAt(int idx)
2121
{
2222
if (IsDisposed)
2323
throw new ObjectDisposedException(nameof(Script), "Cannot access bytecode of a disposed Script.");
2424

25-
return _code[idx];
25+
return ref _code[idx];
2626
}
2727

2828
public override void Dispose()

0 commit comments

Comments
 (0)