From 88e544a0857e93a8a67364384965f03c11a40f90 Mon Sep 17 00:00:00 2001 From: reuk Date: Thu, 17 Oct 2024 12:27:29 +0100 Subject: [PATCH] Javascript: Refactor interrupt handler --- .../juce_core/javascript/juce_Javascript.cpp | 29 +++++-------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/modules/juce_core/javascript/juce_Javascript.cpp b/modules/juce_core/javascript/juce_Javascript.cpp index 44bb15e6d8..9a94132394 100644 --- a/modules/juce_core/javascript/juce_Javascript.cpp +++ b/modules/juce_core/javascript/juce_Javascript.cpp @@ -621,6 +621,11 @@ public: Impl() { DynamicObjectWrapper::createClass (engine.getQuickJSRuntime()); + + engine.setInterruptHandler ([this] + { + return (int64) Time::getMillisecondCounterHiRes() >= timeout; + }); } void registerNativeObject (const Identifier& name, @@ -750,7 +755,7 @@ public: void stop() noexcept { - shouldStop = true; + timeout = (int64) Time::getMillisecondCounterHiRes(); } JSObject getRootObject() const @@ -758,33 +763,15 @@ public: return JSObject { &engine }; } - const detail::QuickJSWrapper& getEngine() const - { - return engine; - } - private: //============================================================================== void resetTimeout (RelativeTime maxExecTime) { - shouldStop = false; - - engine.setInterruptHandler ([this, maxExecTime, started = Time::getMillisecondCounterHiRes()]() - { - if (shouldStop) - return 1; - - const auto elapsed = RelativeTime::milliseconds ((int64) (Time::getMillisecondCounterHiRes() - started)); - - if (elapsed > maxExecTime) - return 1; - - return 0; - }); + timeout = (int64) Time::getMillisecondCounterHiRes() + maxExecTime.inMilliseconds(); } detail::QuickJSWrapper engine; - std::atomic shouldStop = false; + std::atomic timeout{}; }; //==============================================================================