goscript 0.0.24 → 0.0.26
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/README.md +1 -1
- package/cmd/goscript/cmd_compile.go +17 -1
- package/compiler/analysis.go +1 -1
- package/compiler/builtin_test.go +2 -14
- package/compiler/compiler.go +251 -11
- package/compiler/compiler_test.go +60 -0
- package/compiler/decl.go +7 -1
- package/compiler/expr-call.go +212 -2
- package/compiler/expr.go +46 -2
- package/compiler/field.go +4 -4
- package/compiler/spec-value.go +1 -1
- package/compiler/stmt-range.go +204 -1
- package/compiler/type.go +47 -4
- package/dist/gs/builtin/builtin.d.ts +9 -0
- package/dist/gs/builtin/builtin.js +10 -1
- package/dist/gs/builtin/builtin.js.map +1 -1
- package/dist/gs/builtin/channel.d.ts +193 -0
- package/dist/gs/builtin/channel.js.map +1 -1
- package/dist/gs/builtin/defer.d.ts +38 -0
- package/dist/gs/builtin/defer.js.map +1 -1
- package/dist/gs/builtin/index.d.ts +1 -0
- package/dist/gs/builtin/index.js +2 -0
- package/dist/gs/builtin/index.js.map +1 -0
- package/dist/gs/builtin/io.d.ts +16 -0
- package/dist/gs/builtin/io.js.map +1 -1
- package/dist/gs/builtin/map.d.ts +33 -0
- package/dist/gs/builtin/map.js.map +1 -1
- package/dist/gs/builtin/slice.d.ts +173 -0
- package/dist/gs/builtin/slice.js +38 -24
- package/dist/gs/builtin/slice.js.map +1 -1
- package/dist/gs/builtin/type.d.ts +203 -0
- package/dist/gs/builtin/type.js +1 -2
- package/dist/gs/builtin/type.js.map +1 -1
- package/dist/gs/builtin/varRef.d.ts +14 -0
- package/dist/gs/builtin/varRef.js.map +1 -1
- package/dist/gs/cmp/index.d.ts +4 -0
- package/dist/gs/cmp/index.js +27 -0
- package/dist/gs/cmp/index.js.map +1 -0
- package/dist/gs/context/context.d.ts +26 -0
- package/dist/gs/context/context.js +297 -38
- package/dist/gs/context/context.js.map +1 -1
- package/dist/gs/context/index.d.ts +1 -0
- package/dist/gs/errors/errors.d.ts +8 -0
- package/dist/gs/errors/errors.js +190 -0
- package/dist/gs/errors/errors.js.map +1 -0
- package/dist/gs/errors/index.d.ts +1 -0
- package/dist/gs/errors/index.js +2 -0
- package/dist/gs/errors/index.js.map +1 -0
- package/dist/gs/internal/goarch/index.d.ts +6 -0
- package/dist/gs/internal/goarch/index.js +14 -0
- package/dist/gs/internal/goarch/index.js.map +1 -0
- package/dist/gs/iter/index.d.ts +1 -0
- package/dist/gs/iter/index.js +2 -0
- package/dist/gs/iter/index.js.map +1 -0
- package/dist/gs/iter/iter.d.ts +4 -0
- package/dist/gs/iter/iter.js +91 -0
- package/dist/gs/iter/iter.js.map +1 -0
- package/dist/gs/math/bits/index.d.ts +47 -0
- package/dist/gs/math/bits/index.js +300 -0
- package/dist/gs/math/bits/index.js.map +1 -0
- package/dist/gs/runtime/index.d.ts +1 -0
- package/dist/gs/runtime/runtime.d.ts +42 -0
- package/dist/gs/runtime/runtime.js +15 -18
- package/dist/gs/runtime/runtime.js.map +1 -1
- package/dist/gs/slices/index.d.ts +1 -0
- package/dist/gs/slices/index.js +2 -0
- package/dist/gs/slices/index.js.map +1 -0
- package/dist/gs/slices/slices.d.ts +8 -0
- package/dist/gs/slices/slices.js +20 -0
- package/dist/gs/slices/slices.js.map +1 -0
- package/dist/gs/time/index.d.ts +1 -0
- package/dist/gs/time/time.d.ts +57 -0
- package/dist/gs/time/time.js +108 -15
- package/dist/gs/time/time.js.map +1 -1
- package/package.json +3 -2
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Minimal stub for cmp package
|
|
2
|
+
// This provides the Ordered type and comparison functions needed by slices
|
|
3
|
+
// Compare compares two values and returns:
|
|
4
|
+
// -1 if a < b
|
|
5
|
+
// 0 if a == b
|
|
6
|
+
// 1 if a > b
|
|
7
|
+
export function Compare(a, b) {
|
|
8
|
+
if (a < b)
|
|
9
|
+
return -1;
|
|
10
|
+
if (a > b)
|
|
11
|
+
return 1;
|
|
12
|
+
return 0;
|
|
13
|
+
}
|
|
14
|
+
// Less reports whether a < b
|
|
15
|
+
export function Less(a, b) {
|
|
16
|
+
return a < b;
|
|
17
|
+
}
|
|
18
|
+
// Or returns the first non-zero result from the comparison functions,
|
|
19
|
+
// or zero if all comparisons return zero
|
|
20
|
+
export function Or(...comparisons) {
|
|
21
|
+
for (const cmp of comparisons) {
|
|
22
|
+
if (cmp !== 0)
|
|
23
|
+
return cmp;
|
|
24
|
+
}
|
|
25
|
+
return 0;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../gs/cmp/index.ts"],"names":[],"mappings":"AAAA,+BAA+B;AAC/B,2EAA2E;AAK3E,2CAA2C;AAC3C,cAAc;AACd,eAAe;AACf,cAAc;AACd,MAAM,UAAU,OAAO,CAAoB,CAAI,EAAE,CAAI;IACnD,IAAI,CAAC,GAAG,CAAC;QAAE,OAAO,CAAC,CAAC,CAAA;IACpB,IAAI,CAAC,GAAG,CAAC;QAAE,OAAO,CAAC,CAAA;IACnB,OAAO,CAAC,CAAA;AACV,CAAC;AAED,6BAA6B;AAC7B,MAAM,UAAU,IAAI,CAAoB,CAAI,EAAE,CAAI;IAChD,OAAO,CAAC,GAAG,CAAC,CAAA;AACd,CAAC;AAED,sEAAsE;AACtE,yCAAyC;AACzC,MAAM,UAAU,EAAE,CAAC,GAAG,WAAqB;IACzC,KAAK,MAAM,GAAG,IAAI,WAAW,EAAE,CAAC;QAC9B,IAAI,GAAG,KAAK,CAAC;YAAE,OAAO,GAAG,CAAA;IAC3B,CAAC;IACD,OAAO,CAAC,CAAA;AACV,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import * as $ from '@goscript/builtin/builtin.js';
|
|
2
|
+
export declare const Canceled: Error;
|
|
3
|
+
export declare class DeadlineExceededError extends Error {
|
|
4
|
+
constructor();
|
|
5
|
+
}
|
|
6
|
+
export declare const DeadlineExceeded: DeadlineExceededError;
|
|
7
|
+
export type CancelFunc = () => void;
|
|
8
|
+
export type CancelCauseFunc = (cause: Error | null) => void;
|
|
9
|
+
export interface Context {
|
|
10
|
+
Deadline(): [Date | null, boolean];
|
|
11
|
+
Done(): $.Channel<{}>;
|
|
12
|
+
Err(): Error | null;
|
|
13
|
+
Value(key: any): any;
|
|
14
|
+
}
|
|
15
|
+
export declare function Background(): Context;
|
|
16
|
+
export declare function TODO(): Context;
|
|
17
|
+
export declare function WithCancel(parent: Context): [Context, CancelFunc];
|
|
18
|
+
export declare function WithCancelCause(parent: Context): [Context, CancelCauseFunc];
|
|
19
|
+
export declare function WithDeadline(parent: Context, d: Date): [Context, CancelFunc];
|
|
20
|
+
export declare function WithDeadlineCause(parent: Context, d: Date, cause: Error | null): [Context, CancelFunc];
|
|
21
|
+
export declare function WithTimeout(parent: Context, timeout: number): [Context, CancelFunc];
|
|
22
|
+
export declare function WithTimeoutCause(parent: Context, timeout: number, cause: Error | null): [Context, CancelFunc];
|
|
23
|
+
export declare function WithValue(parent: Context, key: any, val: any): Context;
|
|
24
|
+
export declare function WithoutCancel(parent: Context): Context;
|
|
25
|
+
export declare function Cause(ctx: Context): Error | null;
|
|
26
|
+
export declare function AfterFunc(ctx: Context, f: () => void): () => boolean;
|
|
@@ -1,55 +1,314 @@
|
|
|
1
|
-
import * as $ from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
canceled = false;
|
|
1
|
+
import * as $ from '@goscript/builtin/builtin.js';
|
|
2
|
+
export const Canceled = new Error('context canceled');
|
|
3
|
+
Canceled.name = 'CanceledError';
|
|
4
|
+
export class DeadlineExceededError extends Error {
|
|
6
5
|
constructor() {
|
|
7
|
-
|
|
6
|
+
super('context deadline exceeded');
|
|
7
|
+
this.name = 'DeadlineExceededError';
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export const DeadlineExceeded = new DeadlineExceededError();
|
|
11
|
+
// Base implementation for all contexts
|
|
12
|
+
class baseContext {
|
|
13
|
+
}
|
|
14
|
+
// Background/TODO context that is never canceled
|
|
15
|
+
class backgroundContext extends baseContext {
|
|
16
|
+
static neverClosedChannel = $.makeChannel(0, {}, 'both');
|
|
17
|
+
static getNeverClosedChannel() {
|
|
18
|
+
return backgroundContext.neverClosedChannel;
|
|
19
|
+
}
|
|
20
|
+
Deadline() {
|
|
21
|
+
return [null, false];
|
|
22
|
+
}
|
|
23
|
+
Done() {
|
|
24
|
+
return backgroundContext.neverClosedChannel;
|
|
25
|
+
}
|
|
26
|
+
Err() {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
Value(key) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
// Value context wraps a parent and adds a key-value pair
|
|
34
|
+
class valueContext extends baseContext {
|
|
35
|
+
parent;
|
|
36
|
+
key;
|
|
37
|
+
val;
|
|
38
|
+
constructor(parent, key, val) {
|
|
39
|
+
super();
|
|
40
|
+
this.parent = parent;
|
|
41
|
+
this.key = key;
|
|
42
|
+
this.val = val;
|
|
43
|
+
}
|
|
44
|
+
getParent() {
|
|
45
|
+
return this.parent;
|
|
46
|
+
}
|
|
47
|
+
Deadline() {
|
|
48
|
+
return this.parent.Deadline();
|
|
49
|
+
}
|
|
50
|
+
Done() {
|
|
51
|
+
return this.parent.Done();
|
|
52
|
+
}
|
|
53
|
+
Err() {
|
|
54
|
+
return this.parent.Err();
|
|
55
|
+
}
|
|
56
|
+
Value(key) {
|
|
57
|
+
if (this.key === key) {
|
|
58
|
+
return this.val;
|
|
59
|
+
}
|
|
60
|
+
return this.parent.Value(key);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// Cancel context that can be canceled
|
|
64
|
+
class cancelContext extends baseContext {
|
|
65
|
+
doneChannel;
|
|
66
|
+
err = null;
|
|
67
|
+
cause = null;
|
|
68
|
+
children = new Set();
|
|
69
|
+
parent;
|
|
70
|
+
parentCancelCtx = null;
|
|
71
|
+
removeFromParent = null;
|
|
72
|
+
constructor(parent) {
|
|
73
|
+
super();
|
|
74
|
+
this.parent = parent;
|
|
8
75
|
this.doneChannel = $.makeChannel(0, {}, 'both');
|
|
9
76
|
}
|
|
10
|
-
|
|
77
|
+
Deadline() {
|
|
78
|
+
return this.parent.Deadline();
|
|
79
|
+
}
|
|
11
80
|
Done() {
|
|
12
81
|
return this.doneChannel;
|
|
13
82
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
83
|
+
Err() {
|
|
84
|
+
return this.err;
|
|
85
|
+
}
|
|
86
|
+
Value(key) {
|
|
87
|
+
return this.parent.Value(key);
|
|
88
|
+
}
|
|
89
|
+
getCause() {
|
|
90
|
+
if (this.cause !== null) {
|
|
91
|
+
return this.cause;
|
|
92
|
+
}
|
|
93
|
+
return this.err;
|
|
94
|
+
}
|
|
95
|
+
cancel(removeFromParent, err, cause) {
|
|
96
|
+
if (this.err !== null) {
|
|
97
|
+
return; // Already canceled
|
|
98
|
+
}
|
|
99
|
+
this.err = err;
|
|
100
|
+
this.cause = cause;
|
|
101
|
+
this.doneChannel.close();
|
|
102
|
+
// Cancel all children
|
|
103
|
+
for (const child of this.children) {
|
|
104
|
+
child.cancel(false, err, cause);
|
|
105
|
+
}
|
|
106
|
+
this.children.clear();
|
|
107
|
+
// Remove from parent's children if requested
|
|
108
|
+
if (removeFromParent && this.removeFromParent) {
|
|
109
|
+
this.removeFromParent();
|
|
110
|
+
this.removeFromParent = null;
|
|
21
111
|
}
|
|
22
112
|
}
|
|
23
|
-
|
|
24
|
-
|
|
113
|
+
propagateCancel() {
|
|
114
|
+
// Find parent cancelContext if any
|
|
115
|
+
let parent = this.parent;
|
|
116
|
+
while (parent instanceof valueContext) {
|
|
117
|
+
parent = parent.getParent();
|
|
118
|
+
}
|
|
119
|
+
if (parent instanceof cancelContext) {
|
|
120
|
+
// Parent is a cancel context, register as child
|
|
121
|
+
this.parentCancelCtx = parent;
|
|
122
|
+
if (parent.err !== null) {
|
|
123
|
+
// Parent already canceled
|
|
124
|
+
this.cancel(false, parent.err, parent.cause);
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
parent.children.add(this);
|
|
128
|
+
this.removeFromParent = () => {
|
|
129
|
+
parent.children.delete(this);
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
// Watch parent's Done channel
|
|
135
|
+
this.watchParentDone();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
watchParentDone() {
|
|
139
|
+
const parentDone = this.parent.Done();
|
|
140
|
+
(async () => {
|
|
141
|
+
try {
|
|
142
|
+
await parentDone.receive();
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
// Channel closed
|
|
146
|
+
}
|
|
147
|
+
// Parent is done, cancel this context
|
|
148
|
+
const parentErr = this.parent.Err();
|
|
149
|
+
if (parentErr && this.err === null) {
|
|
150
|
+
this.cancel(false, parentErr, null);
|
|
151
|
+
}
|
|
152
|
+
})();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
// Timer context with deadline
|
|
156
|
+
class timerContext extends cancelContext {
|
|
157
|
+
deadline;
|
|
158
|
+
timer;
|
|
159
|
+
constructor(parent, deadline) {
|
|
160
|
+
super(parent);
|
|
161
|
+
this.deadline = deadline;
|
|
162
|
+
}
|
|
163
|
+
Deadline() {
|
|
164
|
+
return [this.deadline, true];
|
|
165
|
+
}
|
|
166
|
+
startTimer() {
|
|
167
|
+
const now = Date.now();
|
|
168
|
+
const duration = this.deadline.getTime() - now;
|
|
169
|
+
if (duration <= 0) {
|
|
170
|
+
// Already expired
|
|
171
|
+
this.cancel(true, DeadlineExceeded, null);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
this.timer = setTimeout(() => {
|
|
175
|
+
this.cancel(true, DeadlineExceeded, null);
|
|
176
|
+
}, duration);
|
|
177
|
+
}
|
|
178
|
+
cancel(removeFromParent, err, cause) {
|
|
179
|
+
super.cancel(removeFromParent, err, cause);
|
|
180
|
+
if (this.timer) {
|
|
181
|
+
clearTimeout(this.timer);
|
|
182
|
+
this.timer = null;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
// Without cancel context - inherits values but not cancellation
|
|
187
|
+
class withoutCancelContext extends baseContext {
|
|
188
|
+
parent;
|
|
189
|
+
constructor(parent) {
|
|
190
|
+
super();
|
|
191
|
+
this.parent = parent;
|
|
192
|
+
}
|
|
193
|
+
Deadline() {
|
|
194
|
+
return [null, false];
|
|
195
|
+
}
|
|
196
|
+
Done() {
|
|
197
|
+
return backgroundContext.getNeverClosedChannel();
|
|
198
|
+
}
|
|
199
|
+
Err() {
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
Value(key) {
|
|
203
|
+
return this.parent.Value(key);
|
|
25
204
|
}
|
|
26
205
|
}
|
|
27
|
-
//
|
|
206
|
+
// Singleton contexts
|
|
207
|
+
const background = new backgroundContext();
|
|
208
|
+
const todo = new backgroundContext();
|
|
209
|
+
// Background returns a non-nil, empty Context that is never canceled
|
|
28
210
|
export function Background() {
|
|
29
|
-
return
|
|
211
|
+
return background;
|
|
212
|
+
}
|
|
213
|
+
// TODO returns a non-nil, empty Context
|
|
214
|
+
export function TODO() {
|
|
215
|
+
return todo;
|
|
30
216
|
}
|
|
31
217
|
// WithCancel returns a copy of parent with a new Done channel
|
|
32
|
-
// The returned context's Done channel is closed when the returned cancel function
|
|
33
|
-
// is called or when the parent context's Done channel is closed, whichever happens first
|
|
34
218
|
export function WithCancel(parent) {
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
219
|
+
const ctx = new cancelContext(parent);
|
|
220
|
+
ctx.propagateCancel();
|
|
221
|
+
return [
|
|
222
|
+
ctx,
|
|
223
|
+
() => {
|
|
224
|
+
ctx.cancel(true, Canceled, null);
|
|
225
|
+
},
|
|
226
|
+
];
|
|
227
|
+
}
|
|
228
|
+
// WithCancelCause returns a copy of parent with a new Done channel and cause recording
|
|
229
|
+
export function WithCancelCause(parent) {
|
|
230
|
+
const ctx = new cancelContext(parent);
|
|
231
|
+
ctx.propagateCancel();
|
|
232
|
+
return [
|
|
233
|
+
ctx,
|
|
234
|
+
(cause) => {
|
|
235
|
+
ctx.cancel(true, Canceled, cause);
|
|
236
|
+
},
|
|
237
|
+
];
|
|
238
|
+
}
|
|
239
|
+
// WithDeadline returns a copy of parent with the deadline adjusted to be no later than d
|
|
240
|
+
export function WithDeadline(parent, d) {
|
|
241
|
+
return WithDeadlineCause(parent, d, null);
|
|
242
|
+
}
|
|
243
|
+
// WithDeadlineCause is like WithDeadline but also sets the cause
|
|
244
|
+
export function WithDeadlineCause(parent, d, cause) {
|
|
245
|
+
// Check if parent deadline is already earlier
|
|
246
|
+
const [parentDeadline, ok] = parent.Deadline();
|
|
247
|
+
if (ok && parentDeadline && parentDeadline <= d) {
|
|
248
|
+
// Parent deadline is already sooner
|
|
249
|
+
return WithCancel(parent);
|
|
250
|
+
}
|
|
251
|
+
const ctx = new timerContext(parent, d);
|
|
252
|
+
ctx.propagateCancel();
|
|
253
|
+
ctx.startTimer();
|
|
254
|
+
return [
|
|
255
|
+
ctx,
|
|
256
|
+
() => {
|
|
257
|
+
ctx.cancel(true, Canceled, cause);
|
|
258
|
+
},
|
|
259
|
+
];
|
|
260
|
+
}
|
|
261
|
+
// WithTimeout returns WithDeadline(parent, Date.now() + timeout)
|
|
262
|
+
export function WithTimeout(parent, timeout) {
|
|
263
|
+
return WithDeadline(parent, new Date(Date.now() + timeout));
|
|
264
|
+
}
|
|
265
|
+
// WithTimeoutCause is like WithTimeout but also sets the cause
|
|
266
|
+
export function WithTimeoutCause(parent, timeout, cause) {
|
|
267
|
+
return WithDeadlineCause(parent, new Date(Date.now() + timeout), cause);
|
|
268
|
+
}
|
|
269
|
+
// WithValue returns a copy of parent with the value associated with key
|
|
270
|
+
export function WithValue(parent, key, val) {
|
|
271
|
+
return new valueContext(parent, key, val);
|
|
272
|
+
}
|
|
273
|
+
// WithoutCancel returns a context that inherits values but not cancellation
|
|
274
|
+
export function WithoutCancel(parent) {
|
|
275
|
+
return new withoutCancelContext(parent);
|
|
276
|
+
}
|
|
277
|
+
// Cause returns the underlying cause of the context's cancellation
|
|
278
|
+
export function Cause(ctx) {
|
|
279
|
+
let c = ctx;
|
|
280
|
+
// Unwrap value contexts
|
|
281
|
+
while (c instanceof valueContext) {
|
|
282
|
+
c = c.getParent();
|
|
283
|
+
}
|
|
284
|
+
if (c instanceof cancelContext) {
|
|
285
|
+
return c.getCause();
|
|
52
286
|
}
|
|
53
|
-
return
|
|
287
|
+
return c.Err();
|
|
288
|
+
}
|
|
289
|
+
// AfterFunc runs f in a separate goroutine after ctx is done
|
|
290
|
+
export function AfterFunc(ctx, f) {
|
|
291
|
+
let stopped = false;
|
|
292
|
+
let done = false;
|
|
293
|
+
const promise = (async () => {
|
|
294
|
+
try {
|
|
295
|
+
await ctx.Done().receive();
|
|
296
|
+
}
|
|
297
|
+
catch {
|
|
298
|
+
// Channel closed
|
|
299
|
+
}
|
|
300
|
+
if (!stopped) {
|
|
301
|
+
done = true;
|
|
302
|
+
// Run in next tick to simulate goroutine
|
|
303
|
+
setImmediate(f);
|
|
304
|
+
}
|
|
305
|
+
})();
|
|
306
|
+
return () => {
|
|
307
|
+
if (!done) {
|
|
308
|
+
stopped = true;
|
|
309
|
+
return true;
|
|
310
|
+
}
|
|
311
|
+
return false;
|
|
312
|
+
};
|
|
54
313
|
}
|
|
55
314
|
//# sourceMappingURL=context.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../../gs/context/context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,8BAA8B,CAAC;
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../../gs/context/context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,8BAA8B,CAAA;AAEjD,MAAM,CAAC,MAAM,QAAQ,GAAG,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAA;AACrD,QAAQ,CAAC,IAAI,GAAG,eAAe,CAAA;AAE/B,MAAM,OAAO,qBAAsB,SAAQ,KAAK;IAC9C;QACE,KAAK,CAAC,2BAA2B,CAAC,CAAA;QAClC,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAA;IACrC,CAAC;CACF;AACD,MAAM,CAAC,MAAM,gBAAgB,GAAG,IAAI,qBAAqB,EAAE,CAAA;AAqB3D,uCAAuC;AACvC,MAAe,WAAW;CAKzB;AAED,iDAAiD;AACjD,MAAM,iBAAkB,SAAQ,WAAW;IACjC,MAAM,CAAU,kBAAkB,GAAG,CAAC,CAAC,WAAW,CAAK,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,CAAA;IAE7E,MAAM,CAAC,qBAAqB;QAC1B,OAAO,iBAAiB,CAAC,kBAAkB,CAAA;IAC7C,CAAC;IAED,QAAQ;QACN,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;IACtB,CAAC;IAED,IAAI;QACF,OAAO,iBAAiB,CAAC,kBAAkB,CAAA;IAC7C,CAAC;IAED,GAAG;QACD,OAAO,IAAI,CAAA;IACb,CAAC;IAED,KAAK,CAAC,GAAQ;QACZ,OAAO,IAAI,CAAA;IACb,CAAC;;AAGH,yDAAyD;AACzD,MAAM,YAAa,SAAQ,WAAW;IAE1B;IACA;IACA;IAHV,YACU,MAAe,EACf,GAAQ,EACR,GAAQ;QAEhB,KAAK,EAAE,CAAA;QAJC,WAAM,GAAN,MAAM,CAAS;QACf,QAAG,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aAAa,EAAE,CAAC;QAC/B,OAAO,CAAC,CAAC,QAAQ,EAAE,CAAA;IACrB,CAAC;IAED,OAAO,CAAC,CAAC,GAAG,EAAE,CAAA;AAChB,CAAC;AAED,6DAA6D;AAC7D,MAAM,UAAU,SAAS,CAAC,GAAY,EAAE,CAAa;IACnD,IAAI,OAAO,GAAG,KAAK,CAAA;IACnB,IAAI,IAAI,GAAG,KAAK,CAAA;IAEhB,MAAM,OAAO,GAAG,CAAC,KAAK,IAAI,EAAE;QAC1B,IAAI,CAAC;YACH,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC,OAAO,EAAE,CAAA;QAC5B,CAAC;QAAC,MAAM,CAAC;YACP,iBAAiB;QACnB,CAAC;QACD,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,IAAI,GAAG,IAAI,CAAA;YACX,yCAAyC;YACzC,YAAY,CAAC,CAAC,CAAC,CAAA;QACjB,CAAC;IACH,CAAC,CAAC,EAAE,CAAA;IAEJ,OAAO,GAAG,EAAE;QACV,IAAI,CAAC,IAAI,EAAE,CAAC;YACV,OAAO,GAAG,IAAI,CAAA;YACd,OAAO,IAAI,CAAA;QACb,CAAC;QACD,OAAO,KAAK,CAAA;IACd,CAAC,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './context.js';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import * as $ from '@goscript/builtin/builtin.js';
|
|
2
|
+
import { GoError } from '@goscript/builtin/io.js';
|
|
3
|
+
export declare function New(text: string): GoError;
|
|
4
|
+
export declare const ErrUnsupported: $.GoError;
|
|
5
|
+
export declare function Unwrap(err: GoError): GoError;
|
|
6
|
+
export declare function Is(err: GoError, target: GoError): boolean;
|
|
7
|
+
export declare function As(err: GoError, target: any): boolean;
|
|
8
|
+
export declare function Join(...errs: GoError[]): GoError;
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
// GoScriptError implements the Go error interface
|
|
2
|
+
class GoScriptError {
|
|
3
|
+
message;
|
|
4
|
+
constructor(message) {
|
|
5
|
+
this.message = message;
|
|
6
|
+
}
|
|
7
|
+
Error() {
|
|
8
|
+
return this.message;
|
|
9
|
+
}
|
|
10
|
+
toString() {
|
|
11
|
+
return this.message;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
// New returns an error that formats as the given text.
|
|
15
|
+
// Each call to New returns a distinct error value even if the text is identical.
|
|
16
|
+
export function New(text) {
|
|
17
|
+
return new GoScriptError(text);
|
|
18
|
+
}
|
|
19
|
+
// ErrUnsupported indicates that a requested operation cannot be performed,
|
|
20
|
+
// because it is unsupported. For example, a call to os.Link when using a
|
|
21
|
+
// file system that does not support hard links.
|
|
22
|
+
//
|
|
23
|
+
// Functions and methods should not return this error but should instead
|
|
24
|
+
// return an error including appropriate context that satisfies
|
|
25
|
+
//
|
|
26
|
+
// errors.Is(err, errors.ErrUnsupported)
|
|
27
|
+
//
|
|
28
|
+
// either by directly wrapping ErrUnsupported or by implementing an Is method.
|
|
29
|
+
export const ErrUnsupported = New('unsupported operation');
|
|
30
|
+
// Unwrap returns the result of calling the Unwrap method on err, if err's
|
|
31
|
+
// type contains an Unwrap method returning error.
|
|
32
|
+
// Otherwise, Unwrap returns nil.
|
|
33
|
+
export function Unwrap(err) {
|
|
34
|
+
if (err === null) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
// Check if the error has an Unwrap method
|
|
38
|
+
if (typeof err.Unwrap === 'function') {
|
|
39
|
+
const result = err.Unwrap();
|
|
40
|
+
if (result && typeof result.Error === 'function') {
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
// Handle case where Unwrap returns []error
|
|
44
|
+
if (Array.isArray(result) &&
|
|
45
|
+
result.length > 0 &&
|
|
46
|
+
result[0] &&
|
|
47
|
+
typeof result[0].Error === 'function') {
|
|
48
|
+
return result[0];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
// Is reports whether any error in err's tree matches target.
|
|
54
|
+
//
|
|
55
|
+
// The tree consists of err itself, followed by the errors obtained by repeatedly
|
|
56
|
+
// calling Unwrap. When err wraps multiple errors, Is examines err followed by a
|
|
57
|
+
// depth-first traversal of its children.
|
|
58
|
+
//
|
|
59
|
+
// An error is considered to match a target if it is equal to that target or if
|
|
60
|
+
// it implements a method Is(error) bool such that Is(target) returns true.
|
|
61
|
+
//
|
|
62
|
+
// An error type might provide an Is method so it can be treated as equivalent
|
|
63
|
+
// to an existing error. For example, if MyError defines
|
|
64
|
+
//
|
|
65
|
+
// func (m MyError) Is(target error) bool { return target == fs.ErrExist }
|
|
66
|
+
//
|
|
67
|
+
// then Is(MyError{}, fs.ErrExist) returns true. See syscall.Errno.Is for
|
|
68
|
+
// an example in the standard library. An Is method should only shallowly
|
|
69
|
+
// compare err and the target and not call Unwrap on either.
|
|
70
|
+
export function Is(err, target) {
|
|
71
|
+
if (target === null) {
|
|
72
|
+
return err === null;
|
|
73
|
+
}
|
|
74
|
+
if (err === null) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
// Check direct equality
|
|
78
|
+
if (err === target) {
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
// Check if error messages are the same
|
|
82
|
+
if (err.Error() === target.Error()) {
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
// Check if err has an Is method
|
|
86
|
+
if (typeof err.Is === 'function') {
|
|
87
|
+
if (err.Is(target)) {
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
// Recursively check wrapped errors
|
|
92
|
+
const unwrapped = Unwrap(err);
|
|
93
|
+
if (unwrapped !== null) {
|
|
94
|
+
return Is(unwrapped, target);
|
|
95
|
+
}
|
|
96
|
+
// Handle multiple wrapped errors
|
|
97
|
+
if (typeof err.Unwrap === 'function') {
|
|
98
|
+
const result = err.Unwrap();
|
|
99
|
+
if (Array.isArray(result)) {
|
|
100
|
+
for (const wrappedErr of result) {
|
|
101
|
+
if (wrappedErr &&
|
|
102
|
+
typeof wrappedErr.Error === 'function' &&
|
|
103
|
+
Is(wrappedErr, target)) {
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
// As finds the first error in err's tree that matches target, and if one is found,
|
|
112
|
+
// sets target to that error value and returns true. Otherwise, it returns false.
|
|
113
|
+
//
|
|
114
|
+
// The tree consists of err itself, followed by the errors obtained by repeatedly
|
|
115
|
+
// calling Unwrap. When err wraps multiple errors, As examines err followed by a
|
|
116
|
+
// depth-first traversal of its children.
|
|
117
|
+
//
|
|
118
|
+
// An error matches target if the error's concrete value is assignable to the value
|
|
119
|
+
// pointed to by target, or if the error has a method As(interface{}) bool such that
|
|
120
|
+
// As(target) returns true. In the latter case, the As method is responsible for
|
|
121
|
+
// setting target.
|
|
122
|
+
//
|
|
123
|
+
// An error type might provide an As method so it can be treated as if it were a
|
|
124
|
+
// different error type.
|
|
125
|
+
//
|
|
126
|
+
// As panics if target is not a non-nil pointer to either a type that implements
|
|
127
|
+
// error, or to any interface type.
|
|
128
|
+
export function As(err, target) {
|
|
129
|
+
if (err === null) {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
if (target === null || typeof target !== 'object') {
|
|
133
|
+
throw new Error('errors: target cannot be nil');
|
|
134
|
+
}
|
|
135
|
+
// Check if err matches target type
|
|
136
|
+
if (err.constructor === target.constructor) {
|
|
137
|
+
// Copy properties from err to target
|
|
138
|
+
Object.assign(target, err);
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
// Check if err has an As method
|
|
142
|
+
if (typeof err.As === 'function') {
|
|
143
|
+
if (err.As(target)) {
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
// Recursively check wrapped errors
|
|
148
|
+
const unwrapped = Unwrap(err);
|
|
149
|
+
if (unwrapped !== null) {
|
|
150
|
+
return As(unwrapped, target);
|
|
151
|
+
}
|
|
152
|
+
// Handle multiple wrapped errors
|
|
153
|
+
if (typeof err.Unwrap === 'function') {
|
|
154
|
+
const result = err.Unwrap();
|
|
155
|
+
if (Array.isArray(result)) {
|
|
156
|
+
for (const wrappedErr of result) {
|
|
157
|
+
if (wrappedErr &&
|
|
158
|
+
typeof wrappedErr.Error === 'function' &&
|
|
159
|
+
As(wrappedErr, target)) {
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
// Join returns an error that wraps the given errors.
|
|
168
|
+
// Any nil error values are discarded.
|
|
169
|
+
// Join returns nil if every value in errs is nil.
|
|
170
|
+
// The error formats as the concatenation of the strings obtained
|
|
171
|
+
// by calling the Error method of each element of errs, with a newline
|
|
172
|
+
// between each string.
|
|
173
|
+
//
|
|
174
|
+
// A non-nil error returned by Join implements the Unwrap() []error method.
|
|
175
|
+
export function Join(...errs) {
|
|
176
|
+
const nonNilErrs = errs.filter((err) => err !== null);
|
|
177
|
+
if (nonNilErrs.length === 0) {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
if (nonNilErrs.length === 1) {
|
|
181
|
+
return nonNilErrs[0];
|
|
182
|
+
}
|
|
183
|
+
const message = nonNilErrs.map((err) => err.Error()).join('\n');
|
|
184
|
+
const joinedError = new GoScriptError(message);
|
|
185
|
+
joinedError.Unwrap = function () {
|
|
186
|
+
return nonNilErrs;
|
|
187
|
+
};
|
|
188
|
+
return joinedError;
|
|
189
|
+
}
|
|
190
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../gs/errors/errors.ts"],"names":[],"mappings":"AAGA,kDAAkD;AAClD,MAAM,aAAa;IACG;IAApB,YAAoB,OAAe;QAAf,YAAO,GAAP,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C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './errors.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../gs/errors/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA"}
|