tor-browser

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

WindowsParser.mjs (4269B)


      1 // generated by diplomat-tool
      2 import { DataError } from "./DataError.mjs"
      3 import { DataProvider } from "./DataProvider.mjs"
      4 import { TimeZone } from "./TimeZone.mjs"
      5 import wasm from "./diplomat-wasm.mjs";
      6 import * as diplomatRuntime from "./diplomat-runtime.mjs";
      7 
      8 
      9 /**
     10 * A mapper between Windows time zone identifiers and BCP-47 time zone identifiers.
     11 *
     12 * This mapper supports two-way mapping, but it is optimized for the case of Windows to BCP-47.
     13 * It also supports normalizing and canonicalizing the Windows strings.
     14 *
     15 * See the [Rust documentation for `WindowsParser`](https://docs.rs/icu/latest/icu/time/zone/windows/struct.WindowsParser.html) for more information.
     16 */
     17 const WindowsParser_box_destroy_registry = new FinalizationRegistry((ptr) => {
     18    wasm.icu4x_WindowsParser_destroy_mv1(ptr);
     19 });
     20 
     21 export class WindowsParser {
     22    // Internal ptr reference:
     23    #ptr = null;
     24 
     25    // Lifetimes are only to keep dependencies alive.
     26    // Since JS won't garbage collect until there are no incoming edges.
     27    #selfEdge = [];
     28 
     29    #internalConstructor(symbol, ptr, selfEdge) {
     30        if (symbol !== diplomatRuntime.internalConstructor) {
     31            console.error("WindowsParser is an Opaque type. You cannot call its constructor.");
     32            return;
     33        }
     34        this.#ptr = ptr;
     35        this.#selfEdge = selfEdge;
     36 
     37        // Are we being borrowed? If not, we can register.
     38        if (this.#selfEdge.length === 0) {
     39            WindowsParser_box_destroy_registry.register(this, this.#ptr);
     40        }
     41 
     42        return this;
     43    }
     44    get ffiValue() {
     45        return this.#ptr;
     46    }
     47 
     48 
     49    /**
     50     * Create a new [`WindowsParser`] using compiled data
     51     *
     52     * See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/time/zone/windows/struct.WindowsParser.html#method.new) for more information.
     53     */
     54    #defaultConstructor() {
     55 
     56        const result = wasm.icu4x_WindowsParser_create_mv1();
     57 
     58        try {
     59            return new WindowsParser(diplomatRuntime.internalConstructor, result, []);
     60        }
     61 
     62        finally {
     63        }
     64    }
     65 
     66    /**
     67     * Create a new [`WindowsParser`] using a particular data source
     68     *
     69     * See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/time/zone/windows/struct.WindowsParser.html#method.new) for more information.
     70     */
     71    static createWithProvider(provider) {
     72        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
     73 
     74 
     75        const result = wasm.icu4x_WindowsParser_create_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue);
     76 
     77        try {
     78            if (!diplomatReceive.resultFlag) {
     79                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
     80                throw new globalThis.Error('DataError: ' + cause.value, { cause });
     81            }
     82            return new WindowsParser(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
     83        }
     84 
     85        finally {
     86            diplomatReceive.free();
     87        }
     88    }
     89 
     90    /**
     91     * See the [Rust documentation for `parse`](https://docs.rs/icu/latest/icu/time/zone/windows/struct.WindowsParserBorrowed.html#method.parse) for more information.
     92     */
     93    parse(value, region) {
     94        let functionCleanupArena = new diplomatRuntime.CleanupArena();
     95 
     96        const valueSlice = diplomatRuntime.DiplomatBuf.str8(wasm, value);
     97        const regionSlice = diplomatRuntime.DiplomatBuf.str8(wasm, region);
     98 
     99        const result = wasm.icu4x_WindowsParser_parse_mv1(this.ffiValue, ...valueSlice.splat(), ...regionSlice.splat());
    100 
    101        try {
    102            return result === 0 ? null : new TimeZone(diplomatRuntime.internalConstructor, result, []);
    103        }
    104 
    105        finally {
    106            functionCleanupArena.free();
    107 
    108        }
    109    }
    110 
    111    constructor() {
    112        if (arguments[0] === diplomatRuntime.exposeConstructor) {
    113            return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1));
    114        } else if (arguments[0] === diplomatRuntime.internalConstructor) {
    115            return this.#internalConstructor(...arguments);
    116        } else {
    117            return this.#defaultConstructor(...arguments);
    118        }
    119    }
    120 }