gofront 1.4.0 → 1.6.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 +174 -403
- package/package.json +16 -19
- package/src/{codegen → backend/js}/expressions.js +265 -77
- package/src/{codegen → backend/js}/index.js +120 -89
- package/src/{codegen → backend/js}/runtime.js +102 -0
- package/src/{codegen → backend/js}/source-map.js +32 -0
- package/src/{codegen → backend/js}/statements.js +139 -64
- package/src/backend/js/stdlib/bits.js +32 -0
- package/src/{codegen → backend/js}/stdlib/errors.js +2 -2
- package/src/{codegen → backend/js}/stdlib/fmt.js +26 -8
- package/src/{codegen → backend/js}/stdlib/index.js +6 -0
- package/src/{codegen → backend/js}/stdlib/maps.js +1 -1
- package/src/{codegen → backend/js}/stdlib/path.js +2 -2
- package/src/backend/js/stdlib/shared.js +17 -0
- package/src/{codegen → backend/js}/stdlib/slices.js +1 -1
- package/src/{codegen → backend/js}/stdlib/sort.js +1 -1
- package/src/backend/js/stdlib/strconv.js +58 -0
- package/src/{codegen → backend/js}/stdlib/strings.js +4 -1
- package/src/{codegen → backend/js}/stdlib/testing.js +2 -2
- package/src/{codegen → backend/js}/stdlib/time.js +3 -3
- package/src/{codegen → backend/js}/templ.js +3 -3
- package/src/backend/wasm/boundary.js +1798 -0
- package/src/backend/wasm/emit-builtins.js +1103 -0
- package/src/backend/wasm/emit-exprs.js +1908 -0
- package/src/backend/wasm/emit-maps.js +1863 -0
- package/src/backend/wasm/emit-shared.js +519 -0
- package/src/backend/wasm/emit-stdlib.js +3006 -0
- package/src/backend/wasm/emit-stmts.js +2472 -0
- package/src/backend/wasm/emit.js +1376 -0
- package/src/backend/wasm/encode.js +937 -0
- package/src/backend/wasm/glue.js +144 -0
- package/src/backend/wasm/index.js +2790 -0
- package/src/backend/wasm/monomorph.js +711 -0
- package/src/backend/wasm/optimize.js +227 -0
- package/src/backend/wasm/types.js +544 -0
- package/src/backend/wasm/wat.js +302 -0
- package/src/cli-core.js +323 -75
- package/src/compiler.js +447 -14
- package/src/dev-server.js +56 -15
- package/src/index.js +99 -120
- package/src/lexer.js +18 -0
- package/src/lower/boxing.js +35 -0
- package/src/lower/captures.js +172 -0
- package/src/lower/embedding.js +30 -0
- package/src/lower/escape.js +164 -0
- package/src/lower/functions.js +302 -0
- package/src/lower/globals.js +316 -0
- package/src/lower/index.js +118 -0
- package/src/lower/ownership.js +137 -0
- package/src/lower/range.js +67 -0
- package/src/lower/walk.js +25 -0
- package/src/minifier.js +2 -0
- package/src/parser/index.js +8 -3
- package/src/resolver.js +3 -1
- package/src/test-runner.js +94 -20
- package/src/typechecker/assignability.js +42 -1
- package/src/typechecker/expressions.js +182 -3
- package/src/typechecker/index.js +261 -5
- package/src/typechecker/resolve.js +35 -6
- package/src/typechecker/stdlib/core.js +34 -19
- package/src/typechecker/stdlib/extended.js +18 -3
- package/src/typechecker/stdlib/shared.js +53 -0
- package/src/typechecker/stdlib.js +2 -0
- package/src/typechecker/types.js +259 -6
- package/src/vendor.js +2 -2
- package/src/codegen/stdlib/strconv.js +0 -33
- /package/src/{codegen → backend/js}/stdlib/builder.js +0 -0
- /package/src/{codegen → backend/js}/stdlib/bytes.js +0 -0
- /package/src/{codegen → backend/js}/stdlib/gom.js +0 -0
- /package/src/{codegen → backend/js}/stdlib/html.js +0 -0
- /package/src/{codegen → backend/js}/stdlib/io.js +0 -0
- /package/src/{codegen → backend/js}/stdlib/math.js +0 -0
- /package/src/{codegen → backend/js}/stdlib/os.js +0 -0
- /package/src/{codegen → backend/js}/stdlib/rand.js +0 -0
- /package/src/{codegen → backend/js}/stdlib/regexp.js +0 -0
- /package/src/{codegen → backend/js}/stdlib/unicode.js +0 -0
- /package/src/{codegen → backend/js}/stdlib/utf8.js +0 -0
package/src/typechecker/types.js
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
// { kind: 'named', name: string, underlying: Type }
|
|
14
14
|
|
|
15
15
|
export class TypeCheckError extends Error {
|
|
16
|
-
constructor(msg, node, filename, sourceCode) {
|
|
16
|
+
constructor(msg, node, filename, sourceCode, hint = null) {
|
|
17
17
|
const lineNum = node?.line || node?._line;
|
|
18
18
|
const colNum = node?.col || node?._col;
|
|
19
19
|
const loc = filename
|
|
@@ -23,6 +23,7 @@ export class TypeCheckError extends Error {
|
|
|
23
23
|
: lineNum
|
|
24
24
|
? ` at line ${lineNum}:${colNum ?? 1}`
|
|
25
25
|
: "";
|
|
26
|
+
const hintText = hint ? `\n ${hint}` : "";
|
|
26
27
|
let lineContext = "";
|
|
27
28
|
if (lineNum && sourceCode) {
|
|
28
29
|
const lines = sourceCode.split("\n");
|
|
@@ -33,10 +34,260 @@ export class TypeCheckError extends Error {
|
|
|
33
34
|
lineContext = `\n${prefix}${lineStr}\n${" ".repeat(prefix.length)}${caretPad}^`;
|
|
34
35
|
}
|
|
35
36
|
}
|
|
36
|
-
super(`Type error${loc}: ${msg}${lineContext}`);
|
|
37
|
+
super(`Type error${loc}: ${msg}${hintText}${lineContext}`);
|
|
38
|
+
this.line = lineNum;
|
|
39
|
+
this.col = colNum;
|
|
37
40
|
}
|
|
38
41
|
}
|
|
39
42
|
|
|
43
|
+
export const BROWSER_GLOBALS = new Set([
|
|
44
|
+
"console",
|
|
45
|
+
"document",
|
|
46
|
+
"window",
|
|
47
|
+
"navigator",
|
|
48
|
+
"location",
|
|
49
|
+
"history",
|
|
50
|
+
"screen",
|
|
51
|
+
"performance",
|
|
52
|
+
"crypto",
|
|
53
|
+
"indexedDB",
|
|
54
|
+
"fetch",
|
|
55
|
+
"setTimeout",
|
|
56
|
+
"setInterval",
|
|
57
|
+
"clearTimeout",
|
|
58
|
+
"clearInterval",
|
|
59
|
+
"requestAnimationFrame",
|
|
60
|
+
"cancelAnimationFrame",
|
|
61
|
+
"Math",
|
|
62
|
+
"JSON",
|
|
63
|
+
"Date",
|
|
64
|
+
"RegExp",
|
|
65
|
+
"Promise",
|
|
66
|
+
"Error",
|
|
67
|
+
"Symbol",
|
|
68
|
+
"String",
|
|
69
|
+
"Number",
|
|
70
|
+
"Boolean",
|
|
71
|
+
"Array",
|
|
72
|
+
"Object",
|
|
73
|
+
"parseInt",
|
|
74
|
+
"parseFloat",
|
|
75
|
+
"isNaN",
|
|
76
|
+
"isFinite",
|
|
77
|
+
"encodeURIComponent",
|
|
78
|
+
"decodeURIComponent",
|
|
79
|
+
"atob",
|
|
80
|
+
"btoa",
|
|
81
|
+
"alert",
|
|
82
|
+
"confirm",
|
|
83
|
+
"prompt",
|
|
84
|
+
"localStorage",
|
|
85
|
+
"sessionStorage",
|
|
86
|
+
]);
|
|
87
|
+
|
|
88
|
+
// Stdlib packages the WASM backend can emit. Extend as src/backend/wasm/emit-stdlib.js
|
|
89
|
+
// gains support; anything not listed is rejected at import with "not yet available in
|
|
90
|
+
// wasm packages".
|
|
91
|
+
export const WASM_SUPPORTED_STDLIB = new Set([
|
|
92
|
+
"math",
|
|
93
|
+
"math/bits",
|
|
94
|
+
"errors",
|
|
95
|
+
"fmt",
|
|
96
|
+
"strings",
|
|
97
|
+
"strconv",
|
|
98
|
+
"unicode/utf8",
|
|
99
|
+
"slices",
|
|
100
|
+
"maps",
|
|
101
|
+
"sort",
|
|
102
|
+
"testing",
|
|
103
|
+
"gofront/shared",
|
|
104
|
+
]);
|
|
105
|
+
|
|
106
|
+
// Per-namespace member subset the WASM backend implements. `null` means every
|
|
107
|
+
// member the checker knows about. Members outside the set are rejected in
|
|
108
|
+
// wasm/both packages with "'strings.Split' is not yet available in wasm packages".
|
|
109
|
+
export const WASM_STDLIB_MEMBERS = new Map([
|
|
110
|
+
[
|
|
111
|
+
"math",
|
|
112
|
+
new Set([
|
|
113
|
+
"Sqrt",
|
|
114
|
+
"Floor",
|
|
115
|
+
"Ceil",
|
|
116
|
+
"Trunc",
|
|
117
|
+
"Abs",
|
|
118
|
+
"Min",
|
|
119
|
+
"Max",
|
|
120
|
+
"Copysign",
|
|
121
|
+
"Dim",
|
|
122
|
+
"Inf",
|
|
123
|
+
"NaN",
|
|
124
|
+
"IsNaN",
|
|
125
|
+
"IsInf",
|
|
126
|
+
"Signbit",
|
|
127
|
+
"Sin",
|
|
128
|
+
"Cos",
|
|
129
|
+
"Tan",
|
|
130
|
+
"Asin",
|
|
131
|
+
"Acos",
|
|
132
|
+
"Atan",
|
|
133
|
+
"Atan2",
|
|
134
|
+
"Pow",
|
|
135
|
+
"Mod",
|
|
136
|
+
"Hypot",
|
|
137
|
+
"Cbrt",
|
|
138
|
+
"Exp",
|
|
139
|
+
"Exp2",
|
|
140
|
+
"Log",
|
|
141
|
+
"Log2",
|
|
142
|
+
"Log10",
|
|
143
|
+
"Round",
|
|
144
|
+
// constants
|
|
145
|
+
"E",
|
|
146
|
+
"Pi",
|
|
147
|
+
"Phi",
|
|
148
|
+
"Sqrt2",
|
|
149
|
+
"SqrtE",
|
|
150
|
+
"SqrtPi",
|
|
151
|
+
"SqrtPhi",
|
|
152
|
+
"Ln2",
|
|
153
|
+
"Log2E",
|
|
154
|
+
"Ln10",
|
|
155
|
+
"Log10E",
|
|
156
|
+
"MaxFloat32",
|
|
157
|
+
"SmallestNonzeroFloat32",
|
|
158
|
+
"MaxFloat64",
|
|
159
|
+
"SmallestNonzeroFloat64",
|
|
160
|
+
"MaxInt",
|
|
161
|
+
"MinInt",
|
|
162
|
+
"MaxInt8",
|
|
163
|
+
"MinInt8",
|
|
164
|
+
"MaxInt16",
|
|
165
|
+
"MinInt16",
|
|
166
|
+
"MaxInt32",
|
|
167
|
+
"MinInt32",
|
|
168
|
+
"MaxInt64",
|
|
169
|
+
"MinInt64",
|
|
170
|
+
"MaxUint8",
|
|
171
|
+
"MaxUint16",
|
|
172
|
+
"MaxUint32",
|
|
173
|
+
"MaxUint",
|
|
174
|
+
"MaxUint64",
|
|
175
|
+
]),
|
|
176
|
+
],
|
|
177
|
+
[
|
|
178
|
+
"bits",
|
|
179
|
+
new Set([
|
|
180
|
+
"LeadingZeros32",
|
|
181
|
+
"LeadingZeros64",
|
|
182
|
+
"TrailingZeros32",
|
|
183
|
+
"TrailingZeros64",
|
|
184
|
+
"OnesCount32",
|
|
185
|
+
"OnesCount64",
|
|
186
|
+
"RotateLeft32",
|
|
187
|
+
"RotateLeft64",
|
|
188
|
+
]),
|
|
189
|
+
],
|
|
190
|
+
["errors", new Set(["New", "Is", "Unwrap"])],
|
|
191
|
+
[
|
|
192
|
+
"utf8",
|
|
193
|
+
new Set([
|
|
194
|
+
"RuneCountInString",
|
|
195
|
+
"RuneLen",
|
|
196
|
+
"ValidString",
|
|
197
|
+
"ValidRune",
|
|
198
|
+
"DecodeRuneInString",
|
|
199
|
+
"DecodeLastRuneInString",
|
|
200
|
+
"FullRuneInString",
|
|
201
|
+
"RuneError",
|
|
202
|
+
"MaxRune",
|
|
203
|
+
"UTFMax",
|
|
204
|
+
]),
|
|
205
|
+
],
|
|
206
|
+
[
|
|
207
|
+
"sort",
|
|
208
|
+
new Set([
|
|
209
|
+
"Ints",
|
|
210
|
+
"Float64s",
|
|
211
|
+
"Strings",
|
|
212
|
+
"Slice",
|
|
213
|
+
"SliceStable",
|
|
214
|
+
"SliceIsSorted",
|
|
215
|
+
"Search",
|
|
216
|
+
"IntsAreSorted",
|
|
217
|
+
"Float64sAreSorted",
|
|
218
|
+
"StringsAreSorted",
|
|
219
|
+
]),
|
|
220
|
+
],
|
|
221
|
+
["fmt", new Set(["Sprintf", "Errorf", "Printf", "Println", "Print"])],
|
|
222
|
+
[
|
|
223
|
+
"strings",
|
|
224
|
+
new Set([
|
|
225
|
+
"Contains",
|
|
226
|
+
"ContainsAny",
|
|
227
|
+
"ContainsRune",
|
|
228
|
+
"HasPrefix",
|
|
229
|
+
"HasSuffix",
|
|
230
|
+
"Index",
|
|
231
|
+
"IndexAny",
|
|
232
|
+
"IndexByte",
|
|
233
|
+
"IndexRune",
|
|
234
|
+
"LastIndex",
|
|
235
|
+
"LastIndexByte",
|
|
236
|
+
"Count",
|
|
237
|
+
"Repeat",
|
|
238
|
+
"Replace",
|
|
239
|
+
"ReplaceAll",
|
|
240
|
+
"ToUpper",
|
|
241
|
+
"ToLower",
|
|
242
|
+
"Title",
|
|
243
|
+
"ToTitle",
|
|
244
|
+
"TrimSpace",
|
|
245
|
+
"Trim",
|
|
246
|
+
"TrimLeft",
|
|
247
|
+
"TrimRight",
|
|
248
|
+
"TrimPrefix",
|
|
249
|
+
"TrimSuffix",
|
|
250
|
+
"EqualFold",
|
|
251
|
+
"Split",
|
|
252
|
+
"Fields",
|
|
253
|
+
"Join",
|
|
254
|
+
]),
|
|
255
|
+
],
|
|
256
|
+
[
|
|
257
|
+
"strconv",
|
|
258
|
+
new Set([
|
|
259
|
+
"Itoa",
|
|
260
|
+
"Atoi",
|
|
261
|
+
"FormatInt",
|
|
262
|
+
"FormatFloat",
|
|
263
|
+
"FormatBool",
|
|
264
|
+
"ParseInt",
|
|
265
|
+
"ParseFloat",
|
|
266
|
+
"ParseBool",
|
|
267
|
+
"Quote",
|
|
268
|
+
]),
|
|
269
|
+
],
|
|
270
|
+
[
|
|
271
|
+
"slices",
|
|
272
|
+
new Set(["Sort", "Reverse", "Contains", "Index", "Equal", "Clone"]),
|
|
273
|
+
],
|
|
274
|
+
[
|
|
275
|
+
"maps",
|
|
276
|
+
new Set([
|
|
277
|
+
"Keys",
|
|
278
|
+
"Values",
|
|
279
|
+
"Clone",
|
|
280
|
+
"Copy",
|
|
281
|
+
"Equal",
|
|
282
|
+
"EqualFunc",
|
|
283
|
+
"Delete",
|
|
284
|
+
"DeleteFunc",
|
|
285
|
+
]),
|
|
286
|
+
],
|
|
287
|
+
]);
|
|
288
|
+
|
|
289
|
+
export const VALID_TARGETS = new Set(["js", "wasm", "both"]);
|
|
290
|
+
|
|
40
291
|
// ── Static operator sets (module-level for reuse) ────────────
|
|
41
292
|
export const CMP_OPS = new Set(["==", "!=", "<", ">", "<=", ">="]);
|
|
42
293
|
export const LOG_OPS = new Set(["&&", "||"]);
|
|
@@ -47,14 +298,14 @@ export const INT = { kind: "basic", name: "int" };
|
|
|
47
298
|
export const INT8 = { kind: "basic", name: "int8" };
|
|
48
299
|
export const INT16 = { kind: "basic", name: "int16" };
|
|
49
300
|
export const INT32 = { kind: "basic", name: "int32" };
|
|
50
|
-
|
|
301
|
+
const INT64 = { kind: "basic", name: "int64" };
|
|
51
302
|
export const UINT = { kind: "basic", name: "uint" };
|
|
52
303
|
export const UINT8 = { kind: "basic", name: "uint8" };
|
|
53
304
|
export const BYTE = UINT8;
|
|
54
305
|
export const UINT16 = { kind: "basic", name: "uint16" };
|
|
55
306
|
export const UINT32 = { kind: "basic", name: "uint32" };
|
|
56
307
|
export const UINT64 = { kind: "basic", name: "uint64" };
|
|
57
|
-
|
|
308
|
+
const UINTPTR = { kind: "basic", name: "uintptr" };
|
|
58
309
|
export const RUNE = INT32;
|
|
59
310
|
export const FLOAT32 = { kind: "basic", name: "float32" };
|
|
60
311
|
export const FLOAT64 = { kind: "basic", name: "float64" };
|
|
@@ -81,7 +332,7 @@ export const UNTYPED_BOOL = { kind: "untyped", base: "bool" };
|
|
|
81
332
|
|
|
82
333
|
// ── Complex types ────────────────────────────────────────────
|
|
83
334
|
export const COMPLEX128 = { kind: "basic", name: "complex128" };
|
|
84
|
-
|
|
335
|
+
const COMPLEX64 = { kind: "basic", name: "complex64" };
|
|
85
336
|
export const UNTYPED_COMPLEX = { kind: "untyped", base: "complex128" };
|
|
86
337
|
|
|
87
338
|
export const BASIC_TYPES = {
|
|
@@ -150,7 +401,7 @@ export const isBool = makeBasicPredicate("bool");
|
|
|
150
401
|
|
|
151
402
|
// ── TypedArray mappings ──────────────────────────────────────
|
|
152
403
|
// []float64 deliberately stays a plain Array for JSON / JS-library interop.
|
|
153
|
-
|
|
404
|
+
const TYPED_ARRAY_CONSTRUCTORS = {
|
|
154
405
|
float32: "Float32Array",
|
|
155
406
|
uint8: "Uint8Array",
|
|
156
407
|
byte: "Uint8Array",
|
|
@@ -240,6 +491,8 @@ export function typeStr(t) {
|
|
|
240
491
|
return `untyped ${t.base}`;
|
|
241
492
|
case "slice":
|
|
242
493
|
return `[]${typeStr(t.elem)}`;
|
|
494
|
+
case "shared":
|
|
495
|
+
return `shared.${t.shared}`;
|
|
243
496
|
case "array":
|
|
244
497
|
return `[${t.size ?? "..."}]${typeStr(t.elem)}`;
|
|
245
498
|
case "map":
|
package/src/vendor.js
CHANGED
|
@@ -59,7 +59,7 @@ ${exports.join(",\n")}
|
|
|
59
59
|
`;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
|
|
62
|
+
function findBundler(projectDir) {
|
|
63
63
|
const root = resolve(projectDir);
|
|
64
64
|
const pkgPath = join(root, "package.json");
|
|
65
65
|
let req;
|
|
@@ -136,7 +136,7 @@ export function loadVendorConfig(projectDir) {
|
|
|
136
136
|
return { dest, packages, minify, globals };
|
|
137
137
|
}
|
|
138
138
|
|
|
139
|
-
|
|
139
|
+
function resolveDestinationPaths(projectRoot, dest) {
|
|
140
140
|
const destList = Array.isArray(dest) ? dest : [dest];
|
|
141
141
|
if (destList.length === 0) {
|
|
142
142
|
throw new Error(
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
// CodeGen for Go `strconv` package.
|
|
2
|
-
|
|
3
|
-
/** @typedef {import('../index.js').CodeGen} CodeGen */
|
|
4
|
-
|
|
5
|
-
const STRCONV_DISPATCH = {
|
|
6
|
-
Itoa: (a) => `String(${a[0]})`,
|
|
7
|
-
Atoi: (a) =>
|
|
8
|
-
`(Number.isNaN(Number(${a[0]})) ? [0, "invalid syntax"] : [Number(${a[0]}) | 0, null])`,
|
|
9
|
-
FormatBool: (a) => `String(${a[0]})`,
|
|
10
|
-
FormatInt: (a) => `(${a[0]}).toString(${a[1]})`,
|
|
11
|
-
FormatFloat: (a) => `String(${a[0]})`,
|
|
12
|
-
ParseFloat: (a) =>
|
|
13
|
-
`(Number.isNaN(Number(${a[0]})) ? [0, "invalid syntax"] : [Number(${a[0]}), null])`,
|
|
14
|
-
ParseInt: (a) =>
|
|
15
|
-
`(Number.isNaN(parseInt(${a[0]}, ${a[1]} || 10)) ? [0, "invalid syntax"] : [parseInt(${a[0]}, ${a[1]} || 10), null])`,
|
|
16
|
-
ParseBool: (a) =>
|
|
17
|
-
`(${a[0]} === "true" || ${a[0]} === "1" ? [true, null] : ${a[0]} === "false" || ${a[0]} === "0" ? [false, null] : [false, "invalid syntax"])`,
|
|
18
|
-
Quote: (a) => `JSON.stringify(${a[0]})`,
|
|
19
|
-
Unquote: (a) =>
|
|
20
|
-
`((s) => { try { const v = JSON.parse(s); return [v, null]; } catch(e) { return ["", "invalid syntax"]; } })(${a[0]})`,
|
|
21
|
-
AppendInt: (a) =>
|
|
22
|
-
`[...(${a[0]}), ...new TextEncoder().encode((${a[1]}).toString(${a[2]}))]`,
|
|
23
|
-
AppendFloat: (a) =>
|
|
24
|
-
`[...(${a[0]}), ...new TextEncoder().encode(${a[1]}.toFixed(${a[3]} < 0 ? 6 : ${a[3]}))]`,
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
/** @type {ThisType<CodeGen>} */
|
|
28
|
-
export const strconvMethods = {
|
|
29
|
-
_genStrconv(fn, a) {
|
|
30
|
-
const gen = STRCONV_DISPATCH[fn];
|
|
31
|
-
return gen ? gen(a()) : undefined;
|
|
32
|
-
},
|
|
33
|
-
};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|