source: branches/4.5.1/src/script/qscriptecmaglobal.cpp@ 921

Last change on this file since 921 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 19.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtScript module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42// for strtoll
43#include <qplatformdefs.h>
44
45#ifndef QT_NO_SCRIPT
46
47#include "qscriptecmaglobal_p.h"
48#include "qscriptengine_p.h"
49#include "qscriptvalueimpl_p.h"
50#include "qscriptcontext_p.h"
51#include "qscriptmember_p.h"
52#include "qscriptobject_p.h"
53
54#include <QtCore/QVarLengthArray>
55#include <QtCore/qnumeric.h>
56
57QT_BEGIN_NAMESPACE
58
59extern double qstrtod(const char *s00, char const **se, bool *ok);
60
61namespace QScript {
62
63extern qsreal integerFromString(const QString &str, int radix);
64
65static inline char toHex(char c)
66{
67 static const char hexnumbers[] = "0123456789ABCDEF";
68 return hexnumbers[c & 0xf];
69}
70
71static int fromHex(char c)
72{
73 if ((c >= '0') && (c <= '9'))
74 return c - '0';
75 if ((c >= 'A') && (c <= 'F'))
76 return c - 'A' + 10;
77 if ((c >= 'a') && (c <= 'f'))
78 return c - 'a' + 10;
79 return -1;
80}
81
82static QByteArray escape(const QString &input)
83{
84 QVarLengthArray<char> output;
85 output.reserve(input.size() * 3);
86 const int length = input.length();
87 for (int i = 0; i < length; ++i) {
88 ushort uc = input.at(i).unicode();
89 if (uc < 0x100) {
90 if ( (uc > 0x60 && uc < 0x7B)
91 || (uc > 0x3F && uc < 0x5B)
92 || (uc > 0x2C && uc < 0x3A)
93 || (uc == 0x2A)
94 || (uc == 0x2B)
95 || (uc == 0x5F)) {
96 output.append(char(uc));
97 } else {
98 output.append('%');
99 output.append(toHex(uc >> 4));
100 output.append(toHex(uc));
101 }
102 } else {
103 output.append('%');
104 output.append('u');
105 output.append(toHex(uc >> 12));
106 output.append(toHex(uc >> 8));
107 output.append(toHex(uc >> 4));
108 output.append(toHex(uc));
109 }
110 }
111 return QByteArray(output.constData(), output.size());
112}
113
114static QString unescape(const QByteArray &input)
115{
116 QString result;
117 int i = 0;
118 const int length = input.length();
119 while (i < length) {
120 char c = input.at(i++);
121 if ((c == '%') && (i + 1 < length)) {
122 char a = input.at(i);
123 if ((a == 'u') && (i + 4 < length)) {
124 int d3 = fromHex(input.at(i+1));
125 int d2 = fromHex(input.at(i+2));
126 int d1 = fromHex(input.at(i+3));
127 int d0 = fromHex(input.at(i+4));
128 if ((d3 != -1) && (d2 != -1) && (d1 != -1) && (d0 != -1)) {
129 ushort uc = ushort((d3 << 12) | (d2 << 8) | (d1 << 4) | d0);
130 result.append(QChar(uc));
131 i += 5;
132 } else {
133 result.append(QLatin1Char(c));
134 }
135 } else {
136 int d1 = fromHex(a);
137 int d0 = fromHex(input.at(i+1));
138 if ((d1 != -1) && (d0 != -1)) {
139 c = (d1 << 4) | d0;
140 i += 2;
141 }
142 result.append(QLatin1Char(c));
143 }
144 } else {
145 result.append(QLatin1Char(c));
146 }
147 }
148 return result;
149}
150
151static const char uriReserved[] = ";/?:@&=+$,";
152static const char uriUnescaped[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()";
153
154static QString encode(const QString &input, const QString &unescapedSet, bool *ok)
155{
156 QString output;
157 const int length = input.length();
158 int i = 0;
159 while (i < length) {
160 const QChar c = input.at(i);
161 if (!unescapedSet.contains(c)) {
162 ushort uc = c.unicode();
163 if ((uc >= 0xDC00) && (uc <= 0xDFFF)) {
164 // URIError
165 break;
166 }
167 if (!((uc < 0xD800) || (uc > 0xDBFF))) {
168 ++i;
169 if (i == length) {
170 // URIError
171 break;
172 }
173 const ushort uc2 = input.at(i).unicode();
174 if ((uc < 0xDC00) || (uc > 0xDFFF)) {
175 // URIError
176 break;
177 }
178 uc = ((uc - 0xD800) * 0x400) + (uc2 - 0xDC00) + 0x10000;
179 }
180 QString tmp(1, QChar(uc));
181 QByteArray octets = tmp.toUtf8();
182 for (int j = 0; j < octets.length(); ++j) {
183 output.append(QLatin1Char('%'));
184 output.append(QLatin1Char(toHex(octets.at(j) >> 4)));
185 output.append(QLatin1Char(toHex(octets.at(j))));
186 }
187 } else {
188 output.append(c);
189 }
190 ++i;
191 }
192 *ok = (i == length);
193 return output;
194}
195
196static QString decode(const QString &input, const QString &reservedSet, bool *ok)
197{
198 QString output;
199 const int length = input.length();
200 int i = 0;
201 const QChar percent = QLatin1Char('%');
202 while (i < length) {
203 const QChar c = input.at(i);
204 if (c == percent) {
205 int start = i;
206 if (i + 2 >= length) {
207 // URIError
208 break;
209 }
210 int d1 = fromHex(input.at(i+1).toLatin1());
211 int d0 = fromHex(input.at(i+2).toLatin1());
212 if ((d1 == -1) || (d0 == -1)) {
213 // URIError
214 break;
215 }
216 int b = (d1 << 4) | d0;
217 i += 2;
218 if (b & 0x80) {
219 int n = -1;
220 while ((b << ++n) & 0x80) ;
221 if ((n == 1) || (n > 4)) {
222 // URIError
223 break;
224 }
225 QByteArray octets;
226 octets.append(b);
227 if (i + (3 * (n - 1)) >= length) {
228 // URIError
229 break;
230 }
231 for (int j = 1; j < n; ++j) {
232 ++i;
233 if (input.at(i) != percent) {
234 // URIError
235 break;
236 }
237 d1 = fromHex(input.at(i+1).toLatin1());
238 d0 = fromHex(input.at(i+2).toLatin1());
239 if ((d1 == -1) || (d0 == -1)) {
240 // URIError
241 break;
242 }
243 b = (d1 << 4) | d0;
244 if ((b & 0xC0) != 0x80) {
245 // URIError
246 break;
247 }
248 i += 2;
249 octets.append(b);
250 }
251 QString tmp = QString::fromUtf8(octets);
252 Q_ASSERT(tmp.length() == 1);
253 uint v = tmp.at(0).unicode(); // ### need 32-bit value
254 if (v < 0x10000) {
255 QChar z = QChar(ushort(v));
256 if (!reservedSet.contains(z)) {
257 output.append(z);
258 } else {
259 output.append(input.mid(start, i - start + 1));
260 }
261 } else {
262 if (v > 0x10FFFF) {
263 // URIError
264 break;
265 }
266 ushort l = ushort(((v - 0x10000) & 0x3FF) + 0xDC00);
267 ushort h = ushort((((v - 0x10000) >> 10) & 0x3FF) + 0xD800);
268 output.append(QChar(l));
269 output.append(QChar(h));
270 }
271 } else {
272 output.append(ushort(b));
273 }
274 } else {
275 output.append(c);
276 }
277 ++i;
278 }
279 *ok = (i == length);
280 return output;
281}
282
283class PrintFunction : public QScriptFunction
284{
285public:
286 PrintFunction() {}
287
288 virtual ~PrintFunction() {}
289
290 virtual void execute(QScriptContextPrivate *context)
291 {
292 QScriptEnginePrivate *eng = context->engine();
293#ifndef Q_SCRIPT_NO_EVENT_NOTIFY
294 eng->notifyFunctionEntry(context);
295#endif
296 QString result;
297 for (int i = 0; i < context->argumentCount(); ++i) {
298 if (i != 0)
299 result.append(QLatin1String(" "));
300
301 QString s = context->argument(i).toString();
302 if (context->state() == QScriptContext::ExceptionState)
303 break;
304 result.append(s);
305 }
306
307 if (context->state() != QScriptContext::ExceptionState) {
308 QTextStream qout(stdout, QIODevice::WriteOnly);
309 qout << result << endl;
310 context->setReturnValue(eng->undefinedValue());
311 }
312#ifndef Q_SCRIPT_NO_EVENT_NOTIFY
313 eng->notifyFunctionExit(context);
314#endif
315 }
316
317 QString functionName() const
318 {
319 return QLatin1String("print");
320 }
321};
322
323} // anonumous
324
325namespace QScript { namespace Ecma {
326
327Global::Global(QScriptEnginePrivate *engine, QScriptClassInfo *classInfo)
328 : m_engine(engine), m_classInfo(classInfo)
329{
330}
331
332Global::~Global()
333{
334}
335
336void Global::construct(QScriptValueImpl *object, QScriptEnginePrivate *eng)
337{
338 QScriptClassInfo *classInfo = eng->registerClass(QLatin1String("global"),
339 QScriptClassInfo::ActivationType);
340
341 // create with prototype null, since Object.prototype doesn't exist at this point
342 eng->newObject(object, eng->nullValue(), classInfo);
343
344 Global *instance = new Global(eng, classInfo);
345 object->setObjectData(instance);
346}
347
348void Global::initialize(QScriptValueImpl *object, QScriptEnginePrivate *eng)
349{
350 // set the real prototype
351 object->setPrototype(eng->objectConstructor->publicPrototype);
352
353 const QScriptValue::PropertyFlags flags = QScriptValue::Undeletable
354 | QScriptValue::SkipInEnumeration;
355
356 object->setProperty(QLatin1String("NaN"), QScriptValueImpl(qSNaN()), flags);
357 object->setProperty(QLatin1String("Infinity"), QScriptValueImpl(qInf()), flags);
358 object->setProperty(QLatin1String("undefined"), eng->undefinedValue(), flags);
359
360 object->setProperty(QLatin1String("print"),
361 eng->createFunction(new PrintFunction()), flags);
362 addFunction(*object, QLatin1String("parseInt"), method_parseInt, 2, flags);
363 addFunction(*object, QLatin1String("parseFloat"), method_parseFloat, 1, flags);
364 addFunction(*object, QLatin1String("isNaN"), method_isNaN, 1, flags);
365 addFunction(*object, QLatin1String("isFinite"), method_isFinite, 1, flags);
366 addFunction(*object, QLatin1String("decodeURI"), method_decodeURI, 1, flags);
367 addFunction(*object, QLatin1String("decodeURIComponent"), method_decodeURIComponent, 1, flags);
368 addFunction(*object, QLatin1String("encodeURI"), method_encodeURI, 1, flags);
369 addFunction(*object, QLatin1String("encodeURIComponent"), method_encodeURIComponent, 1, flags);
370 addFunction(*object, QLatin1String("escape"), method_escape, 1, flags);
371 addFunction(*object, QLatin1String("unescape"), method_unescape, 1, flags);
372 addFunction(*object, QLatin1String("version"), method_version, 0, flags);
373 addFunction(*object, QLatin1String("gc"), method_gc, 0, flags);
374}
375
376QScriptValueImpl Global::method_parseInt(QScriptContextPrivate *context,
377 QScriptEnginePrivate *,
378 QScriptClassInfo *)
379{
380 if (context->argumentCount() == 0)
381 return qSNaN();
382
383 qsreal radix = 0;
384 if (context->argumentCount() > 1) {
385 radix = context->argument(1).toInteger();
386 if (qIsNaN(radix) || (radix && (radix < 2 || radix > 36))) {
387 return qSNaN();
388 }
389 }
390
391 return QScript::integerFromString(context->argument(0).toString(), static_cast<int>(radix));
392}
393
394QScriptValueImpl Global::method_parseFloat(QScriptContextPrivate *context,
395 QScriptEnginePrivate *,
396 QScriptClassInfo *)
397{
398 if (context->argumentCount() == 0)
399 return QScriptValueImpl(qSNaN());
400 QString str = context->argument(0).toString().trimmed();
401 QByteArray latin1 = str.toLatin1();
402 const char *data = latin1.constData();
403 const char *eptr = 0;
404 qsreal result = qstrtod(data, &eptr, 0);
405 if (eptr == data) {
406 if (str == QLatin1String("Infinity"))
407 result = +qInf();
408 else if (str == QLatin1String("+Infinity"))
409 result = +qInf();
410 else if (str == QLatin1String("-Infinity"))
411 result = -qInf();
412 else
413 result = qSNaN();
414 }
415 return result;
416}
417
418QScriptValueImpl Global::method_isNaN(QScriptContextPrivate *context,
419 QScriptEnginePrivate *,
420 QScriptClassInfo *)
421{
422 qsreal v = qSNaN();
423 if (context->argumentCount() > 0)
424 v = context->argument(0).toNumber();
425 return (QScriptValueImpl(qIsNaN(v)));
426}
427
428QScriptValueImpl Global::method_isFinite(QScriptContextPrivate *context,
429 QScriptEnginePrivate *,
430 QScriptClassInfo *)
431{
432 qsreal v = qInf();
433 if (context->argumentCount() > 0)
434 v = context->argument(0).toNumber();
435 return (QScriptValueImpl(qIsFinite(v)));
436}
437
438QScriptValueImpl Global::method_decodeURI(QScriptContextPrivate *context,
439 QScriptEnginePrivate *eng,
440 QScriptClassInfo *)
441{
442 QScriptValueImpl result;
443
444 if (context->argumentCount() > 0) {
445 QString str = context->argument(0).toString();
446 bool ok;
447 QString out = decode(str, QString::fromUtf8(uriReserved) + QString::fromUtf8("#"), &ok);
448 if (ok)
449 return QScriptValueImpl(eng, out);
450 else
451 return context->throwError(QScriptContext::URIError,
452 QLatin1String("malformed URI sequence"));
453 }
454
455 return eng->undefinedValue();
456}
457
458QScriptValueImpl Global::method_decodeURIComponent(QScriptContextPrivate *context,
459 QScriptEnginePrivate *eng,
460 QScriptClassInfo *)
461{
462 QScriptValueImpl result;
463 if (context->argumentCount() > 0) {
464 QString str = context->argument(0).toString();
465 bool ok;
466 QString out = decode(str, QString::fromUtf8(""), &ok);
467 if (ok)
468 result = QScriptValueImpl(eng, out);
469 else
470 result = context->throwError(QScriptContext::URIError,
471 QLatin1String("malformed URI sequence"));
472 } else {
473 result = eng->undefinedValue();
474 }
475 return result;
476}
477
478QScriptValueImpl Global::method_encodeURI(QScriptContextPrivate *context,
479 QScriptEnginePrivate *eng,
480 QScriptClassInfo *)
481{
482 QScriptValueImpl result;
483 if (context->argumentCount() > 0) {
484 QString str = context->argument(0).toString();
485 bool ok;
486 QString out = encode(str,
487 QLatin1String(uriReserved)
488 + QLatin1String(uriUnescaped)
489 + QString::fromUtf8("#"),
490 &ok);
491 if (ok)
492 result = QScriptValueImpl(eng, out);
493 else
494 result = context->throwError(QScriptContext::URIError,
495 QLatin1String("malformed URI sequence"));
496 } else {
497 result = eng->undefinedValue();
498 }
499 return result;
500}
501
502QScriptValueImpl Global::method_encodeURIComponent(QScriptContextPrivate *context,
503 QScriptEnginePrivate *eng,
504 QScriptClassInfo *)
505{
506 QScriptValueImpl result;
507 if (context->argumentCount() > 0) {
508 QString str = context->argument(0).toString();
509 bool ok;
510 QString out = encode(str, QLatin1String(uriUnescaped), &ok);
511 if (ok)
512 result = QScriptValueImpl(eng, out);
513 else
514 result = context->throwError(QScriptContext::URIError,
515 QLatin1String("malformed URI sequence"));
516 } else {
517 result = eng->undefinedValue();
518 }
519 return result;
520}
521
522QScriptValueImpl Global::method_escape(QScriptContextPrivate *context,
523 QScriptEnginePrivate *eng,
524 QScriptClassInfo *)
525{
526 if (context->argumentCount() > 0) {
527 QString str = context->argument(0).toString();
528 return QScriptValueImpl(eng, QLatin1String(escape(str)));
529 }
530 return QScriptValueImpl(eng, QLatin1String("undefined"));
531}
532
533QScriptValueImpl Global::method_unescape(QScriptContextPrivate *context,
534 QScriptEnginePrivate *eng,
535 QScriptClassInfo *)
536{
537 if (context->argumentCount() > 0) {
538 QByteArray data = context->argument(0).toString().toLatin1();
539 return QScriptValueImpl(eng, unescape(data));
540 }
541 return QScriptValueImpl(eng, QLatin1String("undefined"));
542}
543
544QScriptValueImpl Global::method_version(QScriptContextPrivate *,
545 QScriptEnginePrivate *,
546 QScriptClassInfo *)
547{
548 return (QScriptValueImpl(1));
549}
550
551QScriptValueImpl Global::method_gc(QScriptContextPrivate *,
552 QScriptEnginePrivate *eng,
553 QScriptClassInfo *)
554{
555 eng->gc();
556 return QScriptValueImpl(eng->objectAllocator.freeBlocks());
557}
558
559void Global::addFunction(QScriptValueImpl &object, const QString &name,
560 QScriptInternalFunctionSignature fun, int length,
561 const QScriptValue::PropertyFlags flags)
562{
563 QScriptEnginePrivate *eng_p = object.engine();
564 QScriptValueImpl val = eng_p->createFunction(fun, length, object.classInfo(), name);
565 object.setProperty(name, val, flags);
566}
567
568} } // namespace QScript::Ecma
569
570QT_END_NAMESPACE
571
572#endif // QT_NO_SCRIPT
Note: See TracBrowser for help on using the repository browser.