CanonicalDecomposition.mjs (4327B)
1 // generated by diplomat-tool 2 import { DataError } from "./DataError.mjs" 3 import { DataProvider } from "./DataProvider.mjs" 4 import { Decomposed } from "./Decomposed.mjs" 5 import wasm from "./diplomat-wasm.mjs"; 6 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 7 8 9 /** 10 * The raw (non-recursive) canonical decomposition operation. 11 * 12 * Callers should generally use DecomposingNormalizer unless they specifically need raw composition operations 13 * 14 * See the [Rust documentation for `CanonicalDecomposition`](https://docs.rs/icu/latest/icu/normalizer/properties/struct.CanonicalDecomposition.html) for more information. 15 */ 16 const CanonicalDecomposition_box_destroy_registry = new FinalizationRegistry((ptr) => { 17 wasm.icu4x_CanonicalDecomposition_destroy_mv1(ptr); 18 }); 19 20 export class CanonicalDecomposition { 21 // Internal ptr reference: 22 #ptr = null; 23 24 // Lifetimes are only to keep dependencies alive. 25 // Since JS won't garbage collect until there are no incoming edges. 26 #selfEdge = []; 27 28 #internalConstructor(symbol, ptr, selfEdge) { 29 if (symbol !== diplomatRuntime.internalConstructor) { 30 console.error("CanonicalDecomposition is an Opaque type. You cannot call its constructor."); 31 return; 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 CanonicalDecomposition_box_destroy_registry.register(this, this.#ptr); 39 } 40 41 return this; 42 } 43 get ffiValue() { 44 return this.#ptr; 45 } 46 47 48 /** 49 * Construct a new CanonicalDecomposition instance for NFC using compiled data. 50 * 51 * See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/normalizer/properties/struct.CanonicalDecomposition.html#method.new) for more information. 52 */ 53 #defaultConstructor() { 54 55 const result = wasm.icu4x_CanonicalDecomposition_create_mv1(); 56 57 try { 58 return new CanonicalDecomposition(diplomatRuntime.internalConstructor, result, []); 59 } 60 61 finally { 62 } 63 } 64 65 /** 66 * Construct a new CanonicalDecomposition instance for NFC using a particular data source. 67 * 68 * See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/normalizer/properties/struct.CanonicalDecomposition.html#method.new) for more information. 69 */ 70 static createWithProvider(provider) { 71 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 72 73 74 const result = wasm.icu4x_CanonicalDecomposition_create_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue); 75 76 try { 77 if (!diplomatReceive.resultFlag) { 78 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 79 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 80 } 81 return new CanonicalDecomposition(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 82 } 83 84 finally { 85 diplomatReceive.free(); 86 } 87 } 88 89 /** 90 * Performs non-recursive canonical decomposition (including for Hangul). 91 * 92 * See the [Rust documentation for `decompose`](https://docs.rs/icu/latest/icu/normalizer/properties/struct.CanonicalDecompositionBorrowed.html#method.decompose) for more information. 93 */ 94 decompose(c) { 95 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 8, 4, false); 96 97 98 const result = wasm.icu4x_CanonicalDecomposition_decompose_mv1(diplomatReceive.buffer, this.ffiValue, c); 99 100 try { 101 return Decomposed._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer); 102 } 103 104 finally { 105 diplomatReceive.free(); 106 } 107 } 108 109 constructor() { 110 if (arguments[0] === diplomatRuntime.exposeConstructor) { 111 return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1)); 112 } else if (arguments[0] === diplomatRuntime.internalConstructor) { 113 return this.#internalConstructor(...arguments); 114 } else { 115 return this.#defaultConstructor(...arguments); 116 } 117 } 118 }