Qt 4.8
Public Types | Public Functions | Private Functions | Properties | List of all members
QScriptContext Class Reference

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

Detailed Description

The QScriptContext class represents a Qt Script function invocation.

Since
4.3

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. 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:

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;
}

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:

{
QString contents = readTheFile(fileName);
return eng->evaluate(contents, fileName);
}

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.

See also
QScriptContextInfo, QScriptEngine::newFunction(), QScriptable

Definition at line 39 of file qscriptcontext.h.

Enumerations

◆ Error

This enum specifies types of error.

  • ReferenceError A reference error.
  • SyntaxError A syntax error.
  • TypeError A type error.
  • RangeError A range error.
  • URIError A URI error.
  • UnknownError An unknown error.
Enumerator
UnknownError 
ReferenceError 
SyntaxError 
TypeError 
RangeError 
URIError 

Definition at line 47 of file qscriptcontext.h.

◆ ExecutionState

This enum specifies the frameution state of the context.

  • NormalState The context is in a normal state.
  • ExceptionState The context is in an exceptional state.
Enumerator
NormalState 
ExceptionState 

Definition at line 42 of file qscriptcontext.h.

Constructors and Destructors

◆ ~QScriptContext()

QScriptContext::~QScriptContext ( )

Destroys this QScriptContext.

Definition at line 243 of file qscriptcontext.cpp.

244 {
245  //QScriptContext doesn't exist, pointer to QScriptContext are just pointer to JSC::CallFrame
246  Q_ASSERT(false);
247 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823

◆ QScriptContext()

QScriptContext::QScriptContext ( )
private
Warning
This function is not part of the public interface.

Definition at line 158 of file qscriptcontext.cpp.

159 {
160  //QScriptContext doesn't exist, pointer to QScriptContext are just pointer to JSC::CallFrame
161  Q_ASSERT(false);
162 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823

Functions

◆ activationObject()

QScriptValue QScriptContext::activationObject ( ) const

Returns the activation object of this 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 QScriptEngineAgent, as it might be optimized.
See also
argument(), argumentsObject()

Definition at line 450 of file qscriptcontext.cpp.

Referenced by QScriptDebuggerBackend::doPendingEvaluate(), QScriptDebuggerCommandExecutor::execute(), QJSDebuggerAgentPrivate::getLocals(), QScriptEngine::importExtension(), popScope(), pushScope(), scopeChain(), and QScriptDeclarativeClass::scopeChainValue().

451 {
452  JSC::CallFrame *frame = const_cast<JSC::ExecState*>(QScriptEnginePrivate::frameForContext(this));
454  JSC::JSObject *result = 0;
455 
458  //For native functions, lazily create it if needed
460  frame->setScopeChain(frame->scopeChain()->copy()->push(scope));
461  result = scope;
462  QScriptEnginePrivate::setContextFlags(frame, flags | QScriptEnginePrivate::HasScopeContext);
463  } else {
464  // look in scope chain
465  JSC::ScopeChainNode *node = frame->scopeChain();
466  JSC::ScopeChainIterator it(node);
467  for (it = node->begin(); it != node->end(); ++it) {
468  if ((*it) && (*it)->isVariableObject()) {
469  result = *it;
470  break;
471  }
472  }
473  }
474  if (!result) {
475  if (!parentContext())
476  return engine()->globalObject();
477 
478  qWarning("QScriptContext::activationObject: could not get activation object for frame");
479  return QScriptValue();
480  /*JSC::CodeBlock *codeBlock = frame->codeBlock();
481  if (!codeBlock) {
482  // non-Qt native function
483  Q_ASSERT(true); //### this should in theorry not happen
484  result = new (frame)QScript::QScriptActivationObject(frame);
485  } else {
486  // ### this is wrong
487  JSC::FunctionBodyNode *body = static_cast<JSC::FunctionBodyNode*>(codeBlock->ownerNode());
488  result = new (frame)JSC::JSActivation(frame, body);
489  }*/
490  }
491 
492  if (result && result->inherits(&QScript::QScriptActivationObject::info)
493  && (static_cast<QScript::QScriptActivationObject*>(result)->delegate() != 0)) {
494  // Return the object that property access is being delegated to
495  result = static_cast<QScript::QScriptActivationObject*>(result)->delegate();
496  }
497 
499 }
static JSC::ExecState * frameForContext(QScriptContext *context)
QScriptEnginePrivate * scriptEngineFromExec(const JSC::ExecState *exec)
#define it(className, varName)
static uint contextFlags(JSC::ExecState *)
For native context, we use the ReturnValueRegister entry in the stackframe header to store flags...
QScriptValue globalObject() const
Returns this engine&#39;s Global Object.
QScriptEngine * engine() const
Returns the QScriptEngine that this QScriptContext belongs to.
static void setContextFlags(JSC::ExecState *, uint)
Q_CORE_EXPORT void qWarning(const char *,...)
unsigned int uint
Definition: qglobal.h:996
QScriptValue scriptValueFromJSCValue(JSC::JSValue value)
Represent a scope for native function call.
ExecState CallFrame
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
QScriptContext * parentContext() const
Returns the parent context of this QScriptContext.

◆ argument()

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.

See also
argumentCount()

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

267 {
268  if (index < 0)
269  return QScriptValue();
270  if (index >= argumentCount())
273  return v;
274 }
QScriptValue property(const QString &name, const ResolveFlags &mode=ResolvePrototype) const
Returns the value of this QScriptValue&#39;s property with the given name, using the given mode to resolv...
QScriptValue argumentsObject() const
Returns the arguments object of this QScriptContext.
int argumentCount() const
Returns the number of arguments passed to the function in this invocation.
quint16 index
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57

◆ argumentCount()

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

See also
argument()

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

408 {
410  int argc = frame->argumentCount();
411  if (argc != 0)
412  --argc; // -1 due to "this"
413  return argc;
414 }
static JSC::ExecState * frameForContext(QScriptContext *context)
ExecState CallFrame

◆ argumentsObject()

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.

See also
argument(), activationObject()

Definition at line 308 of file qscriptcontext.cpp.

Referenced by argument(), QScript::GlobalObject::getOwnPropertyDescriptor(), and QScript::GlobalObject::getOwnPropertySlot().

309 {
310  JSC::CallFrame *frame = const_cast<JSC::ExecState*>(QScriptEnginePrivate::frameForContext(this));
312 
313  if (frame == frame->lexicalGlobalObject()->globalExec()) {
314  // <global> context doesn't have arguments. return an empty object
316  }
317 
318  //for a js function
319  if (frame->codeBlock() && frame->callee()) {
321  // We have a built-in JS host call.
322  // codeBlock is needed by retrieveArguments(), but since it
323  // contains junk, we would crash. Return an invalid value for now.
324  return QScriptValue();
325  }
326  JSC::JSValue result = frame->interpreter()->retrieveArguments(frame, JSC::asFunction(frame->callee()));
328  }
329 
330  if (frame->callerFrame()->hasHostCallFrameFlag()) {
331  // <eval> context doesn't have arguments. return an empty object
333  }
334 
335  //for a native function
336  if (!frame->optionalCalleeArguments()
337  && QScriptEnginePrivate::hasValidCodeBlockRegister(frame)) { // Make sure we don't go here for host JSFunctions
338  Q_ASSERT(frame->argumentCount() > 0); //we need at least 'this' otherwise we'll crash later
339  JSC::Arguments* arguments = new (&frame->globalData())JSC::Arguments(frame, JSC::Arguments::NoParameters);
340  frame->setCalleeArguments(arguments);
341  }
342  return QScript::scriptEngineFromExec(frame)->scriptValueFromJSCValue(frame->optionalCalleeArguments());
343 }
static JSC::ExecState * frameForContext(QScriptContext *context)
QScriptEnginePrivate * scriptEngineFromExec(const JSC::ExecState *exec)
static QScriptEnginePrivate * get(QScriptEngine *q)
JSC::JSValue newObject()
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QScriptValue scriptValueFromJSCValue(JSC::JSValue value)
ExecState CallFrame
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
static bool hasValidCodeBlockRegister(JSC::ExecState *frame)

◆ backtrace()

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.

See also
QScriptEngine::uncaughtExceptionBacktrace(), QScriptContextInfo, toString()

Definition at line 629 of file qscriptcontext.cpp.

Referenced by QScriptDebuggerBackend::backtrace().

630 {
631  QStringList result;
632  const QScriptContext *ctx = this;
633  while (ctx) {
634  result.append(ctx->toString());
635  ctx = ctx->parentContext();
636  }
637  return result;
638 }
The QScriptContext class represents a Qt Script function invocation.
QString toString() const
Returns a string representation of this context.
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
#define ctx
Definition: qgl.cpp:6094
QScriptContext * parentContext() const
Returns the parent context of this QScriptContext.

◆ callee()

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

281 {
284  QScript::APIShim shim(eng);
285  if (frame->callee() == eng->originalGlobalObject()) {
286  // This is a pushContext()-created context; the callee is a lie.
288  return QScriptValue();
289  }
290  return eng->scriptValueFromJSCValue(frame->callee());
291 }
static JSC::ExecState * frameForContext(QScriptContext *context)
QScriptEnginePrivate * scriptEngineFromExec(const JSC::ExecState *exec)
static uint contextFlags(JSC::ExecState *)
For native context, we use the ReturnValueRegister entry in the stackframe header to store flags...
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
JSC::JSGlobalObject * originalGlobalObject() const
QScriptValue scriptValueFromJSCValue(JSC::JSValue value)
ExecState CallFrame
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57

◆ engine()

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

253 {
256 }
static JSC::ExecState * frameForContext(QScriptContext *context)
QScriptEnginePrivate * scriptEngineFromExec(const JSC::ExecState *exec)
static QScriptEnginePrivate * get(QScriptEngine *q)
ExecState CallFrame

◆ isCalledAsConstructor()

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.

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

Definition at line 355 of file qscriptcontext.cpp.

Referenced by qmlxmlhttprequest_new().

356 {
357  JSC::CallFrame *frame = const_cast<JSC::ExecState*>(QScriptEnginePrivate::frameForContext(this));
359 
360  //For native functions, look up flags.
364 
365  //Not a native function, try to look up in the bytecode if we where called from op_construct
366  JSC::Instruction* returnPC = frame->returnPC();
367 
368  if (!returnPC)
369  return false;
370 
372  if (!callerFrame)
373  return false;
374 
375  if (returnPC[-JSC::op_construct_length].u.opcode == frame->interpreter()->getOpcode(JSC::op_construct)) {
376  //We are maybe called from the op_construct opcode which has 6 opperands.
377  //But we need to check we are not called from op_call with 4 opperands
378 
379  //we make sure that the returnPC[-1] (thisRegister) is smaller than the returnPC[-3] (registerOffset)
380  //as if it was an op_call, the returnPC[-1] would be the registerOffset, bigger than returnPC[-3] (funcRegister)
381  return returnPC[-1].u.operand < returnPC[-3].u.operand;
382  }
383  return false;
384 }
static JSC::ExecState * frameForContext(QScriptContext *context)
QScriptEnginePrivate * scriptEngineFromExec(const JSC::ExecState *exec)
static uint contextFlags(JSC::ExecState *)
For native context, we use the ReturnValueRegister entry in the stackframe header to store flags...
quint16 u
unsigned int uint
Definition: qglobal.h:996
ExecState CallFrame
QScriptContext * parentContext() const
Returns the parent context of this QScriptContext.

◆ parentContext()

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

390 {
393  JSC::CallFrame *callerFrame = frame->callerFrame()->removeHostCallFrameFlag();
394  return QScriptEnginePrivate::contextForFrame(callerFrame);
395 }
static JSC::ExecState * frameForContext(QScriptContext *context)
QScriptEnginePrivate * scriptEngineFromExec(const JSC::ExecState *exec)
static QScriptContext * contextForFrame(JSC::ExecState *frame)
ExecState CallFrame

◆ popScope()

QScriptValue QScriptContext::popScope ( )

Removes the front object from this context's scope chain, and returns the removed object.

Warning
This function is not part of the public interface.
Since
4.5

If the scope chain is already empty, this function returns an invalid QScriptValue.

Definition at line 790 of file qscriptcontext.cpp.

791 {
792  activationObject(); //ensure the creation of the normal scope for native context
794  JSC::ScopeChainNode *scope = frame->scopeChain();
795  Q_ASSERT(scope != 0);
797  QScript::APIShim shim(engine);
798  QScriptValue result = engine->scriptValueFromJSCValue(scope->object);
799  if (!scope->next) {
800  // We cannot have a null scope chain, so just zap the object pointer.
801  scope->object = 0;
802  } else {
803  frame->setScopeChain(scope->pop());
804  }
805  return result;
806 }
static JSC::ExecState * frameForContext(QScriptContext *context)
QScriptEnginePrivate * scriptEngineFromExec(const JSC::ExecState *exec)
QScriptEngine * engine() const
Returns the QScriptEngine that this QScriptContext belongs to.
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QScriptValue scriptValueFromJSCValue(JSC::JSValue value)
QScriptValue activationObject() const
Returns the activation object of this QScriptContext.
ExecState CallFrame
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57

◆ pushScope()

void QScriptContext::pushScope ( const QScriptValue object)

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

Warning
This function is not part of the public interface.
Since
4.5

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

747 {
748  activationObject(); //ensure the creation of the normal scope for native context
749  if (!object.isObject())
750  return;
751  else if (object.engine() != engine()) {
752  qWarning("QScriptContext::pushScope() failed: "
753  "cannot push an object created in "
754  "a different engine");
755  return;
756  }
759  QScript::APIShim shim(engine);
760  JSC::JSObject *jscObject = JSC::asObject(engine->scriptValueToJSCValue(object));
761  if (jscObject == engine->originalGlobalObjectProxy)
762  jscObject = engine->originalGlobalObject();
763  JSC::ScopeChainNode *scope = frame->scopeChain();
764  Q_ASSERT(scope != 0);
765  if (!scope->object) {
766  // pushing to an "empty" chain
767  if (!jscObject->isGlobalObject()) {
768  qWarning("QScriptContext::pushScope() failed: initial object in scope chain has to be the Global Object");
769  return;
770  }
771  scope->object = jscObject;
772  }
773  else
774  frame->setScopeChain(scope->push(jscObject));
775 }
static JSC::ExecState * frameForContext(QScriptContext *context)
QScriptEnginePrivate * scriptEngineFromExec(const JSC::ExecState *exec)
QScriptEngine * engine() const
Returns the QScriptEngine that this QScriptContext belongs to.
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
JSC::JSValue scriptValueToJSCValue(const QScriptValue &value)
JSC::JSGlobalObject * originalGlobalObject() const
Q_CORE_EXPORT void qWarning(const char *,...)
JSC::JSObject * originalGlobalObjectProxy
QScriptValue activationObject() const
Returns the activation object of this QScriptContext.
ExecState CallFrame

◆ returnValue()

QScriptValue QScriptContext::returnValue ( ) const
Warning
This function is not part of the public interface.

Definition at line 419 of file qscriptcontext.cpp.

420 {
421  qWarning("QScriptContext::returnValue() not implemented");
422  return QScriptValue();
423 }
Q_CORE_EXPORT void qWarning(const char *,...)
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57

◆ scopeChain()

QScriptValueList QScriptContext::scopeChain ( ) const

Returns the scope chain of this QScriptContext.

Warning
This function is not part of the public interface.
Since
4.5

Definition at line 712 of file qscriptcontext.cpp.

Referenced by QScriptDebuggerBackend::doPendingEvaluate(), and QScriptDebuggerCommandExecutor::execute().

713 {
714  activationObject(); //ensure the creation of the normal scope for native context
717  QScript::APIShim shim(engine);
718  QScriptValueList result;
719  JSC::ScopeChainNode *node = frame->scopeChain();
720  JSC::ScopeChainIterator it(node);
721  for (it = node->begin(); it != node->end(); ++it) {
722  JSC::JSObject *object = *it;
723  if (!object)
724  continue;
725  if (object->inherits(&QScript::QScriptActivationObject::info)
726  && (static_cast<QScript::QScriptActivationObject*>(object)->delegate() != 0)) {
727  // Return the object that property access is being delegated to
728  object = static_cast<QScript::QScriptActivationObject*>(object)->delegate();
729  }
730  result.append(engine->scriptValueFromJSCValue(object));
731  }
732  return result;
733 }
static JSC::ExecState * frameForContext(QScriptContext *context)
QScriptEnginePrivate * scriptEngineFromExec(const JSC::ExecState *exec)
#define it(className, varName)
QScriptEngine * engine() const
Returns the QScriptEngine that this QScriptContext belongs to.
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
QScriptValue scriptValueFromJSCValue(JSC::JSValue value)
QScriptValue activationObject() const
Returns the activation object of this QScriptContext.
Represent a scope for native function call.
ExecState CallFrame

◆ setActivationObject()

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.

Note
For a context corresponding to a JavaScript function, this is only guaranteed to work if there was an QScriptEngineAgent active on the engine while the function was evaluated.

Definition at line 511 of file qscriptcontext.cpp.

Referenced by QScriptDebuggerBackend::doPendingEvaluate(), QDeclarativeInclude::finished(), QDeclarativeInclude::include(), QDeclarativeWorkerScriptEnginePrivate::processLoad(), and QDeclarativeInclude::worker_include().

512 {
513  if (!activation.isObject())
514  return;
515  else if (activation.engine() != engine()) {
516  qWarning("QScriptContext::setActivationObject() failed: "
517  "cannot set an object created in "
518  "a different engine");
519  return;
520  }
523  QScript::APIShim shim(engine);
524  JSC::JSObject *object = JSC::asObject(engine->scriptValueToJSCValue(activation));
525  if (object == engine->originalGlobalObjectProxy)
526  object = engine->originalGlobalObject();
527 
530  //For native functions, we create a scope node
531  JSC::JSObject *scope = object;
532  if (!scope->isVariableObject()) {
533  // Create a QScriptActivationObject that acts as a proxy
534  scope = new (frame) QScript::QScriptActivationObject(frame, scope);
535  }
536  frame->setScopeChain(frame->scopeChain()->copy()->push(scope));
537  QScriptEnginePrivate::setContextFlags(frame, flags | QScriptEnginePrivate::HasScopeContext);
538  return;
539  }
540 
541  // else replace the first activation object in the scope chain
542  JSC::ScopeChainNode *node = frame->scopeChain();
543  while (node != 0) {
544  if (node->object && node->object->isVariableObject()) {
545  if (!object->isVariableObject()) {
546  if (node->object->inherits(&QScript::QScriptActivationObject::info)) {
547  static_cast<QScript::QScriptActivationObject*>(node->object)->setDelegate(object);
548  } else {
549  // Create a QScriptActivationObject that acts as a proxy
550  node->object = new (frame) QScript::QScriptActivationObject(frame, object);
551  }
552  } else {
553  node->object = object;
554  }
555  break;
556  }
557  node = node->next;
558  }
559 }
static JSC::ExecState * frameForContext(QScriptContext *context)
QScriptEnginePrivate * scriptEngineFromExec(const JSC::ExecState *exec)
static uint contextFlags(JSC::ExecState *)
For native context, we use the ReturnValueRegister entry in the stackframe header to store flags...
QScriptEngine * engine() const
Returns the QScriptEngine that this QScriptContext belongs to.
static void setContextFlags(JSC::ExecState *, uint)
JSC::JSValue scriptValueToJSCValue(const QScriptValue &value)
JSC::JSGlobalObject * originalGlobalObject() const
QScriptEngine * engine() const
Returns the QScriptEngine that created this QScriptValue, or 0 if this QScriptValue is invalid or the...
Q_CORE_EXPORT void qWarning(const char *,...)
unsigned int uint
Definition: qglobal.h:996
JSC::JSObject * originalGlobalObjectProxy
Represent a scope for native function call.
ExecState CallFrame
bool isObject() const
Returns true if this QScriptValue is of the Object type; otherwise returns false. ...

◆ setReturnValue()

void QScriptContext::setReturnValue ( const QScriptValue result)
Warning
This function is not part of the public interface.

Definition at line 428 of file qscriptcontext.cpp.

429 {
431  JSC::CallFrame *callerFrame = frame->callerFrame();
432  if (!callerFrame->codeBlock())
433  return;
434  Q_ASSERT_X(false, Q_FUNC_INFO, "check me");
435  int dst = frame->registers()[JSC::RegisterFile::ReturnValueRegister].i(); // returnValueRegister() is private
436  callerFrame[dst] = QScript::scriptEngineFromExec(frame)->scriptValueToJSCValue(result);
437 }
static JSC::ExecState * frameForContext(QScriptContext *context)
QScriptEnginePrivate * scriptEngineFromExec(const JSC::ExecState *exec)
JSC::JSValue scriptValueToJSCValue(const QScriptValue &value)
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
ExecState CallFrame
#define Q_FUNC_INFO
Definition: qglobal.h:1871

◆ setThisObject()

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

582 {
585  if (!thisObject.isObject())
586  return;
587  if (thisObject.engine() != engine()) {
588  qWarning("QScriptContext::setThisObject() failed: "
589  "cannot set an object created in "
590  "a different engine");
591  return;
592  }
593  if (frame == frame->lexicalGlobalObject()->globalExec()) {
594  engine()->setGlobalObject(thisObject);
595  return;
596  }
597  JSC::JSValue jscThisObject = QScript::scriptEngineFromExec(frame)->scriptValueToJSCValue(thisObject);
598  JSC::CodeBlock *cb = frame->codeBlock();
599  if (cb != 0) {
600  frame[cb->thisRegister()] = jscThisObject;
601  } else {
602  JSC::Register* thisRegister = QScriptEnginePrivate::thisRegisterForFrame(frame);
603  thisRegister[0] = jscThisObject;
604  }
605 }
static JSC::ExecState * frameForContext(QScriptContext *context)
QScriptEnginePrivate * scriptEngineFromExec(const JSC::ExecState *exec)
static JSC::Register * thisRegisterForFrame(JSC::ExecState *frame)
QScriptEngine * engine() const
Returns the QScriptEngine that this QScriptContext belongs to.
JSC::JSValue scriptValueToJSCValue(const QScriptValue &value)
QScriptEngine * engine() const
Returns the QScriptEngine that created this QScriptValue, or 0 if this QScriptValue is invalid or the...
Q_CORE_EXPORT void qWarning(const char *,...)
void setGlobalObject(const QScriptValue &object)
Sets this engine&#39;s Global Object to be the given object.
ExecState CallFrame
bool isObject() const
Returns true if this QScriptValue is of the Object type; otherwise returns false. ...

◆ state()

QScriptContext::ExecutionState QScriptContext::state ( ) const

Returns the frameution state of this QScriptContext.

Definition at line 610 of file qscriptcontext.cpp.

Referenced by QScriptDebuggerCommandExecutor::execute().

611 {
613  if (frame->hadException())
616 }
static JSC::ExecState * frameForContext(QScriptContext *context)
ExecState CallFrame

◆ thisObject()

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

565 {
566  JSC::CallFrame *frame = const_cast<JSC::ExecState*>(QScriptEnginePrivate::frameForContext(this));
568  QScript::APIShim shim(engine);
569  JSC::JSValue result = engine->thisForContext(frame);
570  if (!result || result.isNull())
571  result = frame->globalThisValue();
572  return engine->scriptValueFromJSCValue(result);
573 }
static JSC::ExecState * frameForContext(QScriptContext *context)
QScriptEnginePrivate * scriptEngineFromExec(const JSC::ExecState *exec)
QScriptEngine * engine() const
Returns the QScriptEngine that this QScriptContext belongs to.
QScriptValue scriptValueFromJSCValue(JSC::JSValue value)
ExecState CallFrame
static JSC::JSValue thisForContext(JSC::ExecState *frame)

◆ throwError() [1/2]

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.

See also
throwValue(), state()

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

194 {
197  JSC::ErrorType jscError = JSC::GeneralError;
198  switch (error) {
199  case UnknownError:
200  break;
201  case ReferenceError:
202  jscError = JSC::ReferenceError;
203  break;
204  case SyntaxError:
205  jscError = JSC::SyntaxError;
206  break;
207  case TypeError:
208  jscError = JSC::TypeError;
209  break;
210  case RangeError:
211  jscError = JSC::RangeError;
212  break;
213  case URIError:
214  jscError = JSC::URIError;
215  break;
216  }
217  JSC::JSObject *result = JSC::throwError(frame, jscError, text);
219 }
static JSC::ExecState * frameForContext(QScriptContext *context)
QScriptEnginePrivate * scriptEngineFromExec(const JSC::ExecState *exec)
#define error(msg)
QScriptValue scriptValueFromJSCValue(JSC::JSValue value)
ExecState CallFrame

◆ throwError() [2/2]

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.

See also
throwValue(), state()

Definition at line 232 of file qscriptcontext.cpp.

233 {
236  JSC::JSObject *result = JSC::throwError(frame, JSC::GeneralError, text);
238 }
static JSC::ExecState * frameForContext(QScriptContext *context)
QScriptEnginePrivate * scriptEngineFromExec(const JSC::ExecState *exec)
QScriptValue scriptValueFromJSCValue(JSC::JSValue value)
ExecState CallFrame

◆ throwValue()

QScriptValue QScriptContext::throwValue ( const QScriptValue value)

Throws an exception with the given value.

Returns the value thrown (the same as the argument).

See also
throwError(), state()

Definition at line 170 of file qscriptcontext.cpp.

Referenced by QScriptDebuggerAgent::enterReturnByForceMode().

171 {
174  JSC::JSValue jscValue = QScript::scriptEngineFromExec(frame)->scriptValueToJSCValue(value);
175  frame->setException(jscValue);
176  return value;
177 }
static JSC::ExecState * frameForContext(QScriptContext *context)
QScriptEnginePrivate * scriptEngineFromExec(const JSC::ExecState *exec)
JSC::JSValue scriptValueToJSCValue(const QScriptValue &value)
ExecState CallFrame

◆ toString()

QString QScriptContext::toString ( ) const

Returns a string representation of this context.

Since
4.4

This is useful for debugging.

See also
backtrace()

Definition at line 651 of file qscriptcontext.cpp.

Referenced by backtrace().

652 {
653  QScriptContextInfo info(this);
654  QString result;
655 
656  QString functionName = info.functionName();
657  if (functionName.isEmpty()) {
658  if (parentContext()) {
660  if (info.functionType() == QScriptContextInfo::ScriptFunction)
661  result.append(QLatin1String("<anonymous>"));
662  else if(frame->callerFrame()->hasHostCallFrameFlag())
663  result.append(QLatin1String("<eval>"));
664  else
665  result.append(QLatin1String("<native>"));
666  } else {
667  result.append(QLatin1String("<global>"));
668  }
669  } else {
670  result.append(functionName);
671  }
672 
673  QStringList parameterNames = info.functionParameterNames();
674  result.append(QLatin1Char('('));
675  for (int i = 0; i < argumentCount(); ++i) {
676  if (i > 0)
677  result.append(QLatin1String(", "));
678  if (i < parameterNames.count()) {
679  result.append(parameterNames.at(i));
680  result.append(QLatin1String(" = "));
681  }
682  QScriptValue arg = argument(i);
683  if (arg.isString())
684  result.append(QLatin1Char('\''));
685  result.append(arg.toString());
686  if (arg.isString())
687  result.append(QLatin1Char('\''));
688 
689  }
690  result.append(QLatin1Char(')'));
691 
692  QString fileName = info.fileName();
693  int lineNumber = info.lineNumber();
694  result.append(QLatin1String(" at "));
695  if (!fileName.isEmpty()) {
696  result.append(fileName);
697  result.append(QLatin1Char(':'));
698  }
699  result.append(QString::number(lineNumber));
700  return result;
701 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
static JSC::ExecState * frameForContext(QScriptContext *context)
static mach_timebase_info_data_t info
QString toString() const
Returns the string value of this QScriptValue, as defined in ECMA-262 section 9.8, "ToString".
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
bool isString() const
Returns true if this QScriptValue is of the primitive type String; otherwise returns false...
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
int argumentCount() const
Returns the number of arguments passed to the function in this invocation.
The QScriptContextInfo class provides additional information about a QScriptContext.
QString & append(QChar c)
Definition: qstring.cpp:1777
ExecState CallFrame
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
static QString fileName(const QString &fileUrl)
QScriptValue argument(int index) const
Returns the function argument at the given index.
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
QScriptContext * parentContext() const
Returns the parent context of this QScriptContext.

Properties

◆ d_ptr

QScriptContextPrivate* QScriptContext::d_ptr
private

Definition at line 94 of file qscriptcontext.h.


The documentation for this class was generated from the following files: