summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/modules/import-entries.js
blob: ed668f00692607c0a6b7060bc510cbb7d3eabe42 (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
88
89
90
// |jit-test| --enable-import-attributes

// Test importEntries property

function attributeEq(actual, expected) {
    var actualAttributes = actual['attributes'];
    var expectedAttributes = expected['attributes'];

    if(actualAttributes === null) {
        return expectedAttributes === actualAttributes
    }

    if(actualAttributes.length !== expectedAttributes.length) {
        return false;
    }

    for (var i = 0; i < expectedAttributes.length; i++) {
        if (
            expectedAttributes[i]['key'] !== actualAttributes[i]['key'] ||
            expectedAttributes[i]['value'] !== actualAttributes[i]['value']
        ) {
            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 && attributeEq(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', attributes: null}, importName: 'default', localName: 'v'}]);

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

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

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

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

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

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

    testImportEntries('import v from "mod" with { type: "js"};',
        [{moduleRequest: {specifier: 'mod', attributes: [{ key: 'type', value: 'js'}]}, importName: 'default', localName: 'v'}]);

    testImportEntries('import {x} from "mod" with { type: "js"};',
                  [{moduleRequest: {specifier: 'mod', attributes: [{ key: 'type', value: 'js'}]}, importName: 'x', localName: 'x'}]);

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