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 QtDeclarative 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"
44 #ifndef QJSCONVERTER_IMPL_P_H
45 #define QJSCONVERTER_IMPL_P_H
49 extern char *qdtoa(double d, int mode, int ndigits, int *decpt, int *sign, char **rve, char **digits_str);
50 Q_CORE_EXPORT QString qt_regexp_toCanonical(const QString &, QRegExp::PatternSyntax);
53 quint32 QJSConverter::toArrayIndex(const QString& string)
55 // FIXME this function should be exported by JSC C API.
57 quint32 idx = string.toUInt(&ok);
58 if (!ok || toString(idx) != string)
64 QString QJSConverter::toString(v8::Handle<v8::String> jsString)
66 if (jsString.IsEmpty())
69 qstr.resize(jsString->Length());
70 jsString->Write(reinterpret_cast<uint16_t*>(qstr.data()));
74 v8::Local<v8::String> QJSConverter::toString(const QString& string)
76 return v8::String::New(reinterpret_cast<const uint16_t*>(string.data()), string.size());
79 QString QJSConverter::toString(double value)
81 // FIXME this should be easier. The ideal fix is to create
82 // a new function in V8 API which could cover the functionality.
85 return QString::fromLatin1("NaN");
87 return QString::fromLatin1(value < 0 ? "-Infinity" : "Infinity");
89 return QString::fromLatin1("0");
91 QVarLengthArray<char, 25> buf;
96 (void)qdtoa(value, 0, 0, &decpt, &sign, &endresult, &result);
101 int resultLen = endresult - result;
102 if (decpt <= 0 && decpt > -6) {
103 buf.resize(-decpt + 2 + sign);
104 qMemSet(buf.data(), '0', -decpt + 2 + sign);
105 if (sign) // fix the sign.
108 buf.append(result, resultLen);
112 int length = buf.size() - sign + resultLen;
113 if (decpt <= 21 && decpt > 0) {
114 if (length <= decpt) {
115 const char* zeros = "0000000000000000000000000";
116 buf.append(result, resultLen);
117 buf.append(zeros, decpt - length);
119 buf.append(result, decpt);
121 buf.append(result + decpt, resultLen - decpt);
123 } else if (result[0] >= '0' && result[0] <= '9') {
125 buf.append(result, 1);
127 buf.append(result + 1, resultLen - 1);
129 buf.append(result, resultLen);
131 buf.append(decpt >= 0 ? '+' : '-');
132 int e = qAbs(decpt - 1);
134 buf.append('0' + e / 100);
136 buf.append('0' + (e % 100) / 10);
137 buf.append('0' + e % 10);
142 return QString::fromLatin1(buf.constData());
145 // return a mask of v8::PropertyAttribute that may also contains QScriptValue::PropertyGetter or QScriptValue::PropertySetter
146 uint QJSConverter::toPropertyAttributes(const QFlags<QJSValue::PropertyFlag>& flags)
149 if (flags.testFlag(QJSValue::ReadOnly))
150 attr |= v8::ReadOnly;
151 if (flags.testFlag(QJSValue::Undeletable))
152 attr |= v8::DontDelete;
153 if (flags.testFlag(QJSValue::SkipInEnumeration))
154 attr |= v8::DontEnum;
155 // if (flags.testFlag(QScriptValue::PropertyGetter))
156 // attr |= QScriptValue::PropertyGetter;
157 // if (flags.testFlag(QScriptValue::PropertySetter))
158 // attr |= QScriptValue::PropertySetter;
162 // Converts a JS RegExp to a QRegExp.
163 // The conversion is not 100% exact since ECMA regexp and QRegExp
164 // have different semantics/flags, but we try to do our best.
165 QRegExp QJSConverter::toRegExp(v8::Handle<v8::RegExp> jsRegExp)
167 QString pattern = QJSConverter::toString(jsRegExp->GetSource());
168 Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive;
169 if (jsRegExp->GetFlags() & v8::RegExp::kIgnoreCase)
170 caseSensitivity = Qt::CaseInsensitive;
171 return QRegExp(pattern, caseSensitivity, QRegExp::RegExp2);
174 // Converts a QRegExp to a JS RegExp.
175 // The conversion is not 100% exact since ECMA regexp and QRegExp
176 // have different semantics/flags, but we try to do our best.
177 v8::Local<v8::RegExp> QJSConverter::toRegExp(const QRegExp &re)
179 // Convert the pattern to a ECMAScript pattern.
180 QString pattern = qt_regexp_toCanonical(re.pattern(), re.patternSyntax());
181 if (re.isMinimal()) {
183 int len = pattern.length();
184 ecmaPattern.reserve(len);
186 const QChar *wc = pattern.unicode();
187 bool inBracket = false;
191 switch (c.unicode()) {
197 ecmaPattern += QLatin1Char('?');
201 ecmaPattern += wc[i++];
213 pattern = ecmaPattern;
216 int flags = v8::RegExp::kNone;
217 if (re.caseSensitivity() == Qt::CaseInsensitive)
218 flags |= v8::RegExp::kIgnoreCase;
220 return v8::RegExp::New(QJSConverter::toString(pattern), static_cast<v8::RegExp::Flags>(flags));
223 // Converts a QStringList to JS.
224 // The result is a new Array object with length equal to the length
225 // of the QStringList, and the elements being the QStringList's
226 // elements converted to JS Strings.
227 v8::Local<v8::Array> QJSConverter::toStringList(const QStringList &lst)
229 v8::Local<v8::Array> result = v8::Array::New(lst.size());
230 for (int i = 0; i < lst.size(); ++i)
231 result->Set(i, toString(lst.at(i)));
235 // Converts a JS Array object to a QStringList.
236 // The result is a QStringList with length equal to the length
237 // of the JS Array, and elements being the JS Array's elements
238 // converted to QStrings.
239 QStringList QJSConverter::toStringList(v8::Handle<v8::Array> jsArray)
242 uint32_t length = jsArray->Length();
243 for (uint32_t i = 0; i < length; ++i)
244 result.append(toString(jsArray->Get(i)->ToString()));
249 // Converts a JS Date to a QDateTime.
250 QDateTime QJSConverter::toDateTime(v8::Handle<v8::Date> jsDate)
252 return QDateTime::fromMSecsSinceEpoch(jsDate->NumberValue());
255 // Converts a QDateTime to a JS Date.
256 v8::Local<v8::Value> QJSConverter::toDateTime(const QDateTime &dt)
262 date = dt.toMSecsSinceEpoch();
263 return v8::Date::New(date);
268 #endif // QJSCONVERTER_IMPL_P_H