Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / v8 / qjsconverter_impl_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qjsconverter_p.h"
43
44 #ifndef QJSCONVERTER_IMPL_P_H
45 #define QJSCONVERTER_IMPL_P_H
46
47 QT_BEGIN_NAMESPACE
48
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);
51
52
53 quint32 QJSConverter::toArrayIndex(const QString& string)
54 {
55     // FIXME this function should be exported by JSC C API.
56     bool ok;
57     quint32 idx = string.toUInt(&ok);
58     if (!ok || toString(idx) != string)
59         idx = 0xffffffff;
60
61     return idx;
62 }
63
64 QString QJSConverter::toString(v8::Handle<v8::String> jsString)
65 {
66     if (jsString.IsEmpty())
67         return QString();
68     QString qstr;
69     qstr.resize(jsString->Length());
70     jsString->Write(reinterpret_cast<uint16_t*>(qstr.data()));
71     return qstr;
72 }
73
74 v8::Local<v8::String> QJSConverter::toString(const QString& string)
75 {
76     return v8::String::New(reinterpret_cast<const uint16_t*>(string.data()), string.size());
77 }
78
79 QString QJSConverter::toString(double value)
80 {
81     // FIXME this should be easier. The ideal fix is to create
82     // a new function in V8 API which could cover the functionality.
83
84     if (qIsNaN(value))
85         return QString::fromLatin1("NaN");
86     if (qIsInf(value))
87         return QString::fromLatin1(value < 0 ? "-Infinity" : "Infinity");
88     if (!value)
89         return QString::fromLatin1("0");
90
91     QVarLengthArray<char, 25> buf;
92     int decpt;
93     int sign;
94     char* result = 0;
95     char* endresult;
96     (void)qdtoa(value, 0, 0, &decpt, &sign, &endresult, &result);
97
98     if (!result)
99         return QString();
100
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.
106             buf[0] = '-';
107         buf[sign + 1] = '.';
108         buf.append(result, resultLen);
109     } else {
110         if (sign)
111             buf.append('-');
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);
118             } else {
119                 buf.append(result, decpt);
120                 buf.append('.');
121                 buf.append(result + decpt, resultLen - decpt);
122             }
123         } else if (result[0] >= '0' && result[0] <= '9') {
124             if (length > 1) {
125                 buf.append(result, 1);
126                 buf.append('.');
127                 buf.append(result + 1, resultLen - 1);
128             } else
129                 buf.append(result, resultLen);
130             buf.append('e');
131             buf.append(decpt >= 0 ? '+' : '-');
132             int e = qAbs(decpt - 1);
133             if (e >= 100)
134                 buf.append('0' + e / 100);
135             if (e >= 10)
136                 buf.append('0' + (e % 100) / 10);
137             buf.append('0' + e % 10);
138         }
139     }
140     free(result);
141     buf.append(0);
142     return QString::fromLatin1(buf.constData());
143 }
144
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)
147 {
148     uint attr = 0;
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;
159     return attr;
160 }
161
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)
166 {
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);
172 }
173
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)
178 {
179     // Convert the pattern to a ECMAScript pattern.
180     QString pattern = qt_regexp_toCanonical(re.pattern(), re.patternSyntax());
181     if (re.isMinimal()) {
182         QString ecmaPattern;
183         int len = pattern.length();
184         ecmaPattern.reserve(len);
185         int i = 0;
186         const QChar *wc = pattern.unicode();
187         bool inBracket = false;
188         while (i < len) {
189             QChar c = wc[i++];
190             ecmaPattern += c;
191             switch (c.unicode()) {
192             case '?':
193             case '+':
194             case '*':
195             case '}':
196                 if (!inBracket)
197                     ecmaPattern += QLatin1Char('?');
198                 break;
199             case '\\':
200                 if (i < len)
201                     ecmaPattern += wc[i++];
202                 break;
203             case '[':
204                 inBracket = true;
205                 break;
206             case ']':
207                 inBracket = false;
208                 break;
209             default:
210                 break;
211             }
212         }
213         pattern = ecmaPattern;
214     }
215
216     int flags = v8::RegExp::kNone;
217     if (re.caseSensitivity() == Qt::CaseInsensitive)
218         flags |= v8::RegExp::kIgnoreCase;
219
220     return v8::RegExp::New(QJSConverter::toString(pattern), static_cast<v8::RegExp::Flags>(flags));
221 }
222
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)
228 {
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)));
232     return result;
233 }
234
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)
240 {
241     QStringList result;
242     uint32_t length = jsArray->Length();
243     for (uint32_t i = 0; i < length; ++i)
244         result.append(toString(jsArray->Get(i)->ToString()));
245     return result;
246 }
247
248
249 // Converts a JS Date to a QDateTime.
250 QDateTime QJSConverter::toDateTime(v8::Handle<v8::Date> jsDate)
251 {
252     return QDateTime::fromMSecsSinceEpoch(jsDate->NumberValue());
253 }
254
255 // Converts a QDateTime to a JS Date.
256 v8::Local<v8::Value> QJSConverter::toDateTime(const QDateTime &dt)
257 {
258     double date;
259     if (!dt.isValid())
260         date = qSNaN();
261     else
262         date = dt.toMSecsSinceEpoch();
263     return v8::Date::New(date);
264 }
265
266 QT_END_NAMESPACE
267
268 #endif // QJSCONVERTER_IMPL_P_H