]> git.saurik.com Git - apple/javascriptcore.git/blobdiff - qt/api/qscriptengine.cpp
JavaScriptCore-903.tar.gz
[apple/javascriptcore.git] / qt / api / qscriptengine.cpp
index d49c578162c1b7702d16eaccb297179ac52ef5e7..607b0b9dcd6f81bc022fd783a1f648b85f2bf8e5 100644 (file)
@@ -25,6 +25,8 @@
 #include "qscriptprogram_p.h"
 #include "qscriptsyntaxcheckresult_p.h"
 #include "qscriptvalue_p.h"
+#include <QtCore/qdatetime.h>
+#include <QtCore/qnumeric.h>
 
 /*!
     Constructs a QScriptEngine object.
@@ -95,6 +97,71 @@ QScriptValue QScriptEngine::evaluate(const QScriptProgram& program)
     return QScriptValuePrivate::get(d_ptr->evaluate(QScriptProgramPrivate::get(program)));
 }
 
+/*!
+    Returns true if the last script evaluation resulted in an uncaught
+    exception; otherwise returns false.
+
+    The exception state is cleared when evaluate() is called.
+
+    \sa uncaughtException(), uncaughtExceptionLineNumber(),
+      uncaughtExceptionBacktrace()
+*/
+bool QScriptEngine::hasUncaughtException() const
+{
+    return d_ptr->hasUncaughtException();
+}
+
+/*!
+    Returns the current uncaught exception, or an invalid QScriptValue
+    if there is no uncaught exception.
+
+    The exception value is typically an \c{Error} object; in that case,
+    you can call toString() on the return value to obtain an error
+    message.
+
+    \sa hasUncaughtException(), uncaughtExceptionLineNumber(),
+      uncaughtExceptionBacktrace()
+*/
+QScriptValue QScriptEngine::uncaughtException() const
+{
+    return QScriptValuePrivate::get(d_ptr->uncaughtException());
+}
+
+/*!
+    Clears any uncaught exceptions in this engine.
+
+    \sa hasUncaughtException()
+*/
+void QScriptEngine::clearExceptions()
+{
+    d_ptr->clearExceptions();
+}
+
+/*!
+    Returns the line number where the last uncaught exception occurred.
+
+    Line numbers are 1-based, unless a different base was specified as
+    the second argument to evaluate().
+
+    \sa hasUncaughtException(), uncaughtExceptionBacktrace()
+*/
+int QScriptEngine::uncaughtExceptionLineNumber() const
+{
+    return d_ptr->uncaughtExceptionLineNumber();
+}
+
+/*!
+    Returns a human-readable backtrace of the last uncaught exception.
+
+    Each line is of the form \c{<function-name>(<arguments>)@<file-name>:<line-number>}.
+
+    \sa uncaughtException()
+*/
+QStringList QScriptEngine::uncaughtExceptionBacktrace() const
+{
+    return d_ptr->uncaughtExceptionBacktrace();
+}
+
 /*!
     Runs the garbage collector.
 
@@ -150,6 +217,28 @@ QScriptString QScriptEngine::toStringHandle(const QString& str)
     return QScriptStringPrivate::get(d_ptr->toStringHandle(str));
 }
 
+/*!
+  Converts the given \a value to an object, if such a conversion is
+  possible; otherwise returns an invalid QScriptValue. The conversion
+  is performed according to the following table:
+
+    \table
+    \header \o Input Type \o Result
+    \row    \o Undefined  \o An invalid QScriptValue.
+    \row    \o Null       \o An invalid QScriptValue.
+    \row    \o Boolean    \o A new Boolean object whose internal value is set to the value of the boolean.
+    \row    \o Number     \o A new Number object whose internal value is set to the value of the number.
+    \row    \o String     \o A new String object whose internal value is set to the value of the string.
+    \row    \o Object     \o The result is the object itself (no conversion).
+    \endtable
+
+    \sa newObject()
+*/
+QScriptValue QScriptEngine::toObject(const QScriptValue& value)
+{
+    return QScriptValuePrivate::get(QScriptValuePrivate::get(value)->toObject(d_ptr.data()));
+}
+
 /*!
   Returns a QScriptValue of the primitive type Null.
 
@@ -170,6 +259,137 @@ QScriptValue QScriptEngine::undefinedValue()
     return QScriptValue(this, QScriptValue::UndefinedValue);
 }
 
+/*!
+    Creates a QScriptValue that wraps a native (C++) function. \a fun
+    must be a C++ function with signature QScriptEngine::FunctionSignature.
+    \a length is the number of arguments that \a fun expects; this becomes
+    the \c{length} property of the created QScriptValue.
+
+    Note that \a length only gives an indication of the number of
+    arguments that the function expects; an actual invocation of a
+    function can include any number of arguments. You can check the
+    \l{QScriptContext::argumentCount()}{argumentCount()} of the
+    QScriptContext associated with the invocation to determine the
+    actual number of arguments passed.
+
+    A \c{prototype} property is automatically created for the resulting
+    function object, to provide for the possibility that the function
+    will be used as a constructor.
+
+    By combining newFunction() and the property flags
+    QScriptValue::PropertyGetter and QScriptValue::PropertySetter, you
+    can create script object properties that behave like normal
+    properties in script code, but are in fact accessed through
+    functions (analogous to how properties work in \l{Qt's Property
+    System}). Example:
+
+    \snippet doc/src/snippets/code/src_script_qscriptengine.cpp 11
+
+    When the property \c{foo} of the script object is subsequently
+    accessed in script code, \c{getSetFoo()} will be invoked to handle
+    the access.  In this particular case, we chose to store the "real"
+    value of \c{foo} as a property of the accessor function itself; you
+    are of course free to do whatever you like in this function.
+
+    In the above example, a single native function was used to handle
+    both reads and writes to the property; the argument count is used to
+    determine if we are handling a read or write. You can also use two
+    separate functions; just specify the relevant flag
+    (QScriptValue::PropertyGetter or QScriptValue::PropertySetter) when
+    setting the property, e.g.:
+
+    \snippet doc/src/snippets/code/src_script_qscriptengine.cpp 12
+
+    \sa QScriptValue::call()
+*/
+QScriptValue QScriptEngine::newFunction(QScriptEngine::FunctionSignature fun, int length)
+{
+    return QScriptValuePrivate::get(d_ptr->newFunction(fun, 0, length));
+}
+
+/*!
+    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:
+
+    \snippet doc/src/snippets/code/src_script_qscriptengine.cpp 9
+
+    To wrap a custom type and provide a constructor for it, you'd typically
+    do something like this:
+
+    \snippet doc/src/snippets/code/src_script_qscriptengine.cpp 10
+*/
+QScriptValue QScriptEngine::newFunction(QScriptEngine::FunctionSignature fun, const QScriptValue& prototype, int length)
+{
+    return QScriptValuePrivate::get(d_ptr->newFunction(fun, QScriptValuePrivate::get(prototype), length));
+}
+
+/*!
+    \internal
+    \since 4.4
+*/
+QScriptValue QScriptEngine::newFunction(QScriptEngine::FunctionWithArgSignature fun, void* arg)
+{
+    return QScriptValuePrivate::get(d_ptr->newFunction(fun, arg));
+}
+
+/*!
+  Creates a QtScript object of class Object.
+
+  The prototype of the created object will be the Object
+  prototype object.
+
+  \sa newArray(), QScriptValue::setProperty()
+*/
+QScriptValue QScriptEngine::newObject()
+{
+    return QScriptValuePrivate::get(d_ptr->newObject());
+}
+
+/*!
+  Creates a QtScript object of class Array with the given \a length.
+
+  \sa newObject()
+*/
+QScriptValue QScriptEngine::newArray(uint length)
+{
+    return QScriptValuePrivate::get(d_ptr->newArray(length));
+}
+
+/*!
+    Creates a QtScript object of class Date with the given \a value
+    (the number of milliseconds since 01 January 1970, UTC).
+*/
+QScriptValue QScriptEngine::newDate(qsreal value)
+{
+    return QScriptValuePrivate::get(d_ptr->newDate(value));
+}
+
+/*!
+    Creates a QtScript object of class Date from the given \a value.
+
+    \sa QScriptValue::toDateTime()
+*/
+QScriptValue QScriptEngine::newDate(const QDateTime& value)
+{
+    if (value.isValid())
+        return QScriptValuePrivate::get(d_ptr->newDate(qsreal(value.toMSecsSinceEpoch())));
+    return QScriptValuePrivate::get(d_ptr->newDate(qSNaN()));
+}
+
 /*!
   Returns this engine's Global Object.
 
@@ -184,3 +404,23 @@ QScriptValue QScriptEngine::globalObject() const
 {
     return QScriptValuePrivate::get(d_ptr->globalObject());
 }
+
+/*!
+    \typedef QScriptEngine::FunctionSignature
+    \relates QScriptEngine
+
+    The function signature \c{QScriptValue f(QScriptContext *, QScriptEngine *)}.
+
+    A function with such a signature can be passed to
+    QScriptEngine::newFunction() to wrap the function.
+*/
+
+/*!
+    \typedef QScriptEngine::FunctionWithArgSignature
+    \relates QScriptEngine
+
+    The function signature \c{QScriptValue f(QScriptContext *, QScriptEngine *, void *)}.
+
+    A function with such a signature can be passed to
+    QScriptEngine::newFunction() to wrap the function.
+*/