ReorderedIndexMap.mjs (2778B)
1 // generated by diplomat-tool 2 import wasm from "./diplomat-wasm.mjs"; 3 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 4 5 6 /** 7 * Thin wrapper around a vector that maps visual indices to source indices 8 * 9 * `map[visualIndex] = sourceIndex` 10 * 11 * Produced by `reorder_visual()` on [`Bidi`]. 12 */ 13 const ReorderedIndexMap_box_destroy_registry = new FinalizationRegistry((ptr) => { 14 wasm.icu4x_ReorderedIndexMap_destroy_mv1(ptr); 15 }); 16 17 export class ReorderedIndexMap { 18 // Internal ptr reference: 19 #ptr = null; 20 21 // Lifetimes are only to keep dependencies alive. 22 // Since JS won't garbage collect until there are no incoming edges. 23 #selfEdge = []; 24 25 #internalConstructor(symbol, ptr, selfEdge) { 26 if (symbol !== diplomatRuntime.internalConstructor) { 27 console.error("ReorderedIndexMap is an Opaque type. You cannot call its constructor."); 28 return; 29 } 30 this.#ptr = ptr; 31 this.#selfEdge = selfEdge; 32 33 // Are we being borrowed? If not, we can register. 34 if (this.#selfEdge.length === 0) { 35 ReorderedIndexMap_box_destroy_registry.register(this, this.#ptr); 36 } 37 38 return this; 39 } 40 get ffiValue() { 41 return this.#ptr; 42 } 43 44 45 /** 46 * Get this as a slice/array of indices 47 */ 48 get asSlice() { 49 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 8, 4, false); 50 51 // This lifetime edge depends on lifetimes 'a 52 let aEdges = [this]; 53 54 55 const result = wasm.icu4x_ReorderedIndexMap_as_slice_mv1(diplomatReceive.buffer, this.ffiValue); 56 57 try { 58 return Array.from(new diplomatRuntime.DiplomatSlicePrimitive(wasm, diplomatReceive.buffer, "u32", aEdges).getValue()); 59 } 60 61 finally { 62 diplomatReceive.free(); 63 } 64 } 65 66 /** 67 * The length of this map 68 */ 69 get length() { 70 71 const result = wasm.icu4x_ReorderedIndexMap_len_mv1(this.ffiValue); 72 73 try { 74 return result; 75 } 76 77 finally { 78 } 79 } 80 81 /** 82 * Whether this map is empty 83 */ 84 get isEmpty() { 85 86 const result = wasm.icu4x_ReorderedIndexMap_is_empty_mv1(this.ffiValue); 87 88 try { 89 return result; 90 } 91 92 finally { 93 } 94 } 95 96 /** 97 * Get element at `index`. Returns 0 when out of bounds 98 * (note that 0 is also a valid in-bounds value, please use `len()` 99 * to avoid out-of-bounds) 100 */ 101 get(index) { 102 103 const result = wasm.icu4x_ReorderedIndexMap_get_mv1(this.ffiValue, index); 104 105 try { 106 return result; 107 } 108 109 finally { 110 } 111 } 112 113 constructor(symbol, ptr, selfEdge) { 114 return this.#internalConstructor(...arguments) 115 } 116 }