diff options
Diffstat (limited to 'JavaScriptCore/qt')
26 files changed, 2111 insertions, 71 deletions
diff --git a/JavaScriptCore/qt/api/QtScript.pro b/JavaScriptCore/qt/api/QtScript.pro index c87eaf4..88629c7 100644 --- a/JavaScriptCore/qt/api/QtScript.pro +++ b/JavaScriptCore/qt/api/QtScript.pro @@ -13,14 +13,20 @@ CONFIG(debug, debug|release) { OBJECTS_DIR = obj/release } +isEmpty(OUTPUT_DIR): OUTPUT_DIR = ../../.. include($$PWD/../../../WebKit.pri) + include($$PWD/../../JavaScriptCore.pri) +addJavaScriptCoreLib(../..) INCLUDEPATH += $$PWD/../../API SOURCES += $$PWD/qscriptengine.cpp \ $$PWD/qscriptengine_p.cpp \ $$PWD/qscriptvalue.cpp \ + $$PWD/qscriptstring.cpp \ + $$PWD/qscriptprogram.cpp \ + $$PWD/qscriptsyntaxcheckresult.cpp \ HEADERS += $$PWD/qtscriptglobal.h \ $$PWD/qscriptengine.h \ @@ -28,9 +34,13 @@ HEADERS += $$PWD/qtscriptglobal.h \ $$PWD/qscriptvalue.h \ $$PWD/qscriptvalue_p.h \ $$PWD/qscriptconverter_p.h \ + $$PWD/qscriptstring.h \ + $$PWD/qscriptstring_p.h \ + $$PWD/qscriptprogram.h \ + $$PWD/qscriptprogram_p.h \ + $$PWD/qscriptsyntaxcheckresult.h \ !static: DEFINES += QT_MAKEDLL DESTDIR = $$OUTPUT_DIR/lib - diff --git a/JavaScriptCore/qt/api/qscriptconverter_p.h b/JavaScriptCore/qt/api/qscriptconverter_p.h index c3ca41f..cd86e20 100644 --- a/JavaScriptCore/qt/api/qscriptconverter_p.h +++ b/JavaScriptCore/qt/api/qscriptconverter_p.h @@ -21,7 +21,11 @@ #define qscriptconverter_p_h #include <JavaScriptCore/JavaScript.h> +#include <QtCore/qnumeric.h> #include <QtCore/qstring.h> +#include <QtCore/qvarlengtharray.h> + +extern char *qdtoa(double d, int mode, int ndigits, int *decpt, int *sign, char **rve, char **digits_str); /* \internal @@ -33,6 +37,19 @@ */ class QScriptConverter { public: + static quint32 toArrayIndex(const JSStringRef jsstring) + { + // FIXME this function should be exported by JSC C API. + QString qstring = toString(jsstring); + + bool ok; + quint32 idx = qstring.toUInt(&ok); + if (!ok || toString(idx) != qstring) + idx = 0xffffffff; + + return idx; + } + static QString toString(const JSStringRef str) { return QString(reinterpret_cast<const QChar*>(JSStringGetCharactersPtr(str)), JSStringGetLength(str)); @@ -45,6 +62,71 @@ public: { return JSStringCreateWithUTF8CString(str); } + static QString toString(double value) + { + // FIXME this should be easier. The ideal fix is to create + // a new function in JSC C API which could cover the functionality. + + if (qIsNaN(value)) + return QString::fromLatin1("NaN"); + if (qIsInf(value)) + return QString::fromLatin1(value < 0 ? "-Infinity" : "Infinity"); + if (!value) + return QString::fromLatin1("0"); + + QVarLengthArray<char, 25> buf; + int decpt; + int sign; + char* result = 0; + char* endresult; + (void)qdtoa(value, 0, 0, &decpt, &sign, &endresult, &result); + + if (!result) + return QString(); + + int resultLen = endresult - result; + if (decpt <= 0 && decpt > -6) { + buf.resize(-decpt + 2 + sign); + qMemSet(buf.data(), '0', -decpt + 2 + sign); + if (sign) // fix the sign. + buf[0] = '-'; + buf[sign + 1] = '.'; + buf.append(result, resultLen); + } else { + if (sign) + buf.append('-'); + int length = buf.size() - sign + resultLen; + if (decpt <= 21 && decpt > 0) { + if (length <= decpt) { + const char* zeros = "0000000000000000000000000"; + buf.append(result, resultLen); + buf.append(zeros, decpt - length); + } else { + buf.append(result, decpt); + buf.append('.'); + buf.append(result + decpt, resultLen - decpt); + } + } else if (result[0] >= '0' && result[0] <= '9') { + if (length > 1) { + buf.append(result, 1); + buf.append('.'); + buf.append(result + 1, resultLen - 1); + } else + buf.append(result, resultLen); + buf.append('e'); + buf.append(decpt >= 0 ? '+' : '-'); + int e = qAbs(decpt - 1); + if (e >= 100) + buf.append('0' + e / 100); + if (e >= 10) + buf.append('0' + (e % 100) / 10); + buf.append('0' + e % 10); + } + } + free(result); + buf.append(0); + return QString::fromLatin1(buf.constData()); + } }; #endif // qscriptconverter_p_h diff --git a/JavaScriptCore/qt/api/qscriptengine.cpp b/JavaScriptCore/qt/api/qscriptengine.cpp index fbeb902..d49c578 100644 --- a/JavaScriptCore/qt/api/qscriptengine.cpp +++ b/JavaScriptCore/qt/api/qscriptengine.cpp @@ -22,6 +22,8 @@ #include "qscriptengine.h" #include "qscriptengine_p.h" +#include "qscriptprogram_p.h" +#include "qscriptsyntaxcheckresult_p.h" #include "qscriptvalue_p.h" /*! @@ -42,6 +44,23 @@ QScriptEngine::~QScriptEngine() } /*! + Checks the syntax of the given \a program. Returns a + QScriptSyntaxCheckResult object that contains the result of the check. +*/ +QScriptSyntaxCheckResult QScriptEngine::checkSyntax(const QString &program) +{ + // FIXME This is not optimal. + // The JSC C API needs a context to perform a syntax check, it means that a QScriptEnginePrivate + // had to be created. This function is static so we have to create QScriptEnginePrivate for each + // call. We can't remove the "static" for compatibility reason, at least up to Qt5. + // QScriptSyntaxCheckResultPrivate takes ownership of newly created engine. The engine will be + // kept as long as it is needed for lazy evaluation of properties of + // the QScriptSyntaxCheckResultPrivate. + QScriptEnginePrivate* engine = new QScriptEnginePrivate(/* q_ptr */ 0); + return QScriptSyntaxCheckResultPrivate::get(engine->checkSyntax(program)); +} + +/*! Evaluates \a program, using \a lineNumber as the base line number, and returns the result of the evaluation. @@ -71,6 +90,11 @@ QScriptValue QScriptEngine::evaluate(const QString& program, const QString& file return QScriptValuePrivate::get(d_ptr->evaluate(program, fileName, lineNumber)); } +QScriptValue QScriptEngine::evaluate(const QScriptProgram& program) +{ + return QScriptValuePrivate::get(d_ptr->evaluate(QScriptProgramPrivate::get(program))); +} + /*! Runs the garbage collector. @@ -81,6 +105,8 @@ QScriptValue QScriptEngine::evaluate(const QString& program, const QString& file when the QScriptEngine decides that it's wise to do so (i.e. when a certain number of new objects have been created). However, you can call this function to explicitly request that garbage collection should be performed as soon as possible. + + \sa reportAdditionalMemoryCost() */ void QScriptEngine::collectGarbage() { @@ -88,6 +114,43 @@ void QScriptEngine::collectGarbage() } /*! + Reports an additional memory cost of the given \a size, measured in + bytes, to the garbage collector. + + This function can be called to indicate that a JavaScript object has + memory associated with it that isn't managed by Qt Script itself. + Reporting the additional cost makes it more likely that the garbage + collector will be triggered. + + Note that if the additional memory is shared with objects outside + the scripting environment, the cost should not be reported, since + collecting the JavaScript object would not cause the memory to be + freed anyway. + + Negative \a size values are ignored, i.e. this function can't be + used to report that the additional memory has been deallocated. + + \sa collectGarbage() +*/ +void QScriptEngine::reportAdditionalMemoryCost(int cost) +{ + d_ptr->reportAdditionalMemoryCost(cost); +} + +/*! + Returns a handle that represents the given string, \a str. + + QScriptString can be used to quickly look up properties, and + compare property names, of script objects. + + \sa QScriptValue::property() +*/ +QScriptString QScriptEngine::toStringHandle(const QString& str) +{ + return QScriptStringPrivate::get(d_ptr->toStringHandle(str)); +} + +/*! Returns a QScriptValue of the primitive type Null. \sa undefinedValue() @@ -106,3 +169,18 @@ QScriptValue QScriptEngine::undefinedValue() { return QScriptValue(this, QScriptValue::UndefinedValue); } + +/*! + Returns this engine's Global Object. + + By default, 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 +{ + return QScriptValuePrivate::get(d_ptr->globalObject()); +} diff --git a/JavaScriptCore/qt/api/qscriptengine.h b/JavaScriptCore/qt/api/qscriptengine.h index b8bd5e6..e10888d 100644 --- a/JavaScriptCore/qt/api/qscriptengine.h +++ b/JavaScriptCore/qt/api/qscriptengine.h @@ -20,6 +20,9 @@ #ifndef qscriptengine_h #define qscriptengine_h +#include "qscriptprogram.h" +#include "qscriptstring.h" +#include "qscriptsyntaxcheckresult.h" #include <QtCore/qobject.h> #include <QtCore/qshareddata.h> #include <QtCore/qstring.h> @@ -35,11 +38,18 @@ public: QScriptEngine(); ~QScriptEngine(); + static QScriptSyntaxCheckResult checkSyntax(const QString& program); QScriptValue evaluate(const QString& program, const QString& fileName = QString(), int lineNumber = 1); + QScriptValue evaluate(const QScriptProgram& program); + void collectGarbage(); + void reportAdditionalMemoryCost(int cost); + + QScriptString toStringHandle(const QString& str); QScriptValue nullValue(); QScriptValue undefinedValue(); + QScriptValue globalObject() const; private: friend class QScriptEnginePrivate; diff --git a/JavaScriptCore/qt/api/qscriptengine_p.cpp b/JavaScriptCore/qt/api/qscriptengine_p.cpp index de8a355..38185ab 100644 --- a/JavaScriptCore/qt/api/qscriptengine_p.cpp +++ b/JavaScriptCore/qt/api/qscriptengine_p.cpp @@ -21,6 +21,7 @@ #include "qscriptengine_p.h" +#include "qscriptprogram_p.h" #include "qscriptvalue_p.h" /*! @@ -38,6 +39,19 @@ QScriptEnginePrivate::~QScriptEnginePrivate() JSGlobalContextRelease(m_context); } +QScriptSyntaxCheckResultPrivate* QScriptEnginePrivate::checkSyntax(const QString& program) +{ + JSValueRef exception; + JSStringRef source = QScriptConverter::toString(program); + bool syntaxIsCorrect = JSCheckScriptSyntax(m_context, source, /* url */ 0, /* starting line */ 1, &exception); + JSStringRelease(source); + if (syntaxIsCorrect) { + return new QScriptSyntaxCheckResultPrivate(this); + } + JSValueProtect(m_context, exception); + return new QScriptSyntaxCheckResultPrivate(this, const_cast<JSObjectRef>(exception)); +} + /*! Evaluates program and returns the result of the evaluation. \internal @@ -46,9 +60,25 @@ QScriptValuePrivate* QScriptEnginePrivate::evaluate(const QString& program, cons { JSStringRef script = QScriptConverter::toString(program); JSStringRef file = QScriptConverter::toString(fileName); - JSValueRef exception; - JSValueRef result = JSEvaluateScript(m_context, script, /* Global Object */ 0, file, lineNumber, &exception); - if (!result) - return new QScriptValuePrivate(this, exception); // returns an exception - return new QScriptValuePrivate(this, result); + QScriptValuePrivate* result = new QScriptValuePrivate(this, evaluate(script, file, lineNumber)); + JSStringRelease(script); + JSStringRelease(file); + return result; +} + +/*! + Evaluates program and returns the result of the evaluation. + \internal +*/ +QScriptValuePrivate* QScriptEnginePrivate::evaluate(const QScriptProgramPrivate* program) +{ + if (program->isNull()) + return new QScriptValuePrivate; + return new QScriptValuePrivate(this, evaluate(program->program(), program->file(), program->line())); +} + +QScriptValuePrivate* QScriptEnginePrivate::globalObject() const +{ + JSObjectRef globalObject = JSContextGetGlobalObject(context()); + return new QScriptValuePrivate(this, globalObject, globalObject); } diff --git a/JavaScriptCore/qt/api/qscriptengine_p.h b/JavaScriptCore/qt/api/qscriptengine_p.h index 8e27c42..2bda68e 100644 --- a/JavaScriptCore/qt/api/qscriptengine_p.h +++ b/JavaScriptCore/qt/api/qscriptengine_p.h @@ -22,12 +22,16 @@ #include "qscriptconverter_p.h" #include "qscriptengine.h" +#include "qscriptstring_p.h" +#include "qscriptsyntaxcheckresult_p.h" #include "qscriptvalue.h" #include <JavaScriptCore/JavaScript.h> +#include <JSBasePrivate.h> #include <QtCore/qshareddata.h> #include <QtCore/qstring.h> class QScriptEngine; +class QScriptSyntaxCheckResultPrivate; class QScriptEnginePrivate : public QSharedData { public: @@ -37,8 +41,13 @@ public: QScriptEnginePrivate(const QScriptEngine*); ~QScriptEnginePrivate(); + QScriptSyntaxCheckResultPrivate* checkSyntax(const QString& program); QScriptValuePrivate* evaluate(const QString& program, const QString& fileName, int lineNumber); + QScriptValuePrivate* evaluate(const QScriptProgramPrivate* program); + inline JSValueRef evaluate(JSStringRef program, JSStringRef fileName, int lineNumber); + inline void collectGarbage(); + inline void reportAdditionalMemoryCost(int cost); inline JSValueRef makeJSValue(double number) const; inline JSValueRef makeJSValue(int number) const; @@ -47,17 +56,42 @@ public: inline JSValueRef makeJSValue(bool number) const; inline JSValueRef makeJSValue(QScriptValue::SpecialValue value) const; + QScriptValuePrivate* globalObject() const; + + inline QScriptStringPrivate* toStringHandle(const QString& str) const; + inline JSGlobalContextRef context() const; private: QScriptEngine* q_ptr; JSGlobalContextRef m_context; }; + +/*! + Evaluates given JavaScript program and returns result of the evaluation. + \attention this function doesn't take ownership of the parameters. + \internal +*/ +JSValueRef QScriptEnginePrivate::evaluate(JSStringRef program, JSStringRef fileName, int lineNumber) +{ + JSValueRef exception; + JSValueRef result = JSEvaluateScript(m_context, program, /* Global Object */ 0, fileName, lineNumber, &exception); + if (!result) + return exception; // returns an exception + return result; +} + void QScriptEnginePrivate::collectGarbage() { JSGarbageCollect(m_context); } +void QScriptEnginePrivate::reportAdditionalMemoryCost(int cost) +{ + if (cost > 0) + JSReportExtraMemoryCost(m_context, cost); +} + JSValueRef QScriptEnginePrivate::makeJSValue(double number) const { return JSValueMakeNumber(m_context, number); @@ -75,7 +109,10 @@ JSValueRef QScriptEnginePrivate::makeJSValue(uint number) const JSValueRef QScriptEnginePrivate::makeJSValue(const QString& string) const { - return JSValueMakeString(m_context, QScriptConverter::toString(string)); + JSStringRef tmp = QScriptConverter::toString(string); + JSValueRef result = JSValueMakeString(m_context, tmp); + JSStringRelease(tmp); + return result; } JSValueRef QScriptEnginePrivate::makeJSValue(bool value) const @@ -90,6 +127,11 @@ JSValueRef QScriptEnginePrivate::makeJSValue(QScriptValue::SpecialValue value) c return JSValueMakeUndefined(m_context); } +QScriptStringPrivate* QScriptEnginePrivate::toStringHandle(const QString& str) const +{ + return new QScriptStringPrivate(str); +} + JSGlobalContextRef QScriptEnginePrivate::context() const { return m_context; diff --git a/JavaScriptCore/qt/api/qscriptprogram.cpp b/JavaScriptCore/qt/api/qscriptprogram.cpp new file mode 100644 index 0000000..d7d4948 --- /dev/null +++ b/JavaScriptCore/qt/api/qscriptprogram.cpp @@ -0,0 +1,136 @@ +/* + Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "config.h" + +#include "qscriptprogram.h" + +#include "qscriptprogram_p.h" + +/*! + \internal + + \class QScriptProgram + + \brief The QScriptProgram class encapsulates a Qt Script program. + + \ingroup script + + QScriptProgram retains the compiled representation of the script if + possible. Thus, QScriptProgram can be used to evaluate the same + script multiple times more efficiently. + + \code + QScriptEngine engine; + QScriptProgram program("1 + 2"); + QScriptValue result = engine.evaluate(program); + \endcode +*/ + +/*! + Constructs a null QScriptProgram. +*/ +QScriptProgram::QScriptProgram() + : d_ptr(new QScriptProgramPrivate) +{} + +/*! + Constructs a new QScriptProgram with the given \a sourceCode, \a + fileName and \a firstLineNumber. +*/ +QScriptProgram::QScriptProgram(const QString& sourceCode, + const QString fileName, + int firstLineNumber) + : d_ptr(new QScriptProgramPrivate(sourceCode, fileName, firstLineNumber)) +{} + +/*! + Destroys this QScriptProgram. +*/ +QScriptProgram::~QScriptProgram() +{} + +/*! + Constructs a new QScriptProgram that is a copy of \a other. +*/ +QScriptProgram::QScriptProgram(const QScriptProgram& other) +{ + d_ptr = other.d_ptr; +} + +/*! + Assigns the \a other value to this QScriptProgram. +*/ +QScriptProgram& QScriptProgram::operator=(const QScriptProgram& other) +{ + d_ptr = other.d_ptr; + return *this; +} + +/*! + Returns true if this QScriptProgram is null; otherwise + returns false. +*/ +bool QScriptProgram::isNull() const +{ + return d_ptr->isNull(); +} + +/*! + Returns the source code of this program. +*/ +QString QScriptProgram::sourceCode() const +{ + return d_ptr->sourceCode(); +} + +/*! + Returns the filename associated with this program. +*/ +QString QScriptProgram::fileName() const +{ + return d_ptr->fileName(); +} + +/*! + Returns the line number associated with this program. +*/ +int QScriptProgram::firstLineNumber() const +{ + return d_ptr->firstLineNumber(); +} + +/*! + Returns true if this QScriptProgram is equal to \a other; + otherwise returns false. +*/ +bool QScriptProgram::operator==(const QScriptProgram& other) const +{ + return d_ptr == other.d_ptr || *d_ptr == *other.d_ptr; +} + +/*! + Returns true if this QScriptProgram is not equal to \a other; + otherwise returns false. +*/ +bool QScriptProgram::operator!=(const QScriptProgram& other) const +{ + return d_ptr != other.d_ptr && *d_ptr != *other.d_ptr; +} + diff --git a/JavaScriptCore/qt/api/qscriptprogram.h b/JavaScriptCore/qt/api/qscriptprogram.h new file mode 100644 index 0000000..93c8a3c --- /dev/null +++ b/JavaScriptCore/qt/api/qscriptprogram.h @@ -0,0 +1,53 @@ +/* + Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef qscriptprogram_h +#define qscriptprogram_h + +#include "qtscriptglobal.h" +#include <QtCore/qshareddata.h> +#include <QtCore/qstring.h> + +class QScriptProgramPrivate; +class Q_JAVASCRIPT_EXPORT QScriptProgram { +public: + QScriptProgram(); + QScriptProgram(const QString& sourceCode, + const QString fileName = QString(), + int firstLineNumber = 1); + QScriptProgram(const QScriptProgram& other); + ~QScriptProgram(); + + QScriptProgram& operator=(const QScriptProgram& other); + + bool isNull() const; + + QString sourceCode() const; + QString fileName() const; + int firstLineNumber() const; + + bool operator==(const QScriptProgram& other) const; + bool operator!=(const QScriptProgram& other) const; + +private: + QExplicitlySharedDataPointer<QScriptProgramPrivate> d_ptr; + Q_DECLARE_PRIVATE(QScriptProgram) +}; + +#endif // qscriptprogram_h diff --git a/JavaScriptCore/qt/api/qscriptprogram_p.h b/JavaScriptCore/qt/api/qscriptprogram_p.h new file mode 100644 index 0000000..6e80e85 --- /dev/null +++ b/JavaScriptCore/qt/api/qscriptprogram_p.h @@ -0,0 +1,129 @@ +/* + Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef qscriptprogram_p_h +#define qscriptprogram_p_h + +#include "qscriptconverter_p.h" +#include "qscriptprogram.h" +#include <JavaScriptCore/JavaScript.h> +#include <QtCore/qshareddata.h> +#include <QtCore/qstring.h> + +/* + FIXME The QScriptProgramPrivate potentially could be much faster. In current implementation we + gain CPU time only by avoiding QString -> JSStringRef conversion. In the ideal world we should + have a function inside the JSC C API that could provide us "parse once, execute multiple times" + functionality. +*/ + +class QScriptProgramPrivate : public QSharedData { +public: + inline static QScriptProgramPrivate* get(const QScriptProgram& program); + inline QScriptProgramPrivate(); + inline QScriptProgramPrivate(const QString& sourceCode, + const QString fileName, + int firstLineNumber); + + inline ~QScriptProgramPrivate(); + + inline bool isNull() const; + + inline QString sourceCode() const; + inline QString fileName() const; + inline int firstLineNumber() const; + + inline bool operator==(const QScriptProgramPrivate& other) const; + inline bool operator!=(const QScriptProgramPrivate& other) const; + + inline JSStringRef program() const; + inline JSStringRef file() const; + inline int line() const; +private: + JSStringRef m_program; + JSStringRef m_fileName; + int m_line; +}; + +QScriptProgramPrivate* QScriptProgramPrivate::get(const QScriptProgram& program) +{ + return const_cast<QScriptProgramPrivate*>(program.d_ptr.constData()); +} + +QScriptProgramPrivate::QScriptProgramPrivate() + : m_program(0) + , m_fileName(0) + , m_line(-1) +{} + +QScriptProgramPrivate::QScriptProgramPrivate(const QString& sourceCode, + const QString fileName, + int firstLineNumber) + : m_program(QScriptConverter::toString(sourceCode)) + , m_fileName(QScriptConverter::toString(fileName)) + , m_line(firstLineNumber) +{} + +QScriptProgramPrivate::~QScriptProgramPrivate() +{ + if (!isNull()) { + JSStringRelease(m_program); + JSStringRelease(m_fileName); + } +} + +bool QScriptProgramPrivate::isNull() const +{ + return !m_program; +} + +QString QScriptProgramPrivate::sourceCode() const +{ + return QScriptConverter::toString(m_program); +} + +QString QScriptProgramPrivate::fileName() const +{ + return QScriptConverter::toString(m_fileName); +} + +int QScriptProgramPrivate::firstLineNumber() const +{ + return m_line; +} + +bool QScriptProgramPrivate::operator==(const QScriptProgramPrivate& other) const +{ + return m_line == other.m_line + && JSStringIsEqual(m_fileName, other.m_fileName) + && JSStringIsEqual(m_program, other.m_program); +} + +bool QScriptProgramPrivate::operator!=(const QScriptProgramPrivate& other) const +{ + return m_line != other.m_line + || !JSStringIsEqual(m_fileName, other.m_fileName) + || !JSStringIsEqual(m_program, other.m_program); +} + +JSStringRef QScriptProgramPrivate::program() const { return m_program; } +JSStringRef QScriptProgramPrivate::file() const {return m_fileName; } +int QScriptProgramPrivate::line() const { return m_line; } + +#endif // qscriptprogram_p_h diff --git a/JavaScriptCore/qt/api/qscriptstring.cpp b/JavaScriptCore/qt/api/qscriptstring.cpp new file mode 100644 index 0000000..83c03c5 --- /dev/null +++ b/JavaScriptCore/qt/api/qscriptstring.cpp @@ -0,0 +1,131 @@ +/* + Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "config.h" + +#include "qscriptstring.h" + +#include "qscriptstring_p.h" +#include <QtCore/qhash.h> + +/*! + Constructs an invalid QScriptString. +*/ +QScriptString::QScriptString() + : d_ptr(new QScriptStringPrivate()) +{ +} +/*! + Constructs an QScriptString from internal representation + \internal +*/ +QScriptString::QScriptString(QScriptStringPrivate* d) + : d_ptr(d) +{ +} + +/*! + Constructs a new QScriptString that is a copy of \a other. +*/ +QScriptString::QScriptString(const QScriptString& other) +{ + d_ptr = other.d_ptr; +} + +/*! + Destroys this QScriptString. +*/ +QScriptString::~QScriptString() +{ +} + +/*! + Assigns the \a other value to this QScriptString. +*/ +QScriptString& QScriptString::operator=(const QScriptString& other) +{ + d_ptr = other.d_ptr; + return *this; +} + +/*! + Returns true if this QScriptString is valid; otherwise + returns false. +*/ +bool QScriptString::isValid() const +{ + return d_ptr->isValid(); +} + +/*! + Returns true if this QScriptString is equal to \a other; + otherwise returns false. +*/ +bool QScriptString::operator==(const QScriptString& other) const +{ + return d_ptr == other.d_ptr || *d_ptr == *(other.d_ptr); +} + +/*! + Returns true if this QScriptString is not equal to \a other; + otherwise returns false. +*/ +bool QScriptString::operator!=(const QScriptString& other) const +{ + return d_ptr != other.d_ptr || *d_ptr != *(other.d_ptr); +} + +/*! + Attempts to convert this QScriptString to a QtScript array index, + and returns the result. + + If a conversion error occurs, *\a{ok} is set to false; otherwise + *\a{ok} is set to true. +*/ +quint32 QScriptString::toArrayIndex(bool* ok) const +{ + return d_ptr->toArrayIndex(ok); +} + +/*! + Returns the string that this QScriptString represents, or a + null string if this QScriptString is not valid. + + \sa isValid() +*/ +QString QScriptString::toString() const +{ + return d_ptr->toString(); +} + +/*! + Returns the string that this QScriptString represents, or a + null string if this QScriptString is not valid. + + \sa toString() +*/ +QScriptString::operator QString() const +{ + return d_ptr->toString(); +} + +uint qHash(const QScriptString& key) +{ + return qHash(QScriptStringPrivate::get(key)->id()); +} diff --git a/JavaScriptCore/qt/api/qscriptstring.h b/JavaScriptCore/qt/api/qscriptstring.h new file mode 100644 index 0000000..16593bc --- /dev/null +++ b/JavaScriptCore/qt/api/qscriptstring.h @@ -0,0 +1,58 @@ +/* + Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef qscriptstring_h +#define qscriptstring_h + +#include "qtscriptglobal.h" +#include <QtCore/qshareddata.h> +#include <QtCore/qstring.h> + +class QScriptStringPrivate; +typedef QExplicitlySharedDataPointer<QScriptStringPrivate> QScriptStringPtr; + +class Q_JAVASCRIPT_EXPORT QScriptString { +public: + QScriptString(); + QScriptString(const QScriptString& other); + ~QScriptString(); + + QScriptString& operator=(const QScriptString& other); + + bool isValid() const; + + bool operator==(const QScriptString& other) const; + bool operator!=(const QScriptString& other) const; + + quint32 toArrayIndex(bool* ok = 0) const; + + QString toString() const; + operator QString() const; + +private: + QScriptString(QScriptStringPrivate* d); + + QScriptStringPtr d_ptr; + + friend class QScriptStringPrivate; +}; + +uint qHash(const QScriptString& key); + +#endif // qscriptstring_h diff --git a/JavaScriptCore/qt/api/qscriptstring_p.h b/JavaScriptCore/qt/api/qscriptstring_p.h new file mode 100644 index 0000000..f4fd117 --- /dev/null +++ b/JavaScriptCore/qt/api/qscriptstring_p.h @@ -0,0 +1,112 @@ +/* + Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef qscriptstring_p_h +#define qscriptstring_p_h + +#include "qscriptconverter_p.h" +#include "qscriptstring.h" +#include <JavaScriptCore/JavaScript.h> +#include <QtCore/qnumeric.h> +#include <QtCore/qshareddata.h> + +class QScriptStringPrivate : public QSharedData { +public: + inline QScriptStringPrivate(); + inline QScriptStringPrivate(const QString& qtstring); + inline ~QScriptStringPrivate(); + + static inline QScriptString get(QScriptStringPrivate* d); + static inline QScriptStringPtr get(const QScriptString& p); + + inline bool isValid() const; + + inline bool operator==(const QScriptStringPrivate& other) const; + inline bool operator!=(const QScriptStringPrivate& other) const; + + inline quint32 toArrayIndex(bool* ok = 0) const; + + inline QString toString() const; + + inline quint64 id() const; + +private: + JSStringRef m_string; +}; + + +QScriptStringPrivate::QScriptStringPrivate() + : m_string(0) +{} + +QScriptStringPrivate::QScriptStringPrivate(const QString& qtstring) + : m_string(JSStringRetain(QScriptConverter::toString(qtstring))) +{} + +QScriptStringPrivate::~QScriptStringPrivate() +{ + if (isValid()) + JSStringRelease(m_string); +} + +QScriptString QScriptStringPrivate::get(QScriptStringPrivate* d) +{ + Q_ASSERT(d); + return QScriptString(d); +} + +QScriptStringPtr QScriptStringPrivate::get(const QScriptString& p) +{ + return p.d_ptr; +} + +bool QScriptStringPrivate::isValid() const +{ + return m_string; +} + +bool QScriptStringPrivate::operator==(const QScriptStringPrivate& other) const +{ + return isValid() && other.isValid() && JSStringIsEqual(m_string, other.m_string); +} + +bool QScriptStringPrivate::operator!=(const QScriptStringPrivate& other) const +{ + return isValid() && other.isValid() && !JSStringIsEqual(m_string, other.m_string); +} + +quint32 QScriptStringPrivate::toArrayIndex(bool* ok) const +{ + quint32 idx = QScriptConverter::toArrayIndex(m_string); + if (ok) + *ok = (idx != 0xffffffff); + return idx; +} + +QString QScriptStringPrivate::toString() const +{ + return QScriptConverter::toString(m_string); +} + +quint64 QScriptStringPrivate::id() const +{ + return reinterpret_cast<quint32>(m_string); +} + +#endif // qscriptstring_p_h diff --git a/JavaScriptCore/qt/api/qscriptsyntaxcheckresult.cpp b/JavaScriptCore/qt/api/qscriptsyntaxcheckresult.cpp new file mode 100644 index 0000000..5cf02ef --- /dev/null +++ b/JavaScriptCore/qt/api/qscriptsyntaxcheckresult.cpp @@ -0,0 +1,148 @@ +/* + Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "config.h" + +#include "qscriptsyntaxcheckresult.h" +#include "qscriptsyntaxcheckresult_p.h" + +/*! + \class QScriptSyntaxCheckResult + + \brief The QScriptSyntaxCheckResult class provides the result of a script syntax check. + + \ingroup script + \mainclass + + QScriptSyntaxCheckResult is returned by QScriptEngine::checkSyntax() to + provide information about the syntactical (in)correctness of a script. +*/ + +/*! + \enum QScriptSyntaxCheckResult::State + + This enum specifies the state of a syntax check. + + \value Error The program contains a syntax error. + \value Intermediate The program is incomplete. + \value Valid The program is a syntactically correct Qt Script program. +*/ + +/*! + Constructs a new QScriptSyntaxCheckResult from the \a other result. +*/ +QScriptSyntaxCheckResult::QScriptSyntaxCheckResult(const QScriptSyntaxCheckResult& other) + : d_ptr(other.d_ptr) +{} + +/*! + Constructs a new QScriptSyntaxCheckResult from an internal representation. + \internal +*/ +QScriptSyntaxCheckResult::QScriptSyntaxCheckResult(QScriptSyntaxCheckResultPrivate* d) + : d_ptr(d) +{} + +/*! + Destroys this QScriptSyntaxCheckResult. +*/ +QScriptSyntaxCheckResult::~QScriptSyntaxCheckResult() +{} + +/*! + Assigns the \a other result to this QScriptSyntaxCheckResult, and returns a + reference to this QScriptSyntaxCheckResult. +*/ +QScriptSyntaxCheckResult& QScriptSyntaxCheckResult::operator=(const QScriptSyntaxCheckResult& other) +{ + d_ptr = other.d_ptr; + return *this; +} + +/*! + Returns the state of this QScriptSyntaxCheckResult. +*/ +QScriptSyntaxCheckResult::State QScriptSyntaxCheckResult::state() const +{ + return d_ptr->state(); +} + +/*! + Returns the error line number of this QScriptSyntaxCheckResult, or -1 if + there is no error. + + \sa state(), errorMessage() +*/ +int QScriptSyntaxCheckResult::errorLineNumber() const +{ + return d_ptr->errorLineNumber(); +} + +/*! + Returns the error column number of this QScriptSyntaxCheckResult, or -1 if + there is no error. + + \sa state(), errorLineNumber() +*/ +int QScriptSyntaxCheckResult::errorColumnNumber() const +{ + return d_ptr->errorColumnNumber(); +} + +/*! + Returns the error message of this QScriptSyntaxCheckResult, or an empty + string if there is no error. + + \sa state(), errorLineNumber() +*/ +QString QScriptSyntaxCheckResult::errorMessage() const +{ + return d_ptr->errorMessage(); +} + +QScriptSyntaxCheckResultPrivate::~QScriptSyntaxCheckResultPrivate() +{ + if (m_exception) + JSValueUnprotect(m_engine->context(), m_exception); +} + +QString QScriptSyntaxCheckResultPrivate::errorMessage() const +{ + if (!m_exception) + return QString(); + + JSStringRef tmp = JSValueToStringCopy(m_engine->context(), m_exception, /* exception */ 0); + QString message = QScriptConverter::toString(tmp); + JSStringRelease(tmp); + return message; +} + +int QScriptSyntaxCheckResultPrivate::errorLineNumber() const +{ + if (!m_exception) + return -1; + // m_exception is an instance of the Exception so it has "line" attribute. + JSStringRef lineAttrName = QScriptConverter::toString("line"); + JSValueRef line = JSObjectGetProperty(m_engine->context(), + m_exception, + lineAttrName, + /* exceptions */0); + JSStringRelease(lineAttrName); + return JSValueToNumber(m_engine->context(), line, /* exceptions */0); +} diff --git a/JavaScriptCore/qt/api/qscriptsyntaxcheckresult.h b/JavaScriptCore/qt/api/qscriptsyntaxcheckresult.h new file mode 100644 index 0000000..aa57744 --- /dev/null +++ b/JavaScriptCore/qt/api/qscriptsyntaxcheckresult.h @@ -0,0 +1,50 @@ +/* + Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef qscriptsyntaxcheckresult_h +#define qscriptsyntaxcheckresult_h + +#include "qtscriptglobal.h" +#include <QtCore/qshareddata.h> + +class QScriptSyntaxCheckResultPrivate; +class Q_JAVASCRIPT_EXPORT QScriptSyntaxCheckResult { +public: + enum State { + Error, + Intermediate, + Valid + }; + + QScriptSyntaxCheckResult(const QScriptSyntaxCheckResult& other); + ~QScriptSyntaxCheckResult(); + QScriptSyntaxCheckResult& operator=(const QScriptSyntaxCheckResult& other); + + State state() const; + int errorLineNumber() const; + int errorColumnNumber() const; + QString errorMessage() const; + +private: + QScriptSyntaxCheckResult(QScriptSyntaxCheckResultPrivate* d); + QExplicitlySharedDataPointer<QScriptSyntaxCheckResultPrivate> d_ptr; + + friend class QScriptSyntaxCheckResultPrivate; +}; +#endif // qscriptsyntaxcheckresult_h diff --git a/JavaScriptCore/qt/api/qscriptsyntaxcheckresult_p.h b/JavaScriptCore/qt/api/qscriptsyntaxcheckresult_p.h new file mode 100644 index 0000000..6e1a131 --- /dev/null +++ b/JavaScriptCore/qt/api/qscriptsyntaxcheckresult_p.h @@ -0,0 +1,73 @@ +/* + Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef qscriptsyntaxcheckresult_p_h +#define qscriptsyntaxcheckresult_p_h + +#include "qscriptconverter_p.h" +#include "qscriptengine_p.h" +#include "qscriptsyntaxcheckresult.h" +#include <JavaScriptCore/JavaScript.h> +#include <QtCore/qshareddata.h> + +class QScriptSyntaxCheckResultPrivate : public QSharedData { +public: + static inline QScriptSyntaxCheckResult get(QScriptSyntaxCheckResultPrivate* p); + inline QScriptSyntaxCheckResultPrivate(const QScriptEnginePrivate* engine); + inline QScriptSyntaxCheckResultPrivate(const QScriptEnginePrivate* engine, JSObjectRef value); + ~QScriptSyntaxCheckResultPrivate(); + + inline QScriptSyntaxCheckResult::State state() const; + int errorLineNumber() const; + inline int errorColumnNumber() const; + QString errorMessage() const; +private: + JSObjectRef m_exception; + QScriptEnginePtr m_engine; +}; + +QScriptSyntaxCheckResult QScriptSyntaxCheckResultPrivate::get(QScriptSyntaxCheckResultPrivate* p) +{ + return QScriptSyntaxCheckResult(p); +} + +QScriptSyntaxCheckResultPrivate::QScriptSyntaxCheckResultPrivate(const QScriptEnginePrivate* engine) + : m_exception(0) + , m_engine(const_cast<QScriptEnginePrivate*>(engine)) +{} + +QScriptSyntaxCheckResultPrivate::QScriptSyntaxCheckResultPrivate(const QScriptEnginePrivate* engine, JSObjectRef value) + : m_exception(value) + , m_engine(const_cast<QScriptEnginePrivate*>(engine)) +{} + +QScriptSyntaxCheckResult::State QScriptSyntaxCheckResultPrivate::state() const +{ + // FIXME This function doesn't return QScriptSyntaxCheckResult::Intermediate + return m_exception ? QScriptSyntaxCheckResult::Error : QScriptSyntaxCheckResult::Valid; +} + +int QScriptSyntaxCheckResultPrivate::errorColumnNumber() const +{ + // FIXME JSC C API doesn't expose the error column number. + return m_exception ? 1 : -1; +} + + +#endif // qscriptsyntaxcheckresult_p_h diff --git a/JavaScriptCore/qt/api/qscriptvalue_p.h b/JavaScriptCore/qt/api/qscriptvalue_p.h index 8db43a7..830b38e 100644 --- a/JavaScriptCore/qt/api/qscriptvalue_p.h +++ b/JavaScriptCore/qt/api/qscriptvalue_p.h @@ -38,10 +38,10 @@ class QScriptValue; Implementation of QScriptValue. The implementation is based on a state machine. The states names are included in - QScriptValuePrivate::States. Each method should check for the current state and then perform a + QScriptValuePrivate::State. Each method should check for the current state and then perform a correct action. - States: + State: Invalid -> QSVP is invalid, no assumptions should be made about class members (apart from m_value). CString -> QSVP is created from QString or const char* and no JSC engine has been associated yet. Current value is kept in m_string, @@ -53,7 +53,7 @@ class QScriptValue; is kept in m_number (cast of QScriptValue::SpecialValue) JSValue -> QSVP is associated with engine, but there is no information about real type, the state have really short live cycle. Normally it is created as a function call result. - JSNative -> QSVP is associated with engine, and it is sure that it isn't a JavaScript object. + JSPrimitive -> QSVP is associated with engine, and it is sure that it isn't a JavaScript object. JSObject -> QSVP is associated with engine, and it is sure that it is a JavaScript object. Each state keep all necessary information to invoke all methods, if not it should be changed to @@ -117,14 +117,14 @@ public: private: // Please, update class documentation when you change the enum. - enum States { + enum State { Invalid = 0, CString = 0x1000, CNumber, CBool, CSpecial, JSValue = 0x2000, // JS values are equal or higher then this value. - JSNative, + JSPrimitive, JSObject } m_state; QScriptEnginePtr m_engine; @@ -136,6 +136,7 @@ private: inline void setValue(JSValueRef); inline bool inherits(const char*); + inline State refinedJSValue(); inline bool isJSBased() const; inline bool isNumberBased() const; @@ -209,7 +210,7 @@ QScriptValuePrivate::QScriptValuePrivate(QScriptValue::SpecialValue value) } QScriptValuePrivate::QScriptValuePrivate(const QScriptEngine* engine, bool value) - : m_state(JSNative) + : m_state(JSPrimitive) { if (!engine) { // slower path reinitialization @@ -224,7 +225,7 @@ QScriptValuePrivate::QScriptValuePrivate(const QScriptEngine* engine, bool value } QScriptValuePrivate::QScriptValuePrivate(const QScriptEngine* engine, int value) - : m_state(JSNative) + : m_state(JSPrimitive) { if (!engine) { // slower path reinitialization @@ -239,7 +240,7 @@ QScriptValuePrivate::QScriptValuePrivate(const QScriptEngine* engine, int value) } QScriptValuePrivate::QScriptValuePrivate(const QScriptEngine* engine, uint value) - : m_state(JSNative) + : m_state(JSPrimitive) { if (!engine) { // slower path reinitialization @@ -254,7 +255,7 @@ QScriptValuePrivate::QScriptValuePrivate(const QScriptEngine* engine, uint value } QScriptValuePrivate::QScriptValuePrivate(const QScriptEngine* engine, qsreal value) - : m_state(JSNative) + : m_state(JSPrimitive) { if (!engine) { // slower path reinitialization @@ -269,7 +270,7 @@ QScriptValuePrivate::QScriptValuePrivate(const QScriptEngine* engine, qsreal val } QScriptValuePrivate::QScriptValuePrivate(const QScriptEngine* engine, const QString& value) - : m_state(JSNative) + : m_state(JSPrimitive) { if (!engine) { // slower path reinitialization @@ -284,7 +285,7 @@ QScriptValuePrivate::QScriptValuePrivate(const QScriptEngine* engine, const QStr } QScriptValuePrivate::QScriptValuePrivate(const QScriptEngine* engine, QScriptValue::SpecialValue value) - : m_state(JSNative) + : m_state(JSPrimitive) { if (!engine) { // slower path reinitialization @@ -325,10 +326,10 @@ bool QScriptValuePrivate::isBool() case CBool: return true; case JSValue: - if (isObject()) + if (refinedJSValue() != JSPrimitive) return false; // Fall-through. - case JSNative: + case JSPrimitive: return JSValueIsBoolean(context(), value()); default: return false; @@ -341,10 +342,10 @@ bool QScriptValuePrivate::isNumber() case CNumber: return true; case JSValue: - if (isObject()) + if (refinedJSValue() != JSPrimitive) return false; // Fall-through. - case JSNative: + case JSPrimitive: return JSValueIsNumber(context(), value()); default: return false; @@ -357,10 +358,10 @@ bool QScriptValuePrivate::isNull() case CSpecial: return m_number == static_cast<int>(QScriptValue::NullValue); case JSValue: - if (isObject()) + if (refinedJSValue() != JSPrimitive) return false; // Fall-through. - case JSNative: + case JSPrimitive: return JSValueIsNull(context(), value()); default: return false; @@ -373,10 +374,10 @@ bool QScriptValuePrivate::isString() case CString: return true; case JSValue: - if (isObject()) + if (refinedJSValue() != JSPrimitive) return false; // Fall-through. - case JSNative: + case JSPrimitive: return JSValueIsString(context(), value()); default: return false; @@ -389,10 +390,10 @@ bool QScriptValuePrivate::isUndefined() case CSpecial: return m_number == static_cast<int>(QScriptValue::UndefinedValue); case JSValue: - if (isObject()) + if (refinedJSValue() != JSPrimitive) return false; // Fall-through. - case JSNative: + case JSPrimitive: return JSValueIsUndefined(context(), value()); default: return false; @@ -403,7 +404,7 @@ bool QScriptValuePrivate::isError() { switch (m_state) { case JSValue: - if (!isObject()) + if (refinedJSValue() != JSObject) return false; // Fall-through. case JSObject: @@ -416,14 +417,11 @@ bool QScriptValuePrivate::isError() bool QScriptValuePrivate::isObject() { switch (m_state) { - case JSObject: - return true; case JSValue: - m_object = JSValueToObject(context(), value(), /* exception */ 0); - if (!m_object) - return false; - m_state = JSObject; + return refinedJSValue() == JSObject; + case JSObject: return true; + default: return false; } @@ -433,10 +431,8 @@ bool QScriptValuePrivate::isFunction() { switch (m_state) { case JSValue: - m_object = JSValueToObject(context(), value(), /* exception */ 0); - if (!m_object) + if (refinedJSValue() != JSObject) return false; - m_state = JSObject; // Fall-through. case JSObject: return JSObjectIsFunction(context(), object()); @@ -455,11 +451,11 @@ QString QScriptValuePrivate::toString() const case CString: return m_string; case CNumber: - return QString::number(m_number); + return QScriptConverter::toString(m_number); case CSpecial: return m_number == QScriptValue::NullValue ? QString::fromLatin1("null") : QString::fromLatin1("undefined"); case JSValue: - case JSNative: + case JSPrimitive: case JSObject: return QScriptConverter::toString(JSValueToStringCopy(context(), value(), /* exception */ 0)); } @@ -472,7 +468,7 @@ qsreal QScriptValuePrivate::toNumber() const { switch (m_state) { case JSValue: - case JSNative: + case JSPrimitive: case JSObject: return JSValueToNumber(context(), value(), /* exception */ 0); case CNumber: @@ -504,7 +500,7 @@ bool QScriptValuePrivate::toBool() const { switch (m_state) { case JSValue: - case JSNative: + case JSPrimitive: return JSValueToBoolean(context(), value()); case JSObject: return true; @@ -631,7 +627,7 @@ bool QScriptValuePrivate::assignEngine(QScriptEnginePrivate* engine) return false; } m_engine = engine; - m_state = JSNative; + m_state = JSPrimitive; setValue(value); return true; } @@ -640,12 +636,8 @@ QScriptValuePrivate* QScriptValuePrivate::call(const QScriptValuePrivate*, const { switch (m_state) { case JSValue: - m_object = JSValueToObject(context(), value(), /* exception */ 0); - if (!object()) { - m_state = JSValue; + if (refinedJSValue() != JSObject) return new QScriptValuePrivate; - } - m_state = JSObject; // Fall-through. case JSObject: { @@ -719,12 +711,32 @@ bool QScriptValuePrivate::inherits(const char* name) { Q_ASSERT(isJSBased()); JSObjectRef globalObject = JSContextGetGlobalObject(context()); - JSValueRef error = JSObjectGetProperty(context(), globalObject, QScriptConverter::toString(name), 0); + JSStringRef errorAttrName = QScriptConverter::toString(name); + JSValueRef error = JSObjectGetProperty(context(), globalObject, errorAttrName, /* exception */ 0); + JSStringRelease(errorAttrName); return JSValueIsInstanceOfConstructor(context(), value(), JSValueToObject(context(), error, /* exception */ 0), /* exception */ 0); } /*! \internal + Refines the state of this QScriptValuePrivate. Returns the new state. +*/ +QScriptValuePrivate::State QScriptValuePrivate::refinedJSValue() +{ + Q_ASSERT(m_state == JSValue); + if (!JSValueIsObject(context(), value())) { + m_state = JSPrimitive; + } else { + m_state = JSObject; + // We are sure that value is an JSObject, so we can const_cast safely without + // calling JSC C API (JSValueToObject(context(), value(), /* exceptions */ 0)). + m_object = const_cast<JSObjectRef>(m_value); + } + return m_state; +} + +/*! + \internal Returns true if QSV have an engine associated. */ bool QScriptValuePrivate::isJSBased() const { return m_state >= JSValue; } diff --git a/JavaScriptCore/qt/tests/qscriptengine/qscriptengine.pro b/JavaScriptCore/qt/tests/qscriptengine/qscriptengine.pro index 0dc0902..d521dd8 100644 --- a/JavaScriptCore/qt/tests/qscriptengine/qscriptengine.pro +++ b/JavaScriptCore/qt/tests/qscriptengine/qscriptengine.pro @@ -1,6 +1,7 @@ TEMPLATE = app TARGET = tst_qscriptengine QT += testlib +isEmpty(OUTPUT_DIR): OUTPUT_DIR = ../../../.. include(../tests.pri) SOURCES += tst_qscriptengine.cpp diff --git a/JavaScriptCore/qt/tests/qscriptengine/tst_qscriptengine.cpp b/JavaScriptCore/qt/tests/qscriptengine/tst_qscriptengine.cpp index b36e364..1ec9ad3 100644 --- a/JavaScriptCore/qt/tests/qscriptengine/tst_qscriptengine.cpp +++ b/JavaScriptCore/qt/tests/qscriptengine/tst_qscriptengine.cpp @@ -18,6 +18,8 @@ */ #include "qscriptengine.h" +#include "qscriptprogram.h" +#include "qscriptsyntaxcheckresult.h" #include "qscriptvalue.h" #include <QtTest/qtest.h> @@ -33,10 +35,15 @@ public slots: void cleanup() {} private slots: + void globalObject(); void evaluate(); void collectGarbage(); + void reportAdditionalMemoryCost(); void nullValue(); void undefinedValue(); + void evaluateProgram(); + void checkSyntax_data(); + void checkSyntax(); }; /* Evaluating a script that throw an unhandled exception should return an invalid value. */ @@ -47,6 +54,17 @@ void tst_QScriptEngine::evaluate() QVERIFY2(engine.evaluate("ping").isValid(), "Script throwing an unhandled exception should return an exception value"); } +void tst_QScriptEngine::globalObject() +{ + QScriptEngine engine; + QScriptValue global = engine.globalObject(); + QScriptValue self = engine.evaluate("this"); + QVERIFY(global.isObject()); + QVERIFY(engine.globalObject().equals(engine.evaluate("this"))); + QEXPECT_FAIL("", "strictlyEquals is broken - bug 36600 in bugs.webkit.org", Continue); + QVERIFY(engine.globalObject().strictlyEquals(self)); +} + /* Test garbage collection, at least try to not crash. */ void tst_QScriptEngine::collectGarbage() { @@ -57,6 +75,26 @@ void tst_QScriptEngine::collectGarbage() QCOMPARE(foo.call().toString(), QString::fromAscii("pong")); } +void tst_QScriptEngine::reportAdditionalMemoryCost() +{ + // There isn't any easy way to test the responsiveness of the GC; + // just try to call the function a few times with various sizes. + QScriptEngine eng; + for (int i = 0; i < 100; ++i) { + eng.reportAdditionalMemoryCost(0); + eng.reportAdditionalMemoryCost(10); + eng.reportAdditionalMemoryCost(1000); + eng.reportAdditionalMemoryCost(10000); + eng.reportAdditionalMemoryCost(100000); + eng.reportAdditionalMemoryCost(1000000); + eng.reportAdditionalMemoryCost(10000000); + eng.reportAdditionalMemoryCost(-1); + eng.reportAdditionalMemoryCost(-1000); + QScriptValue obj = eng.evaluate("new Object"); + eng.collectGarbage(); + } +} + void tst_QScriptEngine::nullValue() { QScriptEngine engine; @@ -73,5 +111,198 @@ void tst_QScriptEngine::undefinedValue() QVERIFY(value.isUndefined()); } +void tst_QScriptEngine::evaluateProgram() +{ + QScriptEngine eng; + { + QString code("1 + 2"); + QString fileName("hello.js"); + int lineNumber = 123; + QScriptProgram program(code, fileName, lineNumber); + QVERIFY(!program.isNull()); + QCOMPARE(program.sourceCode(), code); + QCOMPARE(program.fileName(), fileName); + QCOMPARE(program.firstLineNumber(), lineNumber); + + QScriptValue expected = eng.evaluate(code); + for (int x = 0; x < 10; ++x) { + QScriptValue ret = eng.evaluate(program); + QVERIFY(ret.equals(expected)); + } + + // operator= + QScriptProgram sameProgram = program; + QVERIFY(sameProgram == program); + QVERIFY(eng.evaluate(sameProgram).equals(expected)); + + // copy constructor + QScriptProgram sameProgram2(program); + QVERIFY(sameProgram2 == program); + QVERIFY(eng.evaluate(sameProgram2).equals(expected)); + + QScriptProgram differentProgram("2 + 3"); + QVERIFY(differentProgram != program); + QVERIFY(!eng.evaluate(differentProgram).equals(expected)); + } + + // Program that accesses variable in the scope + { + QScriptProgram program("a"); + QVERIFY(!program.isNull()); + { + QScriptValue ret = eng.evaluate(program); + QVERIFY(ret.isError()); + QCOMPARE(ret.toString(), QString::fromLatin1("ReferenceError: Can't find variable: a")); + } + { + QScriptValue ret = eng.evaluate(program); + QVERIFY(ret.isError()); + } + eng.evaluate("a = 456"); + { + QScriptValue ret = eng.evaluate(program); + QVERIFY(!ret.isError()); + QCOMPARE(ret.toNumber(), 456.0); + } + } + + // Program that creates closure + { + QScriptProgram program("(function() { var count = 0; return function() { return count++; }; })"); + QVERIFY(!program.isNull()); + QScriptValue createCounter = eng.evaluate(program); + QVERIFY(createCounter.isFunction()); + QScriptValue counter = createCounter.call(); + QVERIFY(counter.isFunction()); + { + QScriptValue ret = counter.call(); + QVERIFY(ret.isNumber()); + } + QScriptValue counter2 = createCounter.call(); + QVERIFY(counter2.isFunction()); + QVERIFY(!counter2.equals(counter)); + { + QScriptValue ret = counter2.call(); + QVERIFY(ret.isNumber()); + } + } + + // Same program run in different engines + { + QString code("1 + 2"); + QScriptProgram program(code); + QVERIFY(!program.isNull()); + double expected = eng.evaluate(program).toNumber(); + for (int x = 0; x < 2; ++x) { + QScriptEngine eng2; + for (int y = 0; y < 2; ++y) { + double ret = eng2.evaluate(program).toNumber(); + QCOMPARE(ret, expected); + } + } + } + + // No program + { + QScriptProgram program; + QVERIFY(program.isNull()); + QScriptValue ret = eng.evaluate(program); + QVERIFY(!ret.isValid()); + } +} + +void tst_QScriptEngine::checkSyntax_data() +{ + QTest::addColumn<QString>("code"); + QTest::addColumn<int>("expectedState"); + QTest::addColumn<int>("errorLineNumber"); + QTest::addColumn<int>("errorColumnNumber"); + QTest::addColumn<QString>("errorMessage"); + + QTest::newRow("0") + << QString("0") << int(QScriptSyntaxCheckResult::Valid) + << -1 << -1 << ""; + QTest::newRow("if (") + << QString("if (\n") << int(QScriptSyntaxCheckResult::Intermediate) + << 1 << 4 << ""; + QTest::newRow("if else") + << QString("\nif else") << int(QScriptSyntaxCheckResult::Error) + << 2 << 4 << "SyntaxError: Parse error"; + QTest::newRow("{if}") + << QString("{\n{\nif\n}\n") << int(QScriptSyntaxCheckResult::Error) + << 4 << 1 << "SyntaxError: Parse error"; + QTest::newRow("foo[") + << QString("foo[") << int(QScriptSyntaxCheckResult::Error) + << 1 << 4 << "SyntaxError: Parse error"; + QTest::newRow("foo['bar']") + << QString("foo['bar']") << int(QScriptSyntaxCheckResult::Valid) + << -1 << -1 << ""; + + QTest::newRow("/*") + << QString("/*") << int(QScriptSyntaxCheckResult::Intermediate) + << 1 << 1 << "Unclosed comment at end of file"; + QTest::newRow("/*\nMy comment") + << QString("/*\nMy comment") << int(QScriptSyntaxCheckResult::Intermediate) + << 1 << 1 << "Unclosed comment at end of file"; + QTest::newRow("/*\nMy comment */\nfoo = 10") + << QString("/*\nMy comment */\nfoo = 10") << int(QScriptSyntaxCheckResult::Valid) + << -1 << -1 << ""; + QTest::newRow("foo = 10 /*") + << QString("foo = 10 /*") << int(QScriptSyntaxCheckResult::Intermediate) + << -1 << -1 << ""; + QTest::newRow("foo = 10; /*") + << QString("foo = 10; /*") << int(QScriptSyntaxCheckResult::Intermediate) + << 1 << 11 << "Expected `end of file'"; + QTest::newRow("foo = 10 /* My comment */") + << QString("foo = 10 /* My comment */") << int(QScriptSyntaxCheckResult::Valid) + << -1 << -1 << ""; + + QTest::newRow("/=/") + << QString("/=/") << int(QScriptSyntaxCheckResult::Valid) << -1 << -1 << ""; + QTest::newRow("/=/g") + << QString("/=/g") << int(QScriptSyntaxCheckResult::Valid) << -1 << -1 << ""; + QTest::newRow("/a/") + << QString("/a/") << int(QScriptSyntaxCheckResult::Valid) << -1 << -1 << ""; + QTest::newRow("/a/g") + << QString("/a/g") << int(QScriptSyntaxCheckResult::Valid) << -1 << -1 << ""; +} + +void tst_QScriptEngine::checkSyntax() +{ + QFETCH(QString, code); + QFETCH(int, expectedState); + QFETCH(int, errorLineNumber); + QFETCH(int, errorColumnNumber); + QFETCH(QString, errorMessage); + + QScriptSyntaxCheckResult result = QScriptEngine::checkSyntax(code); + + // assignment + { + QScriptSyntaxCheckResult copy = result; + QCOMPARE(copy.state(), result.state()); + QCOMPARE(copy.errorLineNumber(), result.errorLineNumber()); + QCOMPARE(copy.errorColumnNumber(), result.errorColumnNumber()); + QCOMPARE(copy.errorMessage(), result.errorMessage()); + } + { + QScriptSyntaxCheckResult copy(result); + QCOMPARE(copy.state(), result.state()); + QCOMPARE(copy.errorLineNumber(), result.errorLineNumber()); + QCOMPARE(copy.errorColumnNumber(), result.errorColumnNumber()); + QCOMPARE(copy.errorMessage(), result.errorMessage()); + } + + if (expectedState == QScriptSyntaxCheckResult::Intermediate) + QEXPECT_FAIL("", "QScriptSyntaxCheckResult::state() doesn't return the Intermediate state", Abort); + QCOMPARE(result.state(), QScriptSyntaxCheckResult::State(expectedState)); + QCOMPARE(result.errorLineNumber(), errorLineNumber); + if (expectedState != QScriptSyntaxCheckResult::Valid && errorColumnNumber != 1) + QEXPECT_FAIL("", "QScriptSyntaxCheckResult::errorColumnNumber() doesn't return correct value", Continue); + QCOMPARE(result.errorColumnNumber(), errorColumnNumber); + QCOMPARE(result.errorMessage(), errorMessage); +} + + QTEST_MAIN(tst_QScriptEngine) #include "tst_qscriptengine.moc" diff --git a/JavaScriptCore/qt/tests/qscriptstring/qscriptstring.pro b/JavaScriptCore/qt/tests/qscriptstring/qscriptstring.pro new file mode 100644 index 0000000..5ad9b7c --- /dev/null +++ b/JavaScriptCore/qt/tests/qscriptstring/qscriptstring.pro @@ -0,0 +1,7 @@ +TEMPLATE = app +TARGET = tst_qscriptstring +QT += testlib +include(../tests.pri) + +SOURCES += tst_qscriptstring.cpp + diff --git a/JavaScriptCore/qt/tests/qscriptstring/tst_qscriptstring.cpp b/JavaScriptCore/qt/tests/qscriptstring/tst_qscriptstring.cpp new file mode 100644 index 0000000..ff31835 --- /dev/null +++ b/JavaScriptCore/qt/tests/qscriptstring/tst_qscriptstring.cpp @@ -0,0 +1,175 @@ +/* + Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef tst_qscriptstring_h +#define tst_qscriptstring_h + +#include "qscriptengine.h" +#include "qscriptstring.h" +#include <QtCore/qhash.h> +#include <QtTest/QtTest> + +class tst_QScriptString : public QObject { + Q_OBJECT + +public: + tst_QScriptString(); + virtual ~tst_QScriptString(); + +private slots: + void test(); + void hash(); + void toArrayIndex_data(); + void toArrayIndex(); +}; + +tst_QScriptString::tst_QScriptString() +{ +} + +tst_QScriptString::~tst_QScriptString() +{ +} + +void tst_QScriptString::test() +{ + QScriptEngine eng; + { + QScriptString str; + QVERIFY(!str.isValid()); + QVERIFY(str == str); + QVERIFY(!(str != str)); + QVERIFY(str.toString().isNull()); + + QScriptString str1(str); + QVERIFY(!str1.isValid()); + + QScriptString str2 = str; + QVERIFY(!str2.isValid()); + + QCOMPARE(str.toArrayIndex(), quint32(0xffffffff)); + } + for (int x = 0; x < 2; ++x) { + QString ciao = QString::fromLatin1("ciao"); + QScriptString str = eng.toStringHandle(ciao); + QVERIFY(str.isValid()); + QVERIFY(str == str); + QVERIFY(!(str != str)); + QCOMPARE(str.toString(), ciao); + + QScriptString str1(str); + QCOMPARE(str, str1); + + QScriptString str2 = str; + QCOMPARE(str, str2); + + QScriptString str3 = eng.toStringHandle(ciao); + QVERIFY(str3.isValid()); + QCOMPARE(str, str3); + + eng.collectGarbage(); + + QVERIFY(str.isValid()); + QCOMPARE(str.toString(), ciao); + QVERIFY(str1.isValid()); + QCOMPARE(str1.toString(), ciao); + QVERIFY(str2.isValid()); + QCOMPARE(str2.toString(), ciao); + QVERIFY(str3.isValid()); + QCOMPARE(str3.toString(), ciao); + } + { + QScriptEngine* eng2 = new QScriptEngine; + QString one = QString::fromLatin1("one"); + QString two = QString::fromLatin1("two"); + QScriptString oneInterned = eng2->toStringHandle(one); + QCOMPARE(oneInterned.toString(), one); + QScriptString twoInterned = eng2->toStringHandle(two); + QCOMPARE(twoInterned.toString(), two); + QVERIFY(oneInterned != twoInterned); + QVERIFY(!(oneInterned == twoInterned)); + + delete eng2; + } +} + +void tst_QScriptString::hash() +{ + QScriptEngine engine; + QHash<QScriptString, int> stringToInt; + QScriptString foo = engine.toStringHandle("foo"); + + QScriptString bar = engine.toStringHandle("bar"); + QVERIFY(!stringToInt.contains(foo)); + for (int i = 0; i < 1000000; ++i) + stringToInt.insert(foo, 123); + QCOMPARE(stringToInt.value(foo), 123); + QVERIFY(!stringToInt.contains(bar)); + stringToInt.insert(bar, 456); + QCOMPARE(stringToInt.value(bar), 456); + QCOMPARE(stringToInt.value(foo), 123); +} + +void tst_QScriptString::toArrayIndex_data() +{ + QTest::addColumn<QString>("input"); + QTest::addColumn<bool>("expectSuccess"); + QTest::addColumn<quint32>("expectedIndex"); + QTest::newRow("foo") << QString::fromLatin1("foo") << false << quint32(0xffffffff); + QTest::newRow("empty") << QString::fromLatin1("") << false << quint32(0xffffffff); + QTest::newRow("0") << QString::fromLatin1("0") << true << quint32(0); + QTest::newRow("00") << QString::fromLatin1("00") << false << quint32(0xffffffff); + QTest::newRow("1") << QString::fromLatin1("1") << true << quint32(1); + QTest::newRow("123") << QString::fromLatin1("123") << true << quint32(123); + QTest::newRow("-1") << QString::fromLatin1("-1") << false << quint32(0xffffffff); + QTest::newRow("0a") << QString::fromLatin1("0a") << false << quint32(0xffffffff); + QTest::newRow("0x1") << QString::fromLatin1("0x1") << false << quint32(0xffffffff); + QTest::newRow("01") << QString::fromLatin1("01") << false << quint32(0xffffffff); + QTest::newRow("101a") << QString::fromLatin1("101a") << false << quint32(0xffffffff); + QTest::newRow("4294967294") << QString::fromLatin1("4294967294") << true << quint32(0xfffffffe); + QTest::newRow("4294967295") << QString::fromLatin1("4294967295") << false << quint32(0xffffffff); + QTest::newRow("11111111111") << QString::fromLatin1("11111111111") << false << quint32(0xffffffff); + QTest::newRow("0.0") << QString::fromLatin1("0.0") << false << quint32(0xffffffff); + QTest::newRow("1.0") << QString::fromLatin1("1.0") << false << quint32(0xffffffff); + QTest::newRow("1.5") << QString::fromLatin1("1.5") << false << quint32(0xffffffff); + QTest::newRow("1.") << QString::fromLatin1("1.") << false << quint32(0xffffffff); + QTest::newRow(".1") << QString::fromLatin1(".1") << false << quint32(0xffffffff); + QTest::newRow("1e0") << QString::fromLatin1("1e0") << false << quint32(0xffffffff); +} + +void tst_QScriptString::toArrayIndex() +{ + QFETCH(QString, input); + QFETCH(bool, expectSuccess); + QFETCH(quint32, expectedIndex); + QScriptEngine engine; + for (int x = 0; x < 2; ++x) { + bool isArrayIndex; + bool* ptr = (!x) ? &isArrayIndex : (bool*)0; + quint32 result = engine.toStringHandle(input).toArrayIndex(ptr); + if (!x) + QCOMPARE(isArrayIndex, expectSuccess); + QCOMPARE(result, expectedIndex); + } +} + +QTEST_MAIN(tst_QScriptString) +#include "tst_qscriptstring.moc" + +#endif // tst_qscriptstring_h diff --git a/JavaScriptCore/qt/tests/qscriptvalue/qscriptvalue.pro b/JavaScriptCore/qt/tests/qscriptvalue/qscriptvalue.pro index b12337f..35cc17d 100644 --- a/JavaScriptCore/qt/tests/qscriptvalue/qscriptvalue.pro +++ b/JavaScriptCore/qt/tests/qscriptvalue/qscriptvalue.pro @@ -1,6 +1,7 @@ TEMPLATE = app TARGET = tst_qscriptvalue QT += testlib +isEmpty(OUTPUT_DIR): OUTPUT_DIR = ../../../.. include(../tests.pri) SOURCES += \ diff --git a/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.cpp b/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.cpp index b44c5ca..82f0901 100644 --- a/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.cpp +++ b/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.cpp @@ -274,7 +274,7 @@ void tst_QScriptValue::ctor() QVERIFY(QScriptValue(0, QString("ciao")).isString()); } -void tst_QScriptValue::toString_data() +void tst_QScriptValue::toStringSimple_data() { QTest::addColumn<QString>("code"); QTest::addColumn<QString>("result"); @@ -289,7 +289,7 @@ void tst_QScriptValue::toString_data() } /* Test conversion to string from different JSC types */ -void tst_QScriptValue::toString() +void tst_QScriptValue::toStringSimple() { QFETCH(QString, code); QFETCH(QString, result); diff --git a/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.h b/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.h index 1b3c657..28154a9 100644 --- a/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.h +++ b/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue.h @@ -36,8 +36,8 @@ public: virtual ~tst_QScriptValue(); private slots: - void toString_data(); - void toString(); + void toStringSimple_data(); + void toStringSimple(); void copyConstructor_data(); void copyConstructor(); void assignOperator_data(); @@ -76,6 +76,9 @@ private slots: void isValid_data(); void isValid(); + void toString_data(); + void toString(); + void toNumber_data(); void toNumber(); @@ -146,6 +149,10 @@ private: void isValid_makeData(const char* expr); void isValid_test(const char* expr, const QScriptValue& value); + void toString_initData(); + void toString_makeData(const char*); + void toString_test(const char*, const QScriptValue&); + void toNumber_initData(); void toNumber_makeData(const char*); void toNumber_test(const char*, const QScriptValue&); diff --git a/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue_generated.cpp b/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue_generated.cpp index 006b343..970c960 100644 --- a/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue_generated.cpp +++ b/JavaScriptCore/qt/tests/qscriptvalue/tst_qscriptvalue_generated.cpp @@ -130,6 +130,35 @@ void tst_QScriptValue::initScriptValues() DEFINE_TEST_VALUE(engine->evaluate("new Object()")); DEFINE_TEST_VALUE(engine->evaluate("new Array()")); DEFINE_TEST_VALUE(engine->evaluate("new Error()")); + DEFINE_TEST_VALUE(engine->evaluate("a = new Object(); a.foo = 22; a.foo")); + DEFINE_TEST_VALUE(engine->evaluate("Undefined")); + DEFINE_TEST_VALUE(engine->evaluate("Null")); + DEFINE_TEST_VALUE(engine->evaluate("True")); + DEFINE_TEST_VALUE(engine->evaluate("False")); + DEFINE_TEST_VALUE(engine->evaluate("undefined")); + DEFINE_TEST_VALUE(engine->evaluate("null")); + DEFINE_TEST_VALUE(engine->evaluate("true")); + DEFINE_TEST_VALUE(engine->evaluate("false")); + DEFINE_TEST_VALUE(engine->evaluate("122")); + DEFINE_TEST_VALUE(engine->evaluate("124")); + DEFINE_TEST_VALUE(engine->evaluate("0")); + DEFINE_TEST_VALUE(engine->evaluate("0.0")); + DEFINE_TEST_VALUE(engine->evaluate("123.0")); + DEFINE_TEST_VALUE(engine->evaluate("6.37e-8")); + DEFINE_TEST_VALUE(engine->evaluate("-6.37e-8")); + DEFINE_TEST_VALUE(engine->evaluate("0x43211234")); + DEFINE_TEST_VALUE(engine->evaluate("0x10000")); + DEFINE_TEST_VALUE(engine->evaluate("0x10001")); + DEFINE_TEST_VALUE(engine->evaluate("NaN")); + DEFINE_TEST_VALUE(engine->evaluate("Infinity")); + DEFINE_TEST_VALUE(engine->evaluate("-Infinity")); + DEFINE_TEST_VALUE(engine->evaluate("'ciao'")); + DEFINE_TEST_VALUE(engine->evaluate("''")); + DEFINE_TEST_VALUE(engine->evaluate("'0'")); + DEFINE_TEST_VALUE(engine->evaluate("'123'")); + DEFINE_TEST_VALUE(engine->evaluate("'12.4'")); + DEFINE_TEST_VALUE(engine->nullValue()); + DEFINE_TEST_VALUE(engine->undefinedValue()); } @@ -244,7 +273,36 @@ void tst_QScriptValue::isValid_makeData(const char* expr) << "engine->evaluate(\"/foo/\")" << "engine->evaluate(\"new Object()\")" << "engine->evaluate(\"new Array()\")" - << "engine->evaluate(\"new Error()\")"; + << "engine->evaluate(\"new Error()\")" + << "engine->evaluate(\"a = new Object(); a.foo = 22; a.foo\")" + << "engine->evaluate(\"Undefined\")" + << "engine->evaluate(\"Null\")" + << "engine->evaluate(\"True\")" + << "engine->evaluate(\"False\")" + << "engine->evaluate(\"undefined\")" + << "engine->evaluate(\"null\")" + << "engine->evaluate(\"true\")" + << "engine->evaluate(\"false\")" + << "engine->evaluate(\"122\")" + << "engine->evaluate(\"124\")" + << "engine->evaluate(\"0\")" + << "engine->evaluate(\"0.0\")" + << "engine->evaluate(\"123.0\")" + << "engine->evaluate(\"6.37e-8\")" + << "engine->evaluate(\"-6.37e-8\")" + << "engine->evaluate(\"0x43211234\")" + << "engine->evaluate(\"0x10000\")" + << "engine->evaluate(\"0x10001\")" + << "engine->evaluate(\"NaN\")" + << "engine->evaluate(\"Infinity\")" + << "engine->evaluate(\"-Infinity\")" + << "engine->evaluate(\"'ciao'\")" + << "engine->evaluate(\"''\")" + << "engine->evaluate(\"'0'\")" + << "engine->evaluate(\"'123'\")" + << "engine->evaluate(\"'12.4'\")" + << "engine->nullValue()" + << "engine->undefinedValue()"; } newRow(expr) << isValid.contains(expr); } @@ -253,6 +311,7 @@ void tst_QScriptValue::isValid_test(const char*, const QScriptValue& value) { QFETCH(bool, expected); QCOMPARE(value.isValid(), expected); + QCOMPARE(value.isValid(), expected); } DEFINE_TEST_FUNCTION(isValid) @@ -273,7 +332,9 @@ void tst_QScriptValue::isBool_makeData(const char* expr) << "QScriptValue(0, true)" << "QScriptValue(0, false)" << "QScriptValue(engine, true)" - << "QScriptValue(engine, false)"; + << "QScriptValue(engine, false)" + << "engine->evaluate(\"true\")" + << "engine->evaluate(\"false\")"; } newRow(expr) << isBool.contains(expr); } @@ -282,6 +343,7 @@ void tst_QScriptValue::isBool_test(const char*, const QScriptValue& value) { QFETCH(bool, expected); QCOMPARE(value.isBool(), expected); + QCOMPARE(value.isBool(), expected); } DEFINE_TEST_FUNCTION(isBool) @@ -302,7 +364,9 @@ void tst_QScriptValue::isBoolean_makeData(const char* expr) << "QScriptValue(0, true)" << "QScriptValue(0, false)" << "QScriptValue(engine, true)" - << "QScriptValue(engine, false)"; + << "QScriptValue(engine, false)" + << "engine->evaluate(\"true\")" + << "engine->evaluate(\"false\")"; } newRow(expr) << isBoolean.contains(expr); } @@ -311,10 +375,12 @@ void tst_QScriptValue::isBoolean_test(const char*, const QScriptValue& value) { QFETCH(bool, expected); QCOMPARE(value.isBoolean(), expected); + QCOMPARE(value.isBoolean(), expected); } DEFINE_TEST_FUNCTION(isBoolean) + void tst_QScriptValue::isNumber_initData() { QTest::addColumn<bool>("expected"); @@ -366,7 +432,21 @@ void tst_QScriptValue::isNumber_makeData(const char* expr) << "QScriptValue(engine, qSNaN())" << "QScriptValue(engine, qQNaN())" << "QScriptValue(engine, qInf())" - << "QScriptValue(engine, -qInf())"; + << "QScriptValue(engine, -qInf())" + << "engine->evaluate(\"a = new Object(); a.foo = 22; a.foo\")" + << "engine->evaluate(\"122\")" + << "engine->evaluate(\"124\")" + << "engine->evaluate(\"0\")" + << "engine->evaluate(\"0.0\")" + << "engine->evaluate(\"123.0\")" + << "engine->evaluate(\"6.37e-8\")" + << "engine->evaluate(\"-6.37e-8\")" + << "engine->evaluate(\"0x43211234\")" + << "engine->evaluate(\"0x10000\")" + << "engine->evaluate(\"0x10001\")" + << "engine->evaluate(\"NaN\")" + << "engine->evaluate(\"Infinity\")" + << "engine->evaluate(\"-Infinity\")"; } newRow(expr) << isNumber.contains(expr); } @@ -375,6 +455,7 @@ void tst_QScriptValue::isNumber_test(const char*, const QScriptValue& value) { QFETCH(bool, expected); QCOMPARE(value.isNumber(), expected); + QCOMPARE(value.isNumber(), expected); } DEFINE_TEST_FUNCTION(isNumber) @@ -407,6 +488,7 @@ void tst_QScriptValue::isFunction_test(const char*, const QScriptValue& value) { QFETCH(bool, expected); QCOMPARE(value.isFunction(), expected); + QCOMPARE(value.isFunction(), expected); } DEFINE_TEST_FUNCTION(isFunction) @@ -424,7 +506,9 @@ void tst_QScriptValue::isNull_makeData(const char* expr) if (isNull.isEmpty()) { isNull << "QScriptValue(QScriptValue::NullValue)" << "QScriptValue(0, QScriptValue::NullValue)" - << "QScriptValue(engine, QScriptValue::NullValue)"; + << "QScriptValue(engine, QScriptValue::NullValue)" + << "engine->evaluate(\"null\")" + << "engine->nullValue()"; } newRow(expr) << isNull.contains(expr); } @@ -433,6 +517,7 @@ void tst_QScriptValue::isNull_test(const char*, const QScriptValue& value) { QFETCH(bool, expected); QCOMPARE(value.isNull(), expected); + QCOMPARE(value.isNull(), expected); } DEFINE_TEST_FUNCTION(isNull) @@ -477,7 +562,12 @@ void tst_QScriptValue::isString_makeData(const char* expr) << "QScriptValue(engine, QString())" << "QScriptValue(engine, QString(\"0\"))" << "QScriptValue(engine, QString(\"123\"))" - << "QScriptValue(engine, QString(\"1.23\"))"; + << "QScriptValue(engine, QString(\"1.23\"))" + << "engine->evaluate(\"'ciao'\")" + << "engine->evaluate(\"''\")" + << "engine->evaluate(\"'0'\")" + << "engine->evaluate(\"'123'\")" + << "engine->evaluate(\"'12.4'\")"; } newRow(expr) << isString.contains(expr); } @@ -486,6 +576,7 @@ void tst_QScriptValue::isString_test(const char*, const QScriptValue& value) { QFETCH(bool, expected); QCOMPARE(value.isString(), expected); + QCOMPARE(value.isString(), expected); } DEFINE_TEST_FUNCTION(isString) @@ -504,7 +595,9 @@ void tst_QScriptValue::isUndefined_makeData(const char* expr) isUndefined << "QScriptValue(QScriptValue::UndefinedValue)" << "QScriptValue(0, QScriptValue::UndefinedValue)" << "QScriptValue(engine, QScriptValue::UndefinedValue)" - << "engine->evaluate(\"{}\")"; + << "engine->evaluate(\"{}\")" + << "engine->evaluate(\"undefined\")" + << "engine->undefinedValue()"; } newRow(expr) << isUndefined.contains(expr); } @@ -513,10 +606,15 @@ void tst_QScriptValue::isUndefined_test(const char*, const QScriptValue& value) { QFETCH(bool, expected); QCOMPARE(value.isUndefined(), expected); + QCOMPARE(value.isUndefined(), expected); } DEFINE_TEST_FUNCTION(isUndefined) + + + + void tst_QScriptValue::isObject_initData() { QTest::addColumn<bool>("expected"); @@ -543,7 +641,11 @@ void tst_QScriptValue::isObject_makeData(const char* expr) << "engine->evaluate(\"/foo/\")" << "engine->evaluate(\"new Object()\")" << "engine->evaluate(\"new Array()\")" - << "engine->evaluate(\"new Error()\")"; + << "engine->evaluate(\"new Error()\")" + << "engine->evaluate(\"Undefined\")" + << "engine->evaluate(\"Null\")" + << "engine->evaluate(\"True\")" + << "engine->evaluate(\"False\")"; } newRow(expr) << isObject.contains(expr); } @@ -552,10 +654,168 @@ void tst_QScriptValue::isObject_test(const char*, const QScriptValue& value) { QFETCH(bool, expected); QCOMPARE(value.isObject(), expected); + QCOMPARE(value.isObject(), expected); } DEFINE_TEST_FUNCTION(isObject) + +void tst_QScriptValue::toString_initData() +{ + QTest::addColumn<QString>("expected"); + initScriptValues(); +} + +void tst_QScriptValue::toString_makeData(const char* expr) +{ + static QHash<QString, QString> toString; + if (toString.isEmpty()) { + toString.insert("QScriptValue()", ""); + toString.insert("QScriptValue(QScriptValue::UndefinedValue)", "undefined"); + toString.insert("QScriptValue(QScriptValue::NullValue)", "null"); + toString.insert("QScriptValue(true)", "true"); + toString.insert("QScriptValue(false)", "false"); + toString.insert("QScriptValue(int(122))", "122"); + toString.insert("QScriptValue(uint(124))", "124"); + toString.insert("QScriptValue(0)", "0"); + toString.insert("QScriptValue(0.0)", "0"); + toString.insert("QScriptValue(123.0)", "123"); + toString.insert("QScriptValue(6.37e-8)", "6.37e-8"); + toString.insert("QScriptValue(-6.37e-8)", "-6.37e-8"); + toString.insert("QScriptValue(0x43211234)", "1126240820"); + toString.insert("QScriptValue(0x10000)", "65536"); + toString.insert("QScriptValue(0x10001)", "65537"); + toString.insert("QScriptValue(qSNaN())", "NaN"); + toString.insert("QScriptValue(qQNaN())", "NaN"); + toString.insert("QScriptValue(qInf())", "Infinity"); + toString.insert("QScriptValue(-qInf())", "-Infinity"); + toString.insert("QScriptValue(\"NaN\")", "NaN"); + toString.insert("QScriptValue(\"Infinity\")", "Infinity"); + toString.insert("QScriptValue(\"-Infinity\")", "-Infinity"); + toString.insert("QScriptValue(\"ciao\")", "ciao"); + toString.insert("QScriptValue(QString::fromLatin1(\"ciao\"))", "ciao"); + toString.insert("QScriptValue(QString(\"\"))", ""); + toString.insert("QScriptValue(QString())", ""); + toString.insert("QScriptValue(QString(\"0\"))", "0"); + toString.insert("QScriptValue(QString(\"123\"))", "123"); + toString.insert("QScriptValue(QString(\"12.4\"))", "12.4"); + toString.insert("QScriptValue(0, QScriptValue::UndefinedValue)", "undefined"); + toString.insert("QScriptValue(0, QScriptValue::NullValue)", "null"); + toString.insert("QScriptValue(0, true)", "true"); + toString.insert("QScriptValue(0, false)", "false"); + toString.insert("QScriptValue(0, int(122))", "122"); + toString.insert("QScriptValue(0, uint(124))", "124"); + toString.insert("QScriptValue(0, 0)", "0"); + toString.insert("QScriptValue(0, 0.0)", "0"); + toString.insert("QScriptValue(0, 123.0)", "123"); + toString.insert("QScriptValue(0, 6.37e-8)", "6.37e-8"); + toString.insert("QScriptValue(0, -6.37e-8)", "-6.37e-8"); + toString.insert("QScriptValue(0, 0x43211234)", "1126240820"); + toString.insert("QScriptValue(0, 0x10000)", "65536"); + toString.insert("QScriptValue(0, 0x10001)", "65537"); + toString.insert("QScriptValue(0, qSNaN())", "NaN"); + toString.insert("QScriptValue(0, qQNaN())", "NaN"); + toString.insert("QScriptValue(0, qInf())", "Infinity"); + toString.insert("QScriptValue(0, -qInf())", "-Infinity"); + toString.insert("QScriptValue(0, \"NaN\")", "NaN"); + toString.insert("QScriptValue(0, \"Infinity\")", "Infinity"); + toString.insert("QScriptValue(0, \"-Infinity\")", "-Infinity"); + toString.insert("QScriptValue(0, \"ciao\")", "ciao"); + toString.insert("QScriptValue(0, QString::fromLatin1(\"ciao\"))", "ciao"); + toString.insert("QScriptValue(0, QString(\"\"))", ""); + toString.insert("QScriptValue(0, QString())", ""); + toString.insert("QScriptValue(0, QString(\"0\"))", "0"); + toString.insert("QScriptValue(0, QString(\"123\"))", "123"); + toString.insert("QScriptValue(0, QString(\"12.3\"))", "12.3"); + toString.insert("QScriptValue(engine, QScriptValue::UndefinedValue)", "undefined"); + toString.insert("QScriptValue(engine, QScriptValue::NullValue)", "null"); + toString.insert("QScriptValue(engine, true)", "true"); + toString.insert("QScriptValue(engine, false)", "false"); + toString.insert("QScriptValue(engine, int(122))", "122"); + toString.insert("QScriptValue(engine, uint(124))", "124"); + toString.insert("QScriptValue(engine, 0)", "0"); + toString.insert("QScriptValue(engine, 0.0)", "0"); + toString.insert("QScriptValue(engine, 123.0)", "123"); + toString.insert("QScriptValue(engine, 6.37e-8)", "6.37e-8"); + toString.insert("QScriptValue(engine, -6.37e-8)", "-6.37e-8"); + toString.insert("QScriptValue(engine, 0x43211234)", "1126240820"); + toString.insert("QScriptValue(engine, 0x10000)", "65536"); + toString.insert("QScriptValue(engine, 0x10001)", "65537"); + toString.insert("QScriptValue(engine, qSNaN())", "NaN"); + toString.insert("QScriptValue(engine, qQNaN())", "NaN"); + toString.insert("QScriptValue(engine, qInf())", "Infinity"); + toString.insert("QScriptValue(engine, -qInf())", "-Infinity"); + toString.insert("QScriptValue(engine, \"NaN\")", "NaN"); + toString.insert("QScriptValue(engine, \"Infinity\")", "Infinity"); + toString.insert("QScriptValue(engine, \"-Infinity\")", "-Infinity"); + toString.insert("QScriptValue(engine, \"ciao\")", "ciao"); + toString.insert("QScriptValue(engine, QString::fromLatin1(\"ciao\"))", "ciao"); + toString.insert("QScriptValue(engine, QString(\"\"))", ""); + toString.insert("QScriptValue(engine, QString())", ""); + toString.insert("QScriptValue(engine, QString(\"0\"))", "0"); + toString.insert("QScriptValue(engine, QString(\"123\"))", "123"); + toString.insert("QScriptValue(engine, QString(\"1.23\"))", "1.23"); + toString.insert("engine->evaluate(\"[]\")", ""); + toString.insert("engine->evaluate(\"{}\")", "undefined"); + toString.insert("engine->evaluate(\"Object.prototype\")", "[object Object]"); + toString.insert("engine->evaluate(\"Date.prototype\")", "Invalid Date"); + toString.insert("engine->evaluate(\"Array.prototype\")", ""); + toString.insert("engine->evaluate(\"Function.prototype\")", "function () {\n [native code]\n}"); + toString.insert("engine->evaluate(\"Error.prototype\")", "Error: Unknown error"); + toString.insert("engine->evaluate(\"Object\")", "function Object() {\n [native code]\n}"); + toString.insert("engine->evaluate(\"Array\")", "function Array() {\n [native code]\n}"); + toString.insert("engine->evaluate(\"Number\")", "function Number() {\n [native code]\n}"); + toString.insert("engine->evaluate(\"Function\")", "function Function() {\n [native code]\n}"); + toString.insert("engine->evaluate(\"(function() { return 1; })\")", "function () { return 1; }"); + toString.insert("engine->evaluate(\"(function() { return 'ciao'; })\")", "function () { return 'ciao'; }"); + toString.insert("engine->evaluate(\"(function() { throw new Error('foo'); })\")", "function () { throw new Error('foo'); }"); + toString.insert("engine->evaluate(\"/foo/\")", "/foo/"); + toString.insert("engine->evaluate(\"new Object()\")", "[object Object]"); + toString.insert("engine->evaluate(\"new Array()\")", ""); + toString.insert("engine->evaluate(\"new Error()\")", "Error: Unknown error"); + toString.insert("engine->evaluate(\"a = new Object(); a.foo = 22; a.foo\")", "22"); + toString.insert("engine->evaluate(\"Undefined\")", "ReferenceError: Can't find variable: Undefined"); + toString.insert("engine->evaluate(\"Null\")", "ReferenceError: Can't find variable: Null"); + toString.insert("engine->evaluate(\"True\")", "ReferenceError: Can't find variable: True"); + toString.insert("engine->evaluate(\"False\")", "ReferenceError: Can't find variable: False"); + toString.insert("engine->evaluate(\"undefined\")", "undefined"); + toString.insert("engine->evaluate(\"null\")", "null"); + toString.insert("engine->evaluate(\"true\")", "true"); + toString.insert("engine->evaluate(\"false\")", "false"); + toString.insert("engine->evaluate(\"122\")", "122"); + toString.insert("engine->evaluate(\"124\")", "124"); + toString.insert("engine->evaluate(\"0\")", "0"); + toString.insert("engine->evaluate(\"0.0\")", "0"); + toString.insert("engine->evaluate(\"123.0\")", "123"); + toString.insert("engine->evaluate(\"6.37e-8\")", "6.37e-8"); + toString.insert("engine->evaluate(\"-6.37e-8\")", "-6.37e-8"); + toString.insert("engine->evaluate(\"0x43211234\")", "1126240820"); + toString.insert("engine->evaluate(\"0x10000\")", "65536"); + toString.insert("engine->evaluate(\"0x10001\")", "65537"); + toString.insert("engine->evaluate(\"NaN\")", "NaN"); + toString.insert("engine->evaluate(\"Infinity\")", "Infinity"); + toString.insert("engine->evaluate(\"-Infinity\")", "-Infinity"); + toString.insert("engine->evaluate(\"'ciao'\")", "ciao"); + toString.insert("engine->evaluate(\"''\")", ""); + toString.insert("engine->evaluate(\"'0'\")", "0"); + toString.insert("engine->evaluate(\"'123'\")", "123"); + toString.insert("engine->evaluate(\"'12.4'\")", "12.4"); + toString.insert("engine->nullValue()", "null"); + toString.insert("engine->undefinedValue()", "undefined"); + } + newRow(expr) << toString.value(expr); +} + +void tst_QScriptValue::toString_test(const char*, const QScriptValue& value) +{ + QFETCH(QString, expected); + QCOMPARE(value.toString(), expected); + QCOMPARE(value.toString(), expected); +} + +DEFINE_TEST_FUNCTION(toString) + + void tst_QScriptValue::toNumber_initData() { QTest::addColumn<qsreal>("expected"); @@ -669,6 +929,35 @@ void tst_QScriptValue::toNumber_makeData(const char* expr) toNumber.insert("engine->evaluate(\"new Object()\")", qQNaN()); toNumber.insert("engine->evaluate(\"new Array()\")", 0); toNumber.insert("engine->evaluate(\"new Error()\")", qQNaN()); + toNumber.insert("engine->evaluate(\"a = new Object(); a.foo = 22; a.foo\")", 22); + toNumber.insert("engine->evaluate(\"Undefined\")", qQNaN()); + toNumber.insert("engine->evaluate(\"Null\")", qQNaN()); + toNumber.insert("engine->evaluate(\"True\")", qQNaN()); + toNumber.insert("engine->evaluate(\"False\")", qQNaN()); + toNumber.insert("engine->evaluate(\"undefined\")", qQNaN()); + toNumber.insert("engine->evaluate(\"null\")", 0); + toNumber.insert("engine->evaluate(\"true\")", 1); + toNumber.insert("engine->evaluate(\"false\")", 0); + toNumber.insert("engine->evaluate(\"122\")", 122); + toNumber.insert("engine->evaluate(\"124\")", 124); + toNumber.insert("engine->evaluate(\"0\")", 0); + toNumber.insert("engine->evaluate(\"0.0\")", 0); + toNumber.insert("engine->evaluate(\"123.0\")", 123); + toNumber.insert("engine->evaluate(\"6.37e-8\")", 6.369999999999999e-08); + toNumber.insert("engine->evaluate(\"-6.37e-8\")", -6.369999999999999e-08); + toNumber.insert("engine->evaluate(\"0x43211234\")", 1126240820); + toNumber.insert("engine->evaluate(\"0x10000\")", 65536); + toNumber.insert("engine->evaluate(\"0x10001\")", 65537); + toNumber.insert("engine->evaluate(\"NaN\")", qQNaN()); + toNumber.insert("engine->evaluate(\"Infinity\")", qInf()); + toNumber.insert("engine->evaluate(\"-Infinity\")", qInf()); + toNumber.insert("engine->evaluate(\"'ciao'\")", qQNaN()); + toNumber.insert("engine->evaluate(\"''\")", 0); + toNumber.insert("engine->evaluate(\"'0'\")", 0); + toNumber.insert("engine->evaluate(\"'123'\")", 123); + toNumber.insert("engine->evaluate(\"'12.4'\")", 12.4); + toNumber.insert("engine->nullValue()", 0); + toNumber.insert("engine->undefinedValue()", qQNaN()); } newRow(expr) << toNumber.value(expr); } @@ -682,9 +971,11 @@ void tst_QScriptValue::toNumber_test(const char*, const QScriptValue& value) } if (qIsInf(expected)) { QVERIFY(qIsInf(value.toNumber())); + QVERIFY(qIsInf(value.toNumber())); return; } QCOMPARE(value.toNumber(), expected); + QCOMPARE(value.toNumber(), expected); } DEFINE_TEST_FUNCTION(toNumber) @@ -803,6 +1094,35 @@ void tst_QScriptValue::toBool_makeData(const char* expr) toBool.insert("engine->evaluate(\"new Object()\")", true); toBool.insert("engine->evaluate(\"new Array()\")", true); toBool.insert("engine->evaluate(\"new Error()\")", true); + toBool.insert("engine->evaluate(\"a = new Object(); a.foo = 22; a.foo\")", true); + toBool.insert("engine->evaluate(\"Undefined\")", true); + toBool.insert("engine->evaluate(\"Null\")", true); + toBool.insert("engine->evaluate(\"True\")", true); + toBool.insert("engine->evaluate(\"False\")", true); + toBool.insert("engine->evaluate(\"undefined\")", false); + toBool.insert("engine->evaluate(\"null\")", false); + toBool.insert("engine->evaluate(\"true\")", true); + toBool.insert("engine->evaluate(\"false\")", false); + toBool.insert("engine->evaluate(\"122\")", true); + toBool.insert("engine->evaluate(\"124\")", true); + toBool.insert("engine->evaluate(\"0\")", false); + toBool.insert("engine->evaluate(\"0.0\")", false); + toBool.insert("engine->evaluate(\"123.0\")", true); + toBool.insert("engine->evaluate(\"6.37e-8\")", true); + toBool.insert("engine->evaluate(\"-6.37e-8\")", true); + toBool.insert("engine->evaluate(\"0x43211234\")", true); + toBool.insert("engine->evaluate(\"0x10000\")", true); + toBool.insert("engine->evaluate(\"0x10001\")", true); + toBool.insert("engine->evaluate(\"NaN\")", false); + toBool.insert("engine->evaluate(\"Infinity\")", true); + toBool.insert("engine->evaluate(\"-Infinity\")", true); + toBool.insert("engine->evaluate(\"'ciao'\")", true); + toBool.insert("engine->evaluate(\"''\")", false); + toBool.insert("engine->evaluate(\"'0'\")", true); + toBool.insert("engine->evaluate(\"'123'\")", true); + toBool.insert("engine->evaluate(\"'12.4'\")", true); + toBool.insert("engine->nullValue()", false); + toBool.insert("engine->undefinedValue()", false); } newRow(expr) << toBool.value(expr); } @@ -811,6 +1131,7 @@ void tst_QScriptValue::toBool_test(const char*, const QScriptValue& value) { QFETCH(bool, expected); QCOMPARE(value.toBool(), expected); + QCOMPARE(value.toBool(), expected); } DEFINE_TEST_FUNCTION(toBool) @@ -929,6 +1250,35 @@ void tst_QScriptValue::toBoolean_makeData(const char* expr) toBoolean.insert("engine->evaluate(\"new Object()\")", true); toBoolean.insert("engine->evaluate(\"new Array()\")", true); toBoolean.insert("engine->evaluate(\"new Error()\")", true); + toBoolean.insert("engine->evaluate(\"a = new Object(); a.foo = 22; a.foo\")", true); + toBoolean.insert("engine->evaluate(\"Undefined\")", true); + toBoolean.insert("engine->evaluate(\"Null\")", true); + toBoolean.insert("engine->evaluate(\"True\")", true); + toBoolean.insert("engine->evaluate(\"False\")", true); + toBoolean.insert("engine->evaluate(\"undefined\")", false); + toBoolean.insert("engine->evaluate(\"null\")", false); + toBoolean.insert("engine->evaluate(\"true\")", true); + toBoolean.insert("engine->evaluate(\"false\")", false); + toBoolean.insert("engine->evaluate(\"122\")", true); + toBoolean.insert("engine->evaluate(\"124\")", true); + toBoolean.insert("engine->evaluate(\"0\")", false); + toBoolean.insert("engine->evaluate(\"0.0\")", false); + toBoolean.insert("engine->evaluate(\"123.0\")", true); + toBoolean.insert("engine->evaluate(\"6.37e-8\")", true); + toBoolean.insert("engine->evaluate(\"-6.37e-8\")", true); + toBoolean.insert("engine->evaluate(\"0x43211234\")", true); + toBoolean.insert("engine->evaluate(\"0x10000\")", true); + toBoolean.insert("engine->evaluate(\"0x10001\")", true); + toBoolean.insert("engine->evaluate(\"NaN\")", false); + toBoolean.insert("engine->evaluate(\"Infinity\")", true); + toBoolean.insert("engine->evaluate(\"-Infinity\")", true); + toBoolean.insert("engine->evaluate(\"'ciao'\")", true); + toBoolean.insert("engine->evaluate(\"''\")", false); + toBoolean.insert("engine->evaluate(\"'0'\")", true); + toBoolean.insert("engine->evaluate(\"'123'\")", true); + toBoolean.insert("engine->evaluate(\"'12.4'\")", true); + toBoolean.insert("engine->nullValue()", false); + toBoolean.insert("engine->undefinedValue()", false); } newRow(expr) << toBoolean.value(expr); } @@ -937,6 +1287,7 @@ void tst_QScriptValue::toBoolean_test(const char*, const QScriptValue& value) { QFETCH(bool, expected); QCOMPARE(value.toBoolean(), expected); + QCOMPARE(value.toBoolean(), expected); } DEFINE_TEST_FUNCTION(toBoolean) @@ -1055,6 +1406,35 @@ void tst_QScriptValue::toInteger_makeData(const char* expr) toInteger.insert("engine->evaluate(\"new Object()\")", 0); toInteger.insert("engine->evaluate(\"new Array()\")", 0); toInteger.insert("engine->evaluate(\"new Error()\")", 0); + toInteger.insert("engine->evaluate(\"a = new Object(); a.foo = 22; a.foo\")", 22); + toInteger.insert("engine->evaluate(\"Undefined\")", 0); + toInteger.insert("engine->evaluate(\"Null\")", 0); + toInteger.insert("engine->evaluate(\"True\")", 0); + toInteger.insert("engine->evaluate(\"False\")", 0); + toInteger.insert("engine->evaluate(\"undefined\")", 0); + toInteger.insert("engine->evaluate(\"null\")", 0); + toInteger.insert("engine->evaluate(\"true\")", 1); + toInteger.insert("engine->evaluate(\"false\")", 0); + toInteger.insert("engine->evaluate(\"122\")", 122); + toInteger.insert("engine->evaluate(\"124\")", 124); + toInteger.insert("engine->evaluate(\"0\")", 0); + toInteger.insert("engine->evaluate(\"0.0\")", 0); + toInteger.insert("engine->evaluate(\"123.0\")", 123); + toInteger.insert("engine->evaluate(\"6.37e-8\")", 0); + toInteger.insert("engine->evaluate(\"-6.37e-8\")", 0); + toInteger.insert("engine->evaluate(\"0x43211234\")", 1126240820); + toInteger.insert("engine->evaluate(\"0x10000\")", 65536); + toInteger.insert("engine->evaluate(\"0x10001\")", 65537); + toInteger.insert("engine->evaluate(\"NaN\")", 0); + toInteger.insert("engine->evaluate(\"Infinity\")", qInf()); + toInteger.insert("engine->evaluate(\"-Infinity\")", qInf()); + toInteger.insert("engine->evaluate(\"'ciao'\")", 0); + toInteger.insert("engine->evaluate(\"''\")", 0); + toInteger.insert("engine->evaluate(\"'0'\")", 0); + toInteger.insert("engine->evaluate(\"'123'\")", 123); + toInteger.insert("engine->evaluate(\"'12.4'\")", 12); + toInteger.insert("engine->nullValue()", 0); + toInteger.insert("engine->undefinedValue()", 0); } newRow(expr) << toInteger.value(expr); } @@ -1064,9 +1444,11 @@ void tst_QScriptValue::toInteger_test(const char*, const QScriptValue& value) QFETCH(qsreal, expected); if (qIsInf(expected)) { QVERIFY(qIsInf(value.toInteger())); + QVERIFY(qIsInf(value.toInteger())); return; } QCOMPARE(value.toInteger(), expected); + QCOMPARE(value.toInteger(), expected); } DEFINE_TEST_FUNCTION(toInteger) @@ -1185,6 +1567,35 @@ void tst_QScriptValue::toInt32_makeData(const char* expr) toInt32.insert("engine->evaluate(\"new Object()\")", 0); toInt32.insert("engine->evaluate(\"new Array()\")", 0); toInt32.insert("engine->evaluate(\"new Error()\")", 0); + toInt32.insert("engine->evaluate(\"a = new Object(); a.foo = 22; a.foo\")", 22); + toInt32.insert("engine->evaluate(\"Undefined\")", 0); + toInt32.insert("engine->evaluate(\"Null\")", 0); + toInt32.insert("engine->evaluate(\"True\")", 0); + toInt32.insert("engine->evaluate(\"False\")", 0); + toInt32.insert("engine->evaluate(\"undefined\")", 0); + toInt32.insert("engine->evaluate(\"null\")", 0); + toInt32.insert("engine->evaluate(\"true\")", 1); + toInt32.insert("engine->evaluate(\"false\")", 0); + toInt32.insert("engine->evaluate(\"122\")", 122); + toInt32.insert("engine->evaluate(\"124\")", 124); + toInt32.insert("engine->evaluate(\"0\")", 0); + toInt32.insert("engine->evaluate(\"0.0\")", 0); + toInt32.insert("engine->evaluate(\"123.0\")", 123); + toInt32.insert("engine->evaluate(\"6.37e-8\")", 0); + toInt32.insert("engine->evaluate(\"-6.37e-8\")", 0); + toInt32.insert("engine->evaluate(\"0x43211234\")", 1126240820); + toInt32.insert("engine->evaluate(\"0x10000\")", 65536); + toInt32.insert("engine->evaluate(\"0x10001\")", 65537); + toInt32.insert("engine->evaluate(\"NaN\")", 0); + toInt32.insert("engine->evaluate(\"Infinity\")", 0); + toInt32.insert("engine->evaluate(\"-Infinity\")", 0); + toInt32.insert("engine->evaluate(\"'ciao'\")", 0); + toInt32.insert("engine->evaluate(\"''\")", 0); + toInt32.insert("engine->evaluate(\"'0'\")", 0); + toInt32.insert("engine->evaluate(\"'123'\")", 123); + toInt32.insert("engine->evaluate(\"'12.4'\")", 12); + toInt32.insert("engine->nullValue()", 0); + toInt32.insert("engine->undefinedValue()", 0); } newRow(expr) << toInt32.value(expr); } @@ -1193,6 +1604,7 @@ void tst_QScriptValue::toInt32_test(const char*, const QScriptValue& value) { QFETCH(qint32, expected); QCOMPARE(value.toInt32(), expected); + QCOMPARE(value.toInt32(), expected); } DEFINE_TEST_FUNCTION(toInt32) @@ -1311,6 +1723,35 @@ void tst_QScriptValue::toUInt32_makeData(const char* expr) toUInt32.insert("engine->evaluate(\"new Object()\")", 0); toUInt32.insert("engine->evaluate(\"new Array()\")", 0); toUInt32.insert("engine->evaluate(\"new Error()\")", 0); + toUInt32.insert("engine->evaluate(\"a = new Object(); a.foo = 22; a.foo\")", 22); + toUInt32.insert("engine->evaluate(\"Undefined\")", 0); + toUInt32.insert("engine->evaluate(\"Null\")", 0); + toUInt32.insert("engine->evaluate(\"True\")", 0); + toUInt32.insert("engine->evaluate(\"False\")", 0); + toUInt32.insert("engine->evaluate(\"undefined\")", 0); + toUInt32.insert("engine->evaluate(\"null\")", 0); + toUInt32.insert("engine->evaluate(\"true\")", 1); + toUInt32.insert("engine->evaluate(\"false\")", 0); + toUInt32.insert("engine->evaluate(\"122\")", 122); + toUInt32.insert("engine->evaluate(\"124\")", 124); + toUInt32.insert("engine->evaluate(\"0\")", 0); + toUInt32.insert("engine->evaluate(\"0.0\")", 0); + toUInt32.insert("engine->evaluate(\"123.0\")", 123); + toUInt32.insert("engine->evaluate(\"6.37e-8\")", 0); + toUInt32.insert("engine->evaluate(\"-6.37e-8\")", 0); + toUInt32.insert("engine->evaluate(\"0x43211234\")", 1126240820); + toUInt32.insert("engine->evaluate(\"0x10000\")", 65536); + toUInt32.insert("engine->evaluate(\"0x10001\")", 65537); + toUInt32.insert("engine->evaluate(\"NaN\")", 0); + toUInt32.insert("engine->evaluate(\"Infinity\")", 0); + toUInt32.insert("engine->evaluate(\"-Infinity\")", 0); + toUInt32.insert("engine->evaluate(\"'ciao'\")", 0); + toUInt32.insert("engine->evaluate(\"''\")", 0); + toUInt32.insert("engine->evaluate(\"'0'\")", 0); + toUInt32.insert("engine->evaluate(\"'123'\")", 123); + toUInt32.insert("engine->evaluate(\"'12.4'\")", 12); + toUInt32.insert("engine->nullValue()", 0); + toUInt32.insert("engine->undefinedValue()", 0); } newRow(expr) << toUInt32.value(expr); } @@ -1319,6 +1760,7 @@ void tst_QScriptValue::toUInt32_test(const char*, const QScriptValue& value) { QFETCH(quint32, expected); QCOMPARE(value.toUInt32(), expected); + QCOMPARE(value.toUInt32(), expected); } DEFINE_TEST_FUNCTION(toUInt32) @@ -1437,6 +1879,35 @@ void tst_QScriptValue::toUInt16_makeData(const char* expr) toUInt16.insert("engine->evaluate(\"new Object()\")", 0); toUInt16.insert("engine->evaluate(\"new Array()\")", 0); toUInt16.insert("engine->evaluate(\"new Error()\")", 0); + toUInt16.insert("engine->evaluate(\"a = new Object(); a.foo = 22; a.foo\")", 22); + toUInt16.insert("engine->evaluate(\"Undefined\")", 0); + toUInt16.insert("engine->evaluate(\"Null\")", 0); + toUInt16.insert("engine->evaluate(\"True\")", 0); + toUInt16.insert("engine->evaluate(\"False\")", 0); + toUInt16.insert("engine->evaluate(\"undefined\")", 0); + toUInt16.insert("engine->evaluate(\"null\")", 0); + toUInt16.insert("engine->evaluate(\"true\")", 1); + toUInt16.insert("engine->evaluate(\"false\")", 0); + toUInt16.insert("engine->evaluate(\"122\")", 122); + toUInt16.insert("engine->evaluate(\"124\")", 124); + toUInt16.insert("engine->evaluate(\"0\")", 0); + toUInt16.insert("engine->evaluate(\"0.0\")", 0); + toUInt16.insert("engine->evaluate(\"123.0\")", 123); + toUInt16.insert("engine->evaluate(\"6.37e-8\")", 0); + toUInt16.insert("engine->evaluate(\"-6.37e-8\")", 0); + toUInt16.insert("engine->evaluate(\"0x43211234\")", 4660); + toUInt16.insert("engine->evaluate(\"0x10000\")", 0); + toUInt16.insert("engine->evaluate(\"0x10001\")", 1); + toUInt16.insert("engine->evaluate(\"NaN\")", 0); + toUInt16.insert("engine->evaluate(\"Infinity\")", 0); + toUInt16.insert("engine->evaluate(\"-Infinity\")", 0); + toUInt16.insert("engine->evaluate(\"'ciao'\")", 0); + toUInt16.insert("engine->evaluate(\"''\")", 0); + toUInt16.insert("engine->evaluate(\"'0'\")", 0); + toUInt16.insert("engine->evaluate(\"'123'\")", 123); + toUInt16.insert("engine->evaluate(\"'12.4'\")", 12); + toUInt16.insert("engine->nullValue()", 0); + toUInt16.insert("engine->undefinedValue()", 0); } newRow(expr) << toUInt16.value(expr); } @@ -1445,6 +1916,7 @@ void tst_QScriptValue::toUInt16_test(const char*, const QScriptValue& value) { QFETCH(quint16, expected); QCOMPARE(value.toUInt16(), expected); + QCOMPARE(value.toUInt16(), expected); } DEFINE_TEST_FUNCTION(toUInt16) diff --git a/JavaScriptCore/qt/tests/tests.pri b/JavaScriptCore/qt/tests/tests.pri index 1ce238f..5af3383 100644 --- a/JavaScriptCore/qt/tests/tests.pri +++ b/JavaScriptCore/qt/tests/tests.pri @@ -1,12 +1,3 @@ -isEmpty(OUTPUT_DIR) { - CONFIG(debug, debug|release) { - OUTPUT_DIR=$$PWD/WebKitBuild/Debug - } else { # Release - OUTPUT_DIR=$$PWD/WebKitBuild/Release - } -} - - QMAKE_RPATHDIR = $$OUTPUT_DIR/lib $$QMAKE_RPATHDIR QMAKE_LIBDIR = $$OUTPUT_DIR/lib $$QMAKE_LIBDIR mac:!static:contains(QT_CONFIG, qt_framework):!CONFIG(webkit_no_framework) { diff --git a/JavaScriptCore/qt/tests/tests.pro b/JavaScriptCore/qt/tests/tests.pro index 6e5edb1..7c3f590 100644 --- a/JavaScriptCore/qt/tests/tests.pro +++ b/JavaScriptCore/qt/tests/tests.pro @@ -1,3 +1,4 @@ TEMPLATE = subdirs SUBDIRS = qscriptengine \ - qscriptvalue + qscriptvalue \ + qscriptstring |