• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// generated by diplomat-tool
2import { DataError } from "./DataError.mjs"
3import { DataProvider } from "./DataProvider.mjs"
4import { Locale } from "./Locale.mjs"
5import { WordBreakIteratorUtf16 } from "./WordBreakIteratorUtf16.mjs"
6import wasm from "./diplomat-wasm.mjs";
7import * as diplomatRuntime from "./diplomat-runtime.mjs";
8
9
10/** An ICU4X word-break segmenter, capable of finding word breakpoints in strings.
11*
12*See the [Rust documentation for `WordSegmenter`](https://docs.rs/icu/latest/icu/segmenter/struct.WordSegmenter.html) for more information.
13*/
14const WordSegmenter_box_destroy_registry = new FinalizationRegistry((ptr) => {
15    wasm.icu4x_WordSegmenter_destroy_mv1(ptr);
16});
17
18export class WordSegmenter {
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("WordSegmenter 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            WordSegmenter_box_destroy_registry.register(this, this.#ptr);
39        }
40
41        return this;
42    }
43    get ffiValue() {
44        return this.#ptr;
45    }
46
47    static createAuto() {
48        const result = wasm.icu4x_WordSegmenter_create_auto_mv1();
49
50        try {
51            return new WordSegmenter(diplomatRuntime.internalConstructor, result, []);
52        }
53
54        finally {}
55    }
56
57    static createAutoWithContentLocale(locale) {
58        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
59
60        const result = wasm.icu4x_WordSegmenter_create_auto_with_content_locale_mv1(diplomatReceive.buffer, locale.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 WordSegmenter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
68        }
69
70        finally {
71            diplomatReceive.free();
72        }
73    }
74
75    static createAutoWithContentLocaleAndProvider(provider, locale) {
76        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
77
78        const result = wasm.icu4x_WordSegmenter_create_auto_with_content_locale_and_provider_mv1(diplomatReceive.buffer, provider.ffiValue, locale.ffiValue);
79
80        try {
81            if (!diplomatReceive.resultFlag) {
82                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
83                throw new globalThis.Error('DataError: ' + cause.value, { cause });
84            }
85            return new WordSegmenter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
86        }
87
88        finally {
89            diplomatReceive.free();
90        }
91    }
92
93    static createLstm() {
94        const result = wasm.icu4x_WordSegmenter_create_lstm_mv1();
95
96        try {
97            return new WordSegmenter(diplomatRuntime.internalConstructor, result, []);
98        }
99
100        finally {}
101    }
102
103    static createLstmWithContentLocale(locale) {
104        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
105
106        const result = wasm.icu4x_WordSegmenter_create_lstm_with_content_locale_mv1(diplomatReceive.buffer, locale.ffiValue);
107
108        try {
109            if (!diplomatReceive.resultFlag) {
110                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
111                throw new globalThis.Error('DataError: ' + cause.value, { cause });
112            }
113            return new WordSegmenter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
114        }
115
116        finally {
117            diplomatReceive.free();
118        }
119    }
120
121    static createLstmWithContentLocaleAndProvider(provider, locale) {
122        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
123
124        const result = wasm.icu4x_WordSegmenter_create_lstm_with_content_locale_and_provider_mv1(diplomatReceive.buffer, provider.ffiValue, locale.ffiValue);
125
126        try {
127            if (!diplomatReceive.resultFlag) {
128                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
129                throw new globalThis.Error('DataError: ' + cause.value, { cause });
130            }
131            return new WordSegmenter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
132        }
133
134        finally {
135            diplomatReceive.free();
136        }
137    }
138
139    static createDictionary() {
140        const result = wasm.icu4x_WordSegmenter_create_dictionary_mv1();
141
142        try {
143            return new WordSegmenter(diplomatRuntime.internalConstructor, result, []);
144        }
145
146        finally {}
147    }
148
149    static createDictionaryWithContentLocale(locale) {
150        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
151
152        const result = wasm.icu4x_WordSegmenter_create_dictionary_with_content_locale_mv1(diplomatReceive.buffer, locale.ffiValue);
153
154        try {
155            if (!diplomatReceive.resultFlag) {
156                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
157                throw new globalThis.Error('DataError: ' + cause.value, { cause });
158            }
159            return new WordSegmenter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
160        }
161
162        finally {
163            diplomatReceive.free();
164        }
165    }
166
167    static createDictionaryWithContentLocaleAndProvider(provider, locale) {
168        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
169
170        const result = wasm.icu4x_WordSegmenter_create_dictionary_with_content_locale_and_provider_mv1(diplomatReceive.buffer, provider.ffiValue, locale.ffiValue);
171
172        try {
173            if (!diplomatReceive.resultFlag) {
174                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
175                throw new globalThis.Error('DataError: ' + cause.value, { cause });
176            }
177            return new WordSegmenter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
178        }
179
180        finally {
181            diplomatReceive.free();
182        }
183    }
184
185    segment(input) {
186        let functionGarbageCollectorGrip = new diplomatRuntime.GarbageCollectorGrip();
187        const inputSlice = functionGarbageCollectorGrip.alloc(diplomatRuntime.DiplomatBuf.str16(wasm, input));
188
189        // This lifetime edge depends on lifetimes 'a
190        let aEdges = [this, inputSlice];
191
192        const result = wasm.icu4x_WordSegmenter_segment_utf16_mv1(this.ffiValue, ...inputSlice.splat());
193
194        try {
195            return new WordBreakIteratorUtf16(diplomatRuntime.internalConstructor, result, [], aEdges);
196        }
197
198        finally {
199            functionGarbageCollectorGrip.releaseToGarbageCollector();
200        }
201    }
202
203    constructor(symbol, ptr, selfEdge) {
204        return this.#internalConstructor(...arguments)
205    }
206}