Skip to content

Commit

Permalink
fixed Issue #13 Attempting to terminate infinite loop
Browse files Browse the repository at this point in the history
fixed Issue #14 Null reference exception may be thrown if debug is not enabled
  • Loading branch information
JohnMasen committed Apr 24, 2018
1 parent 70b71e5 commit abd8ac6
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 6 deletions.
12 changes: 8 additions & 4 deletions source/ChakraCore.NET.Core/Service/RuntimeDebuggingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,12 @@ public void Step(JavaScriptDiagStepType stepType)

private class TaskQueueRunner
{
private BlockingCollection<Action> taskQueue =null;
public void StartProcess()
private BlockingCollection<Action> taskQueue = null;
private AutoResetEvent processASE = new AutoResetEvent(false);
public void StartProcess(BlockingCollection<Action> queue)
{
foreach (var item in taskQueue.GetConsumingEnumerable())
processASE.Set();
foreach (var item in queue.GetConsumingEnumerable())
{
item();
}
Expand Down Expand Up @@ -294,6 +296,7 @@ public T RunTask<T>(Func<T> func)

public void StopProcess()
{
processASE.WaitOne();
taskQueue.CompleteAdding();
taskQueue = null;
}
Expand All @@ -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
}

}
Expand Down
9 changes: 8 additions & 1 deletion source/ChakraCore.NET.Core/Service/RuntimeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ public interface IRuntimeService:IService,IDisposable
void CollectGarbage();

/// <summary>
/// Obsolete: Use Diabled property instead.
/// Stop running script without clean the context
/// </summary>
[Obsolete]
void TerminateRuningScript();

/// <summary>
/// Indicates if the runtime is able to execute script
/// </summary>
bool Disabled { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<TargetFramework>netstandard1.4</TargetFramework>
<PublishDir>..\..\NugetPackages</PublishDir>
</PropertyGroup>
</Project>
21 changes: 20 additions & 1 deletion source/ChakraCore.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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}
Expand Down
25 changes: 25 additions & 0 deletions source/PauseEngine/PauseEngine.sln
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions source/PauseEngine/PauseEngine/EchoFunctionProvider.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
16 changes: 16 additions & 0 deletions source/PauseEngine/PauseEngine/InterruptableHost.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
20 changes: 20 additions & 0 deletions source/PauseEngine/PauseEngine/PauseEngine.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\ChakraCore.NET.Hosting\ChakraCore.NET.Hosting.csproj" />
<ProjectReference Include="..\..\ChakraCore.NET\ChakraCore.NET.csproj" />
<ProjectReference Include="..\..\Plugins\ChakraCore.NET.Plugin.Common\ChakraCore.NET.Plugin.Common.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="Scripts\app.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
51 changes: 51 additions & 0 deletions source/PauseEngine/PauseEngine/Program.cs
Original file line number Diff line number Diff line change
@@ -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<IRuntimeService>();
StopEngine(runtimeService);
var switchService = app.ServiceNode.GetService<IContextSwitchService>();
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");
}
}
}
21 changes: 21 additions & 0 deletions source/PauseEngine/PauseEngine/Scripts/app.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit abd8ac6

Please sign in to comment.