gofront 1.7.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 (194) hide show
  1. package/README.md +14 -14
  2. package/package.json +10 -38
  3. package/src/backend/js/expressions.js +2060 -389
  4. package/src/backend/js/index.js +586 -174
  5. package/src/backend/js/natives.js +383 -0
  6. package/src/backend/js/runtime.js +936 -1644
  7. package/src/backend/js/statements.js +1349 -132
  8. package/src/backend/js/stdlib/index.js +51 -94
  9. package/src/backend/js/templ.js +67 -118
  10. package/src/backend/js/type-info.js +436 -0
  11. package/src/backend/shared/fixed-float.js +20 -0
  12. package/src/backend/shared/fmt-types.js +80 -0
  13. package/src/backend/shared/host-unicode.js +36 -0
  14. package/src/backend/wasm/boundary.js +753 -872
  15. package/src/backend/wasm/emit-builtins.js +914 -237
  16. package/src/backend/wasm/emit-exprs.js +1456 -195
  17. package/src/backend/wasm/emit-maps.js +78 -380
  18. package/src/backend/wasm/emit-stmts.js +1659 -214
  19. package/src/backend/wasm/emit.js +614 -88
  20. package/src/backend/wasm/encode.js +43 -13
  21. package/src/backend/wasm/globals.js +491 -0
  22. package/src/backend/wasm/glue.js +2 -4
  23. package/src/backend/wasm/index.js +406 -3195
  24. package/src/backend/wasm/method-expr.js +392 -0
  25. package/src/backend/wasm/module-emitter.js +1615 -0
  26. package/src/backend/wasm/monomorph.js +164 -9
  27. package/src/backend/wasm/natives.js +596 -0
  28. package/src/backend/wasm/prescan.js +249 -0
  29. package/src/backend/wasm/prune-funcs.js +89 -0
  30. package/src/backend/wasm/stdlib/index.js +2 -37
  31. package/src/backend/wasm/stdlib/simd.js +293 -28
  32. package/src/backend/wasm/types.js +260 -27
  33. package/src/backend/wasm/variadic.js +93 -0
  34. package/src/backend/wasm/wat.js +47 -20
  35. package/src/{cli-core.js → cli/cli-core.js} +241 -11
  36. package/src/{test-runner.js → cli/test-runner.js} +188 -122
  37. package/src/{vendor.js → cli/vendor.js} +3 -4
  38. package/src/compiler.js +202 -386
  39. package/src/index.js +15 -169
  40. package/src/{lexer.js → lexer/lexer.js} +203 -62
  41. package/src/{templ-lexer.js → lexer/templ-lexer.js} +61 -18
  42. package/src/{tokens.js → lexer/tokens.js} +2 -0
  43. package/src/link/sourcemap.js +97 -0
  44. package/src/link/stdlib.js +737 -0
  45. package/src/link/type-names.js +148 -0
  46. package/src/link/wasm.js +318 -0
  47. package/src/lower/bounds.js +547 -0
  48. package/src/lower/boxing.js +1 -1
  49. package/src/lower/captures.js +267 -64
  50. package/src/lower/embedding.js +32 -3
  51. package/src/lower/errors-as.js +196 -0
  52. package/src/lower/fmt-formatters.js +905 -0
  53. package/src/lower/functions.js +2 -0
  54. package/src/lower/gen-types.js +274 -0
  55. package/src/lower/index.js +33 -19
  56. package/src/lower/json-codecs.js +1379 -0
  57. package/src/lower/ownership.js +47 -8
  58. package/src/lower/range.js +86 -1
  59. package/src/lower/reachability.js +150 -0
  60. package/src/lower/walk.js +12 -0
  61. package/src/parser/declarations.js +83 -15
  62. package/src/parser/expressions.js +110 -18
  63. package/src/parser/index.js +2 -1
  64. package/src/parser/statements.js +38 -22
  65. package/src/{templ-parser.js → parser/templ.js} +179 -17
  66. package/src/parser/types.js +70 -9
  67. package/src/resolver.js +21 -1
  68. package/src/typechecker/assignability.js +291 -19
  69. package/src/typechecker/const-fold.js +399 -0
  70. package/src/typechecker/constants.js +282 -0
  71. package/src/typechecker/expressions.js +1220 -183
  72. package/src/typechecker/index.js +659 -92
  73. package/src/typechecker/init-order.js +426 -0
  74. package/src/typechecker/operand-text.js +40 -0
  75. package/src/typechecker/resolve.js +613 -62
  76. package/src/typechecker/statements.js +212 -20
  77. package/src/typechecker/stdlib/excluded.js +902 -0
  78. package/src/typechecker/stdlib/index.js +38 -0
  79. package/src/typechecker/stdlib/simd.js +65 -15
  80. package/src/typechecker/termination.js +11 -2
  81. package/src/typechecker/types.js +268 -514
  82. package/stdlib/bytes/buffer.go +479 -0
  83. package/stdlib/bytes/bytes.go +1010 -0
  84. package/stdlib/bytes/reader.go +161 -0
  85. package/stdlib/cmp/cmp.go +65 -0
  86. package/stdlib/context/context.go +115 -0
  87. package/stdlib/crypto/rand/rand.go +34 -0
  88. package/stdlib/crypto/sha256/sha256.go +249 -0
  89. package/stdlib/encoding/base64/base64.go +331 -0
  90. package/stdlib/encoding/binary/binary.go +332 -0
  91. package/stdlib/encoding/hex/hex.go +140 -0
  92. package/stdlib/encoding/json/errors.go +31 -0
  93. package/stdlib/encoding/json/indent.go +71 -0
  94. package/stdlib/encoding/json/json.go +62 -0
  95. package/stdlib/encoding/json/number.go +12 -0
  96. package/stdlib/encoding/json/stream.go +173 -0
  97. package/stdlib/errors/errors.go +174 -0
  98. package/stdlib/fmt/errors.go +110 -0
  99. package/stdlib/fmt/format.go +732 -0
  100. package/stdlib/fmt/print.go +1232 -0
  101. package/stdlib/gom/gom.go +293 -0
  102. package/stdlib/hash/hash.go +53 -0
  103. package/stdlib/html/html.go +251 -0
  104. package/stdlib/internal/fmtrt/fmtrt.go +163 -0
  105. package/stdlib/internal/jsonrt/dec.go +871 -0
  106. package/stdlib/internal/jsonrt/enc.go +516 -0
  107. package/stdlib/internal/jsonrt/indent.go +135 -0
  108. package/stdlib/internal/jsonrt/number.go +27 -0
  109. package/stdlib/internal/jsonrt/reader.go +73 -0
  110. package/stdlib/internal/jsonrt/scanner.go +658 -0
  111. package/stdlib/io/io.go +502 -0
  112. package/stdlib/iter/iter.go +18 -0
  113. package/stdlib/maps/maps.go +131 -0
  114. package/stdlib/math/bits/bits.go +507 -0
  115. package/stdlib/math/extra.go +272 -0
  116. package/stdlib/math/math.go +439 -0
  117. package/stdlib/math/rand/rand.go +297 -0
  118. package/stdlib/os/os.go +93 -0
  119. package/stdlib/path/filepath/filepath.go +47 -0
  120. package/stdlib/path/path.go +394 -0
  121. package/stdlib/regexp/regexp.go +231 -0
  122. package/stdlib/slices/slices.go +526 -0
  123. package/stdlib/slices/sort.go +209 -0
  124. package/stdlib/slices/zsort_cmp.go +480 -0
  125. package/stdlib/slices/zsort_ordered.go +482 -0
  126. package/stdlib/sort/sort.go +500 -0
  127. package/stdlib/sort/zsort_func.go +480 -0
  128. package/stdlib/sort/zsort_iface.go +480 -0
  129. package/stdlib/strconv/float.go +1699 -0
  130. package/stdlib/strconv/isprint.go +752 -0
  131. package/stdlib/strconv/quote.go +526 -0
  132. package/stdlib/strconv/strconv.go +616 -0
  133. package/stdlib/strings/builder.go +95 -0
  134. package/stdlib/strings/reader.go +163 -0
  135. package/stdlib/strings/replacer.go +102 -0
  136. package/stdlib/strings/strings.go +807 -0
  137. package/stdlib/sync/sync.go +268 -0
  138. package/stdlib/testing/async_js.go +61 -0
  139. package/stdlib/testing/benchmark.go +297 -0
  140. package/stdlib/testing/testing.go +523 -0
  141. package/stdlib/time/duration.go +233 -0
  142. package/stdlib/time/format.go +745 -0
  143. package/stdlib/time/parse.go +790 -0
  144. package/stdlib/time/sleep_js.go +63 -0
  145. package/stdlib/time/time.go +834 -0
  146. package/stdlib/unicode/unicode.go +250 -0
  147. package/stdlib/unicode/utf8/utf8.go +464 -0
  148. package/src/backend/js/stdlib/base64.js +0 -12
  149. package/src/backend/js/stdlib/binary.js +0 -102
  150. package/src/backend/js/stdlib/bits.js +0 -46
  151. package/src/backend/js/stdlib/builder.js +0 -63
  152. package/src/backend/js/stdlib/bytes.js +0 -122
  153. package/src/backend/js/stdlib/errors.js +0 -44
  154. package/src/backend/js/stdlib/fmt.js +0 -178
  155. package/src/backend/js/stdlib/gom.js +0 -155
  156. package/src/backend/js/stdlib/hex.js +0 -17
  157. package/src/backend/js/stdlib/html.js +0 -20
  158. package/src/backend/js/stdlib/io.js +0 -137
  159. package/src/backend/js/stdlib/json.js +0 -972
  160. package/src/backend/js/stdlib/maps.js +0 -45
  161. package/src/backend/js/stdlib/math.js +0 -69
  162. package/src/backend/js/stdlib/os.js +0 -13
  163. package/src/backend/js/stdlib/path.js +0 -48
  164. package/src/backend/js/stdlib/rand.js +0 -32
  165. package/src/backend/js/stdlib/regexp.js +0 -54
  166. package/src/backend/js/stdlib/sha256.js +0 -12
  167. package/src/backend/js/stdlib/shared.js +0 -17
  168. package/src/backend/js/stdlib/simd.js +0 -24
  169. package/src/backend/js/stdlib/slices.js +0 -77
  170. package/src/backend/js/stdlib/sort.js +0 -30
  171. package/src/backend/js/stdlib/strconv.js +0 -74
  172. package/src/backend/js/stdlib/strings.js +0 -222
  173. package/src/backend/js/stdlib/sync.js +0 -13
  174. package/src/backend/js/stdlib/testing.js +0 -34
  175. package/src/backend/js/stdlib/time.js +0 -128
  176. package/src/backend/js/stdlib/unicode.js +0 -25
  177. package/src/backend/js/stdlib/utf8.js +0 -26
  178. package/src/backend/wasm/const-fold.js +0 -205
  179. package/src/backend/wasm/emit-stdlib.js +0 -13
  180. package/src/backend/wasm/stdlib/bytes.js +0 -5039
  181. package/src/backend/wasm/stdlib/core.js +0 -8903
  182. package/src/backend/wasm/stdlib/io.js +0 -2161
  183. package/src/backend/wasm/stdlib/packages.js +0 -2400
  184. package/src/backend/wasm/stdlib/pure.js +0 -720
  185. package/src/backend/wasm/stdlib/time.js +0 -1200
  186. package/src/typechecker/stdlib/core.js +0 -801
  187. package/src/typechecker/stdlib/extended.js +0 -914
  188. package/src/typechecker/stdlib.js +0 -22
  189. /package/src/{asset-manager.js → cli/asset-manager.js} +0 -0
  190. /package/src/{colors.js → cli/colors.js} +0 -0
  191. /package/src/{dev-server.js → cli/dev-server.js} +0 -0
  192. /package/src/{minifier.js → cli/minifier.js} +0 -0
  193. /package/src/{project-config.js → cli/project-config.js} +0 -0
  194. /package/src/{pwa.js → cli/pwa.js} +0 -0
@@ -0,0 +1,140 @@
1
+ //go:build gofront
2
+
3
+ //gofront:target both
4
+
5
+ // Copyright 2009 The Go Authors. All rights reserved.
6
+ // Use of this source code is governed by a BSD-style
7
+ // license that can be found in the LICENSE file.
8
+
9
+ // Package hex implements hexadecimal encoding and decoding.
10
+ package hex
11
+
12
+ import (
13
+ "errors"
14
+ "fmt"
15
+ )
16
+
17
+ var hextable = []byte("0123456789abcdef")
18
+
19
+ // ErrLength reports an attempt to decode an odd-length input
20
+ // using [Decode] or [DecodeString].
21
+ // The stream-based Decoder returns [io.ErrUnexpectedEOF] instead of ErrLength.
22
+ var ErrLength = errors.New("encoding/hex: odd length hex string")
23
+
24
+ // InvalidByteError values describe errors resulting from an invalid byte in a hex string.
25
+ type InvalidByteError byte
26
+
27
+ func (e InvalidByteError) Error() string {
28
+ return fmt.Sprintf("encoding/hex: invalid byte: %#U", rune(e))
29
+ }
30
+
31
+ // EncodedLen returns the length of an encoding of n source bytes.
32
+ // Specifically, it returns n * 2.
33
+ func EncodedLen(n int) int { return n * 2 }
34
+
35
+ // Encode encodes src into [EncodedLen](len(src))
36
+ // bytes of dst. As a convenience, it returns the number
37
+ // of bytes written to dst, but this value is always [EncodedLen](len(src)).
38
+ // Encode implements hexadecimal encoding.
39
+ func Encode(dst, src []byte) int {
40
+ j := 0
41
+ for _, v := range src {
42
+ dst[j] = hextable[v>>4]
43
+ dst[j+1] = hextable[v&0x0f]
44
+ j += 2
45
+ }
46
+ return len(src) * 2
47
+ }
48
+
49
+ // AppendEncode appends the hexadecimally encoded src to dst
50
+ // and returns the extended buffer.
51
+ func AppendEncode(dst, src []byte) []byte {
52
+ for _, v := range src {
53
+ dst = append(dst, hextable[v>>4], hextable[v&0x0f])
54
+ }
55
+ return dst
56
+ }
57
+
58
+ // DecodedLen returns the length of a decoding of x source bytes.
59
+ // Specifically, it returns x / 2.
60
+ func DecodedLen(x int) int { return x / 2 }
61
+
62
+ // fromHexChar converts a hex character into its value and a success flag.
63
+ func fromHexChar(c byte) (byte, bool) {
64
+ switch {
65
+ case '0' <= c && c <= '9':
66
+ return c - '0', true
67
+ case 'a' <= c && c <= 'f':
68
+ return c - 'a' + 10, true
69
+ case 'A' <= c && c <= 'F':
70
+ return c - 'A' + 10, true
71
+ }
72
+
73
+ return 0, false
74
+ }
75
+
76
+ // Decode decodes src into [DecodedLen](len(src)) bytes,
77
+ // returning the actual number of bytes written to dst.
78
+ //
79
+ // Decode expects that src contains only hexadecimal
80
+ // characters and that src has even length.
81
+ // If the input is malformed, Decode returns the number
82
+ // of bytes decoded before the error.
83
+ func Decode(dst, src []byte) (int, error) {
84
+ i, j := 0, 1
85
+ for ; j < len(src); j += 2 {
86
+ p := src[j-1]
87
+ q := src[j]
88
+
89
+ a, aok := fromHexChar(p)
90
+ if !aok {
91
+ return i, InvalidByteError(p)
92
+ }
93
+ b, bok := fromHexChar(q)
94
+ if !bok {
95
+ return i, InvalidByteError(q)
96
+ }
97
+
98
+ dst[i] = (a << 4) | b
99
+ i++
100
+ }
101
+ if len(src)%2 == 1 {
102
+ // Check for invalid char before reporting bad length,
103
+ // since the invalid char (if present) is an earlier problem.
104
+ if _, ok := fromHexChar(src[j-1]); !ok {
105
+ return i, InvalidByteError(src[j-1])
106
+ }
107
+ return i, ErrLength
108
+ }
109
+ return i, nil
110
+ }
111
+
112
+ // AppendDecode appends the hexadecimally decoded src to dst
113
+ // and returns the extended buffer.
114
+ // If the input is malformed, it returns the partially decoded src and an error.
115
+ func AppendDecode(dst, src []byte) ([]byte, error) {
116
+ buf := make([]byte, DecodedLen(len(src)))
117
+ n, err := Decode(buf, src)
118
+ return append(dst, buf[:n]...), err
119
+ }
120
+
121
+ // EncodeToString returns the hexadecimal encoding of src.
122
+ func EncodeToString(src []byte) string {
123
+ dst := make([]byte, EncodedLen(len(src)))
124
+ Encode(dst, src)
125
+ return string(dst)
126
+ }
127
+
128
+ // DecodeString returns the bytes represented by the hexadecimal string s.
129
+ //
130
+ // DecodeString expects that src contains only hexadecimal
131
+ // characters and that src has even length.
132
+ // If the input is malformed, DecodeString returns
133
+ // the bytes decoded before the error.
134
+ func DecodeString(s string) ([]byte, error) {
135
+ src := []byte(s)
136
+ // We can use the source slice itself as the destination
137
+ // because the decode loop increments by one and then the 'seen' byte is not used anymore.
138
+ n, err := Decode(src, src)
139
+ return src[:n], err
140
+ }
@@ -0,0 +1,31 @@
1
+ //go:build gofront
2
+
3
+ //gofront:target both
4
+
5
+ package json
6
+
7
+ import (
8
+ "internal/jsonrt"
9
+ )
10
+
11
+ // A SyntaxError is a description of a JSON syntax error.
12
+ // [Unmarshal] will return a SyntaxError if the JSON can't be parsed.
13
+ type SyntaxError = jsonrt.SyntaxError
14
+
15
+ // An UnmarshalTypeError describes a JSON value that was not appropriate for
16
+ // a value of a specific Go type.
17
+ type UnmarshalTypeError = jsonrt.UnmarshalTypeError
18
+
19
+ // An InvalidUnmarshalError describes an invalid argument passed to Unmarshal.
20
+ type InvalidUnmarshalError = jsonrt.InvalidUnmarshalError
21
+
22
+ // An UnsupportedValueError is returned by Marshal when attempting to encode
23
+ // an unsupported value.
24
+ type UnsupportedValueError = jsonrt.UnsupportedValueError
25
+
26
+ // UnsupportedTypeError is returned by Marshal when attempting to encode an
27
+ // unsupported value type.
28
+ type UnsupportedTypeError = jsonrt.UnsupportedTypeError
29
+
30
+ // A MarshalerError represents an error from calling a MarshalJSON method.
31
+ type MarshalerError = jsonrt.MarshalerError
@@ -0,0 +1,71 @@
1
+ //go:build gofront
2
+
3
+ //gofront:target both
4
+
5
+ package json
6
+
7
+ import (
8
+ "bytes"
9
+ "internal/jsonrt"
10
+ )
11
+
12
+ var hexDigits = []byte("0123456789abcdef")
13
+
14
+ func escapeLen(src []byte, i int) int {
15
+ c := src[i]
16
+ if c == '<' || c == '>' || c == '&' {
17
+ return 1
18
+ }
19
+ if c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && (src[i+2]&^1) == 0xA8 {
20
+ return 3
21
+ }
22
+ return 0
23
+ }
24
+
25
+ // HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029
26
+ // characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029
27
+ // so that the JSON will be safe to embed inside HTML <script> tags.
28
+ func HTMLEscape(dst *bytes.Buffer, src []byte) {
29
+ start := 0
30
+ for i := 0; i < len(src); {
31
+ n := escapeLen(src, i)
32
+ if n == 0 {
33
+ i++
34
+ continue
35
+ }
36
+ if start < i {
37
+ dst.Write(src[start:i])
38
+ }
39
+ if n == 1 {
40
+ c := src[i]
41
+ dst.Write([]byte{'\\', 'u', '0', '0', hexDigits[c>>4], hexDigits[c&0xF]})
42
+ i++
43
+ } else {
44
+ dst.Write([]byte{'\\', 'u', '2', '0', '2', hexDigits[src[i+2]&0xF]})
45
+ i += 3
46
+ }
47
+ start = i
48
+ }
49
+ if start < len(src) {
50
+ dst.Write(src[start:])
51
+ }
52
+ }
53
+
54
+ // Compact appends to dst the JSON-encoded src with
55
+ // insignificant space characters elided.
56
+ func Compact(dst *bytes.Buffer, src []byte) error {
57
+ b, err := jsonrt.AppendCompact(nil, src, false)
58
+ if len(b) > 0 {
59
+ dst.Write(b)
60
+ }
61
+ return err
62
+ }
63
+
64
+ // Indent appends to dst an indented form of the JSON-encoded src.
65
+ func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error {
66
+ b, err := jsonrt.AppendIndent(nil, src, prefix, indent)
67
+ if len(b) > 0 {
68
+ dst.Write(b)
69
+ }
70
+ return err
71
+ }
@@ -0,0 +1,62 @@
1
+ //go:build gofront
2
+
3
+ //gofront:target both
4
+
5
+ package json
6
+
7
+ import (
8
+ "errors"
9
+ "internal/jsonrt"
10
+ )
11
+
12
+ // Marshal returns the JSON encoding of v.
13
+ //
14
+ // The compiler replaces every call with a function generated for the static
15
+ // type of v, so this body runs only when Marshal is used as a value, which
16
+ // the compiler rejects.
17
+ func Marshal(v any) ([]byte, error) {
18
+ panic("json.Marshal: call sites are rewritten by the compiler")
19
+ }
20
+
21
+ // MarshalIndent is like Marshal but applies Indent to format the output.
22
+ //
23
+ // The compiler replaces every call with a generated function; see Marshal.
24
+ func MarshalIndent(v any, prefix, indent string) ([]byte, error) {
25
+ panic("json.MarshalIndent: call sites are rewritten by the compiler")
26
+ }
27
+
28
+ // Unmarshal parses the JSON-encoded data and stores the result in the value
29
+ // pointed to by v.
30
+ //
31
+ // The compiler replaces every call with a function generated for the static
32
+ // type of v; see Marshal.
33
+ func Unmarshal(data []byte, v any) error {
34
+ panic("json.Unmarshal: call sites are rewritten by the compiler")
35
+ }
36
+
37
+ // Valid reports whether data is a valid JSON encoding.
38
+ func Valid(data []byte) bool {
39
+ return jsonrt.Valid(data)
40
+ }
41
+
42
+ // RawMessage is a raw encoded JSON value. It implements Marshaler and
43
+ // Unmarshaler and can be used to delay JSON decoding or precompute a JSON
44
+ // encoding.
45
+ type RawMessage []byte
46
+
47
+ // MarshalJSON returns m as the JSON encoding of m.
48
+ func (m RawMessage) MarshalJSON() ([]byte, error) {
49
+ if m == nil {
50
+ return []byte("null"), nil
51
+ }
52
+ return m, nil
53
+ }
54
+
55
+ // UnmarshalJSON sets *m to a copy of data.
56
+ func (m *RawMessage) UnmarshalJSON(data []byte) error {
57
+ if m == nil {
58
+ return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
59
+ }
60
+ *m = append((*m)[0:0], data...)
61
+ return nil
62
+ }
@@ -0,0 +1,12 @@
1
+ //go:build gofront
2
+
3
+ //gofront:target both
4
+
5
+ package json
6
+
7
+ import (
8
+ "internal/jsonrt"
9
+ )
10
+
11
+ // A Number represents a JSON number literal.
12
+ type Number = jsonrt.Number
@@ -0,0 +1,173 @@
1
+ //go:build gofront
2
+
3
+ //gofront:target both
4
+
5
+ package json
6
+
7
+ import (
8
+ "bytes"
9
+ "internal/jsonrt"
10
+ "io"
11
+ )
12
+
13
+ // An Encoder writes JSON values to an output stream.
14
+ type Encoder struct {
15
+ w io.Writer
16
+ err error
17
+ prefix string
18
+ indent string
19
+ escapeHTML bool
20
+ }
21
+
22
+ // NewEncoder returns a new encoder that writes to w.
23
+ func NewEncoder(w io.Writer) *Encoder {
24
+ return &Encoder{w: w, escapeHTML: true}
25
+ }
26
+
27
+ // SetIndent instructs the encoder to format each subsequent encoded
28
+ // value as if indented by the package-level function Indent.
29
+ // Calling SetIndent("", "") turns off indentation.
30
+ func (enc *Encoder) SetIndent(prefix, indent string) {
31
+ enc.prefix = prefix
32
+ enc.indent = indent
33
+ }
34
+
35
+ // SetEscapeHTML specifies whether problematic HTML characters
36
+ // should be escaped inside JSON-quoted strings.
37
+ // The default behavior is true.
38
+ func (enc *Encoder) SetEscapeHTML(on bool) {
39
+ enc.escapeHTML = on
40
+ }
41
+
42
+ // Err returns the first error the encoder encountered, if any.
43
+ func (enc *Encoder) Err() error {
44
+ return enc.err
45
+ }
46
+
47
+ // EscapeHTML returns whether HTML escaping is enabled.
48
+ func (enc *Encoder) EscapeHTML() bool {
49
+ return enc.escapeHTML
50
+ }
51
+
52
+ // EncodeRaw formats b with indentation if configured and writes it to the output stream.
53
+ func (enc *Encoder) EncodeRaw(b []byte) error {
54
+ if enc.err != nil {
55
+ return enc.err
56
+ }
57
+ if enc.indent != "" || enc.prefix != "" {
58
+ var buf bytes.Buffer
59
+ if err := Indent(&buf, b, enc.prefix, enc.indent); err != nil {
60
+ return err
61
+ }
62
+ b = buf.Bytes()
63
+ }
64
+ b = append(b, '\n')
65
+ if _, err := enc.w.Write(b); err != nil {
66
+ enc.err = err
67
+ return err
68
+ }
69
+ return nil
70
+ }
71
+
72
+ // Encode writes the JSON encoding of v to the stream, followed by a newline character.
73
+ func (enc *Encoder) Encode(v any) error {
74
+ if enc.err != nil {
75
+ return enc.err
76
+ }
77
+ return enc.EncodeRaw(nil)
78
+ }
79
+
80
+ // A Decoder reads and decodes JSON values from an input stream.
81
+ type Decoder struct {
82
+ r *jsonrt.Reader
83
+ useNumber bool
84
+ disallowUnknownFields bool
85
+ offset int64
86
+ }
87
+
88
+ // NewDecoder returns a new decoder that reads from r.
89
+ func NewDecoder(r io.Reader) *Decoder {
90
+ return &Decoder{r: jsonrt.NewReader(r)}
91
+ }
92
+
93
+ // UseNumber causes the Decoder to unmarshal a number into an interface{} as a
94
+ // Number instead of as a float64.
95
+ func (dec *Decoder) UseNumber() {
96
+ dec.useNumber = true
97
+ }
98
+
99
+ // DisallowUnknownFields causes the Decoder to return an error when the destination
100
+ // is a struct and the input contains object keys which do not match any
101
+ // non-ignored, exported fields in the destination.
102
+ func (dec *Decoder) DisallowUnknownFields() {
103
+ dec.disallowUnknownFields = true
104
+ }
105
+
106
+ // InputOffset returns the input stream byte offset of the current decoder position.
107
+ // The offset gives the location of the end of the most recently returned token
108
+ // and the beginning of the immediately following token.
109
+ func (dec *Decoder) InputOffset() int64 {
110
+ return dec.offset
111
+ }
112
+
113
+ // Buffered returns a reader of the data remaining in the Decoder's
114
+ // buffer. The reader is valid until the next call to Decode.
115
+ func (dec *Decoder) Buffered() io.Reader {
116
+ p := dec.r.Buffered()
117
+ if len(p) > 0 {
118
+ return bytes.NewReader(p)
119
+ }
120
+ return bytes.NewReader(nil)
121
+ }
122
+
123
+ // More reports whether there is another element in the current array
124
+ // or object being parsed, or whether there is another value in the stream.
125
+ func (dec *Decoder) More() bool {
126
+ for {
127
+ b, err := dec.r.ReadByte()
128
+ if err != nil {
129
+ return false
130
+ }
131
+ if isSpace(b) {
132
+ dec.offset++
133
+ continue
134
+ }
135
+ dec.r.UnreadByte()
136
+ if b == ']' || b == '}' {
137
+ return false
138
+ }
139
+ return true
140
+ }
141
+ }
142
+
143
+ // ScanNext consumes whitespace, scans the next JSON value, and updates dec.offset.
144
+ func (dec *Decoder) ScanNext() ([]byte, error) {
145
+ data, skipped, err := jsonrt.ScanNext(dec.r)
146
+ dec.offset += skipped
147
+ if err != nil {
148
+ return nil, err
149
+ }
150
+ dec.offset += int64(len(data))
151
+ return data, nil
152
+ }
153
+
154
+ // SetupDec configures a jsonrt.Dec with this decoder's options.
155
+ func (dec *Decoder) SetupDec(d *jsonrt.Dec) {
156
+ d.UseNumber = dec.useNumber
157
+ d.DisallowUnknownFields = dec.disallowUnknownFields
158
+ }
159
+
160
+ func isSpace(c byte) bool {
161
+ return c <= ' ' && (c == ' ' || c == '\t' || c == '\r' || c == '\n')
162
+ }
163
+
164
+ // Decode reads the next JSON-encoded value from its input and stores it in the
165
+ // value pointed to by v.
166
+ func (dec *Decoder) Decode(v any) error {
167
+ data, err := dec.ScanNext()
168
+ if err != nil {
169
+ return err
170
+ }
171
+ _ = data
172
+ return nil
173
+ }
@@ -0,0 +1,174 @@
1
+ //go:build gofront
2
+
3
+ //gofront:target both
4
+
5
+ // Copyright 2011 The Go Authors. All rights reserved.
6
+ // Use of this source code is governed by a BSD-style
7
+ // license that can be found in the LICENSE file.
8
+
9
+ // Package errors implements functions to manipulate errors.
10
+ //
11
+ // New, Unwrap, Is, Join and ErrUnsupported are written in Go over the
12
+ // real package's algorithms. As stores through a pointer target whose type
13
+ // only the compiler knows, which Go source cannot express without
14
+ // reflection: every call is rewritten to a function generated for its
15
+ // target type (src/lower/errors-as.js).
16
+ //
17
+ // Is does not check that target is comparable before comparing, as Go
18
+ // does with reflection: an error whose dynamic type is a slice, map or
19
+ // func panics when it is compared.
20
+ package errors
21
+
22
+ // New returns an error that formats as the given text.
23
+ // Each call to New returns a distinct error value even if the text is identical.
24
+ func New(text string) error {
25
+ return &errorString{text}
26
+ }
27
+
28
+ // errorString is a trivial implementation of error.
29
+ type errorString struct {
30
+ s string
31
+ }
32
+
33
+ func (e *errorString) Error() string {
34
+ return e.s
35
+ }
36
+
37
+ // GofrontTypeName is the type %T prints for the error. Package fmt cannot
38
+ // name the unexported type, so the error says it.
39
+ func (e *errorString) GofrontTypeName() string {
40
+ return "*errors.errorString"
41
+ }
42
+
43
+ // GofrontFmt is how package fmt prints the error with a verb that does not
44
+ // apply to it (%v only reaches it from a failed verb): Go prints the pointer
45
+ // to the struct, whose one field is a string.
46
+ func (e *errorString) GofrontFmt(verb rune) string {
47
+ if verb == 'v' {
48
+ return "&{" + e.s + "}"
49
+ }
50
+ return "&{%!" + string(verb) + "(string=" + e.s + ")}"
51
+ }
52
+
53
+ // ErrUnsupported indicates that a requested operation cannot be performed,
54
+ // because it is unsupported.
55
+ var ErrUnsupported = New("unsupported operation")
56
+
57
+ // Unwrap returns the result of calling the Unwrap method on err, if err's
58
+ // type contains an Unwrap method returning error.
59
+ // Otherwise, Unwrap returns nil.
60
+ //
61
+ // Unwrap only calls a method of the form "Unwrap() error".
62
+ // In particular Unwrap does not unwrap errors returned by [Join].
63
+ func Unwrap(err error) error {
64
+ u, ok := err.(interface {
65
+ Unwrap() error
66
+ })
67
+ if !ok {
68
+ return nil
69
+ }
70
+ return u.Unwrap()
71
+ }
72
+
73
+ // Is reports whether any error in err's tree matches target.
74
+ //
75
+ // The tree consists of err itself, followed by the errors obtained by
76
+ // repeatedly calling its Unwrap() error or Unwrap() []error method.
77
+ // When err wraps multiple errors, Is examines err followed by a
78
+ // depth-first traversal of its children.
79
+ //
80
+ // An error is considered to match a target if it is equal to that target or if
81
+ // it implements a method Is(error) bool such that Is(target) returns true.
82
+ func Is(err, target error) bool {
83
+ if err == nil || target == nil {
84
+ return err == target
85
+ }
86
+ return is(err, target)
87
+ }
88
+
89
+ func is(err, target error) bool {
90
+ for {
91
+ if err == target {
92
+ return true
93
+ }
94
+ if x, ok := err.(interface{ Is(error) bool }); ok && x.Is(target) {
95
+ return true
96
+ }
97
+ switch x := err.(type) {
98
+ case interface{ Unwrap() error }:
99
+ err = x.Unwrap()
100
+ if err == nil {
101
+ return false
102
+ }
103
+ case interface{ Unwrap() []error }:
104
+ for _, err := range x.Unwrap() {
105
+ if is(err, target) {
106
+ return true
107
+ }
108
+ }
109
+ return false
110
+ default:
111
+ return false
112
+ }
113
+ }
114
+ }
115
+
116
+ // Join returns an error that wraps the given errors.
117
+ // Any nil error values are discarded.
118
+ // Join returns nil if every value in errs is nil.
119
+ // The error formats as the concatenation of the strings obtained
120
+ // by calling the Error method of each element of errs, with a newline
121
+ // between each string.
122
+ //
123
+ // A non-nil error returned by Join implements the Unwrap() []error method.
124
+ func Join(errs ...error) error {
125
+ n := 0
126
+ for _, err := range errs {
127
+ if err != nil {
128
+ n++
129
+ }
130
+ }
131
+ if n == 0 {
132
+ return nil
133
+ }
134
+ e := &joinError{
135
+ errs: make([]error, 0, n),
136
+ }
137
+ for _, err := range errs {
138
+ if err != nil {
139
+ e.errs = append(e.errs, err)
140
+ }
141
+ }
142
+ return e
143
+ }
144
+
145
+ type joinError struct {
146
+ errs []error
147
+ }
148
+
149
+ // GofrontTypeName is the type %T prints for the error (see errorString).
150
+ func (e *joinError) GofrontTypeName() string {
151
+ return "*errors.joinError"
152
+ }
153
+
154
+ func (e *joinError) Error() string {
155
+ s := e.errs[0].Error()
156
+ for _, err := range e.errs[1:] {
157
+ s += "\n" + err.Error()
158
+ }
159
+ return s
160
+ }
161
+
162
+ func (e *joinError) Unwrap() []error {
163
+ return e.errs
164
+ }
165
+
166
+ // As finds the first error in err's tree that matches target, and if one is
167
+ // found, sets target to that error value and returns true.
168
+ //
169
+ // The compiler replaces every call with a generated function for the static
170
+ // type of target, so this body runs only when As is used as a value, which
171
+ // the compiler rejects.
172
+ func As(err error, target any) bool {
173
+ panic("errors.As: call sites are rewritten by the compiler")
174
+ }