Bidi.mjs (7081B)
1 // generated by diplomat-tool 2 import { BidiInfo } from "./BidiInfo.mjs" 3 import { DataError } from "./DataError.mjs" 4 import { DataProvider } from "./DataProvider.mjs" 5 import { ReorderedIndexMap } from "./ReorderedIndexMap.mjs" 6 import wasm from "./diplomat-wasm.mjs"; 7 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 8 9 10 /** 11 * An ICU4X Bidi object, containing loaded bidi data 12 * 13 * See the [Rust documentation for `BidiClass`](https://docs.rs/icu/latest/icu/properties/props/struct.BidiClass.html) for more information. 14 */ 15 const Bidi_box_destroy_registry = new FinalizationRegistry((ptr) => { 16 wasm.icu4x_Bidi_destroy_mv1(ptr); 17 }); 18 19 export class Bidi { 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("Bidi is an Opaque type. You cannot call its constructor."); 30 return; 31 } 32 this.#ptr = ptr; 33 this.#selfEdge = selfEdge; 34 35 // Are we being borrowed? If not, we can register. 36 if (this.#selfEdge.length === 0) { 37 Bidi_box_destroy_registry.register(this, this.#ptr); 38 } 39 40 return this; 41 } 42 get ffiValue() { 43 return this.#ptr; 44 } 45 46 47 /** 48 * Creates a new [`Bidi`] from locale data using compiled data. 49 */ 50 #defaultConstructor() { 51 52 const result = wasm.icu4x_Bidi_create_mv1(); 53 54 try { 55 return new Bidi(diplomatRuntime.internalConstructor, result, []); 56 } 57 58 finally { 59 } 60 } 61 62 /** 63 * Creates a new [`Bidi`] from locale data, and a particular data source. 64 */ 65 static createWithProvider(provider) { 66 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 67 68 69 const result = wasm.icu4x_Bidi_create_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue); 70 71 try { 72 if (!diplomatReceive.resultFlag) { 73 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 74 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 75 } 76 return new Bidi(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 77 } 78 79 finally { 80 diplomatReceive.free(); 81 } 82 } 83 84 /** 85 * Use the data loaded in this object to process a string and calculate bidi information 86 * 87 * Takes in a Level for the default level, if it is an invalid value it will default to LTR 88 * 89 * See the [Rust documentation for `new_with_data_source`](https://docs.rs/unicode_bidi/latest/unicode_bidi/struct.BidiInfo.html#method.new_with_data_source) for more information. 90 */ 91 forText(text, defaultLevel) { 92 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 93 94 let functionGarbageCollectorGrip = new diplomatRuntime.GarbageCollectorGrip(); 95 const textSlice = diplomatRuntime.DiplomatBuf.str8(wasm, text); 96 // This lifetime edge depends on lifetimes 'text 97 let textEdges = [textSlice]; 98 99 100 const result = wasm.icu4x_Bidi_for_text_valid_utf8_mv1(this.ffiValue, ...textSlice.splat(), ...diplomatRuntime.optionToArgsForCalling(defaultLevel, 1, 1, (arrayBuffer, offset, jsValue) => [diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue, Uint8Array)])); 101 102 try { 103 return new BidiInfo(diplomatRuntime.internalConstructor, result, [], textEdges); 104 } 105 106 finally { 107 functionCleanupArena.free(); 108 109 functionGarbageCollectorGrip.releaseToGarbageCollector(); 110 111 } 112 } 113 114 /** 115 * Utility function for producing reorderings given a list of levels 116 * 117 * Produces a map saying which visual index maps to which source index. 118 * 119 * The levels array must not have values greater than 126 (this is the 120 * Bidi maximum explicit depth plus one). 121 * Failure to follow this invariant may lead to incorrect results, 122 * but is still safe. 123 * 124 * See the [Rust documentation for `reorder_visual`](https://docs.rs/unicode_bidi/latest/unicode_bidi/struct.BidiInfo.html#method.reorder_visual) for more information. 125 */ 126 reorderVisual(levels) { 127 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 128 129 const levelsSlice = diplomatRuntime.DiplomatBuf.slice(wasm, levels, "u8"); 130 131 const result = wasm.icu4x_Bidi_reorder_visual_mv1(this.ffiValue, ...levelsSlice.splat()); 132 133 try { 134 return new ReorderedIndexMap(diplomatRuntime.internalConstructor, result, []); 135 } 136 137 finally { 138 functionCleanupArena.free(); 139 140 } 141 } 142 143 /** 144 * Check if a Level returned by level_at is an RTL level. 145 * 146 * Invalid levels (numbers greater than 125) will be assumed LTR 147 * 148 * See the [Rust documentation for `is_rtl`](https://docs.rs/unicode_bidi/latest/unicode_bidi/struct.Level.html#method.is_rtl) for more information. 149 */ 150 static levelIsRtl(level) { 151 152 const result = wasm.icu4x_Bidi_level_is_rtl_mv1(level); 153 154 try { 155 return result; 156 } 157 158 finally { 159 } 160 } 161 162 /** 163 * Check if a Level returned by level_at is an LTR level. 164 * 165 * Invalid levels (numbers greater than 125) will be assumed LTR 166 * 167 * See the [Rust documentation for `is_ltr`](https://docs.rs/unicode_bidi/latest/unicode_bidi/struct.Level.html#method.is_ltr) for more information. 168 */ 169 static levelIsLtr(level) { 170 171 const result = wasm.icu4x_Bidi_level_is_ltr_mv1(level); 172 173 try { 174 return result; 175 } 176 177 finally { 178 } 179 } 180 181 /** 182 * Get a basic RTL Level value 183 * 184 * See the [Rust documentation for `rtl`](https://docs.rs/unicode_bidi/latest/unicode_bidi/struct.Level.html#method.rtl) for more information. 185 */ 186 static levelRtl() { 187 188 const result = wasm.icu4x_Bidi_level_rtl_mv1(); 189 190 try { 191 return result; 192 } 193 194 finally { 195 } 196 } 197 198 /** 199 * Get a simple LTR Level value 200 * 201 * See the [Rust documentation for `ltr`](https://docs.rs/unicode_bidi/latest/unicode_bidi/struct.Level.html#method.ltr) for more information. 202 */ 203 static levelLtr() { 204 205 const result = wasm.icu4x_Bidi_level_ltr_mv1(); 206 207 try { 208 return result; 209 } 210 211 finally { 212 } 213 } 214 215 constructor() { 216 if (arguments[0] === diplomatRuntime.exposeConstructor) { 217 return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1)); 218 } else if (arguments[0] === diplomatRuntime.internalConstructor) { 219 return this.#internalConstructor(...arguments); 220 } else { 221 return this.#defaultConstructor(...arguments); 222 } 223 } 224 }