Embedding JavaScript engine into .NET [closed]
Asked Answered
N

21

223

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

Nodal answered 5/10, 2008 at 22:15 Comment(1)
Multithreaded Javascript Engine for .NET github.com/koculu/topazReinert
M
99

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.

Merrygoround answered 5/10, 2008 at 22:15 Comment(6)
+1. It's simple, and seems to work great.Cutpurse
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.Jealous
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.Stigma
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.Erlking
Since javascriptdotnet is dead, consider using ClearScript instead (see this answer)Theda
If you install via NuGet, set the platform to x86. AnyCPU doesn't seem to work.Shiekh
P
92

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

Preclinical answered 26/10, 2009 at 12:30 Comment(7)
Another possibility built on top of DLR: RemObjects Script for .NET remobjects.com/script.aspx blogs.remobjects.com/blogs/ck/2010/02/23/p1175Nodal
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.Uriisa
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 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.Prepossession
Last release in 2011... Jurassic seems better.Mute
Last release in 2014 actually: github.com/sebastienros/jintElectroencephalograph
Does the rewritten Jint work with Mono 2.0? Unfortunately Unity3D is stuck with that version...Riemann
It should be compatible. As long as it supports the dynamic keyword.Electroencephalograph
T
62

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+

Theda answered 5/10, 2008 at 22:15 Comment(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!Anlage
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.Thirtyone
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.Walrath
G
41

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

Granadilla answered 5/10, 2008 at 22:15 Comment(4)
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!Arman
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.Mute
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.Finnell
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?Gobioid
G
8

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

Gamekeeper answered 5/10, 2008 at 22:15 Comment(1)
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.Curriculum
G
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.

Godolphin answered 5/10, 2008 at 23:1 Comment(2)
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.Nodal
It's 2009 and Managed JScript is dead now.Desperate
F
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.

Faun answered 5/10, 2008 at 22:15 Comment(0)
S
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

Supererogate answered 5/10, 2008 at 22:15 Comment(0)
L
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"));
Leveret answered 5/10, 2008 at 22:15 Comment(1)
Does this work for .Net objects. eg: If I have an Employee class can I pass the object to V8 and manipulate Employee object from JavaScript?Cotonou
S
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.

Seve answered 22/1, 2009 at 14:48 Comment(0)
B
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...

Bergsonism answered 5/10, 2008 at 22:15 Comment(0)
T
2

There is also MsieJavaScriptEngine which uses Internet Explorers Chakra engine

Teide answered 5/10, 2008 at 22:15 Comment(0)
I
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

Ironist answered 5/10, 2008 at 22:15 Comment(0)
A
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:

Apogeotropism answered 5/10, 2008 at 22:15 Comment(0)
M
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 
Moderato answered 5/10, 2008 at 22:15 Comment(0)
C
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.

Colston answered 5/10, 2008 at 22:15 Comment(0)
B
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

Boreas answered 6/10, 2008 at 3:57 Comment(1)
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.Nodal
T
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

Tuberous answered 5/10, 2008 at 22:15 Comment(0)
M
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!

Merlon answered 6/3, 2009 at 20:25 Comment(0)
K
-1

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.

Kobylak answered 5/10, 2008 at 22:15 Comment(3)
You need to get your eyes checked, that doesn't say VB, it says V8. ;)Kobylak
You are right! Too bad it's a dead project.... so it was a moot point anyhow ;)Granadilla
It's not dead, it's my project, maybe sleeping, I just haven't had time to update anything these days, but I might sometime soon. ;) It should still fully work as intended, just perhaps not with the latest V8 engine updates yet.Kobylak
J
-6

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.

Justifiable answered 5/10, 2008 at 22:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.