Skip to content

Commit 132e9ec

Browse files
committed
Update script data type implementations
1 parent b6799bb commit 132e9ec

23 files changed

+95
-53
lines changed

JSchema/RelogicLabs/JSchema/Nodes/JArray.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public override bool Match(JNode node)
4545
public override IList<JNode> Components => Elements;
4646
public IReadOnlyList<IEValue> Values => (IReadOnlyList<IEValue>) Elements;
4747
public IEValue Get(int index) => Elements[index];
48+
public void Set(int index, IEValue value)
49+
=> throw new UpdateNotSupportedException(AUPD01, "Readonly array cannot be updated");
4850
private static bool IsOptional(JNode node) => node is JValidator { Optional: true };
4951
public override string ToString() => Elements.JoinWith(", ", "[", "]");
5052

JSchema/RelogicLabs/JSchema/Nodes/JBoolean.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public override bool Equals(object? obj)
3535
}
3636

3737
public override int GetHashCode() => Value.GetHashCode();
38-
public static implicit operator bool(JBoolean @bool) => @bool.Value;
38+
public static implicit operator bool(JBoolean node) => node.Value;
3939
public override string ToString() => Value.ToString().ToLower();
4040

4141
internal new sealed class Builder : JPrimitive.Builder<bool>

JSchema/RelogicLabs/JSchema/Nodes/JObject.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ private bool OrderedEquals(IList<JProperty> properties)
116116
return property?.Value;
117117
}
118118

119+
public void Set(string key, IEValue value)
120+
=> throw new UpdateNotSupportedException(OUPD01, "Readonly object cannot be updated");
119121
public override int GetHashCode() => Properties.GetHashCode();
120122
public override string ToString() => Properties.JoinWith(", ", "{", "}");
121123

JSchema/RelogicLabs/JSchema/Nodes/JReceiver.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public IList<T> GetValueNodes<T>() where T : JNode
4141
=> FetchValueNodes().Cast<T>().ToList().AsReadOnly();
4242

4343
public IEValue Get(int index) => FetchValueNodes()[index];
44+
public void Set(int index, IEValue value)
45+
=> throw new UpdateNotSupportedException(AUPD02, "Readonly array cannot be updated");
4446

4547
public override bool Equals(object? obj)
4648
{

JSchema/RelogicLabs/JSchema/Nodes/JString.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public override bool Match(JNode node)
2626
if(other == null) return false;
2727
if(Value.Equals(other.Value)) return true;
2828
return Fail(new JsonSchemaException(
29-
new ErrorDetail(STRN01, ValueMismatch),
29+
new ErrorDetail(STRV01, ValueMismatch),
3030
ExpectedDetail.AsValueMismatch(this),
3131
ActualDetail.AsValueMismatch(other)));
3232
}

JSchema/RelogicLabs/JSchema/Script/GArray.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using RelogicLabs.JSchema.Types;
33
using RelogicLabs.JSchema.Utilities;
44
using static RelogicLabs.JSchema.Message.ErrorCode;
5-
using static RelogicLabs.JSchema.Types.IEValue;
65

76
namespace RelogicLabs.JSchema.Script;
87

@@ -31,16 +30,23 @@ public static GArray FilledFrom(IEValue value, int count)
3130
return array;
3231
}
3332

34-
public IEValue Get(int index)
33+
public IEValue Get(int index) => Elements[index];
34+
35+
public void Set(int index, IEValue value)
3536
{
3637
var count = Elements.Count;
37-
if(index < count) return Elements[index];
38-
if(index > MaxLimit) throw new ScriptCommonException(INDX01,
38+
if(index < count)
39+
{
40+
if(Elements[index] is not GReference r) throw new UpdateNotSupportedException(AUPD03,
41+
$"Readonly array index {index} cannot be updated");
42+
r.Value = value;
43+
return;
44+
}
45+
if(index > count) throw new ScriptCommonException(AWRT01,
46+
$"Index {index} is out of bounds for writing to array length {count}");
47+
if(index > MaxLimit) throw new ScriptCommonException(AWRT02,
3948
$"Array index {index} exceeds maximum size limit");
40-
if(index > count) throw new IndexOutOfRangeException($"Array index out of range: {index}");
41-
var reference = new GReference(VOID);
42-
Elements.Add(reference);
43-
return reference;
49+
Elements.Add(new GReference(value));
4450
}
4551

4652
public override string ToString() => Elements.JoinWith(", ", "[", "]");

JSchema/RelogicLabs/JSchema/Script/GBoolean.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ internal sealed class GBoolean : IEBoolean
66
{
77
public static readonly GBoolean TRUE = new(true);
88
public static readonly GBoolean FALSE = new(false);
9-
109
public bool Value { get; }
1110

1211
private GBoolean(bool value) => Value = value;
13-
public static GBoolean Of(bool value) => value? TRUE : FALSE;
1412
public override string ToString() => Value.ToString().ToLower();
1513
}

JSchema/RelogicLabs/JSchema/Script/GDouble.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ internal sealed class GDouble : IEDouble
66
{
77
public double Value { get; }
88
private GDouble(double value) => Value = value;
9-
public static GDouble Of(double value) => new(value);
9+
public static GDouble From(double value) => new(value);
1010

1111
public override string ToString() => Value.ToString();
1212
}

JSchema/RelogicLabs/JSchema/Script/GFunction.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ namespace RelogicLabs.JSchema.Script;
1111

1212
internal sealed class GFunction : IRFunction
1313
{
14-
public const string ConstraintPrefix = "@";
1514
public const int ConstraintMode = 1;
1615
public const int FutureMode = 3;
1716
public const int SubroutineMode = 4;
@@ -32,20 +31,20 @@ public GFunction(GParameter[] parameters, Evaluator body, int mode)
3231
Mode = mode;
3332
}
3433

35-
public ScopeContext Bind(ScopeContext parentScope, List<IEValue> arguments)
34+
public ScriptScope Bind(ScriptScope parentScope, List<IEValue> arguments)
3635
{
37-
AreCompatible(this, arguments, FUNS05);
38-
var scope = new ScopeContext(parentScope);
36+
AreCompatible(this, arguments, FNVK02);
37+
var scope = new ScriptScope(parentScope);
3938
var i = 0;
4039
foreach(var p in Parameters) scope.AddVariable(p.Name, p.Variadic
4140
? new GArray(arguments.GetRange(i)) : arguments[i++]);
4241
return scope;
4342
}
4443

45-
public IEValue Invoke(ScopeContext functionScope, List<IEValue> arguments)
44+
public IEValue Invoke(ScriptScope functionScope, List<IEValue> arguments)
4645
=> Invoke(functionScope);
4746

48-
public IEValue Invoke(ScopeContext functionScope)
47+
public IEValue Invoke(ScriptScope functionScope)
4948
{
5049
var result = Body(functionScope);
5150
if(result is GControl ctrl) return ctrl.Value;

JSchema/RelogicLabs/JSchema/Script/GInteger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal sealed class GInteger : IEInteger
1111

1212
private GInteger(long value) => Value = value;
1313

14-
public static GInteger Of(long value)
14+
public static GInteger From(long value)
1515
=> value is >= CacheStart and <= CacheEnd
1616
? Cache[(int) value - CacheStart] : new GInteger(value);
1717

0 commit comments

Comments
 (0)