views:

6095

answers:

9

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

+3  A: 

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
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
It's 2009 and Managed JScript is dead now.
Alexander Abramov
A: 

Script# compiles C# to js and is the brain child of one of the ASP.Net team. Rick Strahl also has some posts on embedding js as runtime engine for expression evaluation.

David Robbins
interesting, but not quite what i'd like to do. the idea is to allow a universal language for writing server-side components that are actually running on server.
aprilchild
I've started playing with Jint (http://jint.codeplex.com/) and it has great potential. The advantage is have over CSScript is that the javascript engine is an interpreter that runs in the same thread as your App Domain, whereas CSScript compiles your script to an object and executes in a separate App Domain.
David Robbins
A: 

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
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
A: 

I'm searching for the same thing. Did you found a solution for embedding javascript in C# ? I saw ManagedJscript in ASP.NET future release but I can't afford to install this on a production software.

nope, I'm using Spidermonkey at the moment, still not convinced about the production quality though.. Managed.JSCript has no source code available (appart from IronRuby, IronPython), so it's pretty much useless (tied with silverlight, linking against old dlls and such).
aprilchild
+2  A: 

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
A: 

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!

+15  A: 

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

Sébastien Ros
Another possibility built on top of DLR:RemObjects Script for .NEThttp://www.remobjects.com/script.aspxhttp://blogs.remobjects.com/blogs/ck/2010/02/23/p1175
aprilchild
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.
zespri
+4  A: 

Try Javascript .NET:

http://javascriptdotnet.codeplex.com/

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. And V8 is probably the best engine ever created in terms of performance, it generates native code from Javascript.

Michel Boissé
A: 

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