Jint is a Javascript v 5 interpreter written in C# 4.0 using the Portable Library Standard and run or will run on the following platforms:
- Windows (using Microsoft stack)
- MacOS (using Mono or Xamarin stack)
- iOS (using Xamarin stack)
- Android (using Xamarin stack)
- Linux (using Mono stack)
- How to write C# classes and objects that can be exposed to the JavaScript world.
- How to write asynchronous API and call back function
At this time of this writing, the developers of Jint are focusing on Windows. I will personally soon test on iOS and MacOS.
How do I do it?
In this sample, I created a C# console app that
- Instantiate a Jint engine
- Create a JavaScript function print() so we can output object to the console
- Execute a Script
- Get the script return value
class Program
{
static Jint.Engine _engine;
public static void Print(object s)
{
if (s == null)
s = "null";
Console.WriteLine(s.ToString());
}
private static Jint.Engine CreateEngine()
{
return new Engine().SetValue("print", new Action<object>(Print));
}
static void Main(string[] args)
{
var source = @"
var s = 'Hello World From JavaScript ';
for(var i=0; i<4; i++) {
print(s + i);
}
s; // Evaluate and return the expression
";
var s = CreateEngine().Execute(source).GetCompletionValue();
Console.WriteLine(s.AsString());
Console.ReadLine();
}
}
Great!
ReplyDeleteAll the other documents are old and out of date, this is fresh.
Thank you!
Thanks for the good examples.
ReplyDeleteHow can I execute a JS function passing 2 strings as parameters and receive a json object as result?
I tried:
dim jsonResult = engine.Invoke("calcKm", "100;300", "120;350")
but I receive an error: ActiveXObject is not defined
Thanks in advance.
Alessandro
Thank you for providing JavaScript in a C# application. It is very latest and updated code which can help c# programmers. I was searching for this topic in C# tutorial and during this process found and open this link, when I read and use this code then I surprised.
ReplyDeleteHow can i call c# function form javascript using jint.
ReplyDeleteJavaScript Stack is a platform dedicated to sharing useful information, tutorials, tips, and resources related to JavaScript and web development. It helps beginners and developers learn concepts such as JavaScript programming, HTML, CSS, coding techniques, and modern web technologies through simple and easy-to-understand content.
ReplyDeleteThis website is powered by Blogger and created to provide helpful educational resources for developers and technology enthusiasts. JavaScript Full Stack Course
ReplyDeleteOur goal is to make web development easier to understand by sharing practical guides, examples, and useful programming knowledge in a clear and accessible way.