
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQScriptContext class represents a Qt Script function invocation.More...
| Header: | #include <QScriptContext> |
| Since: | Qt 4.3 |
| enum | Error { ReferenceError, SyntaxError, TypeError, RangeError, URIError, UnknownError } |
| enum | ExecutionState { NormalState, ExceptionState } |
| ~QScriptContext() | |
| QScriptValue | activationObject() const |
| QScriptValue | argument(int index) const |
| int | argumentCount() const |
| QScriptValue | argumentsObject() const |
| QStringList | backtrace() const |
| QScriptValue | callee() const |
| QScriptEngine * | engine() const |
| bool | isCalledAsConstructor() const |
| QScriptContext * | parentContext() const |
| void | setActivationObject(const QScriptValue & activation) |
| void | setThisObject(const QScriptValue & thisObject) |
| ExecutionState | state() const |
| QScriptValue | thisObject() const |
| QScriptValue | throwError(Error error, const QString & text) |
| QScriptValue | throwError(const QString & text) |
| QScriptValue | throwValue(const QScriptValue & value) |
| QString | toString() const |
TheQScriptContext class represents a Qt Script function invocation.
AQScriptContext 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 (seeQScriptEngine::newFunction()) that will be called from script code. For example, when the script code
foo(20.5,"hello",new Object())
is evaluated, aQScriptContext will be created, and the context will carry the arguments as QScriptValues; in this particular case, the arguments will be oneQScriptValue containing the number 20.5, a secondQScriptValue containing the string"hello", and a thirdQScriptValue containing a Qt Script object.
UseargumentCount() to get the number of arguments passed to the function, andargument() to get an argument at a certain index. TheargumentsObject() function returns a Qt Script array object containing all the arguments; you can use theQScriptValueIterator to iterate over its elements, or pass the array on as arguments to another script function usingQScriptValue::call().
UsethisObject() to get the `this' object associated with the function call, andsetThisObject() to set the `this' object. If you are implementing a native "instance method", you typically fetch thethisObject() and access one or more of its properties:
QScriptValue Person_prototype_fullName(QScriptContext*context,QScriptEngine*engine){QScriptValue self= context->thisObject();QString result; result+= self.property("firstName").toString(); result+= QLatin1String(" "); result+= self.property("lastName").toString();return result;}
UseisCalledAsConstructor() 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, thethisObject() contains the newly constructed object that the function is expected to initialize.
UsethrowValue() orthrowError() to throw an exception.
Usecallee() to obtain theQScriptValue that represents the function being called. This can for example be used to call the function recursively.
UseparentContext() 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).
TheactivationObject() function returns the object that is used to hold the local variables associated with this function call. You can replace the activation object by callingsetActivationObject(). 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:
QScriptValue myInclude(QScriptContext*ctx,QScriptEngine*eng){QString fileName= ctx->argument(0).toString();QString contents= readTheFile(fileName); ctx->setActivationObject(ctx->parentContext()->activationObject()); ctx->setThisObject(ctx->parentContext()->thisObject());return eng->evaluate(contents, fileName);}
Usebacktrace() to get a human-readable backtrace associated with this context. This can be useful for debugging purposes when implementing native functions. ThetoString() function provides a string representation of the context. (QScriptContextInfo provides more detailed debugging-related information about theQScriptContext.)
Useengine() to obtain a pointer to theQScriptEngine that this context resides in.
See alsoQScriptContextInfo,QScriptEngine::newFunction(), andQScriptable.
This enum specifies types of error.
| Constant | Value | Description |
|---|---|---|
QScriptContext::ReferenceError | 1 | A reference error. |
QScriptContext::SyntaxError | 2 | A syntax error. |
QScriptContext::TypeError | 3 | A type error. |
QScriptContext::RangeError | 4 | A range error. |
QScriptContext::URIError | 5 | A URI error. |
QScriptContext::UnknownError | 0 | An unknown error. |
This enum specifies the frameution state of the context.
| Constant | Value | Description |
|---|---|---|
QScriptContext::NormalState | 0 | The context is in a normal state. |
QScriptContext::ExceptionState | 1 | The context is in an exceptional state. |
Destroys thisQScriptContext.
Returns the activation object of thisQScriptContext. The activation object provides access to the local variables associated with this context.
Note:The activation object might not be available if there is no activeQScriptEngineAgent, as it might be optimized.
See alsosetActivationObject(),argument(), andargumentsObject().
Returns the function argument at the givenindex.
Ifindex >=argumentCount(), aQScriptValue of the primitive type Undefined is returned.
See alsoargumentCount().
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 (thelength property ofcallee()).
See alsoargument().
Returns the arguments object of thisQScriptContext.
The arguments object has propertiescallee (equal tocallee()) andlength (equal toargumentCount()), and properties0,1, ...,argumentCount() - 1 that provide access to the argument values. Initially, propertyP (0 <=P <argumentCount()) has the same value as argument(P). In the case whenP 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.
See alsoargument() andactivationObject().
Returns a human-readable backtrace of thisQScriptContext.
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), useQScriptContextInfo.
See alsoQScriptEngine::uncaughtExceptionBacktrace(),QScriptContextInfo, andtoString().
Returns the callee. The callee is the function object that thisQScriptContext represents an invocation of.
Returns theQScriptEngine that thisQScriptContext belongs to.
Returns true if the function was called as a constructor (e.g."new foo()"); otherwise returns false.
When a function is called as constructor, thethisObject() contains the newly constructed object to be initialized.
Note:This function is only guaranteed to work for a context corresponding to native functions.
Returns the parent context of thisQScriptContext.
Sets the activation object of thisQScriptContext to be the givenactivation.
Ifactivation is not an object, this function does nothing.
Note:For a context corresponding to a JavaScript function, this is only guaranteed to work if there was anQScriptEngineAgent active on the engine while the function was evaluated.
See alsoactivationObject().
Sets the `this' object associated with thisQScriptContext to bethisObject.
IfthisObject is not an object, this function does nothing.
See alsothisObject().
Returns the frameution state of thisQScriptContext.
Returns the `this' object associated with thisQScriptContext.
See alsosetThisObject().
Throws anerror with the giventext. Returns the created error object.
Thetext will be stored in themessage 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 propertieslineNumber,fileName andstack. These properties are described inQtScript Extensions to ECMAScript.
See alsothrowValue() andstate().
This is an overloaded function.
Throws an error with the giventext. Returns the created error object.
See alsothrowValue() andstate().
Throws an exception with the givenvalue. Returns the value thrown (the same as the argument).
See alsothrowError() andstate().
Returns a string representation of this context. This is useful for debugging.
This function was introduced in Qt 4.4.
See alsobacktrace().
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.