tor-browser

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

UnitsConverterFactory.mjs (4517B)


      1 // generated by diplomat-tool
      2 import { DataError } from "./DataError.mjs"
      3 import { DataProvider } from "./DataProvider.mjs"
      4 import { MeasureUnit } from "./MeasureUnit.mjs"
      5 import { UnitsConverter } from "./UnitsConverter.mjs"
      6 import wasm from "./diplomat-wasm.mjs";
      7 import * as diplomatRuntime from "./diplomat-runtime.mjs";
      8 
      9 
     10 /**
     11 * An ICU4X Units Converter Factory object, capable of creating converters a [`UnitsConverter`]
     12 * for converting between two [`MeasureUnit`]s.
     13 *
     14 * Also, it can parse the CLDR unit identifier (e.g. `meter-per-square-second`) and get the [`MeasureUnit`].
     15 *
     16 * See the [Rust documentation for `ConverterFactory`](https://docs.rs/icu/latest/icu/experimental/units/converter_factory/struct.ConverterFactory.html) for more information.
     17 */
     18 const UnitsConverterFactory_box_destroy_registry = new FinalizationRegistry((ptr) => {
     19    wasm.icu4x_UnitsConverterFactory_destroy_mv1(ptr);
     20 });
     21 
     22 export class UnitsConverterFactory {
     23    // Internal ptr reference:
     24    #ptr = null;
     25 
     26    // Lifetimes are only to keep dependencies alive.
     27    // Since JS won't garbage collect until there are no incoming edges.
     28    #selfEdge = [];
     29 
     30    #internalConstructor(symbol, ptr, selfEdge) {
     31        if (symbol !== diplomatRuntime.internalConstructor) {
     32            console.error("UnitsConverterFactory is an Opaque type. You cannot call its constructor.");
     33            return;
     34        }
     35        this.#ptr = ptr;
     36        this.#selfEdge = selfEdge;
     37 
     38        // Are we being borrowed? If not, we can register.
     39        if (this.#selfEdge.length === 0) {
     40            UnitsConverterFactory_box_destroy_registry.register(this, this.#ptr);
     41        }
     42 
     43        return this;
     44    }
     45    get ffiValue() {
     46        return this.#ptr;
     47    }
     48 
     49 
     50    /**
     51     * Construct a new [`UnitsConverterFactory`] instance using compiled data.
     52     *
     53     * See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/experimental/units/converter_factory/struct.ConverterFactory.html#method.new) for more information.
     54     */
     55    #defaultConstructor() {
     56 
     57        const result = wasm.icu4x_UnitsConverterFactory_create_mv1();
     58 
     59        try {
     60            return new UnitsConverterFactory(diplomatRuntime.internalConstructor, result, []);
     61        }
     62 
     63        finally {
     64        }
     65    }
     66 
     67    /**
     68     * Construct a new [`UnitsConverterFactory`] instance using a particular data source.
     69     *
     70     * See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/experimental/units/converter_factory/struct.ConverterFactory.html#method.new) for more information.
     71     */
     72    static createWithProvider(provider) {
     73        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
     74 
     75 
     76        const result = wasm.icu4x_UnitsConverterFactory_create_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue);
     77 
     78        try {
     79            if (!diplomatReceive.resultFlag) {
     80                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
     81                throw new globalThis.Error('DataError: ' + cause.value, { cause });
     82            }
     83            return new UnitsConverterFactory(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
     84        }
     85 
     86        finally {
     87            diplomatReceive.free();
     88        }
     89    }
     90 
     91    /**
     92     * Creates a new [`UnitsConverter`] from the input and output [`MeasureUnit`]s.
     93     * Returns nothing if the conversion between the two units is not possible.
     94     * For example, conversion between `meter` and `second` is not possible.
     95     *
     96     * See the [Rust documentation for `converter`](https://docs.rs/icu/latest/icu/experimental/units/converter_factory/struct.ConverterFactory.html#method.converter) for more information.
     97     */
     98    converter(from, to) {
     99 
    100        const result = wasm.icu4x_UnitsConverterFactory_converter_mv1(this.ffiValue, from.ffiValue, to.ffiValue);
    101 
    102        try {
    103            return result === 0 ? null : new UnitsConverter(diplomatRuntime.internalConstructor, result, []);
    104        }
    105 
    106        finally {
    107        }
    108    }
    109 
    110    constructor() {
    111        if (arguments[0] === diplomatRuntime.exposeConstructor) {
    112            return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1));
    113        } else if (arguments[0] === diplomatRuntime.internalConstructor) {
    114            return this.#internalConstructor(...arguments);
    115        } else {
    116            return this.#defaultConstructor(...arguments);
    117        }
    118    }
    119 }