goscript 0.0.83 → 0.1.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 +13 -1
- package/cmd/goscript/cmd_compile.go +70 -69
- package/cmd/goscript/cmd_compile_test.go +79 -0
- package/cmd/goscript/main.go +10 -5
- package/compiler/compile-request.go +218 -0
- package/compiler/compiler.go +16 -1336
- package/compiler/compliance_test.go +196 -0
- package/compiler/config.go +6 -13
- package/compiler/diagnostic.go +70 -0
- package/compiler/index.test.ts +28 -28
- package/compiler/index.ts +40 -72
- package/compiler/lowered-program.go +132 -0
- package/compiler/lowering.go +3576 -0
- package/compiler/override-registry.go +422 -0
- package/compiler/override-registry_test.go +207 -0
- package/compiler/package-graph.go +231 -0
- package/compiler/package-graph_test.go +281 -0
- package/compiler/result.go +13 -0
- package/compiler/runtime-contract.go +279 -0
- package/compiler/runtime-contract_test.go +90 -0
- package/compiler/semantic-model-types.go +110 -0
- package/compiler/semantic-model.go +922 -0
- package/compiler/semantic-model_test.go +416 -0
- package/compiler/service.go +133 -0
- package/compiler/skeleton_test.go +1145 -0
- package/compiler/typescript-emitter.go +663 -0
- package/compiler/wasm/compile.go +2 -3
- package/compiler/wasm/compile_test.go +29 -0
- package/compiler/wasm_api.go +10 -159
- package/dist/compiler/index.d.ts +1 -3
- package/dist/compiler/index.js +31 -55
- package/dist/compiler/index.js.map +1 -1
- package/dist/gs/builtin/builtin.d.ts +13 -0
- package/dist/gs/builtin/builtin.js +27 -7
- package/dist/gs/builtin/builtin.js.map +1 -1
- package/dist/gs/builtin/channel.d.ts +3 -3
- package/dist/gs/builtin/channel.js.map +1 -1
- package/dist/gs/builtin/hostio.d.ts +86 -0
- package/dist/gs/builtin/hostio.js +266 -0
- package/dist/gs/builtin/hostio.js.map +1 -0
- package/dist/gs/builtin/index.d.ts +1 -0
- package/dist/gs/builtin/index.js +1 -0
- package/dist/gs/builtin/index.js.map +1 -1
- package/dist/gs/builtin/print.d.ts +8 -0
- package/dist/gs/builtin/print.js +111 -0
- package/dist/gs/builtin/print.js.map +1 -0
- package/dist/gs/builtin/slice.d.ts +1 -1
- package/dist/gs/builtin/slice.js.map +1 -1
- package/dist/gs/builtin/type.d.ts +11 -0
- package/dist/gs/builtin/type.js +55 -1
- package/dist/gs/builtin/type.js.map +1 -1
- package/dist/gs/bytes/buffer.gs.js.map +1 -1
- package/dist/gs/bytes/bytes.gs.js.map +1 -1
- package/dist/gs/bytes/reader.gs.js.map +1 -1
- package/dist/gs/context/context.js.map +1 -1
- package/dist/gs/crypto/rand/index.d.ts +5 -0
- package/dist/gs/crypto/rand/index.js +77 -0
- package/dist/gs/crypto/rand/index.js.map +1 -0
- package/dist/gs/encoding/json/index.d.ts +3 -0
- package/dist/gs/encoding/json/index.js +160 -0
- package/dist/gs/encoding/json/index.js.map +1 -0
- package/dist/gs/fmt/fmt.js +2 -22
- package/dist/gs/fmt/fmt.js.map +1 -1
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.d.ts +1 -1
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.js +1 -1
- package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.js.map +1 -1
- package/dist/gs/github.com/aperturerobotics/wasivm/wazero/kernel/runtime/browser/browser.js.map +1 -1
- package/dist/gs/github.com/pkg/errors/errors.js.map +1 -1
- package/dist/gs/github.com/pkg/errors/stack.js.map +1 -1
- package/dist/gs/go/scanner/index.d.ts +29 -0
- package/dist/gs/go/scanner/index.js +120 -0
- package/dist/gs/go/scanner/index.js.map +1 -0
- package/dist/gs/go/token/index.d.ts +31 -0
- package/dist/gs/go/token/index.js +82 -0
- package/dist/gs/go/token/index.js.map +1 -0
- package/dist/gs/internal/abi/index.js.map +1 -1
- package/dist/gs/io/fs/fs.js.map +1 -1
- package/dist/gs/io/fs/readdir.js.map +1 -1
- package/dist/gs/io/fs/readfile.js.map +1 -1
- package/dist/gs/io/fs/stat.js.map +1 -1
- package/dist/gs/io/fs/sub.js.map +1 -1
- package/dist/gs/io/io.js.map +1 -1
- package/dist/gs/os/dir_unix.gs.js.map +1 -1
- package/dist/gs/os/error.gs.js +2 -4
- package/dist/gs/os/error.gs.js.map +1 -1
- package/dist/gs/os/exec.gs.js.map +1 -1
- package/dist/gs/os/exec_posix.gs.js.map +1 -1
- package/dist/gs/os/rawconn_js.gs.js.map +1 -1
- package/dist/gs/os/root_js.gs.js.map +1 -1
- package/dist/gs/os/tempfile.gs.js +66 -9
- package/dist/gs/os/tempfile.gs.js.map +1 -1
- package/dist/gs/os/types.gs.js.map +1 -1
- package/dist/gs/os/types_js.gs.d.ts +2 -51
- package/dist/gs/os/types_js.gs.js +67 -105
- package/dist/gs/os/types_js.gs.js.map +1 -1
- package/dist/gs/os/types_unix.gs.js.map +1 -1
- package/dist/gs/path/filepath/match.js.map +1 -1
- package/dist/gs/path/match.js.map +1 -1
- package/dist/gs/path/path.js.map +1 -1
- package/dist/gs/reflect/index.d.ts +2 -2
- package/dist/gs/reflect/index.js +1 -1
- package/dist/gs/reflect/index.js.map +1 -1
- package/dist/gs/reflect/map.js.map +1 -1
- package/dist/gs/reflect/type.d.ts +2 -1
- package/dist/gs/reflect/type.js +85 -14
- package/dist/gs/reflect/type.js.map +1 -1
- package/dist/gs/reflect/types.js.map +1 -1
- package/dist/gs/reflect/visiblefields.js.map +1 -1
- package/dist/gs/runtime/runtime.js.map +1 -1
- package/dist/gs/sort/sort.gs.js.map +1 -1
- package/dist/gs/strconv/atoi.gs.js.map +1 -1
- package/dist/gs/strconv/quote.gs.js.map +1 -1
- package/dist/gs/strings/builder.js.map +1 -1
- package/dist/gs/strings/reader.js.map +1 -1
- package/dist/gs/strings/replace.js.map +1 -1
- package/dist/gs/sync/atomic/type.gs.js.map +1 -1
- package/dist/gs/sync/atomic/value.gs.js.map +1 -1
- package/dist/gs/sync/sync.d.ts +1 -0
- package/dist/gs/sync/sync.js +12 -0
- package/dist/gs/sync/sync.js.map +1 -1
- package/dist/gs/time/time.js.map +1 -1
- package/dist/gs/unicode/unicode.js.map +1 -1
- package/go.mod +2 -2
- package/gs/builtin/builtin.ts +31 -6
- package/gs/builtin/hostio.test.ts +246 -0
- package/gs/builtin/hostio.ts +413 -0
- package/gs/builtin/index.ts +1 -0
- package/gs/builtin/print.test.ts +48 -0
- package/gs/builtin/print.ts +154 -0
- package/gs/builtin/runtime-contract.test.ts +230 -0
- package/gs/builtin/type.ts +84 -1
- package/gs/crypto/rand/index.test.ts +32 -0
- package/gs/crypto/rand/index.ts +90 -0
- package/gs/crypto/rand/meta.json +5 -0
- package/gs/encoding/json/index.test.ts +65 -0
- package/gs/encoding/json/index.ts +186 -0
- package/gs/fmt/fmt.test.ts +41 -30
- package/gs/fmt/fmt.ts +2 -22
- package/gs/github.com/aperturerobotics/protobuf-go-lite/index.test.ts +23 -0
- package/gs/github.com/aperturerobotics/protobuf-go-lite/index.ts +3 -1
- package/gs/github.com/aperturerobotics/wasivm/wazero/kernel/runtime/browser/meta.json +3 -1
- package/gs/go/scanner/index.test.ts +50 -0
- package/gs/go/scanner/index.ts +157 -0
- package/gs/go/token/index.test.ts +21 -0
- package/gs/go/token/index.ts +120 -0
- package/gs/os/file_unix_js.test.ts +103 -0
- package/gs/os/meta.json +1 -2
- package/gs/os/tempfile.gs.test.ts +85 -0
- package/gs/os/tempfile.gs.ts +71 -11
- package/gs/os/types_js.gs.ts +74 -153
- package/gs/reflect/index.ts +1 -1
- package/gs/reflect/type.ts +106 -17
- package/gs/reflect/typefor.test.ts +75 -0
- package/gs/sync/sync.test.ts +24 -0
- package/gs/sync/sync.ts +12 -0
- package/package.json +13 -13
- package/compiler/analysis.go +0 -3475
- package/compiler/analysis_test.go +0 -338
- package/compiler/assignment.go +0 -580
- package/compiler/builtin_test.go +0 -92
- package/compiler/code-writer.go +0 -115
- package/compiler/compiler_test.go +0 -149
- package/compiler/composite-lit.go +0 -779
- package/compiler/config_test.go +0 -62
- package/compiler/constraint.go +0 -86
- package/compiler/decl.go +0 -801
- package/compiler/expr-call-async.go +0 -188
- package/compiler/expr-call-builtins.go +0 -208
- package/compiler/expr-call-helpers.go +0 -382
- package/compiler/expr-call-make.go +0 -318
- package/compiler/expr-call-type-conversion.go +0 -520
- package/compiler/expr-call.go +0 -413
- package/compiler/expr-selector.go +0 -343
- package/compiler/expr-star.go +0 -82
- package/compiler/expr-type.go +0 -442
- package/compiler/expr-value.go +0 -89
- package/compiler/expr.go +0 -773
- package/compiler/field.go +0 -183
- package/compiler/gs_dependencies_test.go +0 -298
- package/compiler/lit.go +0 -322
- package/compiler/output.go +0 -72
- package/compiler/primitive.go +0 -149
- package/compiler/protobuf.go +0 -697
- package/compiler/sanitize.go +0 -100
- package/compiler/spec-struct.go +0 -995
- package/compiler/spec-value.go +0 -540
- package/compiler/spec.go +0 -725
- package/compiler/stmt-assign.go +0 -664
- package/compiler/stmt-for.go +0 -266
- package/compiler/stmt-range.go +0 -475
- package/compiler/stmt-select.go +0 -262
- package/compiler/stmt-type-switch.go +0 -147
- package/compiler/stmt.go +0 -1308
- package/compiler/type-assert.go +0 -386
- package/compiler/type-info.go +0 -156
- package/compiler/type-utils.go +0 -207
- package/compiler/type.go +0 -892
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
package compiler
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"cmp"
|
|
5
|
+
"slices"
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
// RuntimeHelperCategory names a runtime helper family owned by the contract.
|
|
9
|
+
type RuntimeHelperCategory string
|
|
10
|
+
|
|
11
|
+
const (
|
|
12
|
+
RuntimeHelperCategoryBuiltin RuntimeHelperCategory = "builtin"
|
|
13
|
+
RuntimeHelperCategoryValue RuntimeHelperCategory = "value"
|
|
14
|
+
RuntimeHelperCategoryVarRef RuntimeHelperCategory = "varref"
|
|
15
|
+
RuntimeHelperCategorySlice RuntimeHelperCategory = "slice"
|
|
16
|
+
RuntimeHelperCategoryMap RuntimeHelperCategory = "map"
|
|
17
|
+
RuntimeHelperCategoryError RuntimeHelperCategory = "error"
|
|
18
|
+
RuntimeHelperCategoryType RuntimeHelperCategory = "type"
|
|
19
|
+
RuntimeHelperCategoryChannel RuntimeHelperCategory = "channel"
|
|
20
|
+
RuntimeHelperCategoryDefer RuntimeHelperCategory = "defer"
|
|
21
|
+
RuntimeHelperCategoryHost RuntimeHelperCategory = "host"
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
// RuntimeHelper identifies one compiler-visible helper exported by @goscript/builtin.
|
|
25
|
+
type RuntimeHelper string
|
|
26
|
+
|
|
27
|
+
const (
|
|
28
|
+
RuntimeHelperPrintln RuntimeHelper = "builtin.println"
|
|
29
|
+
RuntimeHelperPrint RuntimeHelper = "builtin.print"
|
|
30
|
+
RuntimeHelperInt RuntimeHelper = "builtin.int"
|
|
31
|
+
RuntimeHelperByte RuntimeHelper = "builtin.byte"
|
|
32
|
+
RuntimeHelperLen RuntimeHelper = "builtin.len"
|
|
33
|
+
RuntimeHelperCap RuntimeHelper = "builtin.cap"
|
|
34
|
+
RuntimeHelperClear RuntimeHelper = "builtin.clear"
|
|
35
|
+
RuntimeHelperPanic RuntimeHelper = "builtin.panic"
|
|
36
|
+
RuntimeHelperRecover RuntimeHelper = "builtin.recover"
|
|
37
|
+
RuntimeHelperMin RuntimeHelper = "builtin.min"
|
|
38
|
+
RuntimeHelperMax RuntimeHelper = "builtin.max"
|
|
39
|
+
|
|
40
|
+
RuntimeHelperAssignStruct RuntimeHelper = "value.assignStruct"
|
|
41
|
+
RuntimeHelperMarkAsStructValue RuntimeHelper = "value.markAsStructValue"
|
|
42
|
+
RuntimeHelperPointerValue RuntimeHelper = "value.pointerValue"
|
|
43
|
+
|
|
44
|
+
RuntimeHelperVarRef RuntimeHelper = "varref.varRef"
|
|
45
|
+
RuntimeHelperUnref RuntimeHelper = "varref.unref"
|
|
46
|
+
RuntimeHelperIsVarRef RuntimeHelper = "varref.isVarRef"
|
|
47
|
+
|
|
48
|
+
RuntimeHelperMakeSlice RuntimeHelper = "slice.makeSlice"
|
|
49
|
+
RuntimeHelperGoSlice RuntimeHelper = "slice.goSlice"
|
|
50
|
+
RuntimeHelperArrayToSlice RuntimeHelper = "slice.arrayToSlice"
|
|
51
|
+
RuntimeHelperAppend RuntimeHelper = "slice.append"
|
|
52
|
+
RuntimeHelperCopy RuntimeHelper = "slice.copy"
|
|
53
|
+
RuntimeHelperAsArray RuntimeHelper = "slice.asArray"
|
|
54
|
+
RuntimeHelperStringToRunes RuntimeHelper = "slice.stringToRunes"
|
|
55
|
+
RuntimeHelperStringToRune RuntimeHelper = "slice.stringToRune"
|
|
56
|
+
RuntimeHelperRunesToString RuntimeHelper = "slice.runesToString"
|
|
57
|
+
RuntimeHelperStringToBytes RuntimeHelper = "slice.stringToBytes"
|
|
58
|
+
RuntimeHelperBytesToString RuntimeHelper = "slice.bytesToString"
|
|
59
|
+
RuntimeHelperGenericBytesOrStringToString RuntimeHelper = "slice.genericBytesOrStringToString"
|
|
60
|
+
RuntimeHelperIndexStringOrBytes RuntimeHelper = "slice.indexStringOrBytes"
|
|
61
|
+
RuntimeHelperSliceStringOrBytes RuntimeHelper = "slice.sliceStringOrBytes"
|
|
62
|
+
|
|
63
|
+
RuntimeHelperMakeMap RuntimeHelper = "map.makeMap"
|
|
64
|
+
RuntimeHelperMapGet RuntimeHelper = "map.mapGet"
|
|
65
|
+
RuntimeHelperMapSet RuntimeHelper = "map.mapSet"
|
|
66
|
+
RuntimeHelperMapHas RuntimeHelper = "map.mapHas"
|
|
67
|
+
RuntimeHelperDeleteMapEntry RuntimeHelper = "map.deleteMapEntry"
|
|
68
|
+
|
|
69
|
+
RuntimeHelperNewError RuntimeHelper = "error.newError"
|
|
70
|
+
RuntimeHelperToGoError RuntimeHelper = "error.toGoError"
|
|
71
|
+
RuntimeHelperWrapPrimitiveError RuntimeHelper = "error.wrapPrimitiveError"
|
|
72
|
+
|
|
73
|
+
RuntimeHelperTypeKind RuntimeHelper = "type.TypeKind"
|
|
74
|
+
RuntimeHelperRegisterStructType RuntimeHelper = "type.registerStructType"
|
|
75
|
+
RuntimeHelperRegisterInterfaceType RuntimeHelper = "type.registerInterfaceType"
|
|
76
|
+
RuntimeHelperGetTypeByName RuntimeHelper = "type.getTypeByName"
|
|
77
|
+
RuntimeHelperTypeAssert RuntimeHelper = "type.typeAssert"
|
|
78
|
+
RuntimeHelperTypeAssertTuple RuntimeHelper = "type.typeAssertTuple"
|
|
79
|
+
RuntimeHelperMustTypeAssert RuntimeHelper = "type.mustTypeAssert"
|
|
80
|
+
RuntimeHelperIs RuntimeHelper = "type.is"
|
|
81
|
+
RuntimeHelperTypeSwitch RuntimeHelper = "type.typeSwitch"
|
|
82
|
+
RuntimeHelperTypedNil RuntimeHelper = "type.typedNil"
|
|
83
|
+
RuntimeHelperInterfaceValue RuntimeHelper = "type.interfaceValue"
|
|
84
|
+
RuntimeHelperFunctionValue RuntimeHelper = "type.functionValue"
|
|
85
|
+
RuntimeHelperNamedFunction RuntimeHelper = "type.namedFunction"
|
|
86
|
+
RuntimeHelperGenericZero RuntimeHelper = "type.genericZero"
|
|
87
|
+
RuntimeHelperCallGenericMethod RuntimeHelper = "type.callGenericMethod"
|
|
88
|
+
|
|
89
|
+
RuntimeHelperMakeChannel RuntimeHelper = "channel.makeChannel"
|
|
90
|
+
RuntimeHelperMakeChannelRef RuntimeHelper = "channel.makeChannelRef"
|
|
91
|
+
RuntimeHelperChanSend RuntimeHelper = "channel.chanSend"
|
|
92
|
+
RuntimeHelperChanRecv RuntimeHelper = "channel.chanRecv"
|
|
93
|
+
RuntimeHelperChanRecvWithOk RuntimeHelper = "channel.chanRecvWithOk"
|
|
94
|
+
RuntimeHelperSelectStatement RuntimeHelper = "channel.selectStatement"
|
|
95
|
+
|
|
96
|
+
RuntimeHelperDisposableStack RuntimeHelper = "defer.DisposableStack"
|
|
97
|
+
RuntimeHelperAsyncDisposableStack RuntimeHelper = "defer.AsyncDisposableStack"
|
|
98
|
+
|
|
99
|
+
RuntimeHelperGetHostRuntime RuntimeHelper = "host.getHostRuntime"
|
|
100
|
+
RuntimeHelperWriteHostStdoutText RuntimeHelper = "host.writeHostStdoutText"
|
|
101
|
+
RuntimeHelperWriteHostStderrText RuntimeHelper = "host.writeHostStderrText"
|
|
102
|
+
RuntimeHelperIsMainScript RuntimeHelper = "host.isMainScript"
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
// RuntimeImport is a generated TypeScript import owned by the runtime contract.
|
|
106
|
+
type RuntimeImport struct {
|
|
107
|
+
Alias string
|
|
108
|
+
Source string
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// RuntimeHelperContract describes one helper exported by the runtime package.
|
|
112
|
+
type RuntimeHelperContract struct {
|
|
113
|
+
Helper RuntimeHelper
|
|
114
|
+
Export string
|
|
115
|
+
Category RuntimeHelperCategory
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// RuntimeContractOwner owns generated-code helper names and runtime capabilities.
|
|
119
|
+
type RuntimeContractOwner struct {
|
|
120
|
+
helpers map[RuntimeHelper]RuntimeHelperContract
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// NewRuntimeContractOwner creates the runtime contract owner.
|
|
124
|
+
func NewRuntimeContractOwner() *RuntimeContractOwner {
|
|
125
|
+
owner := &RuntimeContractOwner{
|
|
126
|
+
helpers: make(map[RuntimeHelper]RuntimeHelperContract),
|
|
127
|
+
}
|
|
128
|
+
for _, helper := range runtimeHelperContracts() {
|
|
129
|
+
owner.helpers[helper.Helper] = helper
|
|
130
|
+
}
|
|
131
|
+
return owner
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// BuiltinImport returns the runtime import used by generated package modules.
|
|
135
|
+
func (o *RuntimeContractOwner) BuiltinImport() RuntimeImport {
|
|
136
|
+
return RuntimeImport{
|
|
137
|
+
Alias: "$",
|
|
138
|
+
Source: "@goscript/builtin/index.js",
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Helpers returns every compiler-visible runtime helper contract.
|
|
143
|
+
func (o *RuntimeContractOwner) Helpers() []RuntimeHelperContract {
|
|
144
|
+
helpers := make([]RuntimeHelperContract, 0, len(o.helpers))
|
|
145
|
+
for _, helper := range o.helpers {
|
|
146
|
+
helpers = append(helpers, helper)
|
|
147
|
+
}
|
|
148
|
+
slices.SortFunc(helpers, compareRuntimeHelperContract)
|
|
149
|
+
return helpers
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// HelpersByCategory returns helper contracts in a category.
|
|
153
|
+
func (o *RuntimeContractOwner) HelpersByCategory(category RuntimeHelperCategory) []RuntimeHelperContract {
|
|
154
|
+
var helpers []RuntimeHelperContract
|
|
155
|
+
for _, helper := range o.helpers {
|
|
156
|
+
if helper.Category == category {
|
|
157
|
+
helpers = append(helpers, helper)
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
slices.SortFunc(helpers, compareRuntimeHelperContract)
|
|
161
|
+
return helpers
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Helper returns one runtime helper contract.
|
|
165
|
+
func (o *RuntimeContractOwner) Helper(helper RuntimeHelper) (RuntimeHelperContract, bool) {
|
|
166
|
+
contract, ok := o.helpers[helper]
|
|
167
|
+
return contract, ok
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// HelperName returns the exported runtime symbol for a helper.
|
|
171
|
+
func (o *RuntimeContractOwner) HelperName(helper RuntimeHelper) (string, bool) {
|
|
172
|
+
contract, ok := o.Helper(helper)
|
|
173
|
+
if !ok {
|
|
174
|
+
return "", false
|
|
175
|
+
}
|
|
176
|
+
return contract.Export, true
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// QualifiedHelper returns a generated-code reference to a runtime helper.
|
|
180
|
+
func (o *RuntimeContractOwner) QualifiedHelper(helper RuntimeHelper) string {
|
|
181
|
+
name, ok := o.HelperName(helper)
|
|
182
|
+
if !ok {
|
|
183
|
+
panic("missing runtime helper contract: " + string(helper))
|
|
184
|
+
}
|
|
185
|
+
return o.BuiltinImport().Alias + "." + name
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
func compareRuntimeHelperContract(a RuntimeHelperContract, b RuntimeHelperContract) int {
|
|
189
|
+
if c := cmp.Compare(a.Category, b.Category); c != 0 {
|
|
190
|
+
return c
|
|
191
|
+
}
|
|
192
|
+
if c := cmp.Compare(a.Export, b.Export); c != 0 {
|
|
193
|
+
return c
|
|
194
|
+
}
|
|
195
|
+
return cmp.Compare(a.Helper, b.Helper)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
func runtimeHelperContracts() []RuntimeHelperContract {
|
|
199
|
+
return []RuntimeHelperContract{
|
|
200
|
+
runtimeHelper(RuntimeHelperPrintln, "println", RuntimeHelperCategoryBuiltin),
|
|
201
|
+
runtimeHelper(RuntimeHelperPrint, "print", RuntimeHelperCategoryBuiltin),
|
|
202
|
+
runtimeHelper(RuntimeHelperInt, "int", RuntimeHelperCategoryBuiltin),
|
|
203
|
+
runtimeHelper(RuntimeHelperByte, "byte", RuntimeHelperCategoryBuiltin),
|
|
204
|
+
runtimeHelper(RuntimeHelperLen, "len", RuntimeHelperCategoryBuiltin),
|
|
205
|
+
runtimeHelper(RuntimeHelperCap, "cap", RuntimeHelperCategoryBuiltin),
|
|
206
|
+
runtimeHelper(RuntimeHelperClear, "clear", RuntimeHelperCategoryBuiltin),
|
|
207
|
+
runtimeHelper(RuntimeHelperPanic, "panic", RuntimeHelperCategoryBuiltin),
|
|
208
|
+
runtimeHelper(RuntimeHelperRecover, "recover", RuntimeHelperCategoryBuiltin),
|
|
209
|
+
runtimeHelper(RuntimeHelperMin, "min", RuntimeHelperCategoryBuiltin),
|
|
210
|
+
runtimeHelper(RuntimeHelperMax, "max", RuntimeHelperCategoryBuiltin),
|
|
211
|
+
runtimeHelper(RuntimeHelperAssignStruct, "assignStruct", RuntimeHelperCategoryValue),
|
|
212
|
+
runtimeHelper(RuntimeHelperMarkAsStructValue, "markAsStructValue", RuntimeHelperCategoryValue),
|
|
213
|
+
runtimeHelper(RuntimeHelperPointerValue, "pointerValue", RuntimeHelperCategoryValue),
|
|
214
|
+
runtimeHelper(RuntimeHelperVarRef, "varRef", RuntimeHelperCategoryVarRef),
|
|
215
|
+
runtimeHelper(RuntimeHelperUnref, "unref", RuntimeHelperCategoryVarRef),
|
|
216
|
+
runtimeHelper(RuntimeHelperIsVarRef, "isVarRef", RuntimeHelperCategoryVarRef),
|
|
217
|
+
runtimeHelper(RuntimeHelperMakeSlice, "makeSlice", RuntimeHelperCategorySlice),
|
|
218
|
+
runtimeHelper(RuntimeHelperGoSlice, "goSlice", RuntimeHelperCategorySlice),
|
|
219
|
+
runtimeHelper(RuntimeHelperArrayToSlice, "arrayToSlice", RuntimeHelperCategorySlice),
|
|
220
|
+
runtimeHelper(RuntimeHelperAppend, "append", RuntimeHelperCategorySlice),
|
|
221
|
+
runtimeHelper(RuntimeHelperCopy, "copy", RuntimeHelperCategorySlice),
|
|
222
|
+
runtimeHelper(RuntimeHelperAsArray, "asArray", RuntimeHelperCategorySlice),
|
|
223
|
+
runtimeHelper(RuntimeHelperStringToRunes, "stringToRunes", RuntimeHelperCategorySlice),
|
|
224
|
+
runtimeHelper(RuntimeHelperStringToRune, "stringToRune", RuntimeHelperCategorySlice),
|
|
225
|
+
runtimeHelper(RuntimeHelperRunesToString, "runesToString", RuntimeHelperCategorySlice),
|
|
226
|
+
runtimeHelper(RuntimeHelperStringToBytes, "stringToBytes", RuntimeHelperCategorySlice),
|
|
227
|
+
runtimeHelper(RuntimeHelperBytesToString, "bytesToString", RuntimeHelperCategorySlice),
|
|
228
|
+
runtimeHelper(RuntimeHelperGenericBytesOrStringToString, "genericBytesOrStringToString", RuntimeHelperCategorySlice),
|
|
229
|
+
runtimeHelper(RuntimeHelperIndexStringOrBytes, "indexStringOrBytes", RuntimeHelperCategorySlice),
|
|
230
|
+
runtimeHelper(RuntimeHelperSliceStringOrBytes, "sliceStringOrBytes", RuntimeHelperCategorySlice),
|
|
231
|
+
runtimeHelper(RuntimeHelperMakeMap, "makeMap", RuntimeHelperCategoryMap),
|
|
232
|
+
runtimeHelper(RuntimeHelperMapGet, "mapGet", RuntimeHelperCategoryMap),
|
|
233
|
+
runtimeHelper(RuntimeHelperMapSet, "mapSet", RuntimeHelperCategoryMap),
|
|
234
|
+
runtimeHelper(RuntimeHelperMapHas, "mapHas", RuntimeHelperCategoryMap),
|
|
235
|
+
runtimeHelper(RuntimeHelperDeleteMapEntry, "deleteMapEntry", RuntimeHelperCategoryMap),
|
|
236
|
+
runtimeHelper(RuntimeHelperNewError, "newError", RuntimeHelperCategoryError),
|
|
237
|
+
runtimeHelper(RuntimeHelperToGoError, "toGoError", RuntimeHelperCategoryError),
|
|
238
|
+
runtimeHelper(RuntimeHelperWrapPrimitiveError, "wrapPrimitiveError", RuntimeHelperCategoryError),
|
|
239
|
+
runtimeHelper(RuntimeHelperTypeKind, "TypeKind", RuntimeHelperCategoryType),
|
|
240
|
+
runtimeHelper(RuntimeHelperRegisterStructType, "registerStructType", RuntimeHelperCategoryType),
|
|
241
|
+
runtimeHelper(RuntimeHelperRegisterInterfaceType, "registerInterfaceType", RuntimeHelperCategoryType),
|
|
242
|
+
runtimeHelper(RuntimeHelperGetTypeByName, "getTypeByName", RuntimeHelperCategoryType),
|
|
243
|
+
runtimeHelper(RuntimeHelperTypeAssert, "typeAssert", RuntimeHelperCategoryType),
|
|
244
|
+
runtimeHelper(RuntimeHelperTypeAssertTuple, "typeAssertTuple", RuntimeHelperCategoryType),
|
|
245
|
+
runtimeHelper(RuntimeHelperMustTypeAssert, "mustTypeAssert", RuntimeHelperCategoryType),
|
|
246
|
+
runtimeHelper(RuntimeHelperIs, "is", RuntimeHelperCategoryType),
|
|
247
|
+
runtimeHelper(RuntimeHelperTypeSwitch, "typeSwitch", RuntimeHelperCategoryType),
|
|
248
|
+
runtimeHelper(RuntimeHelperTypedNil, "typedNil", RuntimeHelperCategoryType),
|
|
249
|
+
runtimeHelper(RuntimeHelperInterfaceValue, "interfaceValue", RuntimeHelperCategoryType),
|
|
250
|
+
runtimeHelper(RuntimeHelperFunctionValue, "functionValue", RuntimeHelperCategoryType),
|
|
251
|
+
runtimeHelper(RuntimeHelperNamedFunction, "namedFunction", RuntimeHelperCategoryType),
|
|
252
|
+
runtimeHelper(RuntimeHelperGenericZero, "genericZero", RuntimeHelperCategoryType),
|
|
253
|
+
runtimeHelper(RuntimeHelperCallGenericMethod, "callGenericMethod", RuntimeHelperCategoryType),
|
|
254
|
+
runtimeHelper(RuntimeHelperMakeChannel, "makeChannel", RuntimeHelperCategoryChannel),
|
|
255
|
+
runtimeHelper(RuntimeHelperMakeChannelRef, "makeChannelRef", RuntimeHelperCategoryChannel),
|
|
256
|
+
runtimeHelper(RuntimeHelperChanSend, "chanSend", RuntimeHelperCategoryChannel),
|
|
257
|
+
runtimeHelper(RuntimeHelperChanRecv, "chanRecv", RuntimeHelperCategoryChannel),
|
|
258
|
+
runtimeHelper(RuntimeHelperChanRecvWithOk, "chanRecvWithOk", RuntimeHelperCategoryChannel),
|
|
259
|
+
runtimeHelper(RuntimeHelperSelectStatement, "selectStatement", RuntimeHelperCategoryChannel),
|
|
260
|
+
runtimeHelper(RuntimeHelperDisposableStack, "DisposableStack", RuntimeHelperCategoryDefer),
|
|
261
|
+
runtimeHelper(RuntimeHelperAsyncDisposableStack, "AsyncDisposableStack", RuntimeHelperCategoryDefer),
|
|
262
|
+
runtimeHelper(RuntimeHelperGetHostRuntime, "getHostRuntime", RuntimeHelperCategoryHost),
|
|
263
|
+
runtimeHelper(RuntimeHelperWriteHostStdoutText, "writeHostStdoutText", RuntimeHelperCategoryHost),
|
|
264
|
+
runtimeHelper(RuntimeHelperWriteHostStderrText, "writeHostStderrText", RuntimeHelperCategoryHost),
|
|
265
|
+
runtimeHelper(RuntimeHelperIsMainScript, "isMainScript", RuntimeHelperCategoryHost),
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
func runtimeHelper(
|
|
270
|
+
helper RuntimeHelper,
|
|
271
|
+
export string,
|
|
272
|
+
category RuntimeHelperCategory,
|
|
273
|
+
) RuntimeHelperContract {
|
|
274
|
+
return RuntimeHelperContract{
|
|
275
|
+
Helper: helper,
|
|
276
|
+
Export: export,
|
|
277
|
+
Category: category,
|
|
278
|
+
}
|
|
279
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
package compiler
|
|
2
|
+
|
|
3
|
+
import "testing"
|
|
4
|
+
|
|
5
|
+
func TestRuntimeContractOwnsBuiltinImportAndHelpers(t *testing.T) {
|
|
6
|
+
owner := NewRuntimeContractOwner()
|
|
7
|
+
|
|
8
|
+
runtimeImport := owner.BuiltinImport()
|
|
9
|
+
if runtimeImport.Alias != "$" || runtimeImport.Source != "@goscript/builtin/index.js" {
|
|
10
|
+
t.Fatalf("unexpected builtin import: %#v", runtimeImport)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
wantHelpers := map[RuntimeHelper]RuntimeHelperCategory{
|
|
14
|
+
RuntimeHelperPrintln: RuntimeHelperCategoryBuiltin,
|
|
15
|
+
RuntimeHelperPrint: RuntimeHelperCategoryBuiltin,
|
|
16
|
+
RuntimeHelperInt: RuntimeHelperCategoryBuiltin,
|
|
17
|
+
RuntimeHelperByte: RuntimeHelperCategoryBuiltin,
|
|
18
|
+
RuntimeHelperVarRef: RuntimeHelperCategoryVarRef,
|
|
19
|
+
RuntimeHelperUnref: RuntimeHelperCategoryVarRef,
|
|
20
|
+
RuntimeHelperAssignStruct: RuntimeHelperCategoryValue,
|
|
21
|
+
RuntimeHelperMarkAsStructValue: RuntimeHelperCategoryValue,
|
|
22
|
+
RuntimeHelperPointerValue: RuntimeHelperCategoryValue,
|
|
23
|
+
RuntimeHelperMakeSlice: RuntimeHelperCategorySlice,
|
|
24
|
+
RuntimeHelperAppend: RuntimeHelperCategorySlice,
|
|
25
|
+
RuntimeHelperMakeMap: RuntimeHelperCategoryMap,
|
|
26
|
+
RuntimeHelperMapGet: RuntimeHelperCategoryMap,
|
|
27
|
+
RuntimeHelperNewError: RuntimeHelperCategoryError,
|
|
28
|
+
RuntimeHelperRegisterStructType: RuntimeHelperCategoryType,
|
|
29
|
+
RuntimeHelperTypeAssert: RuntimeHelperCategoryType,
|
|
30
|
+
RuntimeHelperTypedNil: RuntimeHelperCategoryType,
|
|
31
|
+
RuntimeHelperInterfaceValue: RuntimeHelperCategoryType,
|
|
32
|
+
RuntimeHelperFunctionValue: RuntimeHelperCategoryType,
|
|
33
|
+
RuntimeHelperNamedFunction: RuntimeHelperCategoryType,
|
|
34
|
+
RuntimeHelperGenericZero: RuntimeHelperCategoryType,
|
|
35
|
+
RuntimeHelperCallGenericMethod: RuntimeHelperCategoryType,
|
|
36
|
+
RuntimeHelperMakeChannel: RuntimeHelperCategoryChannel,
|
|
37
|
+
RuntimeHelperSelectStatement: RuntimeHelperCategoryChannel,
|
|
38
|
+
RuntimeHelperDisposableStack: RuntimeHelperCategoryDefer,
|
|
39
|
+
RuntimeHelperAsyncDisposableStack: RuntimeHelperCategoryDefer,
|
|
40
|
+
RuntimeHelperGetHostRuntime: RuntimeHelperCategoryHost,
|
|
41
|
+
RuntimeHelperWriteHostStdoutText: RuntimeHelperCategoryHost,
|
|
42
|
+
RuntimeHelperWriteHostStderrText: RuntimeHelperCategoryHost,
|
|
43
|
+
RuntimeHelperIsMainScript: RuntimeHelperCategoryHost,
|
|
44
|
+
}
|
|
45
|
+
for helper, category := range wantHelpers {
|
|
46
|
+
contract, ok := owner.Helper(helper)
|
|
47
|
+
if !ok {
|
|
48
|
+
t.Fatalf("missing runtime helper %q", helper)
|
|
49
|
+
}
|
|
50
|
+
if contract.Category != category {
|
|
51
|
+
t.Fatalf("helper %q category = %q, want %q", helper, contract.Category, category)
|
|
52
|
+
}
|
|
53
|
+
if contract.Export == "" {
|
|
54
|
+
t.Fatalf("helper %q has empty export name", helper)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
func TestRuntimeContractCoversPhaseFiveCategories(t *testing.T) {
|
|
60
|
+
owner := NewRuntimeContractOwner()
|
|
61
|
+
for _, category := range []RuntimeHelperCategory{
|
|
62
|
+
RuntimeHelperCategoryBuiltin,
|
|
63
|
+
RuntimeHelperCategoryValue,
|
|
64
|
+
RuntimeHelperCategoryVarRef,
|
|
65
|
+
RuntimeHelperCategorySlice,
|
|
66
|
+
RuntimeHelperCategoryMap,
|
|
67
|
+
RuntimeHelperCategoryError,
|
|
68
|
+
RuntimeHelperCategoryType,
|
|
69
|
+
RuntimeHelperCategoryChannel,
|
|
70
|
+
RuntimeHelperCategoryDefer,
|
|
71
|
+
RuntimeHelperCategoryHost,
|
|
72
|
+
} {
|
|
73
|
+
if len(owner.HelpersByCategory(category)) == 0 {
|
|
74
|
+
t.Fatalf("runtime helper category %q has no helpers", category)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
func TestCompileServiceSharesRuntimeContractOwner(t *testing.T) {
|
|
80
|
+
service := NewCompileService()
|
|
81
|
+
if service.LoweringOwner().runtimeOwner != service.RuntimeContractOwner() {
|
|
82
|
+
t.Fatalf("lowering owner does not share the compile service runtime contract")
|
|
83
|
+
}
|
|
84
|
+
if service.TypeScriptEmitOwner().runtimeOwner != service.RuntimeContractOwner() {
|
|
85
|
+
t.Fatalf("emitter owner does not share the compile service runtime contract")
|
|
86
|
+
}
|
|
87
|
+
if got := service.RuntimeContractOwner().QualifiedHelper(RuntimeHelperPrintln); got != "$.println" {
|
|
88
|
+
t.Fatalf("qualified println helper = %q, want $.println", got)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
package compiler
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"go/types"
|
|
5
|
+
|
|
6
|
+
"golang.org/x/tools/go/packages"
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
// SemanticModel is the immutable compiler semantic surface consumed by lowering.
|
|
10
|
+
type SemanticModel struct {
|
|
11
|
+
packages map[string]*semanticPackage
|
|
12
|
+
addressTaken map[types.Object]bool
|
|
13
|
+
needsVarRef map[types.Object]bool
|
|
14
|
+
functions map[*types.Func]*semanticFunction
|
|
15
|
+
types map[*types.Named]*semanticType
|
|
16
|
+
values map[types.Object]*semanticValue
|
|
17
|
+
generatedImports map[string]map[string]bool
|
|
18
|
+
interfaceImplementations []semanticInterfaceImplementation
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type semanticPackage struct {
|
|
22
|
+
pkgPath string
|
|
23
|
+
name string
|
|
24
|
+
source *packages.Package
|
|
25
|
+
declarations []semanticDeclaration
|
|
26
|
+
imports []semanticImport
|
|
27
|
+
types []*semanticType
|
|
28
|
+
values []*semanticValue
|
|
29
|
+
functions []*semanticFunction
|
|
30
|
+
initOrder []types.Object
|
|
31
|
+
generatedImports map[string]map[string]bool
|
|
32
|
+
typeAssertions []semanticTypeAssertion
|
|
33
|
+
nilFacts []semanticNilFact
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type semanticDeclaration struct {
|
|
37
|
+
kind string
|
|
38
|
+
name string
|
|
39
|
+
object types.Object
|
|
40
|
+
position sourcePosition
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
type semanticImport struct {
|
|
44
|
+
path string
|
|
45
|
+
name string
|
|
46
|
+
file string
|
|
47
|
+
position sourcePosition
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
type semanticType struct {
|
|
51
|
+
name string
|
|
52
|
+
named *types.Named
|
|
53
|
+
isInterface bool
|
|
54
|
+
fields []semanticField
|
|
55
|
+
position sourcePosition
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
type semanticField struct {
|
|
59
|
+
name string
|
|
60
|
+
typ types.Type
|
|
61
|
+
doc string
|
|
62
|
+
tag string
|
|
63
|
+
embedded bool
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
type semanticValue struct {
|
|
67
|
+
name string
|
|
68
|
+
object types.Object
|
|
69
|
+
typ types.Type
|
|
70
|
+
zeroValueKind string
|
|
71
|
+
position sourcePosition
|
|
72
|
+
topLevel bool
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
type semanticFunction struct {
|
|
76
|
+
name string
|
|
77
|
+
function *types.Func
|
|
78
|
+
signature *types.Signature
|
|
79
|
+
receiver *types.Named
|
|
80
|
+
receiverPointer bool
|
|
81
|
+
position sourcePosition
|
|
82
|
+
async bool
|
|
83
|
+
asyncReasons []string
|
|
84
|
+
calls map[*types.Func]bool
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
type semanticInterfaceImplementation struct {
|
|
88
|
+
typ *types.Named
|
|
89
|
+
iface *types.Named
|
|
90
|
+
pointer bool
|
|
91
|
+
asyncMethods map[string]bool
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
type semanticTypeAssertion struct {
|
|
95
|
+
position sourcePosition
|
|
96
|
+
source types.Type
|
|
97
|
+
target types.Type
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
type semanticNilFact struct {
|
|
101
|
+
position sourcePosition
|
|
102
|
+
kind string
|
|
103
|
+
typ types.Type
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
type sourcePosition struct {
|
|
107
|
+
file string
|
|
108
|
+
line int
|
|
109
|
+
column int
|
|
110
|
+
}
|