Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix javascript bad interrupt #1448

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions modules/juce_core/javascript/juce_Javascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -688,16 +688,19 @@ 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;

engine.setInterruptHandler ([this, maxExecTime, started = Time::getMillisecondCounterHiRes()]()
timeAtLastStart = Time::getMillisecondCounterHiRes();
maxExecTime = maxExecutionTime;

engine.setInterruptHandler ([this]()
{
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 @@ -716,15 +719,18 @@ 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;
}

var callFunction (const Identifier& function, const var::NativeFunctionArgs& args, Result* errorMessage)
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();

Expand Down Expand Up @@ -771,6 +777,12 @@ 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;
RelativeTime maxExecTime;
};

//==============================================================================
Expand Down Expand Up @@ -801,7 +813,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