QScriptContext

Inheritance diagram of QScriptContext

Synopsis

Functions

Detailed Description

The PySide.QtScript.QScriptContext class represents a Qt Script function invocation.

A PySide.QtScript.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 :meth:`QScriptEngine.newFunction()<PySide.QtScript.QScriptEngine.newFunction> ) that will be called from script code. For example, when the script code

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

is evaluated, a PySide.QtScript.QScriptContext will be created, and the context will carry the arguments as QScriptValues; in this particular case, the arguments will be one PySide.QtScript.QScriptValue containing the number 20.5, a second PySide.QtScript.QScriptValue containing the string "hello" , and a third PySide.QtScript.QScriptValue containing a Qt Script object.

Use PySide.QtScript.QScriptContext.argumentCount() to get the number of arguments passed to the function, and PySide.QtScript.QScriptContext.argument() to get an argument at a certain index. The PySide.QtScript.QScriptContext.argumentsObject() function returns a Qt Script array object containing all the arguments; you can use the PySide.QtScript.QScriptValueIterator to iterate over its elements, or pass the array on as arguments to another script function using QScriptValue.call() .

Use PySide.QtScript.QScriptContext.thisObject() to get the this’ object associated with the function call, and :meth:`PySide.QtScript.QScriptContext.setThisObject to set the this’ object. If you are implementing a native “instance method”, you typically fetch the :meth:`PySide.QtScript.QScriptContext.thisObject and access one or more of its properties:

def Person_prototype_fullName(context, engine):
    self = context.selfObject()
    result = self.property("firstName").toString()
    result += QLatin1String(" ")
    result += self.property("lastName").toString()
    return result

Use PySide.QtScript.QScriptContext.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 PySide.QtScript.QScriptContext.thisObject() contains the newly constructed object that the function is expected to initialize.

Use PySide.QtScript.QScriptContext.throwValue() or PySide.QtScript.QScriptContext.throwError() to throw an exception.

Use PySide.QtScript.QScriptContext.callee() to obtain the PySide.QtScript.QScriptValue that represents the function being called. This can for example be used to call the function recursively.

Use PySide.QtScript.QScriptContext.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 PySide.QtScript.QScriptContext.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 PySide.QtScript.QScriptContext.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:

def myInclude(ctx, eng):
    fileName = ctx.argument(0).toString()
    contents = readTheFile(fileName)
    ctx.setActivationObject(ctx.parentContext().activationObject())
    ctx.setThisObject(ctx.parentContext().selfObject())
    return eng.evaluate(contents, fileName)

Use PySide.QtScript.QScriptContext.backtrace() to get a human-readable backtrace associated with this context. This can be useful for debugging purposes when implementing native functions. The PySide.QtScript.QScriptContext.toString() function provides a string representation of the context. ( PySide.QtScript.QScriptContextInfo provides more detailed debugging-related information about the PySide.QtScript.QScriptContext .)

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

PySide.QtScript.QScriptContext.Error

This enum specifies types of error.

Constant Description
QScriptContext.ReferenceError A reference error.
QScriptContext.SyntaxError A syntax error.
QScriptContext.TypeError A type error.
QScriptContext.RangeError A range error.
QScriptContext.URIError A URI error.
QScriptContext.UnknownError An unknown error.
PySide.QtScript.QScriptContext.ExecutionState

This enum specifies the frameution state of the context.

Constant Description
QScriptContext.NormalState The context is in a normal state.
QScriptContext.ExceptionState The context is in an exceptional state.
PySide.QtScript.QScriptContext.activationObject()
Return type:PySide.QtScript.QScriptValue

Returns the activation object of this PySide.QtScript.QScriptContext . 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 active PySide.QtScript.QScriptEngineAgent , as it might be optimized.

PySide.QtScript.QScriptContext.argument(index)
Parameters:indexPySide.QtCore.int
Return type:PySide.QtScript.QScriptValue

Returns the function argument at the given index .

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

PySide.QtScript.QScriptContext.argumentCount()
Return type:PySide.QtCore.int

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 PySide.QtScript.QScriptContext.callee() ).

PySide.QtScript.QScriptContext.argumentsObject()
Return type:PySide.QtScript.QScriptValue

Returns the arguments object of this PySide.QtScript.QScriptContext .

The arguments object has properties callee (equal to PySide.QtScript.QScriptContext.callee() ) and length (equal to PySide.QtScript.QScriptContext.argumentCount() ), and properties 0 , 1 , ..., PySide.QtScript.QScriptContext.argumentCount() - 1 that provide access to the argument values. Initially, property P (0 <= P < PySide.QtScript.QScriptContext.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 ( PySide.QtScript.QScriptContext.activationObject() ). This means that changing this property changes the corresponding property of the activation object and vice versa.

PySide.QtScript.QScriptContext.backtrace()
Return type:list of strings

Returns a human-readable backtrace of this PySide.QtScript.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 PySide.QtScript.QScriptContextInfo .

PySide.QtScript.QScriptContext.callee()
Return type:PySide.QtScript.QScriptValue

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

PySide.QtScript.QScriptContext.engine()
Return type:PySide.QtScript.QScriptEngine

Returns the PySide.QtScript.QScriptEngine that this PySide.QtScript.QScriptContext belongs to.

PySide.QtScript.QScriptContext.isCalledAsConstructor()
Return type:PySide.QtCore.bool

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 PySide.QtScript.QScriptContext.thisObject() contains the newly constructed object to be initialized.

Note

This function is only guaranteed to work for a context corresponding to native functions.

PySide.QtScript.QScriptContext.parentContext()
Return type:PySide.QtScript.QScriptContext

Returns the parent context of this PySide.QtScript.QScriptContext .

PySide.QtScript.QScriptContext.popScope()
Return type:PySide.QtScript.QScriptValue

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 PySide.QtScript.QScriptValue .

PySide.QtScript.QScriptContext.pushScope(object)
Parameters:objectPySide.QtScript.QScriptValue

Adds the given object to the front of this context’s scope chain.

If object is not an object, this function does nothing.

PySide.QtScript.QScriptContext.returnValue()
Return type:PySide.QtScript.QScriptValue
PySide.QtScript.QScriptContext.scopeChain()
Return type:

Returns the scope chain of this PySide.QtScript.QScriptContext .

PySide.QtScript.QScriptContext.setActivationObject(activation)
Parameters:activationPySide.QtScript.QScriptValue

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

If activation 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 an PySide.QtScript.QScriptEngineAgent active on the engine while the function was evaluated.

PySide.QtScript.QScriptContext.setReturnValue(result)
Parameters:resultPySide.QtScript.QScriptValue
PySide.QtScript.QScriptContext.setThisObject(thisObject)
Parameters:thisObjectPySide.QtScript.QScriptValue

Sets the this’ object associated with this :class:`PySide.QtScript.QScriptContext to be thisObject .

If thisObject is not an object, this function does nothing.

PySide.QtScript.QScriptContext.state()
Return type:PySide.QtScript.QScriptContext.ExecutionState

Returns the frameution state of this PySide.QtScript.QScriptContext .

PySide.QtScript.QScriptContext.thisObject()
Return type:PySide.QtScript.QScriptValue

Returns the this’ object associated with this :class:`PySide.QtScript.QScriptContext .

PySide.QtScript.QScriptContext.throwError(text)
Parameters:text – unicode
Return type:PySide.QtScript.QScriptValue

This is an overloaded function.

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

PySide.QtScript.QScriptContext.throwError(error, text)
Parameters:
Return type:

PySide.QtScript.QScriptValue

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 .

PySide.QtScript.QScriptContext.throwValue(value)
Parameters:valuePySide.QtScript.QScriptValue
Return type:PySide.QtScript.QScriptValue

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

PySide.QtScript.QScriptContext.toString()
Return type:unicode

Returns a string representation of this context. This is useful for debugging.