[c#] Embedding JavaScript engine into .NET

just wondering if anyone has ever tried embedding and actually integrating any js engine into the .net environment. I could find and actually use (after a LOT of pain and effort, since it's pretty outdated and not quite finished) spidermonkey-dotnet project. Anyone with experience in this area? Engines like SquirrelFish, V8..

Not that I'm not satisfied with Mozilla's Spidermonkey (using it for Rails-like miniframework for custom components inside the core ASP.NET application), but I'd still love to explore a bit further with the options. The command-line solutions are not what I'd need, I cannot rely on anything else than CLR, I need to call methods from/to JavaScript/C# objects.

// c# class
public class A
{
    public string Hello(string msg)
    {
        return msg + " whatewer";
    }
}

// js snippet
var a = new A();
console.log(a.Hello('Call me')); // i have a console.log implemented, don't worry, it's not a client-side code :)

Just to clarify - I'm not trying to actually program the application itself in server-side javascript. It's used solely for writing custom user subapplications (can be seen as some sort of DSL). It's much easier (and safer) to allow normal people programming in js than C#.

This question is related to c# javascript spidermonkey

The answer is


You can try ironJS, looks promising although it is in heavy development. https://github.com/fholm/IronJS


Try Javascript .NET. It is hosted on GitHub It was originally hosted on CodePlex, here)

Project discussions: http://javascriptdotnet.codeplex.com/discussions

It implements Google V8. You can compile and run JavaScript directly from .NET code with it, and supply CLI objects to be used by the JavaScript code as well. It generates native code from JavaScript.


It's Possible now with ASP.Net MVC4 Razor View engine. the code will be this:

// c# class
public class A
{
    public string Hello(string msg)
    {
        return msg + " whatewer";
    }
}

// js snippet
<script type="text/javascript">
var a = new A();
console.log('@a.Hello('Call me')'); // i have a console.log implemented, don't worry, it's not a client-side code :)
</script>

and Razor isn't just for MVC4 or another web applications and you can use it in offline desktop applications.


If the language isn't a problem (any sandboxed scripted one) then there's LUA for .NET. The Silverlight version of the .NET framework is also sandboxed afaik.


I guess I am still unclear about what it is you are trying to do, but JScript.NET might be worth looking into, though Managed JScript seems like it may be more appropriate for your needs (it is more like JavaScript than JScript.NET).

Personally, I thought it would be cool to integrate V8 somehow, but I didn't get past downloading the source code; wish I had the time to actually do something with it.


You can use the Chakra engine in C#. Here is an article on msdn showing how:

http://code.msdn.microsoft.com/windowsdesktop/JavaScript-Runtime-Hosting-d3a13880


The open source JavaScript interpreter Jint (http://jint.codeplex.com) does exactly what you are looking for.

Edit:
The project has been entirely rewritten and is now hosted on Github at https://github.com/sebastienros/jint


i believe all the major opensource JS engines (JavaScriptCore, SpiderMonkey, V8, and KJS) provide embedding APIs. The only one I am actually directly familiar with is JavaScriptCore (which is name of the JS engine the SquirrelFish lives in) which provides a pure C API. If memory serves (it's been a while since i used .NET) .NET has fairly good support for linking in C API's.

I'm honestly not sure what the API's for the other engines are like, but I do know that they all provide them.

That said, depending on your purposes JScript.NET may be best, as all of these other engines will require you to include them with your app, as JSC is the only one that actually ships with an OS, but that OS is MacOS :D


You can use Rhino a Mozilla Javascript engine written on Java, and use it with IKVM , here are some instructions

Instructions:https://www.codeproject.com/Articles/41792/Embedding-JavaScript-into-C-with-Rhino-and-IKVM


I know I'm opening up an old thread but I've done a lot of work on smnet (spidermonkey-dotnet). In the recent years. It's main development focus has been seamless embedding of .net objects into the spidermonkey engine. It supports a wide variety of conversions from js values to .net objects. Some of those including delegates and events.

Just saying it might be worth checking into now that there's some steady development on it :). I do keep the SVN repo up to date with bug fixes and new features. The source and project solution files are configured to successfully build on download. If there are any problems using it, feel free to open a discussion.

I do understand the desire to have a managed javascript solution, but of all the managed javascript's I've used they're all very lacking in some key features that help make them both robust and easy to work with. I myself am waiting on IronJS to mature a little. While I wait, I have fun playing with spidermonkey-dotnet =)

spidermonkey-dotnet project and download page

Edit: created documentation wiki page this afternoon.


V8.NET is a new kid on the block (as of April 2013) that more closely wraps the native V8 engine functionality. It allows for more control over the implementation.


Try ReoScript, an open-source JavaScript interpreter implemented in C#.

ReoScript makes your application can execute JavaScript. It has a wide variety of extension methons such as SetVariable, Function Extension, using CLR Type, .Net Event Binding and etc.

Hello World:

ScriptRunningMachine srm = new ScriptRunningMachine();
srm.Run(" alert('hello world!'); ");

And here is an example of script that creates a winform and show it.

import System.Windows.Forms.*;        // import namespace

var f = new Form();                   // create form
f.click = function() { f.close(); };  // close when user clicked on form

f.show();                             // show 

V8.NET is a new kid on the block (as of April 2013) that more closely wraps the native V8 engine functionality. It allows for more control over the implementation.


You might also be interested in Microsoft ClearScript which is hosted on GitHub and published under the Ms-Pl licence.

I am no Microsoft fanboy, but I must admit that the V8 support has about the same functionnalities as Javascript.Net, and more important, the project is still maintained. As far as I am concerned, the support for delegates also functions better than with Spidermonkey-dotnet.

ps: It also support JScript and VBScript but we were not interested by this old stuff.

ps: It is compatible with .NET 4.0 and 4.5+


There is an implementation of an ActiveX Scripting Engine Host in C# available here: parse and execute JS by C#

It allows to use Javascript (or VBScript) directly from C#, in native 32-bit or 64-bit processes. The full source is ~500 lines of C# code. It only has an implicit dependency on the installed JScript (or VBScript) engine DLL.

For example, the following code:

Console.WriteLine(ScriptEngine.Eval("jscript", "1+2/3"));

will display 1.66666666666667


i believe all the major opensource JS engines (JavaScriptCore, SpiderMonkey, V8, and KJS) provide embedding APIs. The only one I am actually directly familiar with is JavaScriptCore (which is name of the JS engine the SquirrelFish lives in) which provides a pure C API. If memory serves (it's been a while since i used .NET) .NET has fairly good support for linking in C API's.

I'm honestly not sure what the API's for the other engines are like, but I do know that they all provide them.

That said, depending on your purposes JScript.NET may be best, as all of these other engines will require you to include them with your app, as JSC is the only one that actually ships with an OS, but that OS is MacOS :D


I know I'm opening up an old thread but I've done a lot of work on smnet (spidermonkey-dotnet). In the recent years. It's main development focus has been seamless embedding of .net objects into the spidermonkey engine. It supports a wide variety of conversions from js values to .net objects. Some of those including delegates and events.

Just saying it might be worth checking into now that there's some steady development on it :). I do keep the SVN repo up to date with bug fixes and new features. The source and project solution files are configured to successfully build on download. If there are any problems using it, feel free to open a discussion.

I do understand the desire to have a managed javascript solution, but of all the managed javascript's I've used they're all very lacking in some key features that help make them both robust and easy to work with. I myself am waiting on IronJS to mature a little. While I wait, I have fun playing with spidermonkey-dotnet =)

spidermonkey-dotnet project and download page

Edit: created documentation wiki page this afternoon.


Try ReoScript, an open-source JavaScript interpreter implemented in C#.

ReoScript makes your application can execute JavaScript. It has a wide variety of extension methons such as SetVariable, Function Extension, using CLR Type, .Net Event Binding and etc.

Hello World:

ScriptRunningMachine srm = new ScriptRunningMachine();
srm.Run(" alert('hello world!'); ");

And here is an example of script that creates a winform and show it.

import System.Windows.Forms.*;        // import namespace

var f = new Form();                   // create form
f.click = function() { f.close(); };  // close when user clicked on form

f.show();                             // show 

i believe all the major opensource JS engines (JavaScriptCore, SpiderMonkey, V8, and KJS) provide embedding APIs. The only one I am actually directly familiar with is JavaScriptCore (which is name of the JS engine the SquirrelFish lives in) which provides a pure C API. If memory serves (it's been a while since i used .NET) .NET has fairly good support for linking in C API's.

I'm honestly not sure what the API's for the other engines are like, but I do know that they all provide them.

That said, depending on your purposes JScript.NET may be best, as all of these other engines will require you to include them with your app, as JSC is the only one that actually ships with an OS, but that OS is MacOS :D


I came up with a much simpler solution instead.

I built a .dll file using Javascript and then compiled it using the Javascript compiler which is available in a VS2013 developer command prompt.

Once we have the .dll we simply add it to the \Support folder and then referenced it in the project which needed to eval Javascript statements.

Detailed Steps to create a .dll:

  1. Create a file in Notepad with only these contents:

    class EvalClass { function Evaluate(expression: String) { return eval(expression); } } 
    
  2. Save the file as C:\MyEval.js

  3. Open a VS2005 Command Prompt (Start, Programs, VS2005, VS2005 Tools)

  4. Type Cd\ to get to C:\

  5. Type

    jsc /t:library C:\MyEval.js
    
  6. A new file is created named MyEval.dll.

  7. Copy MyEval.dll to the project and reference it (also reference Microsoft.Jscript.dll).

  8. Then you should be able to call it like this:

    Dim jScriptEvaluator As New EvalClass
    Dim objResult As Object
    objResult = jScriptEvaluator.Evaluate(“1==1 && 2==2”)
    

objResult is True.


Try Javascript .NET. It is hosted on GitHub It was originally hosted on CodePlex, here)

Project discussions: http://javascriptdotnet.codeplex.com/discussions

It implements Google V8. You can compile and run JavaScript directly from .NET code with it, and supply CLI objects to be used by the JavaScript code as well. It generates native code from JavaScript.


The open source JavaScript interpreter Jint (http://jint.codeplex.com) does exactly what you are looking for.

Edit:
The project has been entirely rewritten and is now hosted on Github at https://github.com/sebastienros/jint


I guess I am still unclear about what it is you are trying to do, but JScript.NET might be worth looking into, though Managed JScript seems like it may be more appropriate for your needs (it is more like JavaScript than JScript.NET).

Personally, I thought it would be cool to integrate V8 somehow, but I didn't get past downloading the source code; wish I had the time to actually do something with it.


You might also be interested in Microsoft ClearScript which is hosted on GitHub and published under the Ms-Pl licence.

I am no Microsoft fanboy, but I must admit that the V8 support has about the same functionnalities as Javascript.Net, and more important, the project is still maintained. As far as I am concerned, the support for delegates also functions better than with Spidermonkey-dotnet.

ps: It also support JScript and VBScript but we were not interested by this old stuff.

ps: It is compatible with .NET 4.0 and 4.5+


If the language isn't a problem (any sandboxed scripted one) then there's LUA for .NET. The Silverlight version of the .NET framework is also sandboxed afaik.


Anybody just tuning in check out Jurassic as well:

http://jurassic.codeplex.com/

edit: this has moved to github (and seems active at first glance)

https://github.com/paulbartrum/jurassic


You can use the Chakra engine in C#. Here is an article on msdn showing how:

http://code.msdn.microsoft.com/windowsdesktop/JavaScript-Runtime-Hosting-d3a13880


I guess I am still unclear about what it is you are trying to do, but JScript.NET might be worth looking into, though Managed JScript seems like it may be more appropriate for your needs (it is more like JavaScript than JScript.NET).

Personally, I thought it would be cool to integrate V8 somehow, but I didn't get past downloading the source code; wish I had the time to actually do something with it.


I just tried RemObjects Script for .Net.

It works, although I had to use a static factory (var a=A.createA();) from JavaScript instead of the var a=new A() syntax. (ExposeType function only exposes statics!) Not much documentation and the source is written with Delphi Prism, which is rather unusual for me and the RedGate Reflector.

So: Easy to use and setup, but not much help for advanced scenarios.

Also having to install something instead of just dropping the assemblies in a directory is a negative for me...


Use JSCRIPT.NET to get a library(dll) of the js . Then reference this dll in your .NET application and you are just there. DONE!


Microsoft's documented way to add script extensibility to anything is IActiveScript. You can use IActiveScript from within anyt .NET app, to call script logic. The logic can party on .NET objects that you've placed into the scripting context.

This answer provides an application that does it, with code:


There is also MsieJavaScriptEngine which uses Internet Explorers Chakra engine


I guess I am still unclear about what it is you are trying to do, but JScript.NET might be worth looking into, though Managed JScript seems like it may be more appropriate for your needs (it is more like JavaScript than JScript.NET).

Personally, I thought it would be cool to integrate V8 somehow, but I didn't get past downloading the source code; wish I had the time to actually do something with it.


Anybody just tuning in check out Jurassic as well:

http://jurassic.codeplex.com/

edit: this has moved to github (and seems active at first glance)

https://github.com/paulbartrum/jurassic


Hey take a look for Javascript .NET on codeplex (http://javascriptdotnet.codeplex.com/) with the version 0.3.1 there is some pretty sweet new features that will probly interest you.

Check out a sample code:

// Initialize the context
JavascriptContext context = new JavascriptContext();

// Setting the externals parameters of the context
context.SetParameter("console", new SystemConsole());
context.SetParameter("message", "Hello World !");
context.SetParameter("number", 1);

// Running the script
context.Run("var i; for (i = 0; i < 5; i++) console.Print(message + ' (' + i + ')'); number += i;");

// Getting a parameter
Console.WriteLine("number: " + context.GetParameter("number"));

I just tried RemObjects Script for .Net.

It works, although I had to use a static factory (var a=A.createA();) from JavaScript instead of the var a=new A() syntax. (ExposeType function only exposes statics!) Not much documentation and the source is written with Delphi Prism, which is rather unusual for me and the RedGate Reflector.

So: Easy to use and setup, but not much help for advanced scenarios.

Also having to install something instead of just dropping the assemblies in a directory is a negative for me...


There is also MsieJavaScriptEngine which uses Internet Explorers Chakra engine


You can use Rhino a Mozilla Javascript engine written on Java, and use it with IKVM , here are some instructions

Instructions:https://www.codeproject.com/Articles/41792/Embedding-JavaScript-into-C-with-Rhino-and-IKVM


Microsoft's documented way to add script extensibility to anything is IActiveScript. You can use IActiveScript from within anyt .NET app, to call script logic. The logic can party on .NET objects that you've placed into the scripting context.

This answer provides an application that does it, with code:


i believe all the major opensource JS engines (JavaScriptCore, SpiderMonkey, V8, and KJS) provide embedding APIs. The only one I am actually directly familiar with is JavaScriptCore (which is name of the JS engine the SquirrelFish lives in) which provides a pure C API. If memory serves (it's been a while since i used .NET) .NET has fairly good support for linking in C API's.

I'm honestly not sure what the API's for the other engines are like, but I do know that they all provide them.

That said, depending on your purposes JScript.NET may be best, as all of these other engines will require you to include them with your app, as JSC is the only one that actually ships with an OS, but that OS is MacOS :D


You can try ironJS, looks promising although it is in heavy development. https://github.com/fholm/IronJS


It's Possible now with ASP.Net MVC4 Razor View engine. the code will be this:

// c# class
public class A
{
    public string Hello(string msg)
    {
        return msg + " whatewer";
    }
}

// js snippet
<script type="text/javascript">
var a = new A();
console.log('@a.Hello('Call me')'); // i have a console.log implemented, don't worry, it's not a client-side code :)
</script>

and Razor isn't just for MVC4 or another web applications and you can use it in offline desktop applications.


There is an implementation of an ActiveX Scripting Engine Host in C# available here: parse and execute JS by C#

It allows to use Javascript (or VBScript) directly from C#, in native 32-bit or 64-bit processes. The full source is ~500 lines of C# code. It only has an implicit dependency on the installed JScript (or VBScript) engine DLL.

For example, the following code:

Console.WriteLine(ScriptEngine.Eval("jscript", "1+2/3"));

will display 1.66666666666667


I came up with a much simpler solution instead.

I built a .dll file using Javascript and then compiled it using the Javascript compiler which is available in a VS2013 developer command prompt.

Once we have the .dll we simply add it to the \Support folder and then referenced it in the project which needed to eval Javascript statements.

Detailed Steps to create a .dll:

  1. Create a file in Notepad with only these contents:

    class EvalClass { function Evaluate(expression: String) { return eval(expression); } } 
    
  2. Save the file as C:\MyEval.js

  3. Open a VS2005 Command Prompt (Start, Programs, VS2005, VS2005 Tools)

  4. Type Cd\ to get to C:\

  5. Type

    jsc /t:library C:\MyEval.js
    
  6. A new file is created named MyEval.dll.

  7. Copy MyEval.dll to the project and reference it (also reference Microsoft.Jscript.dll).

  8. Then you should be able to call it like this:

    Dim jScriptEvaluator As New EvalClass
    Dim objResult As Object
    objResult = jScriptEvaluator.Evaluate(“1==1 && 2==2”)
    

objResult is True.


Hey take a look for Javascript .NET on codeplex (http://javascriptdotnet.codeplex.com/) with the version 0.3.1 there is some pretty sweet new features that will probly interest you.

Check out a sample code:

// Initialize the context
JavascriptContext context = new JavascriptContext();

// Setting the externals parameters of the context
context.SetParameter("console", new SystemConsole());
context.SetParameter("message", "Hello World !");
context.SetParameter("number", 1);

// Running the script
context.Run("var i; for (i = 0; i < 5; i++) console.Print(message + ' (' + i + ')'); number += i;");

// Getting a parameter
Console.WriteLine("number: " + context.GetParameter("number"));

Use JSCRIPT.NET to get a library(dll) of the js . Then reference this dll in your .NET application and you are just there. DONE!