1// generated by diplomat-tool 2import { DataError } from "./DataError.mjs" 3import { DataProvider } from "./DataProvider.mjs" 4import { Locale } from "./Locale.mjs" 5import { SentenceBreakIteratorUtf16 } from "./SentenceBreakIteratorUtf16.mjs" 6import wasm from "./diplomat-wasm.mjs"; 7import * as diplomatRuntime from "./diplomat-runtime.mjs"; 8 9 10/** An ICU4X sentence-break segmenter, capable of finding sentence breakpoints in strings. 11* 12*See the [Rust documentation for `SentenceSegmenter`](https://docs.rs/icu/latest/icu/segmenter/struct.SentenceSegmenter.html) for more information. 13*/ 14const SentenceSegmenter_box_destroy_registry = new FinalizationRegistry((ptr) => { 15 wasm.icu4x_SentenceSegmenter_destroy_mv1(ptr); 16}); 17 18export class SentenceSegmenter { 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("SentenceSegmenter 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 SentenceSegmenter_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_SentenceSegmenter_create_mv1(); 49 50 try { 51 return new SentenceSegmenter(diplomatRuntime.internalConstructor, result, []); 52 } 53 54 finally {} 55 } 56 57 static createWithContentLocale(locale) { 58 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 59 60 const result = wasm.icu4x_SentenceSegmenter_create_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 SentenceSegmenter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 68 } 69 70 finally { 71 diplomatReceive.free(); 72 } 73 } 74 75 static createWithContentLocaleAndProvider(provider, locale) { 76 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 77 78 const result = wasm.icu4x_SentenceSegmenter_create_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 SentenceSegmenter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 86 } 87 88 finally { 89 diplomatReceive.free(); 90 } 91 } 92 93 segment(input) { 94 let functionGarbageCollectorGrip = new diplomatRuntime.GarbageCollectorGrip(); 95 const inputSlice = functionGarbageCollectorGrip.alloc(diplomatRuntime.DiplomatBuf.str16(wasm, input)); 96 97 // This lifetime edge depends on lifetimes 'a 98 let aEdges = [this, inputSlice]; 99 100 const result = wasm.icu4x_SentenceSegmenter_segment_utf16_mv1(this.ffiValue, ...inputSlice.splat()); 101 102 try { 103 return new SentenceBreakIteratorUtf16(diplomatRuntime.internalConstructor, result, [], aEdges); 104 } 105 106 finally { 107 functionGarbageCollectorGrip.releaseToGarbageCollector(); 108 } 109 } 110 111 constructor() { 112 if (arguments[0] === diplomatRuntime.exposeConstructor) { 113 return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1)); 114 } else if (arguments[0] === diplomatRuntime.internalConstructor) { 115 return this.#internalConstructor(...arguments); 116 } else { 117 return this.#defaultConstructor(...arguments); 118 } 119 } 120}