summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/modules/import-entries.js
blob: e20d4e119b2e9420da74f865176b04ba33143629 (plain)
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
// |jit-test| --enable-import-assertions

// Test importEntries property

function assertionEq(actual, expected) {
    var actualAssertions = actual['assertions'];
    var expectedAssertions = expected['assertions'];

    if(actualAssertions === null) {
        return expectedAssertions === actualAssertions
    }

    if(actualAssertions.length !== expectedAssertions.length) {
        return false;
    }

    for (var i = 0; i < expected.length; i++) {
        if(expected[i].type !== actual[i].type) {
            return false;
        }
    }

    return true;
}

function importEntryEq(a, b) {
    var r1 = a['moduleRequest']['specifier'] === b['moduleRequest']['specifier'] &&
        a['importName'] === b['importName'] &&
        a['localName'] === b['localName'];

    return r1 && assertionEq(a['moduleRequest'], b['moduleRequest']);
}

function findImportEntry(array, target)
{
    for (let i = 0; i < array.length; i++) {
        if (importEntryEq(array[i], target))
            return i;
    }
    return -1;
}

function testImportEntries(source, expected) {
    var module = parseModule(source);
    var actual = module.importEntries.slice(0);
    assertEq(actual.length, expected.length);
    for (var i = 0; i < expected.length; i++) {
        let index = findImportEntry(actual, expected[i]);
        assertEq(index >= 0, true);
        actual.splice(index, 1);
    }
}

testImportEntries('', []);

testImportEntries('import v from "mod";',
                  [{moduleRequest: {specifier: 'mod', assertions: null}, importName: 'default', localName: 'v'}]);

testImportEntries('import * as ns from "mod";',
                  [{moduleRequest: {specifier: 'mod', assertions: null}, importName: null, localName: 'ns'}]);

testImportEntries('import {x} from "mod";',
                  [{moduleRequest: {specifier: 'mod', assertions: null}, importName: 'x', localName: 'x'}]);

testImportEntries('import {x as v} from "mod";',
                  [{moduleRequest: {specifier: 'mod', assertions: null}, importName: 'x', localName: 'v'}]);

testImportEntries('import "mod";',
                  []);

testImportEntries('import {x} from "a"; import {y} from "b";',
                  [{moduleRequest: {specifier: 'a', assertions: null}, importName: 'x', localName: 'x'},
                   {moduleRequest: {specifier: 'b', assertions: null}, importName: 'y', localName: 'y'}]);

if (getRealmConfiguration("importAttributes")) {
    testImportEntries('import v from "mod" assert {};',
                  [{moduleRequest: {specifier: 'mod', assertions: null}, importName: 'default', localName: 'v'}]);

    testImportEntries('import v from "mod" assert { type: "js"};',
        [{moduleRequest: {specifier: 'mod', assertions: [{ type: 'js'}]}, importName: 'default', localName: 'v'}]);

    testImportEntries('import {x} from "mod" assert { type: "js"};',
                  [{moduleRequest: {specifier: 'mod', assertions: [{ type: 'js'}]}, importName: 'x', localName: 'x'}]);

    testImportEntries('import {x as v} from "mod" assert { type: "js"};',
                  [{moduleRequest: {specifier: 'mod', assertions: [{ type: 'js'}]}, importName: 'x', localName: 'v'}]);
}