sebastienros / jint

Javascript Interpreter for .NET

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Task<JsValue> is not correctly marshalled into JS code

harrison314 opened this issue · comments

Version used
3.1.2

Describe the bug

When exporting an object to a module, I would expect the return type of Task<JsValue> to be usable by await in JS code just like Task<string> for example. Instead, I get a Task object in JavaScript and not a value in JsValue.

To Reproduce

internal class HttpFunctions
{
    private readonly Engine engine;

    public HttpFunctions(Engine engine)
    {
        this.engine = engine;
    }
   
    public  async Task<JsValue> GetJsonAsync(string url, IDictionary<string, object>? headers)
    {
        await Task.Delay(1500); //Simulate HTTP call
        string content = """
            {"name": "test value", "value": 12.4}
            """;
        return new JsonParser(this.engine).Parse(content);
    }
}
using Engine engine = new Engine(options =>
{
    options.TimeoutInterval(TimeSpan.FromSeconds(60));
});

engine.SetValue("__log", new Action<object?>(val =>
{
    string value = val?.ToString() ?? "<NULL>";
    System.Diagnostics.Debug.WriteLine(value);
}));

HttpFunctions httpFunctions = new HttpFunctions(engine);

engine.Modules.Add("dbApi", bulder =>
{
    bulder.ExportObject("http", httpFunctions);
});


string code = """
    import { http } from 'dbApi';

    let iot = await http.GetJson("https://anyJsonEndpoint.com/exmaple.json");
    __log(iot.value);
    """;

engine.Modules.Add("_main", code);
engine.Modules.Import("_main");

Expected behavior

I would expect that after I call await in the JS code on the value of Task then the variable will have the value in JsValue.

Additional context

I am trying to implement a function to download JSON using HttpClient.

See https://github.com/sebastienros/jint/blob/main/Jint.Tests/Runtime/AsyncTests.cs and options.ExperimentalFeatures = ExperimentalFeature.TaskInterop

Thanks for the guidance.