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.
- package/README.md +3 -3
- package/package.json +5 -4
- package/src/backend/js/expressions.js +849 -371
- package/src/backend/js/index.js +241 -147
- package/src/backend/js/natives.js +383 -0
- package/src/backend/js/runtime.js +512 -2547
- package/src/backend/js/statements.js +506 -103
- package/src/backend/js/stdlib/index.js +45 -93
- package/src/backend/js/templ.js +32 -45
- package/src/backend/js/type-info.js +177 -13
- package/src/backend/shared/fixed-float.js +20 -0
- package/src/backend/shared/fmt-types.js +63 -171
- package/src/backend/shared/host-unicode.js +36 -0
- package/src/backend/wasm/boundary.js +490 -965
- package/src/backend/wasm/emit-builtins.js +643 -256
- package/src/backend/wasm/emit-exprs.js +124 -57
- package/src/backend/wasm/emit-maps.js +17 -367
- package/src/backend/wasm/emit-stmts.js +325 -75
- package/src/backend/wasm/emit.js +104 -13
- package/src/backend/wasm/globals.js +47 -8
- package/src/backend/wasm/glue.js +2 -4
- package/src/backend/wasm/index.js +36 -530
- package/src/backend/wasm/method-expr.js +12 -1
- package/src/backend/wasm/module-emitter.js +92 -717
- package/src/backend/wasm/monomorph.js +103 -6
- package/src/backend/wasm/natives.js +596 -0
- package/src/backend/wasm/prescan.js +5 -153
- package/src/backend/wasm/prune-funcs.js +89 -0
- package/src/backend/wasm/stdlib/index.js +2 -32
- package/src/backend/wasm/types.js +68 -20
- package/src/backend/wasm/variadic.js +3 -3
- package/src/cli/cli-core.js +5 -1
- package/src/cli/test-runner.js +115 -342
- package/src/compiler.js +108 -130
- package/src/lexer/lexer.js +105 -11
- package/src/lexer/templ-lexer.js +61 -18
- package/src/lexer/tokens.js +1 -0
- package/src/link/stdlib.js +354 -132
- package/src/link/type-names.js +148 -0
- package/src/link/wasm.js +30 -6
- package/src/lower/bounds.js +153 -0
- package/src/lower/captures.js +92 -33
- package/src/lower/embedding.js +32 -3
- package/src/lower/errors-as.js +196 -0
- package/src/lower/fmt-formatters.js +905 -0
- package/src/lower/functions.js +2 -0
- package/src/lower/gen-types.js +274 -0
- package/src/lower/index.js +7 -1
- package/src/lower/json-codecs.js +228 -247
- package/src/lower/ownership.js +25 -7
- package/src/lower/reachability.js +69 -51
- package/src/lower/walk.js +12 -0
- package/src/parser/declarations.js +28 -5
- package/src/parser/expressions.js +86 -16
- package/src/parser/statements.js +1 -0
- package/src/parser/templ.js +44 -5
- package/src/parser/types.js +1 -0
- package/src/resolver.js +1 -2
- package/src/typechecker/assignability.js +124 -11
- package/src/typechecker/const-fold.js +70 -27
- package/src/typechecker/constants.js +32 -6
- package/src/typechecker/expressions.js +326 -98
- package/src/typechecker/index.js +208 -33
- package/src/typechecker/init-order.js +136 -40
- package/src/typechecker/operand-text.js +40 -0
- package/src/typechecker/resolve.js +192 -36
- package/src/typechecker/statements.js +122 -11
- package/src/typechecker/stdlib/excluded.js +9 -5
- package/src/typechecker/stdlib/index.js +25 -9
- package/src/typechecker/termination.js +10 -1
- package/src/typechecker/types.js +203 -573
- package/stdlib/bytes/buffer.go +479 -0
- package/stdlib/bytes/bytes.go +1010 -0
- package/stdlib/bytes/reader.go +161 -0
- package/stdlib/context/context.go +115 -0
- package/stdlib/crypto/rand/rand.go +34 -0
- package/stdlib/crypto/sha256/sha256.go +249 -0
- package/stdlib/encoding/base64/base64.go +331 -0
- package/stdlib/encoding/binary/binary.go +332 -0
- package/stdlib/encoding/hex/hex.go +140 -0
- package/stdlib/encoding/json/json.go +62 -0
- package/stdlib/errors/errors.go +174 -0
- package/stdlib/fmt/errors.go +110 -0
- package/stdlib/fmt/format.go +732 -0
- package/stdlib/fmt/print.go +1232 -0
- package/stdlib/gom/gom.go +293 -0
- package/stdlib/html/html.go +251 -0
- package/stdlib/internal/fmtrt/fmtrt.go +163 -0
- package/stdlib/internal/jsonrt/dec.go +171 -38
- package/stdlib/internal/jsonrt/enc.go +186 -36
- package/stdlib/internal/jsonrt/scanner.go +17 -8
- package/stdlib/io/io.go +157 -1
- package/stdlib/maps/maps.go +131 -0
- package/stdlib/math/bits/bits.go +507 -0
- package/stdlib/math/extra.go +16 -14
- package/stdlib/math/math.go +439 -0
- package/stdlib/math/rand/rand.go +297 -0
- package/stdlib/os/os.go +93 -0
- package/stdlib/path/filepath/filepath.go +47 -0
- package/stdlib/path/path.go +394 -0
- package/stdlib/regexp/regexp.go +231 -0
- package/stdlib/slices/slices.go +526 -0
- package/stdlib/slices/sort.go +209 -0
- package/stdlib/slices/zsort_cmp.go +480 -0
- package/stdlib/slices/zsort_ordered.go +482 -0
- package/stdlib/sort/sort.go +391 -452
- package/stdlib/sort/zsort_func.go +480 -0
- package/stdlib/sort/zsort_iface.go +480 -0
- package/stdlib/strconv/float.go +1699 -0
- package/stdlib/strconv/isprint.go +752 -0
- package/stdlib/strconv/quote.go +235 -50
- package/stdlib/strconv/strconv.go +616 -0
- package/stdlib/strings/builder.go +95 -0
- package/stdlib/strings/reader.go +163 -0
- package/stdlib/strings/replacer.go +102 -0
- package/stdlib/strings/strings.go +807 -0
- package/stdlib/sync/sync.go +268 -0
- package/stdlib/testing/async_js.go +61 -0
- package/stdlib/testing/benchmark.go +297 -0
- package/stdlib/testing/testing.go +523 -0
- package/stdlib/time/duration.go +233 -0
- package/stdlib/time/format.go +745 -0
- package/stdlib/time/parse.go +790 -0
- package/stdlib/time/sleep_js.go +63 -0
- package/stdlib/time/time.go +834 -0
- package/stdlib/unicode/unicode.go +141 -3
- package/stdlib/unicode/utf8/utf8.go +464 -0
- package/src/backend/js/fmt-runtime.js +0 -573
- package/src/backend/js/stdlib/base64.js +0 -12
- package/src/backend/js/stdlib/binary.js +0 -102
- package/src/backend/js/stdlib/bits.js +0 -119
- package/src/backend/js/stdlib/builder.js +0 -98
- package/src/backend/js/stdlib/bytes.js +0 -368
- package/src/backend/js/stdlib/errors.js +0 -45
- package/src/backend/js/stdlib/fmt.js +0 -517
- package/src/backend/js/stdlib/gom.js +0 -155
- package/src/backend/js/stdlib/hex.js +0 -17
- package/src/backend/js/stdlib/html.js +0 -20
- package/src/backend/js/stdlib/io.js +0 -137
- package/src/backend/js/stdlib/json.js +0 -1167
- package/src/backend/js/stdlib/maps.js +0 -37
- package/src/backend/js/stdlib/math.js +0 -85
- package/src/backend/js/stdlib/os.js +0 -13
- package/src/backend/js/stdlib/path.js +0 -48
- package/src/backend/js/stdlib/rand.js +0 -32
- package/src/backend/js/stdlib/regexp.js +0 -54
- package/src/backend/js/stdlib/sha256.js +0 -12
- package/src/backend/js/stdlib/shared.js +0 -17
- package/src/backend/js/stdlib/simd.js +0 -34
- package/src/backend/js/stdlib/slices.js +0 -107
- package/src/backend/js/stdlib/sort.js +0 -30
- package/src/backend/js/stdlib/strconv.js +0 -79
- package/src/backend/js/stdlib/strings.js +0 -301
- package/src/backend/js/stdlib/sync.js +0 -13
- package/src/backend/js/stdlib/testing.js +0 -41
- package/src/backend/js/stdlib/time.js +0 -152
- package/src/backend/js/stdlib/unicode.js +0 -25
- package/src/backend/js/stdlib/utf8.js +0 -26
- package/src/backend/wasm/stdlib/bytes.js +0 -6716
- package/src/backend/wasm/stdlib/core.js +0 -10278
- package/src/backend/wasm/stdlib/io.js +0 -3578
- package/src/backend/wasm/stdlib/packages.js +0 -2402
- package/src/backend/wasm/stdlib/pure.js +0 -720
- package/src/backend/wasm/stdlib/time.js +0 -2229
- package/src/typechecker/stdlib/core.js +0 -1079
- package/src/typechecker/stdlib/extended.js +0 -1210
- package/stdlib/bytes/iter.go +0 -192
- package/stdlib/bytes/search.go +0 -240
- package/stdlib/bytes/split.go +0 -218
- package/stdlib/errors/overlay.go +0 -12
- package/stdlib/maps/iter.go +0 -65
- package/stdlib/slices/extra.go +0 -73
- package/stdlib/slices/iter.go +0 -113
- package/stdlib/strings/extra.go +0 -35
- package/stdlib/strings/iter.go +0 -122
- package/stdlib/sync/extra.go +0 -105
- package/stdlib/unicode/utf8/bytes.go +0 -239
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
// src/backend/js/stdlib/binary.js
|
|
2
|
-
// CodeGen for Go encoding/binary package.
|
|
3
|
-
|
|
4
|
-
function sizeOfGoType(gt) {
|
|
5
|
-
if (!gt) return -1;
|
|
6
|
-
if (gt.kind === "basic") {
|
|
7
|
-
switch (gt.name) {
|
|
8
|
-
case "bool":
|
|
9
|
-
case "int8":
|
|
10
|
-
case "uint8":
|
|
11
|
-
case "byte":
|
|
12
|
-
return 1;
|
|
13
|
-
case "int16":
|
|
14
|
-
case "uint16":
|
|
15
|
-
return 2;
|
|
16
|
-
case "int32":
|
|
17
|
-
case "uint32":
|
|
18
|
-
case "float32":
|
|
19
|
-
case "rune":
|
|
20
|
-
return 4;
|
|
21
|
-
case "int64":
|
|
22
|
-
case "uint64":
|
|
23
|
-
case "float64":
|
|
24
|
-
case "int":
|
|
25
|
-
case "uint":
|
|
26
|
-
case "uintptr":
|
|
27
|
-
return 8;
|
|
28
|
-
default:
|
|
29
|
-
return -1;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
if (gt.kind === "array" || gt.kind === "ArrayType") {
|
|
33
|
-
const es = sizeOfGoType(gt.elem);
|
|
34
|
-
const len = gt.len ?? gt.size ?? 0;
|
|
35
|
-
return es > 0 ? es * len : -1;
|
|
36
|
-
}
|
|
37
|
-
if (
|
|
38
|
-
gt.kind === "pointer" ||
|
|
39
|
-
gt.kind === "PointerType" ||
|
|
40
|
-
gt.kind === "StarExpr"
|
|
41
|
-
) {
|
|
42
|
-
return sizeOfGoType(gt.base ?? gt.expr ?? gt.operand);
|
|
43
|
-
}
|
|
44
|
-
if (gt.kind === "struct" || gt.kind === "StructType") {
|
|
45
|
-
let total = 0;
|
|
46
|
-
if (gt.fields instanceof Map) {
|
|
47
|
-
for (const fType of gt.fields.values()) {
|
|
48
|
-
const s = sizeOfGoType(fType);
|
|
49
|
-
if (s < 0) return -1;
|
|
50
|
-
total += s;
|
|
51
|
-
}
|
|
52
|
-
return total;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return -1;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export const binaryMethods = {
|
|
59
|
-
_genBinary(fn, a, expr) {
|
|
60
|
-
this.useHelper("binary");
|
|
61
|
-
this.useHelper("error");
|
|
62
|
-
const args = a();
|
|
63
|
-
if (fn === "Read") {
|
|
64
|
-
const targetType = expr?.args?.[2]?._type;
|
|
65
|
-
const baseType = targetType?.base ?? targetType;
|
|
66
|
-
const resolved =
|
|
67
|
-
baseType?.kind === "named"
|
|
68
|
-
? (baseType.underlying ?? baseType)
|
|
69
|
-
: baseType;
|
|
70
|
-
const sz = sizeOfGoType(resolved);
|
|
71
|
-
const typeTag =
|
|
72
|
-
resolved?.name ??
|
|
73
|
-
(resolved?.kind === "basic" ? resolved.name : (baseType?.name ?? ""));
|
|
74
|
-
return `__binary_read(${args[0]}, ${args[1]}, ${args[2]}${sz > 0 ? `, ${sz}` : ", 0"}, "${typeTag}")`;
|
|
75
|
-
}
|
|
76
|
-
if (fn === "Write") {
|
|
77
|
-
const targetType = expr?.args?.[2]?._type;
|
|
78
|
-
const baseType = targetType?.base ?? targetType;
|
|
79
|
-
const resolved =
|
|
80
|
-
baseType?.kind === "named"
|
|
81
|
-
? (baseType.underlying ?? baseType)
|
|
82
|
-
: baseType;
|
|
83
|
-
const sz = sizeOfGoType(resolved);
|
|
84
|
-
const typeTag =
|
|
85
|
-
resolved?.name ??
|
|
86
|
-
(resolved?.kind === "basic" ? resolved.name : (baseType?.name ?? ""));
|
|
87
|
-
return `__binary_write(${args[0]}, ${args[1]}, ${args[2]}${sz > 0 ? `, ${sz}` : ", 0"}, "${typeTag}")`;
|
|
88
|
-
}
|
|
89
|
-
if (fn === "Size") {
|
|
90
|
-
const sz = sizeOfGoType(expr?.args?.[0]?._type);
|
|
91
|
-
if (sz >= 0) return `${sz}`;
|
|
92
|
-
return `__binary_size(${args[0]})`;
|
|
93
|
-
}
|
|
94
|
-
if (fn === "AppendUint16")
|
|
95
|
-
return `__binary_le.AppendUint16(${args[0]}, ${args[1]})`;
|
|
96
|
-
if (fn === "AppendUint32")
|
|
97
|
-
return `__binary_le.AppendUint32(${args[0]}, ${args[1]})`;
|
|
98
|
-
if (fn === "AppendUint64")
|
|
99
|
-
return `__binary_le.AppendUint64(${args[0]}, ${args[1]})`;
|
|
100
|
-
return undefined;
|
|
101
|
-
},
|
|
102
|
-
};
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
// src/backend/js/stdlib/bits.js
|
|
2
|
-
// CodeGen for Go `math/bits` package.
|
|
3
|
-
|
|
4
|
-
/** @typedef {import('../index.js').CodeGen} CodeGen */
|
|
5
|
-
|
|
6
|
-
function toBig64(x) {
|
|
7
|
-
const trimmed = String(x).trim();
|
|
8
|
-
if (/^-?(0[xX][0-9a-fA-F]+|0[bB][01]+|0[oO][0-7]+|\d+)n?$/.test(trimmed)) {
|
|
9
|
-
return trimmed.endsWith("n") ? trimmed : `${trimmed}n`;
|
|
10
|
-
}
|
|
11
|
-
return `BigInt(${x})`;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const BITS_MAP = {
|
|
15
|
-
Add32: ([x, y, c]) =>
|
|
16
|
-
`((() => { const s = BigInt((${x}) >>> 0) + BigInt((${y}) >>> 0) + BigInt((${c}) >>> 0); return [Number(s & 0xffffffffn), Number(s >> 32n)]; })())`,
|
|
17
|
-
Add64: ([x, y, c]) =>
|
|
18
|
-
`((() => { const s = BigInt.asUintN(64, ${toBig64(x)}) + BigInt.asUintN(64, ${toBig64(y)}) + BigInt.asUintN(64, ${toBig64(c)}); return [Number(BigInt.asUintN(64, s)), Number(s >> 64n)]; })())`,
|
|
19
|
-
Add: (args) => BITS_MAP.Add64(args),
|
|
20
|
-
|
|
21
|
-
Sub32: ([x, y, b]) =>
|
|
22
|
-
`((() => { const diff = BigInt((${x}) >>> 0) - BigInt((${y}) >>> 0) - BigInt((${b}) >>> 0); return [Number(BigInt.asUintN(32, diff)), diff < 0n ? 1 : 0]; })())`,
|
|
23
|
-
Sub64: ([x, y, b]) =>
|
|
24
|
-
`((() => { const diff = BigInt.asUintN(64, ${toBig64(x)}) - BigInt.asUintN(64, ${toBig64(y)}) - BigInt.asUintN(64, ${toBig64(b)}); return [Number(BigInt.asUintN(64, diff)), diff < 0n ? 1 : 0]; })())`,
|
|
25
|
-
Sub: (args) => BITS_MAP.Sub64(args),
|
|
26
|
-
|
|
27
|
-
Mul32: ([x, y]) =>
|
|
28
|
-
`((() => { const p = BigInt((${x}) >>> 0) * BigInt((${y}) >>> 0); return [Number(p >> 32n), Number(p & 0xffffffffn)]; })())`,
|
|
29
|
-
Mul64: ([x, y]) =>
|
|
30
|
-
`((() => { const xB = BigInt.asUintN(64, ${toBig64(x)}); const yB = BigInt.asUintN(64, ${toBig64(y)}); const mask32 = 0xffffffffn; const x0 = xB & mask32, x1 = xB >> 32n; const y0 = yB & mask32, y1 = yB >> 32n; const w0 = x0 * y0; const t = x1 * y0 + (w0 >> 32n); const w1 = (t & mask32) + x0 * y1; const hi = x1 * y1 + (t >> 32n) + (w1 >> 32n); const lo = BigInt.asUintN(64, xB * yB); return [Number(hi), Number(lo)]; })())`,
|
|
31
|
-
Mul: (args) => BITS_MAP.Mul64(args),
|
|
32
|
-
|
|
33
|
-
Div32: ([hi, lo, y]) =>
|
|
34
|
-
`((() => { const yu = (${y}) >>> 0; if (yu === 0) throw new Error("runtime error: integer divide by zero"); const hiu = (${hi}) >>> 0; if (hiu >= yu) throw new Error("runtime error: integer overflow"); const z = (BigInt(hiu) << 32n) | BigInt((${lo}) >>> 0); const yB = BigInt(yu); return [Number(z / yB), Number(z % yB)]; })())`,
|
|
35
|
-
Div64: ([hi, lo, y]) =>
|
|
36
|
-
`((() => { const yB = BigInt.asUintN(64, ${toBig64(y)}); if (yB === 0n) throw new Error("runtime error: integer divide by zero"); const hiB = BigInt.asUintN(64, ${toBig64(hi)}); if (hiB >= yB) throw new Error("runtime error: integer overflow"); const loB = BigInt.asUintN(64, ${toBig64(lo)}); const z = (hiB << 64n) | loB; return [Number(BigInt.asUintN(64, z / yB)), Number(BigInt.asUintN(64, z % yB))]; })())`,
|
|
37
|
-
Div: (args) => BITS_MAP.Div64(args),
|
|
38
|
-
|
|
39
|
-
Rem32: ([hi, lo, y]) =>
|
|
40
|
-
`((() => { const yu = (${y}) >>> 0; if (yu === 0) throw new Error("runtime error: integer divide by zero"); const z = (BigInt((${hi}) >>> 0) << 32n) | BigInt((${lo}) >>> 0); return Number(z % BigInt(yu)); })())`,
|
|
41
|
-
Rem64: ([hi, lo, y]) =>
|
|
42
|
-
`((() => { const yB = BigInt.asUintN(64, ${toBig64(y)}); if (yB === 0n) throw new Error("runtime error: integer divide by zero"); const z = ((BigInt.asUintN(64, ${toBig64(hi)}) % yB) << 64n) | BigInt.asUintN(64, ${toBig64(lo)}); return Number(BigInt.asUintN(64, z % yB)); })())`,
|
|
43
|
-
Rem: (args) => BITS_MAP.Rem64(args),
|
|
44
|
-
|
|
45
|
-
LeadingZeros8: ([x]) =>
|
|
46
|
-
`((() => { const v = (${x}) & 0xff; return v === 0 ? 8 : Math.clz32(v) - 24; })())`,
|
|
47
|
-
LeadingZeros16: ([x]) =>
|
|
48
|
-
`((() => { const v = (${x}) & 0xffff; return v === 0 ? 16 : Math.clz32(v) - 16; })())`,
|
|
49
|
-
LeadingZeros32: ([x]) => `Math.clz32((${x}) >>> 0)`,
|
|
50
|
-
LeadingZeros64: ([x]) =>
|
|
51
|
-
`((() => { const v = BigInt.asUintN(64, ${toBig64(x)}); if (v === 0n) return 64; const hi = Number(v >> 32n); return hi !== 0 ? Math.clz32(hi) : 32 + Math.clz32(Number(v & 0xffffffffn)); })())`,
|
|
52
|
-
LeadingZeros: (args) => BITS_MAP.LeadingZeros64(args),
|
|
53
|
-
|
|
54
|
-
TrailingZeros8: ([x]) =>
|
|
55
|
-
`((() => { const v = (${x}) & 0xff; return v === 0 ? 8 : 31 - Math.clz32(v & -v); })())`,
|
|
56
|
-
TrailingZeros16: ([x]) =>
|
|
57
|
-
`((() => { const v = (${x}) & 0xffff; return v === 0 ? 16 : 31 - Math.clz32(v & -v); })())`,
|
|
58
|
-
TrailingZeros32: ([x]) =>
|
|
59
|
-
`((() => { const v = (${x}) >>> 0; return v === 0 ? 32 : 31 - Math.clz32(v & -v); })())`,
|
|
60
|
-
TrailingZeros64: ([x]) =>
|
|
61
|
-
`((() => { const v = BigInt.asUintN(64, ${toBig64(x)}); if (v === 0n) return 64; const lo = Number(v & 0xffffffffn); if (lo !== 0) return 31 - Math.clz32(lo & -lo); const hi = Number((v >> 32n) & 0xffffffffn); return 32 + (31 - Math.clz32(hi & -hi)); })())`,
|
|
62
|
-
TrailingZeros: (args) => BITS_MAP.TrailingZeros64(args),
|
|
63
|
-
|
|
64
|
-
OnesCount8: ([x]) =>
|
|
65
|
-
`((() => { let v = (${x}) & 0xff; v = v - ((v >>> 1) & 0x55); v = (v & 0x33) + ((v >>> 2) & 0x33); return (v + (v >>> 4)) & 0x0f; })())`,
|
|
66
|
-
OnesCount16: ([x]) =>
|
|
67
|
-
`((() => { let v = (${x}) & 0xffff; v = v - ((v >>> 1) & 0x5555); v = (v & 0x3333) + ((v >>> 2) & 0x3333); return ((Math.imul((v + (v >>> 4)) & 0x0f0f, 0x0101)) >>> 8) & 0xff; })())`,
|
|
68
|
-
OnesCount32: ([x]) =>
|
|
69
|
-
`((() => { let v = (${x}) >>> 0; v = v - ((v >>> 1) & 0x55555555); v = (v & 0x33333333) + ((v >>> 2) & 0x33333333); return (Math.imul((v + (v >>> 4)) & 0x0f0f0f0f, 0x01010101)) >>> 24; })())`,
|
|
70
|
-
OnesCount64: ([x]) =>
|
|
71
|
-
`((() => { let v = BigInt.asUintN(64, ${toBig64(x)}); let c = 0; while (v > 0n) { if (v & 1n) c++; v >>= 1n; } return c; })())`,
|
|
72
|
-
OnesCount: (args) => BITS_MAP.OnesCount64(args),
|
|
73
|
-
|
|
74
|
-
RotateLeft8: ([x, k]) =>
|
|
75
|
-
`((() => { const s = (${k}) & 7; const v = (${x}) & 0xff; return ((v << s) | (v >>> ((8 - s) & 7))) & 0xff; })())`,
|
|
76
|
-
RotateLeft16: ([x, k]) =>
|
|
77
|
-
`((() => { const s = (${k}) & 15; const v = (${x}) & 0xffff; return ((v << s) | (v >>> ((16 - s) & 15))) & 0xffff; })())`,
|
|
78
|
-
RotateLeft32: ([x, k]) =>
|
|
79
|
-
`((() => { const s = (${k}) & 31; const v = (${x}) >>> 0; return ((v << s) | (v >>> ((32 - s) & 31))) >>> 0; })())`,
|
|
80
|
-
RotateLeft64: ([x, k]) =>
|
|
81
|
-
`((() => { const s = ${toBig64(k)} & 63n; const v = BigInt.asUintN(64, ${toBig64(x)}); return Number(BigInt.asUintN(64, (v << s) | (v >> ((64n - s) & 63n)))); })())`,
|
|
82
|
-
RotateLeft: (args) => BITS_MAP.RotateLeft64(args),
|
|
83
|
-
|
|
84
|
-
Len8: ([x]) =>
|
|
85
|
-
`((() => { const v = (${x}) & 0xff; return v === 0 ? 0 : 32 - Math.clz32(v); })())`,
|
|
86
|
-
Len16: ([x]) =>
|
|
87
|
-
`((() => { const v = (${x}) & 0xffff; return v === 0 ? 0 : 32 - Math.clz32(v); })())`,
|
|
88
|
-
Len32: ([x]) => `((v => 32 - Math.clz32(v >>> 0))(${x}))`,
|
|
89
|
-
Len64: ([x]) =>
|
|
90
|
-
`((() => { const v = BigInt.asUintN(64, ${toBig64(x)}); if (v === 0n) return 0; const hi = Number(v >> 32n); return hi !== 0 ? 64 - Math.clz32(hi) : 32 - Math.clz32(Number(v & 0xffffffffn)); })())`,
|
|
91
|
-
Len: (args) => BITS_MAP.Len64(args),
|
|
92
|
-
|
|
93
|
-
Reverse8: ([x]) =>
|
|
94
|
-
`((() => { let v = (${x}) & 0xff; v = ((v >>> 1) & 0x55) | ((v & 0x55) << 1); v = ((v >>> 2) & 0x33) | ((v & 0x33) << 2); return (((v >>> 4) & 0x0f) | ((v & 0x0f) << 4)) & 0xff; })())`,
|
|
95
|
-
Reverse16: ([x]) =>
|
|
96
|
-
`((() => { let v = (${x}) & 0xffff; v = ((v >>> 1) & 0x5555) | ((v & 0x5555) << 1); v = ((v >>> 2) & 0x3333) | ((v & 0x3333) << 2); v = ((v >>> 4) & 0x0f0f) | ((v & 0x0f0f) << 4); return (((v >>> 8) & 0x00ff) | ((v & 0x00ff) << 8)) & 0xffff; })())`,
|
|
97
|
-
Reverse32: ([x]) =>
|
|
98
|
-
`((() => { let v = (${x}) >>> 0; v = ((v >>> 1) & 0x55555555) | ((v & 0x55555555) << 1); v = ((v >>> 2) & 0x33333333) | ((v & 0x33333333) << 2); v = ((v >>> 4) & 0x0f0f0f0f) | ((v & 0x0f0f0f0f) << 4); v = ((v >>> 8) & 0x00ff00ff) | ((v & 0x00ff00ff) << 8); return ((v >>> 16) | (v << 16)) >>> 0; })())`,
|
|
99
|
-
Reverse64: ([x]) =>
|
|
100
|
-
`((() => { const rev32 = (v) => { v = ((v >>> 1) & 0x55555555) | ((v & 0x55555555) << 1); v = ((v >>> 2) & 0x33333333) | ((v & 0x33333333) << 2); v = ((v >>> 4) & 0x0f0f0f0f) | ((v & 0x0f0f0f0f) << 4); v = ((v >>> 8) & 0x00ff00ff) | ((v & 0x00ff00ff) << 8); return ((v >>> 16) | (v << 16)) >>> 0; }; const v = BigInt.asUintN(64, ${toBig64(x)}); const lo = Number(v & 0xffffffffn); const hi = Number((v >> 32n) & 0xffffffffn); return Number((BigInt(rev32(lo)) << 32n) | BigInt(rev32(hi))); })())`,
|
|
101
|
-
Reverse: (args) => BITS_MAP.Reverse64(args),
|
|
102
|
-
|
|
103
|
-
ReverseBytes16: ([x]) =>
|
|
104
|
-
`((v => ((v & 0xff) << 8) | ((v >>> 8) & 0xff))((${x}) & 0xffff))`,
|
|
105
|
-
ReverseBytes32: ([x]) =>
|
|
106
|
-
`((v => (((v & 0xff) << 24) | ((v & 0xff00) << 8) | ((v >>> 8) & 0xff00) | ((v >>> 24) & 0xff)) >>> 0)((${x}) >>> 0))`,
|
|
107
|
-
ReverseBytes64: ([x]) =>
|
|
108
|
-
`((() => { const v = BigInt.asUintN(64, ${toBig64(x)}); let r = 0n; for (let i = 0n; i < 8n; i++) r = (r << 8n) | ((v >> (i * 8n)) & 0xffn); return Number(r); })())`,
|
|
109
|
-
ReverseBytes: (args) => BITS_MAP.ReverseBytes64(args),
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
/** @type {ThisType<CodeGen>} */
|
|
113
|
-
export const bitsMethods = {
|
|
114
|
-
_genBits(fn, a) {
|
|
115
|
-
const args = a();
|
|
116
|
-
const gen = BITS_MAP[fn];
|
|
117
|
-
return gen ? gen(args) : undefined;
|
|
118
|
-
},
|
|
119
|
-
};
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
// CodeGen for `strings.Builder` and `bytes.Buffer` method calls.
|
|
2
|
-
|
|
3
|
-
/** @typedef {import('../index.js').CodeGen} CodeGen */
|
|
4
|
-
|
|
5
|
-
const BUILDER_STR = {
|
|
6
|
-
WriteString: (b, a) =>
|
|
7
|
-
`((__s) => { ${b}._buf += __s; return [new TextEncoder().encode(__s).length, null]; })(${a[0]})`,
|
|
8
|
-
WriteByte: (b, a) => `(${b}._buf += String.fromCodePoint(${a[0]}), null)`,
|
|
9
|
-
WriteRune: (b, a) =>
|
|
10
|
-
`((cp) => { const r = (cp < 0 || cp > 0x10ffff || (cp >= 0xd800 && cp <= 0xdfff)) ? 0xfffd : cp; const s = String.fromCodePoint(r); ${b}._buf += s; return [new TextEncoder().encode(s).length, null]; })(${a[0]})`,
|
|
11
|
-
Write: (b, a) =>
|
|
12
|
-
`(${b}._buf += new TextDecoder().decode(new Uint8Array(${a[0]})), [${a[0]}.length, null])`,
|
|
13
|
-
String: (b) => `${b}._buf`,
|
|
14
|
-
Reset: (b) => `(${b}._buf = "")`,
|
|
15
|
-
Len: (b) => `new TextEncoder().encode(${b}._buf).length`,
|
|
16
|
-
Cap: (b) =>
|
|
17
|
-
`Math.max(${b}._cap ?? 0, new TextEncoder().encode(${b}._buf).length)`,
|
|
18
|
-
Grow: (b, a) =>
|
|
19
|
-
`((n) => { if (n < 0) throw new Error("strings.Builder.Grow: negative count"); ${b}._cap = Math.max(${b}._cap ?? 0, new TextEncoder().encode(${b}._buf).length) + n; })(${a[0]})`,
|
|
20
|
-
};
|
|
21
|
-
const BUILDER_BYTES = {
|
|
22
|
-
WriteString: (b, a) =>
|
|
23
|
-
`((buf, s) => { buf._lastOp = 0; const bytes = new TextEncoder().encode(s); for (let i = 0; i < bytes.length; i++) buf._buf.push(bytes[i]); return [bytes.length, null]; })(${b}, ${a[0]})`,
|
|
24
|
-
WriteByte: (b, a) =>
|
|
25
|
-
`((buf, c) => { buf._lastOp = 0; buf._buf.push(c); return null; })(${b}, ${a[0]})`,
|
|
26
|
-
WriteRune: (b, a) =>
|
|
27
|
-
`((buf, cp) => { buf._lastOp = 0; const r = (cp < 0 || cp > 0x10ffff || (cp >= 0xd800 && cp <= 0xdfff)) ? 0xfffd : cp; const bytes = new TextEncoder().encode(String.fromCodePoint(r)); for (let i = 0; i < bytes.length; i++) buf._buf.push(bytes[i]); return [bytes.length, null]; })(${b}, ${a[0]})`,
|
|
28
|
-
Write: (b, a) =>
|
|
29
|
-
`((buf, d) => { buf._lastOp = 0; for (let i = 0; i < d.length; i++) buf._buf.push(d[i]); return [d.length, null]; })(${b}, ${a[0]})`,
|
|
30
|
-
String: (b) => `new TextDecoder().decode(new Uint8Array(${b}._buf))`,
|
|
31
|
-
Reset: (b) => `(${b}._buf = [])`,
|
|
32
|
-
Read: (b, a) =>
|
|
33
|
-
`((buf, p) => { if (!buf._buf || buf._buf.length === 0) return [0, __io_eof]; const n = Math.min(p.length, buf._buf.length); for (let i = 0; i < n; i++) p[i] = buf._buf.shift(); if (n > 0) { buf._lastOp = 1; buf._lastByte = p[n - 1]; } else { buf._lastOp = 0; } return [n, null]; })(${b}, ${a[0]})`,
|
|
34
|
-
ReadByte: (b) =>
|
|
35
|
-
`((buf) => { if (!buf._buf || buf._buf.length === 0) return [0, __io_eof]; const c = buf._buf.shift(); buf._lastOp = 1; buf._lastByte = c; return [c, null]; })(${b})`,
|
|
36
|
-
UnreadByte: (b) =>
|
|
37
|
-
`((buf) => { if (buf._lastOp !== 1) return __error("bytes.Buffer: UnreadByte: previous operation was not a successful read"); buf._buf.unshift(buf._lastByte); buf._lastOp = 0; return null; })(${b})`,
|
|
38
|
-
ReadRune: (b) =>
|
|
39
|
-
`((buf) => { if (!buf._buf || buf._buf.length === 0) return [0, 0, __io_eof]; const u8 = new Uint8Array(buf._buf); const s = new TextDecoder().decode(u8); const cp = s.codePointAt(0); const runeStr = s.slice(0, cp > 0xffff ? 2 : 1); const sz = new TextEncoder().encode(runeStr).length; for (let i = 0; i < sz; i++) buf._buf.shift(); buf._lastOp = 2; buf._lastRune = runeStr; return [cp, sz, null]; })(${b})`,
|
|
40
|
-
UnreadRune: (b) =>
|
|
41
|
-
`((buf) => { if (buf._lastOp !== 2) return __error("bytes.Buffer: UnreadRune: previous operation was not a successful ReadRune"); const bts = new TextEncoder().encode(buf._lastRune); for (let i = bts.length - 1; i >= 0; i--) buf._buf.unshift(bts[i]); buf._lastOp = 0; return null; })(${b})`,
|
|
42
|
-
ReadBytes: (b, a) =>
|
|
43
|
-
`((buf, delim) => { const idx = buf._buf.indexOf(delim); if (idx < 0) { const rem = Uint8Array.from(buf._buf); buf._buf = []; return [rem, __io_eof]; } const line = Uint8Array.from(buf._buf.splice(0, idx + 1)); return [line, null]; })(${b}, ${a[0]})`,
|
|
44
|
-
ReadString: (b, a) =>
|
|
45
|
-
`((buf, delim) => { const idx = buf._buf.indexOf(delim); if (idx < 0) { const rem = new TextDecoder().decode(Uint8Array.from(buf._buf)); buf._buf = []; return [rem, __io_eof]; } const line = new TextDecoder().decode(Uint8Array.from(buf._buf.splice(0, idx + 1))); return [line, null]; })(${b}, ${a[0]})`,
|
|
46
|
-
ReadFrom: (b, a) =>
|
|
47
|
-
`((buf, r) => { let total = 0n; const tmp = new Uint8Array(4096); while (true) { const [n, err] = r.Read(tmp); if (n > 0) { for (let i = 0; i < n; i++) buf._buf.push(tmp[i]); total += BigInt(n); } if (err) { return [total, (err === __io_eof || err?.Error?.() === "EOF") ? null : err]; } } })(${b}, ${a[0]})`,
|
|
48
|
-
WriteTo: (b, a) =>
|
|
49
|
-
`((buf, w) => { if (!buf._buf || buf._buf.length === 0) return [0n, null]; const bytes = Uint8Array.from(buf._buf); const [n, err] = w.Write(bytes); if (n > 0) buf._buf.splice(0, n); return [BigInt(n), err]; })(${b}, ${a[0]})`,
|
|
50
|
-
Next: (b, a) =>
|
|
51
|
-
`((buf, n) => { const cnt = Math.min(Math.max(0, n), buf._buf.length); return Uint8Array.from(buf._buf.splice(0, cnt)); })(${b}, ${a[0]})`,
|
|
52
|
-
Truncate: (b, a) =>
|
|
53
|
-
`((buf, n) => { if (n < 0 || n > buf._buf.length) throw new Error("bytes.Buffer: truncation out of range"); buf._buf.length = n; })(${b}, ${a[0]})`,
|
|
54
|
-
Available: (b) => `Math.max(0, (${b}._cap ?? 0) - ${b}._buf.length)`,
|
|
55
|
-
AvailableBuffer: () => `new Uint8Array(0)`,
|
|
56
|
-
Peek: (b, a) =>
|
|
57
|
-
`((buf, n) => { if (n < 0) throw new Error("runtime error: slice bounds out of range"); if (n > buf._buf.length) return [Uint8Array.from(buf._buf), __io_eof]; return [Uint8Array.from(buf._buf.slice(0, n)), null]; })(${b}, ${a[0]})`,
|
|
58
|
-
};
|
|
59
|
-
const BUILDER_COMMON = {
|
|
60
|
-
Bytes: (b) => `new Uint8Array(${b}._buf)`,
|
|
61
|
-
Len: (b) => `${b}._buf.length`,
|
|
62
|
-
Cap: (b) => `Math.max(${b}._cap ?? 0, ${b}._buf.length)`,
|
|
63
|
-
Grow: (b, a) =>
|
|
64
|
-
`((n) => { if (n < 0) throw new Error("bytes.Buffer.Grow: negative count"); ${b}._cap = Math.max(${b}._cap ?? 0, ${b}._buf.length) + n; })(${a[0]})`,
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
/** @type {ThisType<CodeGen>} */
|
|
68
|
-
export const builderMethods = {
|
|
69
|
-
_genBuilderCall(typeName, method, expr) {
|
|
70
|
-
const recv = expr.func.expr;
|
|
71
|
-
const base = this._unboxWriter(this.genExpr(recv));
|
|
72
|
-
const args = expr.args.map((a) => this.genExpr(a));
|
|
73
|
-
if (
|
|
74
|
-
method === "Read" ||
|
|
75
|
-
method === "ReadByte" ||
|
|
76
|
-
method === "ReadRune" ||
|
|
77
|
-
method === "ReadBytes" ||
|
|
78
|
-
method === "ReadString" ||
|
|
79
|
-
method === "ReadFrom" ||
|
|
80
|
-
method === "UnreadByte" ||
|
|
81
|
-
method === "UnreadRune" ||
|
|
82
|
-
method === "Peek"
|
|
83
|
-
) {
|
|
84
|
-
this.useHelper("ioEOF");
|
|
85
|
-
this.useHelper("error");
|
|
86
|
-
}
|
|
87
|
-
const isStr = typeName === "strings.Builder";
|
|
88
|
-
const table = isStr ? BUILDER_STR : BUILDER_BYTES;
|
|
89
|
-
const gen = table[method] ?? BUILDER_COMMON[method];
|
|
90
|
-
return gen ? gen(base, args) : undefined;
|
|
91
|
-
},
|
|
92
|
-
|
|
93
|
-
// Accepts a Builder/Buffer or a pointer box around one; evaluates `js` once.
|
|
94
|
-
_unboxWriter(js) {
|
|
95
|
-
if (/^[\w$.]+$/.test(js)) return `(${js}?.value ?? ${js})`;
|
|
96
|
-
return `((__w) => __w?.value ?? __w)(${js})`;
|
|
97
|
-
},
|
|
98
|
-
};
|