goscript 0.0.83 → 0.0.84
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.
- package/dist/gs/builtin/builtin.js +4 -7
- package/dist/gs/builtin/builtin.js.map +1 -1
- package/dist/gs/builtin/hostio.d.ts +72 -0
- package/dist/gs/builtin/hostio.js +181 -0
- package/dist/gs/builtin/hostio.js.map +1 -0
- package/dist/gs/builtin/print.d.ts +8 -0
- package/dist/gs/builtin/print.js +111 -0
- package/dist/gs/builtin/print.js.map +1 -0
- package/dist/gs/fmt/fmt.js +2 -22
- package/dist/gs/fmt/fmt.js.map +1 -1
- package/dist/gs/os/types_js.gs.d.ts +2 -51
- package/dist/gs/os/types_js.gs.js +67 -105
- package/dist/gs/os/types_js.gs.js.map +1 -1
- package/gs/builtin/builtin.ts +4 -6
- package/gs/builtin/hostio.test.ts +69 -0
- package/gs/builtin/hostio.ts +298 -0
- package/gs/builtin/print.test.ts +48 -0
- package/gs/builtin/print.ts +154 -0
- package/gs/fmt/fmt.test.ts +41 -30
- package/gs/fmt/fmt.ts +2 -22
- package/gs/os/file_unix_js.test.ts +53 -0
- package/gs/os/types_js.gs.ts +74 -153
- package/package.json +1 -1
package/gs/os/types_js.gs.ts
CHANGED
|
@@ -5,50 +5,16 @@ import * as fs from "@goscript/io/fs/index.js"
|
|
|
5
5
|
import * as io from "@goscript/io/index.js"
|
|
6
6
|
import * as time from "@goscript/time/index.js"
|
|
7
7
|
import * as syscall from "@goscript/syscall/index.js"
|
|
8
|
+
import {
|
|
9
|
+
DenoFileLike,
|
|
10
|
+
DenoStream,
|
|
11
|
+
HostUnsupportedError,
|
|
12
|
+
hostRuntime,
|
|
13
|
+
NodeFSModule,
|
|
14
|
+
resetHostRuntimeForTests,
|
|
15
|
+
} from "@goscript/builtin/hostio.js"
|
|
8
16
|
import { newRawConn } from "./rawconn_js.gs.js"
|
|
9
17
|
|
|
10
|
-
export type NodeFSModule = {
|
|
11
|
-
readSync(fd: number, buffer: Uint8Array, offset?: number, length?: number, position?: number | null): number
|
|
12
|
-
writeSync(fd: number, buffer: Uint8Array, offset?: number, length?: number, position?: number | null): number
|
|
13
|
-
closeSync?(fd: number): void
|
|
14
|
-
fstatSync?(fd: number): HostStatLike
|
|
15
|
-
fsyncSync?(fd: number): void
|
|
16
|
-
ftruncateSync?(fd: number, len?: number): void
|
|
17
|
-
openSync?(path: string, flags: number | string, mode?: number): number
|
|
18
|
-
chmodSync?(path: string, mode: number): void
|
|
19
|
-
chownSync?(path: string, uid: number, gid: number): void
|
|
20
|
-
lchownSync?(path: string, uid: number, gid: number): void
|
|
21
|
-
linkSync?(existingPath: string, newPath: string): void
|
|
22
|
-
lstatSync?(path: string): HostStatLike
|
|
23
|
-
mkdirSync?(path: string, options?: number | { mode?: number, recursive?: boolean }): void
|
|
24
|
-
readFileSync?(path: string): Uint8Array
|
|
25
|
-
readdirSync?(path: string, options?: { withFileTypes?: boolean }): any[]
|
|
26
|
-
readlinkSync?(path: string): string
|
|
27
|
-
renameSync?(oldPath: string, newPath: string): void
|
|
28
|
-
rmSync?(path: string, options?: { force?: boolean, recursive?: boolean }): void
|
|
29
|
-
rmdirSync?(path: string): void
|
|
30
|
-
statSync?(path: string): HostStatLike
|
|
31
|
-
symlinkSync?(target: string, path: string): void
|
|
32
|
-
truncateSync?(path: string, len?: number): void
|
|
33
|
-
unlinkSync?(path: string): void
|
|
34
|
-
utimesSync?(path: string, atime: Date | number, mtime: Date | number): void
|
|
35
|
-
writeFileSync?(path: string, data: Uint8Array, options?: { mode?: number }): void
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export type DenoStream = {
|
|
39
|
-
readSync?(buffer: Uint8Array): number | null
|
|
40
|
-
writeSync?(buffer: Uint8Array): number
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export type DenoFileLike = DenoStream & {
|
|
44
|
-
close?(): void
|
|
45
|
-
rid?: number
|
|
46
|
-
seekSync?(offset: number, whence: number): number
|
|
47
|
-
syncSync?(): void
|
|
48
|
-
statSync?(): HostStatLike
|
|
49
|
-
truncateSync?(len?: number): void
|
|
50
|
-
}
|
|
51
|
-
|
|
52
18
|
export type HostStatLike = {
|
|
53
19
|
isDirectory(): boolean
|
|
54
20
|
isSymbolicLink?(): boolean
|
|
@@ -66,90 +32,24 @@ export function newHostError(err: unknown): $.GoError {
|
|
|
66
32
|
return { Error: () => message }
|
|
67
33
|
}
|
|
68
34
|
|
|
69
|
-
export function getDynamicRequire(): ((specifier: string) => unknown) | null {
|
|
70
|
-
try {
|
|
71
|
-
return Function(
|
|
72
|
-
"return typeof require !== 'undefined' ? require : null",
|
|
73
|
-
)() as ((specifier: string) => unknown) | null
|
|
74
|
-
} catch {
|
|
75
|
-
return null
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
35
|
export function getNodeFS(): NodeFSModule | null {
|
|
80
|
-
|
|
81
|
-
if (processObj && typeof processObj.getBuiltinModule === "function") {
|
|
82
|
-
const module = processObj.getBuiltinModule("fs")
|
|
83
|
-
if (module && typeof module.readSync === "function" && typeof module.writeSync === "function") {
|
|
84
|
-
return module as NodeFSModule
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const requireFn = getDynamicRequire()
|
|
89
|
-
if (requireFn) {
|
|
90
|
-
for (const specifier of ["node:fs", "fs"]) {
|
|
91
|
-
try {
|
|
92
|
-
const module = requireFn(specifier) as NodeFSModule | null
|
|
93
|
-
if (module && typeof module.readSync === "function" && typeof module.writeSync === "function") {
|
|
94
|
-
return module
|
|
95
|
-
}
|
|
96
|
-
} catch {
|
|
97
|
-
// Try the next fallback.
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return null
|
|
36
|
+
return hostRuntime.nodeFS
|
|
103
37
|
}
|
|
104
38
|
|
|
105
39
|
export function getDeno(): any | null {
|
|
106
|
-
return
|
|
40
|
+
return hostRuntime.deno
|
|
107
41
|
}
|
|
108
42
|
|
|
109
43
|
export function getPlatform(): string {
|
|
110
|
-
|
|
111
|
-
if (denoObj?.build?.os) {
|
|
112
|
-
return denoObj.build.os
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const processObj = (globalThis as any).process
|
|
116
|
-
if (processObj?.platform) {
|
|
117
|
-
return processObj.platform
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return "unknown"
|
|
44
|
+
return hostRuntime.platform
|
|
121
45
|
}
|
|
122
46
|
|
|
123
47
|
export function getEnv(name: string): string {
|
|
124
|
-
|
|
125
|
-
if (denoObj?.env?.get) {
|
|
126
|
-
try {
|
|
127
|
-
return denoObj.env.get(name) ?? ""
|
|
128
|
-
} catch {
|
|
129
|
-
return ""
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const processObj = (globalThis as any).process
|
|
134
|
-
return processObj?.env?.[name] ?? ""
|
|
48
|
+
return hostRuntime.getEnv(name)
|
|
135
49
|
}
|
|
136
50
|
|
|
137
51
|
export function getDenoStream(fd: number): DenoStream | null {
|
|
138
|
-
|
|
139
|
-
if (!denoObj) {
|
|
140
|
-
return null
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
switch (fd) {
|
|
144
|
-
case 0:
|
|
145
|
-
return denoObj.stdin ?? null
|
|
146
|
-
case 1:
|
|
147
|
-
return denoObj.stdout ?? null
|
|
148
|
-
case 2:
|
|
149
|
-
return denoObj.stderr ?? null
|
|
150
|
-
default:
|
|
151
|
-
return null
|
|
152
|
-
}
|
|
52
|
+
return hostRuntime.getStdioHandle(fd)
|
|
153
53
|
}
|
|
154
54
|
|
|
155
55
|
function readFD(fd: number, b: Uint8Array): [number, $.GoError] {
|
|
@@ -157,33 +57,18 @@ function readFD(fd: number, b: Uint8Array): [number, $.GoError] {
|
|
|
157
57
|
return [0, null]
|
|
158
58
|
}
|
|
159
59
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
if (n === null || n === 0) {
|
|
165
|
-
return [0, io.EOF]
|
|
166
|
-
}
|
|
167
|
-
return [n, null]
|
|
168
|
-
} catch (err) {
|
|
169
|
-
return [0, newHostError(err)]
|
|
60
|
+
try {
|
|
61
|
+
const n = hostRuntime.readFD(fd, b)
|
|
62
|
+
if (n === null || n === 0) {
|
|
63
|
+
return [0, io.EOF]
|
|
170
64
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
try {
|
|
176
|
-
const n = nodeFS.readSync(fd, b, 0, b.length, null)
|
|
177
|
-
if (n === 0) {
|
|
178
|
-
return [0, io.EOF]
|
|
179
|
-
}
|
|
180
|
-
return [n, null]
|
|
181
|
-
} catch (err) {
|
|
182
|
-
return [0, newHostError(err)]
|
|
65
|
+
return [n, null]
|
|
66
|
+
} catch (err) {
|
|
67
|
+
if (err instanceof HostUnsupportedError) {
|
|
68
|
+
return [0, ErrUnimplemented]
|
|
183
69
|
}
|
|
70
|
+
return [0, newHostError(err)]
|
|
184
71
|
}
|
|
185
|
-
|
|
186
|
-
return [0, ErrUnimplemented]
|
|
187
72
|
}
|
|
188
73
|
|
|
189
74
|
function writeFD(fd: number, b: Uint8Array): [number, $.GoError] {
|
|
@@ -191,25 +76,51 @@ function writeFD(fd: number, b: Uint8Array): [number, $.GoError] {
|
|
|
191
76
|
return [0, null]
|
|
192
77
|
}
|
|
193
78
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
return [0, newHostError(err)]
|
|
79
|
+
try {
|
|
80
|
+
return [hostRuntime.writeFD(fd, b), null]
|
|
81
|
+
} catch (err) {
|
|
82
|
+
if (err instanceof HostUnsupportedError) {
|
|
83
|
+
return [0, ErrUnimplemented]
|
|
200
84
|
}
|
|
85
|
+
return [0, newHostError(err)]
|
|
201
86
|
}
|
|
87
|
+
}
|
|
202
88
|
|
|
203
|
-
|
|
204
|
-
if (
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
89
|
+
function readHandle(handle: DenoFileLike, b: Uint8Array): [number, $.GoError] {
|
|
90
|
+
if (typeof handle.readSync !== "function") {
|
|
91
|
+
return [0, ErrUnimplemented]
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
const n = handle.readSync(b)
|
|
95
|
+
if (n === null || n === 0) {
|
|
96
|
+
return [0, io.EOF]
|
|
209
97
|
}
|
|
98
|
+
return [n, null]
|
|
99
|
+
} catch (err) {
|
|
100
|
+
return [0, newHostError(err)]
|
|
210
101
|
}
|
|
102
|
+
}
|
|
211
103
|
|
|
212
|
-
|
|
104
|
+
function writeHandle(handle: DenoFileLike, b: Uint8Array): [number, $.GoError] {
|
|
105
|
+
if (typeof handle.writeSync !== "function") {
|
|
106
|
+
return [0, ErrUnimplemented]
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
109
|
+
let offset = 0
|
|
110
|
+
while (offset < b.length) {
|
|
111
|
+
const n = handle.writeSync(b.subarray(offset))
|
|
112
|
+
if (!Number.isFinite(n) || n < 0) {
|
|
113
|
+
throw new Error(`invalid write result: ${n}`)
|
|
114
|
+
}
|
|
115
|
+
if (n === 0) {
|
|
116
|
+
throw new Error("short write")
|
|
117
|
+
}
|
|
118
|
+
offset += n
|
|
119
|
+
}
|
|
120
|
+
return [b.length, null]
|
|
121
|
+
} catch (err) {
|
|
122
|
+
return [0, newHostError(err)]
|
|
123
|
+
}
|
|
213
124
|
}
|
|
214
125
|
|
|
215
126
|
class hostFileInfo {
|
|
@@ -281,11 +192,13 @@ export function createFileInfo(name: string, stat: HostStatLike): fs.FileInfo {
|
|
|
281
192
|
export function createHostFile(name: string, fd: number = -1, handle: DenoFileLike | null = null): File {
|
|
282
193
|
return new File({
|
|
283
194
|
fd,
|
|
284
|
-
file: new file({ handle, path: name }),
|
|
195
|
+
file: new file({ handle: handle ?? hostRuntime.getStdioHandle(fd), path: name }),
|
|
285
196
|
name,
|
|
286
197
|
})
|
|
287
198
|
}
|
|
288
199
|
|
|
200
|
+
export { resetHostRuntimeForTests }
|
|
201
|
+
|
|
289
202
|
// Re-export essential types
|
|
290
203
|
export type Time = time.Time;
|
|
291
204
|
export type FileInfo = fs.FileInfo;
|
|
@@ -371,7 +284,11 @@ export class File {
|
|
|
371
284
|
return [0, null]
|
|
372
285
|
}
|
|
373
286
|
const buf = $.bytesToUint8Array(b)
|
|
374
|
-
const
|
|
287
|
+
const handle = this.file?.handle
|
|
288
|
+
const [n, err] =
|
|
289
|
+
handle && typeof handle.readSync === "function"
|
|
290
|
+
? readHandle(handle, buf)
|
|
291
|
+
: readFD(this.fd, buf)
|
|
375
292
|
if (!(b instanceof Uint8Array) && n > 0) {
|
|
376
293
|
$.copy(b, buf.subarray(0, n))
|
|
377
294
|
}
|
|
@@ -408,7 +325,11 @@ export class File {
|
|
|
408
325
|
if (b === null) {
|
|
409
326
|
return [0, null]
|
|
410
327
|
}
|
|
411
|
-
|
|
328
|
+
const buf = $.bytesToUint8Array(b)
|
|
329
|
+
const handle = this.file?.handle
|
|
330
|
+
return handle && typeof handle.writeSync === "function"
|
|
331
|
+
? writeHandle(handle, buf)
|
|
332
|
+
: writeFD(this.fd, buf)
|
|
412
333
|
}
|
|
413
334
|
|
|
414
335
|
public WriteAt(b: $.Bytes, off: number): [number, $.GoError] {
|