From abd8ac65c683ff08af2962c02d8ebddea04fef0c Mon Sep 17 00:00:00 2001 From: QianFan Jiang Date: Tue, 24 Apr 2018 20:38:22 +0800 Subject: [PATCH] fixed Issue #13 Attempting to terminate infinite loop fixed Issue #14 Null reference exception may be thrown if debug is not enabled --- .../Service/RuntimeDebuggingService.cs | 12 +++-- .../Service/RuntimeService.cs | 9 +++- .../ServiceInterface/IRuntimeService.cs | 7 +++ .../PublishProfiles/FolderProfile.pubxml | 12 +++++ source/ChakraCore.NET.sln | 21 +++++++- source/PauseEngine/PauseEngine.sln | 25 +++++++++ .../PauseEngine/EchoFunctionProvider.cs | 14 +++++ .../PauseEngine/InterruptableHost.cs | 16 ++++++ .../PauseEngine/PauseEngine.csproj | 20 ++++++++ source/PauseEngine/PauseEngine/Program.cs | 51 +++++++++++++++++++ source/PauseEngine/PauseEngine/Scripts/app.js | 21 ++++++++ 11 files changed, 202 insertions(+), 6 deletions(-) create mode 100644 source/ChakraCore.NET.DebugAdapter.VSCode/Properties/PublishProfiles/FolderProfile.pubxml create mode 100644 source/PauseEngine/PauseEngine.sln create mode 100644 source/PauseEngine/PauseEngine/EchoFunctionProvider.cs create mode 100644 source/PauseEngine/PauseEngine/InterruptableHost.cs create mode 100644 source/PauseEngine/PauseEngine/PauseEngine.csproj create mode 100644 source/PauseEngine/PauseEngine/Program.cs create mode 100644 source/PauseEngine/PauseEngine/Scripts/app.js diff --git a/source/ChakraCore.NET.Core/Service/RuntimeDebuggingService.cs b/source/ChakraCore.NET.Core/Service/RuntimeDebuggingService.cs index ef5d17a..f8cd20c 100644 --- a/source/ChakraCore.NET.Core/Service/RuntimeDebuggingService.cs +++ b/source/ChakraCore.NET.Core/Service/RuntimeDebuggingService.cs @@ -261,10 +261,12 @@ public void Step(JavaScriptDiagStepType stepType) private class TaskQueueRunner { - private BlockingCollection taskQueue =null; - public void StartProcess() + private BlockingCollection taskQueue = null; + private AutoResetEvent processASE = new AutoResetEvent(false); + public void StartProcess(BlockingCollection queue) { - foreach (var item in taskQueue.GetConsumingEnumerable()) + processASE.Set(); + foreach (var item in queue.GetConsumingEnumerable()) { item(); } @@ -294,6 +296,7 @@ public T RunTask(Func func) public void StopProcess() { + processASE.WaitOne(); taskQueue.CompleteAdding(); taskQueue = null; } @@ -304,9 +307,10 @@ public void With(Action action) Task.Factory.StartNew(() => { action();//run the action on new thread + StopProcess(); }); - StartProcess();//start process at current thread + StartProcess(taskQueue);//start process at current thread } } diff --git a/source/ChakraCore.NET.Core/Service/RuntimeService.cs b/source/ChakraCore.NET.Core/Service/RuntimeService.cs index 7f1cecf..ed0f2cf 100644 --- a/source/ChakraCore.NET.Core/Service/RuntimeService.cs +++ b/source/ChakraCore.NET.Core/Service/RuntimeService.cs @@ -32,7 +32,14 @@ public void Dispose() public void TerminateRuningScript() { runtime.Disabled = true; - runtime.Disabled = false; } + + + public bool Disabled + { + get { return runtime.Disabled; } + set { runtime.Disabled = value; } + } + } } diff --git a/source/ChakraCore.NET.Core/ServiceInterface/IRuntimeService.cs b/source/ChakraCore.NET.Core/ServiceInterface/IRuntimeService.cs index 0ec2c60..77c5db8 100644 --- a/source/ChakraCore.NET.Core/ServiceInterface/IRuntimeService.cs +++ b/source/ChakraCore.NET.Core/ServiceInterface/IRuntimeService.cs @@ -17,8 +17,15 @@ public interface IRuntimeService:IService,IDisposable void CollectGarbage(); /// + /// Obsolete: Use Diabled property instead. /// Stop running script without clean the context /// + [Obsolete] void TerminateRuningScript(); + + /// + /// Indicates if the runtime is able to execute script + /// + bool Disabled { get; set; } } } diff --git a/source/ChakraCore.NET.DebugAdapter.VSCode/Properties/PublishProfiles/FolderProfile.pubxml b/source/ChakraCore.NET.DebugAdapter.VSCode/Properties/PublishProfiles/FolderProfile.pubxml new file mode 100644 index 0000000..207f86a --- /dev/null +++ b/source/ChakraCore.NET.DebugAdapter.VSCode/Properties/PublishProfiles/FolderProfile.pubxml @@ -0,0 +1,12 @@ + + + + + FileSystem + Release + netstandard1.4 + ..\..\NugetPackages + + \ No newline at end of file diff --git a/source/ChakraCore.NET.sln b/source/ChakraCore.NET.sln index 42d2a13..9d40853 100644 --- a/source/ChakraCore.NET.sln +++ b/source/ChakraCore.NET.sln @@ -27,7 +27,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChakraCore.NET.Plugin.Commo EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChakraCore.NET.Hosting", "ChakraCore.NET.Hosting\ChakraCore.NET.Hosting.csproj", "{6F8AD272-1670-4086-9C5B-43B50CA83D16}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChakraCore.NET.DebugAdapter.VSCode", "ChakraCore.NET.DebugAdapter.VSCode\ChakraCore.NET.DebugAdapter.VSCode.csproj", "{70E4E76C-443B-4EA8-9929-61B21B6765B6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChakraCore.NET.DebugAdapter.VSCode", "ChakraCore.NET.DebugAdapter.VSCode\ChakraCore.NET.DebugAdapter.VSCode.csproj", "{70E4E76C-443B-4EA8-9929-61B21B6765B6}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PauseEngine", "PauseEngine\PauseEngine\PauseEngine.csproj", "{65359582-6F5F-4C52-8FEA-B45EB341C30F}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -217,6 +219,22 @@ Global {70E4E76C-443B-4EA8-9929-61B21B6765B6}.Release|x64.Build.0 = Release|Any CPU {70E4E76C-443B-4EA8-9929-61B21B6765B6}.Release|x86.ActiveCfg = Release|Any CPU {70E4E76C-443B-4EA8-9929-61B21B6765B6}.Release|x86.Build.0 = Release|Any CPU + {65359582-6F5F-4C52-8FEA-B45EB341C30F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {65359582-6F5F-4C52-8FEA-B45EB341C30F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {65359582-6F5F-4C52-8FEA-B45EB341C30F}.Debug|ARM.ActiveCfg = Debug|Any CPU + {65359582-6F5F-4C52-8FEA-B45EB341C30F}.Debug|ARM.Build.0 = Debug|Any CPU + {65359582-6F5F-4C52-8FEA-B45EB341C30F}.Debug|x64.ActiveCfg = Debug|Any CPU + {65359582-6F5F-4C52-8FEA-B45EB341C30F}.Debug|x64.Build.0 = Debug|Any CPU + {65359582-6F5F-4C52-8FEA-B45EB341C30F}.Debug|x86.ActiveCfg = Debug|Any CPU + {65359582-6F5F-4C52-8FEA-B45EB341C30F}.Debug|x86.Build.0 = Debug|Any CPU + {65359582-6F5F-4C52-8FEA-B45EB341C30F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {65359582-6F5F-4C52-8FEA-B45EB341C30F}.Release|Any CPU.Build.0 = Release|Any CPU + {65359582-6F5F-4C52-8FEA-B45EB341C30F}.Release|ARM.ActiveCfg = Release|Any CPU + {65359582-6F5F-4C52-8FEA-B45EB341C30F}.Release|ARM.Build.0 = Release|Any CPU + {65359582-6F5F-4C52-8FEA-B45EB341C30F}.Release|x64.ActiveCfg = Release|Any CPU + {65359582-6F5F-4C52-8FEA-B45EB341C30F}.Release|x64.Build.0 = Release|Any CPU + {65359582-6F5F-4C52-8FEA-B45EB341C30F}.Release|x86.ActiveCfg = Release|Any CPU + {65359582-6F5F-4C52-8FEA-B45EB341C30F}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -225,6 +243,7 @@ Global {C5301EA3-37CA-4E8E-BF1C-BA2E36B3B7B1} = {D6A837FB-16B8-4DF3-953F-8CD0B14C9197} {C6443206-4100-4FE1-9870-34D0D664BB21} = {D37DBE6E-6034-40D2-BE90-40E78616CA0C} {AC7EE6C3-426B-40E3-920A-9DB2C0477830} = {D37DBE6E-6034-40D2-BE90-40E78616CA0C} + {65359582-6F5F-4C52-8FEA-B45EB341C30F} = {D6A837FB-16B8-4DF3-953F-8CD0B14C9197} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {F9866644-699C-4DCC-B947-E172EA716659} diff --git a/source/PauseEngine/PauseEngine.sln b/source/PauseEngine/PauseEngine.sln new file mode 100644 index 0000000..9bae8ad --- /dev/null +++ b/source/PauseEngine/PauseEngine.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27428.2015 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PauseEngine", "PauseEngine\PauseEngine.csproj", "{E94F4ACE-E74E-4508-B744-834C9A20C9C9}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E94F4ACE-E74E-4508-B744-834C9A20C9C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E94F4ACE-E74E-4508-B744-834C9A20C9C9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E94F4ACE-E74E-4508-B744-834C9A20C9C9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E94F4ACE-E74E-4508-B744-834C9A20C9C9}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {8210E508-804A-40F9-AD25-488AF5CAF9FF} + EndGlobalSection +EndGlobal diff --git a/source/PauseEngine/PauseEngine/EchoFunctionProvider.cs b/source/PauseEngine/PauseEngine/EchoFunctionProvider.cs new file mode 100644 index 0000000..b22ef6d --- /dev/null +++ b/source/PauseEngine/PauseEngine/EchoFunctionProvider.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace PauseEngine +{ + class EchoFunctionProvider : ChakraCore.NET.Plugin.Common.IEchoFunctionProvider + { + public void Echo(string message) + { + Console.WriteLine(message); + } + } +} diff --git a/source/PauseEngine/PauseEngine/InterruptableHost.cs b/source/PauseEngine/PauseEngine/InterruptableHost.cs new file mode 100644 index 0000000..25b78a6 --- /dev/null +++ b/source/PauseEngine/PauseEngine/InterruptableHost.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; +using ChakraCore.NET; +using ChakraCore.NET.API; + +namespace PauseEngine +{ + class InterruptableHost:ChakraCore.NET.Hosting.JavaScriptHosting + { + protected override ChakraRuntime createRuntime() + { + return ChakraRuntime.Create(JavaScriptRuntimeAttributes.AllowScriptInterrupt); + } + } +} diff --git a/source/PauseEngine/PauseEngine/PauseEngine.csproj b/source/PauseEngine/PauseEngine/PauseEngine.csproj new file mode 100644 index 0000000..064250e --- /dev/null +++ b/source/PauseEngine/PauseEngine/PauseEngine.csproj @@ -0,0 +1,20 @@ + + + + Exe + netcoreapp2.0 + + + + + + + + + + + PreserveNewest + + + + diff --git a/source/PauseEngine/PauseEngine/Program.cs b/source/PauseEngine/PauseEngine/Program.cs new file mode 100644 index 0000000..60c8423 --- /dev/null +++ b/source/PauseEngine/PauseEngine/Program.cs @@ -0,0 +1,51 @@ +using ChakraCore.NET; +using ChakraCore.NET.Hosting; +using ChakraCore.NET.Plugin.Common; +using System; +using System.Threading.Tasks; + +namespace PauseEngine +{ + class Program + { + static EchoProvider echo = new EchoProvider(new EchoFunctionProvider()); + static void Main(string[] args) + { + JavaScriptHostingConfig config = new JavaScriptHostingConfig(); + config.AddModuleFolder("Scripts"); + config.AddPlugin(echo); + + InterruptableHost host = new InterruptableHost(); + var app=host.GetModuleClass("app", "app", config); + var runtimeService = app.ServiceNode.GetService(); + StopEngine(runtimeService); + var switchService = app.ServiceNode.GetService(); + try + { + app.CallMethod("loop"); + } + catch (Exception ex) when( ex.Message== "Script was terminated.") + { + //switchService.Leave(); + Console.WriteLine("Script terminated"); + runtimeService.Disabled = false; + Console.WriteLine("Engine Enabled"); + + } + catch(Exception) + { + throw; + } + //GC.KeepAlive(host); + Console.Read(); + + } + private static async void StopEngine(IRuntimeService service) + { + await Task.Delay(3000); + Console.WriteLine("Trying to stop engine"); + service.Disabled = true; + Console.WriteLine("Engine Disabled"); + } + } +} diff --git a/source/PauseEngine/PauseEngine/Scripts/app.js b/source/PauseEngine/PauseEngine/Scripts/app.js new file mode 100644 index 0000000..da1d25c --- /dev/null +++ b/source/PauseEngine/PauseEngine/Scripts/app.js @@ -0,0 +1,21 @@ +import { echo } from 'sdk@Echo'; +export class app { + loop() { + let count = 1; + while (true) { + + delay(1000); + echo("hello " + count); + count++; + } + } +} +function delay(ms) { + let last = (new Date()).getTime(); + while (true) { + let now = (new Date()).getTime(); + if (now-last>=ms) { + return; + } + } +} \ No newline at end of file