goscript 0.0.25 → 0.0.28

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 (190) 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 +259 -55
  5. package/compiler/assignment.go +2 -2
  6. package/compiler/builtin_test.go +1 -1
  7. package/compiler/compiler.go +201 -49
  8. package/compiler/compiler_test.go +53 -0
  9. package/compiler/composite-lit.go +32 -8
  10. package/compiler/decl.go +6 -6
  11. package/compiler/expr-call.go +83 -0
  12. package/compiler/expr.go +1 -1
  13. package/compiler/protobuf.go +557 -0
  14. package/compiler/spec-struct.go +4 -0
  15. package/compiler/spec-value.go +11 -3
  16. package/compiler/spec.go +18 -1
  17. package/compiler/stmt-assign.go +35 -0
  18. package/compiler/type-assert.go +87 -0
  19. package/compiler/type.go +5 -2
  20. package/dist/gs/builtin/builtin.d.ts +19 -1
  21. package/dist/gs/builtin/builtin.js +85 -5
  22. package/dist/gs/builtin/builtin.js.map +1 -1
  23. package/dist/gs/builtin/channel.js.map +1 -1
  24. package/dist/gs/builtin/slice.d.ts +1 -1
  25. package/dist/gs/builtin/slice.js +59 -26
  26. package/dist/gs/builtin/slice.js.map +1 -1
  27. package/dist/gs/cmp/index.js.map +1 -1
  28. package/dist/gs/context/context.d.ts +1 -1
  29. package/dist/gs/context/context.js +20 -11
  30. package/dist/gs/context/context.js.map +1 -1
  31. package/dist/gs/errors/errors.d.ts +7 -0
  32. package/dist/gs/errors/errors.js +190 -0
  33. package/dist/gs/errors/errors.js.map +1 -0
  34. package/dist/gs/errors/index.d.ts +1 -0
  35. package/dist/gs/errors/index.js +2 -0
  36. package/dist/gs/errors/index.js.map +1 -0
  37. package/dist/gs/internal/goarch/index.js +1 -1
  38. package/dist/gs/internal/goarch/index.js.map +1 -1
  39. package/dist/gs/io/index.d.ts +1 -0
  40. package/dist/gs/io/index.js +2 -0
  41. package/dist/gs/io/index.js.map +1 -0
  42. package/dist/gs/io/io.d.ts +107 -0
  43. package/dist/gs/io/io.js +385 -0
  44. package/dist/gs/io/io.js.map +1 -0
  45. package/dist/gs/iter/iter.js.map +1 -1
  46. package/dist/gs/math/bits/index.js +34 -32
  47. package/dist/gs/math/bits/index.js.map +1 -1
  48. package/dist/gs/runtime/runtime.d.ts +1 -0
  49. package/dist/gs/runtime/runtime.js +15 -18
  50. package/dist/gs/runtime/runtime.js.map +1 -1
  51. package/dist/gs/slices/slices.d.ts +1 -1
  52. package/dist/gs/slices/slices.js +1 -1
  53. package/dist/gs/slices/slices.js.map +1 -1
  54. package/dist/gs/strings/builder.d.ts +18 -0
  55. package/dist/gs/strings/builder.js +205 -0
  56. package/dist/gs/strings/builder.js.map +1 -0
  57. package/dist/gs/strings/clone.d.ts +1 -0
  58. package/dist/gs/strings/clone.js +16 -0
  59. package/dist/gs/strings/clone.js.map +1 -0
  60. package/dist/gs/strings/compare.d.ts +1 -0
  61. package/dist/gs/strings/compare.js +14 -0
  62. package/dist/gs/strings/compare.js.map +1 -0
  63. package/dist/gs/strings/index.d.ts +2 -0
  64. package/dist/gs/strings/index.js +3 -0
  65. package/dist/gs/strings/index.js.map +1 -0
  66. package/dist/gs/strings/iter.d.ts +8 -0
  67. package/dist/gs/strings/iter.js +160 -0
  68. package/dist/gs/strings/iter.js.map +1 -0
  69. package/dist/gs/strings/reader.d.ts +34 -0
  70. package/dist/gs/strings/reader.js +418 -0
  71. package/dist/gs/strings/reader.js.map +1 -0
  72. package/dist/gs/strings/replace.d.ts +106 -0
  73. package/dist/gs/strings/replace.js +1136 -0
  74. package/dist/gs/strings/replace.js.map +1 -0
  75. package/dist/gs/strings/search.d.ts +24 -0
  76. package/dist/gs/strings/search.js +169 -0
  77. package/dist/gs/strings/search.js.map +1 -0
  78. package/dist/gs/strings/strings.d.ts +47 -0
  79. package/dist/gs/strings/strings.js +418 -0
  80. package/dist/gs/strings/strings.js.map +1 -0
  81. package/dist/gs/stringslite/index.d.ts +1 -0
  82. package/dist/gs/stringslite/index.js +2 -0
  83. package/dist/gs/stringslite/index.js.map +1 -0
  84. package/dist/gs/stringslite/strings.d.ts +11 -0
  85. package/dist/gs/stringslite/strings.js +67 -0
  86. package/dist/gs/stringslite/strings.js.map +1 -0
  87. package/dist/gs/sync/index.d.ts +1 -0
  88. package/dist/gs/sync/index.js +2 -0
  89. package/dist/gs/sync/index.js.map +1 -0
  90. package/dist/gs/sync/sync.d.ts +79 -0
  91. package/dist/gs/sync/sync.js +392 -0
  92. package/dist/gs/sync/sync.js.map +1 -0
  93. package/dist/gs/time/time.js +7 -7
  94. package/dist/gs/time/time.js.map +1 -1
  95. package/dist/gs/unicode/index.d.ts +1 -0
  96. package/dist/gs/unicode/index.js +2 -0
  97. package/dist/gs/unicode/index.js.map +1 -0
  98. package/dist/gs/unicode/unicode.d.ts +105 -0
  99. package/dist/gs/unicode/unicode.js +332 -0
  100. package/dist/gs/unicode/unicode.js.map +1 -0
  101. package/dist/gs/unicode/utf8/index.d.ts +1 -0
  102. package/dist/gs/unicode/utf8/index.js +3 -0
  103. package/dist/gs/unicode/utf8/index.js.map +1 -0
  104. package/dist/gs/unicode/utf8/utf8.d.ts +20 -0
  105. package/dist/gs/unicode/utf8/utf8.js +196 -0
  106. package/dist/gs/unicode/utf8/utf8.js.map +1 -0
  107. package/dist/gs/unsafe/index.d.ts +1 -0
  108. package/dist/gs/unsafe/index.js +2 -0
  109. package/dist/gs/unsafe/index.js.map +1 -0
  110. package/dist/gs/unsafe/unsafe.d.ts +11 -0
  111. package/dist/gs/unsafe/unsafe.js +44 -0
  112. package/dist/gs/unsafe/unsafe.js.map +1 -0
  113. package/go.mod +2 -1
  114. package/go.sum +6 -2
  115. package/gs/README.md +6 -0
  116. package/gs/builtin/builtin.ts +158 -0
  117. package/gs/builtin/channel.ts +683 -0
  118. package/gs/builtin/defer.ts +58 -0
  119. package/gs/builtin/index.ts +1 -0
  120. package/gs/builtin/io.ts +22 -0
  121. package/gs/builtin/map.ts +50 -0
  122. package/gs/builtin/slice.ts +1030 -0
  123. package/gs/builtin/type.ts +1106 -0
  124. package/gs/builtin/varRef.ts +25 -0
  125. package/gs/cmp/godoc.txt +8 -0
  126. package/gs/cmp/index.ts +29 -0
  127. package/gs/context/context.ts +401 -0
  128. package/gs/context/godoc.txt +69 -0
  129. package/gs/context/index.ts +1 -0
  130. package/gs/errors/errors.ts +223 -0
  131. package/gs/errors/godoc.txt +63 -0
  132. package/gs/errors/index.ts +1 -0
  133. package/gs/internal/goarch/godoc.txt +39 -0
  134. package/gs/internal/goarch/index.ts +18 -0
  135. package/gs/io/godoc.txt +61 -0
  136. package/gs/io/index.ts +1 -0
  137. package/gs/io/io.go +75 -0
  138. package/gs/io/io.ts +546 -0
  139. package/gs/iter/godoc.txt +203 -0
  140. package/gs/iter/index.ts +1 -0
  141. package/gs/iter/iter.ts +117 -0
  142. package/gs/math/bits/index.ts +356 -0
  143. package/gs/math/godoc.txt +76 -0
  144. package/gs/runtime/godoc.txt +331 -0
  145. package/gs/runtime/index.ts +1 -0
  146. package/gs/runtime/runtime.ts +178 -0
  147. package/gs/slices/godoc.txt +44 -0
  148. package/gs/slices/index.ts +1 -0
  149. package/gs/slices/slices.ts +22 -0
  150. package/gs/strings/builder.test.ts +121 -0
  151. package/gs/strings/builder.ts +223 -0
  152. package/gs/strings/clone.test.ts +43 -0
  153. package/gs/strings/clone.ts +17 -0
  154. package/gs/strings/compare.test.ts +84 -0
  155. package/gs/strings/compare.ts +13 -0
  156. package/gs/strings/godoc.txt +66 -0
  157. package/gs/strings/index.ts +2 -0
  158. package/gs/strings/iter.test.ts +343 -0
  159. package/gs/strings/iter.ts +171 -0
  160. package/gs/strings/reader.test.ts +243 -0
  161. package/gs/strings/reader.ts +451 -0
  162. package/gs/strings/replace.test.ts +181 -0
  163. package/gs/strings/replace.ts +1310 -0
  164. package/gs/strings/search.test.ts +214 -0
  165. package/gs/strings/search.ts +213 -0
  166. package/gs/strings/strings.test.ts +477 -0
  167. package/gs/strings/strings.ts +510 -0
  168. package/gs/stringslite/godoc.txt +17 -0
  169. package/gs/stringslite/index.ts +1 -0
  170. package/gs/stringslite/strings.ts +82 -0
  171. package/gs/sync/godoc.txt +21 -0
  172. package/gs/sync/index.ts +1 -0
  173. package/gs/sync/sync.go +64 -0
  174. package/gs/sync/sync.ts +449 -0
  175. package/gs/time/godoc.md +116 -0
  176. package/gs/time/godoc.txt +116 -0
  177. package/gs/time/index.ts +1 -0
  178. package/gs/time/time.ts +272 -0
  179. package/gs/unicode/godoc.txt +52 -0
  180. package/gs/unicode/index.ts +1 -0
  181. package/gs/unicode/unicode.go +38 -0
  182. package/gs/unicode/unicode.ts +418 -0
  183. package/gs/unicode/utf8/godoc.txt +22 -0
  184. package/gs/unicode/utf8/index.ts +2 -0
  185. package/gs/unicode/utf8/utf8.ts +227 -0
  186. package/gs/unsafe/godoc.txt +19 -0
  187. package/gs/unsafe/index.ts +1 -0
  188. package/gs/unsafe/unsafe.test.ts +68 -0
  189. package/gs/unsafe/unsafe.ts +77 -0
  190. package/package.json +6 -4
@@ -0,0 +1,58 @@
1
+ /**
2
+ * DisposableStack manages synchronous disposable resources, mimicking Go's defer behavior.
3
+ * Functions added via `defer` are executed in LIFO order when the stack is disposed.
4
+ * Implements the `Disposable` interface for use with `using` declarations.
5
+ */
6
+ export class DisposableStack implements Disposable {
7
+ private stack: (() => void)[] = []
8
+
9
+ /**
10
+ * Adds a function to be executed when the stack is disposed.
11
+ * @param fn The function to defer.
12
+ */
13
+ defer(fn: () => void): void {
14
+ this.stack.push(fn)
15
+ }
16
+
17
+ /**
18
+ * Disposes of the resources in the stack by executing the deferred functions
19
+ * in Last-In, First-Out (LIFO) order.
20
+ * If a deferred function throws an error, disposal stops, and the error is rethrown,
21
+ * similar to Go's panic behavior during defer execution.
22
+ */
23
+ [Symbol.dispose](): void {
24
+ // Emulate Go: if a deferred throws, stop and rethrow
25
+ while (this.stack.length) {
26
+ const fn = this.stack.pop()!
27
+ fn()
28
+ }
29
+ }
30
+ }
31
+
32
+ /**
33
+ * AsyncDisposableStack manages asynchronous disposable resources, mimicking Go's defer behavior.
34
+ * Functions added via `defer` are executed sequentially in LIFO order when the stack is disposed.
35
+ * Implements the `AsyncDisposable` interface for use with `await using` declarations.
36
+ */
37
+ export class AsyncDisposableStack implements AsyncDisposable {
38
+ private stack: (() => Promise<void> | void)[] = []
39
+
40
+ /**
41
+ * Adds a synchronous or asynchronous function to be executed when the stack is disposed.
42
+ * @param fn The function to defer. Can return void or a Promise<void>.
43
+ */
44
+ defer(fn: () => Promise<void> | void): void {
45
+ this.stack.push(fn)
46
+ }
47
+
48
+ /**
49
+ * Asynchronously disposes of the resources in the stack by executing the deferred functions
50
+ * sequentially in Last-In, First-Out (LIFO) order. It awaits each function if it returns a promise.
51
+ */
52
+ async [Symbol.asyncDispose](): Promise<void> {
53
+ // Execute in LIFO order, awaiting each potentially async function
54
+ for (let i = this.stack.length - 1; i >= 0; --i) {
55
+ await this.stack[i]()
56
+ }
57
+ }
58
+ }
@@ -0,0 +1 @@
1
+ export * from './builtin.js'
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Implementation of Go's built-in println function
3
+ * @param args Arguments to print
4
+ */
5
+ export function println(...args: any[]): void {
6
+ console.log(...args)
7
+ }
8
+
9
+ /**
10
+ * Implementation of Go's built-in panic function
11
+ * @param args Arguments passed to panic
12
+ */
13
+ export function panic(...args: any[]): void {
14
+ throw new Error(`panic: ${args.map((arg) => String(arg)).join(' ')}`)
15
+ }
16
+
17
+ /**
18
+ * Represents the Go error type (interface).
19
+ */
20
+ export type GoError = {
21
+ Error(): string
22
+ } | null
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Creates a new map (TypeScript Map).
3
+ * @returns A new TypeScript Map.
4
+ */
5
+ export const makeMap = <K, V>(): Map<K, V> => {
6
+ return new Map<K, V>()
7
+ }
8
+ /**
9
+ * Gets a value from a map, with a default value if the key doesn't exist.
10
+ * @param map The map to get from.
11
+ * @param key The key to get.
12
+ * @param defaultValue The default value to return if the key doesn't exist (defaults to 0).
13
+ * @returns The value for the key, or the default value if the key doesn't exist.
14
+ */
15
+ export const mapGet = <K, V>(
16
+ map: Map<K, V>,
17
+ key: K,
18
+ defaultValue: V | null = null,
19
+ ): V | null => {
20
+ return map.has(key) ? map.get(key)! : defaultValue
21
+ }
22
+
23
+ /**
24
+ * Sets a value in a map.
25
+ * @param map The map to set in.
26
+ * @param key The key to set.
27
+ * @param value The value to set.
28
+ */
29
+ export const mapSet = <K, V>(map: Map<K, V>, key: K, value: V): void => {
30
+ map.set(key, value)
31
+ }
32
+
33
+ /**
34
+ * Deletes a key from a map.
35
+ * @param map The map to delete from.
36
+ * @param key The key to delete.
37
+ */
38
+ export const deleteMapEntry = <K, V>(map: Map<K, V>, key: K): void => {
39
+ map.delete(key)
40
+ }
41
+
42
+ /**
43
+ * Checks if a key exists in a map.
44
+ * @param map The map to check in.
45
+ * @param key The key to check.
46
+ * @returns True if the key exists, false otherwise.
47
+ */
48
+ export const mapHas = <K, V>(map: Map<K, V>, key: K): boolean => {
49
+ return map.has(key)
50
+ }