• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// generated by diplomat-tool
2import { MeasureUnit } from "./MeasureUnit.mjs"
3import wasm from "./diplomat-wasm.mjs";
4import * as diplomatRuntime from "./diplomat-runtime.mjs";
5
6
7/** An ICU4X Measurement Unit parser object which is capable of parsing the CLDR unit identifier
8*(e.g. `meter-per-square-second`) and get the [`MeasureUnit`].
9*
10*See the [Rust documentation for `MeasureUnitParser`](https://docs.rs/icu/latest/icu/experimental/measure/parser/struct.MeasureUnitParser.html) for more information.
11*/
12const MeasureUnitParser_box_destroy_registry = new FinalizationRegistry((ptr) => {
13    wasm.icu4x_MeasureUnitParser_destroy_mv1(ptr);
14});
15
16export class MeasureUnitParser {
17
18    // Internal ptr reference:
19    #ptr = null;
20
21    // Lifetimes are only to keep dependencies alive.
22    // Since JS won't garbage collect until there are no incoming edges.
23    #selfEdge = [];
24    #aEdge = [];
25
26    #internalConstructor(symbol, ptr, selfEdge, aEdge) {
27        if (symbol !== diplomatRuntime.internalConstructor) {
28            console.error("MeasureUnitParser is an Opaque type. You cannot call its constructor.");
29            return;
30        }
31
32
33        this.#aEdge = aEdge;
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            MeasureUnitParser_box_destroy_registry.register(this, this.#ptr);
41        }
42
43        return this;
44    }
45    get ffiValue() {
46        return this.#ptr;
47    }
48
49    parse(unitId) {
50        let functionCleanupArena = new diplomatRuntime.CleanupArena();
51
52        const unitIdSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, unitId));
53
54        const result = wasm.icu4x_MeasureUnitParser_parse_mv1(this.ffiValue, ...unitIdSlice.splat());
55
56        try {
57            return result === 0 ? null : new MeasureUnit(diplomatRuntime.internalConstructor, result, []);
58        }
59
60        finally {
61            functionCleanupArena.free();
62        }
63    }
64
65    constructor(symbol, ptr, selfEdge, aEdge) {
66        return this.#internalConstructor(...arguments)
67    }
68}