Skip to content

Commit

Permalink
Add new features from development 5.0 (#54)
Browse files Browse the repository at this point in the history
* add new features implemented in the developer 5.0 branch, add checking static methods overriding

* changed test

* version

* fix code smells

* fix code smells
  • Loading branch information
teo1962 authored Aug 14, 2023
1 parent 7cf98d4 commit 4b11b73
Show file tree
Hide file tree
Showing 21 changed files with 546 additions and 51 deletions.
2 changes: 2 additions & 0 deletions LuaX.Test/Luax.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<None Remove="stdlib\datetimeTest.luax" />
<None Remove="stdlib\intMapTest.luax" />
<None Remove="stdlib\iotest.luax" />
<None Remove="stdlib\jsonTest.luax" />
<None Remove="stdlib\listTest.luax" />
<None Remove="stdlib\mathTest.luax" />
<None Remove="stdlib\queueTest.luax" />
Expand Down Expand Up @@ -99,6 +100,7 @@
<EmbeddedResource Include="stdlib\iotest.luax">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="stdlib\jsonTest.luax" />
<EmbeddedResource Include="stdlib\listTest.luax">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
Expand Down
1 change: 1 addition & 0 deletions LuaX.Test/project.ini
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ stdlib/intMapTest.luax
stdlib/stringMapTest.luax
stdlib/stringTest.luax
language/arrayWithInitializationTest.luax
stdlib/jsonTest.luax
16 changes: 16 additions & 0 deletions LuaX.Test/stdlib/datetimeTest.luax
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ class datetimeTest
assert.isTrue(stdlib.year(d) == 2020, "yr");
end

@Fact()
public function toutc() : void
var d : datetime;
var utcDate : datetime;
d = stdlib.mkdatetime(2020, 5, 22, 11, 46, 55, 123);
utcDate = stdlib.toutc(d);

assert.isTrue(stdlib.millisecond(utcDate) == 123, "ms");
assert.isTrue(stdlib.second(utcDate) == 55, "s");
assert.isTrue(stdlib.minute(utcDate) == 46, "m");
assert.isTrue(stdlib.hour(utcDate) == 11, "h");
assert.isTrue(stdlib.day(utcDate) == 22, "d");
assert.isTrue(stdlib.month(utcDate) == 5, "mo");
assert.isTrue(stdlib.year(utcDate) == 2020, "yr");
end

@Fact()
public function toJdnAndBack() : void
var d : datetime, r : real;
Expand Down
148 changes: 148 additions & 0 deletions LuaX.Test/stdlib/jsonTest.luax
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
@TestSuite()
class jsonTest
const jsonSource1 = "[
{
\"minimumVersion\": \"1.0\",
\"clientId\": \"37i1qd0ifrwjqje71u46z3k4gdy5b\",
\"supportedConnectionTypes\": [
\"websocket\", null
],
\"advice\": {
\"interval\": 1.2,
\"timeout\": 30000,
\"reconnect\": \"retry\"
},
\"channel\": \"/meta/handshake\",
\"version\": \"1.0\",
\"successful\": true
}
]";
const jsonSource2 = "{
\"minimumVersion\": \"1.0\",
\"clientId\": \"37i1qd0ifrwjqje71u46z3k4gdy5b\",
\"supportedConnectionTypes\": [
\"websocket\"
],
\"advice\": {
\"interval\": 1.2,
\"timeout\": 30000,
\"nil\": null,
\"reconnect\": \"retry\"
},
\"channel\": \"/meta/handshake\",
\"version\": \"1.0\",
\"successful\": true
}";
const jsonSource3 = "{
\"datetime\": 1665119461000
}";

class parseAction : action
public function Invoke() : void
var parser : jsonParser;
parser = new jsonParser();
parser.parse(jsonSource1);
end
end

@Fact()
public function parse() : void
assert.doesNotThrow(new parseAction(), "");
end

@Fact()
public function timeIntervalSince1970() : void
var parser : jsonParser, node : jsonNode, root : jsonNode;
var d : datetime;
var utcDate : datetime;

parser = new jsonParser();
root = parser.parse(jsonSource3);

assert.equals(root.getType(), jsonNode.OBJECT, "object");
node = root.getPropertyByName("datetime");
assert.equals(node.getType(), jsonNode.INT, "int");

d = node.getValueAsDatetime();
utcDate = stdlib.toutc(d);

assert.isTrue(stdlib.millisecond(utcDate) == 0, "ms");
assert.isTrue(stdlib.second(utcDate) == 01, "s");
assert.isTrue(stdlib.minute(utcDate) == 11, "m");
assert.isTrue(stdlib.hour(utcDate) == 5, "h");
assert.isTrue(stdlib.day(utcDate) == 7, "d");
assert.isTrue(stdlib.month(utcDate) == 10, "mo");
assert.isTrue(stdlib.year(utcDate) == 2022, "yr");
end

@Fact()
public function typesOnObject() : void
var parser : jsonParser, root : jsonNode;
parser = new jsonParser();
root = parser.parse(jsonSource2);

assert.equals(root.getType(), jsonNode.OBJECT, "object");
assert.equals(root.getChildrenCount(), 7, "getChildrenCount");
end

@Fact()
public function types() : void
var parser : jsonParser, node : jsonNode, root : jsonNode, child : jsonNode;
parser = new jsonParser();
root = parser.parse(jsonSource1);

assert.equals(root.getType(), jsonNode.ARRAY, "array");
assert.equals(root.getChildrenCount(), 1, "getChildrenCount 1");

node = root.getChildByIndex(0);
assert.doesNotEqual(node, nil, "array element");
assert.equals(node.getType(), jsonNode.OBJECT, "object");

assert.equals(node.getChildrenCount(), 7, "getChildrenCount 2");

child = node.getPropertyByName("successful");
assert.equals(child.getType(), jsonNode.BOOLEAN, "boolean 1");
child = node.getChildByIndex(6);
assert.equals(child.getType(), jsonNode.PROPERTY, "property");
assert.equals(child.getName(), "successful", "property name");
child = child.getValueAsNode();
assert.equals(child.getType(), jsonNode.BOOLEAN, "boolean 2");

child = node.getPropertyByName("supportedConnectionTypes");
assert.equals(child.getType(), jsonNode.ARRAY, "array");
assert.equals(child.getChildrenCount(), 2, "getChildrenCount 3");
assert.equals(child.getChildByIndex(0).getType(), jsonNode.STRING, "string");
assert.equals(child.getChildByIndex(1).getType(), jsonNode.NIL, "nil");

child = node.getPropertyByName("advice");
assert.equals(child.getType(), jsonNode.OBJECT, "object");

assert.equals(child.getPropertyByName("timeout").getType(), jsonNode.INT, "int");
assert.equals(child.getPropertyByName("reconnect").getType(), jsonNode.STRING, "string");
assert.equals(child.getPropertyByName("interval").getType(), jsonNode.REAL, "real");
assert.equals(child.getPropertyByName("nil").getType(), jsonNode.NIL, "nil");
end

@Fact()
public function values() : void
var parser : jsonParser, node : jsonNode, root : jsonNode, child : jsonNode;
parser = new jsonParser();
root = parser.parse(jsonSource1);
node = root.getChildByIndex(0);

child = node.getPropertyByName("successful");
assert.equals(child.getValueAsBoolean(), true, "true");
child = node.getChildByIndex(6);
child = child.getValueAsNode();
assert.equals(child.getValueAsBoolean(), true, "true");

child = node.getPropertyByName("supportedConnectionTypes");
assert.equals(child.getChildByIndex(0).getValueAsString(), "websocket", "string");

child = node.getPropertyByName("advice");
assert.equals(child.getPropertyByName("timeout").getValueAsInt(), 30000, "int");
assert.equals(child.getPropertyByName("reconnect").getValueAsString(), "retry", "string");
assert.equals(child.getPropertyByName("interval").getValueAsReal(), 1.2, "real");
end

end
3 changes: 3 additions & 0 deletions LuaX.Test/typeslib/variantTest.luax
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class variantTest
assert.isTrue(x.type() == "real", "type");
assert.isTrue(x.asReal() == 1.5, "value");
assert.isTrue(cast<real>(x) == 1.5, "back cast");

x = "";
assert.isTrue(x.asReal() == 0.0, "empty value");
end

@Fact()
Expand Down
4 changes: 2 additions & 2 deletions Luax.Interpreter.Test/TestSources/ArrayWithInitTest.luax
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
end
end
class myActionAnother : myAction
public static function create(param : string) : myActionAnother
public static function createAnother(param : string) : myActionAnother
var instance : myActionAnother;
instance = new myActionAnother();
instance.property = param.."!";
Expand Down Expand Up @@ -48,7 +48,7 @@
arr = new myAction[]{myAction.create(2 + 1), myAction.create(" is"),
myAction.create(" a"), myAction.create(" summ"),
myAction.create(" of "), myAction.create(1),
myAction.create(" and "), myActionAnother.create(2)};
myAction.create(" and "), myActionAnother.createAnother(2)};
while i < arr.length do
result = result .. arr[i].invoke();
i = i + 1;
Expand Down
48 changes: 26 additions & 22 deletions Luax.Interpreter/Expression/LuaXMethodExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,7 @@ public static ResultType Execute(LuaXMethod method, LuaXTypesLibrary types, LuaX

if (!method.Static && @this.Class.LuaType.Name != currentClass.LuaType.Name && @this.Class.LuaType.Name.StartsWith($"{currentClass.LuaType.Name}."))
{
LuaXObjectInstance owner = @this.OwnerObjectInstance;
while (owner != null)
{
LuaXClass parent = owner.Class.LuaType;
bool toBreak = false;
while (parent != null)
{
if (parent.Name == currentClass.LuaType.Name)
{
toBreak = true;
break;
}
if (!parent.HasParent)
break;
parent = parent.ParentClass;
}
if (toBreak)
break;
owner = owner.OwnerObjectInstance;
}

@this = owner ?? throw new LuaXExecutionException(method.Location, $"Owner instance {currentClass.LuaType.Name} is not found");
@this = findMethodOwner(method, @this.OwnerObjectInstance, currentClass.LuaType.Name);
}

if (method.Extern)
Expand All @@ -83,6 +62,31 @@ public static ResultType Execute(LuaXMethod method, LuaXTypesLibrary types, LuaX
return rt;
}

private static LuaXObjectInstance findMethodOwner(LuaXMethod method, LuaXObjectInstance owner, string className)
{
while (owner != null)
{
LuaXClass parent = owner.Class.LuaType;
bool toBreak = false;
while (parent != null)
{
if (parent.Name == className)
{
toBreak = true;
break;
}
if (!parent.HasParent)
break;
parent = parent.ParentClass;
}
if (toBreak)
break;
owner = owner.OwnerObjectInstance;
}

return owner ?? throw new LuaXExecutionException(method.Location, $"Owner instance {className} is not found");
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static LuaXVariableInstanceSet CreateVariablesForMethod(LuaXMethod method, LuaXObjectInstance @this, object[] args)
{
Expand Down
33 changes: 20 additions & 13 deletions Luax.Interpreter/Infrastructure/LuaXTypesLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public LuaXTypesLibrary(LuaXApplication application)
ExternMethods.Add(this, typeof(StdlibBitwise));
ExternMethods.Add(this, typeof(StdlibCsvParser));
ExternMethods.Add(this, typeof(StdlibXml));
ExternMethods.Add(this, typeof(StdlibJson));
ExternMethods.Add(this, typeof(StdlibSortedList));
ExternMethods.Add(this, typeof(StdlibIntMap));
ExternMethods.Add(this, typeof(StdlibStringMap));
Expand Down Expand Up @@ -81,7 +82,10 @@ private static bool CastInteger(ref object argument)
case string s1 when int.TryParse(s1, NumberStyles.Integer, CultureInfo.InvariantCulture, out int i1):
argument = i1;
return true;
case string :
case string s1 when s1 != null && s1.Length == 0:
argument = 0;
return true;
case string:
return false;
default:
return false;
Expand All @@ -107,7 +111,10 @@ private static bool CastReal(ref object argument)
case string s2 when double.TryParse(s2, NumberStyles.Float, CultureInfo.InvariantCulture, out double i1):
argument = i1;
return true;
case string :
case string s1 when s1 != null && s1.Length == 0:
argument = 0.0;
return true;
case string:
return false;
default:
return false;
Expand Down Expand Up @@ -143,7 +150,7 @@ private static bool CastDateTime(ref object argument)
DateTimeStyles.None, out var d4):
argument = d4;
return true;
case string :
case string:
return false;
default:
return false;
Expand All @@ -167,15 +174,15 @@ private static bool CastString(ref object argument)
argument = b ? "true" : "false";
return true;
case DateTime d:
{
if (d.Hour == 0 && d.Minute == 0 && d.Second == 0 && d.Millisecond == 0)
argument = d.ToString("yyyy-MM-dd");
else if (d.Millisecond == 0)
argument = d.ToString("yyyy-MM-dd HH:mm:ss");
else
argument = d.ToString("yyyy-MM-dd HH:mm:ss.fff");
return true;
}
{
if (d.Hour == 0 && d.Minute == 0 && d.Second == 0 && d.Millisecond == 0)
argument = d.ToString("yyyy-MM-dd");
else if (d.Millisecond == 0)
argument = d.ToString("yyyy-MM-dd HH:mm:ss");
else
argument = d.ToString("yyyy-MM-dd HH:mm:ss.fff");
return true;
}
default:
return argument is string;
}
Expand All @@ -194,7 +201,7 @@ private static bool CastBoolean(ref object argument)
case string s when s == "false":
argument = false;
return true;
case string :
case string:
return false;
default:
return false;
Expand Down
2 changes: 1 addition & 1 deletion Luax.Interpreter/Infrastructure/Stdlib/StdlibBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ public static object AES128(LuaXObjectInstance data, LuaXObjectInstance key, boo
if (key.Properties["__array"]?.Value is not byte[] keyBuffer)
throw new ArgumentException("The object isn't properly initialized", nameof(data));

System.Security.Cryptography.Aes aes = new System.Security.Cryptography.AesManaged();
System.Security.Cryptography.Aes aes = System.Security.Cryptography.Aes.Create();
aes.IV = new byte[16];
aes.Key = keyBuffer;
aes.Mode = System.Security.Cryptography.CipherMode.CBC;
Expand Down
Loading

0 comments on commit 4b11b73

Please sign in to comment.