mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 23:36:44 +02:00
Initial implementation of raiseException and test
This commit is contained in:
parent
0369949d9b
commit
8f82750f2a
3 changed files with 60 additions and 22 deletions
|
@ -172,28 +172,8 @@ ScriptValue ScriptEngineV8::checkScriptSyntax(ScriptProgramPointer program) {
|
||||||
return undefinedValue();
|
return undefinedValue();
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
bool ScriptEngineV8::raiseException(const V8ScriptValue& exception) {
|
|
||||||
if (!IS_THREADSAFE_INVOCATION(thread(), __FUNCTION__)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
//V8TODO
|
|
||||||
// _v8Isolate->ThrowException(makeError(exception).get());
|
|
||||||
|
|
||||||
|
|
||||||
/*if (QScriptEngine::currentContext()) {
|
|
||||||
// we have an active context / JS stack frame so throw the exception per usual
|
|
||||||
QScriptEngine::currentContext()->throwValue(makeError(exception));
|
|
||||||
return true;
|
|
||||||
} else if (_scriptManager) {
|
|
||||||
// we are within a pure C++ stack frame (ie: being called directly by other C++ code)
|
|
||||||
// in this case no context information is available so just emit the exception for reporting
|
|
||||||
V8ScriptValue thrown = makeError(exception);
|
|
||||||
emit _scriptManager->unhandledException(ScriptValue(new ScriptValueV8Wrapper(this, std::move(thrown))));
|
|
||||||
}*/
|
|
||||||
//emit _scriptManager->unhandledException(ScriptValue(new ScriptValueV8Wrapper(this, std::move(thrown))));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Lambda
|
// Lambda
|
||||||
/*ScriptValue ScriptEngineV8::newLambdaFunction(std::function<V8ScriptValue(V8ScriptContext*, ScriptEngineV8*)> operation,
|
/*ScriptValue ScriptEngineV8::newLambdaFunction(std::function<V8ScriptValue(V8ScriptContext*, ScriptEngineV8*)> operation,
|
||||||
const V8ScriptValue& data,
|
const V8ScriptValue& data,
|
||||||
|
@ -1548,7 +1528,7 @@ std::shared_ptr<ScriptException> ScriptEngineV8::uncaughtException() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ScriptEngineV8::raiseException(const QString& error, const QString &reason) {
|
bool ScriptEngineV8::raiseException(const QString& error, const QString &reason) {
|
||||||
return raiseException(ScriptValue(), reason);
|
return raiseException(newValue(error), reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ScriptEngineV8::raiseException(const ScriptValue& exception, const QString &reason) {
|
bool ScriptEngineV8::raiseException(const ScriptValue& exception, const QString &reason) {
|
||||||
|
@ -1561,10 +1541,38 @@ bool ScriptEngineV8::raiseException(const ScriptValue& exception, const QString
|
||||||
// emit
|
// emit
|
||||||
//return raiseException(qException);
|
//return raiseException(qException);
|
||||||
|
|
||||||
qCCritical(scriptengine) << "Raise exception for reason" << reason << "NOT IMPLEMENTED!";
|
// qCCritical(scriptengine) << "Raise exception for reason" << reason << "NOT IMPLEMENTED!";
|
||||||
|
// return false;
|
||||||
|
|
||||||
|
return raiseException(ScriptValueV8Wrapper::fullUnwrap(this, exception));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
bool ScriptEngineV8::raiseException(const V8ScriptValue& exception) {
|
||||||
|
if (!IS_THREADSAFE_INVOCATION(thread(), __FUNCTION__)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
_v8Isolate->ThrowException(exception.constGet());
|
||||||
|
|
||||||
|
|
||||||
|
/*if (QScriptEngine::currentContext()) {
|
||||||
|
// we have an active context / JS stack frame so throw the exception per usual
|
||||||
|
QScriptEngine::currentContext()->throwValue(makeError(exception));
|
||||||
|
return true;
|
||||||
|
} else if (_scriptManager) {
|
||||||
|
// we are within a pure C++ stack frame (ie: being called directly by other C++ code)
|
||||||
|
// in this case no context information is available so just emit the exception for reporting
|
||||||
|
V8ScriptValue thrown = makeError(exception);
|
||||||
|
emit _scriptManager->unhandledException(ScriptValue(new ScriptValueV8Wrapper(this, std::move(thrown))));
|
||||||
|
}*/
|
||||||
|
//emit _scriptManager->unhandledException(ScriptValue(new ScriptValueV8Wrapper(this, std::move(thrown))));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ScriptValue ScriptEngineV8::create(int type, const void* ptr) {
|
ScriptValue ScriptEngineV8::create(int type, const void* ptr) {
|
||||||
v8::Locker locker(_v8Isolate);
|
v8::Locker locker(_v8Isolate);
|
||||||
v8::Isolate::Scope isolateScope(_v8Isolate);
|
v8::Isolate::Scope isolateScope(_v8Isolate);
|
||||||
|
|
|
@ -218,6 +218,35 @@ void ScriptEngineTests::testRaiseException() {
|
||||||
QVERIFY(ex && ex->errorMessage.contains("Exception test"));
|
QVERIFY(ex && ex->errorMessage.contains("Exception test"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ScriptEngineTests::testRaiseExceptionAndCatch() {
|
||||||
|
QString script =
|
||||||
|
"try {"
|
||||||
|
" testClass.doRaiseTest();"
|
||||||
|
"} catch (err) {"
|
||||||
|
" if (err === \"Exception test!\") {"
|
||||||
|
" print(\"Caught!\");"
|
||||||
|
" }"
|
||||||
|
"}"
|
||||||
|
"Script.stop(true);"
|
||||||
|
;
|
||||||
|
|
||||||
|
QString printed;
|
||||||
|
auto sm = makeManager(script, "testRaiseCatch.js");
|
||||||
|
|
||||||
|
connect(sm.get(), &ScriptManager::printedMessage, [&printed](const QString& message, const QString& engineName){
|
||||||
|
printed.append(message);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
sm->engine()->registerGlobalObject("testClass", new TestClass(sm->engine()));
|
||||||
|
|
||||||
|
sm->run();
|
||||||
|
auto ex = sm->getUncaughtException();
|
||||||
|
|
||||||
|
QVERIFY(!ex);
|
||||||
|
QVERIFY(printed == "Caught!");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void ScriptEngineTests::scriptTest() {
|
void ScriptEngineTests::scriptTest() {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -62,6 +62,7 @@ private slots:
|
||||||
void testRegisterClass();
|
void testRegisterClass();
|
||||||
void testInvokeNonInvokable();
|
void testInvokeNonInvokable();
|
||||||
void testRaiseException();
|
void testRaiseException();
|
||||||
|
void testRaiseExceptionAndCatch();
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
Loading…
Reference in a new issue