4

I'm currently using Jint (https://github.com/sebastienros/jint) to process JavaScript.

I would like to be able to use a custom function in JavaScript that will execute a function created in C# and then return a value to the JavaScript.

For example if the JavaScript was:

var x = MultiplyByTwo(100);

And in my C# I had:

private static int MultiplyByTwo(int obj)
{
    return obj * 2;
}

Then the above would give the variable x the value of 200.

According to the Documentation there is the following option:

var engine = new Engine()
.SetValue("log", new Action<object>(Console.WriteLine));

engine.Execute(@"
    function hello() { 
        log('Hello World');
    };
 
    hello();
");

I can replace the "Console.Writeline" with a function name but it will only accept a void function. In other words, this can call a C# function from JS but not return a value.

Does an option like what I am looking for exist in Jint?

Liam
  • 27,717
  • 28
  • 128
  • 190
adinas
  • 4,150
  • 3
  • 37
  • 47
  • 1
    `Action` only accepts void functions, the see [Func](https://learn.microsoft.com/en-us/dotnet/api/system.func-2?view=net-6.0) if you want a return value. However, the linked page has a complete section about "Accessing .NET assemblies and classes", have you tried that? – JonasH Mar 15 '22 at 15:49
  • 1
    You would have to be looking for maybe an overload of `SetValue` or maybe a different method that takes a `Func<>` or similar. In fact, I'd just boldly try to pass a Func and see what happens. – Fildor Mar 15 '22 at 15:50

3 Answers3

3

Here is a fully functional example. Casting Console.WriteLine is required since there are multiple overloads and we need to tell the compiler which one to use:

using System;
using Jint;

var engine = new Engine()
    .SetValue("multiplyByTwo", MultiplyByTwo)
    .SetValue("log", (Action<string>)Console.WriteLine)
    ;
    
engine.Execute(@"
    var x = multiplyByTwo(3);
    log(x);
");

static int MultiplyByTwo(int obj)
{
    return obj * 2;
}
Sébastien Ros - MSFT
  • 5,091
  • 29
  • 31
1

You can certainly reference a class, which can involve setting properties, and methods can return values. For example:

class Program
{
    static void Main(string[] args)
    {
        var myobj = new MyClass();
        var engine = new Engine()
            .SetValue("log", new Action<object>(Console.WriteLine))
            .SetValue("myobj", myobj);

        engine.Execute(@"
        function hello() { 
            myobj.One = 'asdf';
            var x = myobj.TwoTimes(4);
            log(x); // logs 8
        };
    
        hello();
    ");
        Console.WriteLine($"{myobj.One}, {myobj.Two}");
        // output => "asdf 8"
    }

}

public class MyClass
{
    public string One { get; set; }
    public int Two { get; set; }
    public int TwoTimes(int value)
    {
        Two = 2 * value;
        return Two;
    }
}
David784
  • 7,031
  • 2
  • 22
  • 29
1

As mentioned in some comments, use

SetValue("multiplyByTwo", new Func<int>(MultiplyByTwo))
Sébastien Ros - MSFT
  • 5,091
  • 29
  • 31
  • 1
    Or even just `.SetValue("MultiplyByTwo", MultiplyByTwo);` - since (like in most other cases), the C# compiler will infer the type of the method and add the creation of the required delegate implicitly. – JimmiTh Mar 15 '22 at 20:29