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#.
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:
Save the file as
C:\MyEval.js
Open a VS2005 Command Prompt (Start, Programs, VS2005, VS2005 Tools)
Type
Cd\
to get toC:\
Type
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:
objResult is
True
.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
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:
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:
And here is an example of script that creates a winform and show it.
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
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:
will display 1.66666666666667