blob: 941784ed63a8fa81243aec3a35e7a4e109ef0db6 (
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
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
|
{%- let enum_ = ci.get_enum_definition(name).unwrap() -%}
{%- if enum_.is_flat() -%}
export const {{ enum_.nm() }} = {
{%- for variant in enum_.variants() %}
{{ variant.name().to_shouty_snake_case() }}: {{loop.index}},
{%- endfor %}
};
Object.freeze({{ enum_.nm() }});
// Export the FFIConverter object to make external types work.
export class {{ ffi_converter }} extends FfiConverterArrayBuffer {
static read(dataStream) {
switch (dataStream.readInt32()) {
{%- for variant in enum_.variants() %}
case {{ loop.index }}:
return {{ enum_.nm() }}.{{ variant.name().to_shouty_snake_case() }}
{%- endfor %}
default:
return new Error("Unknown {{ enum_.nm() }} variant");
}
}
static write(dataStream, value) {
{%- for variant in enum_.variants() %}
if (value === {{ enum_.nm() }}.{{ variant.name().to_shouty_snake_case() }}) {
dataStream.writeInt32({{ loop.index }});
return;
}
{%- endfor %}
return new Error("Unknown {{ enum_.nm() }} variant");
}
static computeSize(value) {
return 4;
}
static checkType(value) {
if (!Number.isInteger(value) || value < 1 || value > {{ enum_.variants().len() }}) {
throw new UniFFITypeError(`${value} is not a valid value for {{ enum_.nm() }}`);
}
}
}
{%- else -%}
export class {{ enum_.nm() }} {}
{%- for variant in enum_.variants() %}
{{enum_.nm()}}.{{variant.name().to_upper_camel_case() }} = class extends {{ enum_.nm() }}{
constructor(
{% for field in variant.fields() -%}
{{ field.nm() }}{%- if loop.last %}{%- else %}, {%- endif %}
{% endfor -%}
) {
super();
{%- for field in variant.fields() %}
this.{{field.nm()}} = {{ field.nm() }};
{%- endfor %}
}
}
{%- 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 enum_.variants() %}
case {{ loop.index }}:
return new {{ enum_.nm() }}.{{ variant.name().to_upper_camel_case() }}(
{%- for field in variant.fields() %}
{{ field.ffi_converter() }}.read(dataStream){%- if loop.last %}{% else %}, {%- endif %}
{%- endfor %}
);
{%- endfor %}
default:
return new Error("Unknown {{ enum_.nm() }} variant");
}
}
static write(dataStream, value) {
{%- for variant in enum_.variants() %}
if (value instanceof {{enum_.nm()}}.{{ 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 %}
return new Error("Unknown {{ enum_.nm() }} variant");
}
static computeSize(value) {
// Size of the Int indicating the variant
let totalSize = 4;
{%- for variant in enum_.variants() %}
if (value instanceof {{enum_.nm()}}.{{ variant.name().to_upper_camel_case() }}) {
{%- for field in variant.fields() %}
totalSize += {{ field.ffi_converter() }}.computeSize(value.{{ field.nm() }});
{%- endfor %}
return totalSize;
}
{%- endfor %}
return new Error("Unknown {{ enum_.nm() }} variant");
}
static checkType(value) {
if (!(value instanceof {{ enum_.nm() }})) {
throw new UniFFITypeError(`${value} is not a subclass instance of {{ enum_.nm() }}`);
}
}
}
{%- endif %}
|