LocaleDisplayNamesFormatter.mjs (5249B)
1 // generated by diplomat-tool 2 import { DataError } from "./DataError.mjs" 3 import { DataProvider } from "./DataProvider.mjs" 4 import { DisplayNamesOptions } from "./DisplayNamesOptions.mjs" 5 import { Locale } from "./Locale.mjs" 6 import wasm from "./diplomat-wasm.mjs"; 7 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 8 9 10 /** 11 * See the [Rust documentation for `LocaleDisplayNamesFormatter`](https://docs.rs/icu/latest/icu/experimental/displaynames/struct.LocaleDisplayNamesFormatter.html) for more information. 12 */ 13 const LocaleDisplayNamesFormatter_box_destroy_registry = new FinalizationRegistry((ptr) => { 14 wasm.icu4x_LocaleDisplayNamesFormatter_destroy_mv1(ptr); 15 }); 16 17 export class LocaleDisplayNamesFormatter { 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 this.#ptr = ptr; 31 this.#selfEdge = selfEdge; 32 33 // Are we being borrowed? If not, we can register. 34 if (this.#selfEdge.length === 0) { 35 LocaleDisplayNamesFormatter_box_destroy_registry.register(this, this.#ptr); 36 } 37 38 return this; 39 } 40 get ffiValue() { 41 return this.#ptr; 42 } 43 44 45 /** 46 * Creates a new `LocaleDisplayNamesFormatter` from locale data and an options bag using compiled data. 47 * 48 * See the [Rust documentation for `try_new`](https://docs.rs/icu/latest/icu/experimental/displaynames/struct.LocaleDisplayNamesFormatter.html#method.try_new) for more information. 49 */ 50 #defaultConstructor(locale, options) { 51 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 52 53 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 54 55 56 const result = wasm.icu4x_LocaleDisplayNamesFormatter_create_v1_mv1(diplomatReceive.buffer, locale.ffiValue, ...DisplayNamesOptions._fromSuppliedValue(diplomatRuntime.internalConstructor, options)._intoFFI(functionCleanupArena, {})); 57 58 try { 59 if (!diplomatReceive.resultFlag) { 60 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 61 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 62 } 63 return new LocaleDisplayNamesFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 64 } 65 66 finally { 67 functionCleanupArena.free(); 68 69 diplomatReceive.free(); 70 } 71 } 72 73 /** 74 * Creates a new `LocaleDisplayNamesFormatter` from locale data and an options bag using a particular data source. 75 * 76 * See the [Rust documentation for `try_new`](https://docs.rs/icu/latest/icu/experimental/displaynames/struct.LocaleDisplayNamesFormatter.html#method.try_new) for more information. 77 */ 78 static createWithProvider(provider, locale, options) { 79 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 80 81 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 82 83 84 const result = wasm.icu4x_LocaleDisplayNamesFormatter_create_v1_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue, locale.ffiValue, ...DisplayNamesOptions._fromSuppliedValue(diplomatRuntime.internalConstructor, options)._intoFFI(functionCleanupArena, {})); 85 86 try { 87 if (!diplomatReceive.resultFlag) { 88 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 89 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 90 } 91 return new LocaleDisplayNamesFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 92 } 93 94 finally { 95 functionCleanupArena.free(); 96 97 diplomatReceive.free(); 98 } 99 } 100 101 /** 102 * Returns the locale-specific display name of a locale. 103 * 104 * See the [Rust documentation for `of`](https://docs.rs/icu/latest/icu/experimental/displaynames/struct.LocaleDisplayNamesFormatter.html#method.of) for more information. 105 */ 106 of(locale) { 107 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 108 109 wasm.icu4x_LocaleDisplayNamesFormatter_of_mv1(this.ffiValue, locale.ffiValue, write.buffer); 110 111 try { 112 return write.readString8(); 113 } 114 115 finally { 116 write.free(); 117 } 118 } 119 120 constructor(locale, options) { 121 if (arguments[0] === diplomatRuntime.exposeConstructor) { 122 return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1)); 123 } else if (arguments[0] === diplomatRuntime.internalConstructor) { 124 return this.#internalConstructor(...arguments); 125 } else { 126 return this.#defaultConstructor(...arguments); 127 } 128 } 129 }