• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// generated by diplomat-tool
2import { FixedDecimalLimitError } from "./FixedDecimalLimitError.mjs"
3import { FixedDecimalParseError } from "./FixedDecimalParseError.mjs"
4import { FixedDecimalRoundingIncrement } from "./FixedDecimalRoundingIncrement.mjs"
5import { FixedDecimalSign } from "./FixedDecimalSign.mjs"
6import { FixedDecimalSignDisplay } from "./FixedDecimalSignDisplay.mjs"
7import { FixedDecimalSignedRoundingMode } from "./FixedDecimalSignedRoundingMode.mjs"
8import wasm from "./diplomat-wasm.mjs";
9import * as diplomatRuntime from "./diplomat-runtime.mjs";
10
11
12/** See the [Rust documentation for `FixedDecimal`](https://docs.rs/fixed_decimal/latest/fixed_decimal/struct.FixedDecimal.html) for more information.
13*/
14const Decimal_box_destroy_registry = new FinalizationRegistry((ptr) => {
15    wasm.icu4x_Decimal_destroy_mv1(ptr);
16});
17
18export class Decimal {
19
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("Decimal is an Opaque type. You cannot call its constructor.");
30            return;
31        }
32
33        this.#ptr = ptr;
34        this.#selfEdge = selfEdge;
35
36        // Are we being borrowed? If not, we can register.
37        if (this.#selfEdge.length === 0) {
38            Decimal_box_destroy_registry.register(this, this.#ptr);
39        }
40
41        return this;
42    }
43    get ffiValue() {
44        return this.#ptr;
45    }
46
47    static fromNumber(v) {
48        const result = wasm.icu4x_Decimal_from_int32_mv1(v);
49
50        try {
51            return new Decimal(diplomatRuntime.internalConstructor, result, []);
52        }
53
54        finally {}
55    }
56
57    static fromBigInt(v) {
58        const result = wasm.icu4x_Decimal_from_int64_mv1(v);
59
60        try {
61            return new Decimal(diplomatRuntime.internalConstructor, result, []);
62        }
63
64        finally {}
65    }
66
67    static fromNumberWithLowerMagnitude(f, magnitude) {
68        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
69
70        const result = wasm.icu4x_Decimal_from_double_with_lower_magnitude_mv1(diplomatReceive.buffer, f, magnitude);
71
72        try {
73            if (!diplomatReceive.resultFlag) {
74                const cause = FixedDecimalLimitError.fromFields({}, diplomatRuntime.internalConstructor);
75                throw new globalThis.Error('FixedDecimalLimitError', { cause });
76            }
77            return new Decimal(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
78        }
79
80        finally {
81            diplomatReceive.free();
82        }
83    }
84
85    static fromNumberWithSignificantDigits(f, digits) {
86        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
87
88        const result = wasm.icu4x_Decimal_from_double_with_significant_digits_mv1(diplomatReceive.buffer, f, digits);
89
90        try {
91            if (!diplomatReceive.resultFlag) {
92                const cause = FixedDecimalLimitError.fromFields({}, diplomatRuntime.internalConstructor);
93                throw new globalThis.Error('FixedDecimalLimitError', { cause });
94            }
95            return new Decimal(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
96        }
97
98        finally {
99            diplomatReceive.free();
100        }
101    }
102
103    static fromNumberWithRoundTripPrecision(f) {
104        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
105
106        const result = wasm.icu4x_Decimal_from_double_with_round_trip_precision_mv1(diplomatReceive.buffer, f);
107
108        try {
109            if (!diplomatReceive.resultFlag) {
110                const cause = FixedDecimalLimitError.fromFields({}, diplomatRuntime.internalConstructor);
111                throw new globalThis.Error('FixedDecimalLimitError', { cause });
112            }
113            return new Decimal(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
114        }
115
116        finally {
117            diplomatReceive.free();
118        }
119    }
120
121    static fromString(v) {
122        let functionCleanupArena = new diplomatRuntime.CleanupArena();
123
124        const vSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, v));
125
126        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
127
128        const result = wasm.icu4x_Decimal_from_string_mv1(diplomatReceive.buffer, ...vSlice.splat());
129
130        try {
131            if (!diplomatReceive.resultFlag) {
132                const cause = new FixedDecimalParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
133                throw new globalThis.Error('FixedDecimalParseError: ' + cause.value, { cause });
134            }
135            return new Decimal(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
136        }
137
138        finally {
139            functionCleanupArena.free();
140
141            diplomatReceive.free();
142        }
143    }
144
145    digitAt(magnitude) {
146        const result = wasm.icu4x_Decimal_digit_at_mv1(this.ffiValue, magnitude);
147
148        try {
149            return result;
150        }
151
152        finally {}
153    }
154
155    get magnitudeStart() {
156        const result = wasm.icu4x_Decimal_magnitude_start_mv1(this.ffiValue);
157
158        try {
159            return result;
160        }
161
162        finally {}
163    }
164
165    get magnitudeEnd() {
166        const result = wasm.icu4x_Decimal_magnitude_end_mv1(this.ffiValue);
167
168        try {
169            return result;
170        }
171
172        finally {}
173    }
174
175    get nonzeroMagnitudeStart() {
176        const result = wasm.icu4x_Decimal_nonzero_magnitude_start_mv1(this.ffiValue);
177
178        try {
179            return result;
180        }
181
182        finally {}
183    }
184
185    get nonzeroMagnitudeEnd() {
186        const result = wasm.icu4x_Decimal_nonzero_magnitude_end_mv1(this.ffiValue);
187
188        try {
189            return result;
190        }
191
192        finally {}
193    }
194
195    get isZero() {
196        const result = wasm.icu4x_Decimal_is_zero_mv1(this.ffiValue);
197
198        try {
199            return result;
200        }
201
202        finally {}
203    }
204
205    multiplyPow10(power) {wasm.icu4x_Decimal_multiply_pow10_mv1(this.ffiValue, power);
206
207        try {}
208
209        finally {}
210    }
211
212    get sign() {
213        const result = wasm.icu4x_Decimal_sign_mv1(this.ffiValue);
214
215        try {
216            return new FixedDecimalSign(diplomatRuntime.internalConstructor, result);
217        }
218
219        finally {}
220    }
221
222    set sign(sign) {wasm.icu4x_Decimal_set_sign_mv1(this.ffiValue, sign.ffiValue);
223
224        try {}
225
226        finally {}
227    }
228
229    applySignDisplay(signDisplay) {wasm.icu4x_Decimal_apply_sign_display_mv1(this.ffiValue, signDisplay.ffiValue);
230
231        try {}
232
233        finally {}
234    }
235
236    trimStart() {wasm.icu4x_Decimal_trim_start_mv1(this.ffiValue);
237
238        try {}
239
240        finally {}
241    }
242
243    trimEnd() {wasm.icu4x_Decimal_trim_end_mv1(this.ffiValue);
244
245        try {}
246
247        finally {}
248    }
249
250    trimEndIfInteger() {wasm.icu4x_Decimal_trim_end_if_integer_mv1(this.ffiValue);
251
252        try {}
253
254        finally {}
255    }
256
257    padStart(position) {wasm.icu4x_Decimal_pad_start_mv1(this.ffiValue, position);
258
259        try {}
260
261        finally {}
262    }
263
264    padEnd(position) {wasm.icu4x_Decimal_pad_end_mv1(this.ffiValue, position);
265
266        try {}
267
268        finally {}
269    }
270
271    setMaxPosition(position) {wasm.icu4x_Decimal_set_max_position_mv1(this.ffiValue, position);
272
273        try {}
274
275        finally {}
276    }
277
278    round(position) {wasm.icu4x_Decimal_round_mv1(this.ffiValue, position);
279
280        try {}
281
282        finally {}
283    }
284
285    ceil(position) {wasm.icu4x_Decimal_ceil_mv1(this.ffiValue, position);
286
287        try {}
288
289        finally {}
290    }
291
292    expand(position) {wasm.icu4x_Decimal_expand_mv1(this.ffiValue, position);
293
294        try {}
295
296        finally {}
297    }
298
299    floor(position) {wasm.icu4x_Decimal_floor_mv1(this.ffiValue, position);
300
301        try {}
302
303        finally {}
304    }
305
306    trunc(position) {wasm.icu4x_Decimal_trunc_mv1(this.ffiValue, position);
307
308        try {}
309
310        finally {}
311    }
312
313    roundWithMode(position, mode) {wasm.icu4x_Decimal_round_with_mode_mv1(this.ffiValue, position, mode.ffiValue);
314
315        try {}
316
317        finally {}
318    }
319
320    roundWithModeAndIncrement(position, mode, increment) {wasm.icu4x_Decimal_round_with_mode_and_increment_mv1(this.ffiValue, position, mode.ffiValue, increment.ffiValue);
321
322        try {}
323
324        finally {}
325    }
326
327    concatenateEnd(other) {
328        const result = wasm.icu4x_Decimal_concatenate_end_mv1(this.ffiValue, other.ffiValue);
329
330        try {
331            return result === 1;
332        }
333
334        finally {}
335    }
336
337    toString() {
338        const write = new diplomatRuntime.DiplomatWriteBuf(wasm);
339        wasm.icu4x_Decimal_to_string_mv1(this.ffiValue, write.buffer);
340
341        try {
342            return write.readString8();
343        }
344
345        finally {
346            write.free();
347        }
348    }
349
350    constructor(symbol, ptr, selfEdge) {
351        return this.#internalConstructor(...arguments)
352    }
353}