File: ecmatest.cpp

package info (click to toggle)
kjs 5.103.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,020 kB
  • sloc: cpp: 36,704; javascript: 5,079; yacc: 790; perl: 191; sh: 52; makefile: 7
file content (402 lines) | stat: -rw-r--r-- 12,814 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
 *  This file is part of the KDE libraries
 *
 *  Copyright (C) 2012 Rolf Eike Beer <kde@opensource.sf-tec.de>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 */

#include "ecmatest.h"
#include <QTest>

#include <wtf/HashTraits.h>
#include "JSLock.h"
#include "object.h"
#include "JSVariableObject.h"
#include "Parser.h"

#include <QMap>
#include <QDebug>
#include <QRegularExpression>
#include <QDirIterator>

// Let the interpreter create its own global Object instead of using our selfcreated
#define USE_KJS_GLOBAL 1

// from khtml/ecma/kjs_binding.cpp"
KJS::UString::UString(const QString &d)
{
    unsigned int len = d.length();
    KJS::UChar *dat = static_cast<KJS::UChar *>(fastMalloc(sizeof(KJS::UChar) * len));
    memcpy(dat, reinterpret_cast<const void*>(d.unicode()), len * sizeof(KJS::UChar));
    m_rep = KJS::UString::Rep::create(dat, len);
}

QString KJS::UString::qstring() const
{
    return QString((QChar *) data(), size());
}

// from khtml/ecma/debugger/value2string.cpp
QString valueToString(KJS::JSValue *value)
{
    switch (KJS::JSValue::type(value)) {
    case KJS::NumberType: {
        double v = 0.0;
        KJS::JSValue::getNumber(value, v);
        return QString::number(v);
    }
    case KJS::BooleanType:
        return KJS::JSValue::getBoolean(value) ? "true" : "false";
    case KJS::StringType: {
        KJS::UString s;
        KJS::JSValue::getString(value, s);
        return '"' + s.qstring() + '"';
    }
    case KJS::UndefinedType:
        return "undefined";
    case KJS::NullType:
        return "null";
    case KJS::ObjectType:
        return "[object " + static_cast<KJS::JSObject *>(value)->className().qstring() + "]";
    case KJS::GetterSetterType:
    case KJS::UnspecifiedType:
    default:
        return QString();
    }
}

// from khtml/ecma/debugger/debugwindow.cpp
static QString exceptionToString(KJS::ExecState *exec, KJS::JSValue *exceptionObj)
{
    QString exceptionMsg = valueToString(exceptionObj);

    // Since we purposefully bypass toString, we need to figure out
    // string serialization ourselves.
    //### might be easier to export class info for ErrorInstance ---

    KJS::JSObject *valueObj = KJS::JSValue::getObject(exceptionObj);
    KJS::JSValue  *protoObj = valueObj ? valueObj->prototype() : nullptr;

    bool exception   = false;
    bool syntaxError = false;
    if (protoObj == exec->lexicalInterpreter()->builtinSyntaxErrorPrototype()) {
        exception   = true;
        syntaxError = true;
    }

    if (protoObj == exec->lexicalInterpreter()->builtinErrorPrototype()          ||
            protoObj == exec->lexicalInterpreter()->builtinEvalErrorPrototype()      ||
            protoObj == exec->lexicalInterpreter()->builtinReferenceErrorPrototype() ||
            protoObj == exec->lexicalInterpreter()->builtinRangeErrorPrototype()     ||
            protoObj == exec->lexicalInterpreter()->builtinTypeErrorPrototype()      ||
            protoObj == exec->lexicalInterpreter()->builtinURIErrorPrototype()) {
        exception = true;
    }

    if (!exception) {
        return exceptionMsg;
    }

    // Clear exceptions temporarily so we can get/call a few things.
    // We memorize the old exception first, of course. Note that
    // This is not always the same as exceptionObj since we may be
    //  asked to translate a non-active exception
    KJS::JSValue *oldExceptionObj = exec->exception();
    exec->clearException();

    // We want to serialize the syntax errors ourselves, to provide the line number.
    // The URL is in "sourceURL" and the line is in "line"
    // ### TODO: Perhaps we want to use 'sourceId' in case of eval contexts.
    if (syntaxError) {
        KJS::JSValue *lineValue = valueObj->get(exec, "line");
        KJS::JSValue *urlValue  = valueObj->get(exec, "sourceURL");

        int      line = KJS::JSValue::toNumber(lineValue, exec);
        QString  url  = KJS::JSValue::toString(urlValue, exec).qstring();
        exceptionMsg = QString::fromLatin1("Parse error at %1 line %2").arg(url).arg(line + 1);
    } else {
        // ### it's still not 100% safe to call toString here, even on
        // native exception objects, since someone might have changed the toString property
        // of the exception prototype, but I'll punt on this case for now.
        exceptionMsg = KJS::JSValue::toString(exceptionObj, exec).qstring();
    }
    exec->setException(oldExceptionObj);
    return exceptionMsg;
}

#ifndef USE_KJS_GLOBAL
class GlobalImp : public KJS::JSGlobalObject
{
public:
    virtual KJS::UString className() const
    {
        return "global";
    }
};

static GlobalImp *global;
#endif
static QString basedir("");
static QByteArray testrunner;
static QMap<QByteArray, QByteArray> includes;
static QStringList expectedBroken;  // list of tests we know that will fail

/**
 * load the given file from the harness directory
 * @param fn filename
 * @return if operation succeeded
 *
 * Will load the given file into the "includes" map
 */
static bool loadInclude(const QByteArray &fn)
{
    QFile runnerfile(basedir + QLatin1String("test/harness/") + QString::fromLatin1(fn.constData()));

    if (!runnerfile.open(QIODevice::ReadOnly)) {
        return false;
    }

    includes[ fn ] = runnerfile.readAll();

    return true;
}

QTEST_MAIN(ECMAscriptTest)

void ECMAscriptTest::initTestCase()
{
    basedir = QString::fromUtf8(qgetenv("ECMATEST_BASEDIR").constData());

    if (basedir.isEmpty()) {
        qFatal("ECMATEST_BASEDIR not set");
    }

    if (!basedir.endsWith(QLatin1Char('/'))) {
        basedir += QLatin1Char('/');
    }

    QVERIFY(loadInclude("sta.js"));
    QVERIFY(loadInclude("ed.js"));

    testrunner = includes[ "sta.js" ] + includes[ "ed.js" ] + '\n';

    const QString brokenFn = QString::fromLatin1(qgetenv("ECMATEST_BROKEN").constData());
    if (!brokenFn.isEmpty()) {
        QFile brokenF(brokenFn);
        if (!brokenF.open(QIODevice::ReadOnly)) {
            const QByteArray errmsg = QByteArray("cannot open ") + QFile::encodeName(brokenFn);
            QWARN(errmsg.constData());
        } else {
            expectedBroken = QString::fromLatin1(brokenF.readAll().constData()).split(QLatin1Char('\n'))
                             .filter(QRegularExpression(QStringLiteral("^[^#].*")));
        }
    }

    m_passed = 0;
    m_failed = 0;
}

static QByteArray getTextProperty(const QByteArray &property, const QByteArray &code)
{
    int from = code.indexOf(property);
    if (from == -1) {
        return QByteArray();
    }

    from += property.length();
    while (code[ from ] == ' ') {
        from++;
    }

    int to = code.indexOf('\n', from);
    if (code[to - 1] == '\r') {
        to--;
    }
    // poor mans escaping
    return code.mid(from, to - from).replace("\\", "\\\\").replace("\"", "\\\"");
}

#define ECMATEST_VERIFY( expr ) \
    do { \
        const bool tmp_result = ( expr ); \
        if ( tmp_result ) \
            m_passed++; \
        else \
            m_failed++; \
        if ( knownBroken ) \
            QEXPECT_FAIL(QTest::currentDataTag(), "It is known that KJS doesn't pass this test", Abort); \
        QVERIFY( tmp_result ); \
    } while (0)

static QMap< QByteArray, QByteArray > skips;

void ECMAscriptTest::runAllTests()
{
    static const QByteArray include = "$INCLUDE(\"";

    QFETCH(QString, filename);
    QByteArray expectedError;

    QFile input(filename);

    Q_FOREACH (const QByteArray &skip, skips.keys()) {
        if (skip == QTest::currentDataTag()) {
            QSKIP(skips[ skip ].constData());
        }
    }

    QVERIFY(input.open(QIODevice::ReadOnly));

    const QByteArray testdata = input.readAll();

    QVERIFY(! testdata.isEmpty());

#ifdef USE_KJS_GLOBAL
    RefPtr<KJS::Interpreter> interp = new KJS::Interpreter();
#else
    RefPtr<KJS::Interpreter> interp = new KJS::Interpreter(global);
#endif

    KJS::Interpreter::setShouldPrintExceptions(true);

    QByteArray testscript;

    // test is expected to fail
    if (testdata.indexOf("@negative") >= 0) {
        expectedError = getTextProperty("@negative", testdata);
        if (expectedError.isEmpty()) {
            expectedError = ".";
        }
    }

    int from = 0;
    while ((from = testdata.indexOf(include, from)) >= 0) {
        int endq = testdata.indexOf("\"", from + include.length());
        QVERIFY(endq >= 0);

        const QByteArray includeFile = testdata.mid(from + include.length(), endq - from - include.length());

        if (! includes.contains(includeFile)) {
            QVERIFY(loadInclude(includeFile));
        }

        testscript += includes[ includeFile ];
        from = endq;
    }

    testscript += testrunner;

    testscript += testdata;

    const QFileInfo info(input);

    const QString scriptutf = QString::fromUtf8(testscript.constData());

//     QWARN(filename.toAscii().data());
    KJS::Completion completion = interp->evaluate(info.fileName().toLatin1().constData(), 0, scriptutf);

    const bool knownBroken = expectedBroken.contains(QString::fromLatin1(QTest::currentDataTag()));

    if (expectedError.isEmpty()) {
        ECMATEST_VERIFY(completion.complType() != KJS::Throw);
    } else {
        if (knownBroken && completion.complType() != KJS::Throw) {
            QEXPECT_FAIL(QTest::currentDataTag(), "It is known that KJS doesn't pass this test", Abort);
            m_failed++;
        }

        QCOMPARE(completion.complType(), KJS::Throw);
        QVERIFY(completion.value() != nullptr);

        const QString eMsg = exceptionToString(interp->execState(), completion.value());

        if (expectedError == "^((?!NotEarlyError).)*$") {
            ECMATEST_VERIFY(eMsg.indexOf("NotEarlyError") == -1);
        } else if (expectedError == ".") {
            // means "every exception passes
        } else {
            ECMATEST_VERIFY(eMsg.indexOf(expectedError) >= 0);
        }
    }
}

void ECMAscriptTest::runAllTests_data()
{
#ifndef USE_KJS_GLOBAL
    global = new GlobalImp();
#endif

    QTest::addColumn<QString>("filename");

    const QStringList js(QLatin1String("*.js"));
    const QStringList all(QLatin1String("*"));
    const QString chapter = QString::fromLatin1(qgetenv("ECMATEST_CHAPTER").constData());

    if (!chapter.isEmpty()) {
        QWARN(QByteArray("===> Testing chapter " + chapter.toLatin1()).constData());
    }

    if (chapter.isEmpty() || chapter.startsWith("ch15")) {
        const QByteArray timeZoneDepend = "this test depends on the timezone and may or may not fail, avoid it for the moment";
        // The tests are timezone dependent because of the Date implementation in kjs.
        // It only affects the limit by +/- 24h (or less, depending on your timezone),
        // the "normal" use is not affected.
        // It requires a complete Date rewrite to fix this, see ECMA Edition 5.1r6 15.9.1.1
        skips[ "15.9.5.43-0-8" ] = timeZoneDepend;
        skips[ "15.9.5.43-0-9" ] = timeZoneDepend;
        skips[ "15.9.5.43-0-10" ] = timeZoneDepend;
        skips[ "15.9.5.43-0-11" ] = timeZoneDepend;
        skips[ "15.9.5.43-0-12" ] = timeZoneDepend;
    }

#ifndef USE_KJS_GLOBAL
    // some tests fail when the suite is run as a whole
    if (chapter.isEmpty() || chapter.startsWith("ch15") || chapter.startsWith("ch12")) {
        const QByteArray endlessLoop = "this test causes an endless loop, avoid it for the moment";
        const QByteArray crashTest = "this test causes a crash when run as part of the whole suite";
        skips[ "S12.7_A9_T1" ] = endlessLoop;
        skips[ "S12.7_A9_T2" ] = endlessLoop;
        skips[ "S15.1.2.3_A6" ] = endlessLoop;
        skips[ "S15.1.3.1_A2.5_T1" ] = endlessLoop;
        skips[ "S15.1.3.2_A2.4_T1" ] = endlessLoop;
        skips[ "S15.1.3.2_A2.5_T1" ] = endlessLoop;
        skips[ "15.2.3.4-4-1" ] = crashTest;
        skips[ "15.2.3.11-4-1" ] = crashTest;
        skips[ "15.2.3.12-3-1" ] = crashTest;
    }
#endif

    QDirIterator it(basedir + QLatin1String("test/suite/") + chapter, QDirIterator::Subdirectories);
    while (it.hasNext()) {
        it.next();

        const QFileInfo info = it.fileInfo();

        if (!info.isFile()) {
            continue;
        }

        QString filename = info.fileName();

        filename.chop(3); // .js

        QTest::newRow(filename.toLatin1().constData()) << info.absoluteFilePath();
    }
}

void ECMAscriptTest::cleanup()
{
#ifndef USE_KJS_GLOBAL
    global->clearProperties();
#endif
}

void ECMAscriptTest::cleanupTestCase()
{
    qDebug() << "passed testcases:" << m_passed << "failed testcases:" << m_failed;
}