goscript 0.1.0 → 0.1.1

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 (327) hide show
  1. package/README.md +267 -255
  2. package/cmd/goscript/cmd-test.go +193 -0
  3. package/cmd/goscript/cmd-test_test.go +76 -0
  4. package/cmd/goscript/main.go +1 -0
  5. package/compiler/build-flags.go +38 -0
  6. package/compiler/compile-request.go +2 -0
  7. package/compiler/compliance_test.go +0 -8
  8. package/compiler/gotest/owner.go +24 -0
  9. package/compiler/gotest/package-result.go +67 -0
  10. package/compiler/gotest/request.go +145 -0
  11. package/compiler/gotest/result.go +28 -0
  12. package/compiler/gotest/runner.go +588 -0
  13. package/compiler/gotest/runner_test.go +627 -0
  14. package/compiler/gotest/test.go +9 -0
  15. package/compiler/index.test.ts +1 -1
  16. package/compiler/lowered-program.go +71 -19
  17. package/compiler/lowering.go +5065 -569
  18. package/compiler/override-facts.go +307 -0
  19. package/compiler/override-registry.go +50 -189
  20. package/compiler/override-registry_test.go +47 -0
  21. package/compiler/package-graph.go +50 -27
  22. package/compiler/package-graph_test.go +37 -2
  23. package/compiler/package-test-function.go +9 -0
  24. package/compiler/package-test-graph-package.go +40 -0
  25. package/compiler/package-test-graph-variant.go +105 -0
  26. package/compiler/package-test-graph.go +117 -0
  27. package/compiler/package-test-graph_test.go +144 -0
  28. package/compiler/runtime-contract.go +189 -29
  29. package/compiler/runtime-contract_test.go +44 -30
  30. package/compiler/semantic-model-types.go +9 -6
  31. package/compiler/semantic-model.go +538 -38
  32. package/compiler/semantic-model_test.go +55 -0
  33. package/compiler/service.go +1 -1
  34. package/compiler/skeleton_test.go +679 -49
  35. package/compiler/tsworkspace/owner.go +334 -0
  36. package/compiler/tsworkspace/owner_test.go +93 -0
  37. package/compiler/tsworkspace/result.go +17 -0
  38. package/compiler/typescript-emitter.go +459 -82
  39. package/compiler/wasm/compile.go +1 -1
  40. package/compiler/wasm/compile_test.go +61 -11
  41. package/compiler/wasm_api.go +172 -7
  42. package/dist/gs/builtin/builtin.d.ts +20 -2
  43. package/dist/gs/builtin/builtin.js +194 -6
  44. package/dist/gs/builtin/builtin.js.map +1 -1
  45. package/dist/gs/builtin/channel.d.ts +8 -0
  46. package/dist/gs/builtin/channel.js +12 -0
  47. package/dist/gs/builtin/channel.js.map +1 -1
  48. package/dist/gs/builtin/slice.d.ts +22 -2
  49. package/dist/gs/builtin/slice.js +216 -44
  50. package/dist/gs/builtin/slice.js.map +1 -1
  51. package/dist/gs/builtin/type.d.ts +5 -2
  52. package/dist/gs/builtin/type.js +83 -24
  53. package/dist/gs/builtin/type.js.map +1 -1
  54. package/dist/gs/builtin/varRef.d.ts +5 -0
  55. package/dist/gs/builtin/varRef.js +23 -0
  56. package/dist/gs/builtin/varRef.js.map +1 -1
  57. package/dist/gs/bytes/buffer.gs.js +48 -44
  58. package/dist/gs/bytes/buffer.gs.js.map +1 -1
  59. package/dist/gs/bytes/reader.gs.js +20 -18
  60. package/dist/gs/bytes/reader.gs.js.map +1 -1
  61. package/dist/gs/context/context.d.ts +5 -4
  62. package/dist/gs/context/context.js +10 -10
  63. package/dist/gs/context/context.js.map +1 -1
  64. package/dist/gs/crypto/internal/fips140deps/byteorder/index.d.ts +1 -0
  65. package/dist/gs/crypto/internal/fips140deps/byteorder/index.js +2 -0
  66. package/dist/gs/crypto/internal/fips140deps/byteorder/index.js.map +1 -0
  67. package/dist/gs/crypto/internal/fips140deps/godebug/index.d.ts +1 -0
  68. package/dist/gs/crypto/internal/fips140deps/godebug/index.js +2 -0
  69. package/dist/gs/crypto/internal/fips140deps/godebug/index.js.map +1 -0
  70. package/dist/gs/embed/index.d.ts +7 -0
  71. package/dist/gs/embed/index.js +16 -0
  72. package/dist/gs/embed/index.js.map +1 -0
  73. package/dist/gs/encoding/json/index.d.ts +1 -0
  74. package/dist/gs/encoding/json/index.js +18 -0
  75. package/dist/gs/encoding/json/index.js.map +1 -1
  76. package/dist/gs/errors/errors.d.ts +4 -0
  77. package/dist/gs/errors/errors.js +81 -0
  78. package/dist/gs/errors/errors.js.map +1 -1
  79. package/dist/gs/fmt/fmt.d.ts +4 -4
  80. package/dist/gs/fmt/fmt.js +42 -11
  81. package/dist/gs/fmt/fmt.js.map +1 -1
  82. package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.d.ts +35 -0
  83. package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.js +211 -1
  84. package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.js.map +1 -1
  85. package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.d.ts +189 -0
  86. package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.js +825 -0
  87. package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.js.map +1 -0
  88. package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.d.ts +163 -0
  89. package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.js +449 -0
  90. package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.js.map +1 -0
  91. package/dist/gs/github.com/klauspost/compress/internal/le/index.d.ts +9 -0
  92. package/dist/gs/github.com/klauspost/compress/internal/le/index.js +71 -0
  93. package/dist/gs/github.com/klauspost/compress/internal/le/index.js.map +1 -0
  94. package/dist/gs/go/internal/scannerhooks/index.d.ts +3 -0
  95. package/dist/gs/go/internal/scannerhooks/index.js +5 -0
  96. package/dist/gs/go/internal/scannerhooks/index.js.map +1 -0
  97. package/dist/gs/go/scanner/index.d.ts +13 -0
  98. package/dist/gs/go/scanner/index.js +35 -0
  99. package/dist/gs/go/scanner/index.js.map +1 -1
  100. package/dist/gs/go/token/index.d.ts +156 -0
  101. package/dist/gs/go/token/index.js +500 -4
  102. package/dist/gs/go/token/index.js.map +1 -1
  103. package/dist/gs/internal/abi/index.d.ts +4 -0
  104. package/dist/gs/internal/abi/index.js +10 -0
  105. package/dist/gs/internal/abi/index.js.map +1 -1
  106. package/dist/gs/internal/bytealg/index.d.ts +2 -0
  107. package/dist/gs/internal/bytealg/index.js +14 -0
  108. package/dist/gs/internal/bytealg/index.js.map +1 -1
  109. package/dist/gs/internal/byteorder/index.d.ts +8 -2
  110. package/dist/gs/internal/byteorder/index.js +56 -25
  111. package/dist/gs/internal/byteorder/index.js.map +1 -1
  112. package/dist/gs/internal/godebug/index.d.ts +12 -0
  113. package/dist/gs/internal/godebug/index.js +30 -0
  114. package/dist/gs/internal/godebug/index.js.map +1 -0
  115. package/dist/gs/io/fs/index.d.ts +1 -0
  116. package/dist/gs/io/fs/index.js +1 -0
  117. package/dist/gs/io/fs/index.js.map +1 -1
  118. package/dist/gs/io/fs/readlink.d.ts +8 -0
  119. package/dist/gs/io/fs/readlink.js +64 -0
  120. package/dist/gs/io/fs/readlink.js.map +1 -0
  121. package/dist/gs/io/fs/walk.d.ts +3 -3
  122. package/dist/gs/io/fs/walk.js +7 -7
  123. package/dist/gs/io/fs/walk.js.map +1 -1
  124. package/dist/gs/io/io.d.ts +40 -6
  125. package/dist/gs/io/io.js +151 -26
  126. package/dist/gs/io/io.js.map +1 -1
  127. package/dist/gs/maps/iter.d.ts +3 -3
  128. package/dist/gs/maps/iter.js +3 -3
  129. package/dist/gs/maps/iter.js.map +1 -1
  130. package/dist/gs/maps/maps.d.ts +2 -2
  131. package/dist/gs/maps/maps.js +1 -1
  132. package/dist/gs/maps/maps.js.map +1 -1
  133. package/dist/gs/math/bits/index.d.ts +13 -4
  134. package/dist/gs/math/bits/index.js +66 -34
  135. package/dist/gs/math/bits/index.js.map +1 -1
  136. package/dist/gs/math/const.gs.d.ts +5 -5
  137. package/dist/gs/math/const.gs.js +4 -4
  138. package/dist/gs/math/const.gs.js.map +1 -1
  139. package/dist/gs/mime/index.d.ts +1 -0
  140. package/dist/gs/mime/index.js +50 -0
  141. package/dist/gs/mime/index.js.map +1 -0
  142. package/dist/gs/net/http/httptest/index.d.ts +11 -0
  143. package/dist/gs/net/http/httptest/index.js +21 -0
  144. package/dist/gs/net/http/httptest/index.js.map +1 -0
  145. package/dist/gs/net/http/index.d.ts +27 -0
  146. package/dist/gs/net/http/index.js +61 -0
  147. package/dist/gs/net/http/index.js.map +1 -0
  148. package/dist/gs/os/dir_unix.gs.js +2 -2
  149. package/dist/gs/os/dir_unix.gs.js.map +1 -1
  150. package/dist/gs/os/types_js.gs.js.map +1 -1
  151. package/dist/gs/path/filepath/match.js +165 -3
  152. package/dist/gs/path/filepath/match.js.map +1 -1
  153. package/dist/gs/path/filepath/path.d.ts +3 -1
  154. package/dist/gs/path/filepath/path.js +133 -4
  155. package/dist/gs/path/filepath/path.js.map +1 -1
  156. package/dist/gs/path/path.d.ts +4 -1
  157. package/dist/gs/path/path.js +16 -4
  158. package/dist/gs/path/path.js.map +1 -1
  159. package/dist/gs/reflect/index.d.ts +1 -1
  160. package/dist/gs/reflect/index.js +1 -1
  161. package/dist/gs/reflect/index.js.map +1 -1
  162. package/dist/gs/reflect/map.js +3 -0
  163. package/dist/gs/reflect/map.js.map +1 -1
  164. package/dist/gs/reflect/type.d.ts +7 -4
  165. package/dist/gs/reflect/type.js +148 -7
  166. package/dist/gs/reflect/type.js.map +1 -1
  167. package/dist/gs/runtime/debug/index.d.ts +2 -0
  168. package/dist/gs/runtime/debug/index.js +8 -0
  169. package/dist/gs/runtime/debug/index.js.map +1 -0
  170. package/dist/gs/runtime/runtime.d.ts +35 -3
  171. package/dist/gs/runtime/runtime.js +72 -0
  172. package/dist/gs/runtime/runtime.js.map +1 -1
  173. package/dist/gs/slices/slices.d.ts +24 -5
  174. package/dist/gs/slices/slices.js +214 -5
  175. package/dist/gs/slices/slices.js.map +1 -1
  176. package/dist/gs/sort/slice.gs.d.ts +3 -3
  177. package/dist/gs/sort/slice.gs.js +6 -6
  178. package/dist/gs/sort/slice.gs.js.map +1 -1
  179. package/dist/gs/sort/sort.gs.d.ts +4 -4
  180. package/dist/gs/sort/sort.gs.js +11 -8
  181. package/dist/gs/sort/sort.gs.js.map +1 -1
  182. package/dist/gs/strings/builder.d.ts +1 -1
  183. package/dist/gs/strings/builder.js +3 -2
  184. package/dist/gs/strings/builder.js.map +1 -1
  185. package/dist/gs/sync/atomic/type.gs.d.ts +9 -8
  186. package/dist/gs/sync/atomic/type.gs.js +0 -2
  187. package/dist/gs/sync/atomic/type.gs.js.map +1 -1
  188. package/dist/gs/sync/sync.d.ts +2 -0
  189. package/dist/gs/sync/sync.js +27 -0
  190. package/dist/gs/sync/sync.js.map +1 -1
  191. package/dist/gs/syscall/constants.d.ts +36 -24
  192. package/dist/gs/syscall/constants.js +12 -0
  193. package/dist/gs/syscall/constants.js.map +1 -1
  194. package/dist/gs/syscall/errors.d.ts +2 -0
  195. package/dist/gs/syscall/errors.js +8 -0
  196. package/dist/gs/syscall/errors.js.map +1 -1
  197. package/dist/gs/syscall/fs.d.ts +43 -0
  198. package/dist/gs/syscall/fs.js +102 -0
  199. package/dist/gs/syscall/fs.js.map +1 -1
  200. package/dist/gs/syscall/js/index.d.ts +90 -0
  201. package/dist/gs/syscall/js/index.js +375 -0
  202. package/dist/gs/syscall/js/index.js.map +1 -0
  203. package/dist/gs/syscall/types.d.ts +22 -0
  204. package/dist/gs/syscall/types.js +45 -1
  205. package/dist/gs/syscall/types.js.map +1 -1
  206. package/dist/gs/testing/index.d.ts +1 -0
  207. package/dist/gs/testing/index.js +2 -0
  208. package/dist/gs/testing/index.js.map +1 -0
  209. package/dist/gs/testing/testing.d.ts +77 -0
  210. package/dist/gs/testing/testing.js +301 -0
  211. package/dist/gs/testing/testing.js.map +1 -0
  212. package/dist/gs/time/time.d.ts +41 -4
  213. package/dist/gs/time/time.js +205 -36
  214. package/dist/gs/time/time.js.map +1 -1
  215. package/dist/gs/unicode/unicode.d.ts +23 -1
  216. package/dist/gs/unicode/unicode.js +79 -10
  217. package/dist/gs/unicode/unicode.js.map +1 -1
  218. package/dist/gs/unicode/utf8/utf8.d.ts +4 -4
  219. package/dist/gs/unicode/utf8/utf8.js +24 -11
  220. package/dist/gs/unicode/utf8/utf8.js.map +1 -1
  221. package/dist/gs/unique/index.d.ts +11 -0
  222. package/dist/gs/unique/index.js +71 -0
  223. package/dist/gs/unique/index.js.map +1 -0
  224. package/go.sum +9 -0
  225. package/gs/builtin/builtin.ts +239 -8
  226. package/gs/builtin/channel.ts +22 -0
  227. package/gs/builtin/runtime-contract.test.ts +126 -0
  228. package/gs/builtin/slice.ts +259 -50
  229. package/gs/builtin/type.ts +109 -34
  230. package/gs/builtin/varRef.ts +38 -1
  231. package/gs/bytes/buffer.gs.ts +48 -44
  232. package/gs/bytes/meta.json +8 -3
  233. package/gs/bytes/reader.gs.ts +20 -19
  234. package/gs/context/context.test.ts +41 -0
  235. package/gs/context/context.ts +22 -26
  236. package/gs/crypto/internal/fips140deps/byteorder/index.ts +1 -0
  237. package/gs/crypto/internal/fips140deps/godebug/index.ts +1 -0
  238. package/gs/embed/index.ts +20 -0
  239. package/gs/embed/meta.json +5 -0
  240. package/gs/encoding/json/index.test.ts +15 -1
  241. package/gs/encoding/json/index.ts +24 -0
  242. package/gs/errors/errors.test.ts +82 -0
  243. package/gs/errors/errors.ts +104 -0
  244. package/gs/fmt/fmt.ts +56 -16
  245. package/gs/github.com/aperturerobotics/protobuf-go-lite/index.test.ts +73 -1
  246. package/gs/github.com/aperturerobotics/protobuf-go-lite/index.ts +297 -1
  247. package/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.test.ts +159 -0
  248. package/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.ts +1005 -0
  249. package/gs/github.com/aperturerobotics/starpc/srpc/index.ts +719 -0
  250. package/gs/github.com/aperturerobotics/starpc/srpc/meta.json +40 -0
  251. package/gs/github.com/klauspost/compress/internal/le/index.test.ts +36 -0
  252. package/gs/github.com/klauspost/compress/internal/le/index.ts +114 -0
  253. package/gs/go/internal/scannerhooks/index.test.ts +14 -0
  254. package/gs/go/internal/scannerhooks/index.ts +9 -0
  255. package/gs/go/scanner/index.test.ts +22 -0
  256. package/gs/go/scanner/index.ts +47 -0
  257. package/gs/go/token/index.test.ts +47 -1
  258. package/gs/go/token/index.ts +570 -4
  259. package/gs/internal/abi/index.test.ts +18 -0
  260. package/gs/internal/abi/index.ts +14 -0
  261. package/gs/internal/bytealg/index.test.ts +18 -0
  262. package/gs/internal/bytealg/index.ts +16 -0
  263. package/gs/internal/byteorder/index.test.ts +39 -0
  264. package/gs/internal/byteorder/index.ts +100 -27
  265. package/gs/internal/godebug/index.test.ts +16 -0
  266. package/gs/internal/godebug/index.ts +35 -0
  267. package/gs/io/fs/index.ts +1 -0
  268. package/gs/io/fs/meta.json +5 -0
  269. package/gs/io/fs/readlink.test.ts +43 -0
  270. package/gs/io/fs/readlink.ts +77 -0
  271. package/gs/io/fs/walk.test.ts +61 -0
  272. package/gs/io/fs/walk.ts +9 -9
  273. package/gs/io/io.ts +174 -31
  274. package/gs/io/meta.json +10 -2
  275. package/gs/maps/iter.ts +12 -6
  276. package/gs/maps/maps.ts +8 -6
  277. package/gs/math/bits/index.ts +103 -47
  278. package/gs/math/const.gs.test.ts +11 -5
  279. package/gs/math/const.gs.ts +5 -6
  280. package/gs/mime/index.ts +54 -0
  281. package/gs/net/http/httptest/index.ts +25 -0
  282. package/gs/net/http/index.test.ts +20 -0
  283. package/gs/net/http/index.ts +81 -0
  284. package/gs/os/dir_unix.gs.ts +2 -3
  285. package/gs/os/types_js.gs.ts +2 -2
  286. package/gs/path/filepath/match.test.ts +31 -12
  287. package/gs/path/filepath/match.ts +178 -3
  288. package/gs/path/filepath/path.test.ts +25 -0
  289. package/gs/path/filepath/path.ts +159 -5
  290. package/gs/path/path.ts +20 -5
  291. package/gs/reflect/index.ts +1 -0
  292. package/gs/reflect/map.test.ts +19 -0
  293. package/gs/reflect/map.ts +4 -0
  294. package/gs/reflect/type.ts +197 -17
  295. package/gs/runtime/debug/index.test.ts +24 -0
  296. package/gs/runtime/debug/index.ts +8 -0
  297. package/gs/runtime/runtime.test.ts +19 -0
  298. package/gs/runtime/runtime.ts +98 -3
  299. package/gs/slices/slices.test.ts +94 -0
  300. package/gs/slices/slices.ts +245 -5
  301. package/gs/sort/meta.json +7 -0
  302. package/gs/sort/slice.gs.ts +16 -7
  303. package/gs/sort/sort.gs.ts +16 -13
  304. package/gs/strings/builder.ts +4 -3
  305. package/gs/sync/atomic/type.gs.ts +13 -14
  306. package/gs/sync/meta.json +3 -1
  307. package/gs/sync/sync.test.ts +13 -1
  308. package/gs/sync/sync.ts +27 -0
  309. package/gs/syscall/constants.ts +39 -24
  310. package/gs/syscall/errors.ts +10 -0
  311. package/gs/syscall/fs.ts +195 -0
  312. package/gs/syscall/js/index.ts +458 -0
  313. package/gs/syscall/js/meta.json +4 -0
  314. package/gs/syscall/net.test.ts +85 -0
  315. package/gs/syscall/types.ts +56 -0
  316. package/gs/testing/index.ts +1 -0
  317. package/gs/testing/meta.json +5 -0
  318. package/gs/testing/testing.test.ts +90 -0
  319. package/gs/testing/testing.ts +382 -0
  320. package/gs/time/time.test.ts +106 -0
  321. package/gs/time/time.ts +278 -57
  322. package/gs/unicode/unicode.test.ts +25 -0
  323. package/gs/unicode/unicode.ts +119 -9
  324. package/gs/unicode/utf8/utf8.test.ts +13 -0
  325. package/gs/unicode/utf8/utf8.ts +28 -16
  326. package/gs/unique/index.ts +91 -0
  327. package/package.json +2 -1
@@ -1,17 +1,18 @@
1
1
  import * as $ from '@goscript/builtin/index.js'
2
+ import * as time from '@goscript/time/index.js'
2
3
 
3
4
  export const Canceled = $.newError('context canceled')
4
5
 
5
6
  export const DeadlineExceeded = $.newError('context deadline exceeded')
6
7
 
7
8
  // Function types
8
- export type CancelFunc = () => void
9
+ export type CancelFunc = (() => void) | null
9
10
  export type CancelCauseFunc = (cause: $.GoError) => void
10
11
 
11
12
  // Context interface matching Go's context.Context
12
13
  export type Context = null | {
13
14
  // Deadline returns the time when work done on behalf of this context should be canceled
14
- Deadline(): [Date | null, boolean]
15
+ Deadline(): [time.Time, boolean]
15
16
 
16
17
  // Done returns a channel that's closed when work done on behalf of this context should be canceled
17
18
  Done(): $.Channel<{}>
@@ -28,7 +29,7 @@ export type ContextNonNil = Exclude<Context, null>
28
29
 
29
30
  // Base implementation for all contexts
30
31
  abstract class baseContext implements ContextNonNil {
31
- abstract Deadline(): [Date | null, boolean]
32
+ abstract Deadline(): [time.Time, boolean]
32
33
  abstract Done(): $.Channel<{}>
33
34
  abstract Err(): $.GoError
34
35
  abstract Value(key: any): any
@@ -42,8 +43,8 @@ class backgroundContext extends baseContext {
42
43
  return backgroundContext.neverClosedChannel
43
44
  }
44
45
 
45
- Deadline(): [Date | null, boolean] {
46
- return [null, false]
46
+ Deadline(): [time.Time, boolean] {
47
+ return [new time.Time(), false]
47
48
  }
48
49
 
49
50
  Done(): $.Channel<{}> {
@@ -73,7 +74,7 @@ class valueContext extends baseContext {
73
74
  return this.parent
74
75
  }
75
76
 
76
- Deadline(): [Date | null, boolean] {
77
+ Deadline(): [time.Time, boolean] {
77
78
  return this.parent.Deadline()
78
79
  }
79
80
 
@@ -109,7 +110,7 @@ class cancelContext extends baseContext {
109
110
  this.doneChannel = $.makeChannel<{}>(0, {}, 'both')
110
111
  }
111
112
 
112
- Deadline(): [Date | null, boolean] {
113
+ Deadline(): [time.Time, boolean] {
113
114
  return this.parent.Deadline()
114
115
  }
115
116
 
@@ -195,21 +196,20 @@ class cancelContext extends baseContext {
195
196
 
196
197
  // Timer context with deadline
197
198
  class timerContext extends cancelContext {
198
- private deadline: Date
199
+ private deadline: time.Time
199
200
  private timer: any
200
201
 
201
- constructor(parent: ContextNonNil, deadline: Date) {
202
+ constructor(parent: ContextNonNil, deadline: time.Time) {
202
203
  super(parent)
203
- this.deadline = deadline
204
+ this.deadline = deadline.clone()
204
205
  }
205
206
 
206
- Deadline(): [Date | null, boolean] {
207
- return [this.deadline, true]
207
+ Deadline(): [time.Time, boolean] {
208
+ return [this.deadline.clone(), true]
208
209
  }
209
210
 
210
211
  startTimer(): void {
211
- const now = Date.now()
212
- const duration = this.deadline.getTime() - now
212
+ const duration = this.deadline.Sub(time.Now()) / 1000000
213
213
 
214
214
  if (duration <= 0) {
215
215
  // Already expired
@@ -237,8 +237,8 @@ class withoutCancelContext extends baseContext {
237
237
  super()
238
238
  }
239
239
 
240
- Deadline(): [Date | null, boolean] {
241
- return [null, false]
240
+ Deadline(): [time.Time, boolean] {
241
+ return [new time.Time(), false]
242
242
  }
243
243
 
244
244
  Done(): $.Channel<{}> {
@@ -305,7 +305,7 @@ export function WithCancelCause(
305
305
  // WithDeadline returns a copy of parent with the deadline adjusted to be no later than d
306
306
  export function WithDeadline(
307
307
  parent: Context,
308
- d: Date,
308
+ d: time.Time,
309
309
  ): [ContextNonNil, CancelFunc] {
310
310
  return WithDeadlineCause(parent, d, null)
311
311
  }
@@ -313,7 +313,7 @@ export function WithDeadline(
313
313
  // WithDeadlineCause is like WithDeadline but also sets the cause
314
314
  export function WithDeadlineCause(
315
315
  parent: Context,
316
- d: Date,
316
+ d: time.Time,
317
317
  cause: $.GoError,
318
318
  ): [ContextNonNil, CancelFunc] {
319
319
  if (parent === null) {
@@ -321,7 +321,7 @@ export function WithDeadlineCause(
321
321
  }
322
322
  // Check if parent deadline is already earlier
323
323
  const [parentDeadline, ok] = parent.Deadline()
324
- if (ok && parentDeadline && parentDeadline <= d) {
324
+ if (ok && (parentDeadline.Before(d) || parentDeadline.Equal(d))) {
325
325
  // Parent deadline is already sooner
326
326
  return WithCancel(parent)
327
327
  }
@@ -343,7 +343,7 @@ export function WithTimeout(
343
343
  parent: Context,
344
344
  timeout: number,
345
345
  ): [ContextNonNil, CancelFunc] {
346
- return WithDeadline(parent, new Date(Date.now() + timeout / 1000000))
346
+ return WithDeadline(parent, time.Now().Add(timeout))
347
347
  }
348
348
 
349
349
  // WithTimeoutCause is like WithTimeout but also sets the cause
@@ -352,11 +352,7 @@ export function WithTimeoutCause(
352
352
  timeout: number,
353
353
  cause: $.GoError,
354
354
  ): [ContextNonNil, CancelFunc] {
355
- return WithDeadlineCause(
356
- parent,
357
- new Date(Date.now() + timeout / 1000000),
358
- cause,
359
- )
355
+ return WithDeadlineCause(parent, time.Now().Add(timeout), cause)
360
356
  }
361
357
 
362
358
  // WithValue returns a copy of parent with the value associated with key
@@ -409,7 +405,7 @@ export function AfterFunc(ctx: Context, f: () => void): () => boolean {
409
405
  if (!stopped) {
410
406
  done = true
411
407
  // Run in next tick to simulate goroutine
412
- setImmediate(f)
408
+ queueMicrotask(f)
413
409
  }
414
410
  })()
415
411
 
@@ -0,0 +1 @@
1
+ export * from '@goscript/internal/byteorder/index.js'
@@ -0,0 +1 @@
1
+ export { New, Setting, Value } from '@goscript/internal/godebug/index.js'
@@ -0,0 +1,20 @@
1
+ import * as $ from '@goscript/builtin/index.js'
2
+ import * as fs from '@goscript/io/fs/index.js'
3
+
4
+ export class FS {
5
+ Open(name: string): [fs.File, $.GoError] {
6
+ return [null, pathError('open', name)]
7
+ }
8
+
9
+ ReadDir(name: string): [$.Slice<fs.DirEntry>, $.GoError] {
10
+ return [null, pathError('read', name)]
11
+ }
12
+
13
+ ReadFile(name: string): [Uint8Array, $.GoError] {
14
+ return [new Uint8Array(0), pathError('read', name)]
15
+ }
16
+ }
17
+
18
+ function pathError(op: string, name: string): $.GoError {
19
+ return new fs.PathError({ Op: op, Path: name, Err: fs.ErrNotExist })
20
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "dependencies": [
3
+ "io/fs"
4
+ ]
5
+ }
@@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest'
2
2
 
3
3
  import * as $ from '@goscript/builtin/index.js'
4
4
 
5
- import { Marshal, Unmarshal } from './index.js'
5
+ import { Marshal, MarshalIndent, Unmarshal } from './index.js'
6
6
 
7
7
  class Person {
8
8
  public _fields = {
@@ -39,6 +39,20 @@ describe('encoding/json override', () => {
39
39
  )
40
40
  })
41
41
 
42
+ it('marshals indented JSON with a line prefix', () => {
43
+ const person = new Person()
44
+ person._fields.Name.value = 'Alice'
45
+ person._fields.Age.value = 30
46
+ person._fields.Active.value = true
47
+
48
+ const [data, err] = MarshalIndent(person, '> ', ' ')
49
+
50
+ expect(err).toBeNull()
51
+ expect($.bytesToString(data)).toBe(
52
+ '{\n> "name": "Alice",\n> "age": 30,\n> "active": true\n> }',
53
+ )
54
+ })
55
+
42
56
  it('unmarshals into struct and map pointers', () => {
43
57
  const person = $.varRef(new Person())
44
58
  const personErr = Unmarshal(
@@ -8,6 +8,30 @@ export function Marshal(v: unknown): [$.Slice<number>, $.GoError] {
8
8
  }
9
9
  }
10
10
 
11
+ export function MarshalIndent(
12
+ v: unknown,
13
+ prefix: string,
14
+ indent: string,
15
+ ): [$.Slice<number>, $.GoError] {
16
+ try {
17
+ const text = JSON.stringify(marshalValue(v), null, indent)
18
+ if (prefix === '') {
19
+ return [$.stringToBytes(text), null]
20
+ }
21
+ return [
22
+ $.stringToBytes(
23
+ text
24
+ .split('\n')
25
+ .map((line, idx) => (idx === 0 ? line : prefix + line))
26
+ .join('\n'),
27
+ ),
28
+ null,
29
+ ]
30
+ } catch (err) {
31
+ return [null, goError(err)]
32
+ }
33
+ }
34
+
11
35
  export function Unmarshal(data: $.Slice<number>, v: unknown): $.GoError {
12
36
  try {
13
37
  assignDecodedValue(v, JSON.parse($.bytesToString(data)))
@@ -0,0 +1,82 @@
1
+ import { describe, expect, it } from 'vitest'
2
+
3
+ import * as $ from '@goscript/builtin/index.js'
4
+
5
+ import { AsType, Errorf, Is, Join, Wrap, Wrapf } from './errors.js'
6
+
7
+ class DNSError {
8
+ public readonly IsNotFound = true
9
+
10
+ public Error(): string {
11
+ return 'dns'
12
+ }
13
+ }
14
+
15
+ class Wrapper {
16
+ constructor(private readonly err: $.GoError) {}
17
+
18
+ public Error(): string {
19
+ return 'wrapped'
20
+ }
21
+
22
+ public Unwrap(): $.GoError {
23
+ return this.err
24
+ }
25
+ }
26
+
27
+ const dnsTypeArgs: $.GenericTypeArgs = {
28
+ E: {
29
+ type: { kind: $.TypeKind.Pointer, elemType: 'net.DNSError' },
30
+ zero: () => null,
31
+ },
32
+ }
33
+
34
+ describe('errors.AsType', () => {
35
+ it('returns a directly matching error', () => {
36
+ const dns = $.interfaceValue<$.GoError>(new DNSError(), '*net.DNSError')
37
+
38
+ const [matched, ok] = AsType(dnsTypeArgs, dns)
39
+
40
+ expect(ok).toBe(true)
41
+ expect(matched).toBe(dns)
42
+ })
43
+
44
+ it('walks wrapped errors depth first', () => {
45
+ const dns = $.interfaceValue<$.GoError>(new DNSError(), '*net.DNSError')
46
+ const wrapped = $.interfaceValue<$.GoError>(new Wrapper(dns), '*main.Wrapper')
47
+
48
+ const [matched, ok] = AsType(dnsTypeArgs, Join(null, wrapped))
49
+
50
+ expect(ok).toBe(true)
51
+ expect(matched).toBe(dns)
52
+ })
53
+
54
+ it('returns zero when no error matches', () => {
55
+ const [matched, ok] = AsType(dnsTypeArgs, $.newError('plain'))
56
+
57
+ expect(ok).toBe(false)
58
+ expect(matched).toBe(null)
59
+ })
60
+ })
61
+
62
+ describe('errors github.com/pkg/errors compatibility helpers', () => {
63
+ it('formats new errors', () => {
64
+ expect(Errorf('bad %s: %d', 'value', 42)?.Error()).toBe('bad value: 42')
65
+ })
66
+
67
+ it('wraps and unwraps errors', () => {
68
+ const base = $.newError('root')
69
+ const wrapped = Wrap(base, 'context')
70
+
71
+ expect(wrapped?.Error()).toBe('context: root')
72
+ expect(Is(wrapped, base)).toBe(true)
73
+ })
74
+
75
+ it('wraps formatted context and preserves nil', () => {
76
+ const base = $.newError('root')
77
+
78
+ expect(Wrapf(base, 'context %d', 7)?.Error()).toBe('context 7: root')
79
+ expect(Wrap(null, 'context')).toBe(null)
80
+ expect(Wrapf(null, 'context %d', 7)).toBe(null)
81
+ })
82
+ })
@@ -6,6 +6,57 @@ export function New(text: string): $.GoError {
6
6
  return $.newError(text)
7
7
  }
8
8
 
9
+ export function Errorf(format: string, ...args: any[]): $.GoError {
10
+ return New(formatText(format, args))
11
+ }
12
+
13
+ export function Wrap(err: $.GoError, message: string): $.GoError {
14
+ if (err === null) {
15
+ return null
16
+ }
17
+ return wrapError(err, message)
18
+ }
19
+
20
+ export function Wrapf(
21
+ err: $.GoError,
22
+ format: string,
23
+ ...args: any[]
24
+ ): $.GoError {
25
+ if (err === null) {
26
+ return null
27
+ }
28
+ return wrapError(err, formatText(format, args))
29
+ }
30
+
31
+ function wrapError(err: Exclude<$.GoError, null>, message: string): $.GoError {
32
+ const wrapped = $.newError(`${message}: ${err.Error()}`)
33
+ ;(wrapped as any).Unwrap = function (): $.GoError {
34
+ return err
35
+ }
36
+ return wrapped
37
+ }
38
+
39
+ function formatText(format: string, args: any[]): string {
40
+ let argIndex = 0
41
+ return format.replace(/%[sdqv%]/g, (match) => {
42
+ if (match === '%%') {
43
+ return '%'
44
+ }
45
+ if (argIndex >= args.length) {
46
+ return match
47
+ }
48
+ const arg = args[argIndex++]
49
+ switch (match) {
50
+ case '%d':
51
+ return String(Number(arg))
52
+ case '%q':
53
+ return JSON.stringify(String(arg))
54
+ default:
55
+ return String(arg)
56
+ }
57
+ })
58
+ }
59
+
9
60
  // ErrUnsupported indicates that a requested operation cannot be performed,
10
61
  // because it is unsupported. For example, a call to os.Link when using a
11
62
  // file system that does not support hard links.
@@ -179,6 +230,59 @@ export function As(err: $.GoError, target: any): boolean {
179
230
  return false
180
231
  }
181
232
 
233
+ // AsType finds the first error in err's tree that matches the generic error
234
+ // type E, returning the matching error and true. Otherwise it returns E's zero
235
+ // value and false.
236
+ export function AsType(
237
+ typeArgs: $.GenericTypeArgs | undefined,
238
+ err: $.GoError,
239
+ ): [any, boolean] {
240
+ const descriptor = typeArgs?.E
241
+ const zero = (): any => descriptor?.zero?.() ?? null
242
+ if (err === null || descriptor?.type === undefined) {
243
+ return [zero(), false]
244
+ }
245
+ return asType(err, descriptor.type, zero)
246
+ }
247
+
248
+ function asType(
249
+ err: $.GoError,
250
+ typeInfo: $.TypeInfo | string,
251
+ zero: () => any,
252
+ ): [any, boolean] {
253
+ if (err === null) {
254
+ return [zero(), false]
255
+ }
256
+
257
+ const [asserted, ok] = $.typeAssertTuple<any>(err, typeInfo)
258
+ if (ok) {
259
+ return [asserted, true]
260
+ }
261
+
262
+ if (typeof (err as any).Unwrap === 'function') {
263
+ const result = (err as any).Unwrap()
264
+ if (Array.isArray(result)) {
265
+ for (const wrappedErr of result) {
266
+ if (
267
+ wrappedErr &&
268
+ typeof wrappedErr.Error === 'function'
269
+ ) {
270
+ const [matched, matchedOK] = asType(wrappedErr, typeInfo, zero)
271
+ if (matchedOK) {
272
+ return [matched, true]
273
+ }
274
+ }
275
+ }
276
+ return [zero(), false]
277
+ }
278
+ if (result && typeof result.Error === 'function') {
279
+ return asType(result, typeInfo, zero)
280
+ }
281
+ }
282
+
283
+ return [zero(), false]
284
+ }
285
+
182
286
  // Join returns an error that wraps the given errors.
183
287
  // Any nil error values are discarded.
184
288
  // Join returns nil if every value in errs is nil.
package/gs/fmt/fmt.ts CHANGED
@@ -22,7 +22,7 @@ export interface State {
22
22
  Flag(c: number): boolean
23
23
  Precision(): [number, boolean]
24
24
  Width(): [number, boolean]
25
- Write(b: Uint8Array): [number, $.GoError | null]
25
+ Write(b: $.Bytes): [number, $.GoError | null]
26
26
  }
27
27
 
28
28
  // Simple printf-style formatting implementation
@@ -199,7 +199,11 @@ function parseFormat(format: string, args: any[]): string {
199
199
  const verb = format[j]
200
200
 
201
201
  if (argIndex < args.length) {
202
- let formatted = formatValue(args[argIndex], verb)
202
+ const arg = args[argIndex]
203
+ let formatted = formatWithState(arg, verb, flags, width, precision)
204
+ if (formatted === null) {
205
+ formatted = formatValue(arg, verb)
206
+ }
203
207
 
204
208
  // Apply width and precision formatting
205
209
  if (width && !precision) {
@@ -217,7 +221,7 @@ function parseFormat(format: string, args: any[]): string {
217
221
  (verb === 'f' || verb === 'e' || verb === 'g')
218
222
  ) {
219
223
  const p = parseInt(precision)
220
- const num = Number(args[argIndex])
224
+ const num = Number(arg)
221
225
  if (verb === 'f') {
222
226
  formatted = num.toFixed(p)
223
227
  } else if (verb === 'e') {
@@ -257,6 +261,39 @@ function parseFormat(format: string, args: any[]): string {
257
261
  return result
258
262
  }
259
263
 
264
+ function formatWithState(
265
+ value: any,
266
+ verb: string,
267
+ flags: string,
268
+ width: string,
269
+ precision: string,
270
+ ): string | null {
271
+ if (!value || typeof value.Format !== 'function') {
272
+ return null
273
+ }
274
+
275
+ let out = ''
276
+ const state: State = {
277
+ Flag(c: number): boolean {
278
+ return flags.includes(String.fromCharCode(c))
279
+ },
280
+ Precision(): [number, boolean] {
281
+ return precision === '' ? [0, false] : [parseInt(precision), true]
282
+ },
283
+ Width(): [number, boolean] {
284
+ return width === '' ? [0, false] : [parseInt(width), true]
285
+ },
286
+ Write(b: $.Bytes): [number, $.GoError | null] {
287
+ const text = $.bytesToString(b)
288
+ out += text
289
+ return [text.length, null]
290
+ },
291
+ }
292
+
293
+ value.Format(state, verb.codePointAt(0) ?? 0)
294
+ return out
295
+ }
296
+
260
297
  // Global stdout simulation for Print functions
261
298
  let stdout = {
262
299
  write: (data: string) => {
@@ -361,34 +398,37 @@ export function Fprintln(w: any, ...a: any[]): [number, $.GoError | null] {
361
398
  }
362
399
 
363
400
  // Append functions (append to byte slice)
364
- export function Append(b: Uint8Array, ...a: any[]): Uint8Array {
401
+ export function Append(b: $.Bytes, ...a: any[]): $.Bytes {
365
402
  const result = a.map(defaultFormat).join(' ')
366
403
  const encoded = new TextEncoder().encode(result)
367
- const newArray = new Uint8Array(b.length + encoded.length)
368
- newArray.set(b)
369
- newArray.set(encoded, b.length)
404
+ const base = $.bytesToUint8Array(b)
405
+ const newArray = new Uint8Array(base.length + encoded.length)
406
+ newArray.set(base)
407
+ newArray.set(encoded, base.length)
370
408
  return newArray
371
409
  }
372
410
 
373
411
  export function Appendf(
374
- b: Uint8Array,
412
+ b: $.Bytes,
375
413
  format: string,
376
414
  ...a: any[]
377
- ): Uint8Array {
415
+ ): $.Bytes {
378
416
  const result = parseFormat(format, a)
379
417
  const encoded = new TextEncoder().encode(result)
380
- const newArray = new Uint8Array(b.length + encoded.length)
381
- newArray.set(b)
382
- newArray.set(encoded, b.length)
418
+ const base = $.bytesToUint8Array(b)
419
+ const newArray = new Uint8Array(base.length + encoded.length)
420
+ newArray.set(base)
421
+ newArray.set(encoded, base.length)
383
422
  return newArray
384
423
  }
385
424
 
386
- export function Appendln(b: Uint8Array, ...a: any[]): Uint8Array {
425
+ export function Appendln(b: $.Bytes, ...a: any[]): $.Bytes {
387
426
  const result = a.map(defaultFormat).join(' ') + '\n'
388
427
  const encoded = new TextEncoder().encode(result)
389
- const newArray = new Uint8Array(b.length + encoded.length)
390
- newArray.set(b)
391
- newArray.set(encoded, b.length)
428
+ const base = $.bytesToUint8Array(b)
429
+ const newArray = new Uint8Array(base.length + encoded.length)
430
+ newArray.set(base)
431
+ newArray.set(encoded, base.length)
392
432
  return newArray
393
433
  }
394
434
 
@@ -1,6 +1,21 @@
1
1
  import { describe, expect, it } from 'vitest'
2
2
 
3
- import { CompareEqualVT } from './index.js'
3
+ import {
4
+ AppendVarint,
5
+ CompareEqualVT,
6
+ DecodeFixed32,
7
+ DecodeFixed64,
8
+ DecodeVarint,
9
+ DecodeVarintInt32,
10
+ DecodeVarintInt64,
11
+ DecodeVarintUint32,
12
+ EncodeVarint,
13
+ ErrIntOverflow,
14
+ ErrInvalidLength,
15
+ ErrUnexpectedEndOfGroup,
16
+ SizeOfVarint,
17
+ Skip,
18
+ } from './index.js'
4
19
 
5
20
  class TestValue {
6
21
  constructor(private readonly value: string) {}
@@ -21,3 +36,60 @@ describe('protobuf-go-lite EqualVT helpers', () => {
21
36
  expect(equal(null, null)).toBe(true)
22
37
  })
23
38
  })
39
+
40
+ describe('protobuf-go-lite wire helpers', () => {
41
+ it('encodes and decodes varints', () => {
42
+ const buf = new Uint8Array(4)
43
+
44
+ const offset = EncodeVarint(buf, 4, 300)
45
+
46
+ expect(offset).toBe(2)
47
+ expect(Array.from(buf.slice(offset))).toEqual([0xac, 0x02])
48
+ expect(SizeOfVarint(300)).toBe(2)
49
+ expect(DecodeVarint(buf, offset)).toEqual([300, 4, null])
50
+ expect(DecodeVarintInt32(buf, offset)).toEqual([300, 4, null])
51
+ expect(DecodeVarintInt64(buf, offset)).toEqual([300, 4, null])
52
+ expect(DecodeVarintUint32(buf, offset)).toEqual([300, 4, null])
53
+ expect(Array.from(AppendVarint([], 300) as number[])).toEqual([
54
+ 0xac,
55
+ 0x02,
56
+ ])
57
+ })
58
+
59
+ it('decodes fixed64 values', () => {
60
+ expect(DecodeFixed32(new Uint8Array([0x44, 0x33, 0x22, 0x11]), 0)).toEqual([
61
+ 0x11223344,
62
+ 4,
63
+ null,
64
+ ])
65
+
66
+ expect(
67
+ DecodeFixed64(
68
+ new Uint8Array([0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11]),
69
+ 0,
70
+ ),
71
+ ).toEqual([0x1122334455667800, 8, null])
72
+
73
+ const [, , err] = DecodeFixed64(new Uint8Array([1, 2, 3]), 0)
74
+ expect(err?.Error()).toBe('unexpected EOF')
75
+ })
76
+
77
+ it('skips protobuf records', () => {
78
+ expect(Skip(new Uint8Array([0x08, 0x96, 0x01]))).toEqual([3, null])
79
+ expect(Skip(new Uint8Array([0x12, 0x03, 0x61, 0x62, 0x63]))).toEqual([
80
+ 5,
81
+ null,
82
+ ])
83
+ expect(Skip(new Uint8Array([0x0c]))).toEqual([
84
+ 0,
85
+ ErrUnexpectedEndOfGroup,
86
+ ])
87
+ })
88
+
89
+ it('reports protobuf wire errors as Go errors', () => {
90
+ expect(ErrInvalidLength?.Error()).toContain('negative length')
91
+ expect(ErrIntOverflow?.Error()).toContain('integer overflow')
92
+ const [, eof] = Skip(new Uint8Array([0x08, 0x80]))
93
+ expect(eof?.Error()).toBe('unexpected EOF')
94
+ })
95
+ })