Jurassic.ScriptEngine是一个让net动态执行js的一个引擎。类似的有ironjs等。支持ECMAScript 5,非线程安全
使用
- using Jurassic;
//1简单的执行js字符串
js:
function main(a,b)
{
return a+b;
}
c#
var engine = new Jurassic.ScriptEngine(); engine.Evaluate("上面的js代码");// var addResult= engine.CallGlobalFunction(" main", 5, 6);//结果11
//2加载 js库文件然后执行 js函数
1 2 3 |
var engine = new Jurassic.ScriptEngine(); engine.ExecuteFile(@"underscore-min.js"); tb.Text = engine.Evaluate(" _.first([5, 4, 3, 2, 1]);").ToString(); |
// 先加载js文件 然后执行js库的函数
//3 设置全局变量
engine.SetGlobalValue("interop", 15);
//4获取全局变量
engine.GetGlobalValue<int>("interop")
//5 从js中调用net的方法
engine.SetGlobalFunction("test", new Func<int, int, int>((a, b) => a + b));//设置js全局函数test
engine.Evaluate<int>("test(5, 6)") //调用js函数test
调用的时候注意js和net的类型对应关系
C# type name .NET type name JavaScript type name
bool System.Boolean boolean
int System.Int32 number
double System.Double number
string System.String string
Jurassic.Null Jurassic.Null null
Jurassic.Undefined Jurassic.Undefined undefined
Jurassic.Library.ObjectInstance (or a derived type) Jurassic.Library.ObjectInstance (or a derived type) object
//6 net调用js的方法
engine.Evaluate("function test(a, b) { return a + b }");
engine.CallGlobalFunction<int>("test", 5, 6);
//7 暴露net的class给js
using Jurassic; using Jurassic.Library; public class AppInfo : ObjectInstance { public AppInfo(ScriptEngine engine) : base(engine) { // 重写name属性
this["name"] = "Test Application"; // 只读属性
this.DefineProperty("version", new PropertyDescriptor(5, PropertyAttributes.Sealed), true); } }
engine.SetGlobalValue("appInfo", new AppInfo(engine)); Console.WriteLine(engine.Evaluate<string>("appInfo.name + ' ' + appInfo.version"));
// 8 暴露net的class的静态方法
using Jurassic; using Jurassic.Library; public class Math2 : ObjectInstance { public Math2(ScriptEngine engine) : base(engine) { this.PopulateFunctions(); } [JSFunction(Name = "log10")] public static double Log10(double num) { return Math.Log10(num); } }
engine.SetGlobalValue("math2", new Math2(engine)); engine.Evaluate<double>("math2.log10(1000)");
// 9 暴露net的类实例
using Jurassic; using Jurassic.Library; public class RandomConstructor : ClrFunction { public RandomConstructor(ScriptEngine engine) : base(engine.Function.InstancePrototype, "Random", new RandomInstance(engine.Object.InstancePrototype)) { } [JSConstructorFunction] public RandomInstance Construct(int seed) { return new RandomInstance(this.InstancePrototype, seed); } } public class RandomInstance : ObjectInstance { private Random random; public RandomInstance(ObjectInstance prototype) : base(prototype) { this.PopulateFunctions(); this.random = new Random(0); } public RandomInstance(ObjectInstance prototype, int seed) : base(prototype) { this.random = new Random(seed); } [JSFunction(Name = "nextDouble")] public double NextDouble() { return this.random.NextDouble(); } }
engine.SetGlobalValue("Random", new RandomConstructor(engine)); engine.Evaluate<double>("var rand = new Random(1000); rand.nextDouble()");
备注: 如果同cs-script配合使用,就可以同时动态执行js和net的cs代码,互相调用。