本文整理汇总了C++中QScriptEngine::clearExceptions方法的典型用法代码示例。如果您正苦于以下问题:C++ QScriptEngine::clearExceptions方法的具体用法?C++ QScriptEngine::clearExceptions怎么用?C++ QScriptEngine::clearExceptions使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QScriptEngine
的用法示例。
在下文中一共展示了QScriptEngine::clearExceptions方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: handleRequest
bool HttpHandlerQtScript::handleRequest(Pillow::HttpRequest *request)
{
if (!_scriptFunction.isFunction()) return false;
QScriptEngine* engine = _scriptFunction.engine();
QScriptValue requestObject = engine->newObject();
requestObject.setProperty("nativeRequest", _scriptFunction.engine()->newQObject(request));
requestObject.setProperty("requestMethod", QUrl::fromPercentEncoding(request->requestMethod()));
requestObject.setProperty("requestUri", QUrl::fromPercentEncoding(request->requestUri()));
requestObject.setProperty("requestFragment", QUrl::fromPercentEncoding(request->requestFragment()));
requestObject.setProperty("requestPath", QUrl::fromPercentEncoding(request->requestPath()));
requestObject.setProperty("requestQueryString", QUrl::fromPercentEncoding(request->requestQueryString()));
requestObject.setProperty("requestHeaders", qScriptValueFromValue(engine, request->requestHeaders()));
QList<QPair<QString, QString> > queryParams = QUrl(request->requestUri()).queryItems();
QScriptValue queryParamsObject = engine->newObject();
for (int i = 0, iE = queryParams.size(); i < iE; ++i)
queryParamsObject.setProperty(queryParams.at(i).first, queryParams.at(i).second);
requestObject.setProperty("requestQueryParams", queryParamsObject);
QScriptValue result = _scriptFunction.call(_scriptFunction, QScriptValueList() << requestObject);
if (result.isError())
{
if (request->state() == HttpRequest::SendingHeaders)
{
// Nothing was sent yet... We have a chance to let the client know we had an error.
request->writeResponseString(500, HttpHeaderCollection(), objectToString(result));
}
engine->clearExceptions();
return true;
}
return result.toBool();
}
示例2: handleException
void handleException() {
Q_ASSERT( m_engine );
Q_ASSERT( m_engine->hasUncaughtException() );
const QString err = m_engine->uncaughtException().toString();
const int linenr = m_engine->uncaughtExceptionLineNumber();
const QString trace = m_engine->uncaughtExceptionBacktrace().join("\n");
qrossdebug( QString("%1, line:%2, backtrace:\n%3").arg(err).arg(linenr).arg(trace) );
m_script->action()->setError(err, trace, linenr);
m_engine->clearExceptions();
}
示例3: eval
QScriptValue QDeclarativeQtScriptExpression::eval(QObject *secondaryScope, bool *isUndefined)
{
Q_ASSERT(context() && context()->engine);
DeleteWatcher watcher(this);
QDeclarativeEngine *engine = context()->engine;
QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine);
QScriptEngine *scriptEngine = QDeclarativeEnginePrivate::getScriptEngine(engine);
QDeclarativeContextData *oldSharedContext = 0;
QObject *oldSharedScope = 0;
QObject *oldOverride = 0;
bool isShared = (expressionFunctionMode == SharedContext);
if (isShared) {
oldSharedContext = ep->sharedContext;
oldSharedScope = ep->sharedScope;
ep->sharedContext = context();
ep->sharedScope = scopeObject;
} else {
oldOverride = ep->contextClass->setOverrideObject(expressionContext, secondaryScope);
}
QScriptValue thisObject;
if (evalFlags & RequiresThisObject)
thisObject = ep->objectClass->newQObject(scopeObject);
QScriptValue svalue = expressionFunction.call(thisObject); // This could cause this c++ object to be deleted
if (isShared) {
ep->sharedContext = oldSharedContext;
ep->sharedScope = oldSharedScope;
} else if (!watcher.wasDeleted()) {
ep->contextClass->setOverrideObject(expressionContext, oldOverride);
}
if (isUndefined)
*isUndefined = svalue.isUndefined() || scriptEngine->hasUncaughtException();
// Handle exception
if (scriptEngine->hasUncaughtException()) {
if (!watcher.wasDeleted())
QDeclarativeExpressionPrivate::exceptionToError(scriptEngine, error);
scriptEngine->clearExceptions();
return QScriptValue();
} else {
if (!watcher.wasDeleted())
error = QDeclarativeError();
return svalue;
}
}
示例4: hadUncaughtExceptions
static bool hadUncaughtExceptions(QScriptEngine& engine, const QString& fileName) {
if (engine.hasUncaughtException()) {
const auto backtrace = engine.uncaughtExceptionBacktrace();
const auto exception = engine.uncaughtException().toString();
const auto line = QString::number(engine.uncaughtExceptionLineNumber());
engine.clearExceptions();
auto message = QString("[UncaughtException] %1 in %2:%3").arg(exception, fileName, line);
if (!backtrace.empty()) {
static const auto lineSeparator = "\n ";
message += QString("\n[Backtrace]%1%2").arg(lineSeparator, backtrace.join(lineSeparator));
}
qWarning() << qPrintable(message);
return true;
}
return false;
}
示例5: evalQtScript
QVariant QDeclarativeExpressionPrivate::evalQtScript(QObject *secondaryScope, bool *isUndefined)
{
QDeclarativeExpressionData *data = this->data;
QDeclarativeEngine *engine = data->context()->engine;
QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine);
QScriptEngine *scriptEngine = QDeclarativeEnginePrivate::getScriptEngine(engine);
if (!data->expressionFunctionValid) {
QScriptContext *scriptContext = QScriptDeclarativeClass::pushCleanContext(scriptEngine);
data->expressionContext = ep->contextClass->newContext(data->context(), data->me);
scriptContext->pushScope(data->expressionContext);
scriptContext->pushScope(ep->globalClass->globalObject());
if (data->expressionRewritten) {
data->expressionFunction = scriptEngine->evaluate(data->expression,
data->url, data->line);
} else {
QDeclarativeRewrite::RewriteBinding rewriteBinding;
bool ok = true;
const QString code = rewriteBinding(data->expression, &ok);
if (!ok) {
scriptEngine->popContext();
return QVariant();
}
data->expressionFunction = scriptEngine->evaluate(code, data->url, data->line);
}
scriptEngine->popContext();
data->expressionFunctionValid = true;
}
QDeclarativeContextData *oldSharedContext = 0;
QObject *oldSharedScope = 0;
QObject *oldOverride = 0;
if (data->isShared) {
oldSharedContext = ep->sharedContext;
oldSharedScope = ep->sharedScope;
ep->sharedContext = data->context();
ep->sharedScope = data->me;
} else {
oldOverride = ep->contextClass->setOverrideObject(data->expressionContext, secondaryScope);
}
QScriptValue svalue = data->expressionFunction.call();
if (data->isShared) {
ep->sharedContext = oldSharedContext;
ep->sharedScope = oldSharedScope;
} else {
ep->contextClass->setOverrideObject(data->expressionContext, oldOverride);
}
if (isUndefined)
*isUndefined = svalue.isUndefined() || scriptEngine->hasUncaughtException();
// Handle exception
if (scriptEngine->hasUncaughtException()) {
exceptionToError(scriptEngine, data->error);
scriptEngine->clearExceptions();
return QVariant();
} else {
data->error = QDeclarativeError();
}
QVariant rv;
if (svalue.isArray()) {
int length = svalue.property(QLatin1String("length")).toInt32();
if (length && svalue.property(0).isObject()) {
QList<QObject *> list;
for (int ii = 0; ii < length; ++ii) {
QScriptValue arrayItem = svalue.property(ii);
QObject *d = arrayItem.toQObject();
list << d;
}
rv = QVariant::fromValue(list);
}
} else if (svalue.isObject() &&
ep->objectClass->scriptClass(svalue) == ep->objectClass) {
QObject *o = svalue.toQObject();
int type = QMetaType::QObjectStar;
// If the object is null, we extract the predicted type. While this isn't
// 100% reliable, in many cases it gives us better error messages if we
// assign this null-object to an incompatible property
if (!o) type = ep->objectClass->objectType(svalue);
return QVariant(type, &o);
}
if (rv.isNull())
rv = svalue.toVariant();
return rv;
}
示例6: execute
//.........这里部分代码省略.........
response.setResult(QVariant::fromValue(backend->contextsCheckpoint()));
} break;
case QScriptDebuggerCommand::GetPropertyExpressionValue: {
QScriptContext *ctx = backend->context(command.contextIndex());
int lineNumber = command.lineNumber();
QVariant attr = command.attribute(QScriptDebuggerCommand::UserAttribute);
QStringList path = attr.toStringList();
if (!ctx || path.isEmpty())
break;
QScriptContextInfo ctxInfo(ctx);
if (ctx->callee().isValid()
&& ((lineNumber < ctxInfo.functionStartLineNumber())
|| (lineNumber > ctxInfo.functionEndLineNumber()))) {
break;
}
QScriptValueList objects;
int pathIndex = 0;
if (path.at(0) == QLatin1String("this")) {
objects.append(ctx->thisObject());
++pathIndex;
} else {
objects << ctx->scopeChain();
}
for (int i = 0; i < objects.size(); ++i) {
QScriptValue val = objects.at(i);
for (int j = pathIndex; val.isValid() && (j < path.size()); ++j) {
val = val.property(path.at(j));
}
if (val.isValid()) {
bool hadException = (ctx->state() == QScriptContext::ExceptionState);
QString str = val.toString();
if (!hadException && backend->engine()->hasUncaughtException())
backend->engine()->clearExceptions();
response.setResult(str);
break;
}
}
} break;
case QScriptDebuggerCommand::GetCompletions: {
QScriptContext *ctx = backend->context(command.contextIndex());
QVariant attr = command.attribute(QScriptDebuggerCommand::UserAttribute);
QStringList path = attr.toStringList();
if (!ctx || path.isEmpty())
break;
QScriptValueList objects;
QString prefix = path.last();
QSet<QString> matches;
if (path.size() > 1) {
const QString &topLevelIdent = path.at(0);
QScriptValue obj;
if (topLevelIdent == QLatin1String("this")) {
obj = ctx->thisObject();
} else {
QScriptValueList scopeChain;
scopeChain = ctx->scopeChain();
for (int i = 0; i < scopeChain.size(); ++i) {
QScriptValue oo = scopeChain.at(i).property(topLevelIdent);
if (oo.isObject()) {
obj = oo;
break;
}
}
}
for (int i = 1; obj.isObject() && (i < path.size()-1); ++i)
示例7: pushAndPopScope
void tst_QScriptContext::pushAndPopScope()
{
QScriptEngine eng;
QScriptContext *ctx = eng.currentContext();
QCOMPARE(ctx->scopeChain().size(), 1);
QVERIFY(ctx->scopeChain().at(0).strictlyEquals(eng.globalObject()));
QVERIFY(ctx->popScope().strictlyEquals(eng.globalObject()));
ctx->pushScope(eng.globalObject());
QCOMPARE(ctx->scopeChain().size(), 1);
QVERIFY(ctx->scopeChain().at(0).strictlyEquals(eng.globalObject()));
QScriptValue obj = eng.newObject();
ctx->pushScope(obj);
QCOMPARE(ctx->scopeChain().size(), 2);
QVERIFY(ctx->scopeChain().at(0).strictlyEquals(obj));
QVERIFY(ctx->scopeChain().at(1).strictlyEquals(eng.globalObject()));
QVERIFY(ctx->popScope().strictlyEquals(obj));
QCOMPARE(ctx->scopeChain().size(), 1);
QVERIFY(ctx->scopeChain().at(0).strictlyEquals(eng.globalObject()));
{
QScriptValue ret = eng.evaluate("x");
QVERIFY(ret.isError());
eng.clearExceptions();
}
QCOMPARE(ctx->scopeChain().size(), 1);
QVERIFY(ctx->scopeChain().at(0).strictlyEquals(eng.globalObject()));
// task 236685
QScriptValue qobj = eng.newQObject(this, QScriptEngine::QtOwnership, QScriptEngine::AutoCreateDynamicProperties);
ctx->pushScope(qobj);
QCOMPARE(ctx->scopeChain().size(), 2);
QVERIFY(ctx->scopeChain().at(0).strictlyEquals(qobj));
QVERIFY(ctx->scopeChain().at(1).strictlyEquals(eng.globalObject()));
{
QScriptValue ret = eng.evaluate("print");
QVERIFY(ret.isFunction());
}
ctx->popScope();
ctx->pushScope(obj);
QCOMPARE(ctx->scopeChain().size(), 2);
QVERIFY(ctx->scopeChain().at(0).strictlyEquals(obj));
obj.setProperty("x", 123);
{
QScriptValue ret = eng.evaluate("x");
QVERIFY(ret.isNumber());
QCOMPARE(ret.toInt32(), 123);
}
QVERIFY(ctx->popScope().strictlyEquals(obj));
QCOMPARE(ctx->scopeChain().size(), 1);
QVERIFY(ctx->scopeChain().at(0).strictlyEquals(eng.globalObject()));
ctx->pushScope(QScriptValue());
QCOMPARE(ctx->scopeChain().size(), 1);
QVERIFY(ctx->popScope().strictlyEquals(eng.globalObject()));
QVERIFY(ctx->scopeChain().isEmpty());
// Used to work with old back-end, doesn't with new one because JSC requires that the last object in
// a scope chain is the Global Object.
QTest::ignoreMessage(QtWarningMsg, "QScriptContext::pushScope() failed: initial object in scope chain has to be the Global Object");
ctx->pushScope(obj);
QCOMPARE(ctx->scopeChain().size(), 0);
QScriptEngine eng2;
QScriptValue obj2 = eng2.newObject();
QTest::ignoreMessage(QtWarningMsg, "QScriptContext::pushScope() failed: cannot push an object created in a different engine");
ctx->pushScope(obj2);
QVERIFY(ctx->scopeChain().isEmpty());
QVERIFY(!ctx->popScope().isValid());
}