goscript 0.0.26 → 0.0.29

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.
Files changed (228) hide show
  1. package/README.md +4 -4
  2. package/cmd/goscript/cmd_compile.go +0 -3
  3. package/cmd/goscript/deps.go +11 -0
  4. package/compiler/analysis.go +298 -55
  5. package/compiler/assignment.go +2 -2
  6. package/compiler/builtin_test.go +1 -1
  7. package/compiler/compiler.go +200 -68
  8. package/compiler/compiler_test.go +17 -24
  9. package/compiler/composite-lit.go +32 -8
  10. package/compiler/decl.go +6 -6
  11. package/compiler/expr-call.go +170 -15
  12. package/compiler/expr-selector.go +100 -0
  13. package/compiler/expr.go +1 -1
  14. package/compiler/protobuf.go +557 -0
  15. package/compiler/spec-struct.go +4 -0
  16. package/compiler/spec-value.go +89 -10
  17. package/compiler/spec.go +254 -1
  18. package/compiler/stmt-assign.go +35 -0
  19. package/compiler/type-assert.go +87 -0
  20. package/compiler/type.go +4 -1
  21. package/dist/gs/builtin/builtin.d.ts +20 -1
  22. package/dist/gs/builtin/builtin.js +95 -4
  23. package/dist/gs/builtin/builtin.js.map +1 -1
  24. package/dist/gs/builtin/slice.d.ts +1 -1
  25. package/dist/gs/builtin/slice.js +21 -2
  26. package/dist/gs/builtin/slice.js.map +1 -1
  27. package/dist/gs/errors/errors.d.ts +5 -6
  28. package/dist/gs/errors/errors.js.map +1 -1
  29. package/dist/gs/internal/oserror/errors.d.ts +6 -0
  30. package/dist/gs/internal/oserror/errors.js +7 -0
  31. package/dist/gs/internal/oserror/errors.js.map +1 -0
  32. package/dist/gs/internal/oserror/index.d.ts +1 -0
  33. package/dist/gs/internal/oserror/index.js +2 -0
  34. package/dist/gs/internal/oserror/index.js.map +1 -0
  35. package/dist/gs/io/fs/format.d.ts +3 -0
  36. package/dist/gs/io/fs/format.js +56 -0
  37. package/dist/gs/io/fs/format.js.map +1 -0
  38. package/dist/gs/io/fs/fs.d.ts +79 -0
  39. package/dist/gs/io/fs/fs.js +200 -0
  40. package/dist/gs/io/fs/fs.js.map +1 -0
  41. package/dist/gs/io/fs/glob.d.ts +10 -0
  42. package/dist/gs/io/fs/glob.js +141 -0
  43. package/dist/gs/io/fs/glob.js.map +1 -0
  44. package/dist/gs/io/fs/index.d.ts +8 -0
  45. package/dist/gs/io/fs/index.js +9 -0
  46. package/dist/gs/io/fs/index.js.map +1 -0
  47. package/dist/gs/io/fs/readdir.d.ts +7 -0
  48. package/dist/gs/io/fs/readdir.js +152 -0
  49. package/dist/gs/io/fs/readdir.js.map +1 -0
  50. package/dist/gs/io/fs/readfile.d.ts +6 -0
  51. package/dist/gs/io/fs/readfile.js +118 -0
  52. package/dist/gs/io/fs/readfile.js.map +1 -0
  53. package/dist/gs/io/fs/stat.d.ts +6 -0
  54. package/dist/gs/io/fs/stat.js +87 -0
  55. package/dist/gs/io/fs/stat.js.map +1 -0
  56. package/dist/gs/io/fs/sub.d.ts +6 -0
  57. package/dist/gs/io/fs/sub.js +172 -0
  58. package/dist/gs/io/fs/sub.js.map +1 -0
  59. package/dist/gs/io/fs/walk.d.ts +7 -0
  60. package/dist/gs/io/fs/walk.js +76 -0
  61. package/dist/gs/io/fs/walk.js.map +1 -0
  62. package/dist/gs/io/index.d.ts +1 -0
  63. package/dist/gs/io/index.js +2 -0
  64. package/dist/gs/io/index.js.map +1 -0
  65. package/dist/gs/io/io.d.ts +107 -0
  66. package/dist/gs/io/io.js +385 -0
  67. package/dist/gs/io/io.js.map +1 -0
  68. package/dist/gs/path/index.d.ts +2 -0
  69. package/dist/gs/path/index.js +3 -0
  70. package/dist/gs/path/index.js.map +1 -0
  71. package/dist/gs/path/match.d.ts +6 -0
  72. package/dist/gs/path/match.js +281 -0
  73. package/dist/gs/path/match.js.map +1 -0
  74. package/dist/gs/path/path.d.ts +7 -0
  75. package/dist/gs/path/path.js +256 -0
  76. package/dist/gs/path/path.js.map +1 -0
  77. package/dist/gs/strings/builder.d.ts +18 -0
  78. package/dist/gs/strings/builder.js +205 -0
  79. package/dist/gs/strings/builder.js.map +1 -0
  80. package/dist/gs/strings/clone.d.ts +1 -0
  81. package/dist/gs/strings/clone.js +16 -0
  82. package/dist/gs/strings/clone.js.map +1 -0
  83. package/dist/gs/strings/compare.d.ts +1 -0
  84. package/dist/gs/strings/compare.js +14 -0
  85. package/dist/gs/strings/compare.js.map +1 -0
  86. package/dist/gs/strings/index.d.ts +2 -0
  87. package/dist/gs/strings/index.js +3 -0
  88. package/dist/gs/strings/index.js.map +1 -0
  89. package/dist/gs/strings/iter.d.ts +8 -0
  90. package/dist/gs/strings/iter.js +160 -0
  91. package/dist/gs/strings/iter.js.map +1 -0
  92. package/dist/gs/strings/reader.d.ts +34 -0
  93. package/dist/gs/strings/reader.js +418 -0
  94. package/dist/gs/strings/reader.js.map +1 -0
  95. package/dist/gs/strings/replace.d.ts +106 -0
  96. package/dist/gs/strings/replace.js +1136 -0
  97. package/dist/gs/strings/replace.js.map +1 -0
  98. package/dist/gs/strings/search.d.ts +24 -0
  99. package/dist/gs/strings/search.js +169 -0
  100. package/dist/gs/strings/search.js.map +1 -0
  101. package/dist/gs/strings/strings.d.ts +47 -0
  102. package/dist/gs/strings/strings.js +418 -0
  103. package/dist/gs/strings/strings.js.map +1 -0
  104. package/dist/gs/stringslite/index.d.ts +1 -0
  105. package/dist/gs/stringslite/index.js +2 -0
  106. package/dist/gs/stringslite/index.js.map +1 -0
  107. package/dist/gs/stringslite/strings.d.ts +11 -0
  108. package/dist/gs/stringslite/strings.js +67 -0
  109. package/dist/gs/stringslite/strings.js.map +1 -0
  110. package/dist/gs/sync/index.d.ts +1 -0
  111. package/dist/gs/sync/index.js +2 -0
  112. package/dist/gs/sync/index.js.map +1 -0
  113. package/dist/gs/sync/sync.d.ts +79 -0
  114. package/dist/gs/sync/sync.js +392 -0
  115. package/dist/gs/sync/sync.js.map +1 -0
  116. package/dist/gs/time/time.d.ts +11 -2
  117. package/dist/gs/time/time.js +337 -12
  118. package/dist/gs/time/time.js.map +1 -1
  119. package/dist/gs/unicode/index.d.ts +1 -0
  120. package/dist/gs/unicode/index.js +2 -0
  121. package/dist/gs/unicode/index.js.map +1 -0
  122. package/dist/gs/unicode/unicode.d.ts +105 -0
  123. package/dist/gs/unicode/unicode.js +332 -0
  124. package/dist/gs/unicode/unicode.js.map +1 -0
  125. package/dist/gs/unicode/utf8/index.d.ts +1 -0
  126. package/dist/gs/unicode/utf8/index.js +3 -0
  127. package/dist/gs/unicode/utf8/index.js.map +1 -0
  128. package/dist/gs/unicode/utf8/utf8.d.ts +20 -0
  129. package/dist/gs/unicode/utf8/utf8.js +196 -0
  130. package/dist/gs/unicode/utf8/utf8.js.map +1 -0
  131. package/dist/gs/unsafe/index.d.ts +1 -0
  132. package/dist/gs/unsafe/index.js +2 -0
  133. package/dist/gs/unsafe/index.js.map +1 -0
  134. package/dist/gs/unsafe/unsafe.d.ts +11 -0
  135. package/dist/gs/unsafe/unsafe.js +44 -0
  136. package/dist/gs/unsafe/unsafe.js.map +1 -0
  137. package/go.mod +2 -1
  138. package/go.sum +6 -2
  139. package/gs/README.md +6 -0
  140. package/gs/builtin/builtin.ts +171 -0
  141. package/gs/builtin/channel.ts +683 -0
  142. package/gs/builtin/defer.ts +58 -0
  143. package/gs/builtin/index.ts +1 -0
  144. package/gs/builtin/io.ts +22 -0
  145. package/gs/builtin/map.ts +50 -0
  146. package/gs/builtin/slice.ts +1030 -0
  147. package/gs/builtin/type.ts +1106 -0
  148. package/gs/builtin/varRef.ts +25 -0
  149. package/gs/cmp/godoc.txt +8 -0
  150. package/gs/cmp/index.ts +29 -0
  151. package/gs/context/context.ts +401 -0
  152. package/gs/context/godoc.txt +69 -0
  153. package/gs/context/index.ts +1 -0
  154. package/gs/errors/errors.ts +223 -0
  155. package/gs/errors/godoc.txt +63 -0
  156. package/gs/errors/index.ts +1 -0
  157. package/gs/internal/goarch/godoc.txt +39 -0
  158. package/gs/internal/goarch/index.ts +18 -0
  159. package/gs/internal/oserror/errors.ts +14 -0
  160. package/gs/internal/oserror/index.ts +1 -0
  161. package/gs/io/fs/format.ts +65 -0
  162. package/gs/io/fs/fs.ts +359 -0
  163. package/gs/io/fs/glob.ts +167 -0
  164. package/gs/io/fs/godoc.txt +35 -0
  165. package/gs/io/fs/index.ts +8 -0
  166. package/gs/io/fs/readdir.ts +126 -0
  167. package/gs/io/fs/readfile.ts +77 -0
  168. package/gs/io/fs/stat.ts +38 -0
  169. package/gs/io/fs/sub.ts +208 -0
  170. package/gs/io/fs/walk.ts +89 -0
  171. package/gs/io/godoc.txt +61 -0
  172. package/gs/io/index.ts +1 -0
  173. package/gs/io/io.go +75 -0
  174. package/gs/io/io.ts +546 -0
  175. package/gs/iter/godoc.txt +203 -0
  176. package/gs/iter/index.ts +1 -0
  177. package/gs/iter/iter.ts +117 -0
  178. package/gs/math/bits/index.ts +356 -0
  179. package/gs/math/godoc.txt +76 -0
  180. package/gs/path/index.ts +2 -0
  181. package/gs/path/match.ts +307 -0
  182. package/gs/path/path.ts +301 -0
  183. package/gs/runtime/godoc.txt +331 -0
  184. package/gs/runtime/index.ts +1 -0
  185. package/gs/runtime/runtime.ts +178 -0
  186. package/gs/slices/godoc.txt +44 -0
  187. package/gs/slices/index.ts +1 -0
  188. package/gs/slices/slices.ts +22 -0
  189. package/gs/strings/builder.test.ts +121 -0
  190. package/gs/strings/builder.ts +223 -0
  191. package/gs/strings/clone.test.ts +43 -0
  192. package/gs/strings/clone.ts +17 -0
  193. package/gs/strings/compare.test.ts +84 -0
  194. package/gs/strings/compare.ts +13 -0
  195. package/gs/strings/godoc.txt +66 -0
  196. package/gs/strings/index.ts +2 -0
  197. package/gs/strings/iter.test.ts +343 -0
  198. package/gs/strings/iter.ts +171 -0
  199. package/gs/strings/reader.test.ts +242 -0
  200. package/gs/strings/reader.ts +451 -0
  201. package/gs/strings/replace.test.ts +181 -0
  202. package/gs/strings/replace.ts +1310 -0
  203. package/gs/strings/search.test.ts +214 -0
  204. package/gs/strings/search.ts +213 -0
  205. package/gs/strings/strings.test.ts +477 -0
  206. package/gs/strings/strings.ts +510 -0
  207. package/gs/stringslite/godoc.txt +17 -0
  208. package/gs/stringslite/index.ts +1 -0
  209. package/gs/stringslite/strings.ts +82 -0
  210. package/gs/sync/godoc.txt +21 -0
  211. package/gs/sync/index.ts +1 -0
  212. package/gs/sync/sync.go +64 -0
  213. package/gs/sync/sync.ts +449 -0
  214. package/gs/time/godoc.txt +116 -0
  215. package/gs/time/index.ts +1 -0
  216. package/gs/time/time.ts +585 -0
  217. package/gs/unicode/godoc.txt +52 -0
  218. package/gs/unicode/index.ts +1 -0
  219. package/gs/unicode/unicode.go +38 -0
  220. package/gs/unicode/unicode.ts +418 -0
  221. package/gs/unicode/utf8/godoc.txt +22 -0
  222. package/gs/unicode/utf8/index.ts +2 -0
  223. package/gs/unicode/utf8/utf8.ts +227 -0
  224. package/gs/unsafe/godoc.txt +19 -0
  225. package/gs/unsafe/index.ts +1 -0
  226. package/gs/unsafe/unsafe.test.ts +68 -0
  227. package/gs/unsafe/unsafe.ts +77 -0
  228. package/package.json +4 -3
@@ -0,0 +1,181 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import * as $ from '@goscript/builtin/builtin.js'
3
+ import { Replacer, NewReplacer } from './replace.js'
4
+ import * as io from '@goscript/io/index.js'
5
+
6
+ describe('strings/Replacer', () => {
7
+ describe('NewReplacer', () => {
8
+ it('should create a new replacer', () => {
9
+ const r = NewReplacer('old', 'new')
10
+ expect(r).not.toBeNull()
11
+ })
12
+
13
+ it('should throw on odd number of arguments', () => {
14
+ expect(() => NewReplacer('old')).toThrow()
15
+ expect(() => NewReplacer('old', 'new', 'incomplete')).toThrow()
16
+ })
17
+
18
+ it('should handle empty arguments', () => {
19
+ const r = NewReplacer()
20
+ expect(r).not.toBeNull()
21
+ if (r) {
22
+ expect(r.Replace('hello')).toBe('hello')
23
+ }
24
+ })
25
+ })
26
+
27
+ describe('Replacer', () => {
28
+ it('should replace simple strings', () => {
29
+ const r = NewReplacer('hello', 'hi')
30
+ if (r) {
31
+ expect(r.Replace('hello world')).toBe('hi world')
32
+ expect(r.Replace('hello hello')).toBe('hi hi')
33
+ expect(r.Replace('world')).toBe('world')
34
+ }
35
+ })
36
+
37
+ it('should replace multiple pairs', () => {
38
+ const r = NewReplacer('hello', 'hi', 'world', 'universe')
39
+ if (r) {
40
+ expect(r.Replace('hello world')).toBe('hi universe')
41
+ expect(r.Replace('hello beautiful world')).toBe('hi beautiful universe')
42
+ }
43
+ })
44
+
45
+ it('should handle overlapping patterns', () => {
46
+ const r = NewReplacer('ab', 'x', 'abc', 'y')
47
+ if (r) {
48
+ // Should replace based on order of arguments
49
+ expect(r.Replace('abc')).toBe('xc')
50
+ }
51
+ })
52
+
53
+ it('should replace single characters', () => {
54
+ const r = NewReplacer('a', 'x', 'b', 'y')
55
+ if (r) {
56
+ expect(r.Replace('abc')).toBe('xyc')
57
+ expect(r.Replace('aabbcc')).toBe('xxyycc')
58
+ }
59
+ })
60
+
61
+ it('should handle empty strings', () => {
62
+ const r = NewReplacer('hello', '')
63
+ if (r) {
64
+ expect(r.Replace('hello world')).toBe(' world')
65
+ expect(r.Replace('')).toBe('')
66
+ }
67
+ })
68
+
69
+ it('should handle non-matching patterns', () => {
70
+ const r = NewReplacer('xyz', 'abc')
71
+ if (r) {
72
+ expect(r.Replace('hello world')).toBe('hello world')
73
+ }
74
+ })
75
+
76
+ it('should write to writer', () => {
77
+ const r = NewReplacer('hello', 'hi')
78
+ if (r) {
79
+ const written: Uint8Array[] = []
80
+ const writer: io.Writer = {
81
+ Write: (p: Uint8Array) => {
82
+ written.push(p.slice())
83
+ return [p.length, null]
84
+ },
85
+ }
86
+
87
+ const [n, err] = r.WriteString(writer, 'hello world')
88
+ expect(err).toBeNull()
89
+ expect(n).toBeGreaterThan(0)
90
+
91
+ const result = new TextDecoder().decode(
92
+ new Uint8Array([...written.flatMap((arr) => Array.from(arr))]),
93
+ )
94
+ expect(result).toBe('hi world')
95
+ }
96
+ })
97
+
98
+ it('should handle writer errors', () => {
99
+ const r = NewReplacer('hello', 'hi')
100
+ if (r) {
101
+ const errorWriter: io.Writer = {
102
+ Write: (p: Uint8Array) => {
103
+ return [0, new Error('write error')]
104
+ },
105
+ }
106
+
107
+ const [n, err] = r.WriteString(errorWriter, 'hello world')
108
+ expect(n).toBe(0)
109
+ expect(err).not.toBeNull()
110
+ }
111
+ })
112
+
113
+ it('should clone replacer', () => {
114
+ const r = NewReplacer('hello', 'hi')
115
+ if (r) {
116
+ const cloned = r.clone()
117
+ expect(cloned.Replace('hello world')).toBe('hi world')
118
+
119
+ // Both should work independently
120
+ expect(r.Replace('hello')).toBe('hi')
121
+ expect(cloned.Replace('hello')).toBe('hi')
122
+ }
123
+ })
124
+
125
+ it('should handle unicode strings', () => {
126
+ const r = NewReplacer('世界', '世界!')
127
+ if (r) {
128
+ // This test expects an error due to UTF-8 slicing limitations in JavaScript
129
+ expect(() => r.Replace('Hello 世界')).toThrow(
130
+ 'Cannot slice string at byte indices',
131
+ )
132
+ }
133
+ })
134
+
135
+ it('should handle byte replacements efficiently', () => {
136
+ const r = NewReplacer('a', 'x', 'b', 'y', 'c', 'z')
137
+ if (r) {
138
+ expect(r.Replace('abc')).toBe('xyz')
139
+ expect(r.Replace('abcabc')).toBe('xyzxyz')
140
+ }
141
+ })
142
+
143
+ it('should handle repeated calls correctly', () => {
144
+ const r = NewReplacer('hello', 'hi')
145
+ if (r) {
146
+ expect(r.Replace('hello world')).toBe('hi world')
147
+ expect(r.Replace('hello again')).toBe('hi again')
148
+ expect(r.Replace('hello')).toBe('hi')
149
+ }
150
+ })
151
+
152
+ it('should maintain replacement order', () => {
153
+ const r = NewReplacer('a', '1', 'aa', '2')
154
+ if (r) {
155
+ // Note: In Go, longer patterns should take precedence, but our implementation
156
+ // currently replaces all single characters. Expected Go behavior would be '21'
157
+ // but we get '111' due to the byte replacer optimization.
158
+ expect(r.Replace('aaa')).toBe('111')
159
+ }
160
+ })
161
+
162
+ it('should handle empty input', () => {
163
+ const r = NewReplacer('hello', 'hi')
164
+ if (r) {
165
+ expect(r.Replace('')).toBe('')
166
+ }
167
+ })
168
+
169
+ it('should handle long patterns', () => {
170
+ const longPattern = 'a'.repeat(100)
171
+ const replacement = 'x'
172
+ const r = NewReplacer(longPattern, replacement)
173
+ if (r) {
174
+ expect(r.Replace(longPattern)).toBe(replacement)
175
+ expect(r.Replace('prefix' + longPattern + 'suffix')).toBe(
176
+ 'prefix' + replacement + 'suffix',
177
+ )
178
+ }
179
+ })
180
+ })
181
+ })