]> git.saurik.com Git - apple/javascriptcore.git/blob - qt/api/qscriptconverter_p.h
cd86e2074c7c2930001d7d728ea423d9895476c3
[apple/javascriptcore.git] / qt / api / qscriptconverter_p.h
1 /*
2 Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18 */
19
20 #ifndef qscriptconverter_p_h
21 #define qscriptconverter_p_h
22
23 #include <JavaScriptCore/JavaScript.h>
24 #include <QtCore/qnumeric.h>
25 #include <QtCore/qstring.h>
26 #include <QtCore/qvarlengtharray.h>
27
28 extern char *qdtoa(double d, int mode, int ndigits, int *decpt, int *sign, char **rve, char **digits_str);
29
30 /*
31 \internal
32 \class QScriptConverter
33 QScriptValue and QScriptEngine helper class. This class's responsibility is to convert values
34 between JS values and Qt/C++ values.
35
36 This is a nice way to inline these functions in both QScriptValue and QScriptEngine.
37 */
38 class QScriptConverter {
39 public:
40 static quint32 toArrayIndex(const JSStringRef jsstring)
41 {
42 // FIXME this function should be exported by JSC C API.
43 QString qstring = toString(jsstring);
44
45 bool ok;
46 quint32 idx = qstring.toUInt(&ok);
47 if (!ok || toString(idx) != qstring)
48 idx = 0xffffffff;
49
50 return idx;
51 }
52
53 static QString toString(const JSStringRef str)
54 {
55 return QString(reinterpret_cast<const QChar*>(JSStringGetCharactersPtr(str)), JSStringGetLength(str));
56 }
57 static JSStringRef toString(const QString& str)
58 {
59 return JSStringCreateWithUTF8CString(str.toUtf8().constData());
60 }
61 static JSStringRef toString(const char* str)
62 {
63 return JSStringCreateWithUTF8CString(str);
64 }
65 static QString toString(double value)
66 {
67 // FIXME this should be easier. The ideal fix is to create
68 // a new function in JSC C API which could cover the functionality.
69
70 if (qIsNaN(value))
71 return QString::fromLatin1("NaN");
72 if (qIsInf(value))
73 return QString::fromLatin1(value < 0 ? "-Infinity" : "Infinity");
74 if (!value)
75 return QString::fromLatin1("0");
76
77 QVarLengthArray<char, 25> buf;
78 int decpt;
79 int sign;
80 char* result = 0;
81 char* endresult;
82 (void)qdtoa(value, 0, 0, &decpt, &sign, &endresult, &result);
83
84 if (!result)
85 return QString();
86
87 int resultLen = endresult - result;
88 if (decpt <= 0 && decpt > -6) {
89 buf.resize(-decpt + 2 + sign);
90 qMemSet(buf.data(), '0', -decpt + 2 + sign);
91 if (sign) // fix the sign.
92 buf[0] = '-';
93 buf[sign + 1] = '.';
94 buf.append(result, resultLen);
95 } else {
96 if (sign)
97 buf.append('-');
98 int length = buf.size() - sign + resultLen;
99 if (decpt <= 21 && decpt > 0) {
100 if (length <= decpt) {
101 const char* zeros = "0000000000000000000000000";
102 buf.append(result, resultLen);
103 buf.append(zeros, decpt - length);
104 } else {
105 buf.append(result, decpt);
106 buf.append('.');
107 buf.append(result + decpt, resultLen - decpt);
108 }
109 } else if (result[0] >= '0' && result[0] <= '9') {
110 if (length > 1) {
111 buf.append(result, 1);
112 buf.append('.');
113 buf.append(result + 1, resultLen - 1);
114 } else
115 buf.append(result, resultLen);
116 buf.append('e');
117 buf.append(decpt >= 0 ? '+' : '-');
118 int e = qAbs(decpt - 1);
119 if (e >= 100)
120 buf.append('0' + e / 100);
121 if (e >= 10)
122 buf.append('0' + (e % 100) / 10);
123 buf.append('0' + e % 10);
124 }
125 }
126 free(result);
127 buf.append(0);
128 return QString::fromLatin1(buf.constData());
129 }
130 };
131
132 #endif // qscriptconverter_p_h