1// generated by diplomat-tool 2import { DataError } from "./DataError.mjs" 3import { DataProvider } from "./DataProvider.mjs" 4import { DisplayNamesOptions } from "./DisplayNamesOptions.mjs" 5import { Locale } from "./Locale.mjs" 6import wasm from "./diplomat-wasm.mjs"; 7import * as diplomatRuntime from "./diplomat-runtime.mjs"; 8 9 10/** See the [Rust documentation for `LocaleDisplayNamesFormatter`](https://docs.rs/icu/latest/icu/displaynames/struct.LocaleDisplayNamesFormatter.html) for more information. 11*/ 12const LocaleDisplayNamesFormatter_box_destroy_registry = new FinalizationRegistry((ptr) => { 13 wasm.icu4x_LocaleDisplayNamesFormatter_destroy_mv1(ptr); 14}); 15 16export class LocaleDisplayNamesFormatter { 17 18 // Internal ptr reference: 19 #ptr = null; 20 21 // Lifetimes are only to keep dependencies alive. 22 // Since JS won't garbage collect until there are no incoming edges. 23 #selfEdge = []; 24 25 #internalConstructor(symbol, ptr, selfEdge) { 26 if (symbol !== diplomatRuntime.internalConstructor) { 27 console.error("LocaleDisplayNamesFormatter is an Opaque type. You cannot call its constructor."); 28 return; 29 } 30 31 this.#ptr = ptr; 32 this.#selfEdge = selfEdge; 33 34 // Are we being borrowed? If not, we can register. 35 if (this.#selfEdge.length === 0) { 36 LocaleDisplayNamesFormatter_box_destroy_registry.register(this, this.#ptr); 37 } 38 39 return this; 40 } 41 get ffiValue() { 42 return this.#ptr; 43 } 44 45 #defaultConstructor(locale, options) { 46 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 47 48 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 49 50 const result = wasm.icu4x_LocaleDisplayNamesFormatter_create_v1_mv1(diplomatReceive.buffer, locale.ffiValue, ...DisplayNamesOptions._fromSuppliedValue(diplomatRuntime.internalConstructor, options)._intoFFI(functionCleanupArena, {})); 51 52 try { 53 if (!diplomatReceive.resultFlag) { 54 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 55 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 56 } 57 return new LocaleDisplayNamesFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 58 } 59 60 finally { 61 functionCleanupArena.free(); 62 63 diplomatReceive.free(); 64 } 65 } 66 67 static createWithProvider(provider, locale, options) { 68 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 69 70 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 71 72 const result = wasm.icu4x_LocaleDisplayNamesFormatter_create_v1_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue, locale.ffiValue, ...DisplayNamesOptions._fromSuppliedValue(diplomatRuntime.internalConstructor, options)._intoFFI(functionCleanupArena, {})); 73 74 try { 75 if (!diplomatReceive.resultFlag) { 76 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 77 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 78 } 79 return new LocaleDisplayNamesFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 80 } 81 82 finally { 83 functionCleanupArena.free(); 84 85 diplomatReceive.free(); 86 } 87 } 88 89 of(locale) { 90 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 91 wasm.icu4x_LocaleDisplayNamesFormatter_of_mv1(this.ffiValue, locale.ffiValue, write.buffer); 92 93 try { 94 return write.readString8(); 95 } 96 97 finally { 98 write.free(); 99 } 100 } 101 102 constructor(locale, options) { 103 if (arguments[0] === diplomatRuntime.exposeConstructor) { 104 return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1)); 105 } else if (arguments[0] === diplomatRuntime.internalConstructor) { 106 return this.#internalConstructor(...arguments); 107 } else { 108 return this.#defaultConstructor(...arguments); 109 } 110 } 111}