goscript 0.0.51 → 0.0.53
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/compiler/analysis.go +633 -340
- package/compiler/compiler.go +35 -6
- package/compiler/expr-call-async.go +82 -209
- package/compiler/expr.go +0 -44
- package/compiler/stmt-assign.go +0 -6
- package/compiler/stmt-select.go +5 -5
- package/compiler/type-assert.go +6 -6
- package/dist/gs/builtin/builtin.d.ts +0 -1
- package/dist/gs/builtin/builtin.js +0 -9
- package/dist/gs/builtin/builtin.js.map +1 -1
- package/dist/gs/builtin/channel.d.ts +3 -1
- package/dist/gs/builtin/channel.js +2 -10
- package/dist/gs/builtin/channel.js.map +1 -1
- package/dist/gs/context/context.js +2 -2
- package/dist/gs/context/context.js.map +1 -1
- package/dist/gs/reflect/index.d.ts +8 -8
- package/dist/gs/reflect/index.js +6 -6
- package/dist/gs/reflect/index.js.map +1 -1
- package/dist/gs/reflect/iter.d.ts +1 -1
- package/dist/gs/reflect/iter.js +1 -1
- package/dist/gs/reflect/iter.js.map +1 -1
- package/dist/gs/reflect/swapper.d.ts +1 -1
- package/dist/gs/reflect/value.d.ts +1 -2
- package/dist/gs/reflect/value.js +1 -3
- package/dist/gs/reflect/value.js.map +1 -1
- package/dist/gs/runtime/runtime.d.ts +1 -1
- package/dist/gs/runtime/runtime.js +1 -1
- package/dist/gs/sort/index.d.ts +4 -4
- package/dist/gs/sort/index.js +3 -3
- package/dist/gs/sort/index.js.map +1 -1
- package/dist/gs/strings/index.js +0 -1
- package/dist/gs/strings/index.js.map +1 -1
- package/dist/gs/time/time.d.ts +2 -2
- package/dist/gs/time/time.js +12 -9
- package/dist/gs/time/time.js.map +1 -1
- package/go.mod +4 -4
- package/go.sum +6 -6
- package/gs/builtin/builtin.ts +0 -11
- package/gs/builtin/channel.ts +23 -7
- package/gs/context/context.ts +6 -2
- package/gs/context/meta.json +16 -0
- package/gs/reflect/index.ts +8 -8
- package/gs/reflect/iter.ts +1 -11
- package/gs/reflect/swapper.ts +1 -1
- package/gs/reflect/value.ts +1 -4
- package/gs/runtime/runtime.ts +1 -1
- package/gs/sort/index.ts +4 -4
- package/gs/strings/index.ts +0 -1
- package/gs/syscall/constants.ts +1 -1
- package/gs/syscall/env.ts +1 -1
- package/gs/syscall/errors.ts +1 -1
- package/gs/syscall/fs.ts +1 -1
- package/gs/syscall/rawconn.ts +1 -1
- package/gs/syscall/types.ts +1 -1
- package/gs/time/meta.json +6 -0
- package/gs/time/time.ts +16 -13
- package/gs/unicode/meta.json +24 -0
- package/package.json +1 -1
- package/gs/unicode/unicode.go +0 -38
package/compiler/compiler.go
CHANGED
|
@@ -2,6 +2,8 @@ package compiler
|
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
4
|
"context"
|
|
5
|
+
"crypto/sha256"
|
|
6
|
+
"encoding/hex"
|
|
5
7
|
"encoding/json"
|
|
6
8
|
"fmt"
|
|
7
9
|
"go/ast"
|
|
@@ -184,8 +186,6 @@ func (c *Compiler) CompilePackages(ctx context.Context, patterns ...string) (*Co
|
|
|
184
186
|
|
|
185
187
|
// Reload all collected packages with complete type information
|
|
186
188
|
if len(pkgPaths) > 0 {
|
|
187
|
-
c.le.Debugf("Reloading %d packages with complete type information", len(pkgPaths))
|
|
188
|
-
|
|
189
189
|
fullOpts := c.opts
|
|
190
190
|
fullOpts.Context = ctx
|
|
191
191
|
// Use LoadAllSyntax to get complete type information, syntax trees, and type checking
|
|
@@ -676,10 +676,6 @@ type GoToTSCompiler struct {
|
|
|
676
676
|
pkg *packages.Package
|
|
677
677
|
|
|
678
678
|
analysis *Analysis
|
|
679
|
-
|
|
680
|
-
// awaitedCalls tracks which call expressions have already been processed
|
|
681
|
-
// to avoid adding double await keywords
|
|
682
|
-
awaitedCalls map[*ast.CallExpr]bool
|
|
683
679
|
}
|
|
684
680
|
|
|
685
681
|
// It initializes the compiler with a `TSCodeWriter` for output,
|
|
@@ -693,6 +689,39 @@ func NewGoToTSCompiler(tsw *TSCodeWriter, pkg *packages.Package, analysis *Analy
|
|
|
693
689
|
}
|
|
694
690
|
}
|
|
695
691
|
|
|
692
|
+
// getDeterministicID generates a deterministic unique ID based on file position
|
|
693
|
+
// This replaces the non-deterministic Pos() values to ensure reproducible builds
|
|
694
|
+
func (c *GoToTSCompiler) getDeterministicID(pos token.Pos) string {
|
|
695
|
+
if !pos.IsValid() {
|
|
696
|
+
return "0000"
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
// Get file position information
|
|
700
|
+
position := c.pkg.Fset.Position(pos)
|
|
701
|
+
|
|
702
|
+
// Use package path + base filename + line + column for deterministic hashing
|
|
703
|
+
// This avoids absolute path differences between build environments
|
|
704
|
+
baseFilename := filepath.Base(position.Filename)
|
|
705
|
+
if baseFilename == "" {
|
|
706
|
+
baseFilename = "unknown"
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
packagePath := c.pkg.PkgPath
|
|
710
|
+
if packagePath == "" {
|
|
711
|
+
packagePath = "main"
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
// Create a string that uniquely identifies this position using only relative/stable info
|
|
715
|
+
positionStr := fmt.Sprintf("%s:%s:%d:%d", packagePath, baseFilename, position.Line, position.Column)
|
|
716
|
+
|
|
717
|
+
// Hash the position string with SHA256
|
|
718
|
+
hash := sha256.Sum256([]byte(positionStr))
|
|
719
|
+
|
|
720
|
+
// Convert to hex and take the last 4 characters (lowercase)
|
|
721
|
+
hexStr := hex.EncodeToString(hash[:])
|
|
722
|
+
return hexStr[len(hexStr)-4:]
|
|
723
|
+
}
|
|
724
|
+
|
|
696
725
|
// --- Exported Node-Specific Writers ---
|
|
697
726
|
|
|
698
727
|
// WriteIdent translates a Go identifier (`ast.Ident`) used as a value (e.g.,
|
|
@@ -2,7 +2,6 @@ package compiler
|
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
4
|
"go/ast"
|
|
5
|
-
"go/token"
|
|
6
5
|
"go/types"
|
|
7
6
|
"strings"
|
|
8
7
|
)
|
|
@@ -10,63 +9,105 @@ import (
|
|
|
10
9
|
// writeAsyncCallIfNeeded writes the await prefix for async function or method calls
|
|
11
10
|
// Returns true if await was written, false otherwise
|
|
12
11
|
func (c *GoToTSCompiler) writeAsyncCallIfNeeded(exp *ast.CallExpr) bool {
|
|
13
|
-
// Track if we've already processed this call expression to avoid double await
|
|
14
|
-
if c.awaitedCalls == nil {
|
|
15
|
-
c.awaitedCalls = make(map[*ast.CallExpr]bool)
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
if c.awaitedCalls[exp] {
|
|
19
|
-
return false // Already processed this call
|
|
20
|
-
}
|
|
21
12
|
switch fun := exp.Fun.(type) {
|
|
22
13
|
case *ast.Ident:
|
|
23
14
|
// Function call (e.g., func())
|
|
24
|
-
if obj := c.pkg.TypesInfo.Uses[fun]; obj != nil
|
|
25
|
-
c.
|
|
26
|
-
|
|
27
|
-
|
|
15
|
+
if obj := c.pkg.TypesInfo.Uses[fun]; obj != nil {
|
|
16
|
+
if c.analysis.IsAsyncFunc(obj) {
|
|
17
|
+
c.tsw.WriteLiterally("await ")
|
|
18
|
+
return true
|
|
19
|
+
}
|
|
28
20
|
}
|
|
29
21
|
return false
|
|
30
22
|
|
|
31
23
|
case *ast.SelectorExpr:
|
|
32
|
-
// Method call (e.g., obj.method())
|
|
33
|
-
var
|
|
34
|
-
var
|
|
24
|
+
// Method call (e.g., obj.method() or obj.field.method())
|
|
25
|
+
var obj types.Object
|
|
26
|
+
var objOk bool
|
|
35
27
|
|
|
36
|
-
// Handle
|
|
28
|
+
// Handle different patterns of method receiver
|
|
37
29
|
switch x := fun.X.(type) {
|
|
38
30
|
case *ast.Ident:
|
|
39
|
-
|
|
31
|
+
// Direct identifier: obj.method()
|
|
32
|
+
obj = c.pkg.TypesInfo.Uses[x]
|
|
33
|
+
objOk = obj != nil
|
|
34
|
+
|
|
40
35
|
case *ast.StarExpr:
|
|
41
|
-
//
|
|
36
|
+
// Pointer dereference: (*p).method() or p.method() where p is a pointer
|
|
42
37
|
if id, isIdent := x.X.(*ast.Ident); isIdent {
|
|
43
|
-
|
|
38
|
+
obj = c.pkg.TypesInfo.Uses[id]
|
|
39
|
+
objOk = obj != nil
|
|
44
40
|
}
|
|
41
|
+
|
|
42
|
+
case *ast.SelectorExpr:
|
|
43
|
+
// Field access: obj.field.method()
|
|
44
|
+
// Get the type of the field access expression
|
|
45
|
+
if fieldType := c.pkg.TypesInfo.TypeOf(x); fieldType != nil {
|
|
46
|
+
// For field access, we create a synthetic object representing the field type
|
|
47
|
+
// We'll handle this case below when we determine the method's type
|
|
48
|
+
objOk = true
|
|
49
|
+
}
|
|
50
|
+
|
|
45
51
|
default:
|
|
46
|
-
|
|
52
|
+
objOk = false
|
|
47
53
|
}
|
|
48
54
|
|
|
49
|
-
if !
|
|
55
|
+
if !objOk {
|
|
50
56
|
return false
|
|
51
57
|
}
|
|
52
58
|
|
|
53
|
-
//
|
|
54
|
-
obj
|
|
55
|
-
|
|
56
|
-
|
|
59
|
+
// Handle package-level function calls (e.g., time.Sleep)
|
|
60
|
+
if obj != nil {
|
|
61
|
+
if pkgName, isPkg := obj.(*types.PkgName); isPkg {
|
|
62
|
+
methodName := fun.Sel.Name
|
|
63
|
+
pkgPath := pkgName.Imported().Path()
|
|
64
|
+
|
|
65
|
+
// Check if this package-level function is async (empty TypeName)
|
|
66
|
+
if c.analysis.IsMethodAsync(pkgPath, "", methodName) {
|
|
67
|
+
c.tsw.WriteLiterally("await ")
|
|
68
|
+
return true
|
|
69
|
+
}
|
|
70
|
+
return false
|
|
71
|
+
}
|
|
57
72
|
}
|
|
58
73
|
|
|
59
|
-
|
|
60
|
-
|
|
74
|
+
// Get the type for method calls on objects
|
|
75
|
+
var targetType types.Type
|
|
76
|
+
|
|
77
|
+
if obj != nil {
|
|
78
|
+
// Direct variable case: obj.method()
|
|
79
|
+
if varObj, ok := obj.(*types.Var); ok {
|
|
80
|
+
targetType = varObj.Type()
|
|
81
|
+
} else {
|
|
82
|
+
return false
|
|
83
|
+
}
|
|
84
|
+
} else {
|
|
85
|
+
// Field access case: obj.field.method()
|
|
86
|
+
// Get the type of the field access expression
|
|
87
|
+
targetType = c.pkg.TypesInfo.TypeOf(fun.X)
|
|
88
|
+
if targetType == nil {
|
|
89
|
+
return false
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
methodName := fun.Sel.Name
|
|
94
|
+
|
|
95
|
+
// Check if target type is an interface
|
|
96
|
+
if interfaceType, isInterface := targetType.Underlying().(*types.Interface); isInterface {
|
|
97
|
+
// Interface method call: use interface method async analysis
|
|
98
|
+
if c.analysis.IsInterfaceMethodAsync(interfaceType, methodName) {
|
|
99
|
+
c.tsw.WriteLiterally("await ")
|
|
100
|
+
return true
|
|
101
|
+
}
|
|
61
102
|
return false
|
|
62
103
|
}
|
|
63
104
|
|
|
64
|
-
// Get the type
|
|
105
|
+
// Get the named type from the target type
|
|
65
106
|
var namedType *types.Named
|
|
66
107
|
var namedTypeOk bool
|
|
67
108
|
|
|
68
109
|
// Handle both direct named types and pointer to named types
|
|
69
|
-
switch t :=
|
|
110
|
+
switch t := targetType.(type) {
|
|
70
111
|
case *types.Named:
|
|
71
112
|
namedType, namedTypeOk = t, true
|
|
72
113
|
case *types.Pointer:
|
|
@@ -80,195 +121,27 @@ func (c *GoToTSCompiler) writeAsyncCallIfNeeded(exp *ast.CallExpr) bool {
|
|
|
80
121
|
}
|
|
81
122
|
|
|
82
123
|
typeName := namedType.Obj().Name()
|
|
83
|
-
methodName := fun.Sel.Name
|
|
84
124
|
|
|
85
|
-
//
|
|
125
|
+
// Determine the package path for the method
|
|
126
|
+
var pkgPath string
|
|
86
127
|
typePkg := namedType.Obj().Pkg()
|
|
87
|
-
if typePkg != nil
|
|
88
|
-
|
|
89
|
-
pkgPath := typePkg.Path()
|
|
90
|
-
|
|
91
|
-
// Check if this method is async based on metadata
|
|
92
|
-
if c.analysis.IsMethodAsync(pkgPath, typeName, methodName) {
|
|
93
|
-
c.awaitedCalls[exp] = true
|
|
94
|
-
c.tsw.WriteLiterally("await ")
|
|
95
|
-
return true
|
|
96
|
-
}
|
|
128
|
+
if typePkg != nil {
|
|
129
|
+
pkgPath = typePkg.Path()
|
|
97
130
|
} else {
|
|
98
|
-
//
|
|
99
|
-
|
|
100
|
-
if c.isLocalMethodAsync(namedType, methodName) {
|
|
101
|
-
c.awaitedCalls[exp] = true
|
|
102
|
-
c.tsw.WriteLiterally("await ")
|
|
103
|
-
return true
|
|
104
|
-
}
|
|
131
|
+
// Fallback to current package
|
|
132
|
+
pkgPath = c.pkg.Types.Path()
|
|
105
133
|
}
|
|
106
|
-
return false
|
|
107
134
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
// isLocalMethodAsync checks if a local method contains async operations by inspecting its body
|
|
114
|
-
func (c *GoToTSCompiler) isLocalMethodAsync(namedType *types.Named, methodName string) bool {
|
|
115
|
-
// Find the method in the named type
|
|
116
|
-
for i := 0; i < namedType.NumMethods(); i++ {
|
|
117
|
-
method := namedType.Method(i)
|
|
118
|
-
if method.Name() == methodName {
|
|
119
|
-
// Find the method declaration in the AST
|
|
120
|
-
methodDecl := c.findMethodDecl(namedType, methodName)
|
|
121
|
-
if methodDecl != nil && methodDecl.Body != nil {
|
|
122
|
-
// Check if the method body contains async operations
|
|
123
|
-
return c.containsAsyncOperationsRuntime(methodDecl.Body)
|
|
124
|
-
}
|
|
125
|
-
break
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
return false
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// findMethodDecl finds the AST declaration for a method on a named type
|
|
132
|
-
func (c *GoToTSCompiler) findMethodDecl(namedType *types.Named, methodName string) *ast.FuncDecl {
|
|
133
|
-
// Search through all files in the package for the method declaration
|
|
134
|
-
for _, file := range c.pkg.Syntax {
|
|
135
|
-
for _, decl := range file.Decls {
|
|
136
|
-
if funcDecl, ok := decl.(*ast.FuncDecl); ok {
|
|
137
|
-
// Check if this is a method with the right name
|
|
138
|
-
if funcDecl.Name.Name == methodName && funcDecl.Recv != nil {
|
|
139
|
-
// Check if the receiver type matches
|
|
140
|
-
if c.isReceiverOfType(funcDecl, namedType) {
|
|
141
|
-
return funcDecl
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
135
|
+
// Check if this method is async using unified analysis
|
|
136
|
+
if c.analysis.IsMethodAsync(pkgPath, typeName, methodName) {
|
|
137
|
+
c.tsw.WriteLiterally("await ")
|
|
138
|
+
return true
|
|
145
139
|
}
|
|
146
|
-
}
|
|
147
|
-
return nil
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// isReceiverOfType checks if a function declaration is a method of the given named type
|
|
151
|
-
func (c *GoToTSCompiler) isReceiverOfType(funcDecl *ast.FuncDecl, namedType *types.Named) bool {
|
|
152
|
-
if funcDecl.Recv == nil || len(funcDecl.Recv.List) == 0 {
|
|
153
140
|
return false
|
|
154
|
-
}
|
|
155
141
|
|
|
156
|
-
|
|
157
|
-
if len(recvField.Names) == 0 {
|
|
142
|
+
default:
|
|
158
143
|
return false
|
|
159
144
|
}
|
|
160
|
-
|
|
161
|
-
recvIdent := recvField.Names[0]
|
|
162
|
-
if obj := c.pkg.TypesInfo.Defs[recvIdent]; obj != nil {
|
|
163
|
-
if varObj, ok := obj.(*types.Var); ok {
|
|
164
|
-
// Handle both direct named types and pointer to named types
|
|
165
|
-
var receiverNamedType *types.Named
|
|
166
|
-
switch t := varObj.Type().(type) {
|
|
167
|
-
case *types.Named:
|
|
168
|
-
receiverNamedType = t
|
|
169
|
-
case *types.Pointer:
|
|
170
|
-
if nt, isNamed := t.Elem().(*types.Named); isNamed {
|
|
171
|
-
receiverNamedType = nt
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
if receiverNamedType != nil {
|
|
176
|
-
// For generic types, compare the origin types and names
|
|
177
|
-
// since instantiated generics have different type objects
|
|
178
|
-
targetObj := namedType.Obj()
|
|
179
|
-
receiverObj := receiverNamedType.Obj()
|
|
180
|
-
|
|
181
|
-
// Compare by name and package path
|
|
182
|
-
return targetObj.Name() == receiverObj.Name() &&
|
|
183
|
-
targetObj.Pkg() == receiverObj.Pkg()
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
return false
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
// containsAsyncOperationsRuntime checks if a node contains async operations at runtime
|
|
192
|
-
func (c *GoToTSCompiler) containsAsyncOperationsRuntime(node ast.Node) bool {
|
|
193
|
-
var hasAsync bool
|
|
194
|
-
|
|
195
|
-
ast.Inspect(node, func(n ast.Node) bool {
|
|
196
|
-
if n == nil {
|
|
197
|
-
return false
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
switch s := n.(type) {
|
|
201
|
-
case *ast.SendStmt:
|
|
202
|
-
// Channel send operation (ch <- value)
|
|
203
|
-
hasAsync = true
|
|
204
|
-
return false
|
|
205
|
-
|
|
206
|
-
case *ast.UnaryExpr:
|
|
207
|
-
// Channel receive operation (<-ch)
|
|
208
|
-
if s.Op == token.ARROW {
|
|
209
|
-
hasAsync = true
|
|
210
|
-
return false
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
case *ast.SelectStmt:
|
|
214
|
-
// Select statement with channel operations
|
|
215
|
-
hasAsync = true
|
|
216
|
-
return false
|
|
217
|
-
|
|
218
|
-
case *ast.CallExpr:
|
|
219
|
-
// Check if we're calling a function known to be async
|
|
220
|
-
if funcIdent, ok := s.Fun.(*ast.Ident); ok {
|
|
221
|
-
// Get the object for this function call
|
|
222
|
-
if obj := c.pkg.TypesInfo.Uses[funcIdent]; obj != nil && c.analysis.IsAsyncFunc(obj) {
|
|
223
|
-
hasAsync = true
|
|
224
|
-
return false
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
// Check for method calls on imported types (similar to analysis.go logic)
|
|
229
|
-
if selExpr, ok := s.Fun.(*ast.SelectorExpr); ok {
|
|
230
|
-
if ident, ok := selExpr.X.(*ast.Ident); ok {
|
|
231
|
-
if obj := c.pkg.TypesInfo.Uses[ident]; obj != nil {
|
|
232
|
-
if varObj, ok := obj.(*types.Var); ok {
|
|
233
|
-
var namedType *types.Named
|
|
234
|
-
switch t := varObj.Type().(type) {
|
|
235
|
-
case *types.Named:
|
|
236
|
-
namedType = t
|
|
237
|
-
case *types.Pointer:
|
|
238
|
-
if nt, isNamed := t.Elem().(*types.Named); isNamed {
|
|
239
|
-
namedType = nt
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
if namedType != nil {
|
|
244
|
-
typeName := namedType.Obj().Name()
|
|
245
|
-
methodName := selExpr.Sel.Name
|
|
246
|
-
|
|
247
|
-
// Check if the type is from an imported package
|
|
248
|
-
if typePkg := namedType.Obj().Pkg(); typePkg != nil && typePkg != c.pkg.Types {
|
|
249
|
-
pkgPath := typePkg.Path()
|
|
250
|
-
if c.analysis.IsMethodAsync(pkgPath, typeName, methodName) {
|
|
251
|
-
hasAsync = true
|
|
252
|
-
return false
|
|
253
|
-
}
|
|
254
|
-
} else {
|
|
255
|
-
// For local types, recursively check
|
|
256
|
-
if c.isLocalMethodAsync(namedType, methodName) {
|
|
257
|
-
hasAsync = true
|
|
258
|
-
return false
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
return true
|
|
269
|
-
})
|
|
270
|
-
|
|
271
|
-
return hasAsync
|
|
272
145
|
}
|
|
273
146
|
|
|
274
147
|
// addNonNullAssertion adds ! for function calls that might return null
|
package/compiler/expr.go
CHANGED
|
@@ -405,50 +405,6 @@ func (c *GoToTSCompiler) WriteBinaryExpr(exp *ast.BinaryExpr) error {
|
|
|
405
405
|
return nil
|
|
406
406
|
}
|
|
407
407
|
|
|
408
|
-
// Check for Duration arithmetic operations (multiplication)
|
|
409
|
-
if exp.Op == token.MUL && c.pkg != nil && c.pkg.TypesInfo != nil {
|
|
410
|
-
leftType := c.pkg.TypesInfo.TypeOf(exp.X)
|
|
411
|
-
rightType := c.pkg.TypesInfo.TypeOf(exp.Y)
|
|
412
|
-
|
|
413
|
-
// Check if left operand is a Duration type (from time package)
|
|
414
|
-
if leftType != nil {
|
|
415
|
-
if namedType, ok := leftType.(*types.Named); ok {
|
|
416
|
-
if namedType.Obj().Pkg() != nil && namedType.Obj().Pkg().Path() == "time" && namedType.Obj().Name() == "Duration" {
|
|
417
|
-
// Duration * number -> Duration.multiply(duration, number)
|
|
418
|
-
c.tsw.WriteLiterally("$.multiplyDuration(")
|
|
419
|
-
if err := c.WriteValueExpr(exp.X); err != nil {
|
|
420
|
-
return fmt.Errorf("failed to write Duration in multiplication: %w", err)
|
|
421
|
-
}
|
|
422
|
-
c.tsw.WriteLiterally(", ")
|
|
423
|
-
if err := c.WriteValueExpr(exp.Y); err != nil {
|
|
424
|
-
return fmt.Errorf("failed to write multiplier in Duration multiplication: %w", err)
|
|
425
|
-
}
|
|
426
|
-
c.tsw.WriteLiterally(")")
|
|
427
|
-
return nil
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
// Check if right operand is a Duration type (number * Duration)
|
|
433
|
-
if rightType != nil {
|
|
434
|
-
if namedType, ok := rightType.(*types.Named); ok {
|
|
435
|
-
if namedType.Obj().Pkg() != nil && namedType.Obj().Pkg().Path() == "time" && namedType.Obj().Name() == "Duration" {
|
|
436
|
-
// number * Duration -> Duration.multiply(duration, number)
|
|
437
|
-
c.tsw.WriteLiterally("$.multiplyDuration(")
|
|
438
|
-
if err := c.WriteValueExpr(exp.Y); err != nil {
|
|
439
|
-
return fmt.Errorf("failed to write Duration in multiplication: %w", err)
|
|
440
|
-
}
|
|
441
|
-
c.tsw.WriteLiterally(", ")
|
|
442
|
-
if err := c.WriteValueExpr(exp.X); err != nil {
|
|
443
|
-
return fmt.Errorf("failed to write multiplier in Duration multiplication: %w", err)
|
|
444
|
-
}
|
|
445
|
-
c.tsw.WriteLiterally(")")
|
|
446
|
-
return nil
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
|
|
452
408
|
// Check if the operator is a bitwise operator
|
|
453
409
|
isBitwise := false
|
|
454
410
|
switch exp.Op {
|
package/compiler/stmt-assign.go
CHANGED
|
@@ -117,8 +117,6 @@ func (c *GoToTSCompiler) WriteStmtAssign(exp *ast.AssignStmt) error {
|
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
119
|
c.tsw.WriteLiterally("] = ")
|
|
120
|
-
// Add await if the call is async
|
|
121
|
-
c.writeAsyncCallIfNeeded(callExpr)
|
|
122
120
|
c.WriteValueExpr(callExpr)
|
|
123
121
|
c.tsw.WriteLine("")
|
|
124
122
|
return nil
|
|
@@ -167,8 +165,6 @@ func (c *GoToTSCompiler) WriteStmtAssign(exp *ast.AssignStmt) error {
|
|
|
167
165
|
|
|
168
166
|
// Write a temporary variable to hold the function call result
|
|
169
167
|
c.tsw.WriteLiterally(" const _tmp = ")
|
|
170
|
-
// Add await if the call is async
|
|
171
|
-
c.writeAsyncCallIfNeeded(callExpr)
|
|
172
168
|
if err := c.WriteValueExpr(callExpr); err != nil {
|
|
173
169
|
return fmt.Errorf("failed to write RHS call expression in assignment: %w", err)
|
|
174
170
|
}
|
|
@@ -291,8 +287,6 @@ func (c *GoToTSCompiler) WriteStmtAssign(exp *ast.AssignStmt) error {
|
|
|
291
287
|
}
|
|
292
288
|
c.tsw.WriteLiterally("] = ")
|
|
293
289
|
|
|
294
|
-
// Add await if the call is async
|
|
295
|
-
c.writeAsyncCallIfNeeded(callExpr)
|
|
296
290
|
c.WriteValueExpr(callExpr)
|
|
297
291
|
|
|
298
292
|
c.tsw.WriteLine("")
|
package/compiler/stmt-select.go
CHANGED
|
@@ -69,10 +69,10 @@ func (c *GoToTSCompiler) WriteStmtSelect(exp *ast.SelectStmt) error {
|
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
// Generate unique variable names for this select statement
|
|
72
|
-
selectID :=
|
|
72
|
+
selectID := c.getDeterministicID(exp.Pos()) // Use deterministic position-based ID
|
|
73
73
|
|
|
74
74
|
// Start the selectStatement call and the array literal
|
|
75
|
-
c.tsw.WriteLiterallyf("const [
|
|
75
|
+
c.tsw.WriteLiterallyf("const [_select_has_return_%s, _select_value_%s] = await $.selectStatement(", selectID, selectID)
|
|
76
76
|
c.tsw.WriteLine("[") // Put bracket on new line
|
|
77
77
|
c.tsw.Indent(1)
|
|
78
78
|
|
|
@@ -241,10 +241,10 @@ func (c *GoToTSCompiler) WriteStmtSelect(exp *ast.SelectStmt) error {
|
|
|
241
241
|
c.tsw.WriteLine("")
|
|
242
242
|
|
|
243
243
|
// Add code to handle the return value from selectStatement
|
|
244
|
-
c.tsw.WriteLiterallyf("if (
|
|
244
|
+
c.tsw.WriteLiterallyf("if (_select_has_return_%s) {", selectID)
|
|
245
245
|
c.tsw.WriteLine("")
|
|
246
246
|
c.tsw.Indent(1)
|
|
247
|
-
c.tsw.WriteLiterallyf("return
|
|
247
|
+
c.tsw.WriteLiterallyf("return _select_value_%s!", selectID)
|
|
248
248
|
c.tsw.WriteLine("")
|
|
249
249
|
c.tsw.Indent(-1)
|
|
250
250
|
c.tsw.WriteLine("}")
|
|
@@ -254,7 +254,7 @@ func (c *GoToTSCompiler) WriteStmtSelect(exp *ast.SelectStmt) error {
|
|
|
254
254
|
c.tsw.WriteLine("// All cases should return, this fallback should never execute")
|
|
255
255
|
c.tsw.WriteLine("throw new Error('Unexpected: select statement did not return when all cases should return')")
|
|
256
256
|
} else {
|
|
257
|
-
c.tsw.WriteLiterallyf("// If
|
|
257
|
+
c.tsw.WriteLiterallyf("// If _select_has_return_%s is false, continue execution", selectID)
|
|
258
258
|
c.tsw.WriteLine("")
|
|
259
259
|
}
|
|
260
260
|
|
package/compiler/type-assert.go
CHANGED
|
@@ -96,8 +96,8 @@ func (c *GoToTSCompiler) writeTypeAssert(lhs []ast.Expr, typeAssertExpr *ast.Typ
|
|
|
96
96
|
// For selector expressions as ok, we need to use temporary variables approach
|
|
97
97
|
if okIsSelectorExpr {
|
|
98
98
|
// Use temporary variables approach similar to SelectorExpr case
|
|
99
|
-
tempValName := fmt.Sprintf("_gs_ta_val_%
|
|
100
|
-
tempOkName := fmt.Sprintf("_gs_ta_ok_%
|
|
99
|
+
tempValName := fmt.Sprintf("_gs_ta_val_%s", c.getDeterministicID(typeAssertExpr.Pos())) // Unique name based on deterministic position
|
|
100
|
+
tempOkName := fmt.Sprintf("_gs_ta_ok_%s", c.getDeterministicID(typeAssertExpr.Pos())) // Unique name based on deterministic position
|
|
101
101
|
|
|
102
102
|
// Declare temporary variables:
|
|
103
103
|
// let _gs_ta_val_: AssertedTypeTS;
|
|
@@ -217,8 +217,8 @@ func (c *GoToTSCompiler) writeTypeAssert(lhs []ast.Expr, typeAssertExpr *ast.Typ
|
|
|
217
217
|
|
|
218
218
|
case *ast.SelectorExpr:
|
|
219
219
|
// Handle s.field, ok := expr.(Type)
|
|
220
|
-
tempValName := fmt.Sprintf("_gs_ta_val_%
|
|
221
|
-
tempOkName := fmt.Sprintf("_gs_ta_ok_%
|
|
220
|
+
tempValName := fmt.Sprintf("_gs_ta_val_%s", c.getDeterministicID(typeAssertExpr.Pos())) // Unique name based on deterministic position
|
|
221
|
+
tempOkName := fmt.Sprintf("_gs_ta_ok_%s", c.getDeterministicID(typeAssertExpr.Pos())) // Unique name based on deterministic position
|
|
222
222
|
|
|
223
223
|
// Declare temporary variables:
|
|
224
224
|
// let _gs_ta_val_: AssertedTypeTS;
|
|
@@ -284,8 +284,8 @@ func (c *GoToTSCompiler) writeTypeAssert(lhs []ast.Expr, typeAssertExpr *ast.Typ
|
|
|
284
284
|
case *ast.IndexExpr:
|
|
285
285
|
// Handle slice[index], ok := expr.(Type) or map[key], ok := expr.(Type)
|
|
286
286
|
// Use unique temporary variable names to avoid redeclaration
|
|
287
|
-
tempValName := fmt.Sprintf("_gs_ta_val_%
|
|
288
|
-
tempOkName := fmt.Sprintf("_gs_ta_ok_%
|
|
287
|
+
tempValName := fmt.Sprintf("_gs_ta_val_%s", c.getDeterministicID(vLHS.Pos())) // Unique name based on deterministic position
|
|
288
|
+
tempOkName := fmt.Sprintf("_gs_ta_ok_%s", c.getDeterministicID(vLHS.Pos())) // Unique name based on deterministic position
|
|
289
289
|
|
|
290
290
|
// Declare temporary variables:
|
|
291
291
|
// let _gs_ta_val_N_: AssertedTypeTS;
|
|
@@ -11,7 +11,6 @@ export declare function println(...args: any[]): void;
|
|
|
11
11
|
export declare function panic(...args: any[]): void;
|
|
12
12
|
export type Bytes = Uint8Array | Slice<number>;
|
|
13
13
|
export declare function int(value: number): number;
|
|
14
|
-
export declare function multiplyDuration(duration: any, multiplier: number): any;
|
|
15
14
|
/**
|
|
16
15
|
* Normalizes various byte representations into a `Uint8Array` for protobuf compatibility.
|
|
17
16
|
*
|
|
@@ -27,15 +27,6 @@ export function int(value) {
|
|
|
27
27
|
// For this we use Math.trunc.
|
|
28
28
|
return Math.trunc(value);
|
|
29
29
|
}
|
|
30
|
-
// Duration multiplication helper for time package operations
|
|
31
|
-
// Handles expressions like time.Hour * 24
|
|
32
|
-
export function multiplyDuration(duration, multiplier) {
|
|
33
|
-
// Duration is now a number type alias, so just multiply directly
|
|
34
|
-
if (typeof duration === 'number') {
|
|
35
|
-
return duration * multiplier;
|
|
36
|
-
}
|
|
37
|
-
throw new Error(`Cannot multiply duration of type ${typeof duration}`);
|
|
38
|
-
}
|
|
39
30
|
/**
|
|
40
31
|
* Normalizes various byte representations into a `Uint8Array` for protobuf compatibility.
|
|
41
32
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"builtin.js","sourceRoot":"","sources":["../../../gs/builtin/builtin.ts"],"names":[],"mappings":"AACA,OAAO,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AAG,IAAI,CAAA;QAChB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACvC,IAAI,QAAQ,CAAC,GAAG,GAAG,CAAC,CAAC,KAAK,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC;gBACpC,KAAK,GAAG,KAAK,CAAA;gBACb,MAAK;YACP,CAAC;QACH,CAAC;QACD,IAAI,KAAK,EAAE,CAAC;YACV,KAAK,EAAE,CAAA;YACP,GAAG,IAAI,MAAM,CAAC,MAAM,CAAA;QACtB,CAAC;aAAM,CAAC;YACN,GAAG,EAAE,CAAA;QACP,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAA;AACd,CAAC;AAED,4CAA4C;AAC5C,MAAM,UAAU,GAAG,CAAC,CAAS,EAAE,CAAS;IACtC,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;AACvB,CAAC;AAED,MAAM,UAAU,GAAG,CAAC,CAAS,EAAE,CAAS;IACtC,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;AACvB,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,oBAAoB,CAAC,YAA6B;IAChE,IAAI,OAAO,YAAY,KAAK,QAAQ,EAAE,CAAC;QACrC,OAAO,YAAY,CAAA;IACrB,CAAC;IACD,uEAAuE;IACvE,OAAO,MAAM,CAAC,YAAY,CAAC,YAAY,CAAC,CAAA;AAC1C,CAAC;AAED,sDAAsD;AACtD,MAAM,UAAU,OAAO;IACrB,kFAAkF;IAClF,0DAA0D;IAC1D,OAAO,IAAI,CAAA;AACb,CAAC"}
|
|
@@ -117,7 +117,9 @@ export declare function chanRecvWithOk<T>(channel: Channel<T> | ChannelRef<T> |
|
|
|
117
117
|
* @param direction Optional direction for the channel. Default is 'both' (bidirectional).
|
|
118
118
|
* @returns A new channel instance or channel reference.
|
|
119
119
|
*/
|
|
120
|
-
export declare
|
|
120
|
+
export declare function makeChannel<T>(bufferSize: number, zeroValue: T, direction: 'send'): SendOnlyChannelRef<T>;
|
|
121
|
+
export declare function makeChannel<T>(bufferSize: number, zeroValue: T, direction: 'receive'): ReceiveOnlyChannelRef<T>;
|
|
122
|
+
export declare function makeChannel<T>(bufferSize: number, zeroValue: T, direction?: 'both'): Channel<T>;
|
|
121
123
|
/**
|
|
122
124
|
* Represents a reference to a channel with a specific direction.
|
|
123
125
|
*/
|
|
@@ -143,16 +143,8 @@ export async function chanRecvWithOk(channel) {
|
|
|
143
143
|
}
|
|
144
144
|
return channel.receiveWithOk();
|
|
145
145
|
}
|
|
146
|
-
|
|
147
|
-
* Creates a new channel with the specified buffer size and zero value.
|
|
148
|
-
* @param bufferSize The size of the channel buffer. If 0, creates an unbuffered channel.
|
|
149
|
-
* @param zeroValue The zero value for the channel's element type.
|
|
150
|
-
* @param direction Optional direction for the channel. Default is 'both' (bidirectional).
|
|
151
|
-
* @returns A new channel instance or channel reference.
|
|
152
|
-
*/
|
|
153
|
-
export const makeChannel = (bufferSize, zeroValue, direction = 'both') => {
|
|
146
|
+
export function makeChannel(bufferSize, zeroValue, direction = 'both') {
|
|
154
147
|
const channel = new BufferedChannel(bufferSize, zeroValue);
|
|
155
|
-
// Wrap the channel with the appropriate ChannelRef based on direction
|
|
156
148
|
if (direction === 'send') {
|
|
157
149
|
return new SendOnlyChannelRef(channel);
|
|
158
150
|
}
|
|
@@ -162,7 +154,7 @@ export const makeChannel = (bufferSize, zeroValue, direction = 'both') => {
|
|
|
162
154
|
else {
|
|
163
155
|
return channel;
|
|
164
156
|
}
|
|
165
|
-
}
|
|
157
|
+
}
|
|
166
158
|
// A simple implementation of buffered channels
|
|
167
159
|
class BufferedChannel {
|
|
168
160
|
buffer = [];
|