8000 GitHub · Where software is built 8000
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
FeatureRequest: A Top await DoStringAsync() Entrypoint  #541
Open
@personball

Description

@personball

Hi, after I search of 'async await' in issues, with #514 #295 #52

I know how to do a callback when lua invoke a async C# method with ContinueWith.

But, it is really a 'fire and forgot' invocation. The call stack will not back to the top caller.

How can I do a real one await when invoking DoString() ?

Here is codes reproduce it:

        public async Task<(string?, string?)> CreateNextProjectTaskAsync(ProjectTask upstream, string nextTaskType)
        {
            await Task.Delay(1000);
            return (upstream.TaskName, nextTaskType);
        }
        public void CreateNextTask(ProjectTask upstream, string nextTaskType, LuaFunction? callback = null)
        {
            CreateNextProjectTaskAsync(upstream, nextTaskType).ContinueWith(t =>
            {
                var res = t.Result;
                callback?.Call(res.Item1, res.Item2);
            });
        }

        [Fact]
        public async Task HandleTaskFinished()
        {
            var action =
@"
import 'System'
m:CreateNextTask(task,'Test',function(res1,res2)
    print(res1)
    x=res1
    print(res2)
    y=res1
end)
";
            using var lua = new Lua();
            lua.State.Encoding = Encoding.UTF8;
            lua.LoadCLRPackage();
            lua["m"] = this;
            lua["task"] = new ProjectTask(Guid.NewGuid())
            {
                TaskName = "Test"
            };
            var actionResults = lua.DoString(action);
            var x = lua["x"]; //null!! This line run before lua callback!
            //x.ShouldBe("Test");
            var y = lua["y"];
            //y.ShouldBe("Test");

            await Task.Delay(1000);//thread switch to callback invoke

            lua.State.Status.ShouldBe(KeraLua.LuaStatus.OK);

        }

ProjectTask could be any class with a Guid Id and string TaskName.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0