只是想知道是否有人尝试过将任何js引擎嵌入到。net环境中。我可以找到并实际使用(经过大量的痛苦和努力,因为它相当过时,还没有完全完成)spidermonkey-dotnet项目。有人有这方面的经验吗?引擎像SquirrelFish, V8..

并不是说我对Mozilla的Spidermonkey不满意(在核心ASP中使用它为自定义组件提供类似rails的迷你框架)。NET应用程序),但我仍然想进一步探索这些选项。命令行解决方案不是我所需要的,我不能依赖CLR以外的任何东西,我需要从/到JavaScript/ c#对象调用方法。

// 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 :)

澄清一下——我并不是试图用服务器端javascript编写应用程序本身。它仅用于编写自定义用户子应用程序(可以看作某种DSL)。让普通人用js编程比用c#更容易(也更安全)。


当前回答

您可以使用Rhino在Java上编写的Mozilla Javascript引擎,并将其与IKVM一起使用,这里有一些说明

产品说明:https://www.codeproject.com/Articles/41792/Embedding-JavaScript-into-C-with-Rhino-and-IKVM

其他回答

试试Javascript . net吧。它最初托管在CodePlex上,这里)

项目讨论:http://javascriptdotnet.codeplex.com/discussions

它实现了谷歌V8。您可以使用它直接从. net代码编译和运行JavaScript,并提供JavaScript代码使用的CLI对象。它从JavaScript生成本地代码。

开源JavaScript解释器Jint (http://jint.codeplex.com)正是您想要的。

编辑: 该项目已经完全重写,现在托管在Github上https://github.com/sebastienros/jint

嘿,在codeplex (http://javascriptdotnet.codeplex.com/)上看看Javascript . net的0.3.1版本,有一些非常棒的新特性,你可能会感兴趣。

查看示例代码:

// 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"));

如果语言不是问题(任何沙盒脚本语言),那么。net有LUA。. net框架的Silverlight版本也是沙盒的。

我想我仍然不清楚你要做的是什么,但是JScript。NET可能值得研究,尽管Managed JScript似乎更适合您的需求(它更像JavaScript而不是JScript.NET)。

就我个人而言,我认为以某种方式集成V8会很酷,但我没有下载源代码;真希望我有时间用它做点什么。