• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// generated by diplomat-tool
2import { BidiInfo } from "./BidiInfo.mjs"
3import { DataError } from "./DataError.mjs"
4import { DataProvider } from "./DataProvider.mjs"
5import { ReorderedIndexMap } from "./ReorderedIndexMap.mjs"
6import wasm from "./diplomat-wasm.mjs";
7import * as diplomatRuntime from "./diplomat-runtime.mjs";
8
9
10/** An ICU4X Bidi object, containing loaded bidi data
11*
12*See the [Rust documentation for `BidiClassAdapter`](https://docs.rs/icu/latest/icu/properties/bidi/struct.BidiClassAdapter.html) for more information.
13*
14*See the [Rust documentation for `BidiClass`](https://docs.rs/icu/latest/icu/properties/props/struct.BidiClass.html) for more information.
15*/
16const Bidi_box_destroy_registry = new FinalizationRegistry((ptr) => {
17    wasm.icu4x_Bidi_destroy_mv1(ptr);
18});
19
20export class Bidi {
21
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("Bidi is an Opaque type. You cannot call its constructor.");
32            return;
33        }
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            Bidi_box_destroy_registry.register(this, this.#ptr);
41        }
42
43        return this;
44    }
45    get ffiValue() {
46        return this.#ptr;
47    }
48
49    #defaultConstructor() {
50        const result = wasm.icu4x_Bidi_create_mv1();
51
52        try {
53            return new Bidi(diplomatRuntime.internalConstructor, result, []);
54        }
55
56        finally {}
57    }
58
59    static createWithProvider(provider) {
60        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
61
62        const result = wasm.icu4x_Bidi_create_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue);
63
64        try {
65            if (!diplomatReceive.resultFlag) {
66                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
67                throw new globalThis.Error('DataError: ' + cause.value, { cause });
68            }
69            return new Bidi(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
70        }
71
72        finally {
73            diplomatReceive.free();
74        }
75    }
76
77    forText(text, defaultLevel) {
78        let functionGarbageCollectorGrip = new diplomatRuntime.GarbageCollectorGrip();
79        const textSlice = functionGarbageCollectorGrip.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, text));
80
81        // This lifetime edge depends on lifetimes 'text
82        let textEdges = [textSlice];
83
84        const result = wasm.icu4x_Bidi_for_text_valid_utf8_mv1(this.ffiValue, ...textSlice.splat(), ...diplomatRuntime.optionToArgsForCalling(defaultLevel, 1, 1, false, (arrayBuffer, offset, jsValue) => [diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue, Uint8Array)]));
85
86        try {
87            return new BidiInfo(diplomatRuntime.internalConstructor, result, [], textEdges);
88        }
89
90        finally {
91            functionGarbageCollectorGrip.releaseToGarbageCollector();
92        }
93    }
94
95    reorderVisual(levels) {
96        let functionCleanupArena = new diplomatRuntime.CleanupArena();
97
98        const levelsSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.slice(wasm, levels, "u8"));
99
100        const result = wasm.icu4x_Bidi_reorder_visual_mv1(this.ffiValue, ...levelsSlice.splat());
101
102        try {
103            return new ReorderedIndexMap(diplomatRuntime.internalConstructor, result, []);
104        }
105
106        finally {
107            functionCleanupArena.free();
108        }
109    }
110
111    static levelIsRtl(level) {
112        const result = wasm.icu4x_Bidi_level_is_rtl_mv1(level);
113
114        try {
115            return result;
116        }
117
118        finally {}
119    }
120
121    static levelIsLtr(level) {
122        const result = wasm.icu4x_Bidi_level_is_ltr_mv1(level);
123
124        try {
125            return result;
126        }
127
128        finally {}
129    }
130
131    static levelRtl() {
132        const result = wasm.icu4x_Bidi_level_rtl_mv1();
133
134        try {
135            return result;
136        }
137
138        finally {}
139    }
140
141    static levelLtr() {
142        const result = wasm.icu4x_Bidi_level_ltr_mv1();
143
144        try {
145            return result;
146        }
147
148        finally {}
149    }
150
151    constructor() {
152        if (arguments[0] === diplomatRuntime.exposeConstructor) {
153            return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1));
154        } else if (arguments[0] === diplomatRuntime.internalConstructor) {
155            return this.#internalConstructor(...arguments);
156        } else {
157            return this.#defaultConstructor(...arguments);
158        }
159    }
160}