Skip to content
Merged
Show file tree
Hide file tree
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
12 changes: 6 additions & 6 deletions src/libinputactions/scripting/FunctionWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ class FunctionWrapper : public QObject
if constexpr (std::is_void_v<TReturn>) {
function(engine->fromScriptValue<TArgs>(args[I])...);
return {};
}
} else {
const auto result = function(engine->fromScriptValue<TArgs>(args[I])...);
if constexpr (std::is_same_v<TReturn, QJSValue>) {
return result;
}

const auto result = function(engine->fromScriptValue<TArgs>(args[I])...);
if constexpr (std::is_same_v<TReturn, QJSValue>) {
return result;
return engine->toScriptValue(std::move(result));
}

return engine->toScriptValue(std::move(result));
}

QJSEngine *m_engine;
Expand Down
42 changes: 42 additions & 0 deletions src/libinputactions/scripting/ScriptingEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,48 @@ void ScriptingEngine::initialize()

return m_engine->importModule(module);
}));

// Unhandled promise rejection handling
// TODO Maybe perform the check when the promise is garbage collected if possible
m_engine->evaluate(R"(
const { delay } = require("inputactions/core");

const patch = (promise) => {
promise.__then = promise.then;
promise.then = function(onFulfilled, onRejected) {
this.__handled = true;
return patch(this.__then(onFulfilled, onRejected));
};

promise.__then(undefined, x => {
delay(100).__then(() => {
if (!promise.__handled) {
__unhandledPromiseRejection(x);
}
})
});

return promise;
}

const _Promise = Promise;
Promise = function(executor) {
return patch(new _Promise((resolve, reject) => {
executor(resolve, reject);
}));
}
Promise.all = _Promise.all;
Promise.race = _Promise.race;
Promise.reject = _Promise.reject;
Promise.resolve = _Promise.resolve;
)");
globalObject.setProperty("__unhandledPromiseRejection", newFunction<void, QJSValue>([this](QJSValue error) {
if (error.isError()) {
qCCritical(INPUTACTIONS_SCRIPTING).nospace().noquote() << "Uncaught (in promise) script error\n" << errorToString(error);
} else {
qCCritical(INPUTACTIONS_SCRIPTING).nospace().noquote() << "Uncaught (in promise) " << error.toString();
}
}));
}

void ScriptingEngine::initializeWatchdog()
Expand Down