📄 qscriptengine.cpp
字号:
Q_D(QScriptEngine); return d->newQObject(object, ownership, options);}#endif // QT_NO_QOBJECT/*! Creates a QtScript object of class Object. The prototype of the created object will be the Object prototype object. \sa newArray(), QScriptValue::setProperty()*/QScriptValue QScriptEngine::newObject(){ Q_D(QScriptEngine); QScriptValueImpl v; d->newObject(&v, d->objectConstructor->publicPrototype); return v;}/*! \internal*/QScriptValue QScriptEngine::newActivationObject(){ Q_D(QScriptEngine); QScriptValueImpl v; d->newActivation(&v); return v;}/*! Creates a QScriptValue that wraps a native (C++) function. \a fun must be a C++ function with signature QScriptEngine::FunctionSignature. \a length is the number of arguments that \a fun expects; this becomes the \c{length} property of the created QScriptValue. Note that \a length only gives an indication of the number of arguments that the function expects; an actual invocation of a function can include any number of arguments. You can check the \l{QScriptContext::argumentCount()}{argumentCount()} of the QScriptContext associated with the invocation to determine the actual number of arguments passed. By combining newFunction() and the property flags QScriptValue::PropertyGetter and QScriptValue::PropertySetter, you can create script object properties that behave like normal properties in script code, but are in fact accessed through functions (analogous to how properties work in \l{Qt's Property System}). Example: \code static QScriptValue getSetFoo(QScriptContext *context, QScriptEngine *engine) { QScriptValue callee = context->callee(); if (context->argumentCount() == 1) // writing? callee.setProperty("value", context->argument(0)); return callee.property("value"); } .... QScriptValue object = engine.newObject(); object.setProperty("foo", engine.newFunction(getSetFoo), QScriptValue::PropertyGetter | QScriptValue::PropertySetter); \endcode When the property \c{foo} of the script object is subsequently accessed in script code, \c{getSetFoo()} will be invoked to handle the access. In this particular case, we chose to store the "real" value of \c{foo} as a property of the accessor function itself; you are of course free to do whatever you like in this function. In the above example, a single native function was used to handle both reads and writes to the property; the argument count is used to determine if we are handling a read or write. You can also use two separate functions; just specify the relevant flag (QScriptValue::PropertyGetter or QScriptValue::PropertySetter) when setting the property, e.g.: \code QScriptValue object = engine.newObject(); object.setProperty("foo", engine.newFunction(getFoo), QScriptValue::PropertyGetter); object.setProperty("foo", engine.newFunction(setFoo), QScriptValue::PropertySetter); \endcode \sa QScriptValue::call()*/QScriptValue QScriptEngine::newFunction(QScriptEngine::FunctionSignature fun, int length){ Q_D(QScriptEngine); QScriptValueImpl v = d->createFunction(new QScript::CFunction(fun, length)); QScriptValueImpl prototype = d->newObject(); v.setProperty(d->idTable()->id_prototype, prototype); prototype.setProperty(d->idTable()->id_constructor, v); return v;}/*! Creates a QtScript object of class Array with the given \a length. \sa newObject()*/QScriptValue QScriptEngine::newArray(uint length){ Q_D(QScriptEngine); QScriptValueImpl v; QScript::Array a; a.resize(length); d->newArray(&v, a); return v;}/*! Creates a QtScript object of class RegExp with the given \a pattern and \a flags.*/QScriptValue QScriptEngine::newRegExp(const QString &pattern, const QString &flags){ Q_D(QScriptEngine); QScriptValueImpl v; d->regexpConstructor->newRegExp(&v, pattern, flags); return v;}/*! Creates a QtScript object of class Date with the given \a value (the number of milliseconds since 01 January 1970, UTC).*/QScriptValue QScriptEngine::newDate(qsreal value){ Q_D(QScriptEngine); QScriptValueImpl v; d->dateConstructor->newDate(&v, value); return v;}/*! Creates a QtScript object of class Date from the given \a value. \sa QScriptValue::toDateTime()*/QScriptValue QScriptEngine::newDate(const QDateTime &value){ Q_D(QScriptEngine); QScriptValueImpl v; d->dateConstructor->newDate(&v, value); return v;}#ifndef QT_NO_QOBJECT/*! Creates a QtScript object that represents a QObject class, using the the given \a metaObject and constructor \a ctor. Enums of \a metaObject are available as properties of the created QScriptValue. When the class is called as a function, \a ctor will be called to create a new instance of the class. \sa newQObject()*/QScriptValue QScriptEngine::newQMetaObject( const QMetaObject *metaObject, const QScriptValue &ctor){ Q_D(QScriptEngine); QScriptValueImpl v; d->qmetaObjectConstructor->newQMetaObject(&v, metaObject, QScriptValuePrivate::valueOf(ctor)); return v;}/*! \fn QScriptValue QScriptEngine::scriptValueFromQMetaObject() Creates a QScriptValue that represents the Qt class \c{T}. This function is used in combination with one of the Q_SCRIPT_DECLARE_QMETAOBJECT() macro. Example: \code Q_SCRIPT_DECLARE_QMETAOBJECT(QLineEdit, QWidget*) ... QScriptValue lineEditClass = engine.scriptValueFromQMetaObject<QLineEdit>(); engine.globalObject().setProperty("QLineEdit", lineEditClass); \endcode \warning This function is not available with MSVC 6. Use qScriptValueFromQMetaObject() instead if you need to support that version of the compiler.*//*! \fn QScriptValue qScriptValueFromQMetaObject(QScriptEngine *engine) \since 4.3 \relates QScriptEngine Uses \a engine to create a QScriptValue that represents the Qt class \c{T}. This function is equivalent to QScriptEngine::scriptValueFromQMetaObject(). It is provided as a work-around for MSVC 6, which doesn't support member template functions.*/#endif // QT_NO_QOBJECT/*! Returns true if \a program can be evaluated; i.e. the code is sufficient to determine whether it appears to be a syntactically correct program, or contains a syntax error. This function returns false if \a program is incomplete; i.e. the input is syntactically correct up to the point where the input is terminated. Note that this function only does a static check of \a program; e.g. it does not check whether references to variables are valid, and so on. A typical usage of canEvaluate() is to implement an interactive interpreter for QtScript. The user is repeatedly queried for individual lines of code; the lines are concatened internally, and only when canEvaluate() returns true for the resulting program is it passed to evaluate(). The following are some examples to illustrate the behavior of canEvaluate(). (Note that all example inputs are assumed to have an explicit newline as their last character, since otherwise the QtScript parser would automatically insert a semi-colon character at the end of the input, and this could cause canEvaluate() to produce different results.) Given the input \code if (hello && world) print("hello world"); \endcode canEvaluate() will return true, since the program appears to be complete. Given the input \code if (hello && \endcode canEvaluate() will return false, since the if-statement is not complete, but is syntactically correct so far. Given the input \code 0 = 0 \endcode canEvaluate() will return true, but evaluate() will throw a SyntaxError given the same input. Given the input \code ./test.js \endcode canEvaluate() will return true, even though the code is clearly not syntactically valid QtScript code. evaluate() will throw a SyntaxError when this code is evaluated. Given the input \code foo["bar"] \endcode canEvaluate() will return true, but evaluate() will throw a ReferenceError if \c{foo} is not defined in the script environment. \sa evaluate()*/bool QScriptEngine::canEvaluate(const QString &program) const{ QScript::SyntaxChecker checker; return checker.parse(program);}/*! Evaluates \a program, using \a lineNumber as the base line number, and returns the result of the evaluation. The script code will be evaluated in the current context. The evaluation of \a program can cause an exception in the engine; in this case the return value will be the exception that was thrown (typically an \c{Error} object). You can call hasUncaughtException() to determine if an exception occurred in the last call to evaluate(). \a lineNumber is used to specify a starting line number for \a program; line number information reported by the engine that pertain to this evaluation (e.g. uncaughtExceptionLineNumber()) will be based on this argument. For example, if \a program consists of two lines of code, and the statement on the second line causes a script exception, uncaughtExceptionLineNumber() would return the given \a lineNumber plus one. When no starting line number is specified, line numbers will be 1-based. \a fileName is used for error reporting. For example in error objects the file name is accessible through the "fileName" property if it's provided with this function. \sa canEvaluate(), hasUncaughtException()*/QScriptValue QScriptEngine::evaluate(const QString &program, const QString &fileName, int lineNumber){ Q_D(QScriptEngine); QScriptContextPrivate *ctx_p = QScriptContextPrivate::get(d->currentContext()); d->evaluate(ctx_p, program, lineNumber, fileName); return ctx_p->m_result;}/*! Returns the current context. The current context is typically accessed to retrieve the arguments and `this' object in native functions; for convenience, it is available as the first argument in QScriptEngine::FunctionSignature.*/QScriptContext *QScriptEngine::currentContext() const{ Q_D(const QScriptEngine); return d->currentContext();}/*! Enters a new execution context and returns the associated QScriptContext object. Once you are done with the context, you should call popContext() to restore the old context. By default, the `this' object of the new context is the Global Object. The context's \l{QScriptContext::callee()}{callee}() will be invalid. This function is useful when you want to evaluate script code as if it were the body of a function. You can use the context's \l{QScriptContext::activationObject()}{activationObject}() to initialize local variables that will be available to scripts. Example: \code QScriptEngine engine; QScriptContext *context = engine.pushContext(); context->activationObject().setProperty("myArg", QScriptValue(&engine, 123)); engine.evaluate("var tmp = myArg + 42"); ... engine.popContext(); \endcode In the above example, the new variable "tmp" defined in the script will be local to the context; in other words, the script doesn't have any effect on the global environment. \sa popContext()*/QScriptContext *QScriptEngine::pushContext(){ Q_D(QScriptEngine); QScriptContext *context = d->pushContext(); context->setThisObject(globalObject()); QScriptValue activation = newActivationObject(); activation.setScope(globalObject()); context->setActivationObject(activation); return context;}/*! Pops the current execution context and restores the previous one. This function must be used in conjunction with pushContext(). \sa pushContext()*/void QScriptEngine::popContext(){ Q_D(QScriptEngine); if (d->currentContext() && d->currentContext()->parentContext()) d->popContext();}/*! Returns true if the last script evaluation (whether direct or indirect) resulted in an uncaught exception; otherwise returns false. The exception state is cleared every time a script function call is done in the engine, or when evaluate() is called. \sa uncaughtException(), uncaughtExceptionLineNumber(), uncaughtExceptionBacktrace()*/bool QScriptEngine::hasUncaughtException() const{ Q_D(const QScriptEngine); return d->hasUncaughtException();}/*! Returns the current uncaught exception, or an invalid QScriptValue if there is no uncaught exception. The exception value is typically an \c{Error} object; in that case, you can call toString() on the return value to obtain an error message. \sa hasUncaughtException(), uncaughtExceptionLineNumber(), uncaughtExceptionBacktrace()*/QScriptValue QScriptEngine::uncaughtException() const{ Q_D(const QScriptEngine); return d->uncaughtException();}/*! Returns the line number where the last uncaught exception occurred. Line numbers are 1-based, unless a different base was specified as the second argument to evaluate(). \sa hasUncaughtException(), uncaughtExceptionBacktrace()*/int QScriptEngine::uncaughtExceptionLineNumber() const{ return QScriptContextPrivate::get(currentContext())->errorLineNumber;}/*! Returns a human-readable backtrace of the last uncaught exception. Each line is of the form \c{<function-name>(<arguments>)@<file-name>:<line-number>}. \sa uncaughtException()*/QStringList QScriptEngine::uncaughtExceptionBacktrace() const
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -