1// generated by diplomat-tool 2import { DataError } from "./DataError.mjs" 3import { DataProvider } from "./DataProvider.mjs" 4import { LocaleFallbackConfig } from "./LocaleFallbackConfig.mjs" 5import { LocaleFallbackerWithConfig } from "./LocaleFallbackerWithConfig.mjs" 6import wasm from "./diplomat-wasm.mjs"; 7import * as diplomatRuntime from "./diplomat-runtime.mjs"; 8 9 10/** An object that runs the ICU4X locale fallback algorithm. 11* 12*See the [Rust documentation for `LocaleFallbacker`](https://docs.rs/icu/latest/icu/locale/fallback/struct.LocaleFallbacker.html) for more information. 13*/ 14const LocaleFallbacker_box_destroy_registry = new FinalizationRegistry((ptr) => { 15 wasm.icu4x_LocaleFallbacker_destroy_mv1(ptr); 16}); 17 18export class LocaleFallbacker { 19 20 // Internal ptr reference: 21 #ptr = null; 22 23 // Lifetimes are only to keep dependencies alive. 24 // Since JS won't garbage collect until there are no incoming edges. 25 #selfEdge = []; 26 27 #internalConstructor(symbol, ptr, selfEdge) { 28 if (symbol !== diplomatRuntime.internalConstructor) { 29 console.error("LocaleFallbacker is an Opaque type. You cannot call its constructor."); 30 return; 31 } 32 33 this.#ptr = ptr; 34 this.#selfEdge = selfEdge; 35 36 // Are we being borrowed? If not, we can register. 37 if (this.#selfEdge.length === 0) { 38 LocaleFallbacker_box_destroy_registry.register(this, this.#ptr); 39 } 40 41 return this; 42 } 43 get ffiValue() { 44 return this.#ptr; 45 } 46 47 #defaultConstructor() { 48 const result = wasm.icu4x_LocaleFallbacker_create_mv1(); 49 50 try { 51 return new LocaleFallbacker(diplomatRuntime.internalConstructor, result, []); 52 } 53 54 finally {} 55 } 56 57 static createWithProvider(provider) { 58 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 59 60 const result = wasm.icu4x_LocaleFallbacker_create_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue); 61 62 try { 63 if (!diplomatReceive.resultFlag) { 64 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 65 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 66 } 67 return new LocaleFallbacker(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 68 } 69 70 finally { 71 diplomatReceive.free(); 72 } 73 } 74 75 static withoutData() { 76 const result = wasm.icu4x_LocaleFallbacker_without_data_mv1(); 77 78 try { 79 return new LocaleFallbacker(diplomatRuntime.internalConstructor, result, []); 80 } 81 82 finally {} 83 } 84 85 forConfig(config) { 86 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 87 88 // This lifetime edge depends on lifetimes 'a 89 let aEdges = [this]; 90 91 const result = wasm.icu4x_LocaleFallbacker_for_config_mv1(this.ffiValue, ...LocaleFallbackConfig._fromSuppliedValue(diplomatRuntime.internalConstructor, config)._intoFFI(functionCleanupArena, {})); 92 93 try { 94 return new LocaleFallbackerWithConfig(diplomatRuntime.internalConstructor, result, [], aEdges); 95 } 96 97 finally { 98 functionCleanupArena.free(); 99 } 100 } 101 102 constructor() { 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}