Qt 4.8
|
The QScriptContext class represents a Qt Script function invocation. More...
#include <qscriptcontext.h>
Public Types | |
enum | Error { UnknownError, ReferenceError, SyntaxError, TypeError, RangeError, URIError } |
This enum specifies types of error. More... | |
enum | ExecutionState { NormalState, ExceptionState } |
This enum specifies the frameution state of the context. More... | |
Public Functions | |
QScriptValue | activationObject () const |
Returns the activation object of this QScriptContext. More... | |
QScriptValue | argument (int index) const |
Returns the function argument at the given index. More... | |
int | argumentCount () const |
Returns the number of arguments passed to the function in this invocation. More... | |
QScriptValue | argumentsObject () const |
Returns the arguments object of this QScriptContext. More... | |
QStringList | backtrace () const |
Returns a human-readable backtrace of this QScriptContext. More... | |
QScriptValue | callee () const |
Returns the callee. More... | |
QScriptEngine * | engine () const |
Returns the QScriptEngine that this QScriptContext belongs to. More... | |
bool | isCalledAsConstructor () const |
Returns true if the function was called as a constructor (e.g. More... | |
QScriptContext * | parentContext () const |
Returns the parent context of this QScriptContext. More... | |
QScriptValue | popScope () |
Removes the front object from this context's scope chain, and returns the removed object. More... | |
void | pushScope (const QScriptValue &object) |
Adds the given object to the front of this context's scope chain. More... | |
QScriptValue | returnValue () const |
QScriptValueList | scopeChain () const |
Returns the scope chain of this QScriptContext. More... | |
void | setActivationObject (const QScriptValue &activation) |
Sets the activation object of this QScriptContext to be the given activation. More... | |
void | setReturnValue (const QScriptValue &result) |
void | setThisObject (const QScriptValue &thisObject) |
Sets the `this' object associated with this QScriptContext to be thisObject. More... | |
ExecutionState | state () const |
Returns the frameution state of this QScriptContext. More... | |
QScriptValue | thisObject () const |
Returns the `this' object associated with this QScriptContext. More... | |
QScriptValue | throwError (Error error, const QString &text) |
Throws an error with the given text. More... | |
QScriptValue | throwError (const QString &text) |
Throws an error with the given text. More... | |
QScriptValue | throwValue (const QScriptValue &value) |
Throws an exception with the given value. More... | |
QString | toString () const |
Returns a string representation of this context. More... | |
~QScriptContext () | |
Destroys this QScriptContext. More... | |
Private Functions | |
QScriptContext () | |
Properties | |
QScriptContextPrivate * | d_ptr |
The QScriptContext class represents a Qt Script function invocation.
A QScriptContext provides access to the `this' object and arguments passed to a script function. You typically want to access this information when you're writing a native (C++) function (see QScriptEngine::newFunction()) that will be called from script code. For example, when the script code
is evaluated, a QScriptContext will be created, and the context will carry the arguments as QScriptValues; in this particular case, the arguments will be one QScriptValue containing the number 20.5, a second QScriptValue containing the string "hello"
, and a third QScriptValue containing a Qt Script object.
Use argumentCount() to get the number of arguments passed to the function, and argument() to get an argument at a certain index. The argumentsObject() function returns a Qt Script array object containing all the arguments; you can use the QScriptValueIterator to iterate over its elements, or pass the array on as arguments to another script function using QScriptValue::call().
Use thisObject() to get the `this' object associated with the function call, and setThisObject() to set the `this' object. If you are implementing a native "instance method", you typically fetch the thisObject() and access one or more of its properties:
Use isCalledAsConstructor() to determine if the function was called as a constructor (e.g. "new foo()"
(as constructor) or just "foo()"
). When a function is called as a constructor, the thisObject() contains the newly constructed object that the function is expected to initialize.
Use throwValue() or throwError() to throw an exception.
Use callee() to obtain the QScriptValue that represents the function being called. This can for example be used to call the function recursively.
Use parentContext() to get a pointer to the context that precedes this context in the activation stack. This is mostly useful for debugging purposes (e.g. when constructing some form of backtrace).
The activationObject() function returns the object that is used to hold the local variables associated with this function call. You can replace the activation object by calling setActivationObject(). A typical usage of these functions is when you want script code to be evaluated in the context of the parent context, e.g. to implement an include() function:
Use backtrace() to get a human-readable backtrace associated with this context. This can be useful for debugging purposes when implementing native functions. The toString() function provides a string representation of the context. (QScriptContextInfo provides more detailed debugging-related information about the QScriptContext.)
Use engine() to obtain a pointer to the QScriptEngine that this context resides in.
Definition at line 39 of file qscriptcontext.h.
This enum specifies types of error.
Enumerator | |
---|---|
UnknownError | |
ReferenceError | |
SyntaxError | |
TypeError | |
RangeError | |
URIError |
Definition at line 47 of file qscriptcontext.h.
This enum specifies the frameution state of the context.
Enumerator | |
---|---|
NormalState | |
ExceptionState |
Definition at line 42 of file qscriptcontext.h.
QScriptContext::~QScriptContext | ( | ) |
|
private |
Definition at line 158 of file qscriptcontext.cpp.
QScriptValue QScriptContext::activationObject | ( | ) | const |
Returns the activation object of this QScriptContext.
The activation object provides access to the local variables associated with this context.
Definition at line 450 of file qscriptcontext.cpp.
Referenced by QScriptDebuggerBackend::doPendingEvaluate(), QScriptDebuggerCommandExecutor::execute(), QJSDebuggerAgentPrivate::getLocals(), QScriptEngine::importExtension(), popScope(), pushScope(), scopeChain(), and QScriptDeclarativeClass::scopeChainValue().
QScriptValue QScriptContext::argument | ( | int | index | ) | const |
Returns the function argument at the given index.
If index >= argumentCount(), a QScriptValue of the primitive type Undefined is returned.
Definition at line 266 of file qscriptcontext.cpp.
Referenced by QScript::__setupPackage__(), QDeclarativeEnginePrivate::atob(), QDeclarativeEnginePrivate::btoa(), QDeclarativeObjectMethodScriptClass::callMethod(), QDeclarativeObjectMethodScriptClass::callOverloaded(), QDeclarativeObjectMethodScriptClass::connect(), QDeclarativeEnginePrivate::consoleLog(), QDeclarativeEnginePrivate::createComponent(), QScriptDBusMessageConstructor::createErrorReply(), QDeclarativeEnginePrivate::createQmlObject(), QScriptDBusMessageConstructor::createReply(), QDeclarativeEnginePrivate::darker(), QDeclarativeEnginePrivate::desktopOpenUrl(), QDeclarativeObjectScriptClass::destroy(), QDeclarativeObjectMethodScriptClass::disconnect(), do_dbus_call(), QDeclarativeEnginePrivate::formatDate(), QDeclarativeEnginePrivate::formatDateTime(), QDeclarativeEnginePrivate::formatTime(), QDeclarativeEnginePrivate::hsla(), QDeclarativeInclude::include(), QDeclarativeEnginePrivate::isQtObject(), QDeclarativeEnginePrivate::lighter(), QDeclarativeEnginePrivate::md5(), QDeclarativeWorkerScriptEnginePrivate::onMessage(), QDeclarativeEnginePrivate::point(), qmlsqldatabase_change_version(), qmlsqldatabase_executeSql(), qmlsqldatabase_executeSql_readonly(), qmlsqldatabase_item(), qmlsqldatabase_open_sync(), qmlsqldatabase_transaction_shared(), qmlxmlhttprequest_getResponseHeader(), qmlxmlhttprequest_onreadystatechange(), qmlxmlhttprequest_open(), qmlxmlhttprequest_send(), qmlxmlhttprequest_setRequestHeader(), QScriptDebuggerBackendPrivate::qsassert(), QDeclarativeEnginePrivate::rect(), QDeclarativeScriptEngine::resolvedUrl(), QDeclarativeEnginePrivate::rgba(), QDeclarativeWorkerScriptEnginePrivate::sendMessage(), QDeclarativeEnginePrivate::size(), QDeclarativeEnginePrivate::tint(), toString(), QScriptDebuggerBackendPrivate::trace(), QDeclarativeEnginePrivate::vector3d(), and QDeclarativeInclude::worker_include().
int QScriptContext::argumentCount | ( | ) | const |
Returns the number of arguments passed to the function in this invocation.
Note that the argument count can be different from the formal number of arguments (the length
property of callee()).
Definition at line 407 of file qscriptcontext.cpp.
Referenced by argument(), QDeclarativeEnginePrivate::atob(), QDeclarativeEnginePrivate::btoa(), QDeclarativeObjectMethodScriptClass::callOverloaded(), QDeclarativeObjectMethodScriptClass::callPrecise(), QDeclarativeObjectMethodScriptClass::connect(), QDeclarativeEnginePrivate::consoleLog(), QDeclarativeEnginePrivate::createComponent(), QScriptDBusMessageConstructor::createErrorReply(), QDeclarativeEnginePrivate::createQmlObject(), QScriptDBusMessageConstructor::createReply(), QDeclarativeEnginePrivate::darker(), QDeclarativeEnginePrivate::desktopOpenUrl(), QDeclarativeObjectScriptClass::destroy(), QDeclarativeObjectMethodScriptClass::disconnect(), do_dbus_call(), QDeclarativeEnginePrivate::fontFamilies(), QDeclarativeEnginePrivate::formatDate(), QDeclarativeEnginePrivate::formatDateTime(), QDeclarativeEnginePrivate::formatTime(), QDeclarativeEnginePrivate::hsla(), QDeclarativeInclude::include(), QDeclarativeEnginePrivate::isQtObject(), QDeclarativeEnginePrivate::lighter(), QDeclarativeEnginePrivate::md5(), QDeclarativeWorkerScriptEnginePrivate::onMessage(), QDeclarativeEnginePrivate::point(), qmlsqldatabase_change_version(), qmlsqldatabase_executeSql(), qmlxmlhttprequest_getAllResponseHeaders(), qmlxmlhttprequest_getResponseHeader(), qmlxmlhttprequest_onreadystatechange(), qmlxmlhttprequest_open(), qmlxmlhttprequest_send(), qmlxmlhttprequest_setRequestHeader(), QScriptDebuggerBackendPrivate::qsassert(), QDeclarativeEnginePrivate::rect(), QDeclarativeEnginePrivate::rgba(), QDeclarativeWorkerScriptEnginePrivate::sendMessage(), QDeclarativeEnginePrivate::size(), QDeclarativeEnginePrivate::tint(), toString(), QScriptDebuggerBackendPrivate::trace(), QDeclarativeEnginePrivate::vector3d(), and QDeclarativeInclude::worker_include().
QScriptValue QScriptContext::argumentsObject | ( | ) | const |
Returns the arguments object of this QScriptContext.
The arguments object has properties callee
(equal to callee()) and length
(equal to argumentCount()), and properties 0
, 1
, ..., argumentCount() - 1 that provide access to the argument values. Initially, property P
(0 <= P
< argumentCount()) has the same value as argument(P
). In the case when P
is less than the number of formal parameters of the function, P
shares its value with the corresponding property of the activation object (activationObject()). This means that changing this property changes the corresponding property of the activation object and vice versa.
Definition at line 308 of file qscriptcontext.cpp.
Referenced by argument(), QScript::GlobalObject::getOwnPropertyDescriptor(), and QScript::GlobalObject::getOwnPropertySlot().
QStringList QScriptContext::backtrace | ( | ) | const |
Returns a human-readable backtrace of this QScriptContext.
Each line is of the form <function-name>(<arguments>)<file-name>:<line-number>
.
To access individual pieces of debugging-related information (for example, to construct your own backtrace representation), use QScriptContextInfo.
Definition at line 629 of file qscriptcontext.cpp.
Referenced by QScriptDebuggerBackend::backtrace().
QScriptValue QScriptContext::callee | ( | ) | const |
Returns the callee.
The callee is the function object that this QScriptContext represents an invocation of.
Definition at line 280 of file qscriptcontext.cpp.
Referenced by do_dbus_call(), QScriptDebuggerCommandExecutor::execute(), and QScriptDebuggerBackendPrivate::trace().
QScriptEngine * QScriptContext::engine | ( | ) | const |
Returns the QScriptEngine that this QScriptContext belongs to.
Definition at line 252 of file qscriptcontext.cpp.
Referenced by activationObject(), QDeclarativeEnginePrivate::createQmlObject(), popScope(), pushScope(), QScriptContextInfoPrivate::QScriptContextInfoPrivate(), scopeChain(), setActivationObject(), setThisObject(), thisObject(), and QScriptDeclarativeClass::Value::Value().
bool QScriptContext::isCalledAsConstructor | ( | ) | const |
Returns true if the function was called as a constructor (e.g.
"new foo()"
); otherwise returns false.
When a function is called as constructor, the thisObject() contains the newly constructed object to be initialized.
Definition at line 355 of file qscriptcontext.cpp.
Referenced by qmlxmlhttprequest_new().
QScriptContext * QScriptContext::parentContext | ( | ) | const |
Returns the parent context of this QScriptContext.
Definition at line 389 of file qscriptcontext.cpp.
Referenced by activationObject(), backtrace(), QScriptDebuggerBackend::context(), QScriptDebuggerBackendPrivate::fileName(), isCalledAsConstructor(), QScriptDebuggerBackendPrivate::lineNumber(), QJSDebuggerAgent::localsAtFrame(), QScriptEnginePrivate::mark(), QScriptEngine::popContext(), QScriptDebuggerBackendPrivate::qsassert(), QScriptDebuggerAgent::QScriptDebuggerAgent(), and toString().
QScriptValue QScriptContext::popScope | ( | ) |
Removes the front object from this context's scope chain, and returns the removed object.
If the scope chain is already empty, this function returns an invalid QScriptValue.
Definition at line 790 of file qscriptcontext.cpp.
void QScriptContext::pushScope | ( | const QScriptValue & | object | ) |
Adds the given object to the front of this context's scope chain.
If object is not an object, this function does nothing.
Definition at line 746 of file qscriptcontext.cpp.
Referenced by QDeclarativeContextData::addImportedScript(), QScriptDebuggerBackend::doPendingEvaluate(), QDeclarativeExpressionPrivate::evalInObjectScope(), QDeclarativeInclude::finished(), QDeclarativeInclude::include(), QDeclarativeExpressionPrivate::init(), QDeclarativeWorkerScriptEnginePrivate::processLoad(), QDeclarativeExpressionPrivate::scriptValue(), and QDeclarativeInclude::worker_include().
QScriptValue QScriptContext::returnValue | ( | ) | const |
Definition at line 419 of file qscriptcontext.cpp.
QScriptValueList QScriptContext::scopeChain | ( | ) | const |
Returns the scope chain of this QScriptContext.
Definition at line 712 of file qscriptcontext.cpp.
Referenced by QScriptDebuggerBackend::doPendingEvaluate(), and QScriptDebuggerCommandExecutor::execute().
void QScriptContext::setActivationObject | ( | const QScriptValue & | activation | ) |
Sets the activation object of this QScriptContext to be the given activation.
If activation is not an object, this function does nothing.
Definition at line 511 of file qscriptcontext.cpp.
Referenced by QScriptDebuggerBackend::doPendingEvaluate(), QDeclarativeInclude::finished(), QDeclarativeInclude::include(), QDeclarativeWorkerScriptEnginePrivate::processLoad(), and QDeclarativeInclude::worker_include().
void QScriptContext::setReturnValue | ( | const QScriptValue & | result | ) |
Definition at line 428 of file qscriptcontext.cpp.
void QScriptContext::setThisObject | ( | const QScriptValue & | thisObject | ) |
Sets the `this' object associated with this QScriptContext to be thisObject.
If thisObject is not an object, this function does nothing.
Definition at line 581 of file qscriptcontext.cpp.
Referenced by QScriptDebuggerBackend::doPendingEvaluate(), and QScriptEngine::importExtension().
QScriptContext::ExecutionState QScriptContext::state | ( | ) | const |
Returns the frameution state of this QScriptContext.
Definition at line 610 of file qscriptcontext.cpp.
Referenced by QScriptDebuggerCommandExecutor::execute().
QScriptValue QScriptContext::thisObject | ( | ) | const |
Returns the `this' object associated with this QScriptContext.
Definition at line 564 of file qscriptcontext.cpp.
Referenced by Node::attributes(), Node::childNodes(), QDeclarativeObjectMethodScriptClass::connect(), QScript::ClassObjectDelegate::construct(), QScriptDBusMessageConstructor::createErrorReply(), QScriptDBusMessageConstructor::createReply(), QDeclarativeObjectScriptClass::destroy(), QDeclarativeObjectMethodScriptClass::disconnect(), do_dbus_call(), Document::documentElement(), QScriptDebuggerBackend::doPendingEvaluate(), QScriptDebuggerCommandExecutor::execute(), Node::firstChild(), QJSDebuggerAgentPrivate::getLocals(), Text::isElementContentWhitespace(), Node::lastChild(), NamedNodeMap::length(), NodeList::length(), CharacterData::length(), Attr::name(), Node::nextSibling(), Node::nodeName(), Node::nodeType(), Node::nodeValue(), QDeclarativeWorkerScriptEnginePrivate::onMessage(), Attr::ownerElement(), Node::parentNode(), Node::previousSibling(), QScript::FunctionWrapper::proxyConstruct(), QScript::FunctionWithArgWrapper::proxyConstruct(), qmlsqldatabase_change_version(), qmlsqldatabase_executeSql(), qmlsqldatabase_item(), qmlsqldatabase_transaction_shared(), qmlxmlhttprequest_abort(), qmlxmlhttprequest_getAllResponseHeaders(), qmlxmlhttprequest_getResponseHeader(), qmlxmlhttprequest_new(), qmlxmlhttprequest_onreadystatechange(), qmlxmlhttprequest_open(), qmlxmlhttprequest_readyState(), qmlxmlhttprequest_responseText(), qmlxmlhttprequest_responseXML(), qmlxmlhttprequest_send(), qmlxmlhttprequest_setRequestHeader(), qmlxmlhttprequest_status(), qmlxmlhttprequest_statusText(), QDeclarativeWorkerScriptEnginePrivate::sendMessage(), QDeclarativeObjectScriptClass::tostring(), Attr::value(), Text::wholeText(), Document::xmlEncoding(), Document::xmlStandalone(), and Document::xmlVersion().
QScriptValue QScriptContext::throwError | ( | Error | error, |
const QString & | text | ||
) |
Throws an error with the given text.
Returns the created error object.
The text will be stored in the message
property of the error object.
The error object will be initialized to contain information about the location where the error occurred; specifically, it will have properties lineNumber
, fileName
and stack
. These properties are described in QtScript Extensions to ECMAScript.
Definition at line 193 of file qscriptcontext.cpp.
Referenced by QDeclarativeEnginePrivate::atob(), QDeclarativeEnginePrivate::btoa(), QDeclarativeObjectMethodScriptClass::callOverloaded(), QDeclarativeObjectMethodScriptClass::callPrecise(), QDeclarativeEnginePrivate::createComponent(), QDeclarativeEnginePrivate::createQmlObject(), QDeclarativeEnginePrivate::darker(), QDeclarativeObjectScriptClass::destroy(), QDeclarativeEnginePrivate::fontFamilies(), QDeclarativeEnginePrivate::formatDate(), QDeclarativeEnginePrivate::formatDateTime(), QDeclarativeEnginePrivate::formatTime(), QDeclarativeEnginePrivate::hsla(), QScriptEngine::importExtension(), QDeclarativeInclude::include(), QDeclarativeEnginePrivate::lighter(), QDeclarativeEnginePrivate::md5(), QDeclarativeEnginePrivate::point(), QScriptDebuggerBackendPrivate::qsassert(), QDeclarativeEnginePrivate::rect(), QDeclarativeEnginePrivate::rgba(), QDeclarativeGlobalScriptClass::setProperty(), QDeclarativeObjectScriptClass::setProperty(), QDeclarativeEnginePrivate::size(), QDeclarativeEnginePrivate::tint(), and QDeclarativeEnginePrivate::vector3d().
QScriptValue QScriptContext::throwError | ( | const QString & | text | ) |
Throws an error with the given text.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Returns the created error object.
Definition at line 232 of file qscriptcontext.cpp.
QScriptValue QScriptContext::throwValue | ( | const QScriptValue & | value | ) |
Throws an exception with the given value.
Returns the value thrown (the same as the argument).
Definition at line 170 of file qscriptcontext.cpp.
Referenced by QScriptDebuggerAgent::enterReturnByForceMode().
QString QScriptContext::toString | ( | ) | const |
Returns a string representation of this context.
This is useful for debugging.
Definition at line 651 of file qscriptcontext.cpp.
Referenced by backtrace().
|
private |
Definition at line 94 of file qscriptcontext.h.