31static QString
escape(
const QString &input)
34 output.reserve(input.size() * 3);
35 const int length = input.size();
36 for (
int i = 0; i < length; ++i) {
37 ushort uc = input.at(i).unicode();
39 if ( (uc > 0x60 && uc < 0x7B)
40 || (uc > 0x3F && uc < 0x5B)
41 || (uc > 0x2C && uc < 0x3A)
45 output.append(QChar(uc));
48 output.append(QLatin1Char(toHexUpper(uc >> 4)));
49 output.append(QLatin1Char(toHexUpper(uc)));
54 output.append(QLatin1Char(toHexUpper(uc >> 12)));
55 output.append(QLatin1Char(toHexUpper(uc >> 8)));
56 output.append(QLatin1Char(toHexUpper(uc >> 4)));
57 output.append(QLatin1Char(toHexUpper(uc)));
66 result.reserve(input.size());
68 const int length = input.size();
70 QChar c = input.at(i++);
71 if ((c == u'%') && (i + 1 < length)) {
72 QChar a = input.at(i);
73 if ((a == u'u') && (i + 4 < length)) {
74 int d3 = fromHex(input.at(i+1).unicode());
75 int d2 = fromHex(input.at(i+2).unicode());
76 int d1 = fromHex(input.at(i+3).unicode());
77 int d0 = fromHex(input.at(i+4).unicode());
78 if ((d3 != -1) && (d2 != -1) && (d1 != -1) && (d0 != -1)) {
79 ushort uc = ushort((d3 << 12) | (d2 << 8) | (d1 << 4) | d0);
80 result.append(QChar(uc));
86 int d1 = fromHex(a.unicode());
87 int d0 = fromHex(input.at(i+1).unicode());
88 if ((d1 != -1) && (d0 != -1)) {
89 c = QChar((d1 << 4) | d0);
112static QString
encode(
const QString &input,
const char *unescapedSet,
bool *ok)
116 const int length = input.size();
119 const QChar c = input.at(i);
121 if ((c.unicode() >=
'a' && c.unicode() <=
'z') ||
122 (c.unicode() >=
'A' && c.unicode() <=
'Z') ||
123 (c.unicode() >=
'0' && c.unicode() <=
'9')) {
126 const char *r = unescapedSet;
128 if (*r == c.unicode()) {
136 uint uc = c.unicode();
137 if ((uc >= 0xDC00) && (uc <= 0xDFFF)) {
141 if (!((uc < 0xD800) || (uc > 0xDBFF))) {
147 const uint uc2 = input.at(i).unicode();
148 if ((uc2 < 0xDC00) || (uc2 > 0xDFFF)) {
152 uc = ((uc - 0xD800) * 0x400) + (uc2 - 0xDC00) + 0x10000;
155 addEscapeSequence(output, (uchar)uc);
158 addEscapeSequence(output, 0xc0 | ((uchar) (uc >> 6)));
161 if (QChar::requiresSurrogates(uc)) {
162 addEscapeSequence(output, 0xf0 | ((uchar) (uc >> 18)));
163 addEscapeSequence(output, 0x80 | (((uchar) (uc >> 12)) & 0x3f));
165 addEscapeSequence(output, 0xe0 | (((uchar) (uc >> 12)) & 0x3f));
167 addEscapeSequence(output, 0x80 | (((uchar) (uc >> 6)) & 0x3f));
169 addEscapeSequence(output, 0x80 | ((uchar) (uc&0x3f)));
190 output.reserve(input.size());
191 const int length = input.size();
193 const QChar percent = QLatin1Char(
'%');
195 const QChar ch = input.at(i);
201 int d1 = fromHex(input.at(i+1).unicode());
202 int d0 = fromHex(input.at(i+2).unicode());
203 if ((d1 == -1) || (d0 == -1))
206 int b = (d1 << 4) | d0;
212 if ((b & 0xe0) == 0xc0) {
216 }
else if ((b & 0xf0) == 0xe0) {
220 }
else if ((b & 0xf8) == 0xf0) {
228 if (i + (3 * need) >= length)
231 for (
int j = 0; j < need; ++j) {
233 if (input.at(i) != percent)
236 d1 = fromHex(input.at(i+1).unicode());
237 d0 = fromHex(input.at(i+2).unicode());
238 if ((d1 == -1) || (d0 == -1))
242 if ((b & 0xC0) != 0x80)
246 uc = (uc << 6) + (b & 0x3f);
252 output.append(QChar(uc));
257 ushort l = ushort(((uc - 0x10000) & 0x3FF) + 0xDC00);
258 ushort h = ushort((((uc - 0x10000) >> 10) & 0x3FF) + 0xD800);
259 output.append(QChar(h));
260 output.append(QChar(l));
271 output.append(QStringView{input}.mid(start, i - start + 1));
273 output.append(QChar(b));
275 output.append(QChar(b));
293void Heap::EvalFunction::init(QV4::ExecutionEngine *engine)
296 Heap::FunctionObject::init(engine, s.engine->id_eval());
297 ScopedFunctionObject f(s,
this);
298 f->defineReadonlyConfigurableProperty(s.engine->id_length(), Value::fromInt32(1));
301ReturnedValue EvalFunction::evalCall(
const Value *,
const Value *argv,
int argc,
bool directCall)
const
304 return Encode::undefined();
306 ExecutionEngine *v4 = engine();
307 bool isStrict = v4->currentStackFrame->v4Function->isStrict();
310 ScopedContext ctx(scope, v4->currentContext());
314 ctx = v4->scriptContext();
317 String *scode = argv[0].stringValue();
319 return argv[0].asReturnedValue();
321 const QString code = scode->toQString();
322 bool inheritContext = !isStrict;
324 Script script(ctx, QV4::Compiler::ContextType::Eval, code, QStringLiteral(
"eval code"));
325 script.strictMode = (directCall && isStrict);
326 script.inheritContext = inheritContext;
328 if (v4->hasException)
329 return Encode::undefined();
331 Function *function = script.function();
333 return Encode::undefined();
334 function->kind = Function::Eval;
336 if (function->isStrict() || isStrict) {
337 ScopedFunctionObject e(scope, FunctionObject::createScriptFunction(ctx, function));
338 ScopedValue thisObject(scope, directCall ? scope.engine->currentStackFrame->thisObject() : scope.engine->globalObject->asReturnedValue());
339 return checkedResult(v4, e->call(thisObject,
nullptr, 0));
342 ScopedValue thisObject(scope, scope.engine->currentStackFrame->thisObject());
344 return checkedResult(v4, function->call(thisObject,
nullptr, 0, ctx));
372ReturnedValue
GlobalFunctions::method_parseInt(
const FunctionObject *b,
const Value *,
const Value *argv,
int argc)
375 ScopedValue inputString(scope, argc ? argv[0] : Value::undefinedValue());
376 ScopedValue radix(scope, argc > 1 ? argv[1] : Value::undefinedValue());
377 int R = radix
->isUndefined() ? 0 : radix
->toInt32();
380 QString trimmed = inputString->toQString().trimmed();
383 const QChar *pos = trimmed.constData();
384 const QChar *end = pos + trimmed.size();
388 if (*pos == QLatin1Char(
'-'))
390 if (*pos == QLatin1Char(
'-') || *pos == QLatin1Char(
'+'))
393 bool stripPrefix =
true;
396 RETURN_RESULT(Encode(std::numeric_limits<
double>::quiet_NaN()));
404 && (pos[0] == QLatin1Char(
'0'))
405 && (pos[1] == QLatin1Char(
'x') || pos[1] == QLatin1Char(
'X'))) {
413 RETURN_RESULT(Encode(std::numeric_limits<
double>::quiet_NaN()));
414 bool overflow =
false;
415 qint64 v_overflow = 0;
416 unsigned overflow_digit_count = 0;
417 int d = toInt(*pos++, R);
419 RETURN_RESULT(Encode(std::numeric_limits<
double>::quiet_NaN()));
422 d = toInt(*pos++, R);
426 if (overflow_digit_count == 0) {
430 ++overflow_digit_count;
433 qint64 vNew = v * R + d;
444 double result = (
double) v_overflow * pow(
static_cast<
double>(R),
static_cast<
double>(overflow_digit_count));
453ReturnedValue
GlobalFunctions::method_parseFloat(
const FunctionObject *b,
const Value *,
const Value *argv,
int argc)
457 ScopedString inputString(scope, argc ? argv[0] : Value::undefinedValue(), ScopedString::Convert);
460 QString trimmed = inputString->toQString().trimmed();
463 if (trimmed.startsWith(QLatin1String(
"Infinity"))
464 || trimmed.startsWith(QLatin1String(
"+Infinity")))
466 if (trimmed.startsWith(QLatin1String(
"-Infinity")))
468 QByteArray ba = trimmed.toLatin1();
470 const char *begin = ba.constData();
471 const char *end =
nullptr;
472 double d = qstrtod(begin, &end, &ok);
473 if (end - begin == 0)
474 RETURN_RESULT(Encode(std::numeric_limits<
double>::quiet_NaN()));
508ReturnedValue
GlobalFunctions::method_decodeURI(
const FunctionObject *b,
const Value *,
const Value *argv,
int argc)
513 ExecutionEngine *v4 = b->engine();
514 QString uriString = argv[0].toQString();
516 QString out = decode(uriString, DecodeNonReserved, &ok);
519 ScopedString s(scope, scope.engine->newString(QStringLiteral(
"malformed URI sequence")));
527ReturnedValue
GlobalFunctions::method_decodeURIComponent(
const FunctionObject *b,
const Value *,
const Value *argv,
int argc)
532 ExecutionEngine *v4 = b->engine();
533 QString uriString = argv[0].toQString();
535 QString out = decode(uriString, DecodeAll, &ok);
538 ScopedString s(scope, scope.engine->newString(QStringLiteral(
"malformed URI sequence")));
546ReturnedValue
GlobalFunctions::method_encodeURI(
const FunctionObject *b,
const Value *,
const Value *argv,
int argc)
551 ExecutionEngine *v4 = b->engine();
552 QString uriString = argv[0].toQString();
554 QString out = encode(uriString, uriUnescapedReserved, &ok);
557 ScopedString s(scope, scope.engine->newString(QStringLiteral(
"malformed URI sequence")));
565ReturnedValue
GlobalFunctions::method_encodeURIComponent(
const FunctionObject *b,
const Value *,
const Value *argv,
int argc)
570 ExecutionEngine *v4 = b->engine();
571 QString uriString = argv[0].toQString();
573 QString out = encode(uriString, uriUnescaped, &ok);
576 ScopedString s(scope, scope.engine->newString(QStringLiteral(
"malformed URI sequence")));