Jint打包后大概2M左右,但有一些小bug,比如函数内的严格模式不生效
ClearScript大概30M左右
测试基于windows x64 控制台程序
| <PackageReference Include="Microsoft.ClearScript.Core" Version="7.4.5" /> |
| <PackageReference Include="Microsoft.ClearScript.V8" Version="7.4.5" /> |
| <PackageReference Include="Microsoft.ClearScript.V8.Native.win-x64" Version="7.4.5" /> |
测试代码
| using Microsoft.ClearScript; |
| using Microsoft.ClearScript.JavaScript; |
| using Microsoft.ClearScript.V8; |
| |
| var api = new Api(); |
| var engine = new V8ScriptEngine(); |
| |
| engine.AddHostObject("__API", api); |
| |
| engine.AddHostType("Console", typeof(Console)); |
| engine.Execute("Console.WriteLine(__API.MyProperty);"); |
| |
| |
| engine.Execute(@" |
| function add(a,b){ |
| return a+b; |
| } |
| //定义一个常量 |
| let API = __API; |
| "); |
| Console.WriteLine($"调用add方法-->{engine.Invoke("add", 1, 2)}"); |
| Console.WriteLine($"访问API属性1-->{engine.Evaluate("API.MyProperty")}"); |
| Console.WriteLine($"访问API属性2-->{engine.Evaluate("API.NoProp")}"); |
| |
| |
| engine.Execute(@" |
| function test(api){ |
| return api.MyProperty + 99; |
| } |
| "); |
| Console.WriteLine(engine.Invoke("test", api)); |
| |
| |
| var proxyCode = @" |
| function __proxyObject(inputObject) { |
| let proxy = new Proxy(inputObject, { |
| get(targetObj, propoty, receiver) { |
| if (!targetObj.hasOwnProperty(propoty)) { |
| throw new Error(`'${targetObj}' 未包含属性: '${propoty}' `) |
| } |
| return targetObj[propoty] |
| } |
| }); |
| |
| return proxy; |
| } |
| |
| const API2 = new __proxyObject(__API); |
| "; |
| engine.Execute(proxyCode); |
| |
| |
| |
| |
| |
| |
| engine.DocumentSettings.SearchPath = @"C:\Users\pc\source\repos\ConsoleApp4\"; |
| engine.DocumentSettings.AccessFlags = DocumentAccessFlags.EnableFileLoading; |
| engine.Execute(new DocumentInfo() { Category = ModuleCategory.Standard }, |
| @" |
| import {obj} from './test.js' |
| Console.WriteLine(obj.a); |
| "); |
| |
| Console.Read(); |
| |
| |
| public class Api() |
| { |
| public int MyProperty { get; set; } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2020-06-18 Maxscript encryptFile 加密函数一些小提示