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.
- package/README.md +14 -14
- package/package.json +10 -38
- package/src/backend/js/expressions.js +2060 -389
- package/src/backend/js/index.js +586 -174
- package/src/backend/js/natives.js +383 -0
- package/src/backend/js/runtime.js +936 -1644
- package/src/backend/js/statements.js +1349 -132
- package/src/backend/js/stdlib/index.js +51 -94
- package/src/backend/js/templ.js +67 -118
- package/src/backend/js/type-info.js +436 -0
- package/src/backend/shared/fixed-float.js +20 -0
- package/src/backend/shared/fmt-types.js +80 -0
- package/src/backend/shared/host-unicode.js +36 -0
- package/src/backend/wasm/boundary.js +753 -872
- package/src/backend/wasm/emit-builtins.js +914 -237
- package/src/backend/wasm/emit-exprs.js +1456 -195
- package/src/backend/wasm/emit-maps.js +78 -380
- package/src/backend/wasm/emit-stmts.js +1659 -214
- package/src/backend/wasm/emit.js +614 -88
- package/src/backend/wasm/encode.js +43 -13
- package/src/backend/wasm/globals.js +491 -0
- package/src/backend/wasm/glue.js +2 -4
- package/src/backend/wasm/index.js +406 -3195
- package/src/backend/wasm/method-expr.js +392 -0
- package/src/backend/wasm/module-emitter.js +1615 -0
- package/src/backend/wasm/monomorph.js +164 -9
- package/src/backend/wasm/natives.js +596 -0
- package/src/backend/wasm/prescan.js +249 -0
- package/src/backend/wasm/prune-funcs.js +89 -0
- package/src/backend/wasm/stdlib/index.js +2 -37
- package/src/backend/wasm/stdlib/simd.js +293 -28
- package/src/backend/wasm/types.js +260 -27
- package/src/backend/wasm/variadic.js +93 -0
- package/src/backend/wasm/wat.js +47 -20
- package/src/{cli-core.js → cli/cli-core.js} +241 -11
- package/src/{test-runner.js → cli/test-runner.js} +188 -122
- package/src/{vendor.js → cli/vendor.js} +3 -4
- package/src/compiler.js +202 -386
- package/src/index.js +15 -169
- package/src/{lexer.js → lexer/lexer.js} +203 -62
- package/src/{templ-lexer.js → lexer/templ-lexer.js} +61 -18
- package/src/{tokens.js → lexer/tokens.js} +2 -0
- package/src/link/sourcemap.js +97 -0
- package/src/link/stdlib.js +737 -0
- package/src/link/type-names.js +148 -0
- package/src/link/wasm.js +318 -0
- package/src/lower/bounds.js +547 -0
- package/src/lower/boxing.js +1 -1
- package/src/lower/captures.js +267 -64
- 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 +33 -19
- package/src/lower/json-codecs.js +1379 -0
- package/src/lower/ownership.js +47 -8
- package/src/lower/range.js +86 -1
- package/src/lower/reachability.js +150 -0
- package/src/lower/walk.js +12 -0
- package/src/parser/declarations.js +83 -15
- package/src/parser/expressions.js +110 -18
- package/src/parser/index.js +2 -1
- package/src/parser/statements.js +38 -22
- package/src/{templ-parser.js → parser/templ.js} +179 -17
- package/src/parser/types.js +70 -9
- package/src/resolver.js +21 -1
- package/src/typechecker/assignability.js +291 -19
- package/src/typechecker/const-fold.js +399 -0
- package/src/typechecker/constants.js +282 -0
- package/src/typechecker/expressions.js +1220 -183
- package/src/typechecker/index.js +659 -92
- package/src/typechecker/init-order.js +426 -0
- package/src/typechecker/operand-text.js +40 -0
- package/src/typechecker/resolve.js +613 -62
- package/src/typechecker/statements.js +212 -20
- package/src/typechecker/stdlib/excluded.js +902 -0
- package/src/typechecker/stdlib/index.js +38 -0
- package/src/typechecker/stdlib/simd.js +65 -15
- package/src/typechecker/termination.js +11 -2
- package/src/typechecker/types.js +268 -514
- package/stdlib/bytes/buffer.go +479 -0
- package/stdlib/bytes/bytes.go +1010 -0
- package/stdlib/bytes/reader.go +161 -0
- package/stdlib/cmp/cmp.go +65 -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/errors.go +31 -0
- package/stdlib/encoding/json/indent.go +71 -0
- package/stdlib/encoding/json/json.go +62 -0
- package/stdlib/encoding/json/number.go +12 -0
- package/stdlib/encoding/json/stream.go +173 -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/hash/hash.go +53 -0
- package/stdlib/html/html.go +251 -0
- package/stdlib/internal/fmtrt/fmtrt.go +163 -0
- package/stdlib/internal/jsonrt/dec.go +871 -0
- package/stdlib/internal/jsonrt/enc.go +516 -0
- package/stdlib/internal/jsonrt/indent.go +135 -0
- package/stdlib/internal/jsonrt/number.go +27 -0
- package/stdlib/internal/jsonrt/reader.go +73 -0
- package/stdlib/internal/jsonrt/scanner.go +658 -0
- package/stdlib/io/io.go +502 -0
- package/stdlib/iter/iter.go +18 -0
- package/stdlib/maps/maps.go +131 -0
- package/stdlib/math/bits/bits.go +507 -0
- package/stdlib/math/extra.go +272 -0
- 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 +500 -0
- 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 +526 -0
- 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 +250 -0
- package/stdlib/unicode/utf8/utf8.go +464 -0
- 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 -46
- package/src/backend/js/stdlib/builder.js +0 -63
- package/src/backend/js/stdlib/bytes.js +0 -122
- package/src/backend/js/stdlib/errors.js +0 -44
- package/src/backend/js/stdlib/fmt.js +0 -178
- 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 -972
- package/src/backend/js/stdlib/maps.js +0 -45
- package/src/backend/js/stdlib/math.js +0 -69
- 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 -24
- package/src/backend/js/stdlib/slices.js +0 -77
- package/src/backend/js/stdlib/sort.js +0 -30
- package/src/backend/js/stdlib/strconv.js +0 -74
- package/src/backend/js/stdlib/strings.js +0 -222
- package/src/backend/js/stdlib/sync.js +0 -13
- package/src/backend/js/stdlib/testing.js +0 -34
- package/src/backend/js/stdlib/time.js +0 -128
- package/src/backend/js/stdlib/unicode.js +0 -25
- package/src/backend/js/stdlib/utf8.js +0 -26
- package/src/backend/wasm/const-fold.js +0 -205
- package/src/backend/wasm/emit-stdlib.js +0 -13
- package/src/backend/wasm/stdlib/bytes.js +0 -5039
- package/src/backend/wasm/stdlib/core.js +0 -8903
- package/src/backend/wasm/stdlib/io.js +0 -2161
- package/src/backend/wasm/stdlib/packages.js +0 -2400
- package/src/backend/wasm/stdlib/pure.js +0 -720
- package/src/backend/wasm/stdlib/time.js +0 -1200
- package/src/typechecker/stdlib/core.js +0 -801
- package/src/typechecker/stdlib/extended.js +0 -914
- package/src/typechecker/stdlib.js +0 -22
- /package/src/{asset-manager.js → cli/asset-manager.js} +0 -0
- /package/src/{colors.js → cli/colors.js} +0 -0
- /package/src/{dev-server.js → cli/dev-server.js} +0 -0
- /package/src/{minifier.js → cli/minifier.js} +0 -0
- /package/src/{project-config.js → cli/project-config.js} +0 -0
- /package/src/{pwa.js → cli/pwa.js} +0 -0
|
@@ -1,222 +0,0 @@
|
|
|
1
|
-
// CodeGen for Go `strings` package.
|
|
2
|
-
|
|
3
|
-
/** @typedef {import('../index.js').CodeGen} CodeGen */
|
|
4
|
-
|
|
5
|
-
const STRINGS_M1 = {
|
|
6
|
-
ToUpper: "toUpperCase",
|
|
7
|
-
ToLower: "toLowerCase",
|
|
8
|
-
TrimSpace: "trim",
|
|
9
|
-
ToTitle: "toUpperCase",
|
|
10
|
-
};
|
|
11
|
-
const STRINGS_M2 = {
|
|
12
|
-
Contains: "includes",
|
|
13
|
-
HasPrefix: "startsWith",
|
|
14
|
-
HasSuffix: "endsWith",
|
|
15
|
-
Index: "indexOf",
|
|
16
|
-
LastIndex: "lastIndexOf",
|
|
17
|
-
Repeat: "repeat",
|
|
18
|
-
Split: "split",
|
|
19
|
-
Join: "join",
|
|
20
|
-
};
|
|
21
|
-
const STRINGS_DISPATCH = {
|
|
22
|
-
Count: ([s, s2]) =>
|
|
23
|
-
`((s, sep) => sep === "" ? s.length + 1 : s.split(sep).length - 1)(${s}, ${s2})`,
|
|
24
|
-
Replace: ([s, s2, s3, n]) =>
|
|
25
|
-
n === undefined
|
|
26
|
-
? `${s}.replace(${s2}, ${s3})`
|
|
27
|
-
: `((str, o, nw, cnt) => { if (cnt < 0) return str.replaceAll(o, nw); let r = ""; let i = 0; for (; cnt > 0; cnt--) { const j = str.indexOf(o, i); if (j < 0) break; r += str.slice(i, j) + nw; i = j + o.length; if (o.length === 0) { if (i >= str.length) break; r += str[i]; i++; } } return r + str.slice(i); })(${s}, ${s2}, ${s3}, ${n})`,
|
|
28
|
-
ReplaceAll: ([s, s2, s3]) => `${s}.replaceAll(${s2}, ${s3})`,
|
|
29
|
-
Trim: ([s, s2]) =>
|
|
30
|
-
`((str, cut) => { const cs = new Set(cut); let i = 0, j = str.length; while (i < j && cs.has(str[i])) i++; while (j > i && cs.has(str[j - 1])) j--; return str.slice(i, j); })(${s}, ${s2})`,
|
|
31
|
-
TrimPrefix: ([s, s2]) =>
|
|
32
|
-
`((s, pre) => s.startsWith(pre) ? s.slice(pre.length) : s)(${s}, ${s2})`,
|
|
33
|
-
TrimSuffix: ([s, s2]) =>
|
|
34
|
-
`((s, suf) => !suf.length || !s.endsWith(suf) ? s : s.slice(0, -suf.length))(${s}, ${s2})`,
|
|
35
|
-
TrimLeft: ([s, s2]) =>
|
|
36
|
-
`((str, cut) => { const cs = new Set(cut); let i = 0; while (i < str.length && cs.has(str[i])) i++; return str.slice(i); })(${s}, ${s2})`,
|
|
37
|
-
TrimRight: ([s, s2]) =>
|
|
38
|
-
`((str, cut) => { const cs = new Set(cut); let j = str.length; while (j > 0 && cs.has(str[j - 1])) j--; return str.slice(0, j); })(${s}, ${s2})`,
|
|
39
|
-
EqualFold: ([s, s2]) => `${s}.toLowerCase() === ${s2}.toLowerCase()`,
|
|
40
|
-
Fields: ([s]) => `(${s}).trim() === '' ? [] : (${s}).trim().split(/\\s+/)`,
|
|
41
|
-
FieldsFunc: ([s, f]) =>
|
|
42
|
-
`((s, fn) => { const res = []; const runes = Array.from(s ?? ""); let cur = ""; for (const ch of runes) { const cp = ch.codePointAt(0); if (fn(cp)) { if (cur.length > 0) { res.push(cur); cur = ""; } } else { cur += ch; } } if (cur.length > 0) res.push(cur); return res; })(${s}, ${f})`,
|
|
43
|
-
Cut: ([s, s2]) =>
|
|
44
|
-
`((s, sep) => { const i = s.indexOf(sep); return i < 0 ? [s, "", false] : [s.slice(0, i), s.slice(i + sep.length), true]; })(${s}, ${s2})`,
|
|
45
|
-
CutPrefix: ([s, s2]) =>
|
|
46
|
-
`((str, pre) => str.startsWith(pre) ? [str.slice(pre.length), true] : [str, false])(${s}, ${s2})`,
|
|
47
|
-
CutSuffix: ([s, s2]) =>
|
|
48
|
-
`((str, suf) => { if (suf === "") return [str, true]; return str.endsWith(suf) ? [str.slice(0, -suf.length), true] : [str, false]; })(${s}, ${s2})`,
|
|
49
|
-
SplitN: ([s, s2, s3]) =>
|
|
50
|
-
`((s, sep, n) => {
|
|
51
|
-
if (n === 0) return null;
|
|
52
|
-
if (n < 0) return sep === "" ? [...s] : s.split(sep);
|
|
53
|
-
if (sep === "") {
|
|
54
|
-
const chars = [...s];
|
|
55
|
-
if (chars.length <= n) return chars;
|
|
56
|
-
const r = chars.slice(0, n - 1);
|
|
57
|
-
r.push(chars.slice(n - 1).join(""));
|
|
58
|
-
return r;
|
|
59
|
-
}
|
|
60
|
-
const r = [];
|
|
61
|
-
let cur = s;
|
|
62
|
-
for (let i = 1; i < n && cur.length; i++) {
|
|
63
|
-
const j = cur.indexOf(sep);
|
|
64
|
-
if (j < 0) break;
|
|
65
|
-
r.push(cur.slice(0, j));
|
|
66
|
-
cur = cur.slice(j + sep.length);
|
|
67
|
-
}
|
|
68
|
-
r.push(cur);
|
|
69
|
-
return r;
|
|
70
|
-
})(${s}, ${s2}, ${s3})`,
|
|
71
|
-
SplitAfter: ([s, s2]) =>
|
|
72
|
-
`((s, sep) => {
|
|
73
|
-
if (sep === "") return [...s];
|
|
74
|
-
const r = [];
|
|
75
|
-
let cur = s;
|
|
76
|
-
while (true) {
|
|
77
|
-
const j = cur.indexOf(sep);
|
|
78
|
-
if (j < 0) { r.push(cur); break; }
|
|
79
|
-
r.push(cur.slice(0, j + sep.length));
|
|
80
|
-
cur = cur.slice(j + sep.length);
|
|
81
|
-
}
|
|
82
|
-
return r;
|
|
83
|
-
})(${s}, ${s2})`,
|
|
84
|
-
SplitAfterN: ([s, s2, s3]) =>
|
|
85
|
-
`((s, sep, n) => {
|
|
86
|
-
if (n === 0) return null;
|
|
87
|
-
if (sep === "") {
|
|
88
|
-
const chars = [...s];
|
|
89
|
-
if (n < 0 || chars.length <= n) return chars;
|
|
90
|
-
const r = chars.slice(0, n - 1);
|
|
91
|
-
r.push(chars.slice(n - 1).join(""));
|
|
92
|
-
return r;
|
|
93
|
-
}
|
|
94
|
-
const r = [];
|
|
95
|
-
let cur = s;
|
|
96
|
-
for (let i = 1; (n < 0 || i < n) && cur.length; i++) {
|
|
97
|
-
const j = cur.indexOf(sep);
|
|
98
|
-
if (j < 0) break;
|
|
99
|
-
r.push(cur.slice(0, j + sep.length));
|
|
100
|
-
cur = cur.slice(j + sep.length);
|
|
101
|
-
}
|
|
102
|
-
r.push(cur);
|
|
103
|
-
return r;
|
|
104
|
-
})(${s}, ${s2}, ${s3})`,
|
|
105
|
-
IndexAny: ([s, s2]) =>
|
|
106
|
-
`((s, chars) => { let m = -1; for (const c of chars) { const i = s.indexOf(c); if (i >= 0 && (m < 0 || i < m)) m = i; } return m; })(${s}, ${s2})`,
|
|
107
|
-
LastIndexAny: ([s, s2]) =>
|
|
108
|
-
`((s, chars) => { let m = -1; for (const c of chars) { const i = s.lastIndexOf(c); if (i > m) m = i; } return m; })(${s}, ${s2})`,
|
|
109
|
-
ContainsAny: ([s, s2]) => `[...(${s2})].some(c => (${s}).includes(c))`,
|
|
110
|
-
ContainsRune: ([s, s2]) => `(${s}).includes(String.fromCodePoint(${s2}))`,
|
|
111
|
-
IndexRune: ([s, s2]) => `(${s}).indexOf(String.fromCodePoint(${s2}))`,
|
|
112
|
-
IndexByte: ([s, s2]) => `(${s}).indexOf(String.fromCharCode(${s2}))`,
|
|
113
|
-
LastIndexByte: ([s, s2]) => `(${s}).lastIndexOf(String.fromCharCode(${s2}))`,
|
|
114
|
-
Map: ([s, s2]) =>
|
|
115
|
-
`((f, s) => { let out = ""; for (const c of s) { const r = f(c.codePointAt(0)); if (r >= 0) out += String.fromCodePoint(r); } return out; })(${s}, ${s2})`,
|
|
116
|
-
Title: ([s]) => `(${s}).replace(/\\b\\w/g, c => c.toUpperCase())`,
|
|
117
|
-
TrimFunc: ([s, s2]) =>
|
|
118
|
-
`((s, f) => {
|
|
119
|
-
let l = 0, r = s.length;
|
|
120
|
-
while (l < r) {
|
|
121
|
-
const cp = s.codePointAt(l);
|
|
122
|
-
if (!f(cp)) break;
|
|
123
|
-
l += cp > 0xFFFF ? 2 : 1;
|
|
124
|
-
}
|
|
125
|
-
while (r > l) {
|
|
126
|
-
let cp, step;
|
|
127
|
-
if (r > 1 && s.charCodeAt(r - 1) >= 0xDC00 && s.charCodeAt(r - 1) <= 0xDFFF &&
|
|
128
|
-
s.charCodeAt(r - 2) >= 0xD800 && s.charCodeAt(r - 2) <= 0xDBFF) {
|
|
129
|
-
cp = s.codePointAt(r - 2);
|
|
130
|
-
step = 2;
|
|
131
|
-
} else {
|
|
132
|
-
cp = s.codePointAt(r - 1);
|
|
133
|
-
step = 1;
|
|
134
|
-
}
|
|
135
|
-
if (!f(cp)) break;
|
|
136
|
-
r -= step;
|
|
137
|
-
}
|
|
138
|
-
return s.slice(l, r);
|
|
139
|
-
})(${s}, ${s2})`,
|
|
140
|
-
TrimLeftFunc: ([s, s2]) =>
|
|
141
|
-
`((s, f) => {
|
|
142
|
-
let l = 0;
|
|
143
|
-
while (l < s.length) {
|
|
144
|
-
const cp = s.codePointAt(l);
|
|
145
|
-
if (!f(cp)) break;
|
|
146
|
-
l += cp > 0xFFFF ? 2 : 1;
|
|
147
|
-
}
|
|
148
|
-
return s.slice(l);
|
|
149
|
-
})(${s}, ${s2})`,
|
|
150
|
-
TrimRightFunc: ([s, s2]) =>
|
|
151
|
-
`((s, f) => {
|
|
152
|
-
let r = s.length;
|
|
153
|
-
while (r > 0) {
|
|
154
|
-
let cp, step;
|
|
155
|
-
if (r > 1 && s.charCodeAt(r - 1) >= 0xDC00 && s.charCodeAt(r - 1) <= 0xDFFF &&
|
|
156
|
-
s.charCodeAt(r - 2) >= 0xD800 && s.charCodeAt(r - 2) <= 0xDBFF) {
|
|
157
|
-
cp = s.codePointAt(r - 2);
|
|
158
|
-
step = 2;
|
|
159
|
-
} else {
|
|
160
|
-
cp = s.codePointAt(r - 1);
|
|
161
|
-
step = 1;
|
|
162
|
-
}
|
|
163
|
-
if (!f(cp)) break;
|
|
164
|
-
r -= step;
|
|
165
|
-
}
|
|
166
|
-
return s.slice(0, r);
|
|
167
|
-
})(${s}, ${s2})`,
|
|
168
|
-
IndexFunc: ([s, s2]) =>
|
|
169
|
-
`((s, f) => { for (let i = 0; i < s.length; i++) { const cp = s.codePointAt(i); if (f(cp)) return i; if (cp > 0xFFFF) i++; } return -1; })(${s}, ${s2})`,
|
|
170
|
-
LastIndexFunc: ([s, s2]) =>
|
|
171
|
-
`((s, f) => { for (let i = s.length - 1; i >= 0; i--) { let cp = s.codePointAt(i); if (i > 0 && s.charCodeAt(i) >= 0xDC00 && s.charCodeAt(i) <= 0xDFFF && s.charCodeAt(i - 1) >= 0xD800 && s.charCodeAt(i - 1) <= 0xDBFF) { cp = s.codePointAt(i - 1); i--; } if (f(cp)) return i; } return -1; })(${s}, ${s2})`,
|
|
172
|
-
NewReader: ([s]) =>
|
|
173
|
-
`((s) => ({ _bytes: new TextEncoder().encode(s), _pos: 0, Read(p) { if (this._pos >= this._bytes.length) return [0, __io_eof]; const n = Math.min(p.length, this._bytes.length - this._pos); for (let i = 0; i < n; i++) p[i] = this._bytes[this._pos + i]; this._pos += n; return [n, null]; }, Len() { return Math.max(0, this._bytes.length - this._pos); }, Reset(s2) { this._bytes = new TextEncoder().encode(s2); this._pos = 0; } }))(${s})`,
|
|
174
|
-
NewReplacer: (args) =>
|
|
175
|
-
`((...pairs) => {
|
|
176
|
-
const p = [];
|
|
177
|
-
for (let i = 0; i < pairs.length; i += 2) {
|
|
178
|
-
if (pairs[i] !== undefined && pairs[i+1] !== undefined) {
|
|
179
|
-
p.push([pairs[i], pairs[i+1]]);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
return {
|
|
183
|
-
_p: p,
|
|
184
|
-
Replace(s) {
|
|
185
|
-
s = s ?? "";
|
|
186
|
-
if (!this._p || this._p.length === 0 || s === "") return s;
|
|
187
|
-
let out = "";
|
|
188
|
-
let i = 0;
|
|
189
|
-
while (i < s.length) {
|
|
190
|
-
let matched = false;
|
|
191
|
-
for (let k = 0; k < this._p.length; k++) {
|
|
192
|
-
const [oldStr, newStr] = this._p[k];
|
|
193
|
-
if (oldStr.length > 0 && s.startsWith(oldStr, i)) {
|
|
194
|
-
out += newStr;
|
|
195
|
-
i += oldStr.length;
|
|
196
|
-
matched = true;
|
|
197
|
-
break;
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
if (!matched) {
|
|
201
|
-
out += s[i];
|
|
202
|
-
i++;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
return out;
|
|
206
|
-
}
|
|
207
|
-
};
|
|
208
|
-
})(${args.join(", ")})`,
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
/** @type {ThisType<CodeGen>} */
|
|
212
|
-
export const stringsMethods = {
|
|
213
|
-
_genStrings(fn, a) {
|
|
214
|
-
if (fn === "NewReader") this.useHelper("ioEOF");
|
|
215
|
-
const args = a();
|
|
216
|
-
const [s, s2] = args;
|
|
217
|
-
if (STRINGS_M1[fn]) return `${s}.${STRINGS_M1[fn]}()`;
|
|
218
|
-
if (STRINGS_M2[fn]) return `${s}.${STRINGS_M2[fn]}(${s2})`;
|
|
219
|
-
const gen = STRINGS_DISPATCH[fn];
|
|
220
|
-
return gen ? gen(args) : undefined;
|
|
221
|
-
},
|
|
222
|
-
};
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
// src/backend/js/stdlib/sync.js
|
|
2
|
-
// CodeGen for Go sync package.
|
|
3
|
-
|
|
4
|
-
export const syncMethods = {
|
|
5
|
-
_genSync(fn) {
|
|
6
|
-
this.useHelper("sync");
|
|
7
|
-
if (fn === "Once") return "__sync_once()";
|
|
8
|
-
if (fn === "Mutex") return "__sync_mutex()";
|
|
9
|
-
if (fn === "RWMutex") return "__sync_rwmutex()";
|
|
10
|
-
if (fn === "WaitGroup") return "__sync_waitgroup()";
|
|
11
|
-
return undefined;
|
|
12
|
-
},
|
|
13
|
-
};
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
// CodeGen for Go `testing` package and `*testing.T` methods.
|
|
2
|
-
|
|
3
|
-
/** @typedef {import('../index.js').CodeGen} CodeGen */
|
|
4
|
-
|
|
5
|
-
/** @type {ThisType<CodeGen>} */
|
|
6
|
-
export const testingMethods = {
|
|
7
|
-
_genTesting(fn, _a) {
|
|
8
|
-
switch (fn) {
|
|
9
|
-
case "Short":
|
|
10
|
-
return "false";
|
|
11
|
-
case "Verbose":
|
|
12
|
-
return "Boolean(globalThis.__gofront_verbose)";
|
|
13
|
-
}
|
|
14
|
-
return undefined;
|
|
15
|
-
},
|
|
16
|
-
|
|
17
|
-
_genTestingMethodCall(method, expr) {
|
|
18
|
-
this.useHelper("testing");
|
|
19
|
-
const recv = expr.func.expr;
|
|
20
|
-
const t = this.genExpr(recv);
|
|
21
|
-
const args = expr.args
|
|
22
|
-
.map((a) => (a._spread ? `...${this.genExpr(a)}` : this.genExpr(a)))
|
|
23
|
-
.join(", ");
|
|
24
|
-
if (
|
|
25
|
-
method === "Errorf" ||
|
|
26
|
-
method === "Fatalf" ||
|
|
27
|
-
method === "Logf" ||
|
|
28
|
-
method === "Skipf"
|
|
29
|
-
) {
|
|
30
|
-
this.useHelper("sprintf");
|
|
31
|
-
}
|
|
32
|
-
return `${t}.${method}(${args})`;
|
|
33
|
-
},
|
|
34
|
-
};
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
// CodeGen for Go `time` package — `time.Now`, `time.Since`, etc., plus `*Time` methods.
|
|
2
|
-
|
|
3
|
-
/** @typedef {import('../index.js').CodeGen} CodeGen */
|
|
4
|
-
|
|
5
|
-
const TIME_METHOD_DISPATCH = {
|
|
6
|
-
Year: (r) =>
|
|
7
|
-
`(${r}._offset === 0 ? ${r}._d.getUTCFullYear() : ${r}._d.getFullYear())`,
|
|
8
|
-
Month: (r) =>
|
|
9
|
-
`(${r}._offset === 0 ? ${r}._d.getUTCMonth() + 1 : ${r}._d.getMonth() + 1)`,
|
|
10
|
-
Day: (r) => `(${r}._offset === 0 ? ${r}._d.getUTCDate() : ${r}._d.getDate())`,
|
|
11
|
-
Hour: (r) =>
|
|
12
|
-
`(${r}._offset === 0 ? ${r}._d.getUTCHours() : ${r}._d.getHours())`,
|
|
13
|
-
Minute: (r) =>
|
|
14
|
-
`(${r}._offset === 0 ? ${r}._d.getUTCMinutes() : ${r}._d.getMinutes())`,
|
|
15
|
-
Second: (r) =>
|
|
16
|
-
`(${r}._offset === 0 ? ${r}._d.getUTCSeconds() : ${r}._d.getSeconds())`,
|
|
17
|
-
Weekday: (r) =>
|
|
18
|
-
`(${r}._offset === 0 ? ${r}._d.getUTCDay() : ${r}._d.getDay())`,
|
|
19
|
-
Unix: (r) => `Math.floor(${r}._d.getTime() / 1000)`,
|
|
20
|
-
UnixMilli: (r) => `${r}._d.getTime()`,
|
|
21
|
-
Add: (r, a) =>
|
|
22
|
-
`{_d: new Date(${r}._d.getTime() + Math.round(Number(${a[0]}) / 1000000))}`,
|
|
23
|
-
Sub: (r, a) =>
|
|
24
|
-
`Math.round((${r}._d.getTime() - (${a[0]})._d.getTime()) * 1000000)`,
|
|
25
|
-
Before: (r, a) => `(${r}._d < (${a[0]})._d)`,
|
|
26
|
-
After: (r, a) => `(${r}._d > (${a[0]})._d)`,
|
|
27
|
-
Equal: (r, a) => `(${r}._d.getTime() === (${a[0]})._d.getTime())`,
|
|
28
|
-
IsZero: (r) =>
|
|
29
|
-
`(!${r} || !${r}._d || ${r}._isZero === true || ${r}._d.getTime() === -62135596800000 || (${r}._d.getUTCFullYear() <= 1 && ${r}._d.getUTCMonth() === 0 && ${r}._d.getUTCDate() === 1 && ${r}._d.getUTCHours() === 0 && ${r}._d.getUTCMinutes() === 0 && ${r}._d.getUTCSeconds() === 0 && ${r}._d.getUTCMilliseconds() === 0))`,
|
|
30
|
-
UnixNano: (r) => `(${r}?._d ? Math.round(${r}._d.getTime() * 1000000) : 0)`,
|
|
31
|
-
Nanosecond: (r) =>
|
|
32
|
-
`(${r}?._d ? (${r}._offset === 0 ? ${r}._d.getUTCMilliseconds() : ${r}._d.getMilliseconds()) * 1000000 : 0)`,
|
|
33
|
-
UTC: (r) => `({_d: ${r}?._d ?? new Date(0), _offset: 0})`,
|
|
34
|
-
Local: (r) => `({_d: ${r}?._d ?? new Date(0)})`,
|
|
35
|
-
AddDate: (r, a) =>
|
|
36
|
-
`({_d: new Date(${r}._d.getFullYear() + Number(${a[0]}), ${r}._d.getMonth() + Number(${a[1]}), ${r}._d.getDate() + Number(${a[2]}))})`,
|
|
37
|
-
Truncate: (r, a) =>
|
|
38
|
-
`({_d: new Date(Math.floor(${r}._d.getTime() / (Number(${a[0]}) / 1000000)) * (Number(${a[0]}) / 1000000))})`,
|
|
39
|
-
Round: (r, a) =>
|
|
40
|
-
`({_d: new Date(Math.round(${r}._d.getTime() / (Number(${a[0]}) / 1000000)) * (Number(${a[0]}) / 1000000))})`,
|
|
41
|
-
Date: (r) =>
|
|
42
|
-
`[${r}._offset === 0 ? ${r}._d.getUTCFullYear() : ${r}._d.getFullYear(), ${r}._offset === 0 ? ${r}._d.getUTCMonth() + 1 : ${r}._d.getMonth() + 1, ${r}._offset === 0 ? ${r}._d.getUTCDate() : ${r}._d.getDate()]`,
|
|
43
|
-
Clock: (r) =>
|
|
44
|
-
`[${r}._offset === 0 ? ${r}._d.getUTCHours() : ${r}._d.getHours(), ${r}._offset === 0 ? ${r}._d.getUTCMinutes() : ${r}._d.getMinutes(), ${r}._offset === 0 ? ${r}._d.getUTCSeconds() : ${r}._d.getSeconds()]`,
|
|
45
|
-
YearDay: (r) =>
|
|
46
|
-
`Math.floor((${r}._d - new Date(${r}._d.getFullYear(), 0, 0)) / 86400000)`,
|
|
47
|
-
Compare: (r, a) =>
|
|
48
|
-
`(${r}._d.getTime() < (${a[0]})._d.getTime() ? -1 : (${r}._d.getTime() > (${a[0]})._d.getTime() ? 1 : 0))`,
|
|
49
|
-
In: (r) => `${r}`,
|
|
50
|
-
Location: () => `null`,
|
|
51
|
-
Zone: (r) =>
|
|
52
|
-
`[${r}._offset === 0 ? "UTC" : "LOCAL", ${r}._offset === 0 ? 0 : -${r}._d.getTimezoneOffset() * 60]`,
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
const DURATION_METHOD_DISPATCH = {
|
|
56
|
-
Nanoseconds: (r) => `${r}`,
|
|
57
|
-
Microseconds: (r) => `Math.trunc(Number(${r}) / 1000)`,
|
|
58
|
-
Milliseconds: (r) => `Math.trunc(Number(${r}) / 1000000)`,
|
|
59
|
-
Seconds: (r) => `(Number(${r}) / 1000000000)`,
|
|
60
|
-
Minutes: (r) => `(Number(${r}) / 60000000000)`,
|
|
61
|
-
Hours: (r) => `(Number(${r}) / 3600000000000)`,
|
|
62
|
-
String: (r) => `__durationString(${r})`,
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
const TIME_DISPATCH = {
|
|
66
|
-
Now: () =>
|
|
67
|
-
'(typeof globalThis.__GOFRONT_CLOCK === "function" ? {_d: new Date(globalThis.__GOFRONT_CLOCK())} : {_d: new Date()})',
|
|
68
|
-
Since: (a) =>
|
|
69
|
-
`(Math.round(((typeof globalThis.__GOFRONT_CLOCK === "function" ? globalThis.__GOFRONT_CLOCK() : Date.now()) - (${a[0]})._d.getTime()) * 1000000))`,
|
|
70
|
-
Sleep: (a) => `await new Promise(r => setTimeout(r, ${a[0]} / 1000000))`,
|
|
71
|
-
Unix: (a) =>
|
|
72
|
-
`{_d: new Date(Number(${a[0]}) * 1000 + Math.round(Number(${a[1] || 0}) / 1000000))}`,
|
|
73
|
-
UnixMicro: (a) => `{_d: new Date(Math.round(Number(${a[0]}) / 1000))}`,
|
|
74
|
-
UnixMilli: (a) => `{_d: new Date(Number(${a[0]}))}`,
|
|
75
|
-
Date: (a) =>
|
|
76
|
-
`{_d: new Date(${a[0]}, ${a[1]} - 1, ${a[2]}, ${a[3]}, ${a[4]}, ${a[5]}, Math.round(Number(${a[6] || 0}) / 1000000))}`,
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
/** @type {ThisType<CodeGen>} */
|
|
80
|
-
export const timeMethods = {
|
|
81
|
-
_genTimeMethodCall(method, expr) {
|
|
82
|
-
const recv = expr.func.expr;
|
|
83
|
-
const recvJs = this.genExpr(recv);
|
|
84
|
-
const args = expr.args.map((a) => this.genExpr(a));
|
|
85
|
-
if (method === "Format") {
|
|
86
|
-
this.useHelper("timeFmt");
|
|
87
|
-
return `__timeFmt(${recvJs}, ${args[0]})`;
|
|
88
|
-
}
|
|
89
|
-
if (method === "String") {
|
|
90
|
-
this.useHelper("timeFmt");
|
|
91
|
-
return `__timeFmt(${recvJs}, "2006-01-02 15:04:05.999999999 -0700 MST")`;
|
|
92
|
-
}
|
|
93
|
-
const gen = TIME_METHOD_DISPATCH[method];
|
|
94
|
-
return gen ? gen(recvJs, args) : undefined;
|
|
95
|
-
},
|
|
96
|
-
|
|
97
|
-
_genDurationMethodCall(method, expr) {
|
|
98
|
-
const recv = expr.func.expr;
|
|
99
|
-
const recvJs = this.genExpr(recv);
|
|
100
|
-
if (method === "String") {
|
|
101
|
-
this.useHelper("duration");
|
|
102
|
-
}
|
|
103
|
-
const gen = DURATION_METHOD_DISPATCH[method];
|
|
104
|
-
return gen ? gen(recvJs) : undefined;
|
|
105
|
-
},
|
|
106
|
-
|
|
107
|
-
_genTime(fn, a, expr) {
|
|
108
|
-
if (fn === "Parse") {
|
|
109
|
-
this.useHelper("timeParse");
|
|
110
|
-
return `__timeParse(${a()[0]}, ${a()[1]})`;
|
|
111
|
-
}
|
|
112
|
-
if (fn === "Date") {
|
|
113
|
-
const args = a();
|
|
114
|
-
const locArg = expr?.args?.[7];
|
|
115
|
-
const isUtc =
|
|
116
|
-
(locArg?.kind === "SelectorExpr" &&
|
|
117
|
-
(locArg.sel?.name === "UTC" || locArg.field === "UTC")) ||
|
|
118
|
-
(locArg?.kind === "Ident" && locArg.name === "UTC") ||
|
|
119
|
-
args[7] === '"UTC"';
|
|
120
|
-
if (isUtc) {
|
|
121
|
-
return `{_d: new Date(Date.UTC(${args[0]}, ${args[1]} - 1, ${args[2]}, ${args[3]}, ${args[4]}, ${args[5]}, Math.round(Number(${args[6] || 0}) / 1000000))), _offset: 0}`;
|
|
122
|
-
}
|
|
123
|
-
return `{_d: new Date(${args[0]}, ${args[1]} - 1, ${args[2]}, ${args[3]}, ${args[4]}, ${args[5]}, Math.round(Number(${args[6] || 0}) / 1000000))}`;
|
|
124
|
-
}
|
|
125
|
-
const gen = TIME_DISPATCH[fn];
|
|
126
|
-
return gen ? gen(a()) : undefined;
|
|
127
|
-
},
|
|
128
|
-
};
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
// CodeGen for Go `unicode` package.
|
|
2
|
-
|
|
3
|
-
/** @typedef {import('../index.js').CodeGen} CodeGen */
|
|
4
|
-
|
|
5
|
-
/** @type {ThisType<CodeGen>} */
|
|
6
|
-
export const unicodeMethods = {
|
|
7
|
-
_genUnicode(fn, a) {
|
|
8
|
-
const [arg] = a();
|
|
9
|
-
const cp = `String.fromCodePoint(${arg})`;
|
|
10
|
-
const UNICODE_TEST = {
|
|
11
|
-
IsLetter: `/\\p{L}/u.test(${cp})`,
|
|
12
|
-
IsDigit: `/\\p{Nd}/u.test(${cp})`,
|
|
13
|
-
IsSpace: `/\\s/.test(${cp})`,
|
|
14
|
-
IsUpper: `/\\p{Lu}/u.test(${cp})`,
|
|
15
|
-
IsLower: `/\\p{Ll}/u.test(${cp})`,
|
|
16
|
-
IsPunct: `/\\p{P}/u.test(${cp})`,
|
|
17
|
-
IsControl: `/\\p{Cc}/u.test(${cp})`,
|
|
18
|
-
IsPrint: `!/\\p{Cc}/u.test(${cp})`,
|
|
19
|
-
IsGraphic: `!/\\p{Cc}/u.test(${cp})`,
|
|
20
|
-
ToUpper: `${cp}.toUpperCase().codePointAt(0)`,
|
|
21
|
-
ToLower: `${cp}.toLowerCase().codePointAt(0)`,
|
|
22
|
-
};
|
|
23
|
-
return UNICODE_TEST[fn] ?? undefined;
|
|
24
|
-
},
|
|
25
|
-
};
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
// CodeGen for Go `unicode/utf8` package.
|
|
2
|
-
|
|
3
|
-
/** @typedef {import('../index.js').CodeGen} CodeGen */
|
|
4
|
-
|
|
5
|
-
const UTF8_DISPATCH = {
|
|
6
|
-
RuneCountInString: (a) => `[...(${a[0]})].length`,
|
|
7
|
-
RuneLen: (a) =>
|
|
8
|
-
`((r) => r < 0 ? -1 : r <= 0x7F ? 1 : r <= 0x7FF ? 2 : r <= 0xFFFF ? (r >= 0xD800 && r <= 0xDFFF ? -1 : 3) : r <= 0x10FFFF ? 4 : -1)(${a[0]})`,
|
|
9
|
-
ValidString: (a) =>
|
|
10
|
-
`/[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?<![\\uD800-\\uDBFF])[\\uDC00-\\uDFFF]/.test(${a[0]}) === false`,
|
|
11
|
-
ValidRune: (a) =>
|
|
12
|
-
`(${a[0]}) >= 0 && (${a[0]}) <= 0x10FFFF && !((${a[0]}) >= 0xD800 && (${a[0]}) <= 0xDFFF)`,
|
|
13
|
-
DecodeRuneInString: (a) =>
|
|
14
|
-
`((s) => { if (!s) return [0xFFFD, 0]; const cp = s.codePointAt(0); return [cp, cp > 0xFFFF ? 2 : 1]; })(${a[0]})`,
|
|
15
|
-
DecodeLastRuneInString: (a) =>
|
|
16
|
-
`((s) => { if (!s) return [0xFFFD, 0]; const i = s.length > 1 && s.charCodeAt(s.length - 1) >= 0xDC00 && s.charCodeAt(s.length - 1) <= 0xDFFF ? s.length - 2 : s.length - 1; const cp = s.codePointAt(i); return [cp, cp > 0xFFFF ? 2 : 1]; })(${a[0]})`,
|
|
17
|
-
FullRuneInString: (a) => `(${a[0]}).length > 0`,
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
/** @type {ThisType<CodeGen>} */
|
|
21
|
-
export const utf8Methods = {
|
|
22
|
-
_genUtf8(fn, a) {
|
|
23
|
-
const gen = UTF8_DISPATCH[fn];
|
|
24
|
-
return gen ? gen(a()) : undefined;
|
|
25
|
-
},
|
|
26
|
-
};
|
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
// Compile-time folding of package-level constant expressions for the wasm
|
|
2
|
-
// backend. Folds literals, other already-folded constants, unary/binary
|
|
3
|
-
// operators and `len("literal")` into a single BasicLit. Anything it cannot
|
|
4
|
-
// reduce returns null and the caller falls back to a runtime-initialised
|
|
5
|
-
// global (see `_collectPackageConsts`).
|
|
6
|
-
|
|
7
|
-
const INT = "INT";
|
|
8
|
-
const FLOAT = "FLOAT";
|
|
9
|
-
const STRING = "STRING";
|
|
10
|
-
const BOOL = "BOOL";
|
|
11
|
-
|
|
12
|
-
function fromLit(lit) {
|
|
13
|
-
switch (lit.litKind) {
|
|
14
|
-
case INT: {
|
|
15
|
-
const s = String(lit.value);
|
|
16
|
-
const clean = /^0[0-7]+$/.test(s) ? `0o${s.slice(1)}` : s;
|
|
17
|
-
return { k: INT, v: BigInt(clean) };
|
|
18
|
-
}
|
|
19
|
-
case FLOAT: {
|
|
20
|
-
const n = Number(lit.value);
|
|
21
|
-
return Number.isFinite(n) ? { k: FLOAT, v: n } : null;
|
|
22
|
-
}
|
|
23
|
-
case STRING:
|
|
24
|
-
return { k: STRING, v: String(lit.value ?? "") };
|
|
25
|
-
case BOOL:
|
|
26
|
-
return { k: BOOL, v: lit.value === "true" };
|
|
27
|
-
default:
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function toLit(val) {
|
|
33
|
-
switch (val.k) {
|
|
34
|
-
case INT:
|
|
35
|
-
return { kind: "BasicLit", litKind: INT, value: val.v.toString() };
|
|
36
|
-
case FLOAT:
|
|
37
|
-
return {
|
|
38
|
-
kind: "BasicLit",
|
|
39
|
-
litKind: FLOAT,
|
|
40
|
-
value: Object.is(val.v, -0) ? "-0" : String(val.v),
|
|
41
|
-
};
|
|
42
|
-
case STRING:
|
|
43
|
-
return { kind: "BasicLit", litKind: STRING, value: val.v };
|
|
44
|
-
default:
|
|
45
|
-
return {
|
|
46
|
-
kind: "BasicLit",
|
|
47
|
-
litKind: BOOL,
|
|
48
|
-
value: val.v ? "true" : "false",
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function asFloat(val) {
|
|
54
|
-
return val.k === INT ? Number(val.v) : val.v;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function foldUnary(op, x) {
|
|
58
|
-
if (op === "-" && x.k === INT) return { k: INT, v: -x.v };
|
|
59
|
-
if (op === "-" && x.k === FLOAT) return { k: FLOAT, v: -x.v };
|
|
60
|
-
if (op === "+" && (x.k === INT || x.k === FLOAT)) return x;
|
|
61
|
-
if (op === "!" && x.k === BOOL) return { k: BOOL, v: !x.v };
|
|
62
|
-
if (op === "^" && x.k === INT) return { k: INT, v: ~x.v };
|
|
63
|
-
return null;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function compare(op, a, b) {
|
|
67
|
-
switch (op) {
|
|
68
|
-
case "==":
|
|
69
|
-
return a === b;
|
|
70
|
-
case "!=":
|
|
71
|
-
return a !== b;
|
|
72
|
-
case "<":
|
|
73
|
-
return a < b;
|
|
74
|
-
case "<=":
|
|
75
|
-
return a <= b;
|
|
76
|
-
case ">":
|
|
77
|
-
return a > b;
|
|
78
|
-
case ">=":
|
|
79
|
-
return a >= b;
|
|
80
|
-
default:
|
|
81
|
-
return null;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function foldInts(op, a, b) {
|
|
86
|
-
switch (op) {
|
|
87
|
-
case "+":
|
|
88
|
-
return { k: INT, v: a + b };
|
|
89
|
-
case "-":
|
|
90
|
-
return { k: INT, v: a - b };
|
|
91
|
-
case "*":
|
|
92
|
-
return { k: INT, v: a * b };
|
|
93
|
-
case "/":
|
|
94
|
-
if (b === 0n) throw new Error("division by zero");
|
|
95
|
-
return { k: INT, v: a / b };
|
|
96
|
-
case "%":
|
|
97
|
-
if (b === 0n) throw new Error("division by zero");
|
|
98
|
-
return { k: INT, v: a % b };
|
|
99
|
-
case "&":
|
|
100
|
-
return { k: INT, v: a & b };
|
|
101
|
-
case "|":
|
|
102
|
-
return { k: INT, v: a | b };
|
|
103
|
-
case "^":
|
|
104
|
-
return { k: INT, v: a ^ b };
|
|
105
|
-
case "&^":
|
|
106
|
-
return { k: INT, v: a & ~b };
|
|
107
|
-
case "<<":
|
|
108
|
-
if (b < 0n) throw new Error("negative shift count");
|
|
109
|
-
return b > 1024n ? null : { k: INT, v: a << b };
|
|
110
|
-
case ">>":
|
|
111
|
-
if (b < 0n) throw new Error("negative shift count");
|
|
112
|
-
return b > 1024n ? null : { k: INT, v: a >> b };
|
|
113
|
-
default: {
|
|
114
|
-
const r = compare(op, a, b);
|
|
115
|
-
return r === null ? null : { k: BOOL, v: r };
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function foldFloats(op, a, b) {
|
|
121
|
-
let v;
|
|
122
|
-
switch (op) {
|
|
123
|
-
case "+":
|
|
124
|
-
v = a + b;
|
|
125
|
-
break;
|
|
126
|
-
case "-":
|
|
127
|
-
v = a - b;
|
|
128
|
-
break;
|
|
129
|
-
case "*":
|
|
130
|
-
v = a * b;
|
|
131
|
-
break;
|
|
132
|
-
case "/":
|
|
133
|
-
if (b === 0) throw new Error("division by zero");
|
|
134
|
-
v = a / b;
|
|
135
|
-
break;
|
|
136
|
-
default: {
|
|
137
|
-
const r = compare(op, a, b);
|
|
138
|
-
return r === null ? null : { k: BOOL, v: r };
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
return Number.isFinite(v) ? { k: FLOAT, v } : null;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
function foldBinary(op, a, b) {
|
|
145
|
-
if (a.k === BOOL && b.k === BOOL) {
|
|
146
|
-
if (op === "&&") return { k: BOOL, v: a.v && b.v };
|
|
147
|
-
if (op === "||") return { k: BOOL, v: a.v || b.v };
|
|
148
|
-
const r = op === "==" || op === "!=" ? compare(op, a.v, b.v) : null;
|
|
149
|
-
return r === null ? null : { k: BOOL, v: r };
|
|
150
|
-
}
|
|
151
|
-
if (a.k === STRING && b.k === STRING) {
|
|
152
|
-
if (op === "+") return { k: STRING, v: a.v + b.v };
|
|
153
|
-
const r = compare(op, a.v, b.v);
|
|
154
|
-
return r === null ? null : { k: BOOL, v: r };
|
|
155
|
-
}
|
|
156
|
-
if (a.k === INT && b.k === INT) return foldInts(op, a.v, b.v);
|
|
157
|
-
if (
|
|
158
|
-
(a.k === INT || a.k === FLOAT) &&
|
|
159
|
-
(b.k === INT || b.k === FLOAT) &&
|
|
160
|
-
op !== "<<" &&
|
|
161
|
-
op !== ">>"
|
|
162
|
-
) {
|
|
163
|
-
return foldFloats(op, asFloat(a), asFloat(b));
|
|
164
|
-
}
|
|
165
|
-
return null;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function foldExpr(expr, resolve) {
|
|
169
|
-
switch (expr?.kind) {
|
|
170
|
-
case "BasicLit":
|
|
171
|
-
return fromLit(expr);
|
|
172
|
-
case "Ident": {
|
|
173
|
-
if (expr.name === "true" || expr.name === "false")
|
|
174
|
-
return { k: BOOL, v: expr.name === "true" };
|
|
175
|
-
const lit = resolve(expr.name);
|
|
176
|
-
return lit ? fromLit(lit) : null;
|
|
177
|
-
}
|
|
178
|
-
case "UnaryExpr": {
|
|
179
|
-
const x = foldExpr(expr.operand, resolve);
|
|
180
|
-
return x ? foldUnary(expr.op, x) : null;
|
|
181
|
-
}
|
|
182
|
-
case "BinaryExpr": {
|
|
183
|
-
const a = foldExpr(expr.left, resolve);
|
|
184
|
-
const b = a ? foldExpr(expr.right, resolve) : null;
|
|
185
|
-
return a && b ? foldBinary(expr.op, a, b) : null;
|
|
186
|
-
}
|
|
187
|
-
case "CallExpr": {
|
|
188
|
-
if (expr.func?.kind !== "Ident" || expr.func.name !== "len") return null;
|
|
189
|
-
if (expr.args?.length !== 1) return null;
|
|
190
|
-
const s = foldExpr(expr.args[0], resolve);
|
|
191
|
-
if (s?.k !== STRING) return null;
|
|
192
|
-
// Gofront strings across JS and WASM runtime use UTF-16 code units (s.length).
|
|
193
|
-
return { k: INT, v: BigInt(s.v.length) };
|
|
194
|
-
}
|
|
195
|
-
default:
|
|
196
|
-
return null;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
// Folds `expr` into a BasicLit, or returns null when it needs the runtime.
|
|
201
|
-
// `resolve(name)` yields the BasicLit of an already-folded constant, if any.
|
|
202
|
-
export function foldConstExpr(expr, resolve) {
|
|
203
|
-
const val = foldExpr(expr, resolve);
|
|
204
|
-
return val ? toLit(val) : null;
|
|
205
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
// src/backend/wasm/emit-stdlib.js
|
|
2
|
-
// Backwards compatibility re-export.
|
|
3
|
-
// WASM stdlib implementations have moved to src/backend/wasm/stdlib/.
|
|
4
|
-
|
|
5
|
-
export {
|
|
6
|
-
BINARY_MATH_IMPORTS,
|
|
7
|
-
BytesEmitter,
|
|
8
|
-
IoEmitter,
|
|
9
|
-
PurePackagesEmitter,
|
|
10
|
-
StdlibCoreEmitter,
|
|
11
|
-
StdlibCoreEmitter as StdlibEmitter,
|
|
12
|
-
TimeEmitter,
|
|
13
|
-
} from "./stdlib/index.js";
|