From cc38a65b60d1d43c26343f952573127ff9d4a1f9 Mon Sep 17 00:00:00 2001 From: Ben Kuper Date: Thu, 17 Oct 2024 16:48:40 +0200 Subject: [PATCH 1/3] keep track of last callFunction time to avoid bad interrupt --- .../juce_core/javascript/juce_Javascript.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/modules/juce_core/javascript/juce_Javascript.cpp b/modules/juce_core/javascript/juce_Javascript.cpp index cb39a43edc0..ea2270ef518 100644 --- a/modules/juce_core/javascript/juce_Javascript.cpp +++ b/modules/juce_core/javascript/juce_Javascript.cpp @@ -692,12 +692,14 @@ class JavascriptEngine::Impl { shouldStop = false; - engine.setInterruptHandler ([this, maxExecTime, started = Time::getMillisecondCounterHiRes()]() + timeAtLastStart = Time::getMillisecondCounterHiRes(); + + engine.setInterruptHandler ([this, maxExecTime]() { if (shouldStop) return 1; - const auto elapsed = RelativeTime::milliseconds ((int64) (Time::getMillisecondCounterHiRes() - started)); + const auto elapsed = RelativeTime::milliseconds ((int64) (Time::getMillisecondCounterHiRes() - timeAtLastStart)); return elapsed > maxExecTime ? 1 : 0; }); @@ -723,8 +725,10 @@ class JavascriptEngine::Impl return result; } - var callFunction (const Identifier& function, const var::NativeFunctionArgs& args, Result* errorMessage) + var callFunction (const Identifier& function, const var::NativeFunctionArgs& args, Result* errorMessage, RelativeTime maxExecTime) { + timeAtLastStart = Time::getMillisecondCounterHiRes(); + auto* ctx = engine.getQuickJSContext(); const auto functionStr = function.toString(); @@ -771,6 +775,11 @@ class JavascriptEngine::Impl //============================================================================== detail::QuickJSWrapper engine; std::atomic shouldStop = false; + + /** This value stores the last time a execute, eval or callFunction has been started. + This allows to update the interrupt check when using callFunction() after execute() without having to always override the interrupt handler + */ + double timeAtLastStart = 0; }; //============================================================================== @@ -801,7 +810,7 @@ var JavascriptEngine::callFunction (const Identifier& function, const var::NativeFunctionArgs& args, Result* errorMessage) { - return impl->callFunction (function, args, errorMessage); + return impl->callFunction (function, args, errorMessage, maximumExecutionTime); } void JavascriptEngine::stop() noexcept From de7d752bba29175e1da1aa2b0e7ec972fb39413d Mon Sep 17 00:00:00 2001 From: Ben Kuper Date: Thu, 17 Oct 2024 18:27:17 +0200 Subject: [PATCH 2/3] add maxExecTime as class member so it can be updated in later calls --- modules/juce_core/javascript/juce_Javascript.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/modules/juce_core/javascript/juce_Javascript.cpp b/modules/juce_core/javascript/juce_Javascript.cpp index ea2270ef518..188d127a2e4 100644 --- a/modules/juce_core/javascript/juce_Javascript.cpp +++ b/modules/juce_core/javascript/juce_Javascript.cpp @@ -688,13 +688,14 @@ class JavascriptEngine::Impl wrapper.release(); } - var evaluate (const String& code, Result* errorMessage, RelativeTime maxExecTime) + var evaluate (const String& code, Result* errorMessage, RelativeTime maxExecutionTime) { shouldStop = false; timeAtLastStart = Time::getMillisecondCounterHiRes(); + maxExecTime = maxExecutionTime; - engine.setInterruptHandler ([this, maxExecTime]() + engine.setInterruptHandler ([this]() { if (shouldStop) return 1; @@ -725,9 +726,10 @@ class JavascriptEngine::Impl return result; } - var callFunction (const Identifier& function, const var::NativeFunctionArgs& args, Result* errorMessage, RelativeTime maxExecTime) + var callFunction (const Identifier& function, const var::NativeFunctionArgs& args, Result* errorMessage, RelativeTime maxExecutionTime) { timeAtLastStart = Time::getMillisecondCounterHiRes(); + maxExecTime = maxExecutionTime; auto* ctx = engine.getQuickJSContext(); const auto functionStr = function.toString(); @@ -780,6 +782,7 @@ class JavascriptEngine::Impl This allows to update the interrupt check when using callFunction() after execute() without having to always override the interrupt handler */ double timeAtLastStart = 0; + RelativeTime maxExecTime; }; //============================================================================== From ec683344ecc4f9ddf76511d06a34bdbc7931910f Mon Sep 17 00:00:00 2001 From: Ben Kuper Date: Thu, 17 Oct 2024 18:36:56 +0200 Subject: [PATCH 3/3] fix variable name warning --- modules/juce_core/javascript/juce_Javascript.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/juce_core/javascript/juce_Javascript.cpp b/modules/juce_core/javascript/juce_Javascript.cpp index 188d127a2e4..ebae6497031 100644 --- a/modules/juce_core/javascript/juce_Javascript.cpp +++ b/modules/juce_core/javascript/juce_Javascript.cpp @@ -719,10 +719,10 @@ class JavascriptEngine::Impl return var::undefined(); } - Result execute (const String& code, RelativeTime maxExecTime) + Result execute (const String& code, RelativeTime maxExecutionTime) { auto result = Result::ok(); - evaluate (code, &result, maxExecTime); + evaluate (code, &result, maxExecutionTime); return result; }