标记: Jurassic,js,net

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

源项目地址

Jurassic.ScriptEngine 使用的更多相关文章

  1. 利用Jurassic在.net下运行js函数

    static void Main(string[] args) { var eng = new Jurassic.ScriptEngine(); eng.Evaluate("function ...

  2. C#执行javascript代码,执行复杂的javascript代码新方式

    1. 使用nuget 包"Jurassic", 注意,如果 nuget上的包 用起来出现错误,请自行下载 github代码,自行编译最新代码成dll,再引用. 官方的nuget包 ...

  3. JS学习十四天----server端运行JS代码

    server端运行JS代码 话说,当今不在client使用JS代码才是稀罕事.因为web应用的体验越来越丰富,client用JS实现的逻辑也越来越多,这造成的结果就是某些差点儿一致的逻辑须要在clie ...

  4. 测试了几款 C# 脚本引擎 , Jint , Jurassic , Nlua, ClearScript

    测试类 public class Script_Common { public string read(string filename) { return System.IO.File.ReadAll ...

  5. UVALive - 2965 Jurassic Remains (LA)

    Jurassic Remains Time Limit: 18000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Sub ...

  6. java ScriptEngine 使用 (支持JavaScript脚本,eval()函数等)

    Java SE 6最引人注目的新功能之一就是内嵌了脚本支持.在默认情况下,Java SE 6只支持JavaScript,但这并不以为着Java SE 6只能支持JavaScript.在Java SE ...

  7. 用ScriptEngine在java中和javascript交互的例子(JDK6新特性)

    package demo7; import java.util.Arrays; import java.util.List; import javax.script.Invocable; import ...

  8. LA 2965 Jurassic Remains (中途相遇法)

    Jurassic Remains Paleontologists in Siberia have recently found a number of fragments of Jurassic pe ...

  9. 【中途相遇+二进制】【NEERC 2003】Jurassic Remains

    例题25  侏罗纪(Jurassic Remains, NEERC 2003, LA 2965) 给定n个大写字母组成的字符串.选择尽量多的串,使得每个大写字母都能出现偶数次. [输入格式] 输入包含 ...

随机推荐

  1. codeforce1029B B. Creating the Contest(简单dp,简单版单调栈)

    B. Creating the Contest time limit per test 1 second memory limit per test 256 megabytes input stand ...

  2. Bootstrap-Plugin:插件概览

    ylbtech-Bootstrap-Plugin:插件概览 1.返回顶部 1. Bootstrap 插件概览 在前面 布局组件 章节中所讨论到的组件仅仅是个开始.Bootstrap 自带 12 种 j ...

  3. 编译安装php-5.4.44

    编译安装php-5.4.44 1. 首先,安装必要的库文件,一面编译被打断: yum install -y gcc gcc-c++  make zlib zlib-devel pcre pcre-de ...

  4. django关于静态的信息的配置

    今天搭建完,django后,访问 admin 发现  样式没有加载 需要搭建静态的配置 1 设置项目目录的静态目录 用来存放静态的文件 在setttings.py中 添加以下参数  在 STATIC_ ...

  5. PowerDesigner软件的使用

    1. 报错:Could not Initialize JavaVM 的解决方案: powerDesigner不支持x64JDK,ok.安装32位. 仅仅是安装一下,不要做任何配置.......关闭po ...

  6. socket与http的区别

    ---------------------------------------------------------------------------------------------------- ...

  7. 「小程序JAVA实战」小程序的举报功能开发(68)

    转自:https://idig8.com/2018/09/25/xiaochengxujavashizhanxiaochengxudeweixinapicaidancaozuo66-2/ 通过点击举报 ...

  8. Nginx rewrite使用

    转自: https://www.cnblogs.com/czlun/articles/7010604.html

  9. eclipse双击变量高亮显示开关

    在eclipse/myeclipse中如果不小心把变量的高亮显示弄丢了.可真是件愁人的事,不过看到这你就不用愁了 windows->   preferences-> java-> E ...

  10. AOP术语

    1.连接点(Joinpoint) 程序执行的某个特定位置:如类开始初始化前,类初始化后,类某个方法调用前,调用后,方法跑出异常后.一个类或一段程序代码拥有一些具有边界性质的特定点.这些代码中的特定点就 ...