1// generated by diplomat-tool 2import { CalendarError } from "./CalendarError.mjs" 3import { CalendarParseError } from "./CalendarParseError.mjs" 4import wasm from "./diplomat-wasm.mjs"; 5import * as diplomatRuntime from "./diplomat-runtime.mjs"; 6 7 8/** An ICU4X Time object representing a time in terms of hour, minute, second, nanosecond 9* 10*See the [Rust documentation for `Time`](https://docs.rs/icu/latest/icu/time/struct.Time.html) for more information. 11*/ 12const Time_box_destroy_registry = new FinalizationRegistry((ptr) => { 13 wasm.icu4x_Time_destroy_mv1(ptr); 14}); 15 16export class Time { 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 25 #internalConstructor(symbol, ptr, selfEdge) { 26 if (symbol !== diplomatRuntime.internalConstructor) { 27 console.error("Time is an Opaque type. You cannot call its constructor."); 28 return; 29 } 30 31 this.#ptr = ptr; 32 this.#selfEdge = selfEdge; 33 34 // Are we being borrowed? If not, we can register. 35 if (this.#selfEdge.length === 0) { 36 Time_box_destroy_registry.register(this, this.#ptr); 37 } 38 39 return this; 40 } 41 get ffiValue() { 42 return this.#ptr; 43 } 44 45 #defaultConstructor(hour, minute, second, subsecond) { 46 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 47 48 const result = wasm.icu4x_Time_create_mv1(diplomatReceive.buffer, hour, minute, second, subsecond); 49 50 try { 51 if (!diplomatReceive.resultFlag) { 52 const cause = new CalendarError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 53 throw new globalThis.Error('CalendarError: ' + cause.value, { cause }); 54 } 55 return new Time(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 56 } 57 58 finally { 59 diplomatReceive.free(); 60 } 61 } 62 63 static fromString(v) { 64 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 65 66 const vSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, v)); 67 68 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 69 70 const result = wasm.icu4x_Time_from_string_mv1(diplomatReceive.buffer, ...vSlice.splat()); 71 72 try { 73 if (!diplomatReceive.resultFlag) { 74 const cause = new CalendarParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 75 throw new globalThis.Error('CalendarParseError: ' + cause.value, { cause }); 76 } 77 return new Time(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 78 } 79 80 finally { 81 functionCleanupArena.free(); 82 83 diplomatReceive.free(); 84 } 85 } 86 87 static midnight() { 88 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 89 90 const result = wasm.icu4x_Time_midnight_mv1(diplomatReceive.buffer); 91 92 try { 93 if (!diplomatReceive.resultFlag) { 94 const cause = new CalendarError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 95 throw new globalThis.Error('CalendarError: ' + cause.value, { cause }); 96 } 97 return new Time(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 98 } 99 100 finally { 101 diplomatReceive.free(); 102 } 103 } 104 105 get hour() { 106 const result = wasm.icu4x_Time_hour_mv1(this.ffiValue); 107 108 try { 109 return result; 110 } 111 112 finally {} 113 } 114 115 get minute() { 116 const result = wasm.icu4x_Time_minute_mv1(this.ffiValue); 117 118 try { 119 return result; 120 } 121 122 finally {} 123 } 124 125 get second() { 126 const result = wasm.icu4x_Time_second_mv1(this.ffiValue); 127 128 try { 129 return result; 130 } 131 132 finally {} 133 } 134 135 get subsecond() { 136 const result = wasm.icu4x_Time_subsecond_mv1(this.ffiValue); 137 138 try { 139 return result; 140 } 141 142 finally {} 143 } 144 145 constructor(hour, minute, second, subsecond) { 146 if (arguments[0] === diplomatRuntime.exposeConstructor) { 147 return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1)); 148 } else if (arguments[0] === diplomatRuntime.internalConstructor) { 149 return this.#internalConstructor(...arguments); 150 } else { 151 return this.#defaultConstructor(...arguments); 152 } 153 } 154}