gofront 1.5.1 → 1.6.1

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 (91) hide show
  1. package/README.md +174 -466
  2. package/package.json +16 -20
  3. package/src/backend/js/expressions.js +288 -128
  4. package/src/backend/js/index.js +138 -56
  5. package/src/backend/js/runtime.js +1055 -53
  6. package/src/backend/js/source-map.js +32 -0
  7. package/src/backend/js/statements.js +151 -83
  8. package/src/backend/js/stdlib/base64.js +12 -0
  9. package/src/backend/js/stdlib/binary.js +102 -0
  10. package/src/backend/js/stdlib/bits.js +15 -1
  11. package/src/backend/js/stdlib/builder.js +28 -9
  12. package/src/backend/js/stdlib/bytes.js +75 -18
  13. package/src/backend/js/stdlib/errors.js +22 -3
  14. package/src/backend/js/stdlib/fmt.js +95 -20
  15. package/src/backend/js/stdlib/hex.js +17 -0
  16. package/src/backend/js/stdlib/html.js +1 -1
  17. package/src/backend/js/stdlib/index.js +19 -0
  18. package/src/backend/js/stdlib/io.js +105 -3
  19. package/src/backend/js/stdlib/maps.js +1 -1
  20. package/src/backend/js/stdlib/math.js +13 -1
  21. package/src/backend/js/stdlib/path.js +20 -6
  22. package/src/backend/js/stdlib/rand.js +10 -5
  23. package/src/backend/js/stdlib/sha256.js +12 -0
  24. package/src/backend/js/stdlib/shared.js +17 -0
  25. package/src/backend/js/stdlib/slices.js +53 -27
  26. package/src/backend/js/stdlib/sort.js +1 -1
  27. package/src/backend/js/stdlib/strconv.js +52 -11
  28. package/src/backend/js/stdlib/strings.js +152 -16
  29. package/src/backend/js/stdlib/sync.js +13 -0
  30. package/src/backend/js/stdlib/testing.js +2 -2
  31. package/src/backend/js/stdlib/time.js +93 -22
  32. package/src/backend/js/stdlib/unicode.js +2 -2
  33. package/src/backend/js/templ.js +1 -1
  34. package/src/backend/wasm/boundary.js +1677 -58
  35. package/src/backend/wasm/const-fold.js +205 -0
  36. package/src/backend/wasm/emit-builtins.js +1230 -0
  37. package/src/backend/wasm/emit-exprs.js +2096 -0
  38. package/src/backend/wasm/emit-maps.js +1999 -0
  39. package/src/backend/wasm/emit-shared.js +519 -0
  40. package/src/backend/wasm/emit-stdlib.js +13 -0
  41. package/src/backend/wasm/emit-stmts.js +2466 -0
  42. package/src/backend/wasm/emit.js +656 -5031
  43. package/src/backend/wasm/encode.js +259 -85
  44. package/src/backend/wasm/glue.js +19 -5
  45. package/src/backend/wasm/index.js +2432 -86
  46. package/src/backend/wasm/monomorph.js +711 -0
  47. package/src/backend/wasm/optimize.js +236 -0
  48. package/src/backend/wasm/stdlib/bytes.js +5039 -0
  49. package/src/backend/wasm/stdlib/core.js +8903 -0
  50. package/src/backend/wasm/stdlib/index.js +40 -0
  51. package/src/backend/wasm/stdlib/io.js +2161 -0
  52. package/src/backend/wasm/stdlib/packages.js +2400 -0
  53. package/src/backend/wasm/stdlib/pure.js +720 -0
  54. package/src/backend/wasm/stdlib/time.js +1200 -0
  55. package/src/backend/wasm/types.js +312 -36
  56. package/src/backend/wasm/wat.js +49 -2
  57. package/src/cli-core.js +276 -73
  58. package/src/compiler.js +162 -25
  59. package/src/dts-parser.js +79 -126
  60. package/src/index.js +88 -121
  61. package/src/lexer.js +80 -29
  62. package/src/lower/boxing.js +4 -6
  63. package/src/lower/captures.js +12 -23
  64. package/src/lower/escape.js +4 -9
  65. package/src/lower/functions.js +277 -5
  66. package/src/lower/globals.js +7 -15
  67. package/src/lower/index.js +5 -2
  68. package/src/lower/ownership.js +35 -19
  69. package/src/lower/range.js +2 -5
  70. package/src/lower/walk.js +25 -0
  71. package/src/minifier.js +20 -68
  72. package/src/parser/declarations.js +3 -6
  73. package/src/parser/expressions.js +3 -8
  74. package/src/parser/index.js +18 -3
  75. package/src/parser/statements.js +25 -38
  76. package/src/parser/types.js +1 -2
  77. package/src/resolver.js +3 -27
  78. package/src/templ-lexer.js +29 -63
  79. package/src/templ-parser.js +22 -45
  80. package/src/test-runner.js +20 -0
  81. package/src/typechecker/assignability.js +123 -14
  82. package/src/typechecker/expressions.js +244 -4
  83. package/src/typechecker/index.js +27 -1
  84. package/src/typechecker/resolve.js +71 -12
  85. package/src/typechecker/statements.js +0 -7
  86. package/src/typechecker/stdlib/core.js +292 -103
  87. package/src/typechecker/stdlib/extended.js +471 -4
  88. package/src/typechecker/stdlib/shared.js +53 -0
  89. package/src/typechecker/stdlib.js +2 -0
  90. package/src/typechecker/types.js +545 -8
  91. package/src/vendor.js +2 -2
@@ -83,11 +83,542 @@ export const BROWSER_GLOBALS = new Set([
83
83
  "prompt",
84
84
  "localStorage",
85
85
  "sessionStorage",
86
+ "WebSocket",
87
+ "Worker",
88
+ "HTMLElement",
89
+ "Element",
90
+ "Event",
91
+ "CustomEvent",
92
+ "URL",
93
+ "URLSearchParams",
94
+ "FormData",
95
+ "Headers",
96
+ "Request",
97
+ "Response",
98
+ "Blob",
99
+ "File",
100
+ "FileReader",
101
+ "TextEncoder",
102
+ "TextDecoder",
86
103
  ]);
87
104
 
88
- // Stdlib packages the WASM backend can emit. Extend as src/backend/wasm/emit.js gains support;
89
- // anything not listed is rejected at import with "not yet available in wasm packages".
90
- export const WASM_SUPPORTED_STDLIB = new Set(["math", "math/bits", "testing"]);
105
+ // Stdlib packages the WASM backend can emit. Extend as src/backend/wasm/emit-stdlib.js
106
+ // gains support; anything not listed is rejected at import with "not yet available in
107
+ // wasm packages".
108
+ export const WASM_SUPPORTED_STDLIB = new Set([
109
+ "math",
110
+ "math/bits",
111
+ "math/rand",
112
+ "errors",
113
+ "fmt",
114
+ "strings",
115
+ "bytes",
116
+ "strconv",
117
+ "unicode",
118
+ "unicode/utf8",
119
+ "slices",
120
+ "maps",
121
+ "sort",
122
+ "path",
123
+ "path/filepath",
124
+ "html",
125
+ "io",
126
+ "time",
127
+ "testing",
128
+ "gofront/shared",
129
+ "sync",
130
+ "crypto/rand",
131
+ "crypto/sha256",
132
+ "encoding/binary",
133
+ "encoding/hex",
134
+ "encoding/base64",
135
+ ]);
136
+
137
+ export const STDLIB_PACKAGES = new Set([
138
+ ...WASM_SUPPORTED_STDLIB,
139
+ "os",
140
+ "gom",
141
+ "regexp",
142
+ ]);
143
+
144
+ // Per-namespace member subset the WASM backend implements. `null` means every
145
+ // member the checker knows about. Members outside the set are rejected in
146
+ // wasm/both packages with "'strings.Split' is not yet available in wasm packages".
147
+ export const WASM_STDLIB_MEMBERS = new Map([
148
+ [
149
+ "math",
150
+ new Set([
151
+ "Sqrt",
152
+ "Floor",
153
+ "Ceil",
154
+ "Trunc",
155
+ "Abs",
156
+ "Min",
157
+ "Max",
158
+ "Copysign",
159
+ "Dim",
160
+ "Inf",
161
+ "NaN",
162
+ "IsNaN",
163
+ "IsInf",
164
+ "Signbit",
165
+ "Sin",
166
+ "Cos",
167
+ "Tan",
168
+ "Asin",
169
+ "Acos",
170
+ "Atan",
171
+ "Atan2",
172
+ "Pow",
173
+ "Mod",
174
+ "Hypot",
175
+ "Cbrt",
176
+ "Exp",
177
+ "Exp2",
178
+ "Log",
179
+ "Log2",
180
+ "Log10",
181
+ "Round",
182
+ "Remainder",
183
+ "Float64bits",
184
+ "Float64frombits",
185
+ "Float32bits",
186
+ "Float32frombits",
187
+ "Sinh",
188
+ "Cosh",
189
+ "Tanh",
190
+ // constants
191
+ "E",
192
+ "Pi",
193
+ "Phi",
194
+ "Sqrt2",
195
+ "SqrtE",
196
+ "SqrtPi",
197
+ "SqrtPhi",
198
+ "Ln2",
199
+ "Log2E",
200
+ "Ln10",
201
+ "Log10E",
202
+ "MaxFloat32",
203
+ "SmallestNonzeroFloat32",
204
+ "MaxFloat64",
205
+ "SmallestNonzeroFloat64",
206
+ "MaxInt",
207
+ "MinInt",
208
+ "MaxInt8",
209
+ "MinInt8",
210
+ "MaxInt16",
211
+ "MinInt16",
212
+ "MaxInt32",
213
+ "MinInt32",
214
+ "MaxInt64",
215
+ "MinInt64",
216
+ "MaxUint8",
217
+ "MaxUint16",
218
+ "MaxUint32",
219
+ "MaxUint",
220
+ "MaxUint64",
221
+ ]),
222
+ ],
223
+ [
224
+ "bits",
225
+ new Set([
226
+ "LeadingZeros32",
227
+ "LeadingZeros64",
228
+ "TrailingZeros32",
229
+ "TrailingZeros64",
230
+ "OnesCount32",
231
+ "OnesCount64",
232
+ "RotateLeft32",
233
+ "RotateLeft64",
234
+ "ReverseBytes16",
235
+ "ReverseBytes32",
236
+ "ReverseBytes64",
237
+ "Len32",
238
+ "Len64",
239
+ "Reverse32",
240
+ "Reverse64",
241
+ ]),
242
+ ],
243
+ ["errors", new Set(["New", "Is", "Unwrap", "As", "Join"])],
244
+ [
245
+ "utf8",
246
+ new Set([
247
+ "RuneCountInString",
248
+ "RuneLen",
249
+ "ValidString",
250
+ "ValidRune",
251
+ "DecodeRuneInString",
252
+ "DecodeLastRuneInString",
253
+ "FullRuneInString",
254
+ "RuneError",
255
+ "MaxRune",
256
+ "UTFMax",
257
+ ]),
258
+ ],
259
+ [
260
+ "sort",
261
+ new Set([
262
+ "Ints",
263
+ "Float64s",
264
+ "Strings",
265
+ "Slice",
266
+ "SliceStable",
267
+ "SliceIsSorted",
268
+ "Search",
269
+ "IntsAreSorted",
270
+ "Float64sAreSorted",
271
+ "StringsAreSorted",
272
+ ]),
273
+ ],
274
+ [
275
+ "fmt",
276
+ new Set([
277
+ "Sprintf",
278
+ "Errorf",
279
+ "Printf",
280
+ "Println",
281
+ "Print",
282
+ "Fprintf",
283
+ "Fprintln",
284
+ "Fprint",
285
+ "Sscan",
286
+ "Sscanln",
287
+ "Sscanf",
288
+ ]),
289
+ ],
290
+ [
291
+ "strings",
292
+ new Set([
293
+ "Contains",
294
+ "ContainsAny",
295
+ "ContainsRune",
296
+ "HasPrefix",
297
+ "HasSuffix",
298
+ "Index",
299
+ "IndexAny",
300
+ "IndexByte",
301
+ "IndexRune",
302
+ "LastIndex",
303
+ "LastIndexByte",
304
+ "Count",
305
+ "Repeat",
306
+ "Replace",
307
+ "ReplaceAll",
308
+ "ToUpper",
309
+ "ToLower",
310
+ "Title",
311
+ "ToTitle",
312
+ "TrimSpace",
313
+ "Trim",
314
+ "TrimLeft",
315
+ "TrimRight",
316
+ "TrimPrefix",
317
+ "TrimSuffix",
318
+ "EqualFold",
319
+ "Split",
320
+ "Fields",
321
+ "Join",
322
+ "Cut",
323
+ "CutPrefix",
324
+ "CutSuffix",
325
+ "SplitN",
326
+ "SplitAfter",
327
+ "SplitAfterN",
328
+ "LastIndexAny",
329
+ "Map",
330
+ "IndexFunc",
331
+ "LastIndexFunc",
332
+ "TrimFunc",
333
+ "TrimLeftFunc",
334
+ "TrimRightFunc",
335
+ "FieldsFunc",
336
+ "NewReplacer",
337
+ "Builder",
338
+ "NewReader",
339
+ "Reader",
340
+ ]),
341
+ ],
342
+ [
343
+ "strconv",
344
+ new Set([
345
+ "Itoa",
346
+ "Atoi",
347
+ "FormatInt",
348
+ "FormatUint",
349
+ "FormatFloat",
350
+ "FormatBool",
351
+ "ParseInt",
352
+ "ParseUint",
353
+ "ParseFloat",
354
+ "ParseBool",
355
+ "Quote",
356
+ "Unquote",
357
+ "AppendInt",
358
+ "AppendUint",
359
+ "AppendFloat",
360
+ "AppendBool",
361
+ "ErrSyntax",
362
+ "ErrRange",
363
+ ]),
364
+ ],
365
+ [
366
+ "slices",
367
+ new Set([
368
+ "Sort",
369
+ "Reverse",
370
+ "Contains",
371
+ "Index",
372
+ "IndexFunc",
373
+ "ContainsFunc",
374
+ "BinarySearch",
375
+ "BinarySearchFunc",
376
+ "Equal",
377
+ "Clone",
378
+ "Compare",
379
+ "SortFunc",
380
+ "SortStableFunc",
381
+ "IsSorted",
382
+ "IsSortedFunc",
383
+ "Min",
384
+ "Max",
385
+ "MinFunc",
386
+ "MaxFunc",
387
+ "Compact",
388
+ "CompactFunc",
389
+ "Concat",
390
+ "Delete",
391
+ "DeleteFunc",
392
+ "Insert",
393
+ "Replace",
394
+ "Grow",
395
+ "Clip",
396
+ ]),
397
+ ],
398
+ [
399
+ "maps",
400
+ new Set([
401
+ "Keys",
402
+ "Values",
403
+ "Clone",
404
+ "Copy",
405
+ "Equal",
406
+ "EqualFunc",
407
+ "Delete",
408
+ "DeleteFunc",
409
+ ]),
410
+ ],
411
+ [
412
+ "unicode",
413
+ new Set([
414
+ "IsLetter",
415
+ "IsDigit",
416
+ "IsSpace",
417
+ "IsUpper",
418
+ "IsLower",
419
+ "IsPunct",
420
+ "IsControl",
421
+ "IsPrint",
422
+ "IsGraphic",
423
+ "ToUpper",
424
+ "ToLower",
425
+ ]),
426
+ ],
427
+ [
428
+ "rand",
429
+ new Set([
430
+ "Intn",
431
+ "Float64",
432
+ "Float32",
433
+ "Int",
434
+ "Int63",
435
+ "Int63n",
436
+ "Int31",
437
+ "Int31n",
438
+ "Seed",
439
+ "Shuffle",
440
+ "Perm",
441
+ "Read",
442
+ "Reader",
443
+ ]),
444
+ ],
445
+ [
446
+ "bytes",
447
+ new Set([
448
+ "Contains",
449
+ "HasPrefix",
450
+ "HasSuffix",
451
+ "Index",
452
+ "Count",
453
+ "Repeat",
454
+ "Replace",
455
+ "ReplaceAll",
456
+ "ToUpper",
457
+ "ToLower",
458
+ "TrimSpace",
459
+ "Trim",
460
+ "Equal",
461
+ "Split",
462
+ "Join",
463
+ "TrimPrefix",
464
+ "TrimSuffix",
465
+ "TrimLeft",
466
+ "TrimRight",
467
+ "TrimFunc",
468
+ "IndexByte",
469
+ "LastIndex",
470
+ "LastIndexByte",
471
+ "Fields",
472
+ "FieldsFunc",
473
+ "Clone",
474
+ "Cut",
475
+ "ToTitle",
476
+ "ContainsAny",
477
+ "ContainsRune",
478
+ "Map",
479
+ "SplitN",
480
+ "Buffer",
481
+ "NewReader",
482
+ "Reader",
483
+ ]),
484
+ ],
485
+ [
486
+ "path",
487
+ new Set([
488
+ "Base",
489
+ "Dir",
490
+ "Ext",
491
+ "Join",
492
+ "Clean",
493
+ "IsAbs",
494
+ "Split",
495
+ "Match",
496
+ "ErrBadPattern",
497
+ ]),
498
+ ],
499
+ [
500
+ "filepath",
501
+ new Set([
502
+ "Base",
503
+ "Dir",
504
+ "Ext",
505
+ "Join",
506
+ "Clean",
507
+ "IsAbs",
508
+ "Split",
509
+ "Match",
510
+ "ErrBadPattern",
511
+ ]),
512
+ ],
513
+ ["html", new Set(["EscapeString", "UnescapeString"])],
514
+ [
515
+ "io",
516
+ new Set([
517
+ "Writer",
518
+ "Reader",
519
+ "ReadWriter",
520
+ "Closer",
521
+ "EOF",
522
+ "ErrUnexpectedEOF",
523
+ "ErrShortWrite",
524
+ "ReadFull",
525
+ "ReadAll",
526
+ "Discard",
527
+ "WriteString",
528
+ "LimitReader",
529
+ "Copy",
530
+ "CopyN",
531
+ ]),
532
+ ],
533
+ [
534
+ "time",
535
+ new Set([
536
+ "Now",
537
+ "Since",
538
+ "Parse",
539
+ "Unix",
540
+ "UnixMicro",
541
+ "UnixMilli",
542
+ "Date",
543
+ "Duration",
544
+ "Nanosecond",
545
+ "Microsecond",
546
+ "Millisecond",
547
+ "Second",
548
+ "Minute",
549
+ "Hour",
550
+ "RFC3339",
551
+ "RFC3339Nano",
552
+ "DateOnly",
553
+ "TimeOnly",
554
+ "DateTime",
555
+ "UTC",
556
+ "Local",
557
+ "January",
558
+ "February",
559
+ "March",
560
+ "April",
561
+ "May",
562
+ "June",
563
+ "July",
564
+ "August",
565
+ "September",
566
+ "October",
567
+ "November",
568
+ "December",
569
+ "Sunday",
570
+ "Monday",
571
+ "Tuesday",
572
+ "Wednesday",
573
+ "Thursday",
574
+ "Friday",
575
+ "Saturday",
576
+ "Time",
577
+ ]),
578
+ ],
579
+ ["testing", new Set(["T"])],
580
+ ["sync", new Set(["Mutex", "RWMutex", "Once", "WaitGroup", "Locker"])],
581
+ ["sha256", new Set(["Sum256", "New", "Size", "BlockSize"])],
582
+ [
583
+ "binary",
584
+ new Set([
585
+ "LittleEndian",
586
+ "BigEndian",
587
+ "ByteOrder",
588
+ "Read",
589
+ "Write",
590
+ "Size",
591
+ "AppendUint16",
592
+ "AppendUint32",
593
+ "AppendUint64",
594
+ ]),
595
+ ],
596
+ [
597
+ "hex",
598
+ new Set([
599
+ "EncodeToString",
600
+ "DecodeString",
601
+ "Encode",
602
+ "Decode",
603
+ "EncodedLen",
604
+ "DecodedLen",
605
+ "ErrLength",
606
+ ]),
607
+ ],
608
+ [
609
+ "base64",
610
+ new Set([
611
+ "StdEncoding",
612
+ "URLEncoding",
613
+ "RawStdEncoding",
614
+ "RawURLEncoding",
615
+ "Encoding",
616
+ "NewEncoding",
617
+ "StdPadding",
618
+ "NoPadding",
619
+ ]),
620
+ ],
621
+ ]);
91
622
 
92
623
  export const VALID_TARGETS = new Set(["js", "wasm", "both"]);
93
624
 
@@ -108,7 +639,7 @@ export const BYTE = UINT8;
108
639
  export const UINT16 = { kind: "basic", name: "uint16" };
109
640
  export const UINT32 = { kind: "basic", name: "uint32" };
110
641
  export const UINT64 = { kind: "basic", name: "uint64" };
111
- export const UINTPTR = { kind: "basic", name: "uintptr" };
642
+ const UINTPTR = { kind: "basic", name: "uintptr" };
112
643
  export const RUNE = INT32;
113
644
  export const FLOAT32 = { kind: "basic", name: "float32" };
114
645
  export const FLOAT64 = { kind: "basic", name: "float64" };
@@ -135,7 +666,7 @@ export const UNTYPED_BOOL = { kind: "untyped", base: "bool" };
135
666
 
136
667
  // ── Complex types ────────────────────────────────────────────
137
668
  export const COMPLEX128 = { kind: "basic", name: "complex128" };
138
- export const COMPLEX64 = { kind: "basic", name: "complex64" };
669
+ const COMPLEX64 = { kind: "basic", name: "complex64" };
139
670
  export const UNTYPED_COMPLEX = { kind: "untyped", base: "complex128" };
140
671
 
141
672
  export const BASIC_TYPES = {
@@ -204,7 +735,7 @@ export const isBool = makeBasicPredicate("bool");
204
735
 
205
736
  // ── TypedArray mappings ──────────────────────────────────────
206
737
  // []float64 deliberately stays a plain Array for JSON / JS-library interop.
207
- export const TYPED_ARRAY_CONSTRUCTORS = {
738
+ const TYPED_ARRAY_CONSTRUCTORS = {
208
739
  float32: "Float32Array",
209
740
  uint8: "Uint8Array",
210
741
  byte: "Uint8Array",
@@ -294,12 +825,16 @@ export function typeStr(t) {
294
825
  return `untyped ${t.base}`;
295
826
  case "slice":
296
827
  return `[]${typeStr(t.elem)}`;
828
+ case "shared":
829
+ return `shared.${t.shared}`;
297
830
  case "array":
298
831
  return `[${t.size ?? "..."}]${typeStr(t.elem)}`;
299
832
  case "map":
300
833
  return `map[${typeStr(t.key)}]${typeStr(t.value)}`;
301
834
  case "struct":
302
- return t.name || "struct{...}";
835
+ return t.typeArgs && t.typeArgs.length > 0
836
+ ? `${t.name || "struct"}[${t.typeArgs.map(typeStr).join(", ")}]`
837
+ : t.name || "struct{...}";
303
838
  case "interface":
304
839
  return t.name || "interface{...}";
305
840
  case "namespace":
@@ -309,7 +844,9 @@ export function typeStr(t) {
309
844
  case "tuple":
310
845
  return `(${t.types.map(typeStr).join(", ")})`;
311
846
  case "named":
312
- return t.name;
847
+ return t.typeArgs && t.typeArgs.length > 0
848
+ ? `${t.name}[${t.typeArgs.map(typeStr).join(", ")}]`
849
+ : t.name;
313
850
  case "pointer":
314
851
  return `*${typeStr(t.base)}`;
315
852
  case "typeParam":
package/src/vendor.js CHANGED
@@ -59,7 +59,7 @@ ${exports.join(",\n")}
59
59
  `;
60
60
  }
61
61
 
62
- export function findBundler(projectDir) {
62
+ function findBundler(projectDir) {
63
63
  const root = resolve(projectDir);
64
64
  const pkgPath = join(root, "package.json");
65
65
  let req;
@@ -136,7 +136,7 @@ export function loadVendorConfig(projectDir) {
136
136
  return { dest, packages, minify, globals };
137
137
  }
138
138
 
139
- export function resolveDestinationPaths(projectRoot, dest) {
139
+ function resolveDestinationPaths(projectRoot, dest) {
140
140
  const destList = Array.isArray(dest) ? dest : [dest];
141
141
  if (destList.length === 0) {
142
142
  throw new Error(