• Jurassic.ScriptEngine 使用


    标记: ,,

    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代码,互相调用。

    源项目地址

  • 相关阅读:
    Python Django :HTTP生命周期
    Docker简介及Centos 7 安装[初步配置]
    python:Django 简介。
    python :Django url /views /Template 文件介绍
    python:异常处理及程序调试
    python之正则表达式【re】
    Day 21 python :面向对象 类的相关内置函数 /单例模式 /描述符
    Day 22: 软件开发目录设计规范
    Day 20: 面向对象【多态,封装,反射】字符串模块导入/内置attr /包装 /授权
    SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)
  • 原文地址:https://www.cnblogs.com/wang2650/p/4811918.html
Copyright © 2020-2023  润新知