From 5894436675467bb2e5e12072f3cc7b0c43858fe1 Mon Sep 17 00:00:00 2001 From: MysticBoy Date: Thu, 7 Oct 2021 00:45:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=8D=A2lua=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E8=A7=A3=E9=87=8A=E5=99=A8=E7=BB=A7=E7=BB=AD=E5=B0=9D=E8=AF=95?= =?UTF-8?q?=E3=80=82=20=E5=88=A0=E9=99=A4=E5=A4=9A=E4=BD=99=E7=9A=84?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E3=80=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IoTSharp.Interpreter.JavaScript.csproj | 11 ---- .../JavaScriptEngine.cs | 50 ------------------- .../IoTSharp.Interpreter.csproj | 2 +- IoTSharp.Interpreter/JavaScriptEngine.cs | 2 +- IoTSharp.Interpreter/LuaScriptEngine.cs | 17 +++---- IoTSharp.Test/ScriptEngineTest.cs | 1 - 6 files changed, 10 insertions(+), 73 deletions(-) delete mode 100644 IoTSharp.Interpreter.JavaScript/IoTSharp.Interpreter.JavaScript.csproj delete mode 100644 IoTSharp.Interpreter.JavaScript/JavaScriptEngine.cs diff --git a/IoTSharp.Interpreter.JavaScript/IoTSharp.Interpreter.JavaScript.csproj b/IoTSharp.Interpreter.JavaScript/IoTSharp.Interpreter.JavaScript.csproj deleted file mode 100644 index 9177970c..00000000 --- a/IoTSharp.Interpreter.JavaScript/IoTSharp.Interpreter.JavaScript.csproj +++ /dev/null @@ -1,11 +0,0 @@ - - - - net5.0 - - - - - - - diff --git a/IoTSharp.Interpreter.JavaScript/JavaScriptEngine.cs b/IoTSharp.Interpreter.JavaScript/JavaScriptEngine.cs deleted file mode 100644 index 35bea3ee..00000000 --- a/IoTSharp.Interpreter.JavaScript/JavaScriptEngine.cs +++ /dev/null @@ -1,50 +0,0 @@ -using Jint; -using Jint.Native; -using Jint.Native.Json; -using Microsoft.Extensions.Logging; -using System; -using System.Threading; -using IoTSharp.Interpreter; -namespace IoTSharp.Interpreter.JavaScript -{ - public class JavaScriptEngine - { - private readonly CancellationToken _cancellationToken; - private readonly Engine _engine; - private readonly JsonParser _parser; - private readonly ILogger _logger; - private readonly JsonSerializer _serializer; - private string _source; - - public JavaScriptEngine(ILogger logger, EngineSetting setting, CancellationToken cancellationToken) - { - _cancellationToken = cancellationToken; - var engine = new Engine(options => - { - - // Limit memory allocations to MB - options.LimitMemory(4_000_000); - - // Set a timeout to 4 seconds. - options.TimeoutInterval(TimeSpan.FromSeconds(setting.Timeout)); - - // Set limit of 1000 executed statements. - // options.MaxStatements(1000); - // Use a cancellation token. - options.CancellationToken(_cancellationToken); - }); - _engine = engine; - _parser = new JsonParser(_engine); - _logger = logger; - } - - public string Do(string _source,string input) - { - var js = _engine.SetValue("input",_parser.Parse(input)).Evaluate(_source).ToObject(); - var json= System.Text.Json.JsonSerializer.Serialize(js); - _logger.LogDebug($"source:{Environment.NewLine}{ _source}{Environment.NewLine}{Environment.NewLine}input:{Environment.NewLine}{ input}{Environment.NewLine}{Environment.NewLine} ouput:{Environment.NewLine}{ json}{Environment.NewLine}{Environment.NewLine}"); - return json; - } - } -} - diff --git a/IoTSharp.Interpreter/IoTSharp.Interpreter.csproj b/IoTSharp.Interpreter/IoTSharp.Interpreter.csproj index 66c9a70f..78a66301 100644 --- a/IoTSharp.Interpreter/IoTSharp.Interpreter.csproj +++ b/IoTSharp.Interpreter/IoTSharp.Interpreter.csproj @@ -7,7 +7,7 @@ - + diff --git a/IoTSharp.Interpreter/JavaScriptEngine.cs b/IoTSharp.Interpreter/JavaScriptEngine.cs index 84d49d5f..9eaf1b4d 100644 --- a/IoTSharp.Interpreter/JavaScriptEngine.cs +++ b/IoTSharp.Interpreter/JavaScriptEngine.cs @@ -7,7 +7,7 @@ using System.Threading; using IoTSharp.Interpreter; using Esprima; -namespace IoTSharp.Interpreter.JavaScript +namespace IoTSharp.Interpreter { public class JavaScriptEngine: ScriptEngineBase { diff --git a/IoTSharp.Interpreter/LuaScriptEngine.cs b/IoTSharp.Interpreter/LuaScriptEngine.cs index 4bc12d9f..d03247dd 100644 --- a/IoTSharp.Interpreter/LuaScriptEngine.cs +++ b/IoTSharp.Interpreter/LuaScriptEngine.cs @@ -1,6 +1,6 @@ using Microsoft.Extensions.Logging; using Microsoft.Scripting.Hosting; -using MoonSharp.Interpreter; +using Neo.IronLua; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using System; @@ -11,22 +11,21 @@ namespace IoTSharp.Interpreter { public class LuaScriptEngine : ScriptEngineBase { - private Script _engine; + private Lua _engine; + private LuaGlobal env; public LuaScriptEngine(ILogger logger, EngineSetting setting, CancellationToken cancellationToken) : base(logger, setting, cancellationToken) { - _engine = new Script(); + _engine = new Lua(); + env = _engine.CreateEnvironment(); // Create a environment } public override string Do(string _source, string input) { var expConverter = new ExpandoObjectConverter(); dynamic obj = JsonConvert.DeserializeObject(input, expConverter); - UserData.RegisterExtensionType(obj.GetType()); - UserData.RegisterType(obj.GetType()); - DynValue dvobj = UserData.Create(obj); - _engine.Globals.Set("input", dvobj); - DynValue res = _engine.DoString(_source); - var outputjson = res.ToObject().ToString(); + env["input"] = obj; + var res = env.DoChunk(_source,"lua_iotsharp"); + var outputjson = res.ToString(); return outputjson; } } diff --git a/IoTSharp.Test/ScriptEngineTest.cs b/IoTSharp.Test/ScriptEngineTest.cs index dc251d4b..4eb35d43 100644 --- a/IoTSharp.Test/ScriptEngineTest.cs +++ b/IoTSharp.Test/ScriptEngineTest.cs @@ -1,5 +1,4 @@ using IoTSharp.Interpreter; -using IoTSharp.Interpreter.JavaScript; using Microsoft.Extensions.Logging; using Microsoft.VisualStudio.TestTools.UnitTesting; using System; -- GitLab