• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// generated by diplomat-tool
2import { CollatorOptions } from "./CollatorOptions.mjs"
3import { CollatorResolvedOptions } from "./CollatorResolvedOptions.mjs"
4import { DataError } from "./DataError.mjs"
5import { DataProvider } from "./DataProvider.mjs"
6import { Locale } from "./Locale.mjs"
7import wasm from "./diplomat-wasm.mjs";
8import * as diplomatRuntime from "./diplomat-runtime.mjs";
9
10
11/** See the [Rust documentation for `Collator`](https://docs.rs/icu/latest/icu/collator/struct.Collator.html) for more information.
12*/
13const Collator_box_destroy_registry = new FinalizationRegistry((ptr) => {
14    wasm.icu4x_Collator_destroy_mv1(ptr);
15});
16
17export class Collator {
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("Collator 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            Collator_box_destroy_registry.register(this, this.#ptr);
38        }
39
40        return this;
41    }
42    get ffiValue() {
43        return this.#ptr;
44    }
45
46    static create(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_Collator_create_v1_mv1(diplomatReceive.buffer, locale.ffiValue, ...CollatorOptions._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 Collator(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
59        }
60
61        finally {
62            functionCleanupArena.free();
63
64            diplomatReceive.free();
65        }
66    }
67
68    #defaultConstructor(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_Collator_create_v1_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue, locale.ffiValue, ...CollatorOptions._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 Collator(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
81        }
82
83        finally {
84            functionCleanupArena.free();
85
86            diplomatReceive.free();
87        }
88    }
89
90    compare(left, right) {
91        let functionCleanupArena = new diplomatRuntime.CleanupArena();
92
93        const leftSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str16(wasm, left));
94
95        const rightSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str16(wasm, right));
96
97        const result = wasm.icu4x_Collator_compare_utf16_mv1(this.ffiValue, ...leftSlice.splat(), ...rightSlice.splat());
98
99        try {
100            return result;
101        }
102
103        finally {
104            functionCleanupArena.free();
105        }
106    }
107
108    get resolvedOptions() {
109        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 28, 4, false);
110
111        const result = wasm.icu4x_Collator_resolved_options_v1_mv1(diplomatReceive.buffer, this.ffiValue);
112
113        try {
114            return CollatorResolvedOptions._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer);
115        }
116
117        finally {
118            diplomatReceive.free();
119        }
120    }
121
122    constructor(provider, locale, options) {
123        if (arguments[0] === diplomatRuntime.exposeConstructor) {
124            return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1));
125        } else if (arguments[0] === diplomatRuntime.internalConstructor) {
126            return this.#internalConstructor(...arguments);
127        } else {
128            return this.#defaultConstructor(...arguments);
129        }
130    }
131}