tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

Decomposed.mjs (3417B)


      1 // generated by diplomat-tool
      2 import wasm from "./diplomat-wasm.mjs";
      3 import * as diplomatRuntime from "./diplomat-runtime.mjs";
      4 
      5 
      6 /**
      7 * The outcome of non-recursive canonical decomposition of a character.
      8 * `second` will be NUL when the decomposition expands to a single character
      9 * (which may or may not be the original one)
     10 *
     11 * See the [Rust documentation for `Decomposed`](https://docs.rs/icu/latest/icu/normalizer/properties/enum.Decomposed.html) for more information.
     12 */
     13 
     14 
     15 export class Decomposed {
     16    #first;
     17    get first() {
     18        return this.#first;
     19    }
     20    #second;
     21    get second() {
     22        return this.#second;
     23    }
     24    #internalConstructor(structObj, internalConstructor) {
     25        if (typeof structObj !== "object") {
     26            throw new Error("Decomposed's constructor takes an object of Decomposed's fields.");
     27        }
     28 
     29        if (internalConstructor !== diplomatRuntime.internalConstructor) {
     30            throw new Error("Decomposed is an out struct and can only be created internally.");
     31        }
     32        if ("first" in structObj) {
     33            this.#first = structObj.first;
     34        } else {
     35            throw new Error("Missing required field first.");
     36        }
     37 
     38        if ("second" in structObj) {
     39            this.#second = structObj.second;
     40        } else {
     41            throw new Error("Missing required field second.");
     42        }
     43 
     44        return this;
     45    }
     46 
     47    // Return this struct in FFI function friendly format.
     48    // Returns an array that can be expanded with spread syntax (...)
     49    _intoFFI(
     50        functionCleanupArena,
     51        appendArrayMap
     52    ) {
     53        return [this.#first, this.#second]
     54    }
     55 
     56    static _fromSuppliedValue(internalConstructor, obj) {
     57        if (internalConstructor !== diplomatRuntime.internalConstructor) {
     58            throw new Error("_fromSuppliedValue cannot be called externally.");
     59        }
     60 
     61        if (obj instanceof Decomposed) {
     62            return obj;
     63        }
     64 
     65        return Decomposed.fromFields(obj);
     66    }
     67 
     68    _writeToArrayBuffer(
     69        arrayBuffer,
     70        offset,
     71        functionCleanupArena,
     72        appendArrayMap
     73    ) {
     74        diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, this.#first, Uint32Array);
     75        diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 4, this.#second, Uint32Array);
     76    }
     77 
     78    // This struct contains borrowed fields, so this takes in a list of
     79    // "edges" corresponding to where each lifetime's data may have been borrowed from
     80    // and passes it down to individual fields containing the borrow.
     81    // This method does not attempt to handle any dependencies between lifetimes, the caller
     82    // should handle this when constructing edge arrays.
     83    static _fromFFI(internalConstructor, ptr) {
     84        if (internalConstructor !== diplomatRuntime.internalConstructor) {
     85            throw new Error("Decomposed._fromFFI is not meant to be called externally. Please use the default constructor.");
     86        }
     87        let structObj = {};
     88        const firstDeref = (new Uint32Array(wasm.memory.buffer, ptr, 1))[0];
     89        structObj.first = firstDeref;
     90        const secondDeref = (new Uint32Array(wasm.memory.buffer, ptr + 4, 1))[0];
     91        structObj.second = secondDeref;
     92 
     93        return new Decomposed(structObj, internalConstructor);
     94    }
     95 
     96 
     97    constructor(structObj, internalConstructor) {
     98        return this.#internalConstructor(...arguments)
     99    }
    100 }