CodePointRangeIteratorResult.mjs (4125B)
1 // generated by diplomat-tool 2 import wasm from "./diplomat-wasm.mjs"; 3 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 4 5 6 /** 7 * Result of a single iteration of [`CodePointRangeIterator`]. 8 * Logically can be considered to be an `Option<RangeInclusive<DiplomatChar>>`, 9 * 10 * `start` and `end` represent an inclusive range of code points [start, end], 11 * and `done` will be true if the iterator has already finished. The last contentful 12 * iteration will NOT produce a range done=true, in other words `start` and `end` are useful 13 * values if and only if `done=false`. 14 */ 15 16 17 export class CodePointRangeIteratorResult { 18 #start; 19 get start() { 20 return this.#start; 21 } 22 #end; 23 get end() { 24 return this.#end; 25 } 26 #done; 27 get done() { 28 return this.#done; 29 } 30 #internalConstructor(structObj, internalConstructor) { 31 if (typeof structObj !== "object") { 32 throw new Error("CodePointRangeIteratorResult's constructor takes an object of CodePointRangeIteratorResult's fields."); 33 } 34 35 if (internalConstructor !== diplomatRuntime.internalConstructor) { 36 throw new Error("CodePointRangeIteratorResult is an out struct and can only be created internally."); 37 } 38 if ("start" in structObj) { 39 this.#start = structObj.start; 40 } else { 41 throw new Error("Missing required field start."); 42 } 43 44 if ("end" in structObj) { 45 this.#end = structObj.end; 46 } else { 47 throw new Error("Missing required field end."); 48 } 49 50 if ("done" in structObj) { 51 this.#done = structObj.done; 52 } else { 53 throw new Error("Missing required field done."); 54 } 55 56 return this; 57 } 58 59 // Return this struct in FFI function friendly format. 60 // Returns an array that can be expanded with spread syntax (...) 61 _intoFFI( 62 functionCleanupArena, 63 appendArrayMap 64 ) { 65 return [this.#start, this.#end, this.#done, /* [3 x i8] padding */ 0, 0, 0 /* end padding */] 66 } 67 68 static _fromSuppliedValue(internalConstructor, obj) { 69 if (internalConstructor !== diplomatRuntime.internalConstructor) { 70 throw new Error("_fromSuppliedValue cannot be called externally."); 71 } 72 73 if (obj instanceof CodePointRangeIteratorResult) { 74 return obj; 75 } 76 77 return CodePointRangeIteratorResult.fromFields(obj); 78 } 79 80 _writeToArrayBuffer( 81 arrayBuffer, 82 offset, 83 functionCleanupArena, 84 appendArrayMap 85 ) { 86 diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, this.#start, Uint32Array); 87 diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 4, this.#end, Uint32Array); 88 diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 8, this.#done, Uint8Array); 89 } 90 91 // This struct contains borrowed fields, so this takes in a list of 92 // "edges" corresponding to where each lifetime's data may have been borrowed from 93 // and passes it down to individual fields containing the borrow. 94 // This method does not attempt to handle any dependencies between lifetimes, the caller 95 // should handle this when constructing edge arrays. 96 static _fromFFI(internalConstructor, ptr) { 97 if (internalConstructor !== diplomatRuntime.internalConstructor) { 98 throw new Error("CodePointRangeIteratorResult._fromFFI is not meant to be called externally. Please use the default constructor."); 99 } 100 let structObj = {}; 101 const startDeref = (new Uint32Array(wasm.memory.buffer, ptr, 1))[0]; 102 structObj.start = startDeref; 103 const endDeref = (new Uint32Array(wasm.memory.buffer, ptr + 4, 1))[0]; 104 structObj.end = endDeref; 105 const doneDeref = (new Uint8Array(wasm.memory.buffer, ptr + 8, 1))[0] === 1; 106 structObj.done = doneDeref; 107 108 return new CodePointRangeIteratorResult(structObj, internalConstructor); 109 } 110 111 112 constructor(structObj, internalConstructor) { 113 return this.#internalConstructor(...arguments) 114 } 115 }