当前位置: 首页 > news >正文

C#和JS交互之Microsoft.ClearScript.V8(V8引擎)

之前测试了很多JS引擎,都只支持es5语法,不支持执行es6,测试了下微软的V8反正能跑通,应该是支持的。还得是微软呀。

如图:安装相关包:
在这里插入图片描述
这是参考的官方V8代码

using Microsoft.ClearScript.JavaScript;
using Microsoft.ClearScript.V8;
using Microsoft.ClearScript;
using System.Security.Cryptography;using (var engine = new V8ScriptEngine()){// 指定JavaScript文件路径string filePath = @"D:\test\CallJS\CallJS\test.js";// 读取JavaScript文件内容string javascriptCode = File.ReadAllText(filePath);// expose a host typeengine.Execute("var window = this;");engine.AddHostType("Console", typeof(Console));var type = "mp3";var mid = 440613;var para = $"corp=kuwo&p2p=1&type=convert_url2&sig=0&format={type}&rid={mid}";string str = "Console.WriteLine(encryptQuery('112233'))";string str1 = "Console.WriteLine(encryptQuery('para'))".Replace("para", para);engine.Execute(javascriptCode + C#有偿群:927860652);engine.Execute(javascriptCode + str1);engine.Execute("Console.WriteLine('{0} is an interesting number.', Math.PI)");// expose a host objectengine.AddHostObject("random", new Random());engine.Execute("Console.WriteLine(random.NextDouble())");// expose entire assembliesengine.AddHostObject("lib", new HostTypeCollection("mscorlib", "System.Core"));engine.Execute("Console.WriteLine(lib.System.DateTime.Now)");// create a host object from scriptengine.Execute(@"birthday = new lib.System.DateTime(2007, 5, 22);Console.WriteLine(birthday.ToLongDateString());");// use a generic class from scriptengine.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 parameterengine.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 arrayengine.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 delegateengine.Execute(@"Filter = lib.System.Func(lib.System.Int32, lib.System.Boolean);oddFilter = new Filter(function(value) {return (value & 1) ? true : false;});");// use LINQ from scriptengine.Execute(@"oddNumbers = numbers.Where(oddFilter);Console.WriteLine(lib.System.String.Join(', ', oddNumbers));");// use a dynamic host objectengine.Execute(@"expando = new lib.System.Dynamic.ExpandoObject();expando.foo = 123;expando.bar = 'qux';delete expando.foo;");// call a script functionengine.Execute("function print(x) { Console.WriteLine(x); }");engine.Script.print(DateTime.Now.DayOfWeek);// examine a script objectengine.Execute("person = { name: 'Fred', age: 5 }");Console.WriteLine(engine.Script.person.name);// read a JavaScript typed arrayengine.Execute("values = new Int32Array([1, 2, 3, 4, 5])");var values = (ITypedArray<int>)engine.Script.values;Console.WriteLine(string.Join(", ", values.ToArray()));}

稍微看下官网案例,就知道怎么用了。

再记录下es6转es5步骤:

1.npm执行以下命令:
npm install -g babel-cli 安装babel

2.查看版本 babel --version

3.初始化项目
执行npm init -y

4.安装转换包 npm install --save-dev babel-preset-es2015

5.转为新es5语法文件 babel src -d dist

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
记得把要转换的文件放在项目根目录,我把js文件放在根目录下的src文件夹中,然后创建.babelrc配置文件在根目录中
内容如下:

{"presets": ["es2015"],"plugins": []
}

在这里插入图片描述

然后会在根目录生成新的dist文件夹,es5版本的JS在里面。

http://www.lryc.cn/news/190536.html

相关文章:

  • 9月活动回顾(免费领取PPT)|火山引擎DataLeap、ByteHouse多位专家带来DataOps、实时计算等前沿技术分享!
  • salesforce的按钮执行js代码如何链接到apex代码
  • C语言 —— 操作符
  • 物联网AI MicroPython传感器学习 之 CCS811空气质量检测传感器
  • TCP/IP(十五)拥塞控制
  • vue3 404解决方法
  • Unity中使用Xlua调用lua相关
  • 基于http的protobuf服务实现
  • 基于uniapp的商城外卖小程序
  • 【CSS】Tailwind CSS
  • leetcode-电话号码组合(C CODE)
  • Leetcode92. 反转链表 II
  • 【算法作业记录】
  • 回归预测、分类预测、时间序列预测 都有什么区别?
  • 关于网络协议的若干问题(三)
  • 办公室人人在用的iTab桌面真的好用吗?
  • 循环中的else语句
  • 三.镜头知识之FOV
  • 分布式事务入门
  • Ubuntu的中文乱码问题
  • [GXYCTF2019]Ping Ping Ping - RCE(空格、关键字绕过[3种方式])
  • ceph 分布式存储与部署
  • Go 结构体深度探索:从基础到应用
  • 分布式系统开发技术中的CAP定理原理
  • Mysql 报错 You can‘t specify target table ‘表名‘ for update in FROM clause
  • 【DevOps】DevOps—基本概念
  • 发行版兴趣小组季度动态:Anolis OS 支持大热 AI 软件栈,引入社区合作安全修复流程
  • android app开发环境搭建
  • oracle入门笔记一
  • linux下安装ffmpeg的详细教程、ffmpeg is not installed