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#.
-
Multithreaded Javascript Engine for .NET github.com/koculu/topazAhmed Yasin Koculu– Ahmed Yasin Koculu2021年11月14日 00:50:22 +00:00Commented Nov 14, 2021 at 0:50
21 Answers 21
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.
-
+1. It's simple, and seems to work great.J.C. Inacio– J.C. Inacio2011年06月07日 15:44:04 +00:00Commented Jun 7, 2011 at 15:44
-
6There 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– Alan Macdonald2011年11月24日 16:39:57 +00:00Commented Nov 24, 2011 at 16:39
-
7Noesis (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– Oliver Bock2012年02月09日 23:43:23 +00:00Commented Feb 9, 2012 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– Chris Pfohl2014年02月14日 18:46:00 +00:00Commented Feb 14, 2014 at 18:46
-
4
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
-
6Another possibility built on top of DLR: RemObjects Script for .NET remobjects.com/script.aspx blogs.remobjects.com/blogs/ck/2010/02/23/p1175aprilchild– aprilchild2010年02月23日 17:58:43 +00:00Commented Feb 23, 2010 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– Andrew Savinykh2010年04月01日 07:53:29 +00:00Commented Apr 1, 2010 at 7:53
-
2RemObjects 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.marc hoffman– marc hoffman2011年12月10日 16:14:01 +00:00Commented Dec 10, 2011 at 16:14
-
1Last release in 2011... Jurassic seems better.Roman Starkov– Roman Starkov2014年02月03日 15:24:07 +00:00Commented Feb 3, 2014 at 15:24
-
3Last release in 2014 actually: github.com/sebastienros/jintSébastien Ros - MSFT– Sébastien Ros - MSFT2014年03月03日 06:07:39 +00:00Commented Mar 3, 2014 at 6:07
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+
-
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– Peter2013年07月19日 13:49:22 +00:00Commented Jul 19, 2013 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– Brain20002017年04月05日 23:06:49 +00:00Commented Apr 5, 2017 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– TatiOverflow2017年12月27日 20:44:38 +00:00Commented Dec 27, 2017 at 20:44
Anybody just tuning in check out Jurassic as well:
edit: this has moved to github (and seems active at first glance)
-
3It 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– atlaste2013年01月26日 15:31:17 +00:00Commented Jan 26, 2013 at 15:31
-
2After 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– Roman Starkov2014年02月03日 15:15:35 +00:00Commented Feb 3, 2014 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– Jerome Haltom2016年01月14日 19:10:03 +00:00Commented Jan 14, 2016 at 19:10
-
1Hi 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– Henning2016年05月26日 13:13:29 +00:00Commented May 26, 2016 at 13:13
You can try ironJS, looks promising although it is in heavy development. https://github.com/fholm/IronJS
-
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– russbishop2014年04月26日 19:16:55 +00:00Commented Apr 26, 2014 at 19:16
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.
-
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– aprilchild2008年10月06日 10:34:40 +00:00Commented Oct 6, 2008 at 10:34
-
18It's 2009 and Managed JScript is dead now.Alexander Abramov– Alexander Abramov2009年11月24日 22:48:30 +00:00Commented Nov 24, 2009 at 22:48
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
:
Create a file in Notepad with only these contents:
class EvalClass { function Evaluate(expression: String) { return eval(expression); } }
Save the file as
C:\MyEval.js
Open a VS2005 Command Prompt (Start, Programs, VS2005, VS2005 Tools)
Type
Cd\
to get toC:\
Type
jsc /t:library C:\MyEval.js
A new file is created named
MyEval.dll
.Copy
MyEval.dll
to the project and reference it (also referenceMicrosoft.Jscript.dll
).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
.
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.
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"));
-
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?Joy George Kunjikkuru– Joy George Kunjikkuru2015年11月22日 18:45:07 +00:00Commented Nov 22, 2015 at 18:45
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 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...
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 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
There is also MsieJavaScriptEngine which uses Internet Explorers Chakra engine
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
-
2yes, 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– aprilchild2008年10月06日 10:26:59 +00:00Commented Oct 6, 2008 at 10:26
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
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!
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
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.
-
1You need to get your eyes checked, that doesn't say VB, it says V8. ;)James Wilkins– James Wilkins2023年09月07日 20:34:26 +00:00Commented Sep 7, 2023 at 20:34
-
You are right! Too bad it's a dead project.... so it was a moot point anyhow ;)bbqchickenrobot– bbqchickenrobot2023年09月26日 18:27:38 +00:00Commented Sep 26, 2023 at 18:27
-
1It'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.James Wilkins– James Wilkins2023年09月27日 05:57:05 +00:00Commented Sep 27, 2023 at 5:57
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.