.net 开源 JavaScript 解析引擎
1. Javascript .NET
地址为:http://javascriptdotnet.codeplex.com/
使用方法:
Quick Start
This section provides documentation to get quickly started to embed and run Javascript .NET in your application.
Download Javascript .NET
Download the Javascript .NET latest binary release here. The contained Noesis.Javascript.dll assembly and the Microsoft Runtime Library files in the same directory are all that is required to run Javascript .NET.
Create a new project
Open Visual Studio and create a new Console application.
Reference the Noesis.Javascript assembly from your project
Add a reference to Noesis.Javascript.dll in your project by right clicking on the project and choosing "Add Reference", then select the "Browse" tab, locate the appropriate Noesis.Javascript.dll assembly and click "OK".
Using Noesis.Javascript
Add the following to the "using" statements at the top of you program file:
using Noesis.Javascript;
Initialize a Javascript context
JavascriptContext context = new JavascriptContext()
(Don't forget to Dispose() this object when you are finished with it.)
Set variables in the Javascript context's global scope: SetParameter(string iName, Object iObject);
context.SetParameter("console", new SystemConsole());
context.SetParameter("message", "Hello World !\n");
Get a parameter from the Javascript context: GetParameter(string iName);
context.GetParameter("number");
Compile and run the Javascript: Run(string iScript);
context.Run("var i; for (i = 0; i < 5; i++) console.Print(message + ' (' + i + ')'); number += i;");
"Hello World" quick integration sample
A simple integration is shown below. In the sample, a context is created to which a CLI object is supplied to expose the console to the Javascript. A string and an integer are also supplied on which elementary operations are performed. Finally, the resulting value of the supplied integer value is extracted and printed to the console.
Code
class Program
{
public class SystemConsole
{
public SystemConsole() { } public void Print(string iString)
{
Console.WriteLine(iString);
}
} static void Main(string[] args)
{
// Initialize the context
using (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"));
}
}
}
Output
The following will be outputed by the program above:
Hello World ! (0)
Hello World ! (1)
Hello World ! (2)
Hello World ! (3)
Hello World ! (4)
6 2. ClearScript
微软自家的地址为:http://clearscript.codeplex.com/
使用方法:
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));
"); // use a dynamic host object
engine.Execute(@"
expando = new lib.System.Dynamic.ExpandoObject();
expando.foo = 123;
expando.bar = 'qux';
delete expando.foo;
"); // 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);
}
.net 开源 JavaScript 解析引擎的更多相关文章
- javascript解析引擎(每天有学习一点篇)
======================================================= 有一段时间,经常耳闻web前端的福音,对高性能的V8议论纷纷. 其实对js解析引擎没有深 ...
- 高性能JavaScript模板引擎原理解析
随着 web 发展,前端应用变得越来越复杂,基于后端的 javascript(Node.js) 也开始崭露头角,此时 javascript 被寄予了更大的期望,与此同时 javascript MVC ...
- 高性能JavaScript模板引擎实现原理详解
这篇文章主要介绍了JavaScript模板引擎实现原理详解,本文着重讲解artTemplate模板的实现原理,它采用预编译方式让性能有了质的飞跃,是其它知名模板引擎的25.32 倍,需要的朋友可以参考 ...
- 20个免费的 JavaScript 游戏引擎分享给开发者
这篇文章收集了20个免费的 JavaScript 游戏引擎分享给开发者.这些游戏引擎能够帮助游戏开发人员更快速高效的开发出各种好玩的游戏. 使用 HTML5.CSS3 和 Javascript 可以帮 ...
- 20 款免费的 JavaScript 游戏引擎
使用 HTML5,CSS3 和 Javascript 可以帮助面向对象开发者开发拥有各种特性的游戏,比如:3D 动画效果,Canvas,数学,颜色,声音,WebGL 等等.最明显的优势在于使用 HTM ...
- JavaScript 模板引擎实现原理解析
1.入门实例 首先我们来看一个简单模板: <script type="template" id="template"> <h2> < ...
- HTML解析引擎:Jumony 开源项目
Jumony Core首先提供了一个近乎完美的HTML解析引擎,其解析结果无限逼近浏览器的解析结果.不论是无结束标签的元素,可选结束标签的元素,或是标记属性,或是CSS选择器和样式,一切合法的,不合法 ...
- 推荐13款javascript模板引擎
javaScript 在生成各种页面内容时如果能结合一些模板技术,可以让逻辑和数据之间更加清晰,本文介绍 X 款 JavaScript 的模板引擎.(排名不分先后顺序) 1. Mustache 基于j ...
- 各种JS模板引擎对比数据(高性能JavaScript模板引擎)
最近做了JS模板引擎测试,拿各个JS模板引擎在不同浏览器上去运行同一程序,下面是模板引擎测试数据:通过测试artTemplate.juicer与doT引擎模板整体性能要有绝对优势: js模板引擎 Ja ...
随机推荐
- BZOJ 2200 道路与航线
好厉害呀这道题,有种豁然开朗的感觉.... 按拓扑顺序跑最短路. 然后注意细节,像WA的代码犯下的错是一笔带过没有丝毫考虑的...然而就是错了. 考试的时候一定要拍啊. #include<ios ...
- PHP中常用正则表达式大全
常用正则表达式大全!(例如:匹配中文.匹配html) 匹配中文字符的正则表达式: [u4e00-u9fa5] 评注:匹配中文还真是个头疼的事,有了这个表达式就好办了 匹配双字节字符(包括汉字在内 ...
- PhP访问mysql数据库的基本方式
一,查询 <?php$conn= mysql_connect("127.0.0.1","lanou12","lanou12");$fi ...
- Asp登陆
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="QYLogin.aspx.c ...
- M1: 创建UWP空项目
本小节介绍如何在Visual Studio中创建一个UWP项目,Visual Studio中提供了快速创建各种项目的类型模板.在Visual Studio 2015中,同样提供了UWP项目模板. 学完 ...
- mysql重命名数据表
命令如下: rename table oldtablename to newtablename
- 1024PHP数组
<?php //定义数组//$attr = array();//$attr[0] = 1;//索引数组//$attr = array(1,2,3,4);//关联数组//$attr = array ...
- python发布文件(windows)
怎样发布文件 首先发布本地文件有一个好的用处,就是省去了朋友同import的时候还要使用sys.path,省的自己出错 1.新建文件夹d:\ tool 在的d:\tool文件夹中建立login.py ...
- WebRTC录音(1)-实现通话双向录音
最近公司的iPad项目中一个功能点涉及到了VOIP通讯中的录音,需要在已有的WebRTC引擎中增加录音功能,录制通话双方的声音参考了往上一位兄弟的博文(链接在此 http://blog.csdn.ne ...
- 软件工程课程作业(一)—20道随机四则运算题(C++)
一.编程思想: 1.定义所需要变量2.设置数组,存储运算符,3.通过随机函数random(0,100)找出运算数,random(0,4)找出运算符4.通过输出显示运算式. 二.源代码: //2016 ...