本文整理汇总了C++中QScriptContext::thisObject方法的典型用法代码示例。如果您正苦于以下问题:C++ QScriptContext::thisObject方法的具体用法?C++ QScriptContext::thisObject怎么用?C++ QScriptContext::thisObject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QScriptContext
的用法示例。
在下文中一共展示了QScriptContext::thisObject方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: loadScripts
static QScriptValue loadScripts(QScriptContext *context, QScriptEngine *engine)
{
for (int i = 0; i < context->argumentCount(); ++i) {
QString fileName = context->argument(0).toString();
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
return context->throwError(QString::fromLatin1("could not open %0 for reading").arg(fileName));
QTextStream ts(&file);
QString contents = ts.readAll();
file.close();
QScriptContext *pc = context->parentContext();
context->setActivationObject(pc->activationObject());
context->setThisObject(pc->thisObject());
QScriptValue ret = engine->evaluate(contents);
if (engine->hasUncaughtException())
return ret;
}
return engine->undefinedValue();
}
示例2: construct
JSC::JSObject* ClassObjectDelegate::construct(JSC::ExecState *exec, JSC::JSObject *callee,
const JSC::ArgList &args)
{
Q_ASSERT(callee->inherits(&QScriptObject::info));
QScriptObject *obj = static_cast<QScriptObject*>(callee);
QScriptObjectDelegate *delegate = obj->delegate();
QScriptClass *scriptClass = static_cast<ClassObjectDelegate*>(delegate)->scriptClass();
QScriptEnginePrivate *eng_p = scriptEngineFromExec(exec);
//JSC::ExecState *oldFrame = eng_p->currentFrame;
eng_p->pushContext(exec, JSC::JSValue(), args, callee, true);
QScriptContext *ctx = eng_p->contextForFrame(eng_p->currentFrame);
QScriptValue defaultObject = ctx->thisObject();
QScriptValue result = qvariant_cast<QScriptValue>(scriptClass->extension(QScriptClass::Callable, qVariantFromValue(ctx)));
if (!result.isObject())
result = defaultObject;
return JSC::asObject(eng_p->scriptValueToJSCValue(result));
}
示例3: proxyConstruct
JSC::JSObject* FunctionWithArgWrapper::proxyConstruct(JSC::ExecState *exec, JSC::JSObject *callee,
const JSC::ArgList &args)
{
FunctionWithArgWrapper *self = static_cast<FunctionWithArgWrapper*>(callee);
QScriptEnginePrivate *eng_p = QScript::scriptEngineFromExec(exec);
JSC::ExecState *oldFrame = eng_p->currentFrame;
eng_p->pushContext(exec, JSC::JSValue(), args, callee, true);
QScriptContext *ctx = eng_p->contextForFrame(eng_p->currentFrame);
QScriptValue result = self->data->function(ctx, QScriptEnginePrivate::get(eng_p) , self->data->arg);
if (!result.isObject())
result = ctx->thisObject();
eng_p->popContext();
eng_p->currentFrame = oldFrame;
return JSC::asObject(eng_p->scriptValueToJSCValue(result));
}
示例4: execute
//.........这里部分代码省略.........
break;
case QScriptDebuggerCommand::GetContextCount:
response.setResult(backend->contextCount());
break;
case QScriptDebuggerCommand::GetContextState: {
QScriptContext *ctx = backend->context(command.contextIndex());
if (ctx)
response.setResult(static_cast<int>(ctx->state()));
else
response.setError(QScriptDebuggerResponse::InvalidContextIndex);
} break;
case QScriptDebuggerCommand::GetContextID: {
int idx = command.contextIndex();
if ((idx >= 0) && (idx < backend->contextCount()))
response.setResult(backend->contextIds()[idx]);
else
response.setError(QScriptDebuggerResponse::InvalidContextIndex);
} break;
case QScriptDebuggerCommand::GetContextInfo: {
QScriptContext *ctx = backend->context(command.contextIndex());
if (ctx)
response.setResult(QScriptContextInfo(ctx));
else
response.setError(QScriptDebuggerResponse::InvalidContextIndex);
} break;
case QScriptDebuggerCommand::GetThisObject: {
QScriptContext *ctx = backend->context(command.contextIndex());
if (ctx)
response.setResult(ctx->thisObject());
else
response.setError(QScriptDebuggerResponse::InvalidContextIndex);
} break;
case QScriptDebuggerCommand::GetActivationObject: {
QScriptContext *ctx = backend->context(command.contextIndex());
if (ctx)
response.setResult(ctx->activationObject());
else
response.setError(QScriptDebuggerResponse::InvalidContextIndex);
} break;
case QScriptDebuggerCommand::GetScopeChain: {
QScriptContext *ctx = backend->context(command.contextIndex());
if (ctx) {
QScriptDebuggerValueList dest;
QScriptValueList src = ctx->scopeChain();
for (int i = 0; i < src.size(); ++i)
dest.append(src.at(i));
response.setResult(dest);
} else {
response.setError(QScriptDebuggerResponse::InvalidContextIndex);
}
} break;
case QScriptDebuggerCommand::ContextsCheckpoint: {
response.setResult(QVariant::fromValue(backend->contextsCheckpoint()));
} break;
case QScriptDebuggerCommand::GetPropertyExpressionValue: {
QScriptContext *ctx = backend->context(command.contextIndex());
int lineNumber = command.lineNumber();
示例5: include
QScriptValue include( QScriptContext *context, QScriptEngine *engine )
{
// Check argument count, include() call in global context?
const QScriptContextInfo contextInfo( context->parentContext() );
if ( context->argumentCount() < 1 ) {
context->throwError( i18nc("@info/plain", "One argument expected for <icode>include()</icode>") );
return engine->undefinedValue();
} else if ( context->parentContext() && context->parentContext()->parentContext() ) {
QScriptContext *parentContext = context->parentContext()->parentContext();
bool error = false;
while ( parentContext ) {
const QScriptContextInfo parentContextInfo( parentContext );
if ( !parentContextInfo.fileName().isEmpty() &&
parentContextInfo.fileName() == contextInfo.fileName() )
{
// Parent context is in the same file, error
error = true;
break;
}
parentContext = parentContext->parentContext();
}
if ( error ) {
context->throwError( i18nc("@info/plain", "<icode>include()</icode> calls must be in global context") );
return engine->undefinedValue();
}
}
// Check if this include() call is before all other statements
QVariantHash includeData = context->callee().data().toVariant().toHash();
if ( includeData.contains(contextInfo.fileName()) ) {
const quint16 maxIncludeLine = includeData[ contextInfo.fileName() ].toInt();
if ( contextInfo.lineNumber() > maxIncludeLine ) {
context->throwError( i18nc("@info/plain", "<icode>include()</icode> calls must be the first statements") );
return engine->undefinedValue();
}
}
// Get argument and check that it's not pointing to another directory
const QString fileName = context->argument(0).toString();
if ( fileName.contains('/') ) {
context->throwError( i18nc("@info/plain", "Cannot include files from other directories") );
return engine->undefinedValue();
}
// Get path of the main script
QString path;
QScriptContext *fileInfoContext = context;
do {
path = QFileInfo( QScriptContextInfo(fileInfoContext).fileName() ).path();
fileInfoContext = fileInfoContext->parentContext();
} while ( path.isEmpty() || path == QLatin1String(".") );
// Construct file path to the file to be included and check if the file is already included
const QString filePath = path + '/' + fileName;
QStringList includedFiles =
engine->globalObject().property( "includedFiles" ).toVariant().toStringList();
if ( includedFiles.contains(filePath) ) {
kWarning() << "File already included" << filePath;
return engine->undefinedValue();
}
// Try to open the file to be included
QFile scriptFile( filePath );
if ( !scriptFile.open(QIODevice::ReadOnly) ) {
context->throwError( i18nc("@info/plain", "Cannot find file to be included: "
"<filename>%1</filename>", filePath) );
return engine->undefinedValue();
}
// Read the file
QTextStream stream( &scriptFile );
const QString program = stream.readAll();
scriptFile.close();
if ( !includeData.contains(scriptFile.fileName()) ) {
includeData[ scriptFile.fileName() ] = maxIncludeLine( program );
QScriptValue includeFunction = engine->globalObject().property("include");
Q_ASSERT( includeFunction.isValid() );
includeFunction.setData( qScriptValueFromValue(engine, includeData) );
engine->globalObject().setProperty( "include", includeFunction,
QScriptValue::KeepExistingFlags );
}
// Set script context
QScriptContext *parent = context->parentContext();
if ( parent ) {
context->setActivationObject( parent->activationObject() );
context->setThisObject( parent->thisObject() );
}
// Store included files in global property "includedFiles"
includedFiles << filePath;
includedFiles.removeDuplicates();
QScriptValue::PropertyFlags flags = QScriptValue::ReadOnly | QScriptValue::Undeletable;
engine->globalObject().setProperty( "includedFiles", engine->newVariant(includedFiles), flags );
// Evaluate script
return engine->evaluate( program, filePath );
//.........这里部分代码省略.........
示例6: pushAndPopContext
void tst_QScriptContext::pushAndPopContext()
{
QScriptEngine eng;
QScriptContext *topLevel = eng.currentContext();
QCOMPARE(topLevel->engine(), &eng);
QScriptContext *ctx = eng.pushContext();
QVERIFY(ctx != 0);
QCOMPARE(ctx->parentContext(), topLevel);
QCOMPARE(eng.currentContext(), ctx);
QCOMPARE(ctx->engine(), &eng);
QCOMPARE(ctx->state(), QScriptContext::NormalState);
QCOMPARE(ctx->isCalledAsConstructor(), false);
QCOMPARE(ctx->argumentCount(), 0);
QCOMPARE(ctx->argument(0).isUndefined(), true);
QVERIFY(!ctx->argument(-1).isValid());
QCOMPARE(ctx->argumentsObject().isObject(), true);
QCOMPARE(ctx->activationObject().isObject(), true);
QCOMPARE(ctx->callee().isValid(), false);
QCOMPARE(ctx->thisObject().strictlyEquals(eng.globalObject()), true);
QCOMPARE(ctx->scopeChain().size(), 2);
QVERIFY(ctx->scopeChain().at(0).equals(ctx->activationObject()));
QVERIFY(ctx->scopeChain().at(1).equals(eng.globalObject()));
QScriptContext *ctx2 = eng.pushContext();
QCOMPARE(ctx2->parentContext(), ctx);
QCOMPARE(eng.currentContext(), ctx2);
eng.popContext();
QCOMPARE(eng.currentContext(), ctx);
eng.popContext();
QCOMPARE(eng.currentContext(), topLevel);
// popping the top-level context is not allowed
QTest::ignoreMessage(QtWarningMsg, "QScriptEngine::popContext() doesn't match with pushContext()");
eng.popContext();
QCOMPARE(eng.currentContext(), topLevel);
{
QScriptContext *ctx3 = eng.pushContext();
ctx3->activationObject().setProperty("foo", QScriptValue(&eng, 123));
QVERIFY(eng.evaluate("foo").strictlyEquals(QScriptValue(&eng, 123)));
eng.evaluate("var bar = 'ciao'");
QVERIFY(ctx3->activationObject().property("bar", QScriptValue::ResolveLocal).strictlyEquals(QScriptValue(&eng, "ciao")));
eng.popContext();
}
{
QScriptContext *ctx4 = eng.pushContext();
QScriptValue obj = eng.newObject();
obj.setProperty("prop", QScriptValue(&eng, 456));
ctx4->setThisObject(obj);
QScriptValue ret = eng.evaluate("var tmp = this.prop; tmp + 1");
QCOMPARE(eng.currentContext(), ctx4);
QVERIFY(ret.strictlyEquals(QScriptValue(&eng, 457)));
eng.popContext();
}
// throwing an exception
{
QScriptContext *ctx5 = eng.pushContext();
QScriptValue ret = eng.evaluate("throw new Error('oops')");
QVERIFY(ret.isError());
QVERIFY(eng.hasUncaughtException());
QCOMPARE(eng.currentContext(), ctx5);
eng.popContext();
}
}