1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qjsconverter_p.h"
45 #ifndef QJSCONVERTER_IMPL_P_H
46 #define QJSCONVERTER_IMPL_P_H
54 extern char *qdtoa(double d, int mode, int ndigits, int *decpt, int *sign, char **rve, char **digits_str);
55 Q_CORE_EXPORT QString qt_regexp_toCanonical(const QString &, QRegExp::PatternSyntax);
58 quint32 QJSConverter::toArrayIndex(const QString& string)
60 // FIXME this function should be exported by JSC C API.
62 quint32 idx = string.toUInt(&ok);
63 if (!ok || toString(idx) != string)
69 QString QJSConverter::toString(v8::Handle<v8::String> jsString)
71 if (jsString.IsEmpty())
74 qstr.resize(jsString->Length());
75 jsString->Write(reinterpret_cast<uint16_t*>(qstr.data()));
79 v8::Local<v8::String> QJSConverter::toString(const QString& string)
81 return v8::String::New(reinterpret_cast<const uint16_t*>(string.data()), string.size());
84 QString QJSConverter::toString(double value)
86 // FIXME this should be easier. The ideal fix is to create
87 // a new function in V8 API which could cover the functionality.
90 return QString::fromLatin1("NaN");
92 return QString::fromLatin1(value < 0 ? "-Infinity" : "Infinity");
94 return QString::fromLatin1("0");
96 QVarLengthArray<char, 25> buf;
101 (void)qdtoa(value, 0, 0, &decpt, &sign, &endresult, &result);
106 int resultLen = endresult - result;
107 if (decpt <= 0 && decpt > -6) {
108 buf.resize(-decpt + 2 + sign);
109 memset(buf.data(), '0', -decpt + 2 + sign);
110 if (sign) // fix the sign.
113 buf.append(result, resultLen);
117 int length = buf.size() - sign + resultLen;
118 if (decpt <= 21 && decpt > 0) {
119 if (length <= decpt) {
120 const char* zeros = "0000000000000000000000000";
121 buf.append(result, resultLen);
122 buf.append(zeros, decpt - length);
124 buf.append(result, decpt);
126 buf.append(result + decpt, resultLen - decpt);
128 } else if (result[0] >= '0' && result[0] <= '9') {
130 buf.append(result, 1);
132 buf.append(result + 1, resultLen - 1);
134 buf.append(result, resultLen);
136 buf.append(decpt >= 0 ? '+' : '-');
137 int e = qAbs(decpt - 1);
139 buf.append('0' + e / 100);
141 buf.append('0' + (e % 100) / 10);
142 buf.append('0' + e % 10);
147 return QString::fromLatin1(buf.constData());
150 // return a mask of v8::PropertyAttribute that may also contains QScriptValue::PropertyGetter or QScriptValue::PropertySetter
151 uint QJSConverter::toPropertyAttributes(const QFlags<QJSValuePrivate::PropertyFlag>& flags)
154 if (flags.testFlag(QJSValuePrivate::ReadOnly))
155 attr |= v8::ReadOnly;
156 if (flags.testFlag(QJSValuePrivate::Undeletable))
157 attr |= v8::DontDelete;
158 if (flags.testFlag(QJSValuePrivate::SkipInEnumeration))
159 attr |= v8::DontEnum;
160 // if (flags.testFlag(QScriptValue::PropertyGetter))
161 // attr |= QScriptValue::PropertyGetter;
162 // if (flags.testFlag(QScriptValue::PropertySetter))
163 // attr |= QScriptValue::PropertySetter;
167 // Converts a JS RegExp to a QRegExp.
168 // The conversion is not 100% exact since ECMA regexp and QRegExp
169 // have different semantics/flags, but we try to do our best.
170 QRegExp QJSConverter::toRegExp(v8::Handle<v8::RegExp> jsRegExp)
172 QString pattern = QJSConverter::toString(jsRegExp->GetSource());
173 Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive;
174 if (jsRegExp->GetFlags() & v8::RegExp::kIgnoreCase)
175 caseSensitivity = Qt::CaseInsensitive;
176 return QRegExp(pattern, caseSensitivity, QRegExp::RegExp2);
179 // Converts a QRegExp to a JS RegExp.
180 // The conversion is not 100% exact since ECMA regexp and QRegExp
181 // have different semantics/flags, but we try to do our best.
182 v8::Local<v8::RegExp> QJSConverter::toRegExp(const QRegExp &re)
184 // Convert the pattern to a ECMAScript pattern.
185 QString pattern = qt_regexp_toCanonical(re.pattern(), re.patternSyntax());
186 if (re.isMinimal()) {
188 int len = pattern.length();
189 ecmaPattern.reserve(len);
191 const QChar *wc = pattern.unicode();
192 bool inBracket = false;
196 switch (c.unicode()) {
202 ecmaPattern += QLatin1Char('?');
206 ecmaPattern += wc[i++];
218 pattern = ecmaPattern;
221 int flags = v8::RegExp::kNone;
222 if (re.caseSensitivity() == Qt::CaseInsensitive)
223 flags |= v8::RegExp::kIgnoreCase;
225 return v8::RegExp::New(QJSConverter::toString(pattern), static_cast<v8::RegExp::Flags>(flags));
228 // Converts a QStringList to JS.
229 // The result is a new Array object with length equal to the length
230 // of the QStringList, and the elements being the QStringList's
231 // elements converted to JS Strings.
232 v8::Local<v8::Array> QJSConverter::toStringList(const QStringList &lst)
234 v8::Local<v8::Array> result = v8::Array::New(lst.size());
235 for (int i = 0; i < lst.size(); ++i)
236 result->Set(i, toString(lst.at(i)));
240 // Converts a JS Array object to a QStringList.
241 // The result is a QStringList with length equal to the length
242 // of the JS Array, and elements being the JS Array's elements
243 // converted to QStrings.
244 QStringList QJSConverter::toStringList(v8::Handle<v8::Array> jsArray)
247 uint32_t length = jsArray->Length();
248 for (uint32_t i = 0; i < length; ++i)
249 result.append(toString(jsArray->Get(i)->ToString()));
254 // Converts a JS Date to a QDateTime.
255 QDateTime QJSConverter::toDateTime(v8::Handle<v8::Date> jsDate)
257 return QDateTime::fromMSecsSinceEpoch(jsDate->NumberValue());
260 // Converts a QDateTime to a JS Date.
261 v8::Local<v8::Value> QJSConverter::toDateTime(const QDateTime &dt)
267 date = dt.toMSecsSinceEpoch();
268 return v8::Date::New(date);
273 #endif // QJSCONVERTER_IMPL_P_H