Calendar.mjs (3693B)
1 // generated by diplomat-tool 2 import { CalendarKind } from "./CalendarKind.mjs" 3 import { DataError } from "./DataError.mjs" 4 import { DataProvider } from "./DataProvider.mjs" 5 import wasm from "./diplomat-wasm.mjs"; 6 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 7 8 9 /** 10 * See the [Rust documentation for `AnyCalendar`](https://docs.rs/icu/latest/icu/calendar/enum.AnyCalendar.html) for more information. 11 */ 12 const Calendar_box_destroy_registry = new FinalizationRegistry((ptr) => { 13 wasm.icu4x_Calendar_destroy_mv1(ptr); 14 }); 15 16 export class Calendar { 17 // Internal ptr reference: 18 #ptr = null; 19 20 // Lifetimes are only to keep dependencies alive. 21 // Since JS won't garbage collect until there are no incoming edges. 22 #selfEdge = []; 23 24 #internalConstructor(symbol, ptr, selfEdge) { 25 if (symbol !== diplomatRuntime.internalConstructor) { 26 console.error("Calendar is an Opaque type. You cannot call its constructor."); 27 return; 28 } 29 this.#ptr = ptr; 30 this.#selfEdge = selfEdge; 31 32 // Are we being borrowed? If not, we can register. 33 if (this.#selfEdge.length === 0) { 34 Calendar_box_destroy_registry.register(this, this.#ptr); 35 } 36 37 return this; 38 } 39 get ffiValue() { 40 return this.#ptr; 41 } 42 43 44 /** 45 * Creates a new [`Calendar`] for the specified kind, using compiled data. 46 * 47 * See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/calendar/enum.AnyCalendar.html#method.new) for more information. 48 */ 49 #defaultConstructor(kind) { 50 51 const result = wasm.icu4x_Calendar_create_mv1(kind.ffiValue); 52 53 try { 54 return new Calendar(diplomatRuntime.internalConstructor, result, []); 55 } 56 57 finally { 58 } 59 } 60 61 /** 62 * Creates a new [`Calendar`] for the specified kind, using a particular data source. 63 * 64 * See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/calendar/enum.AnyCalendar.html#method.new) for more information. 65 */ 66 static createWithProvider(provider, kind) { 67 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 68 69 70 const result = wasm.icu4x_Calendar_create_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue, kind.ffiValue); 71 72 try { 73 if (!diplomatReceive.resultFlag) { 74 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 75 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 76 } 77 return new Calendar(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 78 } 79 80 finally { 81 diplomatReceive.free(); 82 } 83 } 84 85 /** 86 * Returns the kind of this calendar 87 * 88 * See the [Rust documentation for `kind`](https://docs.rs/icu/latest/icu/calendar/enum.AnyCalendar.html#method.kind) for more information. 89 */ 90 get kind() { 91 92 const result = wasm.icu4x_Calendar_kind_mv1(this.ffiValue); 93 94 try { 95 return new CalendarKind(diplomatRuntime.internalConstructor, result); 96 } 97 98 finally { 99 } 100 } 101 102 constructor(kind) { 103 if (arguments[0] === diplomatRuntime.exposeConstructor) { 104 return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1)); 105 } else if (arguments[0] === diplomatRuntime.internalConstructor) { 106 return this.#internalConstructor(...arguments); 107 } else { 108 return this.#defaultConstructor(...arguments); 109 } 110 } 111 }