goscript 0.0.23 → 0.0.24
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 +1 -1
- package/compiler/analysis.go +73 -131
- package/compiler/analysis_test.go +220 -0
- package/compiler/assignment.go +37 -43
- package/compiler/builtin_test.go +102 -0
- package/compiler/compiler.go +79 -14
- package/compiler/composite-lit.go +108 -43
- package/compiler/config.go +7 -3
- package/compiler/config_test.go +6 -33
- package/compiler/expr-selector.go +66 -41
- package/compiler/expr-star.go +57 -65
- package/compiler/expr-type.go +1 -1
- package/compiler/expr-value.go +1 -1
- package/compiler/expr.go +79 -18
- package/compiler/primitive.go +11 -10
- package/compiler/spec-struct.go +3 -3
- package/compiler/spec-value.go +75 -29
- package/compiler/spec.go +9 -3
- package/compiler/stmt-assign.go +36 -2
- package/compiler/stmt-for.go +11 -0
- package/compiler/stmt-range.go +110 -0
- package/compiler/stmt.go +52 -0
- package/compiler/type.go +36 -11
- package/dist/gs/builtin/builtin.js +37 -0
- package/dist/gs/builtin/builtin.js.map +1 -0
- package/dist/gs/builtin/channel.js +471 -0
- package/dist/gs/builtin/channel.js.map +1 -0
- package/dist/gs/builtin/defer.js +54 -0
- package/dist/gs/builtin/defer.js.map +1 -0
- package/dist/gs/builtin/io.js +15 -0
- package/dist/gs/builtin/io.js.map +1 -0
- package/dist/gs/builtin/map.js +44 -0
- package/dist/gs/builtin/map.js.map +1 -0
- package/dist/gs/builtin/slice.js +799 -0
- package/dist/gs/builtin/slice.js.map +1 -0
- package/dist/gs/builtin/type.js +745 -0
- package/dist/gs/builtin/type.js.map +1 -0
- package/dist/gs/builtin/varRef.js +14 -0
- package/dist/gs/builtin/varRef.js.map +1 -0
- package/dist/gs/context/context.js +55 -0
- package/dist/gs/context/context.js.map +1 -0
- package/dist/gs/context/index.js +2 -0
- package/dist/gs/context/index.js.map +1 -0
- package/dist/gs/runtime/index.js +2 -0
- package/dist/gs/runtime/index.js.map +1 -0
- package/dist/gs/runtime/runtime.js +158 -0
- package/dist/gs/runtime/runtime.js.map +1 -0
- package/dist/gs/time/index.js +2 -0
- package/dist/gs/time/index.js.map +1 -0
- package/dist/gs/time/time.js +115 -0
- package/dist/gs/time/time.js.map +1 -0
- package/package.json +3 -2
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper for 'select' statements. Takes an array of select cases
|
|
3
|
+
* and resolves when one of them completes, following Go's select rules.
|
|
4
|
+
*
|
|
5
|
+
* @param cases Array of SelectCase objects
|
|
6
|
+
* @param hasDefault Whether there is a default case
|
|
7
|
+
* @returns A promise that resolves with the result of the selected case
|
|
8
|
+
*/
|
|
9
|
+
export async function selectStatement(cases, hasDefault = false) {
|
|
10
|
+
if (cases.length === 0 && !hasDefault) {
|
|
11
|
+
// Go spec: If there are no cases, the select statement blocks forever.
|
|
12
|
+
// Emulate blocking forever with a promise that never resolves.
|
|
13
|
+
return new Promise(() => { }); // Promise never resolves
|
|
14
|
+
}
|
|
15
|
+
// 1. Check for ready (non-blocking) operations
|
|
16
|
+
const readyCases = [];
|
|
17
|
+
for (const caseObj of cases) {
|
|
18
|
+
if (caseObj.id === -1) {
|
|
19
|
+
// Skip default case in this check
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
// Skip nil channels - they are never ready in Go
|
|
23
|
+
if (caseObj.channel === null) {
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (caseObj.channel) {
|
|
27
|
+
if (caseObj.isSend && caseObj.channel.canSendNonBlocking()) {
|
|
28
|
+
readyCases.push(caseObj);
|
|
29
|
+
}
|
|
30
|
+
else if (!caseObj.isSend && caseObj.channel.canReceiveNonBlocking()) {
|
|
31
|
+
readyCases.push(caseObj);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (readyCases.length > 0) {
|
|
36
|
+
// If one or more cases are ready, choose one pseudo-randomly
|
|
37
|
+
const selectedCase = readyCases[Math.floor(Math.random() * readyCases.length)];
|
|
38
|
+
// Execute the selected operation and its onSelected handler
|
|
39
|
+
// Add check for channel existence
|
|
40
|
+
if (selectedCase.channel) {
|
|
41
|
+
if (selectedCase.isSend) {
|
|
42
|
+
const result = await selectedCase.channel.selectSend(selectedCase.value, selectedCase.id);
|
|
43
|
+
if (selectedCase.onSelected) {
|
|
44
|
+
await selectedCase.onSelected(result); // Await the handler
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
const result = await selectedCase.channel.selectReceive(selectedCase.id);
|
|
49
|
+
if (selectedCase.onSelected) {
|
|
50
|
+
await selectedCase.onSelected(result); // Await the handler
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
// This case should ideally not happen if channel is required for non-default cases
|
|
56
|
+
console.error('Selected case without a channel:', selectedCase);
|
|
57
|
+
}
|
|
58
|
+
return; // Return after executing a ready case
|
|
59
|
+
}
|
|
60
|
+
// 2. If no operations are ready and there's a default case, select default
|
|
61
|
+
if (hasDefault) {
|
|
62
|
+
// Find the default case (it will have id -1)
|
|
63
|
+
const defaultCase = cases.find((c) => c.id === -1);
|
|
64
|
+
if (defaultCase && defaultCase.onSelected) {
|
|
65
|
+
// Execute the onSelected handler for the default case
|
|
66
|
+
await defaultCase.onSelected({
|
|
67
|
+
value: undefined,
|
|
68
|
+
ok: false,
|
|
69
|
+
id: -1,
|
|
70
|
+
}); // Await the handler
|
|
71
|
+
}
|
|
72
|
+
return; // Return after executing the default case
|
|
73
|
+
}
|
|
74
|
+
// 3. If no operations are ready and no default case, block until one is ready
|
|
75
|
+
// Use Promise.race on the blocking promises
|
|
76
|
+
const blockingPromises = cases
|
|
77
|
+
.filter((c) => c.id !== -1) // Exclude default case
|
|
78
|
+
.filter((c) => c.channel !== null) // Exclude nil channels (they would block forever)
|
|
79
|
+
.map((caseObj) => {
|
|
80
|
+
// At this point caseObj.channel is guaranteed to be non-null
|
|
81
|
+
if (caseObj.isSend) {
|
|
82
|
+
return caseObj.channel.selectSend(caseObj.value, caseObj.id);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
return caseObj.channel.selectReceive(caseObj.id);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
// If all non-default cases have nil channels, we effectively block forever
|
|
89
|
+
if (blockingPromises.length === 0) {
|
|
90
|
+
// No valid channels to operate on, block forever (unless there's a default)
|
|
91
|
+
return new Promise(() => { }); // Promise never resolves
|
|
92
|
+
}
|
|
93
|
+
const result = await Promise.race(blockingPromises);
|
|
94
|
+
// Execute onSelected handler for the selected case
|
|
95
|
+
const selectedCase = cases.find((c) => c.id === result.id);
|
|
96
|
+
if (selectedCase && selectedCase.onSelected) {
|
|
97
|
+
await selectedCase.onSelected(result); // Await the handler
|
|
98
|
+
}
|
|
99
|
+
// No explicit return needed here, as the function will implicitly return after the await
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Helper function for channel send operations that handles nil channels correctly.
|
|
103
|
+
* In Go, sending to a nil channel blocks forever.
|
|
104
|
+
* @param channel The channel to send to (can be null)
|
|
105
|
+
* @param value The value to send
|
|
106
|
+
* @returns Promise that never resolves if channel is null, otherwise delegates to channel.send()
|
|
107
|
+
*/
|
|
108
|
+
export async function chanSend(channel, value) {
|
|
109
|
+
if (channel === null) {
|
|
110
|
+
// In Go, sending to a nil channel blocks forever
|
|
111
|
+
return new Promise(() => { }); // Promise that never resolves
|
|
112
|
+
}
|
|
113
|
+
return channel.send(value);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Helper function for channel receive operations that handles nil channels correctly.
|
|
117
|
+
* In Go, receiving from a nil channel blocks forever.
|
|
118
|
+
* @param channel The channel to receive from (can be null)
|
|
119
|
+
* @returns Promise that never resolves if channel is null, otherwise delegates to channel.receive()
|
|
120
|
+
*/
|
|
121
|
+
export async function chanRecv(channel) {
|
|
122
|
+
if (channel === null) {
|
|
123
|
+
// In Go, receiving from a nil channel blocks forever
|
|
124
|
+
return new Promise(() => { }); // Promise that never resolves
|
|
125
|
+
}
|
|
126
|
+
return channel.receive();
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Helper function for channel receive operations with ok value that handles nil channels correctly.
|
|
130
|
+
* In Go, receiving from a nil channel blocks forever.
|
|
131
|
+
* @param channel The channel to receive from (can be null)
|
|
132
|
+
* @returns Promise that never resolves if channel is null, otherwise delegates to channel.receiveWithOk()
|
|
133
|
+
*/
|
|
134
|
+
export async function chanRecvWithOk(channel) {
|
|
135
|
+
if (channel === null) {
|
|
136
|
+
// In Go, receiving from a nil channel blocks forever
|
|
137
|
+
return new Promise(() => { }); // Promise that never resolves
|
|
138
|
+
}
|
|
139
|
+
return channel.receiveWithOk();
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Creates a new channel with the specified buffer size and zero value.
|
|
143
|
+
* @param bufferSize The size of the channel buffer. If 0, creates an unbuffered channel.
|
|
144
|
+
* @param zeroValue The zero value for the channel's element type.
|
|
145
|
+
* @param direction Optional direction for the channel. Default is 'both' (bidirectional).
|
|
146
|
+
* @returns A new channel instance or channel reference.
|
|
147
|
+
*/
|
|
148
|
+
export const makeChannel = (bufferSize, zeroValue, direction = 'both') => {
|
|
149
|
+
const channel = new BufferedChannel(bufferSize, zeroValue);
|
|
150
|
+
// Wrap the channel with the appropriate ChannelRef based on direction
|
|
151
|
+
if (direction === 'send') {
|
|
152
|
+
return new SendOnlyChannelRef(channel);
|
|
153
|
+
}
|
|
154
|
+
else if (direction === 'receive') {
|
|
155
|
+
return new ReceiveOnlyChannelRef(channel);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
return channel;
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
// A simple implementation of buffered channels
|
|
162
|
+
class BufferedChannel {
|
|
163
|
+
buffer = [];
|
|
164
|
+
closed = false;
|
|
165
|
+
capacity;
|
|
166
|
+
zeroValue; // Made public for access by ChannelRef or for type inference
|
|
167
|
+
// Senders queue: stores { value, resolve for send, reject for send }
|
|
168
|
+
senders = [];
|
|
169
|
+
// Receivers queue for receive(): stores { resolve for receive, reject for receive }
|
|
170
|
+
receivers = [];
|
|
171
|
+
// Receivers queue for receiveWithOk(): stores { resolve for receiveWithOk }
|
|
172
|
+
receiversWithOk = [];
|
|
173
|
+
constructor(capacity, zeroValue) {
|
|
174
|
+
if (capacity < 0) {
|
|
175
|
+
throw new Error('Channel capacity cannot be negative');
|
|
176
|
+
}
|
|
177
|
+
this.capacity = capacity;
|
|
178
|
+
this.zeroValue = zeroValue;
|
|
179
|
+
}
|
|
180
|
+
async send(value) {
|
|
181
|
+
if (this.closed) {
|
|
182
|
+
throw new Error('send on closed channel');
|
|
183
|
+
}
|
|
184
|
+
// Attempt to hand off to a waiting receiver (rendezvous)
|
|
185
|
+
if (this.receivers.length > 0) {
|
|
186
|
+
const receiverTask = this.receivers.shift();
|
|
187
|
+
queueMicrotask(() => receiverTask.resolveReceive(value));
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
if (this.receiversWithOk.length > 0) {
|
|
191
|
+
const receiverTask = this.receiversWithOk.shift();
|
|
192
|
+
queueMicrotask(() => receiverTask.resolveReceive({ value, ok: true }));
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
// If no waiting receivers, try to buffer if space is available
|
|
196
|
+
if (this.buffer.length < this.capacity) {
|
|
197
|
+
this.buffer.push(value);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
// Buffer is full (or capacity is 0 and no receivers are waiting). Sender must block.
|
|
201
|
+
return new Promise((resolve, reject) => {
|
|
202
|
+
this.senders.push({ value, resolveSend: resolve, rejectSend: reject });
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
async receive() {
|
|
206
|
+
// Attempt to get from buffer first
|
|
207
|
+
if (this.buffer.length > 0) {
|
|
208
|
+
const value = this.buffer.shift();
|
|
209
|
+
// If a sender was waiting because the buffer was full, unblock it.
|
|
210
|
+
if (this.senders.length > 0) {
|
|
211
|
+
const senderTask = this.senders.shift();
|
|
212
|
+
this.buffer.push(senderTask.value); // Sender's value now goes into buffer
|
|
213
|
+
queueMicrotask(() => senderTask.resolveSend()); // Unblock sender
|
|
214
|
+
}
|
|
215
|
+
return value;
|
|
216
|
+
}
|
|
217
|
+
// Buffer is empty.
|
|
218
|
+
// If channel is closed (and buffer is empty), return zero value.
|
|
219
|
+
if (this.closed) {
|
|
220
|
+
return this.zeroValue;
|
|
221
|
+
}
|
|
222
|
+
// Buffer is empty, channel is open.
|
|
223
|
+
// Attempt to rendezvous with a waiting sender.
|
|
224
|
+
if (this.senders.length > 0) {
|
|
225
|
+
const senderTask = this.senders.shift();
|
|
226
|
+
queueMicrotask(() => senderTask.resolveSend()); // Unblock the sender
|
|
227
|
+
return senderTask.value; // Return the value from sender
|
|
228
|
+
}
|
|
229
|
+
// Buffer is empty, channel is open, no waiting senders. Receiver must block.
|
|
230
|
+
return new Promise((resolve, reject) => {
|
|
231
|
+
this.receivers.push({ resolveReceive: resolve, rejectReceive: reject });
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
async receiveWithOk() {
|
|
235
|
+
// Attempt to get from buffer first
|
|
236
|
+
if (this.buffer.length > 0) {
|
|
237
|
+
const value = this.buffer.shift();
|
|
238
|
+
if (this.senders.length > 0) {
|
|
239
|
+
const senderTask = this.senders.shift();
|
|
240
|
+
this.buffer.push(senderTask.value);
|
|
241
|
+
queueMicrotask(() => senderTask.resolveSend());
|
|
242
|
+
}
|
|
243
|
+
return { value, ok: true };
|
|
244
|
+
}
|
|
245
|
+
// Buffer is empty.
|
|
246
|
+
// Attempt to rendezvous with a waiting sender.
|
|
247
|
+
if (this.senders.length > 0) {
|
|
248
|
+
const senderTask = this.senders.shift();
|
|
249
|
+
queueMicrotask(() => senderTask.resolveSend());
|
|
250
|
+
return { value: senderTask.value, ok: true };
|
|
251
|
+
}
|
|
252
|
+
// Buffer is empty, no waiting senders.
|
|
253
|
+
// If channel is closed, return zero value with ok: false.
|
|
254
|
+
if (this.closed) {
|
|
255
|
+
return { value: this.zeroValue, ok: false };
|
|
256
|
+
}
|
|
257
|
+
// Buffer is empty, channel is open, no waiting senders. Receiver must block.
|
|
258
|
+
return new Promise((resolve) => {
|
|
259
|
+
this.receiversWithOk.push({ resolveReceive: resolve });
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
async selectReceive(id) {
|
|
263
|
+
if (this.buffer.length > 0) {
|
|
264
|
+
const value = this.buffer.shift();
|
|
265
|
+
if (this.senders.length > 0) {
|
|
266
|
+
const senderTask = this.senders.shift();
|
|
267
|
+
this.buffer.push(senderTask.value);
|
|
268
|
+
queueMicrotask(() => senderTask.resolveSend());
|
|
269
|
+
}
|
|
270
|
+
return { value, ok: true, id };
|
|
271
|
+
}
|
|
272
|
+
if (this.senders.length > 0) {
|
|
273
|
+
const senderTask = this.senders.shift();
|
|
274
|
+
queueMicrotask(() => senderTask.resolveSend());
|
|
275
|
+
return { value: senderTask.value, ok: true, id };
|
|
276
|
+
}
|
|
277
|
+
if (this.closed) {
|
|
278
|
+
return { value: this.zeroValue, ok: false, id };
|
|
279
|
+
}
|
|
280
|
+
return new Promise((resolve) => {
|
|
281
|
+
this.receiversWithOk.push({
|
|
282
|
+
resolveReceive: (result) => {
|
|
283
|
+
resolve({ ...result, id });
|
|
284
|
+
},
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
async selectSend(value, id) {
|
|
289
|
+
if (this.closed) {
|
|
290
|
+
// A select case sending on a closed channel panics in Go.
|
|
291
|
+
// This will cause Promise.race in selectStatement to reject.
|
|
292
|
+
throw new Error('send on closed channel');
|
|
293
|
+
}
|
|
294
|
+
if (this.receivers.length > 0) {
|
|
295
|
+
const receiverTask = this.receivers.shift();
|
|
296
|
+
queueMicrotask(() => receiverTask.resolveReceive(value));
|
|
297
|
+
return { value: true, ok: true, id };
|
|
298
|
+
}
|
|
299
|
+
if (this.receiversWithOk.length > 0) {
|
|
300
|
+
const receiverTask = this.receiversWithOk.shift();
|
|
301
|
+
queueMicrotask(() => receiverTask.resolveReceive({ value, ok: true }));
|
|
302
|
+
return { value: true, ok: true, id };
|
|
303
|
+
}
|
|
304
|
+
if (this.buffer.length < this.capacity) {
|
|
305
|
+
this.buffer.push(value);
|
|
306
|
+
return { value: true, ok: true, id };
|
|
307
|
+
}
|
|
308
|
+
return new Promise((resolve, reject) => {
|
|
309
|
+
this.senders.push({
|
|
310
|
+
value,
|
|
311
|
+
resolveSend: () => resolve({ value: true, ok: true, id }),
|
|
312
|
+
rejectSend: (e) => reject(e), // Propagate error if channel closes
|
|
313
|
+
});
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
close() {
|
|
317
|
+
if (this.closed) {
|
|
318
|
+
throw new Error('close of closed channel');
|
|
319
|
+
}
|
|
320
|
+
this.closed = true;
|
|
321
|
+
const sendersToNotify = [...this.senders]; // Shallow copy for iteration
|
|
322
|
+
this.senders = [];
|
|
323
|
+
for (const senderTask of sendersToNotify) {
|
|
324
|
+
queueMicrotask(() => senderTask.rejectSend(new Error('send on closed channel')));
|
|
325
|
+
}
|
|
326
|
+
const receiversToNotify = [...this.receivers];
|
|
327
|
+
this.receivers = [];
|
|
328
|
+
for (const receiverTask of receiversToNotify) {
|
|
329
|
+
queueMicrotask(() => receiverTask.resolveReceive(this.zeroValue));
|
|
330
|
+
}
|
|
331
|
+
const receiversWithOkToNotify = [...this.receiversWithOk];
|
|
332
|
+
this.receiversWithOk = [];
|
|
333
|
+
for (const receiverTask of receiversWithOkToNotify) {
|
|
334
|
+
queueMicrotask(() => receiverTask.resolveReceive({ value: this.zeroValue, ok: false }));
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
canReceiveNonBlocking() {
|
|
338
|
+
return this.buffer.length > 0 || this.senders.length > 0 || this.closed;
|
|
339
|
+
}
|
|
340
|
+
canSendNonBlocking() {
|
|
341
|
+
if (this.closed) {
|
|
342
|
+
return true; // Ready to panic
|
|
343
|
+
}
|
|
344
|
+
return (this.buffer.length < this.capacity ||
|
|
345
|
+
this.receivers.length > 0 ||
|
|
346
|
+
this.receiversWithOk.length > 0);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* A bidirectional channel reference.
|
|
351
|
+
*/
|
|
352
|
+
export class BidirectionalChannelRef {
|
|
353
|
+
channel;
|
|
354
|
+
direction = 'both';
|
|
355
|
+
constructor(channel) {
|
|
356
|
+
this.channel = channel;
|
|
357
|
+
}
|
|
358
|
+
// Delegate all methods to the underlying channel
|
|
359
|
+
send(value) {
|
|
360
|
+
return this.channel.send(value);
|
|
361
|
+
}
|
|
362
|
+
receive() {
|
|
363
|
+
return this.channel.receive();
|
|
364
|
+
}
|
|
365
|
+
receiveWithOk() {
|
|
366
|
+
return this.channel.receiveWithOk();
|
|
367
|
+
}
|
|
368
|
+
close() {
|
|
369
|
+
this.channel.close();
|
|
370
|
+
}
|
|
371
|
+
canSendNonBlocking() {
|
|
372
|
+
return this.channel.canSendNonBlocking();
|
|
373
|
+
}
|
|
374
|
+
canReceiveNonBlocking() {
|
|
375
|
+
return this.channel.canReceiveNonBlocking();
|
|
376
|
+
}
|
|
377
|
+
selectSend(value, id) {
|
|
378
|
+
return this.channel.selectSend(value, id);
|
|
379
|
+
}
|
|
380
|
+
selectReceive(id) {
|
|
381
|
+
return this.channel.selectReceive(id);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* A send-only channel reference.
|
|
386
|
+
*/
|
|
387
|
+
export class SendOnlyChannelRef {
|
|
388
|
+
channel;
|
|
389
|
+
direction = 'send';
|
|
390
|
+
constructor(channel) {
|
|
391
|
+
this.channel = channel;
|
|
392
|
+
}
|
|
393
|
+
// Allow send operations
|
|
394
|
+
send(value) {
|
|
395
|
+
return this.channel.send(value);
|
|
396
|
+
}
|
|
397
|
+
// Allow close operations
|
|
398
|
+
close() {
|
|
399
|
+
this.channel.close();
|
|
400
|
+
}
|
|
401
|
+
canSendNonBlocking() {
|
|
402
|
+
return this.channel.canSendNonBlocking();
|
|
403
|
+
}
|
|
404
|
+
selectSend(value, id) {
|
|
405
|
+
return this.channel.selectSend(value, id);
|
|
406
|
+
}
|
|
407
|
+
// Disallow receive operations
|
|
408
|
+
receive() {
|
|
409
|
+
throw new Error('Cannot receive from send-only channel');
|
|
410
|
+
}
|
|
411
|
+
receiveWithOk() {
|
|
412
|
+
throw new Error('Cannot receive from send-only channel');
|
|
413
|
+
}
|
|
414
|
+
canReceiveNonBlocking() {
|
|
415
|
+
return false;
|
|
416
|
+
}
|
|
417
|
+
selectReceive(id) {
|
|
418
|
+
throw new Error('Cannot receive from send-only channel');
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* A receive-only channel reference.
|
|
423
|
+
*/
|
|
424
|
+
export class ReceiveOnlyChannelRef {
|
|
425
|
+
channel;
|
|
426
|
+
direction = 'receive';
|
|
427
|
+
constructor(channel) {
|
|
428
|
+
this.channel = channel;
|
|
429
|
+
}
|
|
430
|
+
// Allow receive operations
|
|
431
|
+
receive() {
|
|
432
|
+
return this.channel.receive();
|
|
433
|
+
}
|
|
434
|
+
receiveWithOk() {
|
|
435
|
+
return this.channel.receiveWithOk();
|
|
436
|
+
}
|
|
437
|
+
canReceiveNonBlocking() {
|
|
438
|
+
return this.channel.canReceiveNonBlocking();
|
|
439
|
+
}
|
|
440
|
+
selectReceive(id) {
|
|
441
|
+
return this.channel.selectReceive(id);
|
|
442
|
+
}
|
|
443
|
+
// Disallow send operations
|
|
444
|
+
send(value) {
|
|
445
|
+
throw new Error('Cannot send to receive-only channel');
|
|
446
|
+
}
|
|
447
|
+
// Disallow close operations
|
|
448
|
+
close() {
|
|
449
|
+
throw new Error('Cannot close receive-only channel');
|
|
450
|
+
}
|
|
451
|
+
canSendNonBlocking() {
|
|
452
|
+
return false;
|
|
453
|
+
}
|
|
454
|
+
selectSend(value, id) {
|
|
455
|
+
throw new Error('Cannot send to receive-only channel');
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Creates a new channel reference with the specified direction.
|
|
460
|
+
*/
|
|
461
|
+
export function makeChannelRef(channel, direction) {
|
|
462
|
+
switch (direction) {
|
|
463
|
+
case 'send':
|
|
464
|
+
return new SendOnlyChannelRef(channel);
|
|
465
|
+
case 'receive':
|
|
466
|
+
return new ReceiveOnlyChannelRef(channel);
|
|
467
|
+
default: // 'both'
|
|
468
|
+
return new BidirectionalChannelRef(channel);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
//# sourceMappingURL=channel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"channel.js","sourceRoot":"","sources":["../../../gs/builtin/channel.ts"],"names":[],"mappings":"AA6FA;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,KAAsB,EACtB,UAAU,GAAY,KAAK,EACZ;IACf,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC;QACtC,uEAAuE;QACvE,+DAA+D;QAC/D,OAAO,IAAI,OAAO,CAAO,GAAG,EAAE,CAAC,EAAC,CAAC,CAAC,CAAA,CAAC,yBAAyB;IAC9D,CAAC;IAED,+CAA+C;IAC/C,MAAM,UAAU,GAAoB,EAAE,CAAA;IACtC,KAAK,MAAM,OAAO,IAAI,KAAK,EAAE,CAAC;QAC5B,IAAI,OAAO,CAAC,EAAE,KAAK,CAAC,CAAC,EAAE,CAAC;YACtB,kCAAkC;YAClC,SAAQ;QACV,CAAC;QACD,iDAAiD;QACjD,IAAI,OAAO,CAAC,OAAO,KAAK,IAAI,EAAE,CAAC;YAC7B,SAAQ;QACV,CAAC;QACD,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;YACpB,IAAI,OAAO,CAAC,MAAM,IAAI,OAAO,C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IAAA,CACpC;IAED,qBAAqB,GAAY;QAC/B,OAAO,IAAI,CAAC,OAAO,CAAC,qBAAqB,EAAE,CAAA;IAAA,CAC5C;IAED,aAAa,CAAC,EAAU,EAA4B;QAClD,OAAO,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,EAAE,CAAC,CAAA;IAAA,CACtC;IAED,2BAA2B;IAC3B,IAAI,CAAC,KAAQ,EAAiB;QAC5B,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAA;IAAA,CACvD;IAED,4BAA4B;IAC5B,KAAK,GAAS;QACZ,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAA;IAAA,CACrD;IAED,kBAAkB,GAAY;QAC5B,OAAO,KAAK,CAAA;IAAA,CACb;IAED,UAAU,CAAC,KAAQ,EAAE,EAAU,EAAkC;QAC/D,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAA;IAAA,CACvD;CACF;AAED;;GAEG;AACH,MAAM,UAAU,cAAc,CAC5B,OAAmB,EACnB,SAAsC,EACvB;IACf,QAAQ,SAAS,EAAE,CAAC;QAClB,KAAK,MAAM;YACT,OAAO,IAAI,kBAAkB,CAAI,OAAO,CAAC,CAAA;QAC3C,KAAK,SAAS;YACZ,OAAO,IAAI,qBAAqB,CAAI,OAAO,CAAC,CAAA;QAC9C,SAAS,SAAS;YAChB,OAAO,IAAI,uBAAuB,CAAI,OAAO,CAAC,CAAA;IAClD,CAAC;AAAA,CACF"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DisposableStack manages synchronous disposable resources, mimicking Go's defer behavior.
|
|
3
|
+
* Functions added via `defer` are executed in LIFO order when the stack is disposed.
|
|
4
|
+
* Implements the `Disposable` interface for use with `using` declarations.
|
|
5
|
+
*/
|
|
6
|
+
export class DisposableStack {
|
|
7
|
+
stack = [];
|
|
8
|
+
/**
|
|
9
|
+
* Adds a function to be executed when the stack is disposed.
|
|
10
|
+
* @param fn The function to defer.
|
|
11
|
+
*/
|
|
12
|
+
defer(fn) {
|
|
13
|
+
this.stack.push(fn);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Disposes of the resources in the stack by executing the deferred functions
|
|
17
|
+
* in Last-In, First-Out (LIFO) order.
|
|
18
|
+
* If a deferred function throws an error, disposal stops, and the error is rethrown,
|
|
19
|
+
* similar to Go's panic behavior during defer execution.
|
|
20
|
+
*/
|
|
21
|
+
[Symbol.dispose]() {
|
|
22
|
+
// Emulate Go: if a deferred throws, stop and rethrow
|
|
23
|
+
while (this.stack.length) {
|
|
24
|
+
const fn = this.stack.pop();
|
|
25
|
+
fn();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* AsyncDisposableStack manages asynchronous disposable resources, mimicking Go's defer behavior.
|
|
31
|
+
* Functions added via `defer` are executed sequentially in LIFO order when the stack is disposed.
|
|
32
|
+
* Implements the `AsyncDisposable` interface for use with `await using` declarations.
|
|
33
|
+
*/
|
|
34
|
+
export class AsyncDisposableStack {
|
|
35
|
+
stack = [];
|
|
36
|
+
/**
|
|
37
|
+
* Adds a synchronous or asynchronous function to be executed when the stack is disposed.
|
|
38
|
+
* @param fn The function to defer. Can return void or a Promise<void>.
|
|
39
|
+
*/
|
|
40
|
+
defer(fn) {
|
|
41
|
+
this.stack.push(fn);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Asynchronously disposes of the resources in the stack by executing the deferred functions
|
|
45
|
+
* sequentially in Last-In, First-Out (LIFO) order. It awaits each function if it returns a promise.
|
|
46
|
+
*/
|
|
47
|
+
async [Symbol.asyncDispose]() {
|
|
48
|
+
// Execute in LIFO order, awaiting each potentially async function
|
|
49
|
+
for (let i = this.stack.length - 1; i >= 0; --i) {
|
|
50
|
+
await this.stack[i]();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=defer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defer.js","sourceRoot":"","sources":["../../../gs/builtin/defer.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,OAAO,eAAe;IAClB,KAAK,GAAmB,EAAE,CAAA;IAElC;;;OAGG;IACH,KAAK,CAAC,EAAc,EAAQ;QAC1B,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;IAAA,CACpB;IAED;;;;;OAKG;IACH,CAAC,MAAM,CAAC,OAAO,CAAC,GAAS;QACvB,qDAAqD;QACrD,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;YACzB,MAAM,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,EAAG,CAAA;YAC5B,EAAE,EAAE,CAAA;QACN,CAAC;IAAA,CACF;CACF;AAED;;;;GAIG;AACH,MAAM,OAAO,oBAAoB;IACvB,KAAK,GAAmC,EAAE,CAAA;IAElD;;;OAGG;IACH,KAAK,CAAC,EAA8B,EAAQ;QAC1C,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;IAAA,CACpB;IAED;;;OAGG;IACH,KAAK,CAAC,CAAC,MAAM,CAAC,YAAY,CAAC,GAAkB;QAC3C,kEAAkE;QAClE,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC;YAChD,MAAM,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAA;QACvB,CAAC;IAAA,CACF;CACF"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Implementation of Go's built-in println function
|
|
3
|
+
* @param args Arguments to print
|
|
4
|
+
*/
|
|
5
|
+
export function println(...args) {
|
|
6
|
+
console.log(...args);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Implementation of Go's built-in panic function
|
|
10
|
+
* @param args Arguments passed to panic
|
|
11
|
+
*/
|
|
12
|
+
export function panic(...args) {
|
|
13
|
+
throw new Error(`panic: ${args.map((arg) => String(arg)).join(' ')}`);
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=io.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"io.js","sourceRoot":"","sources":["../../../gs/builtin/io.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,UAAU,OAAO,CAAC,GAAG,IAAW,EAAQ;IAC5C,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,CAAA;AAAA,CACrB;AAED;;;GAGG;AACH,MAAM,UAAU,KAAK,CAAC,GAAG,IAAW,EAAQ;IAC1C,MAAM,IAAI,KAAK,CAAC,UAAU,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;AAAA,CACtE"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a new map (TypeScript Map).
|
|
3
|
+
* @returns A new TypeScript Map.
|
|
4
|
+
*/
|
|
5
|
+
export const makeMap = () => {
|
|
6
|
+
return new Map();
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Gets a value from a map, with a default value if the key doesn't exist.
|
|
10
|
+
* @param map The map to get from.
|
|
11
|
+
* @param key The key to get.
|
|
12
|
+
* @param defaultValue The default value to return if the key doesn't exist (defaults to 0).
|
|
13
|
+
* @returns The value for the key, or the default value if the key doesn't exist.
|
|
14
|
+
*/
|
|
15
|
+
export const mapGet = (map, key, defaultValue = null) => {
|
|
16
|
+
return map.has(key) ? map.get(key) : defaultValue;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Sets a value in a map.
|
|
20
|
+
* @param map The map to set in.
|
|
21
|
+
* @param key The key to set.
|
|
22
|
+
* @param value The value to set.
|
|
23
|
+
*/
|
|
24
|
+
export const mapSet = (map, key, value) => {
|
|
25
|
+
map.set(key, value);
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Deletes a key from a map.
|
|
29
|
+
* @param map The map to delete from.
|
|
30
|
+
* @param key The key to delete.
|
|
31
|
+
*/
|
|
32
|
+
export const deleteMapEntry = (map, key) => {
|
|
33
|
+
map.delete(key);
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Checks if a key exists in a map.
|
|
37
|
+
* @param map The map to check in.
|
|
38
|
+
* @param key The key to check.
|
|
39
|
+
* @returns True if the key exists, false otherwise.
|
|
40
|
+
*/
|
|
41
|
+
export const mapHas = (map, key) => {
|
|
42
|
+
return map.has(key);
|
|
43
|
+
};
|
|
44
|
+
//# sourceMappingURL=map.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"map.js","sourceRoot":"","sources":["../../../gs/builtin/map.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,GAAoB,EAAE,CAAC;IAC5C,OAAO,IAAI,GAAG,EAAQ,CAAA;AAAA,CACvB,CAAA;AACD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,CACpB,GAAc,EACd,GAAM,EACN,YAAY,GAAa,IAAI,EACnB,EAAE,CAAC;IACb,OAAO,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAE,CAAC,CAAC,CAAC,YAAY,CAAA;AAAA,CACnD,CAAA;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,CAAO,GAAc,EAAE,GAAM,EAAE,KAAQ,EAAQ,EAAE,CAAC;IACtE,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;AAAA,CACpB,CAAA;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAO,GAAc,EAAE,GAAM,EAAQ,EAAE,CAAC;IACpE,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;AAAA,CAChB,CAAA;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,CAAO,GAAc,EAAE,GAAM,EAAW,EAAE,CAAC;IAC/D,OAAO,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;AAAA,CACpB,CAAA"}
|