]>
git.saurik.com Git - apple/javascriptcore.git/blob - qt/api/qscriptconverter_p.h
cd86e2074c7c2930001d7d728ea423d9895476c3
2 Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
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.
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.
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.
20 #ifndef qscriptconverter_p_h
21 #define qscriptconverter_p_h
23 #include <JavaScriptCore/JavaScript.h>
24 #include <QtCore/qnumeric.h>
25 #include <QtCore/qstring.h>
26 #include <QtCore/qvarlengtharray.h>
28 extern char *qdtoa(double d
, int mode
, int ndigits
, int *decpt
, int *sign
, char **rve
, char **digits_str
);
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.
36 This is a nice way to inline these functions in both QScriptValue and QScriptEngine.
38 class QScriptConverter
{
40 static quint32
toArrayIndex(const JSStringRef jsstring
)
42 // FIXME this function should be exported by JSC C API.
43 QString qstring
= toString(jsstring
);
46 quint32 idx
= qstring
.toUInt(&ok
);
47 if (!ok
|| toString(idx
) != qstring
)
53 static QString
toString(const JSStringRef str
)
55 return QString(reinterpret_cast<const QChar
*>(JSStringGetCharactersPtr(str
)), JSStringGetLength(str
));
57 static JSStringRef
toString(const QString
& str
)
59 return JSStringCreateWithUTF8CString(str
.toUtf8().constData());
61 static JSStringRef
toString(const char* str
)
63 return JSStringCreateWithUTF8CString(str
);
65 static QString
toString(double value
)
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.
71 return QString::fromLatin1("NaN");
73 return QString::fromLatin1(value
< 0 ? "-Infinity" : "Infinity");
75 return QString::fromLatin1("0");
77 QVarLengthArray
<char, 25> buf
;
82 (void)qdtoa(value
, 0, 0, &decpt
, &sign
, &endresult
, &result
);
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.
94 buf
.append(result
, resultLen
);
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
);
105 buf
.append(result
, decpt
);
107 buf
.append(result
+ decpt
, resultLen
- decpt
);
109 } else if (result
[0] >= '0' && result
[0] <= '9') {
111 buf
.append(result
, 1);
113 buf
.append(result
+ 1, resultLen
- 1);
115 buf
.append(result
, resultLen
);
117 buf
.append(decpt
>= 0 ? '+' : '-');
118 int e
= qAbs(decpt
- 1);
120 buf
.append('0' + e
/ 100);
122 buf
.append('0' + (e
% 100) / 10);
123 buf
.append('0' + e
% 10);
128 return QString::fromLatin1(buf
.constData());
132 #endif // qscriptconverter_p_h