⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qscriptengine.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtScript module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qscriptengine.h"#ifndef QT_NO_SCRIPT#include "qscriptengine_p.h"#include "qscriptvalueimpl_p.h"#include "qscriptcontext_p.h"#include "qscriptmember_p.h"#include "qscriptobject_p.h"#include "qscriptsyntaxchecker_p.h"/*!  \since 4.3  \class QScriptEngine  \brief The QScriptEngine class provides an environment for evaluating Qt Script code.  \ingroup script  \mainclass  See the \l{QtScript} documentation for information about the Qt Script language,  and how to get started with scripting your C++ application.  Use evaluate() to evaluate script code.  \code    QScriptEngine myEngine;    QScriptValue three = myEngine.evaluate("1 + 2");  \endcode  evaluate() can throw a script exception (e.g. due to a syntax  error); in that case, the return value is the value that was thrown  (typically an \c{Error} object). You can check whether the  evaluation caused an exception by calling hasUncaughtException(). In  that case, you can call toString() on the error object to obtain an  error message. The current uncaught exception is also available  through uncaughtException(). You can obtain a human-readable  backtrace of the exception with uncaughtExceptionBacktrace().  \code    QScriptValue result = myEngine.evaluate(...);    if (myEngine.hasUncaughtException()) {        int line = myEngine.uncaughtExceptionLineNumber();        qDebug() << "uncaught exception at line" << line << ":" << result.toString();    }  \endcode  When handling possibly incomplete input, the canEvaluate() function  can be used to determine whether code can usefully be passed to  evaluate(). This can be useful when implementing tools that allow  code to be written incrementally, such as command line interpreters.  Use newObject() to create a standard Qt Script object. You can use  the object-specific functionality in QScriptValue to manipulate the  script object (e.g. QScriptValue::setProperty()). Use newArray() to  create a Qt script array object. Use newDate() to create a \c{Date}  object, and newRegExp() to create a \c{RegExp} object. Use  newVariant() to wrap a QVariant.  Use newQObject() to wrap a QObject (or subclass) pointer, and  newQMetaObject() to wrap a QMetaObject. When wrapping a QObject  pointer with newQObject(), properties, children and signals and  slots of the QObject will then become available to script code as  properties of the created Qt Script object.  No binding code is  needed because it is done dynamically using the Qt meta object  system. See the \l{QtScript} documentation for more information.  Use newFunction() to wrap native (C++) functions, including  constructors for your own custom types.  Use importExtension() to import plugin-based extensions into the  engine.  Use globalObject() to access the unique \bold {Global Object}  associated with the script engine. Properties of the Global Object  are accessible from any script code. Typically, you set properties  in the engine's Global Object to make your own extensions available  to scripts. Here is an example of how to expose a number value  through the Global Object:  \code    QScriptValue myNumber = QScriptValue(&myEngine, 123);    myEngine.globalObject().setProperty("myNumber", myNumber);    ...    QScriptValue myNumberPlusOne = myEngine.evaluate("myNumber + 1");  \endcode  In addition to exposing plain data, you can also write C++ functions  that can be invoked from script code. Such functions must have the  signature QScriptEngine::FunctionSignature. You may then pass the function  as argument to newFunction(). Here is an example of a function that  returns the sum of its first two arguments:  \code    QScriptValue myAdd(QScriptContext *context, QScriptEngine *engine)    {       QScriptValue a = context->argument(0);       QScriptValue b = context->argument(1);       return QScriptValue(engine, a.toNumber() + b.toNumber());    }  \endcode  To expose this function to script code, you can set it as a property  of the Global Object:  \code    QScriptValue fun = myEngine.newFunction(myAdd);    myEngine.globalObject().setProperty("myAdd", fun);  \endcode  Once this is done, script code can call your function in the exact  same manner as a "normal" script function:  \code    QScriptValue result = myEngine.evaluate("myAdd(myNumber, 1)");  \endcode  You can define shared script functionality for a custom C++ type  by creating your own default prototype object and setting it with  setDefaultPrototype(); see also QScriptable.  Use fromScriptValue() to cast from a QScriptValue to another type,  and toScriptValue() to create a QScriptValue from another value.  You can specify how the conversion of C++ types is to be performed  with qScriptRegisterMetaType() and qScriptRegisterSequenceMetaType().  \sa QScriptValue, QScriptContext*//*!    \enum QScriptEngine::ValueOwnership    This enum specifies the ownership when wrapping a C++ value, e.g. by using newQObject().    \value QtOwnership The standard Qt ownership rules apply, i.e. the associated object will never be explicitly deleted by the script engine. This is the default. (QObject ownership is explained in \l{Object Trees and Object Ownership}.)    \value ScriptOwnership The value is owned by the script environment. The associated data will be deleted when appropriate (i.e. after the garbage collector has discovered that there are no more live references to the value).    \value AutoOwnership If the associated object has a parent, the Qt ownership rules apply (QtOwnership); otherwise, the object is owned by the script environment (ScriptOwnership).*//*!    \enum  QScriptEngine::QObjectWrapOption    These flags specify options when wrapping a QObject pointer with newQObject().    \value ExcludeChildObjects The script object will not expose child objects as properties.    \value ExcludeSuperClassMethods The script object will not expose signals and slots inherited from the superclass.    \value ExcludeSuperClassProperties The script object will not expose properties inherited from the superclass.    \value AutoCreateDynamicProperties Properties that don't already exist in the QObject will be created as dynamic properties of that object, rather than as properties of the script object.*/#ifdef QT_NO_QOBJECTQScriptEngine::QScriptEngine()    : d_ptr(new QScriptEnginePrivate){    d_ptr->q_ptr = this;    d_ptr->init();}/*! \internal*/QScriptEngine::QScriptEngine(QScriptEnginePrivate &dd)    : d_ptr(&dd){    d_ptr->q_ptr = this;    d_ptr->init();}#else/*!    Constructs a QScriptEngine object.    The globalObject() is initialized to have properties as described in    \l{ECMA-262}, Section 15.1.*/QScriptEngine::QScriptEngine()    : QObject(*new QScriptEnginePrivate, 0){    Q_D(QScriptEngine);    d->init();}/*!    Constructs a QScriptEngine object with the given \a parent.    The globalObject() is initialized to have properties as described in    \l{ECMA-262}, Section 15.1.*/QScriptEngine::QScriptEngine(QObject *parent)    : QObject(*new QScriptEnginePrivate, parent){    Q_D(QScriptEngine);    d->init();}/*! \internal*/QScriptEngine::QScriptEngine(QScriptEnginePrivate &dd, QObject *parent)    : QObject(dd, parent){    Q_D(QScriptEngine);    d->init();}#endif/*!  Destroys this QScriptEngine.*/QScriptEngine::~QScriptEngine(){    Q_D(QScriptEngine);    d->popContext();    d->objectAllocator.destruct();#ifdef QT_NO_QOBJECT    delete d_ptr;    d_ptr = 0;#endif}/*!  Returns this engine's Global Object.  The Global Object contains the built-in objects that are part of  \l{ECMA-262}, such as Math, Date and String. Additionally, you can  set properties of the Global Object to make your own extensions  available to all script code. Non-local variables in script code  will be created as properties of the Global Object, as well as local  variables in global code.*/QScriptValue QScriptEngine::globalObject() const{    Q_D(const QScriptEngine);    return d->m_globalObject;}/*!  Returns a QScriptValue of the primitive type Null.  \sa undefinedValue()*/QScriptValue QScriptEngine::nullValue(){    Q_D(QScriptEngine);    return d->nullValue();}/*!  Returns a QScriptValue of the primitive type Undefined.  \sa nullValue()*/QScriptValue QScriptEngine::undefinedValue(){    Q_D(QScriptEngine);    return d->undefinedValue();}/*!  Creates a constructor function from \a fun, with the given \a length.  The \c{prototype} property of the resulting function is set to be the  given \a prototype. The \c{constructor} property of \a prototype is  set to be the resulting function.  When a function is called as a constructor (e.g. \c{new Foo()}), the  `this' object associated with the function call is the new object  that the function is expected to initialize; the prototype of this  default constructed object will be the function's public  \c{prototype} property. If you always want the function to behave as  a constructor (e.g. \c{Foo()} should also create a new object), or  if you need to create your own object rather than using the default  `this' object, you should make sure that the prototype of your  object is set correctly; either by setting it manually, or, when  wrapping a custom type, by having registered the defaultPrototype()  of that type. Example:  \code  QScriptValue Foo(QScriptContext *context, QScriptEngine *engine)  {      if (context->calledAsConstructor()) {          // initialize the new object          context->thisObject().setProperty("bar", ...);          // ...          // return a non-object value to indicate that the          // thisObject() should be the result of the "new Foo()" expression          return engine->undefinedValue();      } else {          // not called as "new Foo()", just "Foo()"          // create our own object and return that one          QScriptValue object = engine->newObject();          object.setPrototype(context->callee().property("prototype"));          object.setProperty("baz", ...);          return object;      }  }  ...  QScriptValue fooProto = engine->newObject();  fooProto.setProperty("whatever", ...);  engine->globalObject().setProperty("Foo", engine->newFunction(Foo, fooProto));  \endcode  To wrap a custom type and provide a constructor for it, you'd typically  do something like this:  \code  class Bar { ... };  Q_DECLARE_METATYPE(Bar)  QScriptValue constructBar(QScriptContext *context, QScriptEngine *engine)  {      Bar bar;      // initialize from arguments in context, if desired      ...      return engine->toScriptValue(bar);  }  class BarPrototype : public QObject, public QScriptable  {  // provide the scriptable interface of this type using slots and properties  ...  };  ...  // create and register the Bar prototype and constructor in the engine  BarPrototype *barPrototypeObject = new BarPrototype(...);  QScriptValue barProto = engine->newQObject(barPrototypeObject);  engine->setDefaultPrototype(qMetaTypeId<Bar>, barProto);  QScriptValue barCtor = engine->newFunction(constructBar, barProto);  engine->globalObject().setProperty("Bar", barCtor);  \endcode*/QScriptValue QScriptEngine::newFunction(QScriptEngine::FunctionSignature fun,                                        const QScriptValue &prototype,                                        int length){    Q_D(QScriptEngine);    QScriptValueImpl v = d->createFunction(new QScript::CFunction(fun, length));    QScriptValueImpl proto = QScriptValuePrivate::valueOf(prototype);    v.setProperty(d->idTable()->id_prototype, proto,                  QScriptValue::Undeletable);    proto.setProperty(d->idTable()->id_constructor, v,                      QScriptValue::Undeletable                      | QScriptValue::SkipInEnumeration);    return v;}#ifndef QT_NO_REGEXP/*!  Creates a QtScript object of class RegExp with the given  \a regexp.  \sa QScriptValue::toRegExp()*/QScriptValue QScriptEngine::newRegExp(const QRegExp &regexp){    Q_D(QScriptEngine);    QScriptValueImpl v;    d->regexpConstructor->newRegExp(&v, regexp);    return v;}#endif // QT_NO_REGEXP/*!  Creates a QtScript object holding the given variant \a value.  If a default prototype has been registered with the meta type id of  \a value, then the prototype of the created object will be that  prototype; otherwise, the prototype will be the Object prototype  object.  \sa setDefaultPrototype(), QScriptValue::toVariant()*/QScriptValue QScriptEngine::newVariant(const QVariant &value){    Q_D(QScriptEngine);    return d->newVariant(value);}#ifndef QT_NO_QOBJECT/*!  Creates a QtScript object that wraps the given QObject \a  object, using the given \a ownership. The given \a options control  various aspects of the interaction with the resulting script object.  Signals and slots, properties and children of \a object are  available as properties of the created QScriptValue. For more  information, see the \l{QtScript} documentation.  If \a object is a null pointer, this function returns nullValue().  If the given \a object is deleted outside of QtScript's control, any  attempt to access the deleted QObject's members through the QtScript  wrapper object (either by script code or C++) will result in a  script exception.  \sa QScriptValue::toQObject()*/QScriptValue QScriptEngine::newQObject(QObject *object, ValueOwnership ownership,                                       const QObjectWrapOptions &options){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -