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,8 +1,58 @@
1
+ import * as $ from '../../../builtin/index.js'
2
+
3
+ export const ErrInvalidLength = $.newError(
4
+ 'proto: negative length found during unmarshaling',
5
+ )
6
+ export const ErrIntOverflow = $.newError('proto: integer overflow')
7
+ export const ErrUnexpectedEndOfGroup = $.newError(
8
+ 'proto: unexpected end of group',
9
+ )
10
+
11
+ export interface Message {
12
+ SizeVT(): number
13
+ MarshalToSizedBufferVT(dAtA: $.Slice<number>): [number, $.GoError]
14
+ MarshalVT(): [$.Slice<number>, $.GoError]
15
+ UnmarshalVT(data: $.Slice<number>): $.GoError
16
+ Reset(): void
17
+ }
18
+
19
+ export interface JSONMessage {
20
+ MarshalJSON(): [$.Slice<number>, $.GoError]
21
+ UnmarshalJSON(data: $.Slice<number>): $.GoError
22
+ }
23
+
24
+ export interface CloneMessage extends Message {
25
+ CloneMessageVT(): CloneMessage | null
26
+ }
27
+
28
+ export interface CloneVT<T> extends CloneMessage {
29
+ CloneVT(): T
30
+ }
31
+
1
32
  export interface EqualVT<T> {
2
33
  EqualVT(other: T): boolean
3
34
  }
4
35
 
5
- export function IsEqualVT<T extends EqualVT<T>>(t1: T | null, t2: T | null): boolean {
36
+ export function CompareComparable<T>(): (t1: T, t2: T) => boolean {
37
+ return (t1, t2) => t1 === t2
38
+ }
39
+
40
+ export function IsEqualVT<T extends EqualVT<T>>(
41
+ t1: T | null,
42
+ t2: T | null,
43
+ ): boolean
44
+ export function IsEqualVT<T extends EqualVT<T>>(
45
+ _typeArgs: unknown,
46
+ t1: T | null,
47
+ t2: T | null,
48
+ ): boolean
49
+ export function IsEqualVT<T extends EqualVT<T>>(
50
+ arg0: unknown,
51
+ arg1: T | null,
52
+ arg2?: T | null,
53
+ ): boolean {
54
+ const t1 = arg2 === undefined ? (arg0 as T | null) : arg1
55
+ const t2 = arg2 === undefined ? arg1 : arg2
6
56
  if (t1 == null || t2 == null) {
7
57
  return t1 == t2
8
58
  }
@@ -14,3 +64,249 @@ export function CompareEqualVT<T extends EqualVT<T>>(
14
64
  ): (t1: T | null, t2: T | null) => boolean {
15
65
  return (t1, t2) => IsEqualVT(t1, t2)
16
66
  }
67
+
68
+ export function IsEqualVTSlice<T extends EqualVT<T>>(
69
+ s1: $.Slice<T>,
70
+ s2: $.Slice<T>,
71
+ ): boolean {
72
+ if ($.len(s1) !== $.len(s2)) {
73
+ return false
74
+ }
75
+ for (let i = 0; i < $.len(s1); i++) {
76
+ if (!IsEqualVT((s1 as any)[i] as T, (s2 as any)[i] as T)) {
77
+ return false
78
+ }
79
+ }
80
+ return true
81
+ }
82
+
83
+ export function EncodeVarint(
84
+ dAtA: $.Slice<number>,
85
+ offset: number,
86
+ v: number,
87
+ ): number {
88
+ offset -= SizeOfVarint(v)
89
+ const base = offset
90
+ while (v >= 0x80) {
91
+ setByte(dAtA, offset, (v % 0x80) | 0x80)
92
+ v = Math.floor(v / 0x80)
93
+ offset++
94
+ }
95
+ setByte(dAtA, offset, v)
96
+ return base
97
+ }
98
+
99
+ export function AppendVarint(b: $.Slice<number>, v: number): $.Slice<number> {
100
+ const bytes: number[] = []
101
+ while (v >= 0x80) {
102
+ bytes.push((v % 0x80) | 0x80)
103
+ v = Math.floor(v / 0x80)
104
+ }
105
+ bytes.push(v)
106
+ return $.append(b, ...bytes)
107
+ }
108
+
109
+ export function ConsumeVarint(b: $.Slice<number>): [number, number] {
110
+ let v = 0
111
+ let shift = 0
112
+ for (let i = 0; i < 10; i++) {
113
+ if (i >= $.len(b)) {
114
+ return [0, -1]
115
+ }
116
+ const value = byteSliceValue(b, i)
117
+ if (shift === 63 && value > 1) {
118
+ return [0, -2]
119
+ }
120
+ v += (value & 0x7f) * 2 ** shift
121
+ if (value < 0x80) {
122
+ return [v, i + 1]
123
+ }
124
+ shift += 7
125
+ }
126
+ return [0, -2]
127
+ }
128
+
129
+ export function SizeOfVarint(x: number): number {
130
+ let n = 1
131
+ while (x >= 0x80) {
132
+ x = Math.floor(x / 0x80)
133
+ n++
134
+ }
135
+ return n
136
+ }
137
+
138
+ export function DecodeVarint(
139
+ b: $.Slice<number>,
140
+ idx: number,
141
+ ): [number, number, $.GoError] {
142
+ const [v, n] = ConsumeVarint($.goSlice(b, idx, undefined))
143
+ if (n < 0) {
144
+ return [0, 0, n === -1 ? ioUnexpectedEOF() : ErrIntOverflow]
145
+ }
146
+ return [v, idx + n, null]
147
+ }
148
+
149
+ export function DecodeVarintInt32(
150
+ b: $.Slice<number>,
151
+ idx: number,
152
+ ): [number, number, $.GoError] {
153
+ const [v, next, err] = DecodeVarint(b, idx)
154
+ return [toInt32(v), next, err]
155
+ }
156
+
157
+ export function DecodeVarintInt64(
158
+ b: $.Slice<number>,
159
+ idx: number,
160
+ ): [number, number, $.GoError] {
161
+ return DecodeVarint(b, idx)
162
+ }
163
+
164
+ export function DecodeVarintUint32(
165
+ b: $.Slice<number>,
166
+ idx: number,
167
+ ): [number, number, $.GoError] {
168
+ const [v, next, err] = DecodeVarint(b, idx)
169
+ return [v >>> 0, next, err]
170
+ }
171
+
172
+ export function DecodeFixed32(
173
+ b: $.Slice<number>,
174
+ idx: number,
175
+ ): [number, number, $.GoError] {
176
+ if (idx + 4 > $.len(b)) {
177
+ return [0, 0, ioUnexpectedEOF()]
178
+ }
179
+ const value =
180
+ byteSliceValue(b, idx) +
181
+ byteSliceValue(b, idx + 1) * 2 ** 8 +
182
+ byteSliceValue(b, idx + 2) * 2 ** 16 +
183
+ byteSliceValue(b, idx + 3) * 2 ** 24
184
+ return [value >>> 0, idx + 4, null]
185
+ }
186
+
187
+ export function DecodeFixed64(
188
+ b: $.Slice<number>,
189
+ idx: number,
190
+ ): [number, number, $.GoError] {
191
+ if (idx + 8 > $.len(b)) {
192
+ return [0, 0, ioUnexpectedEOF()]
193
+ }
194
+ const low =
195
+ byteSliceValue(b, idx) +
196
+ byteSliceValue(b, idx + 1) * 2 ** 8 +
197
+ byteSliceValue(b, idx + 2) * 2 ** 16 +
198
+ byteSliceValue(b, idx + 3) * 2 ** 24
199
+ const high =
200
+ byteSliceValue(b, idx + 4) +
201
+ byteSliceValue(b, idx + 5) * 2 ** 8 +
202
+ byteSliceValue(b, idx + 6) * 2 ** 16 +
203
+ byteSliceValue(b, idx + 7) * 2 ** 24
204
+ return [low + high * 2 ** 32, idx + 8, null]
205
+ }
206
+
207
+ export function Skip(dAtA: $.Slice<number>): [number, $.GoError] {
208
+ const l = $.len(dAtA)
209
+ let iNdEx = 0
210
+ let depth = 0
211
+ while (iNdEx < l) {
212
+ let wire = 0
213
+ for (let shift = 0; ; shift += 7) {
214
+ if (shift >= 64) {
215
+ return [0, ErrIntOverflow]
216
+ }
217
+ if (iNdEx >= l) {
218
+ return [0, ioUnexpectedEOF()]
219
+ }
220
+ const b = byteSliceValue(dAtA, iNdEx)
221
+ iNdEx++
222
+ wire += (b & 0x7f) * 2 ** shift
223
+ if (b < 0x80) {
224
+ break
225
+ }
226
+ }
227
+
228
+ const wireType = wire & 0x7
229
+ switch (wireType) {
230
+ case 0:
231
+ for (let shift = 0; ; shift += 7) {
232
+ if (shift >= 64) {
233
+ return [0, ErrIntOverflow]
234
+ }
235
+ if (iNdEx >= l) {
236
+ return [0, ioUnexpectedEOF()]
237
+ }
238
+ const b = byteSliceValue(dAtA, iNdEx)
239
+ iNdEx++
240
+ if (b < 0x80) {
241
+ break
242
+ }
243
+ }
244
+ break
245
+ case 1:
246
+ iNdEx += 8
247
+ break
248
+ case 2: {
249
+ let length = 0
250
+ for (let shift = 0; ; shift += 7) {
251
+ if (shift >= 64) {
252
+ return [0, ErrIntOverflow]
253
+ }
254
+ if (iNdEx >= l) {
255
+ return [0, ioUnexpectedEOF()]
256
+ }
257
+ const b = byteSliceValue(dAtA, iNdEx)
258
+ iNdEx++
259
+ length += (b & 0x7f) * 2 ** shift
260
+ if (b < 0x80) {
261
+ break
262
+ }
263
+ }
264
+ if (length < 0) {
265
+ return [0, ErrInvalidLength]
266
+ }
267
+ iNdEx += length
268
+ break
269
+ }
270
+ case 3:
271
+ depth++
272
+ break
273
+ case 4:
274
+ if (depth === 0) {
275
+ return [0, ErrUnexpectedEndOfGroup]
276
+ }
277
+ depth--
278
+ break
279
+ case 5:
280
+ iNdEx += 4
281
+ break
282
+ default:
283
+ return [0, $.newError(`proto: illegal wireType ${wireType}`)]
284
+ }
285
+ if (iNdEx < 0) {
286
+ return [0, ErrInvalidLength]
287
+ }
288
+ if (depth === 0) {
289
+ return [iNdEx, null]
290
+ }
291
+ }
292
+ return [0, ioUnexpectedEOF()]
293
+ }
294
+
295
+ function byteSliceValue(b: $.Slice<number>, idx: number): number {
296
+ return $.indexStringOrBytes(b, idx)
297
+ }
298
+
299
+ function setByte(b: $.Slice<number>, idx: number, value: number): void {
300
+ if (b == null) {
301
+ throw new Error('assignment to nil byte slice')
302
+ }
303
+ b[idx] = value & 0xff
304
+ }
305
+
306
+ function toInt32(v: number): number {
307
+ return v | 0
308
+ }
309
+
310
+ function ioUnexpectedEOF(): $.GoError {
311
+ return $.newError('unexpected EOF')
312
+ }
@@ -0,0 +1,159 @@
1
+ import { describe, expect, it } from 'vitest'
2
+
3
+ import * as $ from '../../../../builtin/index.js'
4
+ import * as time from '../../../../time/index.js'
5
+ import {
6
+ DefaultMarshalerConfig,
7
+ DefaultUnmarshalerConfig,
8
+ JsonStream,
9
+ MarshalMap,
10
+ MarshalSlice,
11
+ MarshalState,
12
+ NewUnmarshalState,
13
+ UnmarshalState,
14
+ } from './index.js'
15
+
16
+ function jsonBytes(value: string): $.Slice<number> {
17
+ return $.arrayToSlice(Array.from(new TextEncoder().encode(value)))
18
+ }
19
+
20
+ class TestMessage {
21
+ constructor(
22
+ public name = '',
23
+ public count = 0,
24
+ ) {}
25
+
26
+ MarshalProtoJSON(s: MarshalState | null): void {
27
+ s?.WriteObjectStart()
28
+ s?.WriteObjectField('name')
29
+ s?.WriteString(this.name)
30
+ s?.WriteMore()
31
+ s?.WriteObjectField('count')
32
+ s?.WriteInt64(this.count)
33
+ s?.WriteObjectEnd()
34
+ }
35
+
36
+ UnmarshalProtoJSON(s: UnmarshalState | null): void {
37
+ s?.ReadObject((key) => {
38
+ switch (key) {
39
+ case 'name':
40
+ this.name = s.ReadString()
41
+ return
42
+ case 'count':
43
+ this.count = s.ReadInt64()
44
+ return
45
+ default:
46
+ s.Skip()
47
+ }
48
+ })
49
+ }
50
+ }
51
+
52
+ describe('protobuf-go-lite/json override', () => {
53
+ it('marshals scalar values with protobuf JSON semantics', () => {
54
+ const [data, err] = DefaultMarshalerConfig.Marshal({
55
+ MarshalProtoJSON(s) {
56
+ s?.WriteObjectStart()
57
+ s?.WriteObjectField('bytes')
58
+ s?.WriteBytes(new Uint8Array([102, 111, 111, 98]))
59
+ s?.WriteMore()
60
+ s?.WriteObjectField('int64')
61
+ s?.WriteInt64(-12)
62
+ s?.WriteMore()
63
+ s?.WriteObjectField('uint64')
64
+ s?.WriteUint64(12)
65
+ s?.WriteObjectEnd()
66
+ },
67
+ })
68
+
69
+ expect(err).toBeNull()
70
+ expect(new TextDecoder().decode(new Uint8Array(data ?? []))).toBe(
71
+ '{"bytes":"Zm9vYg==","int64":"-12","uint64":"12"}',
72
+ )
73
+ })
74
+
75
+ it('marshals map and slice containers', () => {
76
+ const [mapData, mapErr] = MarshalMap(undefined, DefaultMarshalerConfig, {
77
+ c: new TestMessage('three', 3),
78
+ a: new TestMessage('one', 1),
79
+ b: new TestMessage('two', 2),
80
+ })
81
+ const [sliceData, sliceErr] = MarshalSlice(undefined, DefaultMarshalerConfig, [
82
+ new TestMessage('one', 1),
83
+ new TestMessage('two', 2),
84
+ ])
85
+
86
+ expect(mapErr).toBeNull()
87
+ expect(sliceErr).toBeNull()
88
+ expect(new TextDecoder().decode(new Uint8Array(mapData ?? []))).toBe(
89
+ '{"a":{"name":"one","count":"1"},"b":{"name":"two","count":"2"},"c":{"name":"three","count":"3"}}',
90
+ )
91
+ expect(new TextDecoder().decode(new Uint8Array(sliceData ?? []))).toBe(
92
+ '[{"name":"one","count":"1"},{"name":"two","count":"2"}]',
93
+ )
94
+ })
95
+
96
+ it('unmarshals object fields and repeated scalar arrays', () => {
97
+ const msg = new TestMessage()
98
+ const err = DefaultUnmarshalerConfig.Unmarshal(
99
+ jsonBytes('{"name":"ok","count":"42"}'),
100
+ msg,
101
+ )
102
+ const state = NewUnmarshalState(jsonBytes('["Zm9vYg==","YXI="]'), DefaultUnmarshalerConfig)
103
+
104
+ expect(err).toBeNull()
105
+ expect(msg).toEqual(new TestMessage('ok', 42))
106
+ expect(state?.ReadBytesArray()).toEqual([
107
+ [102, 111, 111, 98],
108
+ [97, 114],
109
+ ])
110
+ })
111
+
112
+ it('tracks unmarshaled field masks through nested fields', () => {
113
+ const state = NewUnmarshalState(jsonBytes('{}'), DefaultUnmarshalerConfig)
114
+ state?.AddField('top')
115
+ state?.WithField('nested')?.AddField('leaf')
116
+ state?.WithField('ignored', false)?.AddField('leaf')
117
+
118
+ expect(state?.FieldMask()?.GetPaths()).toEqual(['top', 'nested.leaf'])
119
+ })
120
+
121
+ it('accepts raw wrapped values and object wrapped values', () => {
122
+ const raw = NewUnmarshalState(jsonBytes('"abc"'), DefaultUnmarshalerConfig)
123
+ const object = NewUnmarshalState(jsonBytes('{"value": "def"}'), DefaultUnmarshalerConfig)
124
+
125
+ expect(raw?.ReadWrappedString()).toBe('abc')
126
+ expect(raw?.Err()).toBeNull()
127
+ expect(object?.ReadWrappedString()).toBe('def')
128
+ expect(object?.Err()).toBeNull()
129
+ })
130
+
131
+ it('reads and writes protobuf timestamp values as time.Time pointers', () => {
132
+ const state = NewUnmarshalState(jsonBytes('"2025-05-15T01:10:42Z"'), DefaultUnmarshalerConfig)
133
+ const parsed = state?.ReadTime()
134
+ const jsonStream = new JsonStream()
135
+ const stream = new MarshalState({ stream: jsonStream })
136
+
137
+ expect(state?.Err()).toBeNull()
138
+ expect($.pointerValue(parsed)?.Format(time.RFC3339)).toBe('2025-05-15T01:10:42Z')
139
+
140
+ stream.WriteTime(parsed ?? null)
141
+
142
+ expect(stream.Err()).toBeNull()
143
+ expect(jsonStream.String()).toBe('"2025-05-15T01:10:42Z"')
144
+ })
145
+
146
+ it('rejects invalid bool and numeric map keys', () => {
147
+ const boolState = NewUnmarshalState(jsonBytes('{"yes": 1}'), DefaultUnmarshalerConfig)
148
+ const intState = NewUnmarshalState(jsonBytes('{"1.5": 1}'), DefaultUnmarshalerConfig)
149
+ const uintState = NewUnmarshalState(jsonBytes('{"-1": 1}'), DefaultUnmarshalerConfig)
150
+
151
+ boolState?.ReadBoolMap(() => {})
152
+ intState?.ReadInt32Map(() => {})
153
+ uintState?.ReadUint32Map(() => {})
154
+
155
+ expect(boolState?.Err()?.Error()).toContain('invalid map key')
156
+ expect(intState?.Err()?.Error()).toContain('invalid map key')
157
+ expect(uintState?.Err()?.Error()).toContain('invalid map key')
158
+ })
159
+ })