gofront 1.8.0 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (177) hide show
  1. package/README.md +3 -3
  2. package/package.json +5 -4
  3. package/src/backend/js/expressions.js +849 -371
  4. package/src/backend/js/index.js +241 -147
  5. package/src/backend/js/natives.js +383 -0
  6. package/src/backend/js/runtime.js +512 -2547
  7. package/src/backend/js/statements.js +506 -103
  8. package/src/backend/js/stdlib/index.js +45 -93
  9. package/src/backend/js/templ.js +32 -45
  10. package/src/backend/js/type-info.js +177 -13
  11. package/src/backend/shared/fixed-float.js +20 -0
  12. package/src/backend/shared/fmt-types.js +63 -171
  13. package/src/backend/shared/host-unicode.js +36 -0
  14. package/src/backend/wasm/boundary.js +490 -965
  15. package/src/backend/wasm/emit-builtins.js +643 -256
  16. package/src/backend/wasm/emit-exprs.js +124 -57
  17. package/src/backend/wasm/emit-maps.js +17 -367
  18. package/src/backend/wasm/emit-stmts.js +325 -75
  19. package/src/backend/wasm/emit.js +104 -13
  20. package/src/backend/wasm/globals.js +47 -8
  21. package/src/backend/wasm/glue.js +2 -4
  22. package/src/backend/wasm/index.js +36 -530
  23. package/src/backend/wasm/method-expr.js +12 -1
  24. package/src/backend/wasm/module-emitter.js +92 -717
  25. package/src/backend/wasm/monomorph.js +103 -6
  26. package/src/backend/wasm/natives.js +596 -0
  27. package/src/backend/wasm/prescan.js +5 -153
  28. package/src/backend/wasm/prune-funcs.js +89 -0
  29. package/src/backend/wasm/stdlib/index.js +2 -32
  30. package/src/backend/wasm/types.js +68 -20
  31. package/src/backend/wasm/variadic.js +3 -3
  32. package/src/cli/cli-core.js +5 -1
  33. package/src/cli/test-runner.js +115 -342
  34. package/src/compiler.js +108 -130
  35. package/src/lexer/lexer.js +105 -11
  36. package/src/lexer/templ-lexer.js +61 -18
  37. package/src/lexer/tokens.js +1 -0
  38. package/src/link/stdlib.js +354 -132
  39. package/src/link/type-names.js +148 -0
  40. package/src/link/wasm.js +30 -6
  41. package/src/lower/bounds.js +153 -0
  42. package/src/lower/captures.js +92 -33
  43. package/src/lower/embedding.js +32 -3
  44. package/src/lower/errors-as.js +196 -0
  45. package/src/lower/fmt-formatters.js +905 -0
  46. package/src/lower/functions.js +2 -0
  47. package/src/lower/gen-types.js +274 -0
  48. package/src/lower/index.js +7 -1
  49. package/src/lower/json-codecs.js +228 -247
  50. package/src/lower/ownership.js +25 -7
  51. package/src/lower/reachability.js +69 -51
  52. package/src/lower/walk.js +12 -0
  53. package/src/parser/declarations.js +28 -5
  54. package/src/parser/expressions.js +86 -16
  55. package/src/parser/statements.js +1 -0
  56. package/src/parser/templ.js +44 -5
  57. package/src/parser/types.js +1 -0
  58. package/src/resolver.js +1 -2
  59. package/src/typechecker/assignability.js +124 -11
  60. package/src/typechecker/const-fold.js +70 -27
  61. package/src/typechecker/constants.js +32 -6
  62. package/src/typechecker/expressions.js +326 -98
  63. package/src/typechecker/index.js +208 -33
  64. package/src/typechecker/init-order.js +136 -40
  65. package/src/typechecker/operand-text.js +40 -0
  66. package/src/typechecker/resolve.js +192 -36
  67. package/src/typechecker/statements.js +122 -11
  68. package/src/typechecker/stdlib/excluded.js +9 -5
  69. package/src/typechecker/stdlib/index.js +25 -9
  70. package/src/typechecker/termination.js +10 -1
  71. package/src/typechecker/types.js +203 -573
  72. package/stdlib/bytes/buffer.go +479 -0
  73. package/stdlib/bytes/bytes.go +1010 -0
  74. package/stdlib/bytes/reader.go +161 -0
  75. package/stdlib/context/context.go +115 -0
  76. package/stdlib/crypto/rand/rand.go +34 -0
  77. package/stdlib/crypto/sha256/sha256.go +249 -0
  78. package/stdlib/encoding/base64/base64.go +331 -0
  79. package/stdlib/encoding/binary/binary.go +332 -0
  80. package/stdlib/encoding/hex/hex.go +140 -0
  81. package/stdlib/encoding/json/json.go +62 -0
  82. package/stdlib/errors/errors.go +174 -0
  83. package/stdlib/fmt/errors.go +110 -0
  84. package/stdlib/fmt/format.go +732 -0
  85. package/stdlib/fmt/print.go +1232 -0
  86. package/stdlib/gom/gom.go +293 -0
  87. package/stdlib/html/html.go +251 -0
  88. package/stdlib/internal/fmtrt/fmtrt.go +163 -0
  89. package/stdlib/internal/jsonrt/dec.go +171 -38
  90. package/stdlib/internal/jsonrt/enc.go +186 -36
  91. package/stdlib/internal/jsonrt/scanner.go +17 -8
  92. package/stdlib/io/io.go +157 -1
  93. package/stdlib/maps/maps.go +131 -0
  94. package/stdlib/math/bits/bits.go +507 -0
  95. package/stdlib/math/extra.go +16 -14
  96. package/stdlib/math/math.go +439 -0
  97. package/stdlib/math/rand/rand.go +297 -0
  98. package/stdlib/os/os.go +93 -0
  99. package/stdlib/path/filepath/filepath.go +47 -0
  100. package/stdlib/path/path.go +394 -0
  101. package/stdlib/regexp/regexp.go +231 -0
  102. package/stdlib/slices/slices.go +526 -0
  103. package/stdlib/slices/sort.go +209 -0
  104. package/stdlib/slices/zsort_cmp.go +480 -0
  105. package/stdlib/slices/zsort_ordered.go +482 -0
  106. package/stdlib/sort/sort.go +391 -452
  107. package/stdlib/sort/zsort_func.go +480 -0
  108. package/stdlib/sort/zsort_iface.go +480 -0
  109. package/stdlib/strconv/float.go +1699 -0
  110. package/stdlib/strconv/isprint.go +752 -0
  111. package/stdlib/strconv/quote.go +235 -50
  112. package/stdlib/strconv/strconv.go +616 -0
  113. package/stdlib/strings/builder.go +95 -0
  114. package/stdlib/strings/reader.go +163 -0
  115. package/stdlib/strings/replacer.go +102 -0
  116. package/stdlib/strings/strings.go +807 -0
  117. package/stdlib/sync/sync.go +268 -0
  118. package/stdlib/testing/async_js.go +61 -0
  119. package/stdlib/testing/benchmark.go +297 -0
  120. package/stdlib/testing/testing.go +523 -0
  121. package/stdlib/time/duration.go +233 -0
  122. package/stdlib/time/format.go +745 -0
  123. package/stdlib/time/parse.go +790 -0
  124. package/stdlib/time/sleep_js.go +63 -0
  125. package/stdlib/time/time.go +834 -0
  126. package/stdlib/unicode/unicode.go +141 -3
  127. package/stdlib/unicode/utf8/utf8.go +464 -0
  128. package/src/backend/js/fmt-runtime.js +0 -573
  129. package/src/backend/js/stdlib/base64.js +0 -12
  130. package/src/backend/js/stdlib/binary.js +0 -102
  131. package/src/backend/js/stdlib/bits.js +0 -119
  132. package/src/backend/js/stdlib/builder.js +0 -98
  133. package/src/backend/js/stdlib/bytes.js +0 -368
  134. package/src/backend/js/stdlib/errors.js +0 -45
  135. package/src/backend/js/stdlib/fmt.js +0 -517
  136. package/src/backend/js/stdlib/gom.js +0 -155
  137. package/src/backend/js/stdlib/hex.js +0 -17
  138. package/src/backend/js/stdlib/html.js +0 -20
  139. package/src/backend/js/stdlib/io.js +0 -137
  140. package/src/backend/js/stdlib/json.js +0 -1167
  141. package/src/backend/js/stdlib/maps.js +0 -37
  142. package/src/backend/js/stdlib/math.js +0 -85
  143. package/src/backend/js/stdlib/os.js +0 -13
  144. package/src/backend/js/stdlib/path.js +0 -48
  145. package/src/backend/js/stdlib/rand.js +0 -32
  146. package/src/backend/js/stdlib/regexp.js +0 -54
  147. package/src/backend/js/stdlib/sha256.js +0 -12
  148. package/src/backend/js/stdlib/shared.js +0 -17
  149. package/src/backend/js/stdlib/simd.js +0 -34
  150. package/src/backend/js/stdlib/slices.js +0 -107
  151. package/src/backend/js/stdlib/sort.js +0 -30
  152. package/src/backend/js/stdlib/strconv.js +0 -79
  153. package/src/backend/js/stdlib/strings.js +0 -301
  154. package/src/backend/js/stdlib/sync.js +0 -13
  155. package/src/backend/js/stdlib/testing.js +0 -41
  156. package/src/backend/js/stdlib/time.js +0 -152
  157. package/src/backend/js/stdlib/unicode.js +0 -25
  158. package/src/backend/js/stdlib/utf8.js +0 -26
  159. package/src/backend/wasm/stdlib/bytes.js +0 -6716
  160. package/src/backend/wasm/stdlib/core.js +0 -10278
  161. package/src/backend/wasm/stdlib/io.js +0 -3578
  162. package/src/backend/wasm/stdlib/packages.js +0 -2402
  163. package/src/backend/wasm/stdlib/pure.js +0 -720
  164. package/src/backend/wasm/stdlib/time.js +0 -2229
  165. package/src/typechecker/stdlib/core.js +0 -1079
  166. package/src/typechecker/stdlib/extended.js +0 -1210
  167. package/stdlib/bytes/iter.go +0 -192
  168. package/stdlib/bytes/search.go +0 -240
  169. package/stdlib/bytes/split.go +0 -218
  170. package/stdlib/errors/overlay.go +0 -12
  171. package/stdlib/maps/iter.go +0 -65
  172. package/stdlib/slices/extra.go +0 -73
  173. package/stdlib/slices/iter.go +0 -113
  174. package/stdlib/strings/extra.go +0 -35
  175. package/stdlib/strings/iter.go +0 -122
  176. package/stdlib/sync/extra.go +0 -105
  177. package/stdlib/unicode/utf8/bytes.go +0 -239
@@ -1,2229 +0,0 @@
1
- // src/backend/wasm/stdlib/time.js
2
- // FunctionEmitter mixin: time package.
3
-
4
- const DURATION_VALS = {
5
- Nanosecond: 1n,
6
- Microsecond: 1000n,
7
- Millisecond: 1000000n,
8
- Second: 1000000000n,
9
- Minute: 60000000000n,
10
- Hour: 3600000000000n,
11
- };
12
-
13
- const LAYOUT_STRS = {
14
- RFC3339: "2006-01-02T15:04:05Z07:00",
15
- RFC3339Nano: "2006-01-02T15:04:05.999999999Z07:00",
16
- DateOnly: "2006-01-02",
17
- TimeOnly: "15:04:05",
18
- DateTime: "2006-01-02 15:04:05",
19
- };
20
-
21
- const TIME_INT_CONSTS = {
22
- January: 1,
23
- February: 2,
24
- March: 3,
25
- April: 4,
26
- May: 5,
27
- June: 6,
28
- July: 7,
29
- August: 8,
30
- September: 9,
31
- October: 10,
32
- November: 11,
33
- December: 12,
34
- Sunday: 0,
35
- Monday: 1,
36
- Tuesday: 2,
37
- Wednesday: 3,
38
- Thursday: 4,
39
- Friday: 5,
40
- Saturday: 6,
41
- };
42
-
43
- export class TimeEmitter {
44
- _isTimeConst(expr) {
45
- const f = expr.field;
46
- return (
47
- this._isPkgExpr(expr, "time") &&
48
- (f in DURATION_VALS ||
49
- f in LAYOUT_STRS ||
50
- f in TIME_INT_CONSTS ||
51
- f === "UTC" ||
52
- f === "Local")
53
- );
54
- }
55
-
56
- _isUtcExpr(expr) {
57
- if (!expr) return false;
58
- if (
59
- expr.kind === "SelectorExpr" &&
60
- (expr.sel?.name === "UTC" || expr.field === "UTC")
61
- )
62
- return true;
63
- if (expr.kind === "Ident" && expr.name === "UTC") return true;
64
- return false;
65
- }
66
-
67
- emitTimeConst(field, targetWasmType) {
68
- if (field in DURATION_VALS) {
69
- this.pushInstruction({ op: "i64.const", value: DURATION_VALS[field] });
70
- if (targetWasmType === "i32") {
71
- this.pushInstruction("i32.wrap_i64");
72
- }
73
- return;
74
- }
75
- if (field in LAYOUT_STRS) {
76
- this.emitBasicLit(
77
- { kind: "BasicLit", litKind: "STRING", value: LAYOUT_STRS[field] },
78
- targetWasmType,
79
- );
80
- return;
81
- }
82
- if (field in TIME_INT_CONSTS) {
83
- const v = TIME_INT_CONSTS[field];
84
- if (targetWasmType === "i64") {
85
- this.pushInstruction({ op: "i64.const", value: BigInt(v) });
86
- } else {
87
- this.pushInstruction({ op: "i32.const", value: v });
88
- }
89
- return;
90
- }
91
- if (field === "UTC" || field === "Local") {
92
- this.pushInstruction({ op: "ref.null", heapType: "any" });
93
- }
94
- }
95
-
96
- emitTimeCall(field, call, targetWasmType) {
97
- switch (field) {
98
- case "Now":
99
- this._emitTimeNow(targetWasmType);
100
- break;
101
- case "Since":
102
- this._emitTimeSince(call.args, targetWasmType);
103
- break;
104
- case "Unix":
105
- this._emitTimeUnix(call.args, targetWasmType);
106
- break;
107
- case "Date":
108
- this._emitTimeDate(call.args, targetWasmType);
109
- break;
110
- case "Parse":
111
- this._emitTimeParse(call.args, targetWasmType);
112
- break;
113
- case "ParseDuration":
114
- this._emitTimeParseDuration(call.args, targetWasmType);
115
- break;
116
- case "FixedZone":
117
- this._emitTimeFixedZone(call.args, targetWasmType);
118
- break;
119
- default:
120
- throw new Error(`Unsupported time function '${field}'`);
121
- }
122
- }
123
-
124
- emitTimeMethodCall(field, expr, args, targetWasmType) {
125
- switch (field) {
126
- case "Format":
127
- this._emitTimeMethodFormat(expr, args, targetWasmType);
128
- break;
129
- case "String":
130
- this._emitTimeMethodString(expr, targetWasmType);
131
- break;
132
- case "Unix":
133
- this._emitTimeMethodUnix(expr, targetWasmType);
134
- break;
135
- case "UnixMilli":
136
- this._emitTimeMethodUnixMilli(expr, targetWasmType);
137
- break;
138
- case "Add":
139
- this._emitTimeMethodAdd(expr, args, targetWasmType);
140
- break;
141
- case "AddDate":
142
- this._emitTimeMethodAddDate(expr, args, targetWasmType);
143
- break;
144
- case "Sub":
145
- this._emitTimeMethodSub(expr, args, targetWasmType);
146
- break;
147
- case "Before":
148
- this._emitTimeMethodBefore(expr, args, targetWasmType);
149
- break;
150
- case "After":
151
- this._emitTimeMethodAfter(expr, args, targetWasmType);
152
- break;
153
- case "Equal":
154
- this._emitTimeMethodEqual(expr, args, targetWasmType);
155
- break;
156
- case "Compare":
157
- this._emitTimeMethodCompare(expr, args, targetWasmType);
158
- break;
159
- case "Hour":
160
- this._emitTimeMethodHour(expr, targetWasmType);
161
- break;
162
- case "Minute":
163
- this._emitTimeMethodMinute(expr, targetWasmType);
164
- break;
165
- case "Second":
166
- this._emitTimeMethodSecond(expr, targetWasmType);
167
- break;
168
- case "Weekday":
169
- this._emitTimeMethodWeekday(expr, targetWasmType);
170
- break;
171
- case "Year":
172
- this._emitTimeMethodYMD(expr, targetWasmType, 0);
173
- break;
174
- case "Month":
175
- this._emitTimeMethodYMD(expr, targetWasmType, 1);
176
- break;
177
- case "Day":
178
- this._emitTimeMethodYMD(expr, targetWasmType, 2);
179
- break;
180
- case "YearDay":
181
- this._emitTimeMethodYearDay(expr, targetWasmType);
182
- break;
183
- case "ISOWeek":
184
- this._emitTimeMethodISOWeek(expr, targetWasmType);
185
- break;
186
- case "Zone":
187
- this._emitTimeMethodZone(expr, targetWasmType);
188
- break;
189
- case "Location":
190
- this._emitTimeMethodLocation(expr, targetWasmType);
191
- break;
192
- case "IsZero":
193
- this._emitTimeMethodIsZero(expr);
194
- break;
195
- case "UnixNano":
196
- this._emitTimeMethodUnixNano(expr, targetWasmType);
197
- break;
198
- case "Nanosecond":
199
- this._emitTimeMethodNanosecond(expr, targetWasmType);
200
- break;
201
- case "Truncate":
202
- this._emitTimeMethodTruncate(expr, args, targetWasmType);
203
- break;
204
- case "Round":
205
- this._emitTimeMethodRound(expr, args, targetWasmType);
206
- break;
207
- case "MarshalJSON":
208
- this._emitTimeMethodMarshalJSON(expr, targetWasmType);
209
- break;
210
- case "UnmarshalJSON":
211
- this._emitTimeMethodUnmarshalJSON(expr, args, targetWasmType);
212
- break;
213
- case "MarshalText":
214
- this._emitTimeMethodMarshalText(expr, targetWasmType);
215
- break;
216
- case "UnmarshalText":
217
- this._emitTimeMethodUnmarshalText(expr, args, targetWasmType);
218
- break;
219
- case "UTC":
220
- this._emitTimeMethodUTC(expr);
221
- break;
222
- case "Local":
223
- this._emitTimeMethodLocal(expr);
224
- break;
225
- default:
226
- throw new Error(`Unsupported time.Time method '${field}'`);
227
- }
228
- }
229
-
230
- emitDurationMethodCall(field, expr, args, _targetWasmType) {
231
- switch (field) {
232
- case "Nanoseconds":
233
- this.emitExpr(expr, "i64");
234
- return;
235
- case "Microseconds":
236
- this.emitExpr(expr, "i64");
237
- this.pushInstruction({ op: "i64.const", value: 1000n });
238
- this.pushInstruction("i64.div_s");
239
- return;
240
- case "Milliseconds":
241
- this.emitExpr(expr, "i64");
242
- this.pushInstruction({ op: "i64.const", value: 1000000n });
243
- this.pushInstruction("i64.div_s");
244
- return;
245
- case "Seconds":
246
- this.emitExpr(expr, "i64");
247
- this.pushInstruction("f64.convert_i64_s");
248
- this.pushInstruction({ op: "f64.const", value: 1000000000.0 });
249
- this.pushInstruction("f64.div");
250
- return;
251
- case "Minutes":
252
- this.emitExpr(expr, "i64");
253
- this.pushInstruction("f64.convert_i64_s");
254
- this.pushInstruction({ op: "f64.const", value: 60000000000.0 });
255
- this.pushInstruction("f64.div");
256
- return;
257
- case "Hours":
258
- this.emitExpr(expr, "i64");
259
- this.pushInstruction("f64.convert_i64_s");
260
- this.pushInstruction({ op: "f64.const", value: 3600000000000.0 });
261
- this.pushInstruction("f64.div");
262
- return;
263
- case "Truncate":
264
- this._emitDurationTruncate(expr, args);
265
- return;
266
- case "Round":
267
- this._emitDurationRound(expr, args);
268
- return;
269
- case "Abs":
270
- this._emitDurationAbs(expr);
271
- return;
272
- case "String":
273
- this.emitExpr(expr, "i64");
274
- this._callStdlib("time_duration_string");
275
- return;
276
- default:
277
- throw new Error(`Unsupported time.Duration method '${field}'`);
278
- }
279
- }
280
-
281
- _emitTimeNow(_targetWasmType) {
282
- const timeInfo = this.mod.getStructType("time.Time");
283
- const msTmp = this.acquireTemp("f64");
284
- const offTmp = this.acquireTemp("i32");
285
- this._callStdlib("time_now_ms");
286
- this.pushInstruction({ op: "local.tee", index: msTmp });
287
- this._callStdlib("time_timezone_offset");
288
- this.pushInstruction({ op: "local.set", index: offTmp });
289
- this.pushInstruction({ op: "local.get", index: msTmp });
290
- this.pushInstruction("i64.trunc_sat_f64_s");
291
- this.pushInstruction({ op: "i64.const", value: 1000000n });
292
- this.pushInstruction("i64.mul");
293
- this.pushInstruction({ op: "local.get", index: offTmp });
294
- this.pushInstruction({ op: "ref.null", heapType: "any" });
295
- this.pushInstruction({ op: "struct.new", typeIndex: timeInfo.typeIndex });
296
- this.releaseTemp(offTmp, "i32");
297
- this.releaseTemp(msTmp, "f64");
298
- }
299
-
300
- _emitTimeSince(args, targetWasmType) {
301
- const timeInfo = this.mod.getStructType("time.Time");
302
- const timeW = {
303
- kind: "ref",
304
- nullable: true,
305
- typeIndex: timeInfo.typeIndex,
306
- };
307
- const argTmp = this.acquireTemp(timeW);
308
- this.emitExpr(args[0], timeW);
309
- this.pushInstruction({ op: "local.set", index: argTmp });
310
-
311
- const msTmp = this.acquireTemp("f64");
312
- this._callStdlib("time_now_ms");
313
- this.pushInstruction({ op: "local.set", index: msTmp });
314
- this.pushInstruction({ op: "local.get", index: msTmp });
315
- this.pushInstruction("i64.trunc_sat_f64_s");
316
- this.pushInstruction({ op: "i64.const", value: 1000000n });
317
- this.pushInstruction("i64.mul");
318
- this.pushInstruction({ op: "local.get", index: argTmp });
319
- this.pushInstruction({
320
- op: "struct.get",
321
- typeIndex: timeInfo.typeIndex,
322
- fieldIndex: 0,
323
- });
324
- this.pushInstruction("i64.sub");
325
- this.releaseTemp(msTmp, "f64");
326
- this.releaseTemp(argTmp, timeW);
327
-
328
- if (targetWasmType === "i32") {
329
- this.pushInstruction("i32.wrap_i64");
330
- }
331
- }
332
-
333
- _emitTimeUnix(args, _targetWasmType) {
334
- const timeInfo = this.mod.getStructType("time.Time");
335
- const secTmp = this.acquireTemp("i64");
336
- const nsecTmp = this.acquireTemp("i64");
337
- const nanoTmp = this.acquireTemp("i64");
338
- const offTmp = this.acquireTemp("i32");
339
-
340
- this.emitExpr(args[0], "i64");
341
- this.pushInstruction({ op: "local.set", index: secTmp });
342
- this.emitExpr(args[1], "i64");
343
- this.pushInstruction({ op: "local.set", index: nsecTmp });
344
-
345
- this.pushInstruction({ op: "local.get", index: secTmp });
346
- this.pushInstruction({ op: "i64.const", value: 1000000000n });
347
- this.pushInstruction("i64.mul");
348
- this.pushInstruction({ op: "local.get", index: nsecTmp });
349
- this.pushInstruction("i64.add");
350
- this.pushInstruction({ op: "local.tee", index: nanoTmp });
351
-
352
- this.pushInstruction({ op: "i64.const", value: 1000000n });
353
- this.pushInstruction("i64.div_s");
354
- this.pushInstruction("f64.convert_i64_s");
355
- this._callStdlib("time_timezone_offset");
356
- this.pushInstruction({ op: "local.set", index: offTmp });
357
-
358
- this.pushInstruction({ op: "local.get", index: nanoTmp });
359
- this.pushInstruction({ op: "local.get", index: offTmp });
360
- this.pushInstruction({ op: "ref.null", heapType: "any" });
361
- this.pushInstruction({ op: "struct.new", typeIndex: timeInfo.typeIndex });
362
-
363
- this.releaseTemp(offTmp, "i32");
364
- this.releaseTemp(nanoTmp, "i64");
365
- this.releaseTemp(nsecTmp, "i64");
366
- this.releaseTemp(secTmp, "i64");
367
- }
368
-
369
- _emitCivilDays(yTmp, mTmp, dTmp, daysTmp) {
370
- // Evaluates:
371
- // if (m <= 2) y -= 1
372
- // era = (y >= 0 ? y : y - 399) / 400
373
- // yoe = y - era * 400
374
- // doy = (153 * (m > 2 ? m - 3 : m + 9) + 2) / 5 + d - 1
375
- // doe = yoe * 365 + yoe / 4 - yoe / 100 + doy
376
- // days = era * 146097 + doe - 719468
377
- const eraTmp = this.acquireTemp("i32");
378
- const yoeTmp = this.acquireTemp("i32");
379
- const doyTmp = this.acquireTemp("i32");
380
- const doeTmp = this.acquireTemp("i32");
381
-
382
- this.pushInstruction({ op: "local.get", index: mTmp });
383
- this.pushInstruction({ op: "i32.const", value: 2 });
384
- this.pushInstruction("i32.le_s");
385
- this.pushInstruction({ op: "if", blockType: "void" });
386
- this.pushInstruction({ op: "local.get", index: yTmp });
387
- this.pushInstruction({ op: "i32.const", value: 1 });
388
- this.pushInstruction("i32.sub");
389
- this.pushInstruction({ op: "local.set", index: yTmp });
390
- this.pushInstruction("end");
391
-
392
- this.pushInstruction({ op: "local.get", index: yTmp });
393
- this.pushInstruction({ op: "i32.const", value: 0 });
394
- this.pushInstruction("i32.ge_s");
395
- this.pushInstruction({ op: "if", blockType: "i32" });
396
- this.pushInstruction({ op: "local.get", index: yTmp });
397
- this.pushInstruction("else");
398
- this.pushInstruction({ op: "local.get", index: yTmp });
399
- this.pushInstruction({ op: "i32.const", value: 399 });
400
- this.pushInstruction("i32.sub");
401
- this.pushInstruction("end");
402
- this.pushInstruction({ op: "i32.const", value: 400 });
403
- this.pushInstruction("i32.div_s");
404
- this.pushInstruction({ op: "local.set", index: eraTmp });
405
- this.pushInstruction({ op: "local.get", index: yTmp });
406
- this.pushInstruction({ op: "local.get", index: eraTmp });
407
- this.pushInstruction({ op: "i32.const", value: 400 });
408
- this.pushInstruction("i32.mul");
409
- this.pushInstruction("i32.sub");
410
- this.pushInstruction({ op: "local.set", index: yoeTmp });
411
-
412
- // doy = (153 * (m > 2 ? m - 3 : m + 9) + 2) / 5 + d - 1
413
- this.pushInstruction({ op: "local.get", index: mTmp });
414
- this.pushInstruction({ op: "i32.const", value: 2 });
415
- this.pushInstruction("i32.gt_s");
416
- this.pushInstruction({ op: "if", blockType: "i32" });
417
- this.pushInstruction({ op: "local.get", index: mTmp });
418
- this.pushInstruction({ op: "i32.const", value: 3 });
419
- this.pushInstruction("i32.sub");
420
- this.pushInstruction("else");
421
- this.pushInstruction({ op: "local.get", index: mTmp });
422
- this.pushInstruction({ op: "i32.const", value: 9 });
423
- this.pushInstruction("i32.add");
424
- this.pushInstruction("end");
425
- this.pushInstruction({ op: "i32.const", value: 153 });
426
- this.pushInstruction("i32.mul");
427
- this.pushInstruction({ op: "i32.const", value: 2 });
428
- this.pushInstruction("i32.add");
429
- this.pushInstruction({ op: "i32.const", value: 5 });
430
- this.pushInstruction("i32.div_s");
431
- this.pushInstruction({ op: "local.get", index: dTmp });
432
- this.pushInstruction("i32.add");
433
- this.pushInstruction({ op: "i32.const", value: 1 });
434
- this.pushInstruction("i32.sub");
435
- this.pushInstruction({ op: "local.set", index: doyTmp });
436
-
437
- // doe = yoe * 365 + yoe / 4 - yoe / 100 + doy
438
- this.pushInstruction({ op: "local.get", index: yoeTmp });
439
- this.pushInstruction({ op: "i32.const", value: 365 });
440
- this.pushInstruction("i32.mul");
441
- this.pushInstruction({ op: "local.get", index: yoeTmp });
442
- this.pushInstruction({ op: "i32.const", value: 4 });
443
- this.pushInstruction("i32.div_s");
444
- this.pushInstruction("i32.add");
445
- this.pushInstruction({ op: "local.get", index: yoeTmp });
446
- this.pushInstruction({ op: "i32.const", value: 100 });
447
- this.pushInstruction("i32.div_s");
448
- this.pushInstruction("i32.sub");
449
- this.pushInstruction({ op: "local.get", index: doyTmp });
450
- this.pushInstruction("i32.add");
451
- this.pushInstruction({ op: "local.set", index: doeTmp });
452
-
453
- // days = era * 146097 + doe - 719468 (in i64)
454
- this.pushInstruction({ op: "local.get", index: eraTmp });
455
- this.pushInstruction("i64.extend_i32_s");
456
- this.pushInstruction({ op: "i64.const", value: 146097n });
457
- this.pushInstruction("i64.mul");
458
- this.pushInstruction({ op: "local.get", index: doeTmp });
459
- this.pushInstruction("i64.extend_i32_s");
460
- this.pushInstruction("i64.add");
461
- this.pushInstruction({ op: "i64.const", value: 719468n });
462
- this.pushInstruction("i64.sub");
463
- this.pushInstruction({ op: "local.set", index: daysTmp });
464
-
465
- this.releaseTemp(doeTmp, "i32");
466
- this.releaseTemp(doyTmp, "i32");
467
- this.releaseTemp(yoeTmp, "i32");
468
- this.releaseTemp(eraTmp, "i32");
469
- }
470
-
471
- _emitTimeDate(args, _targetWasmType) {
472
- const timeInfo = this.mod.getStructType("time.Time");
473
- const yTmp = this.acquireTemp("i32");
474
- const mTmp = this.acquireTemp("i32");
475
- const dTmp = this.acquireTemp("i32");
476
- const hrTmp = this.acquireTemp("i32");
477
- const minTmp = this.acquireTemp("i32");
478
- const secTmp = this.acquireTemp("i32");
479
- const nsecTmp = this.acquireTemp("i64");
480
- const locTmp = this.acquireTemp("anyref");
481
-
482
- this.emitExpr(args[0], "i32");
483
- this.pushInstruction({ op: "local.set", index: yTmp });
484
- this.emitExpr(args[1], "i32");
485
- this.pushInstruction({ op: "local.set", index: mTmp });
486
- this.emitExpr(args[2], "i32");
487
- this.pushInstruction({ op: "local.set", index: dTmp });
488
- this.emitExpr(args[3], "i32");
489
- this.pushInstruction({ op: "local.set", index: hrTmp });
490
- this.emitExpr(args[4], "i32");
491
- this.pushInstruction({ op: "local.set", index: minTmp });
492
- this.emitExpr(args[5], "i32");
493
- this.pushInstruction({ op: "local.set", index: secTmp });
494
- this.emitExpr(args[6], "i64");
495
- this.pushInstruction({ op: "local.set", index: nsecTmp });
496
- this.emitExpr(args[7], "anyref");
497
- this.pushInstruction({ op: "local.set", index: locTmp });
498
-
499
- const daysTmp = this.acquireTemp("i64");
500
- this._emitCivilDays(yTmp, mTmp, dTmp, daysTmp);
501
-
502
- const localSecTmp = this.acquireTemp("i64");
503
- // secOfDay = hr * 3600 + min * 60 + sec
504
- this.pushInstruction({ op: "local.get", index: hrTmp });
505
- this.pushInstruction({ op: "i32.const", value: 3600 });
506
- this.pushInstruction("i32.mul");
507
- this.pushInstruction({ op: "local.get", index: minTmp });
508
- this.pushInstruction({ op: "i32.const", value: 60 });
509
- this.pushInstruction("i32.mul");
510
- this.pushInstruction("i32.add");
511
- this.pushInstruction({ op: "local.get", index: secTmp });
512
- this.pushInstruction("i32.add");
513
- this.pushInstruction("i64.extend_i32_s");
514
- this.pushInstruction({ op: "local.get", index: daysTmp });
515
- this.pushInstruction({ op: "i64.const", value: 86400n });
516
- this.pushInstruction("i64.mul");
517
- this.pushInstruction("i64.add");
518
- this.pushInstruction({ op: "local.set", index: localSecTmp });
519
-
520
- const offTmp = this.acquireTemp("i32");
521
- this.pushInstruction({ op: "local.get", index: locTmp });
522
- this.pushInstruction("ref.is_null");
523
- this.pushInstruction({ op: "if", blockType: "void" });
524
- if (this._isUtcExpr(args[7])) {
525
- this.pushInstruction({ op: "i32.const", value: 0 });
526
- this.pushInstruction({ op: "local.set", index: offTmp });
527
- } else {
528
- this.pushInstruction({ op: "local.get", index: localSecTmp });
529
- this.pushInstruction({ op: "i64.const", value: 1000n });
530
- this.pushInstruction("i64.mul");
531
- this.pushInstruction("f64.convert_i64_s");
532
- this._callStdlib("time_timezone_offset");
533
- this.pushInstruction({ op: "local.set", index: offTmp });
534
- }
535
- this.pushInstruction("else");
536
- const locInfo = this.mod.getStructType("time.Location");
537
- if (locInfo) {
538
- this.pushInstruction({ op: "local.get", index: locTmp });
539
- this.pushInstruction({
540
- op: "ref.cast",
541
- nullable: true,
542
- typeIndex: locInfo.typeIndex,
543
- });
544
- this.pushInstruction({
545
- op: "struct.get",
546
- typeIndex: locInfo.typeIndex,
547
- fieldIndex: 1,
548
- });
549
- this.pushInstruction("i32.wrap_i64");
550
- this.pushInstruction({ op: "local.set", index: offTmp });
551
- }
552
- this.pushInstruction("end");
553
-
554
- // unixSec = localSec - offset
555
- this.pushInstruction({ op: "local.get", index: localSecTmp });
556
- this.pushInstruction({ op: "local.get", index: offTmp });
557
- this.pushInstruction("i64.extend_i32_s");
558
- this.pushInstruction("i64.sub");
559
- this.pushInstruction({ op: "i64.const", value: 1000000000n });
560
- this.pushInstruction("i64.mul");
561
- this.pushInstruction({ op: "local.get", index: nsecTmp });
562
- this.pushInstruction("i64.add");
563
-
564
- this.pushInstruction({ op: "local.get", index: offTmp });
565
- this.pushInstruction({ op: "local.get", index: locTmp });
566
- this.pushInstruction({ op: "struct.new", typeIndex: timeInfo.typeIndex });
567
-
568
- this.releaseTemp(offTmp, "i32");
569
- this.releaseTemp(localSecTmp, "i64");
570
- this.releaseTemp(daysTmp, "i64");
571
- this.releaseTemp(locTmp, "anyref");
572
- this.releaseTemp(nsecTmp, "i64");
573
- this.releaseTemp(secTmp, "i32");
574
- this.releaseTemp(minTmp, "i32");
575
- this.releaseTemp(hrTmp, "i32");
576
- this.releaseTemp(dTmp, "i32");
577
- this.releaseTemp(mTmp, "i32");
578
- this.releaseTemp(yTmp, "i32");
579
- }
580
-
581
- _emitTimeParse(args, _targetWasmType) {
582
- const timeInfo = this.mod.getStructType("time.Time");
583
- const timeW = {
584
- kind: "ref",
585
- nullable: true,
586
- typeIndex: timeInfo.typeIndex,
587
- };
588
- const msTmp = this.acquireTemp("f64");
589
- const offTmp = this.acquireTemp("i32");
590
- const errStrTmp = this.acquireTemp("externref");
591
- const resTimeTmp = this.acquireTemp(timeW);
592
- const resErrTmp = this.acquireTemp("anyref");
593
-
594
- this.emitExpr(args[0], "externref");
595
- this.emitExpr(args[1], "externref");
596
- this._callStdlib("time_parse");
597
- this.pushInstruction({ op: "local.set", index: msTmp });
598
- this._callStdlib("time_parse_offset");
599
- this.pushInstruction({ op: "local.set", index: offTmp });
600
- this._callStdlib("time_parse_err");
601
- this.pushInstruction({ op: "local.set", index: errStrTmp });
602
-
603
- this.pushInstruction({ op: "local.get", index: errStrTmp });
604
- this.pushInstruction("ref.is_null");
605
- this.pushInstruction({ op: "if", blockType: "void" });
606
-
607
- // Success
608
- this.pushInstruction({ op: "local.get", index: msTmp });
609
- this.pushInstruction("i64.trunc_sat_f64_s");
610
- this.pushInstruction({ op: "i64.const", value: 1000000n });
611
- this.pushInstruction("i64.mul");
612
- this._callStdlib("time_parse_sub_ns");
613
- this.pushInstruction("i64.extend_i32_s");
614
- this.pushInstruction("i64.add");
615
- this.pushInstruction({ op: "local.get", index: offTmp });
616
- this.pushInstruction({ op: "ref.null", heapType: "any" });
617
- this.pushInstruction({ op: "struct.new", typeIndex: timeInfo.typeIndex });
618
- this.pushInstruction({ op: "local.set", index: resTimeTmp });
619
- this.pushInstruction({ op: "ref.null", heapType: "any" });
620
- this.pushInstruction({ op: "local.set", index: resErrTmp });
621
-
622
- this.pushInstruction("else");
623
-
624
- // Error
625
- this.pushInstruction({ op: "i64.const", value: 0n });
626
- this.pushInstruction({ op: "i32.const", value: 0 });
627
- this.pushInstruction({ op: "ref.null", heapType: "any" });
628
- this.pushInstruction({ op: "struct.new", typeIndex: timeInfo.typeIndex });
629
- this.pushInstruction({ op: "local.set", index: resTimeTmp });
630
- this.pushInstruction({ op: "local.get", index: errStrTmp });
631
- this.pushInstruction({ op: "ref.null", heapType: "any" });
632
- this.pushInstruction({
633
- op: "call",
634
- funcIndex: this.mod.resolveFuncIndex("__mk_error"),
635
- });
636
- this.pushInstruction({ op: "local.set", index: resErrTmp });
637
-
638
- this.pushInstruction("end");
639
-
640
- this.pushInstruction({ op: "local.get", index: resTimeTmp });
641
- this.pushInstruction({ op: "local.get", index: resErrTmp });
642
-
643
- this.releaseTemp(resErrTmp, "anyref");
644
- this.releaseTemp(resTimeTmp, timeW);
645
- this.releaseTemp(errStrTmp, "externref");
646
- this.releaseTemp(offTmp, "i32");
647
- this.releaseTemp(msTmp, "f64");
648
- }
649
-
650
- _emitTimeMethodAdd(expr, args, _targetWasmType) {
651
- const timeInfo = this.mod.getStructType("time.Time");
652
- const timeW = {
653
- kind: "ref",
654
- nullable: true,
655
- typeIndex: timeInfo.typeIndex,
656
- };
657
- const rTmp = this.acquireTemp(timeW);
658
- const dTmp = this.acquireTemp("i64");
659
-
660
- this.emitExpr(expr, timeW);
661
- this.pushInstruction({ op: "local.set", index: rTmp });
662
- this.emitExpr(args[0], "i64");
663
- this.pushInstruction({ op: "local.set", index: dTmp });
664
-
665
- this.pushInstruction({ op: "local.get", index: rTmp });
666
- this.pushInstruction({
667
- op: "struct.get",
668
- typeIndex: timeInfo.typeIndex,
669
- fieldIndex: 0,
670
- });
671
- this.pushInstruction({ op: "local.get", index: dTmp });
672
- this.pushInstruction("i64.add");
673
-
674
- this.pushInstruction({ op: "local.get", index: rTmp });
675
- this.pushInstruction({
676
- op: "struct.get",
677
- typeIndex: timeInfo.typeIndex,
678
- fieldIndex: 1,
679
- });
680
-
681
- this.pushInstruction({ op: "local.get", index: rTmp });
682
- this.pushInstruction({
683
- op: "struct.get",
684
- typeIndex: timeInfo.typeIndex,
685
- fieldIndex: 2,
686
- });
687
-
688
- this.pushInstruction({ op: "struct.new", typeIndex: timeInfo.typeIndex });
689
-
690
- this.releaseTemp(dTmp, "i64");
691
- this.releaseTemp(rTmp, timeW);
692
- }
693
-
694
- _emitTimeMethodSub(expr, args, targetWasmType) {
695
- const timeInfo = this.mod.getStructType("time.Time");
696
- const timeW = {
697
- kind: "ref",
698
- nullable: true,
699
- typeIndex: timeInfo.typeIndex,
700
- };
701
- const rTmp = this.acquireTemp(timeW);
702
- const uTmp = this.acquireTemp(timeW);
703
-
704
- this.emitExpr(expr, timeW);
705
- this.pushInstruction({ op: "local.set", index: rTmp });
706
- this.emitExpr(args[0], timeW);
707
- this.pushInstruction({ op: "local.set", index: uTmp });
708
-
709
- this.pushInstruction({ op: "local.get", index: rTmp });
710
- this.pushInstruction({
711
- op: "struct.get",
712
- typeIndex: timeInfo.typeIndex,
713
- fieldIndex: 0,
714
- });
715
- this.pushInstruction({ op: "local.get", index: uTmp });
716
- this.pushInstruction({
717
- op: "struct.get",
718
- typeIndex: timeInfo.typeIndex,
719
- fieldIndex: 0,
720
- });
721
- this.pushInstruction("i64.sub");
722
-
723
- this.releaseTemp(uTmp, timeW);
724
- this.releaseTemp(rTmp, timeW);
725
-
726
- if (targetWasmType === "i32") {
727
- this.pushInstruction("i32.wrap_i64");
728
- }
729
- }
730
-
731
- _emitTimeMethodBefore(expr, args, _targetWasmType) {
732
- this._emitTimeCmp(expr, args, "i64.lt_s");
733
- }
734
-
735
- _emitTimeMethodAfter(expr, args, _targetWasmType) {
736
- this._emitTimeCmp(expr, args, "i64.gt_s");
737
- }
738
-
739
- _emitTimeMethodEqual(expr, args, _targetWasmType) {
740
- this._emitTimeCmp(expr, args, "i64.eq");
741
- }
742
-
743
- _emitTimeCmp(expr, args, op) {
744
- const timeInfo = this.mod.getStructType("time.Time");
745
- const timeW = {
746
- kind: "ref",
747
- nullable: true,
748
- typeIndex: timeInfo.typeIndex,
749
- };
750
- const rTmp = this.acquireTemp(timeW);
751
- const uTmp = this.acquireTemp(timeW);
752
-
753
- this.emitExpr(expr, timeW);
754
- this.pushInstruction({ op: "local.set", index: rTmp });
755
- this.emitExpr(args[0], timeW);
756
- this.pushInstruction({ op: "local.set", index: uTmp });
757
-
758
- this.pushInstruction({ op: "local.get", index: rTmp });
759
- this.pushInstruction({
760
- op: "struct.get",
761
- typeIndex: timeInfo.typeIndex,
762
- fieldIndex: 0,
763
- });
764
- this.pushInstruction({ op: "local.get", index: uTmp });
765
- this.pushInstruction({
766
- op: "struct.get",
767
- typeIndex: timeInfo.typeIndex,
768
- fieldIndex: 0,
769
- });
770
- this.pushInstruction(op);
771
-
772
- this.releaseTemp(uTmp, timeW);
773
- this.releaseTemp(rTmp, timeW);
774
- }
775
-
776
- _emitTimeGetNano(expr) {
777
- const timeInfo = this.mod.getStructType("time.Time");
778
- this.emitExpr(expr, {
779
- kind: "ref",
780
- nullable: true,
781
- typeIndex: timeInfo.typeIndex,
782
- });
783
- this.pushInstruction({
784
- op: "struct.get",
785
- typeIndex: timeInfo.typeIndex,
786
- fieldIndex: 0,
787
- });
788
- return timeInfo;
789
- }
790
-
791
- _emitTimeMethodUnix(expr, targetWasmType) {
792
- this._emitTimeGetNano(expr);
793
- this.pushInstruction({ op: "i64.const", value: 1000000000n });
794
- this.pushInstruction("i64.div_s");
795
- if (targetWasmType === "i32") {
796
- this.pushInstruction("i32.wrap_i64");
797
- }
798
- }
799
-
800
- _emitTimeMethodUnixMilli(expr, targetWasmType) {
801
- this._emitTimeGetNano(expr);
802
- this.pushInstruction({ op: "i64.const", value: 1000000n });
803
- this.pushInstruction("i64.div_s");
804
- if (targetWasmType === "i32") {
805
- this.pushInstruction("i32.wrap_i64");
806
- }
807
- }
808
-
809
- _emitTimeMethodUnixNano(expr, targetWasmType) {
810
- this._emitTimeGetNano(expr);
811
- if (targetWasmType === "i32") {
812
- this.pushInstruction("i32.wrap_i64");
813
- }
814
- }
815
-
816
- _emitTimeMethodIsZero(expr) {
817
- this._emitTimeGetNano(expr);
818
- this.pushInstruction({ op: "i64.const", value: 0n });
819
- this.pushInstruction("i64.eq");
820
- }
821
-
822
- _emitTimeMethodNanosecond(expr, targetWasmType) {
823
- this._emitTimeGetNano(expr);
824
- this.pushInstruction({ op: "i64.const", value: 1000000000n });
825
- this.pushInstruction("i64.rem_s");
826
- const nsTmp = this.acquireTemp("i64");
827
- this.pushInstruction({ op: "local.tee", index: nsTmp });
828
- this.pushInstruction({ op: "i64.const", value: 0n });
829
- this.pushInstruction("i64.lt_s");
830
- this.pushInstruction({ op: "if", blockType: "i64" });
831
- this.pushInstruction({ op: "local.get", index: nsTmp });
832
- this.pushInstruction({ op: "i64.const", value: 1000000000n });
833
- this.pushInstruction("i64.add");
834
- this.pushInstruction({ op: "else" });
835
- this.pushInstruction({ op: "local.get", index: nsTmp });
836
- this.pushInstruction("end");
837
- this.releaseTemp(nsTmp, "i64");
838
- if (targetWasmType !== "i64") {
839
- this.pushInstruction("i32.wrap_i64");
840
- }
841
- }
842
-
843
- _emitTimeMethodUTC(expr) {
844
- const timeInfo = this._emitTimeGetNano(expr);
845
- this.pushInstruction({ op: "i32.const", value: 0 });
846
- this.pushInstruction({ op: "ref.null", heapType: "any" });
847
- this.pushInstruction({
848
- op: "struct.new",
849
- typeIndex: timeInfo.typeIndex,
850
- });
851
- }
852
-
853
- _emitTimeMethodLocal(expr) {
854
- const timeInfo = this.mod.getStructType("time.Time");
855
- const timeW = {
856
- kind: "ref",
857
- nullable: true,
858
- typeIndex: timeInfo.typeIndex,
859
- };
860
- const rTmp = this.acquireTemp(timeW);
861
- this.emitExpr(expr, timeW);
862
- this.pushInstruction({ op: "local.set", index: rTmp });
863
-
864
- this.pushInstruction({ op: "local.get", index: rTmp });
865
- this.pushInstruction({
866
- op: "struct.get",
867
- typeIndex: timeInfo.typeIndex,
868
- fieldIndex: 0,
869
- });
870
-
871
- this.pushInstruction({ op: "local.get", index: rTmp });
872
- this.pushInstruction({
873
- op: "struct.get",
874
- typeIndex: timeInfo.typeIndex,
875
- fieldIndex: 0,
876
- });
877
- this.pushInstruction({ op: "i64.const", value: 1000000n });
878
- this.pushInstruction("i64.div_s");
879
- this.pushInstruction("f64.convert_i64_s");
880
- this.pushInstruction({
881
- op: "call",
882
- funcIndex: this.mod.getTimezoneOffsetImportIndex(),
883
- });
884
- this.pushInstruction({ op: "ref.null", heapType: "any" });
885
- this.pushInstruction({
886
- op: "struct.new",
887
- typeIndex: timeInfo.typeIndex,
888
- });
889
- this.releaseTemp(rTmp, timeW);
890
- }
891
-
892
- _emitCivilDateFromDays(daysTmp, yTmp, mTmp, dTmp) {
893
- // Computes (y, m, d) in i32 from days in i64
894
- const d64 = this.acquireTemp("i64");
895
- this.pushInstruction({ op: "local.get", index: daysTmp });
896
- this.pushInstruction({ op: "i64.const", value: 719468n });
897
- this.pushInstruction("i64.add");
898
- this.pushInstruction({ op: "local.set", index: d64 });
899
-
900
- const eraTmp = this.acquireTemp("i32");
901
- const doeTmp = this.acquireTemp("i32");
902
- const yoeTmp = this.acquireTemp("i32");
903
- const doyTmp = this.acquireTemp("i32");
904
- const mpTmp = this.acquireTemp("i32");
905
-
906
- // era = (d64 >= 0 ? d64 : d64 - 146096n) / 146097n
907
- this.pushInstruction({ op: "local.get", index: d64 });
908
- this.pushInstruction({ op: "i64.const", value: 0n });
909
- this.pushInstruction("i64.ge_s");
910
- this.pushInstruction({ op: "if", blockType: "i64" });
911
- this.pushInstruction({ op: "local.get", index: d64 });
912
- this.pushInstruction("else");
913
- this.pushInstruction({ op: "local.get", index: d64 });
914
- this.pushInstruction({ op: "i64.const", value: 146096n });
915
- this.pushInstruction("i64.sub");
916
- this.pushInstruction("end");
917
- this.pushInstruction({ op: "i64.const", value: 146097n });
918
- this.pushInstruction("i64.div_s");
919
- this.pushInstruction("i32.wrap_i64");
920
- this.pushInstruction({ op: "local.set", index: eraTmp });
921
-
922
- // doe = i32(d64 - era * 146097n)
923
- this.pushInstruction({ op: "local.get", index: d64 });
924
- this.pushInstruction({ op: "local.get", index: eraTmp });
925
- this.pushInstruction("i64.extend_i32_s");
926
- this.pushInstruction({ op: "i64.const", value: 146097n });
927
- this.pushInstruction("i64.mul");
928
- this.pushInstruction("i64.sub");
929
- this.pushInstruction("i32.wrap_i64");
930
- this.pushInstruction({ op: "local.set", index: doeTmp });
931
-
932
- // yoe = (doe - doe/1460 + doe/36524 - doe/146096) / 365
933
- this.pushInstruction({ op: "local.get", index: doeTmp });
934
- this.pushInstruction({ op: "local.get", index: doeTmp });
935
- this.pushInstruction({ op: "i32.const", value: 1460 });
936
- this.pushInstruction("i32.div_s");
937
- this.pushInstruction("i32.sub");
938
- this.pushInstruction({ op: "local.get", index: doeTmp });
939
- this.pushInstruction({ op: "i32.const", value: 36524 });
940
- this.pushInstruction("i32.div_s");
941
- this.pushInstruction("i32.add");
942
- this.pushInstruction({ op: "local.get", index: doeTmp });
943
- this.pushInstruction({ op: "i32.const", value: 146096 });
944
- this.pushInstruction("i32.div_s");
945
- this.pushInstruction("i32.sub");
946
- this.pushInstruction({ op: "i32.const", value: 365 });
947
- this.pushInstruction("i32.div_s");
948
- this.pushInstruction({ op: "local.set", index: yoeTmp });
949
-
950
- // y = yoe + era * 400
951
- this.pushInstruction({ op: "local.get", index: yoeTmp });
952
- this.pushInstruction({ op: "local.get", index: eraTmp });
953
- this.pushInstruction({ op: "i32.const", value: 400 });
954
- this.pushInstruction("i32.mul");
955
- this.pushInstruction("i32.add");
956
- this.pushInstruction({ op: "local.set", index: yTmp });
957
-
958
- // doy = doe - (365*yoe + yoe/4 - yoe/100)
959
- this.pushInstruction({ op: "local.get", index: doeTmp });
960
- this.pushInstruction({ op: "local.get", index: yoeTmp });
961
- this.pushInstruction({ op: "i32.const", value: 365 });
962
- this.pushInstruction("i32.mul");
963
- this.pushInstruction({ op: "local.get", index: yoeTmp });
964
- this.pushInstruction({ op: "i32.const", value: 4 });
965
- this.pushInstruction("i32.div_s");
966
- this.pushInstruction("i32.add");
967
- this.pushInstruction({ op: "local.get", index: yoeTmp });
968
- this.pushInstruction({ op: "i32.const", value: 100 });
969
- this.pushInstruction("i32.div_s");
970
- this.pushInstruction("i32.sub");
971
- this.pushInstruction("i32.sub");
972
- this.pushInstruction({ op: "local.set", index: doyTmp });
973
-
974
- // mp = (5*doy + 2) / 153
975
- this.pushInstruction({ op: "local.get", index: doyTmp });
976
- this.pushInstruction({ op: "i32.const", value: 5 });
977
- this.pushInstruction("i32.mul");
978
- this.pushInstruction({ op: "i32.const", value: 2 });
979
- this.pushInstruction("i32.add");
980
- this.pushInstruction({ op: "i32.const", value: 153 });
981
- this.pushInstruction("i32.div_s");
982
- this.pushInstruction({ op: "local.set", index: mpTmp });
983
-
984
- // d = doy - (153*mp + 2)/5 + 1
985
- this.pushInstruction({ op: "local.get", index: doyTmp });
986
- this.pushInstruction({ op: "local.get", index: mpTmp });
987
- this.pushInstruction({ op: "i32.const", value: 153 });
988
- this.pushInstruction("i32.mul");
989
- this.pushInstruction({ op: "i32.const", value: 2 });
990
- this.pushInstruction("i32.add");
991
- this.pushInstruction({ op: "i32.const", value: 5 });
992
- this.pushInstruction("i32.div_s");
993
- this.pushInstruction("i32.sub");
994
- this.pushInstruction({ op: "i32.const", value: 1 });
995
- this.pushInstruction("i32.add");
996
- this.pushInstruction({ op: "local.set", index: dTmp });
997
-
998
- // m = mp + (mp < 10 ? 3 : -9)
999
- this.pushInstruction({ op: "local.get", index: mpTmp });
1000
- this.pushInstruction({ op: "local.get", index: mpTmp });
1001
- this.pushInstruction({ op: "i32.const", value: 10 });
1002
- this.pushInstruction("i32.lt_s");
1003
- this.pushInstruction({ op: "if", blockType: "i32" });
1004
- this.pushInstruction({ op: "i32.const", value: 3 });
1005
- this.pushInstruction("else");
1006
- this.pushInstruction({ op: "i32.const", value: -9 });
1007
- this.pushInstruction("end");
1008
- this.pushInstruction("i32.add");
1009
- this.pushInstruction({ op: "local.set", index: mTmp });
1010
-
1011
- // if (m <= 2) y += 1
1012
- this.pushInstruction({ op: "local.get", index: mTmp });
1013
- this.pushInstruction({ op: "i32.const", value: 2 });
1014
- this.pushInstruction("i32.le_s");
1015
- this.pushInstruction({ op: "if", blockType: "void" });
1016
- this.pushInstruction({ op: "local.get", index: yTmp });
1017
- this.pushInstruction({ op: "i32.const", value: 1 });
1018
- this.pushInstruction("i32.add");
1019
- this.pushInstruction({ op: "local.set", index: yTmp });
1020
- this.pushInstruction("end");
1021
-
1022
- this.releaseTemp(mpTmp, "i32");
1023
- this.releaseTemp(doyTmp, "i32");
1024
- this.releaseTemp(yoeTmp, "i32");
1025
- this.releaseTemp(doeTmp, "i32");
1026
- this.releaseTemp(eraTmp, "i32");
1027
- this.releaseTemp(d64, "i64");
1028
- }
1029
-
1030
- _emitTimeDecomposeParts(expr) {
1031
- const timeInfo = this.mod.getStructType("time.Time");
1032
- const timeW = {
1033
- kind: "ref",
1034
- nullable: true,
1035
- typeIndex: timeInfo.typeIndex,
1036
- };
1037
- const rTmp = this.acquireTemp(timeW);
1038
- this.emitExpr(expr, timeW);
1039
- this.pushInstruction({ op: "local.set", index: rTmp });
1040
-
1041
- const localSecTmp = this.acquireTemp("i64");
1042
- const daysTmp = this.acquireTemp("i64");
1043
- const remSecTmp = this.acquireTemp("i64");
1044
- const offTmp = this.acquireTemp("i32");
1045
- const nanoTmp = this.acquireTemp("i64");
1046
-
1047
- this.pushInstruction({ op: "local.get", index: rTmp });
1048
- this.pushInstruction({
1049
- op: "struct.get",
1050
- typeIndex: timeInfo.typeIndex,
1051
- fieldIndex: 0,
1052
- });
1053
- this.pushInstruction({ op: "local.set", index: nanoTmp });
1054
-
1055
- this.pushInstruction({ op: "local.get", index: rTmp });
1056
- this.pushInstruction({
1057
- op: "struct.get",
1058
- typeIndex: timeInfo.typeIndex,
1059
- fieldIndex: 1,
1060
- });
1061
- this.pushInstruction({ op: "local.set", index: offTmp });
1062
-
1063
- // localSec = nanoTmp / 1000000000n + offTmp
1064
- this.pushInstruction({ op: "local.get", index: nanoTmp });
1065
- this.pushInstruction({ op: "i64.const", value: 1000000000n });
1066
- this.pushInstruction("i64.div_s");
1067
- this.pushInstruction({ op: "local.get", index: offTmp });
1068
- this.pushInstruction("i64.extend_i32_s");
1069
- this.pushInstruction("i64.add");
1070
- this.pushInstruction({ op: "local.set", index: localSecTmp });
1071
-
1072
- // days = localSec / 86400n
1073
- this.pushInstruction({ op: "local.get", index: localSecTmp });
1074
- this.pushInstruction({ op: "i64.const", value: 86400n });
1075
- this.pushInstruction("i64.div_s");
1076
- this.pushInstruction({ op: "local.set", index: daysTmp });
1077
-
1078
- // remSec = localSec % 86400n
1079
- this.pushInstruction({ op: "local.get", index: localSecTmp });
1080
- this.pushInstruction({ op: "i64.const", value: 86400n });
1081
- this.pushInstruction("i64.rem_s");
1082
- this.pushInstruction({ op: "local.tee", index: remSecTmp });
1083
- this.pushInstruction({ op: "i64.const", value: 0n });
1084
- this.pushInstruction("i64.lt_s");
1085
- this.pushInstruction({ op: "if", blockType: "void" });
1086
- this.pushInstruction({ op: "local.get", index: remSecTmp });
1087
- this.pushInstruction({ op: "i64.const", value: 86400n });
1088
- this.pushInstruction("i64.add");
1089
- this.pushInstruction({ op: "local.set", index: remSecTmp });
1090
- this.pushInstruction({ op: "local.get", index: daysTmp });
1091
- this.pushInstruction({ op: "i64.const", value: 1n });
1092
- this.pushInstruction("i64.sub");
1093
- this.pushInstruction({ op: "local.set", index: daysTmp });
1094
- this.pushInstruction("end");
1095
-
1096
- return { rTmp, timeW, localSecTmp, daysTmp, remSecTmp, offTmp, nanoTmp };
1097
- }
1098
-
1099
- _emitTimeMethodHour(expr, targetWasmType) {
1100
- const p = this._emitTimeDecomposeParts(expr);
1101
- this.pushInstruction({ op: "local.get", index: p.remSecTmp });
1102
- this.pushInstruction({ op: "i64.const", value: 3600n });
1103
- this.pushInstruction("i64.div_s");
1104
- this.pushInstruction("i32.wrap_i64");
1105
- if (targetWasmType === "i64") this.pushInstruction("i64.extend_i32_s");
1106
- this._releaseTimeDecomposeParts(p);
1107
- }
1108
-
1109
- _emitTimeMethodMinute(expr, targetWasmType) {
1110
- const p = this._emitTimeDecomposeParts(expr);
1111
- this.pushInstruction({ op: "local.get", index: p.remSecTmp });
1112
- this.pushInstruction({ op: "i64.const", value: 3600n });
1113
- this.pushInstruction("i64.rem_s");
1114
- this.pushInstruction({ op: "i64.const", value: 60n });
1115
- this.pushInstruction("i64.div_s");
1116
- this.pushInstruction("i32.wrap_i64");
1117
- if (targetWasmType === "i64") this.pushInstruction("i64.extend_i32_s");
1118
- this._releaseTimeDecomposeParts(p);
1119
- }
1120
-
1121
- _emitTimeMethodSecond(expr, targetWasmType) {
1122
- const p = this._emitTimeDecomposeParts(expr);
1123
- this.pushInstruction({ op: "local.get", index: p.remSecTmp });
1124
- this.pushInstruction({ op: "i64.const", value: 60n });
1125
- this.pushInstruction("i64.rem_s");
1126
- this.pushInstruction("i32.wrap_i64");
1127
- if (targetWasmType === "i64") this.pushInstruction("i64.extend_i32_s");
1128
- this._releaseTimeDecomposeParts(p);
1129
- }
1130
-
1131
- _emitTimeMethodWeekday(expr, targetWasmType) {
1132
- const p = this._emitTimeDecomposeParts(expr);
1133
- const wdTmp = this.acquireTemp("i32");
1134
- this.pushInstruction({ op: "local.get", index: p.daysTmp });
1135
- this.pushInstruction({ op: "i64.const", value: 4n });
1136
- this.pushInstruction("i64.add");
1137
- this.pushInstruction({ op: "i64.const", value: 7n });
1138
- this.pushInstruction("i64.rem_s");
1139
- this.pushInstruction("i32.wrap_i64");
1140
- this.pushInstruction({ op: "local.tee", index: wdTmp });
1141
- this.pushInstruction({ op: "i32.const", value: 0 });
1142
- this.pushInstruction("i32.lt_s");
1143
- this.pushInstruction({ op: "if", blockType: "void" });
1144
- this.pushInstruction({ op: "local.get", index: wdTmp });
1145
- this.pushInstruction({ op: "i32.const", value: 7 });
1146
- this.pushInstruction("i32.add");
1147
- this.pushInstruction({ op: "local.set", index: wdTmp });
1148
- this.pushInstruction("end");
1149
-
1150
- this.pushInstruction({ op: "local.get", index: wdTmp });
1151
- if (targetWasmType === "i64") this.pushInstruction("i64.extend_i32_s");
1152
- this.releaseTemp(wdTmp, "i32");
1153
- this._releaseTimeDecomposeParts(p);
1154
- }
1155
-
1156
- _emitTimeMethodYMD(expr, targetWasmType, partIdx) {
1157
- const p = this._emitTimeDecomposeParts(expr);
1158
- const yTmp = this.acquireTemp("i32");
1159
- const mTmp = this.acquireTemp("i32");
1160
- const dTmp = this.acquireTemp("i32");
1161
- this._emitCivilDateFromDays(p.daysTmp, yTmp, mTmp, dTmp);
1162
- this.pushInstruction({
1163
- op: "local.get",
1164
- index: partIdx === 0 ? yTmp : partIdx === 1 ? mTmp : dTmp,
1165
- });
1166
- if (targetWasmType === "i64") this.pushInstruction("i64.extend_i32_s");
1167
- this.releaseTemp(dTmp, "i32");
1168
- this.releaseTemp(mTmp, "i32");
1169
- this.releaseTemp(yTmp, "i32");
1170
- this._releaseTimeDecomposeParts(p);
1171
- }
1172
-
1173
- _releaseTimeDecomposeParts(p) {
1174
- this.releaseTemp(p.nanoTmp, "i64");
1175
- this.releaseTemp(p.offTmp, "i32");
1176
- this.releaseTemp(p.remSecTmp, "i64");
1177
- this.releaseTemp(p.daysTmp, "i64");
1178
- this.releaseTemp(p.localSecTmp, "i64");
1179
- this.releaseTemp(p.rTmp, p.timeW);
1180
- }
1181
-
1182
- _emitTimeMethodFormat(expr, args, _targetWasmType) {
1183
- const p = this._emitTimeDecomposeParts(expr);
1184
- const layoutTmp = this.acquireTemp("externref");
1185
- this.emitExpr(args[0], "externref");
1186
- this.pushInstruction({ op: "local.set", index: layoutTmp });
1187
-
1188
- const yTmp = this.acquireTemp("i32");
1189
- const mTmp = this.acquireTemp("i32");
1190
- const dTmp = this.acquireTemp("i32");
1191
- this._emitCivilDateFromDays(p.daysTmp, yTmp, mTmp, dTmp);
1192
-
1193
- // hr = remSec / 3600
1194
- const hrTmp = this.acquireTemp("i32");
1195
- this.pushInstruction({ op: "local.get", index: p.remSecTmp });
1196
- this.pushInstruction({ op: "i64.const", value: 3600n });
1197
- this.pushInstruction("i64.div_s");
1198
- this.pushInstruction("i32.wrap_i64");
1199
- this.pushInstruction({ op: "local.set", index: hrTmp });
1200
-
1201
- // min = (remSec % 3600) / 60
1202
- const minTmp = this.acquireTemp("i32");
1203
- this.pushInstruction({ op: "local.get", index: p.remSecTmp });
1204
- this.pushInstruction({ op: "i64.const", value: 3600n });
1205
- this.pushInstruction("i64.rem_s");
1206
- this.pushInstruction({ op: "i64.const", value: 60n });
1207
- this.pushInstruction("i64.div_s");
1208
- this.pushInstruction("i32.wrap_i64");
1209
- this.pushInstruction({ op: "local.set", index: minTmp });
1210
-
1211
- // sec = remSec % 60
1212
- const secTmp = this.acquireTemp("i32");
1213
- this.pushInstruction({ op: "local.get", index: p.remSecTmp });
1214
- this.pushInstruction({ op: "i64.const", value: 60n });
1215
- this.pushInstruction("i64.rem_s");
1216
- this.pushInstruction("i32.wrap_i64");
1217
- this.pushInstruction({ op: "local.set", index: secTmp });
1218
-
1219
- // wd = (days + 4) % 7
1220
- const wdTmp = this.acquireTemp("i32");
1221
- this.pushInstruction({ op: "local.get", index: p.daysTmp });
1222
- this.pushInstruction({ op: "i64.const", value: 4n });
1223
- this.pushInstruction("i64.add");
1224
- this.pushInstruction({ op: "i64.const", value: 7n });
1225
- this.pushInstruction("i64.rem_s");
1226
- this.pushInstruction("i32.wrap_i64");
1227
- this.pushInstruction({ op: "local.tee", index: wdTmp });
1228
- this.pushInstruction({ op: "i32.const", value: 0 });
1229
- this.pushInstruction("i32.lt_s");
1230
- this.pushInstruction({ op: "if", blockType: "void" });
1231
- this.pushInstruction({ op: "local.get", index: wdTmp });
1232
- this.pushInstruction({ op: "i32.const", value: 7 });
1233
- this.pushInstruction("i32.add");
1234
- this.pushInstruction({ op: "local.set", index: wdTmp });
1235
- this.pushInstruction("end");
1236
-
1237
- // ns = i32(nanoTmp % 1000000000n)
1238
- const msTmp = this.acquireTemp("i32");
1239
- this.pushInstruction({ op: "local.get", index: p.nanoTmp });
1240
- this.pushInstruction({ op: "i64.const", value: 1000000000n });
1241
- this.pushInstruction("i64.rem_s");
1242
- this.pushInstruction("i32.wrap_i64");
1243
- this.pushInstruction({ op: "local.tee", index: msTmp });
1244
- this.pushInstruction({ op: "i32.const", value: 0 });
1245
- this.pushInstruction("i32.lt_s");
1246
- this.pushInstruction({ op: "if", blockType: "void" });
1247
- this.pushInstruction({ op: "local.get", index: msTmp });
1248
- this.pushInstruction({ op: "i32.const", value: 1000000000 });
1249
- this.pushInstruction("i32.add");
1250
- this.pushInstruction({ op: "local.set", index: msTmp });
1251
- this.pushInstruction("end");
1252
-
1253
- // Call time_format(y, m, d, hr, min, sec, wd, ms, off, layout)
1254
- this.pushInstruction({ op: "local.get", index: yTmp });
1255
- this.pushInstruction({ op: "local.get", index: mTmp });
1256
- this.pushInstruction({ op: "local.get", index: dTmp });
1257
- this.pushInstruction({ op: "local.get", index: hrTmp });
1258
- this.pushInstruction({ op: "local.get", index: minTmp });
1259
- this.pushInstruction({ op: "local.get", index: secTmp });
1260
- this.pushInstruction({ op: "local.get", index: wdTmp });
1261
- this.pushInstruction({ op: "local.get", index: msTmp });
1262
- this.pushInstruction({ op: "local.get", index: p.offTmp });
1263
- this.pushInstruction({ op: "local.get", index: layoutTmp });
1264
- this._callStdlib("time_format");
1265
-
1266
- this.releaseTemp(msTmp, "i32");
1267
- this.releaseTemp(wdTmp, "i32");
1268
- this.releaseTemp(secTmp, "i32");
1269
- this.releaseTemp(minTmp, "i32");
1270
- this.releaseTemp(hrTmp, "i32");
1271
- this.releaseTemp(dTmp, "i32");
1272
- this.releaseTemp(mTmp, "i32");
1273
- this.releaseTemp(yTmp, "i32");
1274
- this.releaseTemp(layoutTmp, "externref");
1275
- this._releaseTimeDecomposeParts(p);
1276
- }
1277
-
1278
- _emitTimeMethodString(expr, targetWasmType) {
1279
- const strLit = {
1280
- kind: "BasicLit",
1281
- litKind: "STRING",
1282
- value: "2006-01-02T15:04:05Z07:00",
1283
- };
1284
- this._emitTimeMethodFormat(expr, [strLit], targetWasmType);
1285
- }
1286
-
1287
- _emitTimeParseDuration(args, _targetWasmType) {
1288
- const durTmp = this.acquireTemp("i64");
1289
- const errStrTmp = this.acquireTemp("externref");
1290
- const resErrTmp = this.acquireTemp("anyref");
1291
-
1292
- this.emitExpr(args[0], "externref");
1293
- this._callStdlib("time_parse_duration");
1294
- this.pushInstruction({ op: "local.set", index: durTmp });
1295
- this._callStdlib("time_parse_duration_err");
1296
- this.pushInstruction({ op: "local.set", index: errStrTmp });
1297
-
1298
- this.pushInstruction({ op: "local.get", index: errStrTmp });
1299
- this.pushInstruction("ref.is_null");
1300
- this.pushInstruction({ op: "if", blockType: "void" });
1301
- this.pushInstruction({ op: "ref.null", heapType: "any" });
1302
- this.pushInstruction({ op: "local.set", index: resErrTmp });
1303
- this.pushInstruction("else");
1304
- this.pushInstruction({ op: "local.get", index: errStrTmp });
1305
- this.pushInstruction({ op: "ref.null", heapType: "any" });
1306
- this.pushInstruction({
1307
- op: "call",
1308
- funcIndex: this.mod.resolveFuncIndex("__mk_error"),
1309
- });
1310
- this.pushInstruction({ op: "local.set", index: resErrTmp });
1311
- this.pushInstruction("end");
1312
-
1313
- this.pushInstruction({ op: "local.get", index: durTmp });
1314
- this.pushInstruction({ op: "local.get", index: resErrTmp });
1315
-
1316
- this.releaseTemp(resErrTmp, "anyref");
1317
- this.releaseTemp(errStrTmp, "externref");
1318
- this.releaseTemp(durTmp, "i64");
1319
- }
1320
-
1321
- _emitTimeFixedZone(args, _targetWasmType) {
1322
- const locInfo = this.mod.getStructType("time.Location");
1323
- this.emitExpr(args[0], "externref");
1324
- this.emitExpr(args[1], "i64");
1325
- this.pushInstruction({ op: "struct.new", typeIndex: locInfo.typeIndex });
1326
- }
1327
-
1328
- _emitDurationAbs(expr) {
1329
- const dTmp = this.acquireTemp("i64");
1330
- this.emitExpr(expr, "i64");
1331
- this.pushInstruction({ op: "local.tee", index: dTmp });
1332
- this.pushInstruction({ op: "i64.const", value: 0n });
1333
- this.pushInstruction("i64.ge_s");
1334
- this.pushInstruction({ op: "if", blockType: "i64" });
1335
- this.pushInstruction({ op: "local.get", index: dTmp });
1336
- this.pushInstruction("else");
1337
- this.pushInstruction({ op: "local.get", index: dTmp });
1338
- this.pushInstruction({ op: "i64.const", value: -9223372036854775808n });
1339
- this.pushInstruction("i64.eq");
1340
- this.pushInstruction({ op: "if", blockType: "i64" });
1341
- this.pushInstruction({ op: "i64.const", value: 9223372036854775807n });
1342
- this.pushInstruction("else");
1343
- this.pushInstruction({ op: "i64.const", value: 0n });
1344
- this.pushInstruction({ op: "local.get", index: dTmp });
1345
- this.pushInstruction("i64.sub");
1346
- this.pushInstruction("end");
1347
- this.pushInstruction("end");
1348
- this.releaseTemp(dTmp, "i64");
1349
- }
1350
-
1351
- _emitDurationTruncate(expr, args) {
1352
- const dTmp = this.acquireTemp("i64");
1353
- const mTmp = this.acquireTemp("i64");
1354
- this.emitExpr(expr, "i64");
1355
- this.pushInstruction({ op: "local.set", index: dTmp });
1356
- this.emitExpr(args[0], "i64");
1357
- this.pushInstruction({ op: "local.tee", index: mTmp });
1358
- this.pushInstruction({ op: "i64.const", value: 0n });
1359
- this.pushInstruction("i64.le_s");
1360
- this.pushInstruction({ op: "if", blockType: "i64" });
1361
- this.pushInstruction({ op: "local.get", index: dTmp });
1362
- this.pushInstruction("else");
1363
- this.pushInstruction({ op: "local.get", index: dTmp });
1364
- this.pushInstruction({ op: "local.get", index: dTmp });
1365
- this.pushInstruction({ op: "local.get", index: mTmp });
1366
- this.pushInstruction("i64.rem_s");
1367
- this.pushInstruction("i64.sub");
1368
- this.pushInstruction("end");
1369
- this.releaseTemp(mTmp, "i64");
1370
- this.releaseTemp(dTmp, "i64");
1371
- }
1372
-
1373
- _emitDurationRound(expr, args) {
1374
- const dTmp = this.acquireTemp("i64");
1375
- const mTmp = this.acquireTemp("i64");
1376
- const rTmp = this.acquireTemp("i64");
1377
- this.emitExpr(expr, "i64");
1378
- this.pushInstruction({ op: "local.set", index: dTmp });
1379
- this.emitExpr(args[0], "i64");
1380
- this.pushInstruction({ op: "local.set", index: mTmp });
1381
-
1382
- this.pushInstruction({ op: "local.get", index: mTmp });
1383
- this.pushInstruction({ op: "i64.const", value: 0n });
1384
- this.pushInstruction("i64.le_s");
1385
- this.pushInstruction({ op: "if", blockType: "i64" });
1386
- this.pushInstruction({ op: "local.get", index: dTmp });
1387
- this.pushInstruction("else");
1388
-
1389
- this.pushInstruction({ op: "local.get", index: dTmp });
1390
- this.pushInstruction({ op: "local.get", index: mTmp });
1391
- this.pushInstruction("i64.rem_s");
1392
- this.pushInstruction({ op: "local.set", index: rTmp });
1393
-
1394
- this.pushInstruction({ op: "local.get", index: dTmp });
1395
- this.pushInstruction({ op: "i64.const", value: 0n });
1396
- this.pushInstruction("i64.lt_s");
1397
- this.pushInstruction({ op: "if", blockType: "i64" });
1398
-
1399
- this.pushInstruction({ op: "i64.const", value: 0n });
1400
- this.pushInstruction({ op: "local.get", index: rTmp });
1401
- this.pushInstruction("i64.sub");
1402
- this.pushInstruction({ op: "local.set", index: rTmp });
1403
-
1404
- this.pushInstruction({ op: "local.get", index: rTmp });
1405
- this.pushInstruction({ op: "local.get", index: mTmp });
1406
- this.pushInstruction({ op: "local.get", index: rTmp });
1407
- this.pushInstruction("i64.sub");
1408
- this.pushInstruction("i64.lt_u");
1409
- this.pushInstruction({ op: "if", blockType: "i64" });
1410
- this.pushInstruction({ op: "local.get", index: dTmp });
1411
- this.pushInstruction({ op: "local.get", index: rTmp });
1412
- this.pushInstruction("i64.add");
1413
- this.pushInstruction("else");
1414
- this.pushInstruction({ op: "local.get", index: dTmp });
1415
- this.pushInstruction({ op: "local.get", index: mTmp });
1416
- this.pushInstruction("i64.sub");
1417
- this.pushInstruction({ op: "local.get", index: rTmp });
1418
- this.pushInstruction("i64.add");
1419
- this.pushInstruction("end");
1420
-
1421
- this.pushInstruction("else");
1422
-
1423
- this.pushInstruction({ op: "local.get", index: rTmp });
1424
- this.pushInstruction({ op: "local.get", index: mTmp });
1425
- this.pushInstruction({ op: "local.get", index: rTmp });
1426
- this.pushInstruction("i64.sub");
1427
- this.pushInstruction("i64.lt_u");
1428
- this.pushInstruction({ op: "if", blockType: "i64" });
1429
- this.pushInstruction({ op: "local.get", index: dTmp });
1430
- this.pushInstruction({ op: "local.get", index: rTmp });
1431
- this.pushInstruction("i64.sub");
1432
- this.pushInstruction("else");
1433
- this.pushInstruction({ op: "local.get", index: dTmp });
1434
- this.pushInstruction({ op: "local.get", index: mTmp });
1435
- this.pushInstruction("i64.add");
1436
- this.pushInstruction({ op: "local.get", index: rTmp });
1437
- this.pushInstruction("i64.sub");
1438
- this.pushInstruction("end");
1439
-
1440
- this.pushInstruction("end");
1441
- this.pushInstruction("end");
1442
-
1443
- this.releaseTemp(rTmp, "i64");
1444
- this.releaseTemp(mTmp, "i64");
1445
- this.releaseTemp(dTmp, "i64");
1446
- }
1447
-
1448
- _emitTimeMethodCompare(expr, args, targetWasmType) {
1449
- const timeInfo = this.mod.getStructType("time.Time");
1450
- const timeW = {
1451
- kind: "ref",
1452
- nullable: true,
1453
- typeIndex: timeInfo.typeIndex,
1454
- };
1455
- const tTmp = this.acquireTemp(timeW);
1456
- const uTmp = this.acquireTemp(timeW);
1457
- const tNano = this.acquireTemp("i64");
1458
- const uNano = this.acquireTemp("i64");
1459
-
1460
- this.emitExpr(expr, timeW);
1461
- this.pushInstruction({ op: "local.set", index: tTmp });
1462
- this.emitExpr(args[0], timeW);
1463
- this.pushInstruction({ op: "local.set", index: uTmp });
1464
-
1465
- this.pushInstruction({ op: "local.get", index: tTmp });
1466
- this.pushInstruction({
1467
- op: "struct.get",
1468
- typeIndex: timeInfo.typeIndex,
1469
- fieldIndex: 0,
1470
- });
1471
- this.pushInstruction({ op: "local.set", index: tNano });
1472
- this.pushInstruction({ op: "local.get", index: uTmp });
1473
- this.pushInstruction({
1474
- op: "struct.get",
1475
- typeIndex: timeInfo.typeIndex,
1476
- fieldIndex: 0,
1477
- });
1478
- this.pushInstruction({ op: "local.set", index: uNano });
1479
-
1480
- this.pushInstruction({ op: "local.get", index: tNano });
1481
- this.pushInstruction({ op: "local.get", index: uNano });
1482
- this.pushInstruction("i64.lt_s");
1483
- this.pushInstruction({ op: "if", blockType: "i32" });
1484
- this.pushInstruction({ op: "i32.const", value: -1 });
1485
- this.pushInstruction("else");
1486
- this.pushInstruction({ op: "local.get", index: tNano });
1487
- this.pushInstruction({ op: "local.get", index: uNano });
1488
- this.pushInstruction("i64.gt_s");
1489
- this.pushInstruction({ op: "if", blockType: "i32" });
1490
- this.pushInstruction({ op: "i32.const", value: 1 });
1491
- this.pushInstruction("else");
1492
- this.pushInstruction({ op: "i32.const", value: 0 });
1493
- this.pushInstruction("end");
1494
- this.pushInstruction("end");
1495
-
1496
- this.releaseTemp(uNano, "i64");
1497
- this.releaseTemp(tNano, "i64");
1498
- this.releaseTemp(uTmp, timeW);
1499
- this.releaseTemp(tTmp, timeW);
1500
-
1501
- if (targetWasmType === "i64") {
1502
- this.pushInstruction("i64.extend_i32_s");
1503
- }
1504
- }
1505
-
1506
- _emitTimeMethodYearDay(expr, targetWasmType) {
1507
- const p = this._emitTimeDecomposeParts(expr);
1508
- const yTmp = this.acquireTemp("i32");
1509
- const mTmp = this.acquireTemp("i32");
1510
- const dTmp = this.acquireTemp("i32");
1511
- this._emitCivilDateFromDays(p.daysTmp, yTmp, mTmp, dTmp);
1512
-
1513
- const jan1YTmp = this.acquireTemp("i32");
1514
- const jan1MTmp = this.acquireTemp("i32");
1515
- const jan1DTmp = this.acquireTemp("i32");
1516
- const jan1DaysTmp = this.acquireTemp("i64");
1517
-
1518
- this.pushInstruction({ op: "local.get", index: yTmp });
1519
- this.pushInstruction({ op: "local.set", index: jan1YTmp });
1520
- this.pushInstruction({ op: "i32.const", value: 1 });
1521
- this.pushInstruction({ op: "local.set", index: jan1MTmp });
1522
- this.pushInstruction({ op: "i32.const", value: 1 });
1523
- this.pushInstruction({ op: "local.set", index: jan1DTmp });
1524
- this._emitCivilDays(jan1YTmp, jan1MTmp, jan1DTmp, jan1DaysTmp);
1525
-
1526
- this.pushInstruction({ op: "local.get", index: p.daysTmp });
1527
- this.pushInstruction({ op: "local.get", index: jan1DaysTmp });
1528
- this.pushInstruction("i64.sub");
1529
- this.pushInstruction({ op: "i64.const", value: 1n });
1530
- this.pushInstruction("i64.add");
1531
-
1532
- if (targetWasmType === "i32") {
1533
- this.pushInstruction("i32.wrap_i64");
1534
- }
1535
-
1536
- this.releaseTemp(jan1DaysTmp, "i64");
1537
- this.releaseTemp(jan1DTmp, "i32");
1538
- this.releaseTemp(jan1MTmp, "i32");
1539
- this.releaseTemp(jan1YTmp, "i32");
1540
- this.releaseTemp(dTmp, "i32");
1541
- this.releaseTemp(mTmp, "i32");
1542
- this.releaseTemp(yTmp, "i32");
1543
- this._releaseTimeDecomposeParts(p);
1544
- }
1545
-
1546
- _emitTimeMethodISOWeek(expr, targetWasmType) {
1547
- const p = this._emitTimeDecomposeParts(expr);
1548
- const monOffsetTmp = this.acquireTemp("i32");
1549
- const thuDaysTmp = this.acquireTemp("i64");
1550
- this.pushInstruction({ op: "local.get", index: p.daysTmp });
1551
- this.pushInstruction({ op: "i64.const", value: 3n });
1552
- this.pushInstruction("i64.add");
1553
- this.pushInstruction({ op: "i64.const", value: 7n });
1554
- this.pushInstruction("i64.rem_s");
1555
- this.pushInstruction("i32.wrap_i64");
1556
- this.pushInstruction({ op: "local.tee", index: monOffsetTmp });
1557
- this.pushInstruction({ op: "i32.const", value: 0 });
1558
- this.pushInstruction("i32.lt_s");
1559
- this.pushInstruction({ op: "if", blockType: "void" });
1560
- this.pushInstruction({ op: "local.get", index: monOffsetTmp });
1561
- this.pushInstruction({ op: "i32.const", value: 7 });
1562
- this.pushInstruction("i32.add");
1563
- this.pushInstruction({ op: "local.set", index: monOffsetTmp });
1564
- this.pushInstruction("end");
1565
-
1566
- this.pushInstruction({ op: "local.get", index: p.daysTmp });
1567
- this.pushInstruction({ op: "local.get", index: monOffsetTmp });
1568
- this.pushInstruction("i64.extend_i32_s");
1569
- this.pushInstruction("i64.sub");
1570
- this.pushInstruction({ op: "i64.const", value: 3n });
1571
- this.pushInstruction("i64.add");
1572
- this.pushInstruction({ op: "local.set", index: thuDaysTmp });
1573
-
1574
- const isoYearTmp = this.acquireTemp("i32");
1575
- const dummyMTmp = this.acquireTemp("i32");
1576
- const dummyDTmp = this.acquireTemp("i32");
1577
- this._emitCivilDateFromDays(thuDaysTmp, isoYearTmp, dummyMTmp, dummyDTmp);
1578
- this.releaseTemp(dummyDTmp, "i32");
1579
- this.releaseTemp(dummyMTmp, "i32");
1580
-
1581
- const jan4YTmp = this.acquireTemp("i32");
1582
- const jan4MTmp = this.acquireTemp("i32");
1583
- const jan4DTmp = this.acquireTemp("i32");
1584
- const jan4DaysTmp = this.acquireTemp("i64");
1585
- this.pushInstruction({ op: "local.get", index: isoYearTmp });
1586
- this.pushInstruction({ op: "local.set", index: jan4YTmp });
1587
- this.pushInstruction({ op: "i32.const", value: 1 });
1588
- this.pushInstruction({ op: "local.set", index: jan4MTmp });
1589
- this.pushInstruction({ op: "i32.const", value: 4 });
1590
- this.pushInstruction({ op: "local.set", index: jan4DTmp });
1591
- this._emitCivilDays(jan4YTmp, jan4MTmp, jan4DTmp, jan4DaysTmp);
1592
- this.releaseTemp(jan4DTmp, "i32");
1593
- this.releaseTemp(jan4MTmp, "i32");
1594
- this.releaseTemp(jan4YTmp, "i32");
1595
-
1596
- const jan4MonOffTmp = this.acquireTemp("i32");
1597
- const firstThuDaysTmp = this.acquireTemp("i64");
1598
- this.pushInstruction({ op: "local.get", index: jan4DaysTmp });
1599
- this.pushInstruction({ op: "i64.const", value: 3n });
1600
- this.pushInstruction("i64.add");
1601
- this.pushInstruction({ op: "i64.const", value: 7n });
1602
- this.pushInstruction("i64.rem_s");
1603
- this.pushInstruction("i32.wrap_i64");
1604
- this.pushInstruction({ op: "local.tee", index: jan4MonOffTmp });
1605
- this.pushInstruction({ op: "i32.const", value: 0 });
1606
- this.pushInstruction("i32.lt_s");
1607
- this.pushInstruction({ op: "if", blockType: "void" });
1608
- this.pushInstruction({ op: "local.get", index: jan4MonOffTmp });
1609
- this.pushInstruction({ op: "i32.const", value: 7 });
1610
- this.pushInstruction("i32.add");
1611
- this.pushInstruction({ op: "local.set", index: jan4MonOffTmp });
1612
- this.pushInstruction("end");
1613
-
1614
- this.pushInstruction({ op: "local.get", index: jan4DaysTmp });
1615
- this.pushInstruction({ op: "local.get", index: jan4MonOffTmp });
1616
- this.pushInstruction("i64.extend_i32_s");
1617
- this.pushInstruction("i64.sub");
1618
- this.pushInstruction({ op: "i64.const", value: 3n });
1619
- this.pushInstruction("i64.add");
1620
- this.pushInstruction({ op: "local.set", index: firstThuDaysTmp });
1621
-
1622
- const weekTmp = this.acquireTemp("i32");
1623
- this.pushInstruction({ op: "local.get", index: thuDaysTmp });
1624
- this.pushInstruction({ op: "local.get", index: firstThuDaysTmp });
1625
- this.pushInstruction("i64.sub");
1626
- this.pushInstruction({ op: "i64.const", value: 7n });
1627
- this.pushInstruction("i64.div_s");
1628
- this.pushInstruction("i32.wrap_i64");
1629
- this.pushInstruction({ op: "i32.const", value: 1 });
1630
- this.pushInstruction("i32.add");
1631
- this.pushInstruction({ op: "local.set", index: weekTmp });
1632
-
1633
- this.pushInstruction({ op: "local.get", index: isoYearTmp });
1634
- this.pushInstruction("i64.extend_i32_s");
1635
- if (targetWasmType === "i32") this.pushInstruction("i32.wrap_i64");
1636
- this.pushInstruction({ op: "local.get", index: weekTmp });
1637
- this.pushInstruction("i64.extend_i32_s");
1638
- if (targetWasmType === "i32") this.pushInstruction("i32.wrap_i64");
1639
-
1640
- this.releaseTemp(weekTmp, "i32");
1641
- this.releaseTemp(firstThuDaysTmp, "i64");
1642
- this.releaseTemp(jan4MonOffTmp, "i32");
1643
- this.releaseTemp(jan4DaysTmp, "i64");
1644
- this.releaseTemp(isoYearTmp, "i32");
1645
- this.releaseTemp(thuDaysTmp, "i64");
1646
- this.releaseTemp(monOffsetTmp, "i32");
1647
- this._releaseTimeDecomposeParts(p);
1648
- }
1649
-
1650
- _emitTimeMethodZone(expr, targetWasmType) {
1651
- const timeInfo = this.mod.getStructType("time.Time");
1652
- const timeW = {
1653
- kind: "ref",
1654
- nullable: true,
1655
- typeIndex: timeInfo.typeIndex,
1656
- };
1657
- const rTmp = this.acquireTemp(timeW);
1658
- const offTmp = this.acquireTemp("i32");
1659
- const locTmp = this.acquireTemp("anyref");
1660
- this.emitExpr(expr, timeW);
1661
- this.pushInstruction({ op: "local.tee", index: rTmp });
1662
- this.pushInstruction({
1663
- op: "struct.get",
1664
- typeIndex: timeInfo.typeIndex,
1665
- fieldIndex: 1,
1666
- });
1667
- this.pushInstruction({ op: "local.set", index: offTmp });
1668
-
1669
- this.pushInstruction({ op: "local.get", index: rTmp });
1670
- this.pushInstruction({
1671
- op: "struct.get",
1672
- typeIndex: timeInfo.typeIndex,
1673
- fieldIndex: 2,
1674
- });
1675
- this.pushInstruction({ op: "local.tee", index: locTmp });
1676
- this.pushInstruction("ref.is_null");
1677
- this.pushInstruction({ op: "if", blockType: "externref" });
1678
-
1679
- this.pushInstruction({ op: "local.get", index: offTmp });
1680
- this.pushInstruction({ op: "i32.const", value: 0 });
1681
- this.pushInstruction("i32.eq");
1682
- this.pushInstruction({ op: "if", blockType: "externref" });
1683
- this.emitBasicLit(
1684
- { kind: "BasicLit", litKind: "STRING", value: "UTC" },
1685
- "externref",
1686
- );
1687
- this.pushInstruction("else");
1688
- this.emitBasicLit(
1689
- { kind: "BasicLit", litKind: "STRING", value: "LOCAL" },
1690
- "externref",
1691
- );
1692
- this.pushInstruction("end");
1693
-
1694
- this.pushInstruction("else");
1695
- const locInfo = this.mod.getStructType("time.Location");
1696
- this.pushInstruction({ op: "local.get", index: locTmp });
1697
- this.pushInstruction({
1698
- op: "ref.cast",
1699
- nullable: true,
1700
- typeIndex: locInfo.typeIndex,
1701
- });
1702
- this.pushInstruction({
1703
- op: "struct.get",
1704
- typeIndex: locInfo.typeIndex,
1705
- fieldIndex: 0,
1706
- });
1707
- this.pushInstruction("end");
1708
-
1709
- this.pushInstruction({ op: "local.get", index: offTmp });
1710
- this.pushInstruction("i64.extend_i32_s");
1711
- if (targetWasmType === "i32") {
1712
- this.pushInstruction("i32.wrap_i64");
1713
- }
1714
-
1715
- this.releaseTemp(locTmp, "anyref");
1716
- this.releaseTemp(offTmp, "i32");
1717
- this.releaseTemp(rTmp, timeW);
1718
- }
1719
-
1720
- _emitTimeMethodLocation(expr, _targetWasmType) {
1721
- const timeInfo = this.mod.getStructType("time.Time");
1722
- const locInfo = this.mod.getStructType("time.Location");
1723
- const timeW = {
1724
- kind: "ref",
1725
- nullable: true,
1726
- typeIndex: timeInfo.typeIndex,
1727
- };
1728
- const rTmp = this.acquireTemp(timeW);
1729
- const offTmp = this.acquireTemp("i32");
1730
- const locTmp = this.acquireTemp("anyref");
1731
-
1732
- this.emitExpr(expr, timeW);
1733
- this.pushInstruction({ op: "local.tee", index: rTmp });
1734
- this.pushInstruction({
1735
- op: "struct.get",
1736
- typeIndex: timeInfo.typeIndex,
1737
- fieldIndex: 2,
1738
- });
1739
- this.pushInstruction({ op: "local.tee", index: locTmp });
1740
- this.pushInstruction("ref.is_null");
1741
- this.pushInstruction({
1742
- op: "if",
1743
- blockType: { kind: "ref", nullable: true, typeIndex: locInfo.typeIndex },
1744
- });
1745
-
1746
- this.pushInstruction({ op: "local.get", index: rTmp });
1747
- this.pushInstruction({
1748
- op: "struct.get",
1749
- typeIndex: timeInfo.typeIndex,
1750
- fieldIndex: 1,
1751
- });
1752
- this.pushInstruction({ op: "local.set", index: offTmp });
1753
-
1754
- this.pushInstruction({ op: "local.get", index: offTmp });
1755
- this.pushInstruction({ op: "i32.const", value: 0 });
1756
- this.pushInstruction("i32.eq");
1757
- this.pushInstruction({ op: "if", blockType: "externref" });
1758
- this.emitBasicLit(
1759
- { kind: "BasicLit", litKind: "STRING", value: "UTC" },
1760
- "externref",
1761
- );
1762
- this.pushInstruction("else");
1763
- this.emitBasicLit(
1764
- { kind: "BasicLit", litKind: "STRING", value: "Local" },
1765
- "externref",
1766
- );
1767
- this.pushInstruction("end");
1768
-
1769
- this.pushInstruction({ op: "local.get", index: offTmp });
1770
- this.pushInstruction("i64.extend_i32_s");
1771
-
1772
- this.pushInstruction({ op: "struct.new", typeIndex: locInfo.typeIndex });
1773
-
1774
- this.pushInstruction("else");
1775
- this.pushInstruction({ op: "local.get", index: locTmp });
1776
- this.pushInstruction({
1777
- op: "ref.cast",
1778
- nullable: true,
1779
- typeIndex: locInfo.typeIndex,
1780
- });
1781
- this.pushInstruction("end");
1782
-
1783
- this.releaseTemp(locTmp, "anyref");
1784
- this.releaseTemp(offTmp, "i32");
1785
- this.releaseTemp(rTmp, timeW);
1786
- }
1787
-
1788
- _emitTimeMethodTruncate(expr, args, _targetWasmType) {
1789
- const timeInfo = this.mod.getStructType("time.Time");
1790
- const timeW = {
1791
- kind: "ref",
1792
- nullable: true,
1793
- typeIndex: timeInfo.typeIndex,
1794
- };
1795
- const rTmp = this.acquireTemp(timeW);
1796
- const dTmp = this.acquireTemp("i64");
1797
- const nanoTmp = this.acquireTemp("i64");
1798
- const remTmp = this.acquireTemp("i64");
1799
- const resNanoTmp = this.acquireTemp("i64");
1800
-
1801
- this.emitExpr(expr, timeW);
1802
- this.pushInstruction({ op: "local.set", index: rTmp });
1803
- this.emitExpr(args[0], "i64");
1804
- this.pushInstruction({ op: "local.set", index: dTmp });
1805
-
1806
- this.pushInstruction({ op: "local.get", index: rTmp });
1807
- this.pushInstruction({
1808
- op: "struct.get",
1809
- typeIndex: timeInfo.typeIndex,
1810
- fieldIndex: 0,
1811
- });
1812
- this.pushInstruction({ op: "local.set", index: nanoTmp });
1813
-
1814
- this.pushInstruction({ op: "local.get", index: dTmp });
1815
- this.pushInstruction({ op: "i64.const", value: 0n });
1816
- this.pushInstruction("i64.le_s");
1817
- this.pushInstruction({ op: "if", blockType: "void" });
1818
- this.pushInstruction({ op: "local.get", index: nanoTmp });
1819
- this.pushInstruction({ op: "local.set", index: resNanoTmp });
1820
- this.pushInstruction("else");
1821
- this.pushInstruction({ op: "local.get", index: nanoTmp });
1822
- this.pushInstruction({ op: "local.get", index: dTmp });
1823
- this.pushInstruction("i64.rem_s");
1824
- this.pushInstruction({ op: "local.tee", index: remTmp });
1825
- this.pushInstruction({ op: "i64.const", value: 0n });
1826
- this.pushInstruction("i64.lt_s");
1827
- this.pushInstruction({ op: "if", blockType: "void" });
1828
- this.pushInstruction({ op: "local.get", index: remTmp });
1829
- this.pushInstruction({ op: "local.get", index: dTmp });
1830
- this.pushInstruction("i64.add");
1831
- this.pushInstruction({ op: "local.set", index: remTmp });
1832
- this.pushInstruction("end");
1833
- this.pushInstruction({ op: "local.get", index: nanoTmp });
1834
- this.pushInstruction({ op: "local.get", index: remTmp });
1835
- this.pushInstruction("i64.sub");
1836
- this.pushInstruction({ op: "local.set", index: resNanoTmp });
1837
- this.pushInstruction("end");
1838
-
1839
- this.pushInstruction({ op: "local.get", index: resNanoTmp });
1840
- this.pushInstruction({ op: "local.get", index: rTmp });
1841
- this.pushInstruction({
1842
- op: "struct.get",
1843
- typeIndex: timeInfo.typeIndex,
1844
- fieldIndex: 1,
1845
- });
1846
- this.pushInstruction({ op: "local.get", index: rTmp });
1847
- this.pushInstruction({
1848
- op: "struct.get",
1849
- typeIndex: timeInfo.typeIndex,
1850
- fieldIndex: 2,
1851
- });
1852
- this.pushInstruction({ op: "struct.new", typeIndex: timeInfo.typeIndex });
1853
-
1854
- this.releaseTemp(resNanoTmp, "i64");
1855
- this.releaseTemp(remTmp, "i64");
1856
- this.releaseTemp(nanoTmp, "i64");
1857
- this.releaseTemp(dTmp, "i64");
1858
- this.releaseTemp(rTmp, timeW);
1859
- }
1860
-
1861
- _emitTimeMethodRound(expr, args, _targetWasmType) {
1862
- const timeInfo = this.mod.getStructType("time.Time");
1863
- const timeW = {
1864
- kind: "ref",
1865
- nullable: true,
1866
- typeIndex: timeInfo.typeIndex,
1867
- };
1868
- const rTmp = this.acquireTemp(timeW);
1869
- const dTmp = this.acquireTemp("i64");
1870
- const nanoTmp = this.acquireTemp("i64");
1871
- const remTmp = this.acquireTemp("i64");
1872
- const resNanoTmp = this.acquireTemp("i64");
1873
-
1874
- this.emitExpr(expr, timeW);
1875
- this.pushInstruction({ op: "local.set", index: rTmp });
1876
- this.emitExpr(args[0], "i64");
1877
- this.pushInstruction({ op: "local.set", index: dTmp });
1878
-
1879
- this.pushInstruction({ op: "local.get", index: rTmp });
1880
- this.pushInstruction({
1881
- op: "struct.get",
1882
- typeIndex: timeInfo.typeIndex,
1883
- fieldIndex: 0,
1884
- });
1885
- this.pushInstruction({ op: "local.set", index: nanoTmp });
1886
-
1887
- this.pushInstruction({ op: "local.get", index: dTmp });
1888
- this.pushInstruction({ op: "i64.const", value: 0n });
1889
- this.pushInstruction("i64.le_s");
1890
- this.pushInstruction({ op: "if", blockType: "void" });
1891
- this.pushInstruction({ op: "local.get", index: nanoTmp });
1892
- this.pushInstruction({ op: "local.set", index: resNanoTmp });
1893
- this.pushInstruction("else");
1894
- this.pushInstruction({ op: "local.get", index: nanoTmp });
1895
- this.pushInstruction({ op: "local.get", index: dTmp });
1896
- this.pushInstruction("i64.rem_s");
1897
- this.pushInstruction({ op: "local.tee", index: remTmp });
1898
- this.pushInstruction({ op: "i64.const", value: 0n });
1899
- this.pushInstruction("i64.lt_s");
1900
- this.pushInstruction({ op: "if", blockType: "void" });
1901
- this.pushInstruction({ op: "local.get", index: remTmp });
1902
- this.pushInstruction({ op: "local.get", index: dTmp });
1903
- this.pushInstruction("i64.add");
1904
- this.pushInstruction({ op: "local.set", index: remTmp });
1905
- this.pushInstruction("end");
1906
-
1907
- this.pushInstruction({ op: "local.get", index: remTmp });
1908
- this.pushInstruction({ op: "local.get", index: dTmp });
1909
- this.pushInstruction({ op: "local.get", index: remTmp });
1910
- this.pushInstruction("i64.sub");
1911
- this.pushInstruction("i64.lt_u");
1912
- this.pushInstruction({ op: "if", blockType: "void" });
1913
- this.pushInstruction({ op: "local.get", index: nanoTmp });
1914
- this.pushInstruction({ op: "local.get", index: remTmp });
1915
- this.pushInstruction("i64.sub");
1916
- this.pushInstruction({ op: "local.set", index: resNanoTmp });
1917
- this.pushInstruction("else");
1918
- this.pushInstruction({ op: "local.get", index: nanoTmp });
1919
- this.pushInstruction({ op: "local.get", index: dTmp });
1920
- this.pushInstruction("i64.add");
1921
- this.pushInstruction({ op: "local.get", index: remTmp });
1922
- this.pushInstruction("i64.sub");
1923
- this.pushInstruction({ op: "local.set", index: resNanoTmp });
1924
- this.pushInstruction("end");
1925
- this.pushInstruction("end");
1926
-
1927
- this.pushInstruction({ op: "local.get", index: resNanoTmp });
1928
- this.pushInstruction({ op: "local.get", index: rTmp });
1929
- this.pushInstruction({
1930
- op: "struct.get",
1931
- typeIndex: timeInfo.typeIndex,
1932
- fieldIndex: 1,
1933
- });
1934
- this.pushInstruction({ op: "local.get", index: rTmp });
1935
- this.pushInstruction({
1936
- op: "struct.get",
1937
- typeIndex: timeInfo.typeIndex,
1938
- fieldIndex: 2,
1939
- });
1940
- this.pushInstruction({ op: "struct.new", typeIndex: timeInfo.typeIndex });
1941
-
1942
- this.releaseTemp(resNanoTmp, "i64");
1943
- this.releaseTemp(remTmp, "i64");
1944
- this.releaseTemp(nanoTmp, "i64");
1945
- this.releaseTemp(dTmp, "i64");
1946
- this.releaseTemp(rTmp, timeW);
1947
- }
1948
-
1949
- _emitTimeMethodAddDate(expr, args, _targetWasmType) {
1950
- const timeInfo = this.mod.getStructType("time.Time");
1951
- const p = this._emitTimeDecomposeParts(expr);
1952
-
1953
- const yearsTmp = this.acquireTemp("i32");
1954
- const monthsTmp = this.acquireTemp("i32");
1955
- const daysAddTmp = this.acquireTemp("i32");
1956
- this.emitExpr(args[0], "i32");
1957
- this.pushInstruction({ op: "local.set", index: yearsTmp });
1958
- this.emitExpr(args[1], "i32");
1959
- this.pushInstruction({ op: "local.set", index: monthsTmp });
1960
- this.emitExpr(args[2], "i32");
1961
- this.pushInstruction({ op: "local.set", index: daysAddTmp });
1962
-
1963
- const curY = this.acquireTemp("i32");
1964
- const curM = this.acquireTemp("i32");
1965
- const curD = this.acquireTemp("i32");
1966
- this._emitCivilDateFromDays(p.daysTmp, curY, curM, curD);
1967
-
1968
- this.pushInstruction({ op: "local.get", index: curY });
1969
- this.pushInstruction({ op: "local.get", index: yearsTmp });
1970
- this.pushInstruction("i32.add");
1971
- this.pushInstruction({ op: "local.set", index: curY });
1972
-
1973
- this.pushInstruction({ op: "local.get", index: curM });
1974
- this.pushInstruction({ op: "local.get", index: monthsTmp });
1975
- this.pushInstruction("i32.add");
1976
- this.pushInstruction({ op: "local.set", index: curM });
1977
-
1978
- this.pushInstruction({ op: "local.get", index: curD });
1979
- this.pushInstruction({ op: "local.get", index: daysAddTmp });
1980
- this.pushInstruction("i32.add");
1981
- this.pushInstruction({ op: "local.set", index: curD });
1982
-
1983
- const newDaysTmp = this.acquireTemp("i64");
1984
- this._emitCivilDays(curY, curM, curD, newDaysTmp);
1985
-
1986
- const newLocalSecTmp = this.acquireTemp("i64");
1987
- this.pushInstruction({ op: "local.get", index: newDaysTmp });
1988
- this.pushInstruction({ op: "i64.const", value: 86400n });
1989
- this.pushInstruction("i64.mul");
1990
- this.pushInstruction({ op: "local.get", index: p.remSecTmp });
1991
- this.pushInstruction("i64.add");
1992
- this.pushInstruction({ op: "local.set", index: newLocalSecTmp });
1993
-
1994
- const nsecTmp = this.acquireTemp("i64");
1995
- this.pushInstruction({ op: "local.get", index: p.nanoTmp });
1996
- this.pushInstruction({ op: "i64.const", value: 1000000000n });
1997
- this.pushInstruction("i64.rem_s");
1998
- this.pushInstruction({ op: "local.tee", index: nsecTmp });
1999
- this.pushInstruction({ op: "i64.const", value: 0n });
2000
- this.pushInstruction("i64.lt_s");
2001
- this.pushInstruction({ op: "if", blockType: "void" });
2002
- this.pushInstruction({ op: "local.get", index: nsecTmp });
2003
- this.pushInstruction({ op: "i64.const", value: 1000000000n });
2004
- this.pushInstruction("i64.add");
2005
- this.pushInstruction({ op: "local.set", index: nsecTmp });
2006
- this.pushInstruction("end");
2007
-
2008
- this.pushInstruction({ op: "local.get", index: newLocalSecTmp });
2009
- this.pushInstruction({ op: "local.get", index: p.offTmp });
2010
- this.pushInstruction("i64.extend_i32_s");
2011
- this.pushInstruction("i64.sub");
2012
- this.pushInstruction({ op: "i64.const", value: 1000000000n });
2013
- this.pushInstruction("i64.mul");
2014
- this.pushInstruction({ op: "local.get", index: nsecTmp });
2015
- this.pushInstruction("i64.add");
2016
-
2017
- this.pushInstruction({ op: "local.get", index: p.offTmp });
2018
- this.pushInstruction({ op: "local.get", index: p.rTmp });
2019
- this.pushInstruction({
2020
- op: "struct.get",
2021
- typeIndex: timeInfo.typeIndex,
2022
- fieldIndex: 2,
2023
- });
2024
- this.pushInstruction({ op: "struct.new", typeIndex: timeInfo.typeIndex });
2025
-
2026
- this.releaseTemp(nsecTmp, "i64");
2027
- this.releaseTemp(newLocalSecTmp, "i64");
2028
- this.releaseTemp(newDaysTmp, "i64");
2029
- this.releaseTemp(curD, "i32");
2030
- this.releaseTemp(curM, "i32");
2031
- this.releaseTemp(curY, "i32");
2032
- this.releaseTemp(daysAddTmp, "i32");
2033
- this.releaseTemp(monthsTmp, "i32");
2034
- this.releaseTemp(yearsTmp, "i32");
2035
- this._releaseTimeDecomposeParts(p);
2036
- }
2037
-
2038
- _emitTimeMethodMarshalText(expr, _targetWasmType) {
2039
- const rfcLit = {
2040
- kind: "BasicLit",
2041
- litKind: "STRING",
2042
- value: "2006-01-02T15:04:05.999999999Z07:00",
2043
- };
2044
- this._emitTimeMethodFormat(expr, [rfcLit], "externref");
2045
- this._emitStringToByteSlice({ elem: { kind: "basic", name: "byte" } });
2046
- this.pushInstruction({ op: "ref.null", heapType: "any" });
2047
- }
2048
-
2049
- _emitTimeMethodMarshalJSON(expr, _targetWasmType) {
2050
- const rfcLit = {
2051
- kind: "BasicLit",
2052
- litKind: "STRING",
2053
- value: "2006-01-02T15:04:05.999999999Z07:00",
2054
- };
2055
- this._emitTimeMethodFormat(expr, [rfcLit], "externref");
2056
- const strTmp = this.acquireTemp("externref");
2057
- this.pushInstruction({ op: "local.set", index: strTmp });
2058
- const qLit = { kind: "BasicLit", litKind: "STRING", value: '"' };
2059
- this.emitBasicLit(qLit, "externref");
2060
- this.pushInstruction({ op: "local.get", index: strTmp });
2061
- this.pushInstruction({
2062
- op: "call",
2063
- funcIndex: this.mod.getStringConcatImportIndex(),
2064
- });
2065
- this.emitBasicLit(qLit, "externref");
2066
- this.pushInstruction({
2067
- op: "call",
2068
- funcIndex: this.mod.getStringConcatImportIndex(),
2069
- });
2070
- this.releaseTemp(strTmp, "externref");
2071
- this._emitStringToByteSlice({ elem: { kind: "basic", name: "byte" } });
2072
- this.pushInstruction({ op: "ref.null", heapType: "any" });
2073
- }
2074
-
2075
- _emitTimeMethodUnmarshalText(expr, args, _targetWasmType) {
2076
- this._emitTimeMethodUnmarshalCore(expr, args[0]);
2077
- }
2078
-
2079
- _emitTimeMethodUnmarshalJSON(expr, args, _targetWasmType) {
2080
- this._emitTimeMethodUnmarshalCore(expr, args[0]);
2081
- }
2082
-
2083
- _emitTimeMethodUnmarshalCore(expr, dataArg) {
2084
- const timeInfo = this.mod.getStructType("time.Time");
2085
- const timeW = {
2086
- kind: "ref",
2087
- nullable: true,
2088
- typeIndex: timeInfo.typeIndex,
2089
- };
2090
- const rTmp = this.acquireTemp(timeW);
2091
- this.emitExpr(expr, timeW);
2092
- this.pushInstruction({ op: "local.set", index: rTmp });
2093
-
2094
- const byteSliceGo = dataArg._type ?? this._resolveExprGoType(dataArg);
2095
- this.emitExpr(dataArg, this.toWasmType(byteSliceGo));
2096
- this._emitByteSliceToString(byteSliceGo);
2097
-
2098
- const strTmp = this.acquireTemp("externref");
2099
- this.pushInstruction({ op: "local.set", index: strTmp });
2100
-
2101
- const rfcLit = {
2102
- kind: "BasicLit",
2103
- litKind: "STRING",
2104
- value: "2006-01-02T15:04:05.999999999Z07:00",
2105
- };
2106
- this.emitBasicLit(rfcLit, "externref");
2107
- this.pushInstruction({ op: "local.get", index: strTmp });
2108
- this._callStdlib("time_parse");
2109
-
2110
- const msTmp = this.acquireTemp("f64");
2111
- const offTmp = this.acquireTemp("i32");
2112
- const errStrTmp = this.acquireTemp("externref");
2113
- const resErrTmp = this.acquireTemp("anyref");
2114
-
2115
- this.pushInstruction({ op: "local.set", index: msTmp });
2116
- this._callStdlib("time_parse_offset");
2117
- this.pushInstruction({ op: "local.set", index: offTmp });
2118
- this._callStdlib("time_parse_err");
2119
- this.pushInstruction({ op: "local.set", index: errStrTmp });
2120
-
2121
- this.pushInstruction({ op: "local.get", index: errStrTmp });
2122
- this.pushInstruction("ref.is_null");
2123
- this.pushInstruction({ op: "if", blockType: "void" });
2124
-
2125
- this.pushInstruction({ op: "local.get", index: rTmp });
2126
- this.pushInstruction({ op: "local.get", index: msTmp });
2127
- this.pushInstruction("i64.trunc_sat_f64_s");
2128
- this.pushInstruction({ op: "i64.const", value: 1000000n });
2129
- this.pushInstruction("i64.mul");
2130
- this._callStdlib("time_parse_sub_ns");
2131
- this.pushInstruction("i64.extend_i32_s");
2132
- this.pushInstruction("i64.add");
2133
- this.pushInstruction({
2134
- op: "struct.set",
2135
- typeIndex: timeInfo.typeIndex,
2136
- fieldIndex: 0,
2137
- });
2138
-
2139
- this.pushInstruction({ op: "local.get", index: rTmp });
2140
- this.pushInstruction({ op: "local.get", index: offTmp });
2141
- this.pushInstruction({
2142
- op: "struct.set",
2143
- typeIndex: timeInfo.typeIndex,
2144
- fieldIndex: 1,
2145
- });
2146
-
2147
- this.pushInstruction({ op: "local.get", index: rTmp });
2148
- this.pushInstruction({ op: "ref.null", heapType: "any" });
2149
- this.pushInstruction({
2150
- op: "struct.set",
2151
- typeIndex: timeInfo.typeIndex,
2152
- fieldIndex: 2,
2153
- });
2154
-
2155
- this.pushInstruction({ op: "ref.null", heapType: "any" });
2156
- this.pushInstruction({ op: "local.set", index: resErrTmp });
2157
-
2158
- this.pushInstruction("else");
2159
-
2160
- this.pushInstruction({ op: "local.get", index: errStrTmp });
2161
- this.pushInstruction({ op: "ref.null", heapType: "any" });
2162
- this.pushInstruction({
2163
- op: "call",
2164
- funcIndex: this.mod.resolveFuncIndex("__mk_error"),
2165
- });
2166
- this.pushInstruction({ op: "local.set", index: resErrTmp });
2167
-
2168
- this.pushInstruction("end");
2169
-
2170
- this.pushInstruction({ op: "local.get", index: resErrTmp });
2171
-
2172
- this.releaseTemp(resErrTmp, "anyref");
2173
- this.releaseTemp(errStrTmp, "externref");
2174
- this.releaseTemp(offTmp, "i32");
2175
- this.releaseTemp(msTmp, "f64");
2176
- this.releaseTemp(strTmp, "externref");
2177
- this.releaseTemp(rTmp, timeW);
2178
- }
2179
-
2180
- emitTimeAuxMethodCall(recvTypeName, field, expr, _args, _targetWasmType) {
2181
- if (field === "String") {
2182
- if (recvTypeName === "time.Month") {
2183
- this.emitExpr(expr, "i32");
2184
- this._callStdlib("time_month_string");
2185
- return;
2186
- }
2187
- if (recvTypeName === "time.Weekday") {
2188
- this.emitExpr(expr, "i32");
2189
- this._callStdlib("time_weekday_string");
2190
- return;
2191
- }
2192
- if (recvTypeName === "time.Location") {
2193
- const locInfo = this.mod.getStructType("time.Location");
2194
- const locTmp = this.acquireTemp({
2195
- kind: "ref",
2196
- nullable: true,
2197
- typeIndex: locInfo.typeIndex,
2198
- });
2199
- this.emitExpr(expr, {
2200
- kind: "ref",
2201
- nullable: true,
2202
- typeIndex: locInfo.typeIndex,
2203
- });
2204
- this.pushInstruction({ op: "local.tee", index: locTmp });
2205
- this.pushInstruction("ref.is_null");
2206
- this.pushInstruction({ op: "if", blockType: "externref" });
2207
- this.emitBasicLit(
2208
- { kind: "BasicLit", litKind: "STRING", value: "UTC" },
2209
- "externref",
2210
- );
2211
- this.pushInstruction("else");
2212
- this.pushInstruction({ op: "local.get", index: locTmp });
2213
- this.pushInstruction({
2214
- op: "struct.get",
2215
- typeIndex: locInfo.typeIndex,
2216
- fieldIndex: 0,
2217
- });
2218
- this.pushInstruction("end");
2219
- this.releaseTemp(locTmp, {
2220
- kind: "ref",
2221
- nullable: true,
2222
- typeIndex: locInfo.typeIndex,
2223
- });
2224
- return;
2225
- }
2226
- }
2227
- throw new Error(`Unsupported method ${recvTypeName}.${field}`);
2228
- }
2229
- }