summaryrefslogtreecommitdiffstats
path: root/toolkit/components/uniffi-bindgen-gecko-js/src/templates/js/Enum.sys.mjs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /toolkit/components/uniffi-bindgen-gecko-js/src/templates/js/Enum.sys.mjs
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/uniffi-bindgen-gecko-js/src/templates/js/Enum.sys.mjs')
-rw-r--r--toolkit/components/uniffi-bindgen-gecko-js/src/templates/js/Enum.sys.mjs113
1 files changed, 113 insertions, 0 deletions
diff --git a/toolkit/components/uniffi-bindgen-gecko-js/src/templates/js/Enum.sys.mjs b/toolkit/components/uniffi-bindgen-gecko-js/src/templates/js/Enum.sys.mjs
new file mode 100644
index 0000000000..f7716ac6d8
--- /dev/null
+++ b/toolkit/components/uniffi-bindgen-gecko-js/src/templates/js/Enum.sys.mjs
@@ -0,0 +1,113 @@
+{%- 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 %}