Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QScriptContext Class

TheQScriptContext class represents a Qt Script function invocation.More...

Header:#include <QScriptContext>
Since: Qt 4.3

Public Types

enumError { ReferenceError, SyntaxError, TypeError, RangeError, URIError, UnknownError }
enumExecutionState { NormalState, ExceptionState }

Public Functions

~QScriptContext()
QScriptValueactivationObject() const
QScriptValueargument(int index) const
intargumentCount() const
QScriptValueargumentsObject() const
QStringListbacktrace() const
QScriptValuecallee() const
QScriptEngine *engine() const
boolisCalledAsConstructor() const
QScriptContext *parentContext() const
voidsetActivationObject(const QScriptValue & activation)
voidsetThisObject(const QScriptValue & thisObject)
ExecutionStatestate() const
QScriptValuethisObject() const
QScriptValuethrowError(Error error, const QString & text)
QScriptValuethrowError(const QString & text)
QScriptValuethrowValue(const QScriptValue & value)
QStringtoString() const

Detailed Description

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.

Member Type Documentation

enum QScriptContext::Error

This enum specifies types of error.

ConstantValueDescription
QScriptContext::ReferenceError1A reference error.
QScriptContext::SyntaxError2A syntax error.
QScriptContext::TypeError3A type error.
QScriptContext::RangeError4A range error.
QScriptContext::URIError5A URI error.
QScriptContext::UnknownError0An unknown error.

enum QScriptContext::ExecutionState

This enum specifies the frameution state of the context.

ConstantValueDescription
QScriptContext::NormalState0The context is in a normal state.
QScriptContext::ExceptionState1The context is in an exceptional state.

Member Function Documentation

QScriptContext::~QScriptContext()

Destroys thisQScriptContext.

QScriptValue QScriptContext::activationObject() const

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

QScriptValue QScriptContext::argument(int index) const

Returns the function argument at the givenindex.

Ifindex >=argumentCount(), aQScriptValue of the primitive type Undefined is returned.

See alsoargumentCount().

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 (thelength property ofcallee()).

See alsoargument().

QScriptValue QScriptContext::argumentsObject() const

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

QStringList QScriptContext::backtrace() const

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

QScriptValue QScriptContext::callee() const

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

QScriptEngine * QScriptContext::engine() const

Returns theQScriptEngine that thisQScriptContext belongs to.

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, thethisObject() contains the newly constructed object to be initialized.

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

QScriptContext * QScriptContext::parentContext() const

Returns the parent context of thisQScriptContext.

void QScriptContext::setActivationObject(constQScriptValue & activation)

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

void QScriptContext::setThisObject(constQScriptValue & thisObject)

Sets the `this' object associated with thisQScriptContext to bethisObject.

IfthisObject is not an object, this function does nothing.

See alsothisObject().

ExecutionState QScriptContext::state() const

Returns the frameution state of thisQScriptContext.

QScriptValue QScriptContext::thisObject() const

Returns the `this' object associated with thisQScriptContext.

See alsosetThisObject().

QScriptValue QScriptContext::throwError(Error error, constQString & text)

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

QScriptValue QScriptContext::throwError(constQString & text)

This is an overloaded function.

Throws an error with the giventext. Returns the created error object.

See alsothrowValue() andstate().

QScriptValue QScriptContext::throwValue(constQScriptValue & value)

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

See alsothrowError() andstate().

QString QScriptContext::toString() const

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.


[8]ページ先頭

©2009-2025 Movatter.jp