goscript 0.0.35 → 0.0.37

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/compiler/expr-call.go +26 -0
  2. package/compiler/spec-struct.go +41 -8
  3. package/compiler/spec-value.go +4 -0
  4. package/compiler/spec.go +1 -1
  5. package/compiler/stmt.go +26 -8
  6. package/dist/gs/internal/byteorder/index.d.ts +8 -0
  7. package/dist/gs/internal/byteorder/index.js +34 -0
  8. package/dist/gs/internal/byteorder/index.js.map +1 -0
  9. package/dist/gs/internal/testlog/index.d.ts +1 -0
  10. package/dist/gs/internal/testlog/index.js +5 -0
  11. package/dist/gs/internal/testlog/index.js.map +1 -0
  12. package/dist/gs/maps/iter.d.ts +1 -1
  13. package/dist/gs/os/dir.gs.d.ts +6 -0
  14. package/dist/gs/os/dir.gs.js +35 -0
  15. package/dist/gs/os/dir.gs.js.map +1 -0
  16. package/dist/gs/os/dir_unix.gs.d.ts +7 -0
  17. package/dist/gs/os/dir_unix.gs.js +113 -0
  18. package/dist/gs/os/dir_unix.gs.js.map +1 -0
  19. package/dist/gs/os/dirent_js.gs.d.ts +5 -0
  20. package/dist/gs/os/dirent_js.gs.js +20 -0
  21. package/dist/gs/os/dirent_js.gs.js.map +1 -0
  22. package/dist/gs/os/env.gs.d.ts +12 -0
  23. package/dist/gs/os/env.gs.js +157 -0
  24. package/dist/gs/os/env.gs.js.map +1 -0
  25. package/dist/gs/os/error.gs.d.ts +40 -0
  26. package/dist/gs/os/error.gs.js +158 -0
  27. package/dist/gs/os/error.gs.js.map +1 -0
  28. package/dist/gs/os/error_errno.gs.d.ts +1 -0
  29. package/dist/gs/os/error_errno.gs.js +5 -0
  30. package/dist/gs/os/error_errno.gs.js.map +1 -0
  31. package/dist/gs/os/exec.gs.d.ts +60 -0
  32. package/dist/gs/os/exec.gs.js +136 -0
  33. package/dist/gs/os/exec.gs.js.map +1 -0
  34. package/dist/gs/os/exec_nohandle.gs.d.ts +1 -0
  35. package/dist/gs/os/exec_nohandle.gs.js +2 -0
  36. package/dist/gs/os/exec_nohandle.gs.js.map +1 -0
  37. package/dist/gs/os/exec_posix.gs.d.ts +25 -0
  38. package/dist/gs/os/exec_posix.gs.js +67 -0
  39. package/dist/gs/os/exec_posix.gs.js.map +1 -0
  40. package/dist/gs/os/exec_unix.gs.d.ts +4 -0
  41. package/dist/gs/os/exec_unix.gs.js +46 -0
  42. package/dist/gs/os/exec_unix.gs.js.map +1 -0
  43. package/dist/gs/os/executable.gs.d.ts +2 -0
  44. package/dist/gs/os/executable.gs.js +16 -0
  45. package/dist/gs/os/executable.gs.js.map +1 -0
  46. package/dist/gs/os/executable_js.gs.d.ts +2 -0
  47. package/dist/gs/os/executable_js.gs.js +5 -0
  48. package/dist/gs/os/executable_js.gs.js.map +1 -0
  49. package/dist/gs/os/executable_wasm.gs.d.ts +2 -0
  50. package/dist/gs/os/executable_wasm.gs.js +6 -0
  51. package/dist/gs/os/executable_wasm.gs.js.map +1 -0
  52. package/dist/gs/os/file_constants_js.gs.d.ts +41 -0
  53. package/dist/gs/os/file_constants_js.gs.js +78 -0
  54. package/dist/gs/os/file_constants_js.gs.js.map +1 -0
  55. package/dist/gs/os/file_js.gs.d.ts +22 -0
  56. package/dist/gs/os/file_js.gs.js +73 -0
  57. package/dist/gs/os/file_js.gs.js.map +1 -0
  58. package/dist/gs/os/file_open_unix.gs.d.ts +6 -0
  59. package/dist/gs/os/file_open_unix.gs.js +10 -0
  60. package/dist/gs/os/file_open_unix.gs.js.map +1 -0
  61. package/dist/gs/os/file_posix_js.gs.d.ts +8 -0
  62. package/dist/gs/os/file_posix_js.gs.js +32 -0
  63. package/dist/gs/os/file_posix_js.gs.js.map +1 -0
  64. package/dist/gs/os/file_unix_js.gs.d.ts +27 -0
  65. package/dist/gs/os/file_unix_js.gs.js +64 -0
  66. package/dist/gs/os/file_unix_js.gs.js.map +1 -0
  67. package/dist/gs/os/getwd_js.gs.d.ts +6 -0
  68. package/dist/gs/os/getwd_js.gs.js +21 -0
  69. package/dist/gs/os/getwd_js.gs.js.map +1 -0
  70. package/dist/gs/os/index.d.ts +19 -0
  71. package/dist/gs/os/index.js +20 -0
  72. package/dist/gs/os/index.js.map +1 -0
  73. package/dist/gs/os/path.gs.d.ts +4 -0
  74. package/dist/gs/os/path.gs.js +32 -0
  75. package/dist/gs/os/path.gs.js.map +1 -0
  76. package/dist/gs/os/path_unix.gs.d.ts +4 -0
  77. package/dist/gs/os/path_unix.gs.js +40 -0
  78. package/dist/gs/os/path_unix.gs.js.map +1 -0
  79. package/dist/gs/os/pidfd_js.gs.d.ts +6 -0
  80. package/dist/gs/os/pidfd_js.gs.js +14 -0
  81. package/dist/gs/os/pidfd_js.gs.js.map +1 -0
  82. package/dist/gs/os/pipe_wasm.gs.d.ts +2 -0
  83. package/dist/gs/os/pipe_wasm.gs.js +14 -0
  84. package/dist/gs/os/pipe_wasm.gs.js.map +1 -0
  85. package/dist/gs/os/proc.gs.d.ts +11 -0
  86. package/dist/gs/os/proc.gs.js +68 -0
  87. package/dist/gs/os/proc.gs.js.map +1 -0
  88. package/dist/gs/os/proc_js.gs.d.ts +3 -0
  89. package/dist/gs/os/proc_js.gs.js +12 -0
  90. package/dist/gs/os/proc_js.gs.js.map +1 -0
  91. package/dist/gs/os/rawconn_js.gs.d.ts +10 -0
  92. package/dist/gs/os/rawconn_js.gs.js +26 -0
  93. package/dist/gs/os/rawconn_js.gs.js.map +1 -0
  94. package/dist/gs/os/removeall_js.gs.d.ts +2 -0
  95. package/dist/gs/os/removeall_js.gs.js +7 -0
  96. package/dist/gs/os/removeall_js.gs.js.map +1 -0
  97. package/dist/gs/os/root_js.gs.d.ts +23 -0
  98. package/dist/gs/os/root_js.gs.js +80 -0
  99. package/dist/gs/os/root_js.gs.js.map +1 -0
  100. package/dist/gs/os/root_nonwindows.gs.d.ts +2 -0
  101. package/dist/gs/os/root_nonwindows.gs.js +4 -0
  102. package/dist/gs/os/root_nonwindows.gs.js.map +1 -0
  103. package/dist/gs/os/root_noopenat.gs.d.ts +11 -0
  104. package/dist/gs/os/root_noopenat.gs.js +31 -0
  105. package/dist/gs/os/root_noopenat.gs.js.map +1 -0
  106. package/dist/gs/os/stat.gs.d.ts +24 -0
  107. package/dist/gs/os/stat.gs.js +20 -0
  108. package/dist/gs/os/stat.gs.js.map +1 -0
  109. package/dist/gs/os/stat_js.gs.d.ts +6 -0
  110. package/dist/gs/os/stat_js.gs.js +23 -0
  111. package/dist/gs/os/stat_js.gs.js.map +1 -0
  112. package/dist/gs/os/stat_unix_js.gs.d.ts +8 -0
  113. package/dist/gs/os/stat_unix_js.gs.js +16 -0
  114. package/dist/gs/os/stat_unix_js.gs.js.map +1 -0
  115. package/dist/gs/os/sticky_bsd.gs.d.ts +1 -0
  116. package/dist/gs/os/sticky_bsd.gs.js +3 -0
  117. package/dist/gs/os/sticky_bsd.gs.js.map +1 -0
  118. package/dist/gs/os/sys.gs.d.ts +2 -0
  119. package/dist/gs/os/sys.gs.js +10 -0
  120. package/dist/gs/os/sys.gs.js.map +1 -0
  121. package/dist/gs/os/sys_bsd.gs.d.ts +2 -0
  122. package/dist/gs/os/sys_bsd.gs.js +15 -0
  123. package/dist/gs/os/sys_bsd.gs.js.map +1 -0
  124. package/dist/gs/os/sys_js.gs.d.ts +2 -0
  125. package/dist/gs/os/sys_js.gs.js +6 -0
  126. package/dist/gs/os/sys_js.gs.js.map +1 -0
  127. package/dist/gs/os/tempfile.gs.d.ts +5 -0
  128. package/dist/gs/os/tempfile.gs.js +30 -0
  129. package/dist/gs/os/tempfile.gs.js.map +1 -0
  130. package/dist/gs/os/types.gs.d.ts +59 -0
  131. package/dist/gs/os/types.gs.js +123 -0
  132. package/dist/gs/os/types.gs.js.map +1 -0
  133. package/dist/gs/os/types_js.gs.d.ts +74 -0
  134. package/dist/gs/os/types_js.gs.js +147 -0
  135. package/dist/gs/os/types_js.gs.js.map +1 -0
  136. package/dist/gs/os/types_unix.gs.d.ts +40 -0
  137. package/dist/gs/os/types_unix.gs.js +87 -0
  138. package/dist/gs/os/types_unix.gs.js.map +1 -0
  139. package/dist/gs/os/wait_unimp.gs.d.ts +1 -0
  140. package/dist/gs/os/wait_unimp.gs.js +2 -0
  141. package/dist/gs/os/wait_unimp.gs.js.map +1 -0
  142. package/dist/gs/os/zero_copy_posix.gs.d.ts +4 -0
  143. package/dist/gs/os/zero_copy_posix.gs.js +27 -0
  144. package/dist/gs/os/zero_copy_posix.gs.js.map +1 -0
  145. package/dist/gs/os/zero_copy_stub.gs.d.ts +1 -0
  146. package/dist/gs/os/zero_copy_stub.gs.js +2 -0
  147. package/dist/gs/os/zero_copy_stub.gs.js.map +1 -0
  148. package/dist/gs/sync/atomic/doc.gs.d.ts +28 -0
  149. package/dist/gs/sync/atomic/doc.gs.js +265 -0
  150. package/dist/gs/sync/atomic/doc.gs.js.map +1 -0
  151. package/dist/gs/sync/atomic/doc_64.gs.d.ts +15 -0
  152. package/dist/gs/sync/atomic/doc_64.gs.js +165 -0
  153. package/dist/gs/sync/atomic/doc_64.gs.js.map +1 -0
  154. package/dist/gs/sync/atomic/index.d.ts +4 -0
  155. package/dist/gs/sync/atomic/index.js +5 -0
  156. package/dist/gs/sync/atomic/index.js.map +1 -0
  157. package/dist/gs/sync/atomic/type.gs.d.ts +130 -0
  158. package/dist/gs/sync/atomic/type.gs.js +433 -0
  159. package/dist/gs/sync/atomic/type.gs.js.map +1 -0
  160. package/dist/gs/sync/atomic/value.gs.d.ts +19 -0
  161. package/dist/gs/sync/atomic/value.gs.js +116 -0
  162. package/dist/gs/sync/atomic/value.gs.js.map +1 -0
  163. package/dist/gs/syscall/index.d.ts +80 -0
  164. package/dist/gs/syscall/index.js +163 -0
  165. package/dist/gs/syscall/index.js.map +1 -0
  166. package/dist/gs/unsafe/unsafe.d.ts +1 -0
  167. package/dist/gs/unsafe/unsafe.js +5 -0
  168. package/dist/gs/unsafe/unsafe.js.map +1 -1
  169. package/gs/internal/byteorder/index.ts +40 -0
  170. package/gs/internal/testlog/index.ts +7 -0
  171. package/gs/maps/iter.ts +1 -1
  172. package/gs/math/erfinv.gs.test.ts +2 -2
  173. package/gs/math/fma.gs.test.ts +5 -5
  174. package/gs/math/ldexp.gs.test.ts +5 -5
  175. package/gs/math/lgamma.gs.test.ts +2 -2
  176. package/gs/os/dir.gs.ts +42 -0
  177. package/gs/os/dir_unix.gs.ts +155 -0
  178. package/gs/os/dirent_js.gs.ts +27 -0
  179. package/gs/os/env.gs.ts +179 -0
  180. package/gs/os/error.gs.ts +205 -0
  181. package/gs/os/error_errno.gs.ts +12 -0
  182. package/gs/os/exec.gs.ts +198 -0
  183. package/gs/os/exec_nohandle.gs.ts +2 -0
  184. package/gs/os/exec_posix.gs.ts +95 -0
  185. package/gs/os/exec_unix.gs.ts +64 -0
  186. package/gs/os/executable.gs.ts +18 -0
  187. package/gs/os/executable_js.gs.ts +6 -0
  188. package/gs/os/executable_wasm.gs.ts +10 -0
  189. package/gs/os/file_constants_js.gs.ts +98 -0
  190. package/gs/os/file_js.gs.ts +99 -0
  191. package/gs/os/file_open_unix.gs.ts +15 -0
  192. package/gs/os/file_posix_js.gs.ts +40 -0
  193. package/gs/os/file_unix_js.gs.ts +94 -0
  194. package/gs/os/getwd_js.gs.ts +28 -0
  195. package/gs/os/index.ts +19 -0
  196. package/gs/os/path.gs.ts +35 -0
  197. package/gs/os/path_unix.gs.ts +49 -0
  198. package/gs/os/pidfd_js.gs.ts +25 -0
  199. package/gs/os/pipe_wasm.gs.ts +17 -0
  200. package/gs/os/proc.gs.ts +84 -0
  201. package/gs/os/proc_js.gs.ts +15 -0
  202. package/gs/os/rawconn_js.gs.ts +30 -0
  203. package/gs/os/removeall_js.gs.ts +9 -0
  204. package/gs/os/root_js.gs.ts +111 -0
  205. package/gs/os/root_nonwindows.gs.ts +6 -0
  206. package/gs/os/root_noopenat.gs.ts +45 -0
  207. package/gs/os/stat.gs.ts +38 -0
  208. package/gs/os/stat_js.gs.ts +30 -0
  209. package/gs/os/stat_unix_js.gs.ts +27 -0
  210. package/gs/os/sticky_bsd.gs.ts +4 -0
  211. package/gs/os/sys.gs.ts +12 -0
  212. package/gs/os/sys_bsd.gs.ts +17 -0
  213. package/gs/os/sys_js.gs.ts +9 -0
  214. package/gs/os/tempfile.gs.ts +34 -0
  215. package/gs/os/types.gs.ts +170 -0
  216. package/gs/os/types_js.gs.ts +205 -0
  217. package/gs/os/types_unix.gs.ts +118 -0
  218. package/gs/os/wait_unimp.gs.ts +2 -0
  219. package/gs/os/zero_copy_posix.gs.ts +35 -0
  220. package/gs/os/zero_copy_stub.gs.ts +4 -0
  221. package/gs/sync/atomic/doc.gs.ts +276 -0
  222. package/gs/sync/atomic/doc_64.gs.ts +168 -0
  223. package/gs/sync/atomic/index.ts +4 -0
  224. package/gs/sync/atomic/type.gs.ts +596 -0
  225. package/gs/sync/atomic/value.gs.ts +158 -0
  226. package/gs/syscall/index.ts +204 -0
  227. package/gs/unsafe/unsafe.ts +6 -0
  228. package/package.json +1 -1
@@ -0,0 +1,155 @@
1
+ import * as $ from "@goscript/builtin/builtin.js";
2
+ import { direntIno, direntNamlen, direntReclen, direntType } from "./dirent_js.gs.js";
3
+ import { IsNotExist } from "./error.gs.js";
4
+ import { newUnixDirent } from "./file_unix_js.gs.js";
5
+
6
+ import * as byteorder from "@goscript/internal/byteorder/index.js"
7
+
8
+ import * as goarch from "@goscript/internal/goarch/index.js"
9
+
10
+ import * as io from "@goscript/io/index.js"
11
+
12
+ import * as runtime from "@goscript/runtime/index.js"
13
+
14
+ import * as sync from "@goscript/sync/index.js"
15
+
16
+ import * as syscall from "@goscript/syscall/index.js"
17
+
18
+ import * as unsafe from "@goscript/unsafe/index.js"
19
+
20
+ // Import uintptr type
21
+ type uintptr = syscall.uintptr
22
+
23
+ class dirInfo {
24
+ public get mu(): sync.Mutex {
25
+ return this._fields.mu.value
26
+ }
27
+ public set mu(value: sync.Mutex) {
28
+ this._fields.mu.value = value
29
+ }
30
+
31
+ // buffer for directory I/O
32
+ public get buf(): $.VarRef<$.Bytes> | null {
33
+ return this._fields.buf.value
34
+ }
35
+ public set buf(value: $.VarRef<$.Bytes> | null) {
36
+ this._fields.buf.value = value
37
+ }
38
+
39
+ // length of buf; return value from Getdirentries
40
+ public get nbuf(): number {
41
+ return this._fields.nbuf.value
42
+ }
43
+ public set nbuf(value: number) {
44
+ this._fields.nbuf.value = value
45
+ }
46
+
47
+ // location of next record in buf.
48
+ public get bufp(): number {
49
+ return this._fields.bufp.value
50
+ }
51
+ public set bufp(value: number) {
52
+ this._fields.bufp.value = value
53
+ }
54
+
55
+ public _fields: {
56
+ mu: $.VarRef<sync.Mutex>;
57
+ buf: $.VarRef<$.VarRef<$.Bytes> | null>;
58
+ nbuf: $.VarRef<number>;
59
+ bufp: $.VarRef<number>;
60
+ }
61
+
62
+ constructor(init?: Partial<{buf?: $.VarRef<$.Bytes> | null, bufp?: number, mu?: sync.Mutex, nbuf?: number}>) {
63
+ this._fields = {
64
+ mu: $.varRef(init?.mu?.clone() ?? new sync.Mutex()),
65
+ buf: $.varRef(init?.buf ?? null),
66
+ nbuf: $.varRef(init?.nbuf ?? 0),
67
+ bufp: $.varRef(init?.bufp ?? 0)
68
+ }
69
+ }
70
+
71
+ public clone(): dirInfo {
72
+ const cloned = new dirInfo()
73
+ cloned._fields = {
74
+ mu: $.varRef(this._fields.mu.value?.clone() ?? null),
75
+ buf: $.varRef(this._fields.buf.value),
76
+ nbuf: $.varRef(this._fields.nbuf.value),
77
+ bufp: $.varRef(this._fields.bufp.value)
78
+ }
79
+ return cloned
80
+ }
81
+
82
+ public close(): void {
83
+ const d = this
84
+ if (d.buf != null) {
85
+ dirBufPool.Put(d.buf)
86
+ d.buf = null
87
+ }
88
+ }
89
+
90
+ // Register this type with the runtime type system
91
+ static __typeInfo = $.registerStructType(
92
+ 'dirInfo',
93
+ new dirInfo(),
94
+ [{ name: "close", args: [], returns: [] }],
95
+ dirInfo,
96
+ {"mu": "Mutex", "buf": { kind: $.TypeKind.Pointer, elemType: { kind: $.TypeKind.Slice, elemType: { kind: $.TypeKind.Basic, name: "number" } } }, "nbuf": { kind: $.TypeKind.Basic, name: "number" }, "bufp": { kind: $.TypeKind.Basic, name: "number" }}
97
+ );
98
+ }
99
+
100
+ // More than 5760 to work around https://golang.org/issue/24015.
101
+ let blockSize: number = 8192
102
+
103
+ let dirBufPool: sync.Pool = new sync.Pool({New: (): null | any => {
104
+ // The buffer must be at least a block long.
105
+ let buf = new Uint8Array(8192)
106
+ return buf
107
+ }})
108
+
109
+ // readInt returns the size-bytes unsigned integer in native byte order at offset off.
110
+ export function readInt(b: $.Bytes, off: uintptr, size: uintptr): [number, boolean] {
111
+ let u: number = 0
112
+ let ok: boolean = false
113
+ {
114
+ if ($.len(b) < $.int(off + size)) {
115
+ return [0, false]
116
+ }
117
+ if (goarch.BigEndian) {
118
+ return [readIntBE($.goSlice(b, off, undefined), size), true]
119
+ }
120
+ return [readIntLE($.goSlice(b, off, undefined), size), true]
121
+ }
122
+ }
123
+
124
+ export function readIntBE(b: $.Bytes, size: uintptr): number {
125
+ switch (size) {
126
+ case 1:
127
+ return (b![0] as number)
128
+ case 2:
129
+ return (byteorder.BEUint16(b) as number)
130
+ case 4:
131
+ return (byteorder.BEUint32(b) as number)
132
+ case 8:
133
+ return (byteorder.BEUint64(b) as number)
134
+ default:
135
+ $.panic("syscall: readInt with unsupported size")
136
+ return 0 // This line will never be reached due to panic, but satisfies TypeScript
137
+ }
138
+ }
139
+
140
+ export function readIntLE(b: $.Bytes, size: uintptr): number {
141
+ switch (size) {
142
+ case 1:
143
+ return (b![0] as number)
144
+ case 2:
145
+ return (byteorder.LEUint16(b) as number)
146
+ case 4:
147
+ return (byteorder.LEUint32(b) as number)
148
+ case 8:
149
+ return (byteorder.LEUint64(b) as number)
150
+ default:
151
+ $.panic("syscall: readInt with unsupported size")
152
+ return 0 // This line will never be reached due to panic, but satisfies TypeScript
153
+ }
154
+ }
155
+
@@ -0,0 +1,27 @@
1
+ import * as $ from "@goscript/builtin/builtin.js";
2
+ import { readInt } from "./dir_unix.gs.js";
3
+
4
+ import * as syscall from "@goscript/syscall/index.js"
5
+
6
+ import * as unsafe from "@goscript/unsafe/index.js"
7
+
8
+ export function direntIno(buf: $.Bytes): [number, boolean] {
9
+ return [1, true]
10
+ }
11
+
12
+ export function direntReclen(buf: $.Bytes): [number, boolean] {
13
+ return readInt(buf, unsafe.Offsetof(new syscall.Dirent({}).Reclen), unsafe.Sizeof(new syscall.Dirent({}).Reclen))
14
+ }
15
+
16
+ export function direntNamlen(buf: $.Bytes): [number, boolean] {
17
+ let [reclen, ok] = direntReclen(buf)
18
+ if (!ok) {
19
+ return [0, false]
20
+ }
21
+ return [reclen - (unsafe.Offsetof(new syscall.Dirent({}).Name) as number), true]
22
+ }
23
+
24
+ export function direntType(buf: $.Bytes): number {
25
+ return ~(0 as number)
26
+ }
27
+
@@ -0,0 +1,179 @@
1
+ import * as $ from "@goscript/builtin/builtin.js";
2
+ import { NewSyscallError } from "./error.gs.js";
3
+
4
+ import * as syscall from "@goscript/syscall/index.js"
5
+
6
+ // Expand replaces ${var} or $var in the string based on the mapping function.
7
+ // For example, [os.ExpandEnv](s) is equivalent to [os.Expand](s, [os.Getenv]).
8
+ export function Expand(s: string, mapping: ((p0: string) => string) | null): string {
9
+ let buf: $.Bytes = new Uint8Array(0)
10
+ // ${} is all ASCII, so bytes are fine for this operation.
11
+ let i = 0
12
+
13
+ // Encountered invalid syntax; eat the
14
+ // characters.
15
+
16
+ // Valid syntax, but $ was not followed by a
17
+ // name. Leave the dollar character untouched.
18
+ for (let j = 0; j < $.len(s); j++) {
19
+
20
+ // Encountered invalid syntax; eat the
21
+ // characters.
22
+
23
+ // Valid syntax, but $ was not followed by a
24
+ // name. Leave the dollar character untouched.
25
+ if ($.indexString(s, j) == 36 && j + 1 < $.len(s)) {
26
+ if (buf == null) {
27
+ buf = $.makeSlice<number>(0, 2 * $.len(s), 'byte')
28
+ }
29
+ buf = $.append(buf, ...$.stringToBytes($.sliceString(s, i, j)))
30
+ let [name, w] = getShellName($.sliceString(s, j + 1, undefined))
31
+
32
+ // Encountered invalid syntax; eat the
33
+ // characters.
34
+
35
+ // Valid syntax, but $ was not followed by a
36
+ // name. Leave the dollar character untouched.
37
+ if (name == "" && w > 0) {
38
+
39
+ } else if (name == "") {
40
+ // Valid syntax, but $ was not followed by a
41
+ // name. Leave the dollar character untouched.
42
+ buf = $.append(buf, $.indexString(s, j))
43
+ } else {
44
+ buf = $.append(buf, ...$.stringToBytes(mapping!(name)))
45
+ }
46
+ j += w
47
+ i = j + 1
48
+ }
49
+ }
50
+ if (buf == null) {
51
+ return s
52
+ }
53
+ return $.bytesToString(buf) + $.sliceString(s, i, undefined)
54
+ }
55
+
56
+ // ExpandEnv replaces ${var} or $var in the string according to the values
57
+ // of the current environment variables. References to undefined
58
+ // variables are replaced by the empty string.
59
+ export function ExpandEnv(s: string): string {
60
+ return Expand(s, Getenv)
61
+ }
62
+
63
+ // isShellSpecialVar reports whether the character identifies a special
64
+ // shell variable such as $*.
65
+ export function isShellSpecialVar(c: number): boolean {
66
+ switch (c) {
67
+ case 42:
68
+ case 35:
69
+ case 36:
70
+ case 64:
71
+ case 33:
72
+ case 63:
73
+ case 45:
74
+ case 48:
75
+ case 49:
76
+ case 50:
77
+ case 51:
78
+ case 52:
79
+ case 53:
80
+ case 54:
81
+ case 55:
82
+ case 56:
83
+ case 57:
84
+ return true
85
+ break
86
+ }
87
+ return false
88
+ }
89
+
90
+ // isAlphaNum reports whether the byte is an ASCII letter, number, or underscore.
91
+ export function isAlphaNum(c: number): boolean {
92
+ return c == 95 || 48 <= c && c <= 57 || 97 <= c && c <= 122 || 65 <= c && c <= 90
93
+ }
94
+
95
+ // getShellName returns the name that begins the string and the number of bytes
96
+ // consumed to extract it. If the name is enclosed in {}, it's part of a ${}
97
+ // expansion and two more bytes are needed than the length of the name.
98
+ export function getShellName(s: string): [string, number] {
99
+
100
+ // Scan to closing brace
101
+
102
+ // Bad syntax; eat "${}"
103
+
104
+ // Bad syntax; eat "${"
105
+ switch (true) {
106
+ case $.indexString(s, 0) == 123:
107
+ if ($.len(s) > 2 && isShellSpecialVar($.indexString(s, 1)) && $.indexString(s, 2) == 125) {
108
+ return [$.sliceString(s, 1, 2), 3]
109
+ }
110
+ for (let i = 1; i < $.len(s); i++) {
111
+
112
+ // Bad syntax; eat "${}"
113
+ if ($.indexString(s, i) == 125) {
114
+
115
+ // Bad syntax; eat "${}"
116
+ if (i == 1) {
117
+ return ["", 2]
118
+ }
119
+ return [$.sliceString(s, 1, i), i + 1]
120
+ }
121
+ }
122
+ return ["", 1]
123
+ break
124
+ case isShellSpecialVar($.indexString(s, 0)):
125
+ return [$.sliceString(s, 0, 1), 1]
126
+ break
127
+ }
128
+ // Scan alphanumerics.
129
+ let i: number = 0
130
+ for (i = 0; i < $.len(s) && isAlphaNum($.indexString(s, i)); i++) {
131
+ }
132
+ return [$.sliceString(s, undefined, i), i]
133
+ }
134
+
135
+ // Getenv retrieves the value of the environment variable named by the key.
136
+ // It returns the value, which will be empty if the variable is not present.
137
+ // To distinguish between an empty value and an unset value, use [LookupEnv].
138
+ export function Getenv(key: string): string {
139
+ // testlog.Getenv(key) // Testlog not available in JavaScript
140
+ let [v, ] = syscall.Getenv(key)
141
+ return v
142
+ }
143
+
144
+ // LookupEnv retrieves the value of the environment variable named
145
+ // by the key. If the variable is present in the environment the
146
+ // value (which may be empty) is returned and the boolean is true.
147
+ // Otherwise the returned value will be empty and the boolean will
148
+ // be false.
149
+ export function LookupEnv(key: string): [string, boolean] {
150
+ // testlog.Getenv(key) // Testlog not available in JavaScript
151
+ return syscall.Getenv(key)
152
+ }
153
+
154
+ // Setenv sets the value of the environment variable named by the key.
155
+ // It returns an error, if any.
156
+ export function Setenv(key: string, value: string): $.GoError {
157
+ let err = syscall.Setenv(key, value)
158
+ if (err != null) {
159
+ return NewSyscallError("setenv", err)
160
+ }
161
+ return null
162
+ }
163
+
164
+ // Unsetenv unsets a single environment variable.
165
+ export function Unsetenv(key: string): $.GoError {
166
+ return syscall.Unsetenv(key)
167
+ }
168
+
169
+ // Clearenv deletes all environment variables.
170
+ export function Clearenv(): void {
171
+ syscall.Clearenv()
172
+ }
173
+
174
+ // Environ returns a copy of strings representing the environment,
175
+ // in the form "key=value".
176
+ export function Environ(): $.Slice<string> {
177
+ return syscall.Environ()
178
+ }
179
+
@@ -0,0 +1,205 @@
1
+ import * as $ from "@goscript/builtin/builtin.js";
2
+
3
+ // import * as poll from "@goscript/internal/poll/index.js" // Not available in JavaScript
4
+
5
+ import * as fs from "@goscript/io/fs/index.js"
6
+
7
+ // ErrInvalid indicates an invalid argument.
8
+ // Methods on File will return this error when the receiver is nil.
9
+ // "invalid argument"
10
+ export let ErrInvalid: $.GoError = fs.ErrInvalid
11
+
12
+ // "permission denied"
13
+ export let ErrPermission: $.GoError = fs.ErrPermission
14
+
15
+ // "file already exists"
16
+ export let ErrExist: $.GoError = fs.ErrExist
17
+
18
+ // "file does not exist"
19
+ export let ErrNotExist: $.GoError = fs.ErrNotExist
20
+
21
+ // "file already closed"
22
+ export let ErrClosed: $.GoError = fs.ErrClosed
23
+
24
+ // "file type does not support deadline"
25
+ export let ErrNoDeadline: $.GoError = errNoDeadline()
26
+
27
+ // "i/o timeout"
28
+ export let ErrDeadlineExceeded: $.GoError = errDeadlineExceeded()
29
+
30
+ // "operation not implemented in JavaScript environment"
31
+ export let ErrUnimplemented: $.GoError = {
32
+ Error: () => "operation not implemented in JavaScript environment"
33
+ }
34
+
35
+ export function errNoDeadline(): $.GoError {
36
+ return {Error: () => "file type does not support deadline"}
37
+ }
38
+
39
+ // errDeadlineExceeded returns the value for os.ErrDeadlineExceeded.
40
+ // This error comes from the internal/poll package, which is also
41
+ // used by package net. Doing it this way ensures that the net
42
+ // package will return os.ErrDeadlineExceeded for an exceeded deadline,
43
+ // as documented by net.Conn.SetDeadline, without requiring any extra
44
+ // work in the net package and without requiring the internal/poll
45
+ // package to import os (which it can't, because that would be circular).
46
+ export function errDeadlineExceeded(): $.GoError {
47
+ return {Error: () => "i/o timeout"}
48
+ }
49
+
50
+ type timeout = null | {
51
+ Timeout(): boolean
52
+ }
53
+
54
+ $.registerInterfaceType(
55
+ 'timeout',
56
+ null, // Zero value for interface is null
57
+ [{ name: "Timeout", args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: "boolean" } }] }]
58
+ );
59
+
60
+ export type PathError = fs.PathError;
61
+ export const PathError = null as any;
62
+
63
+ export class SyscallError {
64
+ public get Syscall(): string {
65
+ return this._fields.Syscall.value
66
+ }
67
+ public set Syscall(value: string) {
68
+ this._fields.Syscall.value = value
69
+ }
70
+
71
+ public get Err(): $.GoError {
72
+ return this._fields.Err.value
73
+ }
74
+ public set Err(value: $.GoError) {
75
+ this._fields.Err.value = value
76
+ }
77
+
78
+ public _fields: {
79
+ Syscall: $.VarRef<string>;
80
+ Err: $.VarRef<$.GoError>;
81
+ }
82
+
83
+ constructor(init?: Partial<{Err?: $.GoError, Syscall?: string}>) {
84
+ this._fields = {
85
+ Syscall: $.varRef(init?.Syscall ?? ""),
86
+ Err: $.varRef(init?.Err ?? null)
87
+ }
88
+ }
89
+
90
+ public clone(): SyscallError {
91
+ const cloned = new SyscallError()
92
+ cloned._fields = {
93
+ Syscall: $.varRef(this._fields.Syscall.value),
94
+ Err: $.varRef(this._fields.Err.value)
95
+ }
96
+ return cloned
97
+ }
98
+
99
+ public Error(): string {
100
+ const e = this
101
+ return e.Syscall + ": " + e.Err!.Error()
102
+ }
103
+
104
+ public Unwrap(): $.GoError {
105
+ const e = this
106
+ return e.Err
107
+ }
108
+
109
+ // Timeout reports whether this error represents a timeout.
110
+ public Timeout(): boolean {
111
+ const e = this
112
+ let { value: t, ok: ok } = $.typeAssert<timeout>(e.Err, 'timeout')
113
+ return ok && t!.Timeout()
114
+ }
115
+
116
+ // Register this type with the runtime type system
117
+ static __typeInfo = $.registerStructType(
118
+ 'SyscallError',
119
+ new SyscallError(),
120
+ [{ name: "Error", args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: "string" } }] }, { name: "Unwrap", args: [], returns: [{ type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }, { name: "Timeout", args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: "boolean" } }] }],
121
+ SyscallError,
122
+ {"Syscall": { kind: $.TypeKind.Basic, name: "string" }, "Err": { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] }}
123
+ );
124
+ }
125
+
126
+ // NewSyscallError returns, as an error, a new [SyscallError]
127
+ // with the given system call name and error details.
128
+ // As a convenience, if err is nil, NewSyscallError returns nil.
129
+ export function NewSyscallError(syscall: string, err: $.GoError): $.GoError {
130
+ if (err == null) {
131
+ return null
132
+ }
133
+ return new SyscallError({})
134
+ }
135
+
136
+ // IsExist returns a boolean indicating whether its argument is known to report
137
+ // that a file or directory already exists. It is satisfied by [ErrExist] as
138
+ // well as some syscall errors.
139
+ //
140
+ // This function predates [errors.Is]. It only supports errors returned by
141
+ // the os package. New code should use errors.Is(err, fs.ErrExist).
142
+ export function IsExist(err: $.GoError): boolean {
143
+ return underlyingErrorIs(err, ErrExist)
144
+ }
145
+
146
+ // IsNotExist returns a boolean indicating whether its argument is known to
147
+ // report that a file or directory does not exist. It is satisfied by
148
+ // [ErrNotExist] as well as some syscall errors.
149
+ //
150
+ // This function predates [errors.Is]. It only supports errors returned by
151
+ // the os package. New code should use errors.Is(err, fs.ErrNotExist).
152
+ export function IsNotExist(err: $.GoError): boolean {
153
+ return underlyingErrorIs(err, ErrNotExist)
154
+ }
155
+
156
+ // IsPermission returns a boolean indicating whether its argument is known to
157
+ // report that permission is denied. It is satisfied by [ErrPermission] as well
158
+ // as some syscall errors.
159
+ //
160
+ // This function predates [errors.Is]. It only supports errors returned by
161
+ // the os package. New code should use errors.Is(err, fs.ErrPermission).
162
+ export function IsPermission(err: $.GoError): boolean {
163
+ return underlyingErrorIs(err, ErrPermission)
164
+ }
165
+
166
+ // IsTimeout returns a boolean indicating whether its argument is known
167
+ // to report that a timeout occurred.
168
+ //
169
+ // This function predates [errors.Is], and the notion of whether an
170
+ // error indicates a timeout can be ambiguous. For example, the Unix
171
+ // error EWOULDBLOCK sometimes indicates a timeout and sometimes does not.
172
+ // New code should use errors.Is with a value appropriate to the call
173
+ // returning the error, such as [os.ErrDeadlineExceeded].
174
+ export function IsTimeout(err: $.GoError): boolean {
175
+ let { value: terr, ok: ok } = $.typeAssert<timeout>(underlyingError(err), 'timeout')
176
+ return ok && terr!.Timeout()
177
+ }
178
+
179
+ export function underlyingErrorIs(err: $.GoError, target: $.GoError): boolean {
180
+ // Note that this function is not errors.Is:
181
+ // underlyingError only unwraps the specific error-wrapping types
182
+ // that it historically did, not all errors implementing Unwrap().
183
+ err = underlyingError(err)
184
+ if (err == target) {
185
+ return true
186
+ }
187
+ // To preserve prior behavior, only examine syscall errors.
188
+ let { value: e, ok: ok } = $.typeAssert<any>(err, 'syscallErrorType')
189
+ return ok && e.Is(target)
190
+ }
191
+
192
+ // underlyingError returns the underlying error for known os error types.
193
+ export function underlyingError(err: $.GoError): $.GoError {
194
+ $.typeSwitch(err, [{ types: [{kind: $.TypeKind.Pointer, elemType: 'PathError'}], body: (err) => {
195
+ return err!.Err
196
+ }},
197
+ { types: [{kind: $.TypeKind.Pointer, elemType: 'LinkError'}], body: (err) => {
198
+ return err!.Err
199
+ }},
200
+ { types: [{kind: $.TypeKind.Pointer, elemType: 'SyscallError'}], body: (err) => {
201
+ return err!.Err
202
+ }}])
203
+ return err
204
+ }
205
+
@@ -0,0 +1,12 @@
1
+ import * as $ from "@goscript/builtin/builtin.js";
2
+
3
+ import * as syscall from "@goscript/syscall/index.js"
4
+
5
+ type syscallErrorType = syscall.Errno;
6
+
7
+ let errENOSYS: syscall.Errno = syscall.ENOSYS
8
+
9
+ let errERANGE: syscall.Errno = syscall.ERANGE
10
+
11
+ let errENOMEM: syscall.Errno = syscall.ENOMEM
12
+