|  | Home · All Classes · Modules

# QScriptContext Class Reference
[QtScript module]

The QScriptContext class represents a Qt Script function invocation. More...

### Types

- enum Error { UnknownError, ReferenceError, SyntaxError, TypeError, RangeError, URIError }
- enum ExecutionState { NormalState, ExceptionState }

### Methods

- QScriptValue activationObject (self)
- QScriptValue argument (self, int index)
- int argumentCount (self)
- QScriptValue argumentsObject (self)
- QStringList backtrace (self)
- QScriptValue callee (self)
- QScriptEngine engine (self)
- bool isCalledAsConstructor (self)
- QScriptContext parentContext (self)
- setActivationObject (self, QScriptValue activation)
- setThisObject (self, QScriptValue thisObject)
- ExecutionState state (self)
- QScriptValue thisObject (self)
- QScriptValue throwError (self, Error error, QString text)
- QScriptValue throwError (self, QString text)
- QScriptValue throwValue (self, QScriptValue value)

## Detailed Description

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

     foo(20.5, "hello", new Object())

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.

Use thisObject() to get the `this' object associated with the function call, and setThisObject() to set the `this' object.

Use isCalledAsConstructor() to determine if the function was called as a constructor (e.g. `"new foo()"` (as constructor) or just `"foo()"`).

Use throwValue() or throwError() to throw an exception.

Use callee() to obtain the QScriptValue that represents the function being called.

Use parentContext() to get a pointer to the context that precedes this context in the activation stack.

Use engine() to obtain a pointer to the QScriptEngine that this context resides in.

Use backtrace() to get a human-readable backtrace associated with this context. This can be useful for debugging purposes when implementing native functions.

See also QScriptEngine.newFunction() and QScriptable.

## Type Documentation

### QScriptContext.Error

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.

### QScriptContext.ExecutionState

This enum specifies the execution 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.

## Method Documentation

### QScriptValue QScriptContext.activationObject (self)

Returns the activation object of this QScriptContext. The activation object provides access to the local variables associated with this context.

See also setActivationObject(), argument(), and argumentsObject().

### QScriptValue QScriptContext.argument (self, int index)

Returns the function argument at the given index.

If index >= argumentCount(), a QScriptValue of the primitive type Undefined is returned.

See also argumentCount().

### int QScriptContext.argumentCount (self)

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()).

See also argument().

### QScriptValue QScriptContext.argumentsObject (self)

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.

See also argument() and activationObject().

### QStringList QScriptContext.backtrace (self)

Returns a human-readable backtrace of this QScriptContext.

Each line is of the form `<function-name>(<arguments>)@<file-name>:<line-number>`.

See also QScriptEngine.uncaughtExceptionBacktrace().

### QScriptValue QScriptContext.callee (self)

Returns the callee. The callee is the function object that this QScriptContext represents an invocation of.

### QScriptEngine QScriptContext.engine (self)

Returns the QScriptEngine that this QScriptContext belongs to.

### bool QScriptContext.isCalledAsConstructor (self)

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.

### QScriptContext QScriptContext.parentContext (self)

Returns the parent context of this QScriptContext.

### QScriptContext.setActivationObject (self, QScriptValue activation)

Sets the activation object of this QScriptContext to be the given activation.

See also activationObject().

### QScriptContext.setThisObject (self, QScriptValue thisObject)

Sets the `this' object associated with this QScriptContext to be thisObject.

See also thisObject().

### ExecutionState QScriptContext.state (self)

Returns the execution state of this QScriptContext.

### QScriptValue QScriptContext.thisObject (self)

Returns the `this' object associated with this QScriptContext.

See also setThisObject().

### QScriptValue QScriptContext.throwError (self, Error error, 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.

See also throwValue() and state().

### QScriptValue QScriptContext.throwError (self, QString text)

This is an overloaded member function, provided for convenience.

Throws an error with the given text. Returns the created error object.

See also throwValue() and state().

### QScriptValue QScriptContext.throwValue (self, QScriptValue value)

Throws an exception with the given value. Returns the value thrown (the same as the argument).

See also throwError() and state().