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