Skip to content

Commit 398564d

Browse files
committed
Added the ability to persist locals (false by default)
1 parent 79e0ff5 commit 398564d

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/Lib/Starscript.HV.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ internal StringSegment RunImpl(Script script, StringBuilder sb)
401401

402402
EndExecution:
403403

404-
Locals = null;
404+
if (!_persistentLocals)
405+
Locals = null;
405406

406407
if (firstSegment != null)
407408
{

src/Lib/Starscript.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,31 @@ public partial class StarscriptHypervisor
1414

1515
public ValueMap? Locals { get; private set; }
1616

17+
private readonly bool _persistentLocals;
18+
1719
public static StarscriptHypervisor Create()
1820
=> new();
1921

2022
public static StarscriptHypervisor CreateFromParent(StarscriptHypervisor hv)
2123
=> new(hv.Globals);
2224

23-
public static StarscriptHypervisor CreateWithLocals(ValueMap locals)
24-
=> new(locals: locals);
25+
public static StarscriptHypervisor CreateWithLocals(ValueMap locals, bool persistentLocals = false)
26+
=> new(locals: locals, persistentLocals: persistentLocals);
2527

26-
public static StarscriptHypervisor CreateFromParentWithLocals(StarscriptHypervisor hv, ValueMap locals)
27-
=> new(hv.Globals, locals);
28+
public static StarscriptHypervisor CreateFromParentWithLocals(StarscriptHypervisor hv, ValueMap locals, bool persistentLocals = false)
29+
=> new(hv.Globals, locals, persistentLocals);
2830

29-
public static StarscriptHypervisor CreateWithLocals(IStarscriptObject obj)
30-
=> new(locals: obj.ToStarscript());
31+
public static StarscriptHypervisor CreateWithLocals(IStarscriptObject obj, bool persistentLocals = false)
32+
=> CreateWithLocals(obj.ToStarscript(), persistentLocals);
3133

32-
public static StarscriptHypervisor CreateFromParentWithLocals(StarscriptHypervisor hv, IStarscriptObject obj)
33-
=> new(hv.Globals, obj.ToStarscript());
34+
public static StarscriptHypervisor CreateFromParentWithLocals(StarscriptHypervisor hv, IStarscriptObject obj, bool persistentLocals = false)
35+
=> CreateFromParentWithLocals(hv, obj.ToStarscript(), persistentLocals);
3436

35-
private StarscriptHypervisor(ValueMap? globals = null, ValueMap? locals = null)
37+
private StarscriptHypervisor(ValueMap? globals = null, ValueMap? locals = null, bool persistentLocals = false)
3638
{
3739
Globals = globals?.Copy() ?? new ValueMap();
3840
Locals = locals;
41+
_persistentLocals = persistentLocals;
3942
}
4043

4144
public static StarscriptException Error([StringSyntax("CompositeFormat")] string format, params object?[] args)

0 commit comments

Comments
 (0)