Skip to content

Commit

Permalink
keep track of last callFunction time to avoid bad interrupt
Browse files Browse the repository at this point in the history
  • Loading branch information
benkuper committed Oct 17, 2024
1 parent 5179f4e commit cc38a65
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions modules/juce_core/javascript/juce_Javascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

Expand All @@ -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();

Expand Down Expand Up @@ -771,6 +775,11 @@ class JavascriptEngine::Impl
//==============================================================================
detail::QuickJSWrapper engine;
std::atomic<bool> 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;
};

//==============================================================================
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cc38a65

Please sign in to comment.