using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Unvell.ReoScript;
namespace BPASmartClient.Compiler
{
///
/// 编译器
/// 创建人:奉友福
/// 创建时间:20220824
///
public class Config
{
#region 单例模式
public static Config Instance = null;
public static Config GetInstance()
{
if (Instance == null)
{
Instance = new Config();
}
return Instance;
}
#endregion
public static ScriptRunningMachine srm { get; } = new ScriptRunningMachine();
public Config()
{
srm.WorkMode |=
// Enable DirectAccess
MachineWorkMode.AllowDirectAccess
// Ignore exceptions in CLR calling (by default)
| MachineWorkMode.IgnoreCLRExceptions
// Enable CLR Event Binding
| MachineWorkMode.AllowCLREventBind;
RegisterFunction();
}
///
/// 运行脚本
///
///
public void RunJsScipt(string script)
{
try
{
srm.Run(script);
}
catch (Exception e)
{
//MessageBox.Show(e.Message, "脚本错误");
}
}
///
/// 注册对象到js
///
public void SetVariable(string name, object obj)
{
srm.SetGlobalVariable(name, obj);
}
///
/// 注册方法到Js
///
private static void RegisterFunction()
{
srm["ShowMessage"] = new NativeFunctionObject("ShowMessage", (ctx, owner, args) =>
{
StringBuilder sb = new StringBuilder();
foreach (var item in args)
{
sb.Append(item.ToString());
}
//MessageBox.Show($"{sb}", "提示");
return null;
});
srm["SetICDValue"] = new NativeFunctionObject("SetICDValue", (ctx, owner, args) =>
{
//MessageBox.Show($"发送ICD数据", "提示");
return null;
});
}
}
}