• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// generated by diplomat-tool
2import { DataError } from "./DataError.mjs"
3import { DataProvider } from "./DataProvider.mjs"
4import { GraphemeClusterBreakIteratorUtf16 } from "./GraphemeClusterBreakIteratorUtf16.mjs"
5import wasm from "./diplomat-wasm.mjs";
6import * as diplomatRuntime from "./diplomat-runtime.mjs";
7
8
9/** An ICU4X grapheme-cluster-break segmenter, capable of finding grapheme cluster breakpoints
10*in strings.
11*
12*See the [Rust documentation for `GraphemeClusterSegmenter`](https://docs.rs/icu/latest/icu/segmenter/struct.GraphemeClusterSegmenter.html) for more information.
13*/
14const GraphemeClusterSegmenter_box_destroy_registry = new FinalizationRegistry((ptr) => {
15    wasm.icu4x_GraphemeClusterSegmenter_destroy_mv1(ptr);
16});
17
18export class GraphemeClusterSegmenter {
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("GraphemeClusterSegmenter 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            GraphemeClusterSegmenter_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_GraphemeClusterSegmenter_create_mv1();
49
50        try {
51            return new GraphemeClusterSegmenter(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_GraphemeClusterSegmenter_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 GraphemeClusterSegmenter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
68        }
69
70        finally {
71            diplomatReceive.free();
72        }
73    }
74
75    segment(input) {
76        let functionGarbageCollectorGrip = new diplomatRuntime.GarbageCollectorGrip();
77        const inputSlice = functionGarbageCollectorGrip.alloc(diplomatRuntime.DiplomatBuf.str16(wasm, input));
78
79        // This lifetime edge depends on lifetimes 'a
80        let aEdges = [this, inputSlice];
81
82        const result = wasm.icu4x_GraphemeClusterSegmenter_segment_utf16_mv1(this.ffiValue, ...inputSlice.splat());
83
84        try {
85            return new GraphemeClusterBreakIteratorUtf16(diplomatRuntime.internalConstructor, result, [], aEdges);
86        }
87
88        finally {
89            functionGarbageCollectorGrip.releaseToGarbageCollector();
90        }
91    }
92
93    constructor() {
94        if (arguments[0] === diplomatRuntime.exposeConstructor) {
95            return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1));
96        } else if (arguments[0] === diplomatRuntime.internalConstructor) {
97            return this.#internalConstructor(...arguments);
98        } else {
99            return this.#defaultConstructor(...arguments);
100        }
101    }
102}