Are there any .NET CLR/DLR implementations of ECMAScript?

泄露秘密 提交于 2019-11-28 15:43:56

Currently, I've modified a version of the EcmaScript.NET inside my YUICompressor.NET port (project).

If you grab the source code from here, I've included my modified code in the project, which you can reference. This is the only source of code i've found in .NET which can handle parsing javascript, server side.

Unfortunately, I can't remember how I finally found it. It was a tough process, I must admit. I think i found some references Mozilla dev pages somewhere about Rhino (the java code) which led me to finding that C# .NET implimentation.

I had to modify it slighty to sync up with some changes the YUI Compressor guys did to their code branch. So the modified branch I have might not be the best .. but it's the closest I've seen to the original Java branch.

The original c# code for EcmaScript.NET hasn't been touched since 2007 ... at least for the downloads page.

Maybe this might help??

HTH.

Still Roughly alive:

  • Jurassic (Had a commit within the last year.)
  • Jint (currently taking pull requests.)

Other Projects, Mostly Dead:

Crazy Method:

  • Rhino over IKVM (Mentioned in comments, but here's a link to more information about doing it.)

Nobody has mentioned jurassic http://jurassic.codeplex.com/ I do not know how good it is in general (performance, usability, etc) but at least it parses pretty complex scripts (other implementations do not) and it supports ECMAScript 5 spec. I just add the link here for reference.

Nobody's mentioned ClearScript, so ClearScript.

It's not an implementation; it's an interop wrapper that supports V8, JScript and VBScript, with a really nice API to call into them from .NET code.

Example code from the CodePlex page:

using System;
using Microsoft.ClearScript;
using Microsoft.ClearScript.V8;

// create a script engine
using (var engine = new V8ScriptEngine())
{
    // expose a host type
    engine.AddHostType("Console", typeof(Console));
    engine.Execute("Console.WriteLine('{0} is an interesting number.', Math.PI)");

    // expose a host object
    engine.AddHostObject("random", new Random());
    engine.Execute("Console.WriteLine(random.NextDouble())");

    // expose entire assemblies
    engine.AddHostObject("lib", new HostTypeCollection("mscorlib", "System.Core"));
    engine.Execute("Console.WriteLine(lib.System.DateTime.Now)");

    // create a host object from script
    engine.Execute(@"
        birthday = new lib.System.DateTime(2007, 5, 22);
        Console.WriteLine(birthday.ToLongDateString());
    ");

    // use a generic class from script
    engine.Execute(@"
        Dictionary = lib.System.Collections.Generic.Dictionary;
        dict = new Dictionary(lib.System.String, lib.System.Int32);
        dict.Add('foo', 123);
    ");

    // call a host method with an output parameter
    engine.AddHostObject("host", new HostFunctions());
    engine.Execute(@"
        intVar = host.newVar(lib.System.Int32);
        found = dict.TryGetValue('foo', intVar.out);
        Console.WriteLine('{0} {1}', found, intVar);
    ");

    // create and populate a host array
    engine.Execute(@"
        numbers = host.newArr(lib.System.Int32, 20);
        for (var i = 0; i < numbers.Length; i++) { numbers[i] = i; }
        Console.WriteLine(lib.System.String.Join(', ', numbers));
    ");

    // create a script delegate
    engine.Execute(@"
        Filter = lib.System.Func(lib.System.Int32, lib.System.Boolean);
        oddFilter = new Filter(function(value) {
            return (value & 1) ? true : false;
        });
    ");

    // use LINQ from script
    engine.Execute(@"
        oddNumbers = numbers.Where(oddFilter);
        Console.WriteLine(lib.System.String.Join(', ', oddNumbers));
    ");

    // call a script function
    engine.Execute("function print(x) { Console.WriteLine(x); }");
    engine.Script.print(DateTime.Now.DayOfWeek);

    // examine a script object
    engine.Execute("person = { name: 'Fred', age: 5 }");
    Console.WriteLine(engine.Script.person.name);
}

You can take a look at Jint (http://jint.codeplex.com) which is an open-source ECMAScript interpreter.

Update

Newly rewritten version available on Github at https://github.com/sebastienros/jint

You should try Javascript .NET (http://javascriptdotnet.codeplex.com/) on Codeplex. They wrapped v8 with managed C++ and then you can use this library with a .NET application and it works like a charm. The open source offers some pretty good features if you ask me.

Cheers.

You can use Jscript.net and it will actually work with arbitrary javascript code; You just need to turn off "fast mode" by compiling with jsc /fast- bar.js

I have not tested this; I just noticed it while reading this post and figured it would be another reasonable solution. MSDN Has the docs for this option and what the limitations are if you don't use it.

V8.NET

This one is probably the best one I've come across yet.
https://v8dotnet.codeplex.com/documentation

Plus its been built from the beginning with a Mono port in mind. It gives you full control over the power of the V8 engine from managed code.

ultra mother hacker

I prefer JINT rather than the others.

JINT may be slow, but it is easy to debug and to interact your own .NET classes. (It is hard to set [ComVisile] attributes every time for jscript.dll etc).

In terms of threading, JINT and Jurassic work as I expected. In order to work with JScript engine or Google V8, you have to pay attention to UI-threading issue.

However, JINT is out-dated in some aspect, because I have trouble to compile JQuery 1.5 or later.

I hope Jurassic can remove its limit to stick its own class to work with by creating 'AllowBob'sCLRClass=true'.

I have to re-write the entire class. It is hard...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!