219

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#.

John Saunders
  • 157,405
  • 24
  • 229
  • 388
aprilchild
  • 566
  • 3
  • 5
  • 7

21 Answers21

98

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.

Sachin Joseph
  • 15,841
  • 3
  • 36
  • 54
Michel Boissé
  • 177
  • 3
  • 4
  • +1. It's simple, and seems to work great. – J.C. Inacio Jun 07 '11 at 15:44
  • 6
    There are claims that this is dead in the project's discussion boards, the documentation beyond a basic example is poor and I am struggling to add a JavaScript event handler to a .Net event with it in a script. – Alan Macdonald Nov 24 '11 at 16:39
  • 7
    Noesis (original developing company) lost interest in the project, but eventually decided to give control to the community. Consequently there is now a new release that includes many bug fixes. Unfortunately JavaScript callbacks from .Net are not yet supported. – Oliver Bock Feb 09 '12 at 23:43
  • Another note for the future: it appears that the Nuget package is borked somehow...it currently doesn't "just work" when you install the package. – Chris Pfohl Feb 14 '14 at 18:46
  • 4
    Since javascriptdotnet is dead, consider using ClearScript instead (see this [answer](http://stackoverflow.com/questions/172753/embedding-javascript-engine-into-net/17110645#17110645)) – JB. With Monica. May 26 '14 at 15:20
  • If you install via NuGet, set the platform to x86. AnyCPU doesn't seem to work. – Mrchief Jul 18 '14 at 03:21
88

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

Sébastien Ros - MSFT
  • 4,811
  • 25
  • 30
  • 6
    Another possibility built on top of DLR: RemObjects Script for .NET http://www.remobjects.com/script.aspx http://blogs.remobjects.com/blogs/ck/2010/02/23/p1175 – aprilchild Feb 23 '10 at 17:58
  • I think it's written on Delphi Prism / Oxygen, and provided as source code so if you haven't purchased a license for the aforementioned products it's not that useful. Am I wrong? If there is a version in binary form anywhere I'd like to give it a try. – Andrew Savinykh Apr 01 '10 at 07:53
  • 2
    RemObjects Script is written in Oxygene, correct, but you dont actually need an Oxygene license to use it - for one, you can get the binary installer from http://remobjects.com/free, for another you could even get the free Oxygene command line compiler if you did need to rebuild it yourself, from source. One of the great benefits of the .NEt platform is language independence, after all. it doesn't matter what language a library is written in, for you to use it. – marc hoffman Dec 10 '11 at 16:14
  • 1
    Last release in 2011... Jurassic seems better. – Roman Starkov Feb 03 '14 at 15:24
  • 3
    Last release in 2014 actually: https://github.com/sebastienros/jint – Sébastien Ros - MSFT Mar 03 '14 at 06:07
  • Does the rewritten Jint work with Mono 2.0? Unfortunately Unity3D is stuck with that version... – kungfooman Apr 21 '15 at 05:48
  • It should be compatible. As long as it supports the dynamic keyword. – Sébastien Ros - MSFT Apr 24 '15 at 23:10
56

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+

ToolmakerSteve
  • 5,893
  • 8
  • 67
  • 145
JB. With Monica.
  • 1,016
  • 1
  • 20
  • 36
  • 3
    +1 I was totally unaware of this project, seems promising lets hope it doesn't die like most other projects trying to do the same thing! – Peter Jul 19 '13 at 13:49
  • One thing I've found different in ClearScript using the Windows.JScript engine is the .net objects are now case sensitive, where the older ScriptControl object did not matter. – Brain2000 Apr 05 '17 at 23:06
  • Here is my problem with ClearScript. When run it complains there are missing dlls. Then i am asked to install extra visual studio components. That's no problem except i will have no admin privilege where I will run the code. It would be nice if it was totally self contained in a dll. – TatiOverflow Dec 27 '17 at 20:44
40

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

kas
  • 93
  • 7
bbqchickenrobot
  • 3,365
  • 3
  • 38
  • 62
  • 3
    It seems this is one of the few projects that has both native .NET code (instead of using f.ex. the V8 engine) and still seems to be supported (Ecmascript.net, jscript.net and a bunch of others seem to be dead). +1 and thanks for the link! – atlaste Jan 26 '13 at 15:31
  • 2
    After checking out every single project listed in these answers, I've homed in on Jurassic. It's one of only two or three that are still actively maintained, it's true JS, highly tested, and is pure .NET. – Roman Starkov Feb 03 '14 at 15:15
  • Would add my support for Jurassic. It also has the ability to serialize the run state of the execution context. That means you can literally persist your JavaScript runtime, variables and all. Extremely useful feature for some cases. – Jerome Haltom Jan 14 '16 at 19:10
  • Hi wasabi, I was not able to find anything on how to serialize the run state of the execution context. How did you achieve that? – Henning May 26 '16 at 13:13
8

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

Sergi Mansilla
  • 11,137
  • 8
  • 36
  • 45
  • We use IronJS currently but it is a dead project these days and the compiler is written in F# which I can tool around in but am not proficient with. – russbishop Apr 26 '14 at 19:16
7

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.

Jason Bunting
  • 55,140
  • 13
  • 95
  • 93
  • actually Managed JScript is the closest thing to my needs. I knew it was in Silverlight, didn't know it was released. JScript.NET is not an option. I really don't want to give users access to the whole .NET stack. I just need couple of core built-in objects they can use for their applications. – aprilchild Oct 06 '08 at 10:34
  • 17
    It's 2009 and Managed JScript is dead now. – Alexander Abramov Nov 24 '09 at 22:48
6

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.

Kevin Panko
  • 7,844
  • 19
  • 46
  • 58
sinanguler
  • 53
  • 1
  • 5
5

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

justin.m.chase
  • 11,241
  • 6
  • 42
  • 84
5

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"));
Deacon Frost
  • 265
  • 5
  • 7
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.

Chris S
  • 62,476
  • 49
  • 214
  • 238
4

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...

BenMorel
  • 30,280
  • 40
  • 163
  • 285
sanosdole
  • 2,409
  • 14
  • 17
2

There is also MsieJavaScriptEngine which uses Internet Explorers Chakra engine

Simon
  • 30,844
  • 15
  • 120
  • 187
2

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:

Community
  • 1
  • 1
Cheeso
  • 180,104
  • 92
  • 446
  • 681
2

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

Community
  • 1
  • 1
Simon Mourier
  • 117,251
  • 17
  • 221
  • 269
1

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 
Necowood
  • 1
  • 2
1

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

olliej
  • 32,789
  • 8
  • 55
  • 54
  • 2
    yes, all major engines are generally embeddable, but there are simply too many obstacles. studying the embedding guide, learning the C(++) API.. i was hoping there already would be some work on integration done. JScript.NET cannot be used, the actual code is written and executed by internet users. – aprilchild Oct 06 '08 at 10:26
1

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.

SilverX
  • 1,449
  • 15
  • 17
0

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.

James Wilkins
  • 5,536
  • 2
  • 35
  • 62
0

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

luiseduardohd
  • 351
  • 3
  • 6
0

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!

-5

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.

ahmadali shafiee
  • 3,586
  • 11
  • 45
  • 85