Skip to content

Commit

Permalink
fix issue in scheduler, fix issue in calculation owner parent methods…
Browse files Browse the repository at this point in the history
… in inner class
  • Loading branch information
teo1962 committed Jun 19, 2023
1 parent 4766faa commit 7cf98d4
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Luax.Interpreter.Test/Luax.Interpreter.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<None Remove="TestSources\InnerClass3.luax" />
<None Remove="TestSources\InnerClass4.luax" />
<None Remove="TestSources\InnerClass5.luax" />
<None Remove="TestSources\InnerClass6.luax" />
<None Remove="TestSources\LoggerTest.luax" />
<None Remove="TestSources\LuaXStackTraceFramesTest.luax" />
<None Remove="TestSources\Project1.ini" />
Expand All @@ -39,6 +40,7 @@
<None Remove="TestSources\Project5.ini" />
<None Remove="TestSources\SchedulerTest1.luax" />
<None Remove="TestSources\SchedulerTest2.luax" />
<None Remove="TestSources\SchedulerTest3.luax" />
<None Remove="TestSources\Stdlibtest.luax" />
<None Remove="TestSources\TestSuite.luax" />
<None Remove="TestSources\WhileBreakTest.luax" />
Expand All @@ -58,6 +60,7 @@
<EmbeddedResource Include="TestSources\InnerClass3.luax" />
<EmbeddedResource Include="TestSources\InnerClass4.luax" />
<EmbeddedResource Include="TestSources\InnerClass5.luax" />
<EmbeddedResource Include="TestSources\InnerClass6.luax" />
<EmbeddedResource Include="TestSources\LoggerTest.luax" />
<EmbeddedResource Include="TestSources\LuaXStackTraceFramesTest.luax" />
<EmbeddedResource Include="TestSources\RepeatBreakTest.luax" />
Expand All @@ -67,6 +70,7 @@
<EmbeddedResource Include="TestSources\ForTest.luax" />
<EmbeddedResource Include="TestSources\SchedulerTest1.luax" />
<EmbeddedResource Include="TestSources\SchedulerTest2.luax" />
<EmbeddedResource Include="TestSources\SchedulerTest3.luax" />
<EmbeddedResource Include="TestSources\TryCatchTest.luax" />
<EmbeddedResource Include="TestSources\ThrowTest.luax" />
<EmbeddedResource Include="TestSources\IfTest.luax" />
Expand Down
59 changes: 59 additions & 0 deletions Luax.Interpreter.Test/TestExecution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,24 @@ public void TestInnerClasses5()
r.Should().Be("result");
}

[Fact]
public void TestInnerClasses6()
{
var app = new LuaXApplication();
app.CompileResource("InnerClass6");
app.Pass2();
var typelib = new LuaXTypesLibrary(app);

typelib.SearchClass("program", out var program).Should().BeTrue();
program.SearchMethod("main", null, out var method).Should().BeTrue();
method.Static.Should().BeTrue();
method.ReturnType.IsString().Should().BeTrue();

LuaXMethodExecutor.Execute(method, typelib, null, new object[] { }, out var r);
r.Should().BeOfType<string>();
r.Should().Be("result");
}

[Theory]
[InlineData(0, 2, 1, 3)]
[InlineData(1, 2, 1, 2)]
Expand Down Expand Up @@ -1014,6 +1032,47 @@ public void TestSchedulerWithoutDelayMode()
i.Should().Be(10);
}

[Fact]
public void TestSchedulerStartStopMode()
{
var app = new LuaXApplication();
app.CompileResource("SchedulerTest3");
app.Pass2();
var typelib = new LuaXTypesLibrary(app);
typelib.SearchClass("test", out var program).Should().BeTrue();

program.SearchMethod("startImmediately", null, out var method).Should().BeTrue();
method.Static.Should().BeTrue();
method.Arguments.Should().HaveCount(0);
method.ReturnType.IsObject().Should().BeTrue();
method.ReturnType.Class.Should().Be("testCallback");
LuaXMethodExecutor.Execute(method, typelib, null, Array.Empty<object>(), out var r);
r.Should().BeOfType<LuaXObjectInstance>();
System.Threading.Thread.Sleep(900);
LuaXObjectInstance callback = (LuaXObjectInstance)r;
int i = (int)callback.Properties["i"].Value;
LuaXObjectInstance sched = (LuaXObjectInstance)callback.Properties["sc"].Value;
sched.Class.SearchMethod("stop", null, out Parser.Ast.LuaXMethod stop);
LuaXMethodExecutor.Execute(stop, typelib, sched, Array.Empty<object>(), out var _);
i.Should().BeLessThanOrEqualTo(5);

program.SearchMethod("startWithDelay", null, out method).Should().BeTrue();
method.Static.Should().BeTrue();
method.Arguments.Should().HaveCount(1);
method.ReturnType.IsObject().Should().BeTrue();
method.ReturnType.Class.Should().Be("testCallback");
LuaXMethodExecutor.Execute(method, typelib, null, new object[] { r }, out r);
r.Should().BeOfType<LuaXObjectInstance>();
System.Threading.Thread.Sleep(900);
callback = (LuaXObjectInstance)r;
i = (int)callback.Properties["i"].Value;
sched = (LuaXObjectInstance)callback.Properties["sc"].Value;
sched.Class.SearchMethod("stop", null, out stop);
LuaXMethodExecutor.Execute(stop, typelib, sched, Array.Empty<object>(), out var _);
i.Should().BeGreaterThanOrEqualTo(5);
i.Should().BeLessThanOrEqualTo(9);
}

[Fact]
public void TestSchedulerFailed()
{
Expand Down
39 changes: 39 additions & 0 deletions Luax.Interpreter.Test/TestSources/InnerClass6.luax
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class runner
private var result : string;
class A
public function run() : void
var classB : B;
classB = new B();
classB.run();
end
end
class B
public function run() : void
ownerResult();
end
end

private function ownerResult() : void
result = "result";
end

public function run() : string
var classA : A;
classA = new A();
classA.run();
return result;
end
end
class runnerBuilder : runner
public function runnerBuilder(): void
end
public function build(): runner
return this;
end
end

class program
public static function main() : string
return (new runnerBuilder()).build().run();
end
end
32 changes: 32 additions & 0 deletions Luax.Interpreter.Test/TestSources/SchedulerTest3.luax
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class testCallback : action
public var sc : scheduler;
public var i : int;
public function testCallback() : void
i = 0;
end
public function invoke() : void
sc.stop();
i = i + 1;
sc.startWithDelay();
end
public function setSched(sc: scheduler) : void
this.sc = sc;
end
end

class test
public static function startImmediately() : testCallback
var callback : testCallback;
var sc : scheduler;

callback = new testCallback();
sc = scheduler.create(200, callback);
callback.setSched(sc);
sc.startImmediately();
return callback;
end
public static function startWithDelay(callback : testCallback) : testCallback
callback.sc.startWithDelay();
return callback;
end
end
15 changes: 14 additions & 1 deletion Luax.Interpreter/Expression/LuaXMethodExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,20 @@ public static ResultType Execute(LuaXMethod method, LuaXTypesLibrary types, LuaX
LuaXObjectInstance owner = @this.OwnerObjectInstance;
while (owner != null)
{
if (owner.Class.LuaType.Name == currentClass.LuaType.Name)
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;
}
Expand Down
2 changes: 1 addition & 1 deletion Luax.Interpreter/Infrastructure/Stdlib/StdlibScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public static object StartWithDelay(LuaXObjectInstance @this)
throw new ArgumentException("The scheduler isn't properly initialized", nameof(@this));
callback.Class.SearchMethod("invoke", null, out LuaXMethod invoke);

@this.Properties["__needStop"].Value = false;
if (periodInMilliseconds >= 0)
{
tokenSource = new CancellationTokenSource();
Expand All @@ -94,6 +93,7 @@ public static object StartWithDelay(LuaXObjectInstance @this)
}
else
{
@this.Properties["__needStop"].Value = false;
while (!(bool)@this.Properties["__needStop"].Value)
LuaXMethodExecutor.Execute(invoke, mTypeLibrary, callback, Array.Empty<object>(), out var _);
}
Expand Down

0 comments on commit 7cf98d4

Please sign in to comment.