summaryrefslogtreecommitdiffstats
path: root/toolkit/components/uniffi-bindgen-gecko-js/src/templates/js/Error.sys.mjs
blob: c472f1a27dab6d1c7fbe41ff479bd8eadcb01830 (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
{%- let string_type = Type::String %}
{%- let string_ffi_converter = string_type.ffi_converter() %}

export class {{ error.nm() }} extends Error {}
{% for variant in error.variants() %}

export class {{ variant.name().to_upper_camel_case() }} extends {{ error.nm() }} {
{% if error.is_flat() %}
    constructor(message, ...params) {
        super(...params);
        this.message = message;
    }
{%- else %}
    constructor(
        {% for field in variant.fields() -%}
        {{field.nm()}},
        {% endfor -%}
        ...params
    ) {
        super(...params);
        {%- for field in variant.fields() %}
        this.{{field.nm()}} = {{ field.nm() }};
        {%- endfor %}
    }
{%- endif %}
    toString() {
        return `{{ variant.name().to_upper_camel_case() }}: ${super.toString()}`
    }
}
{%- endfor %}

// Export the FFIConverter object to make external types work.
export class {{ ffi_converter }} extends FfiConverterArrayBuffer {
    static read(dataStream) {
        switch (dataStream.readInt32()) {
            {%- for variant in error.variants() %}
            case {{ loop.index }}:
                {%- if error.is_flat() %}
                return new {{ variant.name().to_upper_camel_case()  }}({{ string_ffi_converter }}.read(dataStream));
                {%- else %}
                return new {{ variant.name().to_upper_camel_case()  }}(
                    {%- for field in variant.fields() %}
                    {{ field.ffi_converter() }}.read(dataStream){%- if loop.last %}{% else %}, {%- endif %}
                    {%- endfor %}
                    );
                {%- endif %}
            {%- endfor %}
            default:
                throw new UniFFITypeError("Unknown {{ error.nm() }} variant");
        }
    }
    static computeSize(value) {
        // Size of the Int indicating the variant
        let totalSize = 4;
        {%- for variant in error.variants() %}
        if (value instanceof {{ variant.name().to_upper_camel_case() }}) {
            {%- for field in variant.fields() %}
            totalSize += {{ field.ffi_converter() }}.computeSize(value.{{ field.nm() }});
            {%- endfor %}
            return totalSize;
        }
        {%- endfor %}
        throw new UniFFITypeError("Unknown {{ error.nm() }} variant");
    }
    static write(dataStream, value) {
        {%- for variant in error.variants() %}
        if (value instanceof {{ variant.name().to_upper_camel_case() }}) {
            dataStream.writeInt32({{ loop.index }});
            {%- for field in variant.fields() %}
            {{ field.ffi_converter() }}.write(dataStream, value.{{ field.nm() }});
            {%- endfor %}
            return;
        }
        {%- endfor %}
        throw new UniFFITypeError("Unknown {{ error.nm() }} variant");
    }

    static errorClass = {{ error.nm() }};
}