.net 开源 JavaScript 解析引擎
2015-06-03 15:39
543 查看
1. Javascript .NET
地址为:http://javascriptdotnet.codeplex.com/
使用方法:
(Don't forget to Dispose() this object when you are finished with it.)
地址为: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); }
相关文章推荐
- JS setTimeout()学习笔记
- Chart.js报表制作
- JSON 数据列表取值
- JSONP
- JS简介
- 表单JS提交失效
- js操作json对象
- dom和json使用response 返回
- 解决json_encode中文UNICODE转码问题
- Js-加密
- JavaScript window.open()属性
- 由几道JS笔试题引发的知识点探究一——JS有哪些全局函数?
- 关于JSLink你不知道的五件事
- js中callback.call()和callback()的区别
- 细说javascript 中的 window.open() 参数设置
- js-函数eval
- js对dom对象的操作之select元素:js不能获取select对象
- 编码规范的自动化合规检查
- js动态生成input指定My97DatePicker时间问题
- Json-遍历