BidiInfo.mjs (2920B)
1 // generated by diplomat-tool 2 import { BidiParagraph } from "./BidiParagraph.mjs" 3 import wasm from "./diplomat-wasm.mjs"; 4 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 5 6 7 /** 8 * An object containing bidi information for a given string, produced by `for_text()` on `Bidi` 9 * 10 * See the [Rust documentation for `BidiInfo`](https://docs.rs/unicode_bidi/latest/unicode_bidi/struct.BidiInfo.html) for more information. 11 */ 12 const BidiInfo_box_destroy_registry = new FinalizationRegistry((ptr) => { 13 wasm.icu4x_BidiInfo_destroy_mv1(ptr); 14 }); 15 16 export class BidiInfo { 17 // Internal ptr reference: 18 #ptr = null; 19 20 // Lifetimes are only to keep dependencies alive. 21 // Since JS won't garbage collect until there are no incoming edges. 22 #selfEdge = []; 23 #textEdge = []; 24 25 #internalConstructor(symbol, ptr, selfEdge, textEdge) { 26 if (symbol !== diplomatRuntime.internalConstructor) { 27 console.error("BidiInfo is an Opaque type. You cannot call its constructor."); 28 return; 29 } 30 this.#textEdge = textEdge; 31 this.#ptr = ptr; 32 this.#selfEdge = selfEdge; 33 34 // Are we being borrowed? If not, we can register. 35 if (this.#selfEdge.length === 0) { 36 BidiInfo_box_destroy_registry.register(this, this.#ptr); 37 } 38 39 return this; 40 } 41 get ffiValue() { 42 return this.#ptr; 43 } 44 45 46 /** 47 * The number of paragraphs contained here 48 */ 49 get paragraphCount() { 50 51 const result = wasm.icu4x_BidiInfo_paragraph_count_mv1(this.ffiValue); 52 53 try { 54 return result; 55 } 56 57 finally { 58 } 59 } 60 61 /** 62 * Get the nth paragraph, returning `None` if out of bounds 63 */ 64 paragraphAt(n) { 65 // This lifetime edge depends on lifetimes 'text 66 let textEdges = [this]; 67 68 69 const result = wasm.icu4x_BidiInfo_paragraph_at_mv1(this.ffiValue, n); 70 71 try { 72 return result === 0 ? null : new BidiParagraph(diplomatRuntime.internalConstructor, result, [], textEdges); 73 } 74 75 finally { 76 } 77 } 78 79 /** 80 * The number of bytes in this full text 81 */ 82 get size() { 83 84 const result = wasm.icu4x_BidiInfo_size_mv1(this.ffiValue); 85 86 try { 87 return result; 88 } 89 90 finally { 91 } 92 } 93 94 /** 95 * Get the BIDI level at a particular byte index in the full text. 96 * This integer is conceptually a `unicode_bidi::Level`, 97 * and can be further inspected using the static methods on Bidi. 98 * 99 * Returns 0 (equivalent to `Level::ltr()`) on error 100 */ 101 levelAt(pos) { 102 103 const result = wasm.icu4x_BidiInfo_level_at_mv1(this.ffiValue, pos); 104 105 try { 106 return result; 107 } 108 109 finally { 110 } 111 } 112 113 constructor(symbol, ptr, selfEdge, textEdge) { 114 return this.#internalConstructor(...arguments) 115 } 116 }