Skip to content

Commit 719a3ed

Browse files
committed
Prevent creating variables with keywords as names.
From MeteorDevelopment/starscript#27
1 parent 1a48126 commit 719a3ed

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/Lib/Public/Values/ValueMap.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using System.Collections;
2+
using System.Collections.Immutable;
23
using System.Diagnostics.CodeAnalysis;
34

45
namespace Starscript;
56

67
public class ValueMap : IReadOnlyDictionary<string, Func<Value>>
78
{
9+
public static readonly ImmutableArray<string> Keywords = ["null", "true", "false", "and", "or"];
10+
811
private readonly Dictionary<string, Func<Value>> _entries;
912

1013
/// <summary>
@@ -22,6 +25,12 @@ public ValueMap(Dictionary<string, Func<Value>>? entries = null)
2225
/// </summary>
2326
public ValueMap SetRaw(string name, Func<Value> value)
2427
{
28+
if (Keywords.Contains(name))
29+
throw new StarscriptException($"The name of a variable cannot be a keyword. " +
30+
$"Disallowed words: [{
31+
string.Join(", ", Keywords.Select(x => $"\"{x}\""))
32+
}]");
33+
2534
_entries[name] = value;
2635
return this;
2736
}

src/TestProgram/Program.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ internal static void Main()
6060
// StarscriptHypervisor should error here, as the source string calls a function that is defined as a local, so it is cleared when the script execution ends.
6161
// NotImplementedException is not caught as that is an error case, that being StarscriptHypervisor *not* erroring.
6262
}
63+
64+
try
65+
{
66+
hypervisor.Set("true", Value.Null);
67+
68+
throw new NotImplementedException();
69+
}
70+
catch (StarscriptException)
71+
{
72+
Log("Successfully errored when trying to set a variable name to a keyword.");
73+
}
6374

6475
script.Dispose();
6576
#endif

0 commit comments

Comments
 (0)