Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use spread operator on .net instances #748

Closed
faramos opened this issue Jun 18, 2020 · 1 comment · Fixed by #749
Closed

How to use spread operator on .net instances #748

faramos opened this issue Jun 18, 2020 · 1 comment · Fixed by #749

Comments

@faramos
Copy link

faramos commented Jun 18, 2020

Testing Jing (3.0.0-beta-1828) I can´t get a spread operator to work on .NET instances.

Working ok:

 private static void TestJintExpressionPureJs()
 {
        var engine = new Engine();
        var result = engine
            .Execute("({supplier: 'S1', ...{number: 42}})")
            .GetCompletionValue()
            .ToObject() as IDictionary<string, object>;
        Console.WriteLine(result["supplier"]); //"S1"
        Console.WriteLine(result["number"]); //"42"
    }

Not working as expected:

    private static void TestJintExpression()
    {
        var engine = new Engine();
        IDictionary<string, object> state = new Dictionary<string, object>
        {
            {"invoice", new Dictionary<string, object> {["number"] = "42"}}
        };
        engine.SetValue("state", state);
        var result = engine
            .Execute("({supplier: 'S1', ...state.invoice})")
            .GetCompletionValue()
            .ToObject() as IDictionary<string, object>;
        Console.WriteLine(result["supplier"]); //"S1"
        Console.WriteLine(result["number"]); //exception key not found
    }

Neither:

    private static void TestJintFunc()
    {
        var engine = new Engine();
        IDictionary<string, object> state = new Dictionary<string, object>
        {
            {"invoice", new Dictionary<string, object> {["number"] = "42"}}
        };
        engine.SetValue("state", state);
        var result = engine
            .Execute("function getValue() { return {supplier: 'S1', ...state.invoice}; }")
            .Invoke("getValue")
            .ToObject() as IDictionary<string, object>;
        Console.WriteLine(result["supplier"]); //"S1"
        Console.WriteLine(result["number"]); //exception key not found
    }

I already tried to export an ExpandoObject with the same result.
Is this feature supported?

Thanks in advance

Really great work!

@lahma
Copy link
Collaborator

lahma commented Jun 19, 2020

Thanks for reporting. Wrapped CLR objects currently don't expose an iterator which is needed for spread and also some other functionality. I've created a PR to address this and your test cases now pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants