src/script/api/qscriptengine.h
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtScript module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef QSCRIPTENGINE_H
       
    43 #define QSCRIPTENGINE_H
       
    44 
       
    45 #include <QtCore/qmetatype.h>
       
    46 
       
    47 #include <QtCore/qvariant.h>
       
    48 #include <QtCore/qsharedpointer.h>
       
    49 
       
    50 #ifndef QT_NO_QOBJECT
       
    51 #include <QtCore/qobject.h>
       
    52 #else
       
    53 #include <QtCore/qobjectdefs.h>
       
    54 #endif
       
    55 
       
    56 #include <QtScript/qscriptvalue.h>
       
    57 #include <QtScript/qscriptcontext.h>
       
    58 #include <QtScript/qscriptstring.h>
       
    59 
       
    60 QT_BEGIN_HEADER
       
    61 
       
    62 QT_BEGIN_NAMESPACE
       
    63 
       
    64 QT_MODULE(Script)
       
    65 
       
    66 class QDateTime;
       
    67 class QScriptClass;
       
    68 class QScriptEngineAgent;
       
    69 class QScriptEnginePrivate;
       
    70 
       
    71 #ifndef QT_NO_QOBJECT
       
    72 
       
    73 template <class T>
       
    74 inline QScriptValue qscriptQMetaObjectConstructor(QScriptContext *, QScriptEngine *, T *)
       
    75 {
       
    76     return QScriptValue();
       
    77 }
       
    78 
       
    79 #endif // QT_NO_QOBJECT
       
    80 
       
    81 #ifndef QT_NO_REGEXP
       
    82 class QRegExp;
       
    83 #endif
       
    84 
       
    85 #ifndef QT_NO_MEMBER_TEMPLATES
       
    86 template <typename T>
       
    87 inline QScriptValue qScriptValueFromValue(QScriptEngine *, const T &);
       
    88 
       
    89 template <typename T>
       
    90 inline T qScriptValueToValue(const QScriptValue &);
       
    91 #endif
       
    92 
       
    93 class QScriptSyntaxCheckResultPrivate;
       
    94 class Q_SCRIPT_EXPORT QScriptSyntaxCheckResult
       
    95 {
       
    96 public:
       
    97     enum State {
       
    98         Error,
       
    99         Intermediate,
       
   100         Valid
       
   101     };
       
   102 
       
   103     QScriptSyntaxCheckResult(const QScriptSyntaxCheckResult &other);
       
   104     ~QScriptSyntaxCheckResult();
       
   105 
       
   106     State state() const;
       
   107     int errorLineNumber() const;
       
   108     int errorColumnNumber() const;
       
   109     QString errorMessage() const;
       
   110 
       
   111     QScriptSyntaxCheckResult &operator=(const QScriptSyntaxCheckResult &other);
       
   112 
       
   113 private:
       
   114     QScriptSyntaxCheckResult();
       
   115     QScriptSyntaxCheckResult(QScriptSyntaxCheckResultPrivate *d);
       
   116     QExplicitlySharedDataPointer<QScriptSyntaxCheckResultPrivate> d_ptr;
       
   117 
       
   118     Q_DECLARE_PRIVATE(QScriptSyntaxCheckResult)
       
   119     friend class QScriptEngine;
       
   120     friend class QScriptEnginePrivate;
       
   121 };
       
   122 
       
   123 class Q_SCRIPT_EXPORT QScriptEngine
       
   124 #ifndef QT_NO_QOBJECT
       
   125     : public QObject
       
   126 #endif
       
   127 {
       
   128 #ifndef QT_NO_QOBJECT
       
   129     Q_OBJECT
       
   130 #endif
       
   131 public:
       
   132     enum ValueOwnership {
       
   133         QtOwnership,
       
   134         ScriptOwnership,
       
   135         AutoOwnership
       
   136     };
       
   137 
       
   138     enum QObjectWrapOption {
       
   139         ExcludeChildObjects = 0x0001,
       
   140         ExcludeSuperClassMethods = 0x0002,
       
   141         ExcludeSuperClassProperties = 0x0004,
       
   142         ExcludeSuperClassContents = 0x0006,
       
   143         SkipMethodsInEnumeration = 0x0008,
       
   144         ExcludeDeleteLater = 0x0010,
       
   145 
       
   146         AutoCreateDynamicProperties = 0x0100,
       
   147         PreferExistingWrapperObject = 0x0200
       
   148     };
       
   149     Q_DECLARE_FLAGS(QObjectWrapOptions, QObjectWrapOption)
       
   150 
       
   151     QScriptEngine();
       
   152 #ifndef QT_NO_QOBJECT
       
   153     explicit QScriptEngine(QObject *parent);
       
   154 #endif
       
   155     virtual ~QScriptEngine();
       
   156 
       
   157     QScriptValue globalObject() const;
       
   158     void setGlobalObject(const QScriptValue &object);
       
   159 
       
   160     QScriptContext *currentContext() const;
       
   161     QScriptContext *pushContext();
       
   162     void popContext();
       
   163 
       
   164     bool canEvaluate(const QString &program) const;
       
   165     static QScriptSyntaxCheckResult checkSyntax(const QString &program);
       
   166 
       
   167     QScriptValue evaluate(const QString &program, const QString &fileName = QString(), int lineNumber = 1);
       
   168 
       
   169     bool isEvaluating() const;
       
   170     void abortEvaluation(const QScriptValue &result = QScriptValue());
       
   171 
       
   172     bool hasUncaughtException() const;
       
   173     QScriptValue uncaughtException() const;
       
   174     int uncaughtExceptionLineNumber() const;
       
   175     QStringList uncaughtExceptionBacktrace() const;
       
   176     void clearExceptions();
       
   177 
       
   178     QScriptValue nullValue();
       
   179     QScriptValue undefinedValue();
       
   180 
       
   181     typedef QScriptValue (*FunctionSignature)(QScriptContext *, QScriptEngine *);
       
   182     typedef QScriptValue (*FunctionWithArgSignature)(QScriptContext *, QScriptEngine *, void *);
       
   183 
       
   184     QScriptValue newFunction(FunctionSignature signature, int length = 0);
       
   185     QScriptValue newFunction(FunctionSignature signature, const QScriptValue &prototype, int length = 0);
       
   186 
       
   187     QScriptValue newFunction(FunctionWithArgSignature signature, void *arg);
       
   188 
       
   189     QScriptValue newVariant(const QVariant &value);
       
   190     QScriptValue newVariant(const QScriptValue &object, const QVariant &value);
       
   191 
       
   192 #ifndef QT_NO_REGEXP
       
   193     QScriptValue newRegExp(const QRegExp &regexp);
       
   194 #endif
       
   195 
       
   196     QScriptValue newObject();
       
   197     QScriptValue newObject(QScriptClass *scriptClass, const QScriptValue &data = QScriptValue());
       
   198     QScriptValue newArray(uint length = 0);
       
   199     QScriptValue newRegExp(const QString &pattern, const QString &flags);
       
   200     QScriptValue newDate(qsreal value);
       
   201     QScriptValue newDate(const QDateTime &value);
       
   202     QScriptValue newActivationObject();
       
   203 
       
   204 #ifndef QT_NO_QOBJECT
       
   205     QScriptValue newQObject(QObject *object, ValueOwnership ownership = QtOwnership,
       
   206                             const QObjectWrapOptions &options = 0);
       
   207     QScriptValue newQObject(const QScriptValue &scriptObject, QObject *qtObject,
       
   208                             ValueOwnership ownership = QtOwnership,
       
   209                             const QObjectWrapOptions &options = 0);
       
   210 
       
   211     QScriptValue newQMetaObject(const QMetaObject *metaObject, const QScriptValue &ctor = QScriptValue());
       
   212 
       
   213 #  ifndef QT_NO_MEMBER_TEMPLATES
       
   214     template <class T> QScriptValue scriptValueFromQMetaObject();
       
   215 #  endif // QT_NO_MEMBER_TEMPLATES
       
   216 
       
   217 #endif // QT_NO_QOBJECT
       
   218 
       
   219 
       
   220 
       
   221     QScriptValue defaultPrototype(int metaTypeId) const;
       
   222     void setDefaultPrototype(int metaTypeId, const QScriptValue &prototype);
       
   223 
       
   224 
       
   225     typedef QScriptValue (*MarshalFunction)(QScriptEngine *, const void *);
       
   226     typedef void (*DemarshalFunction)(const QScriptValue &, void *);
       
   227 
       
   228 
       
   229 
       
   230 #ifndef QT_NO_MEMBER_TEMPLATES
       
   231     template <typename T>
       
   232     inline QScriptValue toScriptValue(const T &value)
       
   233     {
       
   234         return qScriptValueFromValue(this, value);
       
   235     }
       
   236     template <typename T>
       
   237     inline T fromScriptValue(const QScriptValue &value)
       
   238     {
       
   239         return qScriptValueToValue<T>(value);
       
   240     }
       
   241 #endif // QT_NO_MEMBER_TEMPLATES
       
   242 
       
   243     void installTranslatorFunctions(const QScriptValue &object = QScriptValue());
       
   244 
       
   245     QScriptValue importExtension(const QString &extension);
       
   246     QStringList availableExtensions() const;
       
   247     QStringList importedExtensions() const;
       
   248 
       
   249     void collectGarbage();
       
   250 
       
   251     void setProcessEventsInterval(int interval);
       
   252     int processEventsInterval() const;
       
   253 
       
   254     void setAgent(QScriptEngineAgent *agent);
       
   255     QScriptEngineAgent *agent() const;
       
   256 
       
   257     QScriptString toStringHandle(const QString &str);
       
   258     QScriptValue toObject(const QScriptValue &value);
       
   259 
       
   260     QScriptValue objectById(qint64 id) const;
       
   261 
       
   262 #ifndef QT_NO_QOBJECT
       
   263 Q_SIGNALS:
       
   264     void signalHandlerException(const QScriptValue &exception);
       
   265 #endif
       
   266 
       
   267 private:
       
   268     QScriptValue create(int type, const void *ptr);
       
   269 
       
   270     bool convert(const QScriptValue &value, int type, void *ptr);
       
   271     static bool convertV2(const QScriptValue &value, int type, void *ptr);
       
   272 
       
   273     void registerCustomType(int type, MarshalFunction mf, DemarshalFunction df,
       
   274                             const QScriptValue &prototype);
       
   275 
       
   276     friend inline void qScriptRegisterMetaType_helper(QScriptEngine *,
       
   277         int, MarshalFunction, DemarshalFunction, const QScriptValue &);
       
   278 
       
   279     friend inline QScriptValue qScriptValueFromValue_helper(QScriptEngine *, int, const void *);
       
   280 
       
   281     friend inline bool qscriptvalue_cast_helper(const QScriptValue &, int, void *);
       
   282 
       
   283 protected:
       
   284 #ifdef QT_NO_QOBJECT
       
   285     QScopedPointer<QScriptEnginePrivate> d_ptr;
       
   286 
       
   287     QScriptEngine(QScriptEnginePrivate &dd);
       
   288 #else
       
   289     QScriptEngine(QScriptEnginePrivate &dd, QObject *parent = 0);
       
   290 #endif
       
   291 
       
   292 private:
       
   293     Q_DECLARE_PRIVATE(QScriptEngine)
       
   294     Q_DISABLE_COPY(QScriptEngine)
       
   295 #ifndef QT_NO_QOBJECT
       
   296     Q_PRIVATE_SLOT(d_func(), void _q_objectDestroyed(QObject *))
       
   297 #endif
       
   298 };
       
   299 
       
   300 #ifndef QT_NO_QOBJECT
       
   301 template <class T>
       
   302 inline QScriptValue qScriptValueFromQMetaObject(
       
   303     QScriptEngine *engine
       
   304 #ifndef qdoc
       
   305     , T * /* dummy */ = 0
       
   306 #endif
       
   307     )
       
   308 {
       
   309     typedef QScriptValue(*ConstructPtr)(QScriptContext *, QScriptEngine *, T *);
       
   310     ConstructPtr cptr = qscriptQMetaObjectConstructor<T>;
       
   311     return engine->newQMetaObject(&T::staticMetaObject,
       
   312                                   engine->newFunction(reinterpret_cast<QScriptEngine::FunctionWithArgSignature>(cptr), 0));
       
   313 }
       
   314 
       
   315 #define Q_SCRIPT_DECLARE_QMETAOBJECT(T, _Arg1) \
       
   316 template<> inline QScriptValue qscriptQMetaObjectConstructor<T>(QScriptContext *ctx, QScriptEngine *eng, T *) \
       
   317 { \
       
   318     _Arg1 arg1 = qscriptvalue_cast<_Arg1> (ctx->argument(0)); \
       
   319     T* t = new T(arg1); \
       
   320     if (ctx->isCalledAsConstructor()) \
       
   321         return eng->newQObject(ctx->thisObject(), t, QScriptEngine::AutoOwnership); \
       
   322     QScriptValue o = eng->newQObject(t, QScriptEngine::AutoOwnership); \
       
   323     o.setPrototype(ctx->callee().property(QString::fromLatin1("prototype"))); \
       
   324     return o; \
       
   325 }
       
   326 
       
   327 #  ifndef QT_NO_MEMBER_TEMPLATES
       
   328     template <class T> QScriptValue QScriptEngine::scriptValueFromQMetaObject()
       
   329     {
       
   330         return qScriptValueFromQMetaObject<T>(this);
       
   331     }
       
   332 #  endif // QT_NO_MEMBER_TEMPLATES
       
   333 
       
   334 #endif // QT_NO_QOBJECT
       
   335 
       
   336 inline QScriptValue qScriptValueFromValue_helper(QScriptEngine *engine, int type, const void *ptr)
       
   337 {
       
   338     if (!engine)
       
   339         return QScriptValue();
       
   340 
       
   341     return engine->create(type, ptr);
       
   342 }
       
   343 
       
   344 template <typename T>
       
   345 inline QScriptValue qScriptValueFromValue(QScriptEngine *engine, const T &t)
       
   346 {
       
   347     return qScriptValueFromValue_helper(engine, qMetaTypeId<T>(), &t);
       
   348 }
       
   349 
       
   350 template <>
       
   351 inline QScriptValue qScriptValueFromValue<QVariant>(QScriptEngine *engine, const QVariant &v)
       
   352 {
       
   353     QScriptValue result = qScriptValueFromValue_helper(engine, v.userType(), v.data());
       
   354     if (!result.isValid())
       
   355         result = engine->newVariant(v);
       
   356     return result;
       
   357 }
       
   358 
       
   359 inline bool qscriptvalue_cast_helper(const QScriptValue &value, int type, void *ptr)
       
   360 {
       
   361     return QScriptEngine::convertV2(value, type, ptr);
       
   362 }
       
   363 
       
   364 template<typename T>
       
   365 T qscriptvalue_cast(const QScriptValue &value
       
   366 #if !defined qdoc && defined Q_CC_MSVC && _MSC_VER < 1300
       
   367 , T * = 0
       
   368 #endif
       
   369     )
       
   370 {
       
   371     T t;
       
   372     const int id = qMetaTypeId<T>();
       
   373 
       
   374     if (qscriptvalue_cast_helper(value, id, &t))
       
   375         return t;
       
   376     else if (value.isVariant())
       
   377         return qvariant_cast<T>(value.toVariant());
       
   378 
       
   379     return T();
       
   380 }
       
   381 
       
   382 #if !defined Q_CC_MSVC || _MSC_VER >= 1300
       
   383 template <>
       
   384 inline QVariant qscriptvalue_cast<QVariant>(const QScriptValue &value)
       
   385 {
       
   386     return value.toVariant();
       
   387 }
       
   388 #endif
       
   389 
       
   390 template <typename T>
       
   391 inline T qScriptValueToValue(const QScriptValue &value)
       
   392 {
       
   393     return qscriptvalue_cast<T>(value);
       
   394 }
       
   395 
       
   396 inline void qScriptRegisterMetaType_helper(QScriptEngine *eng, int type,
       
   397                                            QScriptEngine::MarshalFunction mf,
       
   398                                            QScriptEngine::DemarshalFunction df,
       
   399                                            const QScriptValue &prototype)
       
   400 {
       
   401     eng->registerCustomType(type, mf, df, prototype);
       
   402 }
       
   403 
       
   404 template<typename T>
       
   405 int qScriptRegisterMetaType(
       
   406     QScriptEngine *eng,
       
   407     QScriptValue (*toScriptValue)(QScriptEngine *, const T &t),
       
   408     void (*fromScriptValue)(const QScriptValue &, T &t),
       
   409     const QScriptValue &prototype = QScriptValue()
       
   410 #ifndef qdoc
       
   411     , T * /* dummy */ = 0
       
   412 #endif
       
   413 )
       
   414 {
       
   415     const int id = qRegisterMetaType<T>(); // make sure it's registered
       
   416 
       
   417     qScriptRegisterMetaType_helper(
       
   418         eng, id, reinterpret_cast<QScriptEngine::MarshalFunction>(toScriptValue),
       
   419         reinterpret_cast<QScriptEngine::DemarshalFunction>(fromScriptValue),
       
   420         prototype);
       
   421 
       
   422     return id;
       
   423 }
       
   424 
       
   425 template <class Container>
       
   426 QScriptValue qScriptValueFromSequence(QScriptEngine *eng, const Container &cont)
       
   427 {
       
   428     QScriptValue a = eng->newArray();
       
   429     typename Container::const_iterator begin = cont.begin();
       
   430     typename Container::const_iterator end = cont.end();
       
   431     typename Container::const_iterator it;
       
   432     quint32 i;
       
   433     for (it = begin, i = 0; it != end; ++it, ++i)
       
   434         a.setProperty(i, qScriptValueFromValue(eng, *it));
       
   435     return a;
       
   436 }
       
   437 
       
   438 template <class Container>
       
   439 void qScriptValueToSequence(const QScriptValue &value, Container &cont)
       
   440 {
       
   441     quint32 len = value.property(QLatin1String("length")).toUInt32();
       
   442     for (quint32 i = 0; i < len; ++i) {
       
   443         QScriptValue item = value.property(i);
       
   444 #if defined Q_CC_MSVC && !defined Q_CC_MSVC_NET
       
   445         cont.push_back(qscriptvalue_cast<Container::value_type>(item));
       
   446 #else
       
   447         cont.push_back(qscriptvalue_cast<typename Container::value_type>(item));
       
   448 #endif
       
   449     }
       
   450 }
       
   451 
       
   452 template<typename T>
       
   453 int qScriptRegisterSequenceMetaType(
       
   454     QScriptEngine *engine,
       
   455     const QScriptValue &prototype = QScriptValue()
       
   456 #ifndef qdoc
       
   457     , T * /* dummy */ = 0
       
   458 #endif
       
   459 )
       
   460 {
       
   461     return qScriptRegisterMetaType<T>(engine, qScriptValueFromSequence,
       
   462                                       qScriptValueToSequence, prototype);
       
   463 }
       
   464 
       
   465 #ifndef QT_NO_QOBJECT
       
   466 Q_SCRIPT_EXPORT bool qScriptConnect(QObject *sender, const char *signal,
       
   467                                     const QScriptValue &receiver,
       
   468                                     const QScriptValue &function);
       
   469 Q_SCRIPT_EXPORT bool qScriptDisconnect(QObject *sender, const char *signal,
       
   470                                        const QScriptValue &receiver,
       
   471                                        const QScriptValue &function);
       
   472 #endif // QT_NO_QOBJECT
       
   473 
       
   474 Q_DECLARE_OPERATORS_FOR_FLAGS(QScriptEngine::QObjectWrapOptions)
       
   475 
       
   476 QT_END_NAMESPACE
       
   477 
       
   478 QT_END_HEADER
       
   479 
       
   480 #endif // QSCRIPTENGINE_H