• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// generated by diplomat-tool
2import { LocaleParseError } from "./LocaleParseError.mjs"
3import wasm from "./diplomat-wasm.mjs";
4import * as diplomatRuntime from "./diplomat-runtime.mjs";
5
6
7/** An ICU4X Locale, capable of representing strings like `"en-US"`.
8*
9*See the [Rust documentation for `Locale`](https://docs.rs/icu/latest/icu/locale/struct.Locale.html) for more information.
10*/
11const Locale_box_destroy_registry = new FinalizationRegistry((ptr) => {
12    wasm.icu4x_Locale_destroy_mv1(ptr);
13});
14
15export class Locale {
16
17    // Internal ptr reference:
18    #ptr = null;
19
20    // Lifetimes are only to keep dependencies alive.
21    // Since JS won't garbage collect until there are no incoming edges.
22    #selfEdge = [];
23
24    #internalConstructor(symbol, ptr, selfEdge) {
25        if (symbol !== diplomatRuntime.internalConstructor) {
26            console.error("Locale is an Opaque type. You cannot call its constructor.");
27            return;
28        }
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            Locale_box_destroy_registry.register(this, this.#ptr);
36        }
37
38        return this;
39    }
40    get ffiValue() {
41        return this.#ptr;
42    }
43
44    static fromString(name) {
45        let functionCleanupArena = new diplomatRuntime.CleanupArena();
46
47        const nameSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, name));
48
49        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
50
51        const result = wasm.icu4x_Locale_from_string_mv1(diplomatReceive.buffer, ...nameSlice.splat());
52
53        try {
54            if (!diplomatReceive.resultFlag) {
55                const cause = new LocaleParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
56                throw new globalThis.Error('LocaleParseError: ' + cause.value, { cause });
57            }
58            return new Locale(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
59        }
60
61        finally {
62            functionCleanupArena.free();
63
64            diplomatReceive.free();
65        }
66    }
67
68    static und() {
69        const result = wasm.icu4x_Locale_und_mv1();
70
71        try {
72            return new Locale(diplomatRuntime.internalConstructor, result, []);
73        }
74
75        finally {}
76    }
77
78    clone() {
79        const result = wasm.icu4x_Locale_clone_mv1(this.ffiValue);
80
81        try {
82            return new Locale(diplomatRuntime.internalConstructor, result, []);
83        }
84
85        finally {}
86    }
87
88    get basename() {
89        const write = new diplomatRuntime.DiplomatWriteBuf(wasm);
90        wasm.icu4x_Locale_basename_mv1(this.ffiValue, write.buffer);
91
92        try {
93            return write.readString8();
94        }
95
96        finally {
97            write.free();
98        }
99    }
100
101    getUnicodeExtension(s) {
102        let functionCleanupArena = new diplomatRuntime.CleanupArena();
103
104        const sSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, s));
105
106        const write = new diplomatRuntime.DiplomatWriteBuf(wasm);
107
108        const result = wasm.icu4x_Locale_get_unicode_extension_mv1(this.ffiValue, ...sSlice.splat(), write.buffer);
109
110        try {
111            return result === 0 ? null : write.readString8();
112        }
113
114        finally {
115            functionCleanupArena.free();
116
117            write.free();
118        }
119    }
120
121    get language() {
122        const write = new diplomatRuntime.DiplomatWriteBuf(wasm);
123        wasm.icu4x_Locale_language_mv1(this.ffiValue, write.buffer);
124
125        try {
126            return write.readString8();
127        }
128
129        finally {
130            write.free();
131        }
132    }
133
134    set language(s) {
135        let functionCleanupArena = new diplomatRuntime.CleanupArena();
136
137        const sSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, s));
138
139        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
140
141        const result = wasm.icu4x_Locale_set_language_mv1(diplomatReceive.buffer, this.ffiValue, ...sSlice.splat());
142
143        try {
144            if (!diplomatReceive.resultFlag) {
145                const cause = new LocaleParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
146                throw new globalThis.Error('LocaleParseError: ' + cause.value, { cause });
147            }
148
149        }
150
151        finally {
152            functionCleanupArena.free();
153
154            diplomatReceive.free();
155        }
156    }
157
158    get region() {
159        const write = new diplomatRuntime.DiplomatWriteBuf(wasm);
160
161        const result = wasm.icu4x_Locale_region_mv1(this.ffiValue, write.buffer);
162
163        try {
164            return result === 0 ? null : write.readString8();
165        }
166
167        finally {
168            write.free();
169        }
170    }
171
172    set region(s) {
173        let functionCleanupArena = new diplomatRuntime.CleanupArena();
174
175        const sSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, s));
176
177        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
178
179        const result = wasm.icu4x_Locale_set_region_mv1(diplomatReceive.buffer, this.ffiValue, ...sSlice.splat());
180
181        try {
182            if (!diplomatReceive.resultFlag) {
183                const cause = new LocaleParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
184                throw new globalThis.Error('LocaleParseError: ' + cause.value, { cause });
185            }
186
187        }
188
189        finally {
190            functionCleanupArena.free();
191
192            diplomatReceive.free();
193        }
194    }
195
196    get script() {
197        const write = new diplomatRuntime.DiplomatWriteBuf(wasm);
198
199        const result = wasm.icu4x_Locale_script_mv1(this.ffiValue, write.buffer);
200
201        try {
202            return result === 0 ? null : write.readString8();
203        }
204
205        finally {
206            write.free();
207        }
208    }
209
210    set script(s) {
211        let functionCleanupArena = new diplomatRuntime.CleanupArena();
212
213        const sSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, s));
214
215        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
216
217        const result = wasm.icu4x_Locale_set_script_mv1(diplomatReceive.buffer, this.ffiValue, ...sSlice.splat());
218
219        try {
220            if (!diplomatReceive.resultFlag) {
221                const cause = new LocaleParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
222                throw new globalThis.Error('LocaleParseError: ' + cause.value, { cause });
223            }
224
225        }
226
227        finally {
228            functionCleanupArena.free();
229
230            diplomatReceive.free();
231        }
232    }
233
234    static normalize(s) {
235        let functionCleanupArena = new diplomatRuntime.CleanupArena();
236
237        const sSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, s));
238
239        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
240
241        const write = new diplomatRuntime.DiplomatWriteBuf(wasm);
242
243        const result = wasm.icu4x_Locale_normalize_mv1(diplomatReceive.buffer, ...sSlice.splat(), write.buffer);
244
245        try {
246            if (!diplomatReceive.resultFlag) {
247                const cause = new LocaleParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
248                throw new globalThis.Error('LocaleParseError: ' + cause.value, { cause });
249            }
250            return write.readString8();
251        }
252
253        finally {
254            functionCleanupArena.free();
255
256            diplomatReceive.free();
257
258            write.free();
259        }
260    }
261
262    toString() {
263        const write = new diplomatRuntime.DiplomatWriteBuf(wasm);
264        wasm.icu4x_Locale_to_string_mv1(this.ffiValue, write.buffer);
265
266        try {
267            return write.readString8();
268        }
269
270        finally {
271            write.free();
272        }
273    }
274
275    normalizingEq(other) {
276        let functionCleanupArena = new diplomatRuntime.CleanupArena();
277
278        const otherSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, other));
279
280        const result = wasm.icu4x_Locale_normalizing_eq_mv1(this.ffiValue, ...otherSlice.splat());
281
282        try {
283            return result;
284        }
285
286        finally {
287            functionCleanupArena.free();
288        }
289    }
290
291    compareToString(other) {
292        let functionCleanupArena = new diplomatRuntime.CleanupArena();
293
294        const otherSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, other));
295
296        const result = wasm.icu4x_Locale_compare_to_string_mv1(this.ffiValue, ...otherSlice.splat());
297
298        try {
299            return result;
300        }
301
302        finally {
303            functionCleanupArena.free();
304        }
305    }
306
307    compareTo(other) {
308        const result = wasm.icu4x_Locale_compare_to_mv1(this.ffiValue, other.ffiValue);
309
310        try {
311            return result;
312        }
313
314        finally {}
315    }
316
317    constructor(symbol, ptr, selfEdge) {
318        return this.#internalConstructor(...arguments)
319    }
320}