C# 将 JavaScript 引擎嵌入到 .NET 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/172753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:37:40  来源:igfitidea点击:

Embedding JavaScript engine into .NET

c#javascriptspidermonkey

提问by aprilchild

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 LOTof 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..

只是想知道是否有人曾尝试将任何 js 引擎嵌入并实际集成到 .net 环境中。我能找到和实际使用(一后LOT艰辛和努力的,因为它是非常过时的,不完全结束)SpiderMonkey的-的dotnet项目。有这方面经验的人吗?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.

并不是说我对 Mozilla 的 Spidermonkey 不满意(将它用于类似 Rails 的微型框架,用于在核心 ASP.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 :)

Just to clarify - I'm not trying to actually program the application itselfin 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#.

只是为了澄清 - 我并没有尝试在服务器端 javascript 中实际对应用程序本身进行编程。它仅用于编写自定义用户子应用程序(可以视为某种 DSL)。与 C# 相比,允许普通人使用 js 编程更容易(也更安全)。

回答by Jason Bunting

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 JScriptseems like it may be more appropriate for your needs (it is more like JavaScript than JScript.NET).

我想我仍然不清楚您正在尝试做什么,但 JScript.NET 可能值得研究,尽管Managed JScript似乎更适合您的需求(它更像是 JavaScript 而不是 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.

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

回答by olliej

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.

我相信所有主要的开源 JS 引擎(JavaScriptCore、SpiderMonkey、V8 和 KJS)都提供嵌入 API。我真正直接熟悉的唯一一个是 JavaScriptCore(这是 SquirrelFish 所在的 JS 引擎的名称),它提供了一个纯 C API。如果没记错的话(自从我使用 .NET 已经有一段时间了).NET 对 C API 中的链接有相当好的支持。

I'm honestly not sure what the API's for the other engines are like, but I do know that they all provide them.

老实说,我不确定其他引擎的 API 是什么样的,但我知道它们都提供了它们。

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

也就是说,根据您的目的,JScript.NET 可能是最好的,因为所有这些其他引擎都要求您将它们包含在您的应用程序中,因为 JSC 是唯一一个实际附带操作系统的引擎,但该操作系统是 MacOS :D

回答by Chris S

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.

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

回答by Chris S

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!

使用 JSCRIPT.NET 获取 js 的库(dll)。然后在你的 .NET 应用程序中引用这个 dll,你就在那里。完毕!

回答by Sébastien Ros - MSFT

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

开源 JavaScript 解释器 Jint ( http://jint.codeplex.com) 完全符合您的要求。

Edit:
The project has been entirely rewritten and is now hosted on Github at https://github.com/sebastienros/jint

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

回答by Michel Boissé

Try Javascript .NET. It is hosted on GitHub It was originally hosted on CodePlex, here)

尝试Javascript .NET。它托管在 GitHub 上它最初托管在 CodePlex 上,这里)

Project discussions: http://javascriptdotnet.codeplex.com/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.

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

回答by Deacon Frost

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.

嘿,在 0.3.1 版的codeplex ( http://javascriptdotnet.codeplex.com/)上寻找 Javascript .NET有一些非常棒的新功能,您可能会感兴趣。

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

回答by Sergi Mansilla

You can try ironJS, looks promising although it is in heavy development. https://github.com/fholm/IronJS

你可以试试 IronJS,虽然它正在大量开发中,但看起来很有希望。https://github.com/fholm/IronJS

回答by sanosdole

I just tried RemObjects Script for .Net.

我刚刚尝试了 .Net 的 RemObjects 脚本

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.

它有效,尽管我必须使用var a=A.createA();JavaScript 中的静态工厂 ( ) 而不是var a=new A()语法。(ExposeType 函数只公开静态!)没有太多文档,源代码是用 Delphi Prism 编写的,这对我和 RedGate 反射器来说相当不寻常。

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...

还必须安装一些东西而不是仅仅将程序集放在目录中对我来说是不利的......

回答by SilverX

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.

我知道我正在打开一个旧线程,但我在 smnet (spidermonkey-dotnet) 上做了很多工作。近年来。它的主要开发重点是将.net 对象无缝嵌入到spidermonkey 引擎中。它支持从 js 值到 .net 对象的各种转换。其中一些包括代表和活动。

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.

只是说现在可能值得检查一下,因为它有一些稳定的发展:)。我确实通过错误修复和新功能使 SVN 存储库保持最新状态。源和项目解决方案文件配置为在下载时成功构建。如果在使用中遇到任何问题,请随时进行讨论。

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 =)

我确实理解拥有托管 javascript 解决方案的愿望,但是在我使用的所有托管 javascript 中,它们都非常缺乏一些有助于使它们既健壮又易于使用的关键功能。我自己正在等待 IronJS 成熟一点。在我等待的时候,我玩蜘蛛猴-dotnet 玩得很开心 =)

spidermonkey-dotnet project and download page

Spidermonkey-dotnet 项目和下载页面

Edit: created documentation wiki page this afternoon.

编辑:今天下午创建了文档维基页面。