tor-browser

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

LocaleFallbacker.mjs (4839B)


      1 // generated by diplomat-tool
      2 import { DataError } from "./DataError.mjs"
      3 import { DataProvider } from "./DataProvider.mjs"
      4 import { LocaleFallbackConfig } from "./LocaleFallbackConfig.mjs"
      5 import { LocaleFallbackerWithConfig } from "./LocaleFallbackerWithConfig.mjs"
      6 import wasm from "./diplomat-wasm.mjs";
      7 import * as diplomatRuntime from "./diplomat-runtime.mjs";
      8 
      9 
     10 /**
     11 * An object that runs the ICU4X locale fallback algorithm.
     12 *
     13 * See the [Rust documentation for `LocaleFallbacker`](https://docs.rs/icu/latest/icu/locale/fallback/struct.LocaleFallbacker.html) for more information.
     14 */
     15 const LocaleFallbacker_box_destroy_registry = new FinalizationRegistry((ptr) => {
     16    wasm.icu4x_LocaleFallbacker_destroy_mv1(ptr);
     17 });
     18 
     19 export class LocaleFallbacker {
     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("LocaleFallbacker 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            LocaleFallbacker_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 `LocaleFallbacker` from compiled data.
     49     *
     50     * See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/locale/fallback/struct.LocaleFallbacker.html#method.new) for more information.
     51     */
     52    #defaultConstructor() {
     53 
     54        const result = wasm.icu4x_LocaleFallbacker_create_mv1();
     55 
     56        try {
     57            return new LocaleFallbacker(diplomatRuntime.internalConstructor, result, []);
     58        }
     59 
     60        finally {
     61        }
     62    }
     63 
     64    /**
     65     * Creates a new `LocaleFallbacker` from a data provider.
     66     *
     67     * See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/locale/fallback/struct.LocaleFallbacker.html#method.new) for more information.
     68     */
     69    static createWithProvider(provider) {
     70        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
     71 
     72 
     73        const result = wasm.icu4x_LocaleFallbacker_create_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue);
     74 
     75        try {
     76            if (!diplomatReceive.resultFlag) {
     77                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
     78                throw new globalThis.Error('DataError: ' + cause.value, { cause });
     79            }
     80            return new LocaleFallbacker(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
     81        }
     82 
     83        finally {
     84            diplomatReceive.free();
     85        }
     86    }
     87 
     88    /**
     89     * Creates a new `LocaleFallbacker` without data for limited functionality.
     90     *
     91     * See the [Rust documentation for `new_without_data`](https://docs.rs/icu/latest/icu/locale/fallback/struct.LocaleFallbacker.html#method.new_without_data) for more information.
     92     */
     93    static withoutData() {
     94 
     95        const result = wasm.icu4x_LocaleFallbacker_without_data_mv1();
     96 
     97        try {
     98            return new LocaleFallbacker(diplomatRuntime.internalConstructor, result, []);
     99        }
    100 
    101        finally {
    102        }
    103    }
    104 
    105    /**
    106     * Associates this `LocaleFallbacker` with configuration options.
    107     *
    108     * See the [Rust documentation for `for_config`](https://docs.rs/icu/latest/icu/locale/fallback/struct.LocaleFallbacker.html#method.for_config) for more information.
    109     */
    110    forConfig(config) {
    111        let functionCleanupArena = new diplomatRuntime.CleanupArena();
    112 
    113        // This lifetime edge depends on lifetimes 'a
    114        let aEdges = [this];
    115 
    116 
    117        const result = wasm.icu4x_LocaleFallbacker_for_config_mv1(this.ffiValue, ...LocaleFallbackConfig._fromSuppliedValue(diplomatRuntime.internalConstructor, config)._intoFFI(functionCleanupArena, {}));
    118 
    119        try {
    120            return new LocaleFallbackerWithConfig(diplomatRuntime.internalConstructor, result, [], aEdges);
    121        }
    122 
    123        finally {
    124            functionCleanupArena.free();
    125 
    126        }
    127    }
    128 
    129    constructor() {
    130        if (arguments[0] === diplomatRuntime.exposeConstructor) {
    131            return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1));
    132        } else if (arguments[0] === diplomatRuntime.internalConstructor) {
    133            return this.#internalConstructor(...arguments);
    134        } else {
    135            return this.#defaultConstructor(...arguments);
    136        }
    137    }
    138 }