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
@@ -1,155 +0,0 @@
1
- // CodeGen for the GoFront-built-in `gom` DOM-rendering package.
2
-
3
- /** @typedef {import('../index.js').CodeGen} CodeGen */
4
-
5
- const GOM_ELEMENT_TAGS = {
6
- Div: "div",
7
- Section: "section",
8
- Article: "article",
9
- Aside: "aside",
10
- Header: "header",
11
- Footer: "footer",
12
- Main: "main",
13
- Nav: "nav",
14
- Figure: "figure",
15
- H1: "h1",
16
- H2: "h2",
17
- H3: "h3",
18
- H4: "h4",
19
- H5: "h5",
20
- H6: "h6",
21
- Span: "span",
22
- A: "a",
23
- Strong: "strong",
24
- Em: "em",
25
- Code: "code",
26
- Pre: "pre",
27
- Small: "small",
28
- Mark: "mark",
29
- P: "p",
30
- Br: "br",
31
- Hr: "hr",
32
- Ul: "ul",
33
- Ol: "ol",
34
- Li: "li",
35
- Dl: "dl",
36
- Dt: "dt",
37
- Dd: "dd",
38
- Form: "form",
39
- Input: "input",
40
- Button: "button",
41
- Textarea: "textarea",
42
- Select: "select",
43
- Option: "option",
44
- Label: "label",
45
- Fieldset: "fieldset",
46
- Legend: "legend",
47
- Img: "img",
48
- Video: "video",
49
- Audio: "audio",
50
- Canvas: "canvas",
51
- Table: "table",
52
- Thead: "thead",
53
- Tbody: "tbody",
54
- Tfoot: "tfoot",
55
- Tr: "tr",
56
- Th: "th",
57
- Td: "td",
58
- };
59
- const GOM_ATTR_HELPERS = {
60
- For: "for",
61
- Name: "name",
62
- Value: "value",
63
- Target: "target",
64
- Rel: "rel",
65
- Alt: "alt",
66
- Title: "title",
67
- Lang: "lang",
68
- Action: "action",
69
- Method: "method",
70
- AutoComplete: "autocomplete",
71
- Draggable: "draggable",
72
- Role: "role",
73
- StyleAttr: "style",
74
- AriaLabel: "aria-label",
75
- };
76
- const GOM_BOOL_ATTRS = {
77
- Disabled: "disabled",
78
- Checked: "checked",
79
- Selected: "selected",
80
- Readonly: "readonly",
81
- };
82
- const GOM_PROP_SETTERS = {
83
- Class: "className",
84
- Type: "type",
85
- Href: "href",
86
- Src: "src",
87
- Placeholder: "placeholder",
88
- };
89
-
90
- /** @type {ThisType<CodeGen>} */
91
- export const gomMethods = {
92
- _genGom(fn, expr) {
93
- const a = () =>
94
- expr.args.map((e) =>
95
- e._spread ? `...${this.genExpr(e)}` : this.genExpr(e),
96
- );
97
-
98
- if (GOM_ELEMENT_TAGS[fn]) {
99
- const tag = GOM_ELEMENT_TAGS[fn];
100
- const args = a();
101
- if (args.length === 0)
102
- return `(()=>({Mount(p){const e=document.createElement("${tag}");p.appendChild(e);}}))()`;
103
- return (
104
- `((...c)=>({Mount(p){const e=document.createElement("${tag}");c.forEach(n=>n?.Mount?.(e));p.appendChild(e);}}))` +
105
- `(${args.join(",")})`
106
- );
107
- }
108
-
109
- if (GOM_ATTR_HELPERS[fn]) {
110
- const [v] = a();
111
- return `((v)=>({Mount(e){e.setAttribute("${GOM_ATTR_HELPERS[fn]}",v)}}))(${v})`;
112
- }
113
-
114
- if (GOM_BOOL_ATTRS[fn])
115
- return `({Mount(e){e.setAttribute("${GOM_BOOL_ATTRS[fn]}","")}})`;
116
-
117
- const args = a();
118
-
119
- if (GOM_PROP_SETTERS[fn])
120
- return `((v)=>({Mount(e){e.${GOM_PROP_SETTERS[fn]}=v}}))(${args[0]})`;
121
-
122
- switch (fn) {
123
- case "El": {
124
- const [tag, ...children] = args;
125
- if (children.length === 0)
126
- return `((t)=>({Mount(p){const e=document.createElement(t);p.appendChild(e);}})) (${tag})`;
127
- return (
128
- `((t,...c)=>({Mount(p){const e=document.createElement(t);c.forEach(n=>n?.Mount?.(e));p.appendChild(e);}}))` +
129
- `(${tag},${children.join(",")})`
130
- );
131
- }
132
- case "Text": {
133
- const [s] = args;
134
- return `((s)=>({Mount(p){p.appendChild(document.createTextNode(s))}}))(${s})`;
135
- }
136
- case "Attr": {
137
- const [name, value] = args;
138
- return `((n,v)=>({Mount(e){e.setAttribute(n,v)}}))(${name},${value})`;
139
- }
140
- case "DataAttr":
141
- return `((k,v)=>({Mount(e){e.setAttribute("data-"+k,v)}}))(${args[0]},${args[1]})`;
142
- case "If":
143
- return `((c,n)=>c?n:{Mount(){}})(${args[0]},${args[1]})`;
144
- case "Map":
145
- return `((s,f)=>{const c=s.map(f);return{_items:c,Mount(p){c.forEach(n=>n.Mount(p))}}})(${args[0]},${args[1]})`;
146
- case "Style":
147
- return `((s)=>({Mount(p){const e=document.createElement("style");e.textContent=s;p.appendChild(e);}})) (${args[0]})`;
148
- case "Mount":
149
- return `((sel,n,r)=>{const e=document.querySelector(sel);e.innerHTML="";n.Mount(e,r)})(${args[0]},${args[1]},${args[2] ?? "undefined"})`;
150
- case "MountTo":
151
- return `((sel,n,r)=>{const e=document.querySelector(sel);n.Mount(e,r)})(${args[0]},${args[1]},${args[2] ?? "undefined"})`;
152
- }
153
- return undefined;
154
- },
155
- };
@@ -1,17 +0,0 @@
1
- // src/backend/js/stdlib/hex.js
2
- // CodeGen for Go encoding/hex package.
3
-
4
- export const hexMethods = {
5
- _genHex(fn, a) {
6
- this.useHelper("hex");
7
- this.useHelper("error");
8
- const args = a();
9
- if (fn === "EncodeToString") return `__hex_encode_to_string(${args[0]})`;
10
- if (fn === "DecodeString") return `__hex_decode_string(${args[0]})`;
11
- if (fn === "Encode") return `__hex_encode(${args[0]}, ${args[1]})`;
12
- if (fn === "Decode") return `__hex_decode(${args[0]}, ${args[1]})`;
13
- if (fn === "EncodedLen") return `(${args[0]} * 2)`;
14
- if (fn === "DecodedLen") return `Math.floor(${args[0]} / 2)`;
15
- return undefined;
16
- },
17
- };
@@ -1,20 +0,0 @@
1
- // CodeGen for Go `html` package.
2
-
3
- /** @typedef {import('../index.js').CodeGen} CodeGen */
4
-
5
- /** @type {ThisType<CodeGen>} */
6
- export const htmlMethods = {
7
- _genHtml(fn, a) {
8
- switch (fn) {
9
- case "EscapeString": {
10
- const [s] = a();
11
- return `${s}.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&#34;").replace(/'/g,"&#39;")`;
12
- }
13
- case "UnescapeString": {
14
- const [s] = a();
15
- return `(${s}).replace(/&(?:quot|apos|#39|#34|gt|lt|amp|#(?:[0-9]+|[xX][0-9a-fA-F]+));/g, m => { switch (m) { case "&quot;": case "&#34;": return '"'; case "&apos;": case "&#39;": return "'"; case "&gt;": return ">"; case "&lt;": return "<"; case "&amp;": return "&"; default: { if (m.startsWith("&#")) { const raw = m.slice(2, -1); const cp = (raw.startsWith("x") || raw.startsWith("X")) ? parseInt(raw.slice(1), 16) : parseInt(raw, 10); return (cp >= 0 && cp <= 0x10FFFF && !(cp >= 0xD800 && cp <= 0xDFFF)) ? String.fromCodePoint(cp) : "\\uFFFD"; } return m; } } })`;
16
- }
17
- }
18
- return undefined;
19
- },
20
- };
@@ -1,137 +0,0 @@
1
- // CodeGen for Go `io` package.
2
-
3
- /** @typedef {import('../index.js').CodeGen} CodeGen */
4
-
5
- /** @type {ThisType<CodeGen>} */
6
- export const ioMethods = {
7
- _genIo(fn, _a, expr) {
8
- if (fn === "ReadAll") this.useHelper("error");
9
- switch (fn) {
10
- case "WriteString": {
11
- const writerArg = expr.args[0];
12
- const writerType = writerArg._type;
13
- const typeName =
14
- writerType?.name ??
15
- (writerType?.kind === "pointer" ? writerType.base?.name : null);
16
- const w = this.genExpr(writerArg);
17
- const s = this.genExpr(expr.args[1]);
18
- const base = this._unboxWriter(w);
19
- if (typeName === "strings.Builder") {
20
- return `((b,s) => { b._buf += s; return [s.length, null]; })(${base}, ${s})`;
21
- }
22
- if (typeName === "bytes.Buffer") {
23
- return `((b,s) => { const bytes = new TextEncoder().encode(s); for (let i = 0; i < bytes.length; i++) b._buf.push(bytes[i]); return [bytes.length, null]; })(${base}, ${s})`;
24
- }
25
- // Generic: auto-dereference pointer, then dispatch on concrete writer type.
26
- // strings.Builder has { _buf: string }, bytes.Buffer has { _buf: [] }.
27
- return `((b,s) => { const w = b?.value ?? b; if (typeof w.WriteString === "function") return w.WriteString(s); if (typeof w._buf === "string") { w._buf += s; return [s.length, null]; } if (Array.isArray(w._buf)) { const bytes = new TextEncoder().encode(s); for (let i = 0; i < bytes.length; i++) w._buf.push(bytes[i]); return [bytes.length, null]; } if (typeof w.Write === "function") return w.Write(new TextEncoder().encode(s)); return [0, "io.WriteString: unsupported writer"]; })(${w}, ${s})`;
28
- }
29
- case "ReadAll": {
30
- this.useHelper("ioEOF");
31
- const r = this.genExpr(expr.args[0]);
32
- return `((r) => { const w = r?.value ?? r; const chunks = []; const buf = new Uint8Array(512); let err = null; while (!err) { const [n, e] = w.Read(buf); if (n > 0) { for (let i = 0; i < n; i++) chunks.push(buf[i]); } if (n === 0 && !e) break; err = e; } return [new Uint8Array(chunks), (err === "EOF" || err === __io_eof || err?._msg === "EOF") ? null : err]; })(${r})`;
33
- }
34
- case "LimitReader": {
35
- this.useHelper("ioEOF");
36
- const r = this.genExpr(expr.args[0]);
37
- const n = this.genExpr(expr.args[1]);
38
- return `((r, n) => {
39
- let rem = typeof n === "bigint" ? Number(n) : Number(n);
40
- const reader = r?.value ?? r;
41
- return {
42
- Read(buf) {
43
- if (rem <= 0) return [0, __io_eof];
44
- const max = buf.length > rem ? rem : buf.length;
45
- const slice = buf.subarray ? buf.subarray(0, max) : buf.slice(0, max);
46
- const [readN, err] = reader.Read(slice);
47
- if (readN > 0) {
48
- if (buf.set && slice !== buf) buf.set(slice.subarray(0, readN), 0);
49
- rem -= readN;
50
- }
51
- return [readN, err];
52
- }
53
- };
54
- })(${r}, ${n})`;
55
- }
56
- case "Copy": {
57
- this.useHelper("ioEOF");
58
- const dst = this.genExpr(expr.args[0]);
59
- const src = this.genExpr(expr.args[1]);
60
- return `((dst, src) => {
61
- const w = dst?.value ?? dst;
62
- const r = src?.value ?? src;
63
- const writeChunk = (chunk) => {
64
- if (typeof w?.Write === "function") return w.Write(chunk);
65
- if (Array.isArray(w?._buf)) {
66
- for (let i = 0; i < chunk.length; i++) w._buf.push(chunk[i]);
67
- return [chunk.length, null];
68
- }
69
- if (typeof w?._buf === "string") {
70
- w._buf += new TextDecoder().decode(chunk);
71
- return [chunk.length, null];
72
- }
73
- return [0, "unsupported writer"];
74
- };
75
- const buf = new Uint8Array(32 * 1024);
76
- let total = 0;
77
- while (true) {
78
- const [n, rerr] = r.Read(buf);
79
- if (n > 0) {
80
- const chunk = buf.subarray(0, n);
81
- const [wn, werr] = writeChunk(chunk);
82
- total += wn;
83
- if (werr) return [total, werr];
84
- }
85
- if (rerr) {
86
- if (rerr === "EOF" || rerr === __io_eof || rerr?._msg === "EOF") return [total, null];
87
- return [total, rerr];
88
- }
89
- }
90
- })(${dst}, ${src})`;
91
- }
92
- case "CopyN": {
93
- this.useHelper("ioEOF");
94
- const dst = this.genExpr(expr.args[0]);
95
- const src = this.genExpr(expr.args[1]);
96
- const n = this.genExpr(expr.args[2]);
97
- return `((dst, src, n) => {
98
- const w = dst?.value ?? dst;
99
- const r = src?.value ?? src;
100
- const writeChunk = (chunk) => {
101
- if (typeof w?.Write === "function") return w.Write(chunk);
102
- if (Array.isArray(w?._buf)) {
103
- for (let i = 0; i < chunk.length; i++) w._buf.push(chunk[i]);
104
- return [chunk.length, null];
105
- }
106
- if (typeof w?._buf === "string") {
107
- w._buf += new TextDecoder().decode(chunk);
108
- return [chunk.length, null];
109
- }
110
- return [0, "unsupported writer"];
111
- };
112
- let rem = typeof n === "bigint" ? Number(n) : Number(n);
113
- let total = 0;
114
- const buf = new Uint8Array(32 * 1024);
115
- while (rem > 0) {
116
- const toRead = rem < buf.length ? rem : buf.length;
117
- const [rn, rerr] = r.Read(buf.subarray(0, toRead));
118
- if (rn > 0) {
119
- const [wn, werr] = writeChunk(buf.subarray(0, rn));
120
- total += wn;
121
- rem -= wn;
122
- if (werr) return [total, werr];
123
- }
124
- if (rerr) {
125
- if ((rerr === "EOF" || rerr === __io_eof || rerr?._msg === "EOF") && rem === 0) {
126
- return [total, null];
127
- }
128
- return [total, rerr];
129
- }
130
- }
131
- return [total, null];
132
- })(${dst}, ${src}, ${n})`;
133
- }
134
- }
135
- return undefined;
136
- },
137
- };