Skip to content

Commit ec8e7c9

Browse files
committed
added the ability to override the execution end logic (including the part that clears the locals)
1 parent 6f62012 commit ec8e7c9

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Runtime.CompilerServices;
2+
using System.Text;
3+
4+
namespace Starscript;
5+
6+
// ReSharper disable VirtualMemberNeverOverridden.Global
7+
// ReSharper disable once ClassWithVirtualMembersNeverInherited.Global
8+
public partial class StarscriptHypervisor
9+
{
10+
protected virtual StringSegment EndExecution(
11+
ref StringBuilder sb,
12+
ref StringSegment firstSegment,
13+
ref StringSegment segment,
14+
int index)
15+
{
16+
if (!_persistentLocals)
17+
ClearLocals();
18+
19+
if (!Unsafe.IsNullRef(ref firstSegment) && !Unsafe.IsNullRef(ref segment))
20+
{
21+
segment.Next = new StringSegment(index, sb.ToString());
22+
return firstSegment;
23+
}
24+
25+
return new StringSegment(index, sb.ToString());
26+
}
27+
}

src/Lib/Public/Hypervisor/Starscript.HV.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Text;
22
using Starscript.Internal;
3+
using Starscript.Util;
34

45
namespace Starscript;
56

@@ -97,7 +98,7 @@ internal StringSegment RunImpl(ExecutableScript script, StringBuilder sb)
9798

9899
case Instruction.End:
99100
#if DEBUG
100-
DebugLog($"Encountered {Enum.GetName(Instruction.End)} instruction. Breaking execution.");
101+
DebugLog("End of script code reached. Breaking execution.");
101102
#endif
102103

103104
goto EndExecution;
@@ -114,16 +115,7 @@ internal StringSegment RunImpl(ExecutableScript script, StringBuilder sb)
114115

115116
EndExecution:
116117

117-
if (!_persistentLocals)
118-
ClearLocals();
119-
120-
if (firstSegment != null)
121-
{
122-
segment!.Next = new StringSegment(index, sb.ToString());
123-
return firstSegment;
124-
}
125-
126-
return new StringSegment(index, sb.ToString());
118+
return EndExecution(ref sb, ref firstSegment.NullableRef(), ref segment.NullableRef(), index);
127119
}
128120

129121
private static void AppendValue(StringBuilder sb, Value? value) => sb.Append(value ?? Value.Null);

src/Lib/Public/Starscript.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ protected StarscriptHypervisor(ValueMap? globals = null, ValueMap? locals = null
4747
public static StarscriptException Error([StringSyntax("CompositeFormat")] string format, params object?[] args)
4848
=> new(args.Length == 0 ? format : string.Format(format, args));
4949

50-
public StarscriptHypervisor ClearLocals()
50+
public virtual StarscriptHypervisor ClearLocals()
5151
{
5252
Locals = null;
5353
return this;

src/Lib/Public/Util/Extensions.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1-
using System.Text;
1+
using System.Diagnostics.CodeAnalysis;
2+
using System.Runtime.CompilerServices;
3+
using System.Text;
24

35
namespace Starscript.Util;
46

57
public static class Extensions
68
{
9+
public static ref T NullableRef<T>(this T? value) where T : class
10+
{
11+
if (value is null)
12+
return ref Unsafe.NullRef<T>();
13+
14+
return ref Unsafe.AsRef(ref value);
15+
}
16+
17+
public static bool TryGetNullableRef<T>(this T? value, out T result) where T : class
18+
{
19+
result = NullableRef(value);
20+
return !Unsafe.IsNullRef(ref result);
21+
}
22+
723
public static string Pluralize(this string word, int quantity, Plurality? plurality = null,
824
bool prefixQuantity = false)
925
{

0 commit comments

Comments
 (0)