.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 ...
 
随机推荐
- css if hack之兼容ie
			
1.Css if hack条件语法< !--[if IE]> Only IE <![endif]-->仅所有的WIN系统自带IE可识别< !--[if IE 5.0]&g ...
 - IOS 作业项目 TableView两个section中cell置顶功能实现
			
点击cell会置顶,其他的下移
 - 30道四则运算<1>
			
#include<iostream> using namespace std; #define random()(rand()%100) class shuzi //shuzi类的功能是产 ...
 - Inno Setup入门(十一)——完成安装后执行某些程序
			
Inno Setup入门(十一)——完成安装后执行某些程序 2011-02-16 16:24:23| 分类: Inno Setup | 标签:inno setup |举报 |字号 订阅 ...
 - 重学STM32---(八)----SDIO
			
1. SDIO(SD/SDIO MMC卡主机模块)在AHB外设总线和多媒体卡(MMC).SD存储卡.SDIO卡和CE-ATA设备间提供了操作接口.(SDIO没有SPI兼容的通信模式 ) 1.1.什么是 ...
 - 故事板(Storyboard)
			
1 使用Storyboard完成各项常见功能 1.1 问题 故事板Storyboard是IOS5开始引入的一个新的系统,将多个视图文件(类似xib文件)集中到一个单独的可视化工作区间,负责创建和管理所 ...
 - LeetCode  Word Pattern (模拟)
			
题意: 给出一个模式串pattern,再给出一个串str,问str的模板是否是pattern. 思路: 注意点:只要对于所有pattern[i]相同的i,str中对应的所有words[i]也必须相同, ...
 - android定时更新文件
			
static变量在程序退出时不会清空的,除非系统内存不足以运行其他程序,才会清空.给SD卡上的文件过期时间.可以简单的在给文件命名时后面加个创建时间,在下次访问时判断是否需要更新.比如本来文件名是 i ...
 - cuffdiff 和 edgeR 对差异表达基因的描述
			
ASE又走到了关键的一步 要生成能决定是否有差异表达的table. 准备借鉴一下cuffdiff和edgeR 的结果 cuffdiff对差异表达基因的描述: 一共十四列: 第一列, test_id ...
 - 冒泡排序(python版)
			
实现源码 def bubble(array): flag = len(array)- : iter = for i in range(flag): ]: array[i], array[i+]= ar ...