diff options
Diffstat (limited to 'toolkit/components/uniffi-bindgen-gecko-js/src/templates/js/Optional.jsm')
-rw-r--r-- | toolkit/components/uniffi-bindgen-gecko-js/src/templates/js/Optional.jsm | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/toolkit/components/uniffi-bindgen-gecko-js/src/templates/js/Optional.jsm b/toolkit/components/uniffi-bindgen-gecko-js/src/templates/js/Optional.jsm new file mode 100644 index 0000000000..eaa96fe55e --- /dev/null +++ b/toolkit/components/uniffi-bindgen-gecko-js/src/templates/js/Optional.jsm @@ -0,0 +1,35 @@ +class {{ ffi_converter }} extends FfiConverterArrayBuffer { + static checkType(name, value) { + if (value !== undefined && value !== null) { + {{ inner.ffi_converter() }}.checkType(name, value) + } + } + + static read(dataStream) { + const code = dataStream.readUint8(0); + switch (code) { + case 0: + return null + case 1: + return {{ inner.ffi_converter() }}.read(dataStream) + default: + throw UniFFIError(`Unexpected code: ${code}`); + } + } + + static write(dataStream, value) { + if (value === null || value === undefined) { + dataStream.writeUint8(0); + return; + } + dataStream.writeUint8(1); + {{ inner.ffi_converter() }}.write(dataStream, value) + } + + static computeSize(value) { + if (value === null || value === undefined) { + return 1; + } + return 1 + {{ inner.ffi_converter() }}.computeSize(value) + } +} |