From c3aec22691f04dd352f779aafa2f656d3e382a9f Mon Sep 17 00:00:00 2001 From: EvilBeaver Date: Fri, 3 Mar 2017 00:54:30 +0300 Subject: [PATCH 1/5] =?UTF-8?q?=D0=9D=D0=B5=20=D0=B8=D0=BD=D0=B8=D1=86?= =?UTF-8?q?=D0=B8=D0=B0=D0=BB=D0=B8=D0=B7=D0=B8=D1=80=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D1=81=D1=8C=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D0=B8?= =?UTF-8?q?,=20=D0=BF=D0=BE=D0=B4=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=BD=D1=8B=D0=B5=20=D0=B2=20=D1=86=D0=B5=D0=BF=D0=BE=D1=87?= =?UTF-8?q?=D0=BA=D0=B5=20=D0=97=D0=B0=D0=BF=D1=83=D1=81=D0=BA->=D0=9F?= =?UTF-8?q?=D0=BE=D0=B4=D0=BA=D0=BB=D1=8E=D1=87=D0=B8=D1=82=D1=8C=D0=A1?= =?UTF-8?q?=D1=86=D0=B5=D0=BD=D0=B0=D1=80=D0=B8=D0=B9->=D0=98=D1=81=D0=BF?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ScriptEngine.HostedScript/HostedScriptEngine.cs | 13 ++++++++++--- src/ScriptEngine/RuntimeEnvironment.cs | 11 +++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/ScriptEngine.HostedScript/HostedScriptEngine.cs b/src/ScriptEngine.HostedScript/HostedScriptEngine.cs index 7ae8f95c5..89e0fe871 100644 --- a/src/ScriptEngine.HostedScript/HostedScriptEngine.cs +++ b/src/ScriptEngine.HostedScript/HostedScriptEngine.cs @@ -219,6 +219,7 @@ private Process InitProcess(IHostApplication host, ref LoadedModuleHandle module { Initialize(); CompileDelayedModules(); + _env.EnvironmentChanged += LoadUserModuleAsProperty; var process = new Process(host, module, _engine); return process; @@ -229,12 +230,17 @@ private void CompileDelayedModules() var scripts = GetUserAddedScripts().Where(x => x.Type == UserAddedScriptType.Module); foreach (var script in scripts) { - var loaded = _engine.LoadModuleImage(script.Module); - var instance = (IValue)_engine.NewObject(loaded); - _env.SetGlobalProperty(script.Symbol, instance); + LoadUserModuleAsProperty(script); } } + private void LoadUserModuleAsProperty(UserAddedScript script) + { + var loaded = _engine.LoadModuleImage(script.Module); + var instance = (IValue) _engine.NewObject(loaded); + _env.SetGlobalProperty(script.Symbol, instance); + } + public void EnableCodeStatistics(string outputFileName) { _codeStat = new CodeStatProcessor(outputFileName); @@ -244,6 +250,7 @@ public void EnableCodeStatistics(string outputFileName) public void Finalize() { _codeStat?.OutputCodeStat(); + _env.EnvironmentChanged -= LoadUserModuleAsProperty; } } } diff --git a/src/ScriptEngine/RuntimeEnvironment.cs b/src/ScriptEngine/RuntimeEnvironment.cs index d44f5aab9..f8f199846 100644 --- a/src/ScriptEngine/RuntimeEnvironment.cs +++ b/src/ScriptEngine/RuntimeEnvironment.cs @@ -14,6 +14,8 @@ This Source Code Form is subject to the terms of the namespace ScriptEngine { + public delegate void EnvironmentChangedHandler(UserAddedScript script); + public class RuntimeEnvironment { private readonly List _objects = new List(); @@ -88,14 +90,19 @@ public void NotifyClassAdded(ScriptModuleHandle module, string symbol) public void NotifyModuleAdded(ScriptModuleHandle module, string symbol) { - _externalScripts.Add(new UserAddedScript() + var script = new UserAddedScript() { Type = UserAddedScriptType.Module, Symbol = symbol, Module = module - }); + }; + + _externalScripts.Add(script); + EnvironmentChanged?.Invoke(script); } + public event EnvironmentChangedHandler EnvironmentChanged; + public IEnumerable GetUserAddedScripts() { return _externalScripts; From 5094bb4730c5b0e6bc4e5900a5ddabf804477510 Mon Sep 17 00:00:00 2001 From: EvilBeaver Date: Tue, 7 Mar 2017 20:07:47 +0300 Subject: [PATCH 2/5] =?UTF-8?q?=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B1=D0=B8=D0=B1=D0=BB=D0=B8=D0=BE=D1=82?= =?UTF-8?q?=D0=B5=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- oscript-library | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oscript-library b/oscript-library index 06fb21aea..d161c1d91 160000 --- a/oscript-library +++ b/oscript-library @@ -1 +1 @@ -Subproject commit 06fb21aea24bbfedb238c9e23306121e8b441462 +Subproject commit d161c1d9152f8009b6850bee798e746fba6d5805 From b284f444f1cffec4d4b3f013492e238bd7dbb959 Mon Sep 17 00:00:00 2001 From: EvilBeaver Date: Thu, 9 Mar 2017 19:25:02 +0300 Subject: [PATCH 3/5] fixed #417 --- src/ScriptEngine.HostedScript/Library/ProcessContext.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ScriptEngine.HostedScript/Library/ProcessContext.cs b/src/ScriptEngine.HostedScript/Library/ProcessContext.cs index 73238ce4c..8c5ede759 100644 --- a/src/ScriptEngine.HostedScript/Library/ProcessContext.cs +++ b/src/ScriptEngine.HostedScript/Library/ProcessContext.cs @@ -120,11 +120,11 @@ public void Start() /// /// /// Пример правильной обработки цикла ожидания завершения процесса: - /// Процесс не завершается, пока любоой из потоков (stdout, stderr) открыт для чтения. + /// Процесс не завершается, пока любой из потоков (stdout, stderr) открыт для чтения. /// Процесс висит и ждет, пока его освободят от текста в обоих потоках. /// /// Пока НЕ Процесс.Завершен ИЛИ Процесс.ПотокВывода.ЕстьДанные ИЛИ Процесс.ПотокОшибок.ЕстьДанные Цикл - /// Если ПериодОпросаВМиллисекундах <> 0 Тогда + /// Если ПериодОпросаВМиллисекундах <> 0 Тогда /// Приостановить(ПериодОпросаВМиллисекундах); /// КонецЕсли; /// From 2a80a9ce79b7f72a3c39e2d8c239db2e4e4f77bf Mon Sep 17 00:00:00 2001 From: EvilBeaver Date: Fri, 10 Mar 2017 18:36:29 +0300 Subject: [PATCH 4/5] =?UTF-8?q?=D0=92=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD?= =?UTF-8?q?=D0=BE=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=20deadlock=20=D0=BD=D0=B0=20=D1=87=D1=82=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B8=20=D0=BF=D0=BE=D1=82=D0=BE=D0=BA=D0=BE=D0=B2=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D1=86=D0=B5=D1=81=D1=81=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Library/ProcessOutputWrapper.cs | 22 ++++++------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/ScriptEngine.HostedScript/Library/ProcessOutputWrapper.cs b/src/ScriptEngine.HostedScript/Library/ProcessOutputWrapper.cs index 0ef56c7f3..31ea948ef 100644 --- a/src/ScriptEngine.HostedScript/Library/ProcessOutputWrapper.cs +++ b/src/ScriptEngine.HostedScript/Library/ProcessOutputWrapper.cs @@ -15,9 +15,7 @@ class ProcessOutputWrapper : TextReader private OutputVariant _variant; private StringBuilder _buffer = new StringBuilder(4096); private ReaderWriterLockSlim _locker; - - private ManualResetEventSlim _finalWriteEvent = new ManualResetEventSlim(); - + private int _bufferIndex = 0; private bool AlreadyReading { get; set; } @@ -74,19 +72,13 @@ private void StreamDataReceived(object sender, sys.DataReceivedEventArgs e) { try { - _locker.EnterWriteLock(); + if (e.Data != null) { - if (e.Data != null) - { - if (_buffer.Length != 0) - _buffer.Append(System.Environment.NewLine); + _locker.EnterWriteLock(); + if (_buffer.Length != 0) + _buffer.Append(System.Environment.NewLine); - _buffer.Append(e.Data); - } - else - { - _finalWriteEvent.Set(); - } + _buffer.Append(e.Data); } } finally @@ -221,7 +213,7 @@ private void EnterReadLock() { if (_process.HasExited) { - _finalWriteEvent.Wait(); + _process.WaitForExit(); // ожидание закрытия потоков } _locker.EnterReadLock(); From 9cd77f6d9ee68d71c4717eb83c780d7a955494a0 Mon Sep 17 00:00:00 2001 From: EvilBeaver Date: Fri, 10 Mar 2017 19:11:58 +0300 Subject: [PATCH 5/5] =?UTF-8?q?=D0=A1=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=B0?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=B4=D0=BF=D0=B8=D1=81=D0=BA=D0=B0,=20=D0=BF?= =?UTF-8?q?=D0=BE=D1=82=D0=BE=D0=BC=20=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=BE?= =?UTF-8?q?=20=D1=87=D1=82=D0=B5=D0=BD=D0=B8=D1=8F.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ScriptEngine.HostedScript/Library/ProcessOutputWrapper.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ScriptEngine.HostedScript/Library/ProcessOutputWrapper.cs b/src/ScriptEngine.HostedScript/Library/ProcessOutputWrapper.cs index 31ea948ef..ceacf1216 100644 --- a/src/ScriptEngine.HostedScript/Library/ProcessOutputWrapper.cs +++ b/src/ScriptEngine.HostedScript/Library/ProcessOutputWrapper.cs @@ -43,14 +43,14 @@ public void StartReading() if (_variant == OutputVariant.Stdout) { Encoding = _process.StartInfo.StandardOutputEncoding; - _process.BeginOutputReadLine(); _process.OutputDataReceived += StreamDataReceived; + _process.BeginOutputReadLine(); } else { Encoding = _process.StartInfo.StandardErrorEncoding; - _process.BeginErrorReadLine(); _process.ErrorDataReceived += StreamDataReceived; + _process.BeginErrorReadLine(); } AlreadyReading = true;