goscript 0.0.21 → 0.0.23
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/cmd/goscript/cmd_compile.go +2 -2
- package/compiler/analysis.go +229 -51
- package/compiler/assignment.go +412 -0
- package/compiler/compiler.go +185 -5885
- package/compiler/compiler_test.go +40 -8
- package/compiler/composite-lit.go +552 -0
- package/compiler/config.go +3 -0
- package/compiler/decl.go +259 -0
- package/compiler/expr-call.go +479 -0
- package/compiler/expr-selector.go +125 -0
- package/compiler/expr-star.go +90 -0
- package/compiler/expr-type.go +309 -0
- package/compiler/expr-value.go +89 -0
- package/compiler/expr.go +591 -0
- package/compiler/field.go +169 -0
- package/compiler/lit.go +131 -0
- package/compiler/primitive.go +148 -0
- package/compiler/{write-type-spec.go → spec-struct.go} +211 -204
- package/compiler/spec-value.go +226 -0
- package/compiler/spec.go +272 -0
- package/compiler/stmt-assign.go +439 -0
- package/compiler/stmt-for.go +178 -0
- package/compiler/stmt-range.go +235 -0
- package/compiler/stmt-select.go +211 -0
- package/compiler/stmt-type-switch.go +147 -0
- package/compiler/stmt.go +792 -0
- package/compiler/type-assert.go +209 -0
- package/compiler/type-info.go +141 -0
- package/compiler/type.go +618 -0
- package/go.mod +2 -1
- package/go.sum +4 -2
- package/package.json +6 -6
- package/builtin/builtin.go +0 -11
- package/builtin/builtin.ts +0 -2114
- package/dist/builtin/builtin.d.ts +0 -495
- package/dist/builtin/builtin.js +0 -1490
- package/dist/builtin/builtin.js.map +0 -1
- /package/compiler/{writer.go → code-writer.go} +0 -0
|
@@ -1,495 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* GoSliceObject contains metadata for complex slice views
|
|
3
|
-
*/
|
|
4
|
-
interface GoSliceObject<T> {
|
|
5
|
-
backing: T[];
|
|
6
|
-
offset: number;
|
|
7
|
-
length: number;
|
|
8
|
-
capacity: number;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* SliceProxy is a proxy object for complex slices
|
|
12
|
-
*/
|
|
13
|
-
export type SliceProxy<T> = T[] & {
|
|
14
|
-
__meta__: GoSliceObject<T>;
|
|
15
|
-
};
|
|
16
|
-
/**
|
|
17
|
-
* Slice<T> is a union type that is either a plain array or a proxy
|
|
18
|
-
* null represents the nil state.
|
|
19
|
-
*/
|
|
20
|
-
export type Slice<T> = T[] | SliceProxy<T> | null;
|
|
21
|
-
export declare function asArray<T>(slice: Slice<T>): T[];
|
|
22
|
-
/**
|
|
23
|
-
* Creates a new slice with the specified length and capacity.
|
|
24
|
-
* @param length The length of the slice.
|
|
25
|
-
* @param capacity The capacity of the slice (optional).
|
|
26
|
-
* @returns A new slice.
|
|
27
|
-
*/
|
|
28
|
-
export declare const makeSlice: <T>(length: number, capacity?: number) => Slice<T>;
|
|
29
|
-
/**
|
|
30
|
-
* goSlice creates a slice from s[low:high:max]
|
|
31
|
-
* Arguments mirror Go semantics; omitted indices are undefined.
|
|
32
|
-
*
|
|
33
|
-
* @param s The original slice
|
|
34
|
-
* @param low Starting index (defaults to 0)
|
|
35
|
-
* @param high Ending index (defaults to s.length)
|
|
36
|
-
* @param max Capacity limit (defaults to original capacity)
|
|
37
|
-
*/
|
|
38
|
-
export declare const goSlice: <T>(s: Slice<T>, low?: number, high?: number, max?: number) => Slice<T>;
|
|
39
|
-
/**
|
|
40
|
-
* Creates a new map (TypeScript Map).
|
|
41
|
-
* @returns A new TypeScript Map.
|
|
42
|
-
*/
|
|
43
|
-
export declare const makeMap: <K, V>() => Map<K, V>;
|
|
44
|
-
/**
|
|
45
|
-
* Converts a JavaScript array to a Go slice.
|
|
46
|
-
* For multi-dimensional arrays, recursively converts nested arrays to slices.
|
|
47
|
-
* @param arr The JavaScript array to convert
|
|
48
|
-
* @param depth How many levels of nesting to convert (default: 1, use Infinity for all levels)
|
|
49
|
-
* @returns A Go slice containing the same elements
|
|
50
|
-
*/
|
|
51
|
-
export declare const arrayToSlice: <T>(arr: T[] | null | undefined, depth?: number) => T[];
|
|
52
|
-
/**
|
|
53
|
-
* Returns the length of a collection (string, array, slice, map, or set).
|
|
54
|
-
* @param obj The collection to get the length of.
|
|
55
|
-
* @returns The length of the collection.
|
|
56
|
-
*/
|
|
57
|
-
export declare const len: <T = unknown, V = unknown>(obj: string | Array<T> | Slice<T> | Map<T, V> | Set<T> | null | undefined) => number;
|
|
58
|
-
/**
|
|
59
|
-
* Returns the capacity of a slice.
|
|
60
|
-
* @param obj The slice.
|
|
61
|
-
* @returns The capacity of the slice.
|
|
62
|
-
*/
|
|
63
|
-
export declare const cap: <T>(obj: Slice<T>) => number;
|
|
64
|
-
/**
|
|
65
|
-
* Appends elements to a slice.
|
|
66
|
-
* Note: In Go, append can return a new slice if the underlying array is reallocated.
|
|
67
|
-
* This helper emulates that by returning the modified or new slice.
|
|
68
|
-
* @param slice The slice to append to.
|
|
69
|
-
* @param elements The elements to append.
|
|
70
|
-
* @returns The modified or new slice.
|
|
71
|
-
*/
|
|
72
|
-
export declare const append: <T>(slice: Slice<T>, ...elements: T[]) => T[];
|
|
73
|
-
/**
|
|
74
|
-
* Copies elements from src to dst.
|
|
75
|
-
* @param dst The destination slice.
|
|
76
|
-
* @param src The source slice.
|
|
77
|
-
* @returns The number of elements copied.
|
|
78
|
-
*/
|
|
79
|
-
export declare const copy: <T>(dst: Slice<T>, src: Slice<T>) => number;
|
|
80
|
-
/**
|
|
81
|
-
* Represents the Go error type (interface).
|
|
82
|
-
*/
|
|
83
|
-
export type GoError = {
|
|
84
|
-
Error(): string;
|
|
85
|
-
} | null;
|
|
86
|
-
/**
|
|
87
|
-
* Converts a string to an array of Unicode code points (runes).
|
|
88
|
-
* @param str The input string.
|
|
89
|
-
* @returns An array of numbers representing the Unicode code points.
|
|
90
|
-
*/
|
|
91
|
-
export declare const stringToRunes: (str: string) => number[];
|
|
92
|
-
/**
|
|
93
|
-
* Converts an array of Unicode code points (runes) to a string.
|
|
94
|
-
* @param runes The input array of numbers representing Unicode code points.
|
|
95
|
-
* @returns The resulting string.
|
|
96
|
-
*/
|
|
97
|
-
export declare const runesToString: (runes: Slice<number>) => string;
|
|
98
|
-
/**
|
|
99
|
-
* Converts a number to a byte (uint8) by truncating to the range 0-255.
|
|
100
|
-
* Equivalent to Go's byte() conversion.
|
|
101
|
-
* @param n The number to convert to a byte.
|
|
102
|
-
* @returns The byte value (0-255).
|
|
103
|
-
*/
|
|
104
|
-
export declare const byte: (n: number) => number;
|
|
105
|
-
/** Box represents a Go variable which can be referred to by other variables.
|
|
106
|
-
*
|
|
107
|
-
* For example:
|
|
108
|
-
* var myVariable int
|
|
109
|
-
*
|
|
110
|
-
*/
|
|
111
|
-
export type Box<T> = {
|
|
112
|
-
value: T;
|
|
113
|
-
};
|
|
114
|
-
/** Wrap a non-null T in a pointer‐box. */
|
|
115
|
-
export declare function box<T>(v: T): Box<T>;
|
|
116
|
-
/** Dereference a pointer‐box, throws on null → simulates Go panic. */
|
|
117
|
-
export declare function unbox<T>(b: Box<T>): T;
|
|
118
|
-
/**
|
|
119
|
-
* Gets a value from a map, with a default value if the key doesn't exist.
|
|
120
|
-
* @param map The map to get from.
|
|
121
|
-
* @param key The key to get.
|
|
122
|
-
* @param defaultValue The default value to return if the key doesn't exist (defaults to 0).
|
|
123
|
-
* @returns The value for the key, or the default value if the key doesn't exist.
|
|
124
|
-
*/
|
|
125
|
-
export declare const mapGet: <K, V>(map: Map<K, V>, key: K, defaultValue?: V | null) => V | null;
|
|
126
|
-
/**
|
|
127
|
-
* Sets a value in a map.
|
|
128
|
-
* @param map The map to set in.
|
|
129
|
-
* @param key The key to set.
|
|
130
|
-
* @param value The value to set.
|
|
131
|
-
*/
|
|
132
|
-
export declare const mapSet: <K, V>(map: Map<K, V>, key: K, value: V) => void;
|
|
133
|
-
/**
|
|
134
|
-
* Deletes a key from a map.
|
|
135
|
-
* @param map The map to delete from.
|
|
136
|
-
* @param key The key to delete.
|
|
137
|
-
*/
|
|
138
|
-
export declare const deleteMapEntry: <K, V>(map: Map<K, V>, key: K) => void;
|
|
139
|
-
/**
|
|
140
|
-
* Checks if a key exists in a map.
|
|
141
|
-
* @param map The map to check in.
|
|
142
|
-
* @param key The key to check.
|
|
143
|
-
* @returns True if the key exists, false otherwise.
|
|
144
|
-
*/
|
|
145
|
-
export declare const mapHas: <K, V>(map: Map<K, V>, key: K) => boolean;
|
|
146
|
-
/**
|
|
147
|
-
* Represents the kinds of Go types that can be registered at runtime.
|
|
148
|
-
*/
|
|
149
|
-
export declare enum TypeKind {
|
|
150
|
-
Basic = "basic",
|
|
151
|
-
Interface = "interface",
|
|
152
|
-
Struct = "struct",
|
|
153
|
-
Map = "map",
|
|
154
|
-
Slice = "slice",
|
|
155
|
-
Array = "array",
|
|
156
|
-
Pointer = "pointer",
|
|
157
|
-
Function = "function",
|
|
158
|
-
Channel = "channel"
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* TypeInfo is used for runtime type checking.
|
|
162
|
-
* Can be a registered type (from typeRegistry) or an ad-hoc type description.
|
|
163
|
-
* When used as input to typeAssert, it can be a string (type name) or a structured description.
|
|
164
|
-
*/
|
|
165
|
-
/**
|
|
166
|
-
* Base type information shared by all type kinds
|
|
167
|
-
*/
|
|
168
|
-
export interface BaseTypeInfo {
|
|
169
|
-
name?: string;
|
|
170
|
-
kind: TypeKind;
|
|
171
|
-
zeroValue?: any;
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Type information for struct types
|
|
175
|
-
*/
|
|
176
|
-
export interface StructTypeInfo extends BaseTypeInfo {
|
|
177
|
-
kind: TypeKind.Struct;
|
|
178
|
-
methods: Set<string>;
|
|
179
|
-
ctor?: new (...args: any[]) => any;
|
|
180
|
-
fields: Record<string, TypeInfo | string>;
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* Type information for interface types
|
|
184
|
-
*/
|
|
185
|
-
export interface InterfaceTypeInfo extends BaseTypeInfo {
|
|
186
|
-
kind: TypeKind.Interface;
|
|
187
|
-
methods: Set<string>;
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* Type information for basic types (string, number, boolean)
|
|
191
|
-
*/
|
|
192
|
-
export interface BasicTypeInfo extends BaseTypeInfo {
|
|
193
|
-
kind: TypeKind.Basic;
|
|
194
|
-
}
|
|
195
|
-
/**
|
|
196
|
-
* Type information for map types
|
|
197
|
-
*/
|
|
198
|
-
export interface MapTypeInfo extends BaseTypeInfo {
|
|
199
|
-
kind: TypeKind.Map;
|
|
200
|
-
keyType?: string | TypeInfo;
|
|
201
|
-
elemType?: string | TypeInfo;
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Type information for slice types
|
|
205
|
-
*/
|
|
206
|
-
export interface SliceTypeInfo extends BaseTypeInfo {
|
|
207
|
-
kind: TypeKind.Slice;
|
|
208
|
-
elemType?: string | TypeInfo;
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* Type information for array types
|
|
212
|
-
*/
|
|
213
|
-
export interface ArrayTypeInfo extends BaseTypeInfo {
|
|
214
|
-
kind: TypeKind.Array;
|
|
215
|
-
elemType?: string | TypeInfo;
|
|
216
|
-
length: number;
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* Type information for pointer types
|
|
220
|
-
*/
|
|
221
|
-
export interface PointerTypeInfo extends BaseTypeInfo {
|
|
222
|
-
kind: TypeKind.Pointer;
|
|
223
|
-
elemType?: string | TypeInfo;
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* Type information for function types
|
|
227
|
-
*/
|
|
228
|
-
export interface FunctionTypeInfo extends BaseTypeInfo {
|
|
229
|
-
kind: TypeKind.Function;
|
|
230
|
-
params?: (string | TypeInfo)[];
|
|
231
|
-
results?: (string | TypeInfo)[];
|
|
232
|
-
}
|
|
233
|
-
/**
|
|
234
|
-
* Type information for channel types
|
|
235
|
-
*/
|
|
236
|
-
export interface ChannelTypeInfo extends BaseTypeInfo {
|
|
237
|
-
kind: TypeKind.Channel;
|
|
238
|
-
elemType?: string | TypeInfo;
|
|
239
|
-
direction?: 'send' | 'receive' | 'both';
|
|
240
|
-
}
|
|
241
|
-
/**
|
|
242
|
-
* Union type representing all possible TypeInfo variants
|
|
243
|
-
*/
|
|
244
|
-
export type TypeInfo = StructTypeInfo | InterfaceTypeInfo | BasicTypeInfo | MapTypeInfo | SliceTypeInfo | ArrayTypeInfo | PointerTypeInfo | FunctionTypeInfo | ChannelTypeInfo;
|
|
245
|
-
export declare function isStructTypeInfo(info: TypeInfo): info is StructTypeInfo;
|
|
246
|
-
export declare function isInterfaceTypeInfo(info: TypeInfo): info is InterfaceTypeInfo;
|
|
247
|
-
export declare function isBasicTypeInfo(info: TypeInfo): info is BasicTypeInfo;
|
|
248
|
-
export declare function isMapTypeInfo(info: TypeInfo): info is MapTypeInfo;
|
|
249
|
-
export declare function isSliceTypeInfo(info: TypeInfo): info is SliceTypeInfo;
|
|
250
|
-
export declare function isArrayTypeInfo(info: TypeInfo): info is ArrayTypeInfo;
|
|
251
|
-
export declare function isPointerTypeInfo(info: TypeInfo): info is PointerTypeInfo;
|
|
252
|
-
export declare function isFunctionTypeInfo(info: TypeInfo): info is FunctionTypeInfo;
|
|
253
|
-
export declare function isChannelTypeInfo(info: TypeInfo): info is ChannelTypeInfo;
|
|
254
|
-
/**
|
|
255
|
-
* Registers a struct type with the runtime type system.
|
|
256
|
-
*
|
|
257
|
-
* @param name The name of the type.
|
|
258
|
-
* @param zeroValue The zero value for the type.
|
|
259
|
-
* @param methods Set of method names for the struct.
|
|
260
|
-
* @param ctor Constructor for the struct.
|
|
261
|
-
* @returns The struct type information object.
|
|
262
|
-
*/
|
|
263
|
-
export declare const registerStructType: (name: string, zeroValue: any, methods: Set<string>, ctor: new (...args: any[]) => any, fields?: Record<string, TypeInfo | string>) => StructTypeInfo;
|
|
264
|
-
/**
|
|
265
|
-
* Registers an interface type with the runtime type system.
|
|
266
|
-
*
|
|
267
|
-
* @param name The name of the type.
|
|
268
|
-
* @param zeroValue The zero value for the type (usually null).
|
|
269
|
-
* @param methods Set of method names the interface requires.
|
|
270
|
-
* @returns The interface type information object.
|
|
271
|
-
*/
|
|
272
|
-
export declare const registerInterfaceType: (name: string, zeroValue: any, methods: Set<string>) => InterfaceTypeInfo;
|
|
273
|
-
/**
|
|
274
|
-
* Represents the result of a type assertion.
|
|
275
|
-
*/
|
|
276
|
-
export interface TypeAssertResult<T> {
|
|
277
|
-
value: T;
|
|
278
|
-
ok: boolean;
|
|
279
|
-
}
|
|
280
|
-
export declare function typeAssert<T>(value: any, typeInfo: string | TypeInfo): TypeAssertResult<T>;
|
|
281
|
-
/**
|
|
282
|
-
* Represents the result of a channel receive operation with 'ok' value
|
|
283
|
-
*/
|
|
284
|
-
export interface ChannelReceiveResult<T> {
|
|
285
|
-
value: T;
|
|
286
|
-
ok: boolean;
|
|
287
|
-
}
|
|
288
|
-
/**
|
|
289
|
-
* Represents a result from a select operation
|
|
290
|
-
*/
|
|
291
|
-
export interface SelectResult<T> {
|
|
292
|
-
value: T;
|
|
293
|
-
ok: boolean;
|
|
294
|
-
id: number;
|
|
295
|
-
}
|
|
296
|
-
/**
|
|
297
|
-
* Represents a Go channel in TypeScript.
|
|
298
|
-
* Supports asynchronous sending and receiving of values.
|
|
299
|
-
*/
|
|
300
|
-
export interface Channel<T> {
|
|
301
|
-
/**
|
|
302
|
-
* Sends a value to the channel.
|
|
303
|
-
* Returns a promise that resolves when the value is accepted by the channel.
|
|
304
|
-
* @param value The value to send.
|
|
305
|
-
*/
|
|
306
|
-
send(value: T): Promise<void>;
|
|
307
|
-
/**
|
|
308
|
-
* Receives a value from the channel.
|
|
309
|
-
* Returns a promise that resolves with the received value.
|
|
310
|
-
* If the channel is closed, it throws an error.
|
|
311
|
-
*/
|
|
312
|
-
receive(): Promise<T>;
|
|
313
|
-
/**
|
|
314
|
-
* Receives a value from the channel along with a boolean indicating
|
|
315
|
-
* whether the channel is still open.
|
|
316
|
-
* Returns a promise that resolves with {value, ok}.
|
|
317
|
-
* - If channel is open and has data: {value: <data>, ok: true}
|
|
318
|
-
* - If channel is closed and empty: {value: <zero value>, ok: false}
|
|
319
|
-
* - If channel is closed but has remaining buffered data: {value: <data>, ok: true}
|
|
320
|
-
*/
|
|
321
|
-
receiveWithOk(): Promise<ChannelReceiveResult<T>>;
|
|
322
|
-
/**
|
|
323
|
-
* Closes the channel.
|
|
324
|
-
* No more values can be sent to a closed channel.
|
|
325
|
-
* Receive operations on a closed channel return the zero value and ok=false.
|
|
326
|
-
*/
|
|
327
|
-
close(): void;
|
|
328
|
-
/**
|
|
329
|
-
* Used in select statements to create a receive operation promise.
|
|
330
|
-
* @param id An identifier for this case in the select statement
|
|
331
|
-
* @returns Promise that resolves when this case is selected
|
|
332
|
-
*/
|
|
333
|
-
selectReceive(id: number): Promise<SelectResult<T>>;
|
|
334
|
-
/**
|
|
335
|
-
* Used in select statements to create a send operation promise.
|
|
336
|
-
* @param value The value to send
|
|
337
|
-
* @param id An identifier for this case in the select statement
|
|
338
|
-
* @returns Promise that resolves when this case is selected
|
|
339
|
-
*/
|
|
340
|
-
selectSend(value: T, id: number): Promise<SelectResult<boolean>>;
|
|
341
|
-
/**
|
|
342
|
-
* Checks if the channel has data ready to be received without blocking.
|
|
343
|
-
* Used for non-blocking select operations.
|
|
344
|
-
*/
|
|
345
|
-
canReceiveNonBlocking(): boolean;
|
|
346
|
-
/**
|
|
347
|
-
* Checks if the channel can accept a send operation without blocking.
|
|
348
|
-
* Used for non-blocking select operations.
|
|
349
|
-
*/
|
|
350
|
-
canSendNonBlocking(): boolean;
|
|
351
|
-
}
|
|
352
|
-
/**
|
|
353
|
-
* Represents a reference to a channel with a specific direction.
|
|
354
|
-
*/
|
|
355
|
-
export interface ChannelRef<T> {
|
|
356
|
-
/**
|
|
357
|
-
* The underlying channel
|
|
358
|
-
*/
|
|
359
|
-
channel: Channel<T>;
|
|
360
|
-
/**
|
|
361
|
-
* The direction of this channel reference
|
|
362
|
-
*/
|
|
363
|
-
direction: 'send' | 'receive' | 'both';
|
|
364
|
-
send(value: T): Promise<void>;
|
|
365
|
-
receive(): Promise<T>;
|
|
366
|
-
receiveWithOk(): Promise<ChannelReceiveResult<T>>;
|
|
367
|
-
close(): void;
|
|
368
|
-
canSendNonBlocking(): boolean;
|
|
369
|
-
canReceiveNonBlocking(): boolean;
|
|
370
|
-
selectSend(value: T, id: number): Promise<SelectResult<boolean>>;
|
|
371
|
-
selectReceive(id: number): Promise<SelectResult<T>>;
|
|
372
|
-
}
|
|
373
|
-
/**
|
|
374
|
-
* A bidirectional channel reference.
|
|
375
|
-
*/
|
|
376
|
-
export declare class BidirectionalChannelRef<T> implements ChannelRef<T> {
|
|
377
|
-
channel: Channel<T>;
|
|
378
|
-
direction: 'both';
|
|
379
|
-
constructor(channel: Channel<T>);
|
|
380
|
-
send(value: T): Promise<void>;
|
|
381
|
-
receive(): Promise<T>;
|
|
382
|
-
receiveWithOk(): Promise<ChannelReceiveResult<T>>;
|
|
383
|
-
close(): void;
|
|
384
|
-
canSendNonBlocking(): boolean;
|
|
385
|
-
canReceiveNonBlocking(): boolean;
|
|
386
|
-
selectSend(value: T, id: number): Promise<SelectResult<boolean>>;
|
|
387
|
-
selectReceive(id: number): Promise<SelectResult<T>>;
|
|
388
|
-
}
|
|
389
|
-
/**
|
|
390
|
-
* A send-only channel reference.
|
|
391
|
-
*/
|
|
392
|
-
export declare class SendOnlyChannelRef<T> implements ChannelRef<T> {
|
|
393
|
-
channel: Channel<T>;
|
|
394
|
-
direction: 'send';
|
|
395
|
-
constructor(channel: Channel<T>);
|
|
396
|
-
send(value: T): Promise<void>;
|
|
397
|
-
close(): void;
|
|
398
|
-
canSendNonBlocking(): boolean;
|
|
399
|
-
selectSend(value: T, id: number): Promise<SelectResult<boolean>>;
|
|
400
|
-
receive(): Promise<T>;
|
|
401
|
-
receiveWithOk(): Promise<ChannelReceiveResult<T>>;
|
|
402
|
-
canReceiveNonBlocking(): boolean;
|
|
403
|
-
selectReceive(id: number): Promise<SelectResult<T>>;
|
|
404
|
-
}
|
|
405
|
-
/**
|
|
406
|
-
* A receive-only channel reference.
|
|
407
|
-
*/
|
|
408
|
-
export declare class ReceiveOnlyChannelRef<T> implements ChannelRef<T> {
|
|
409
|
-
channel: Channel<T>;
|
|
410
|
-
direction: 'receive';
|
|
411
|
-
constructor(channel: Channel<T>);
|
|
412
|
-
receive(): Promise<T>;
|
|
413
|
-
receiveWithOk(): Promise<ChannelReceiveResult<T>>;
|
|
414
|
-
canReceiveNonBlocking(): boolean;
|
|
415
|
-
selectReceive(id: number): Promise<SelectResult<T>>;
|
|
416
|
-
send(value: T): Promise<void>;
|
|
417
|
-
close(): void;
|
|
418
|
-
canSendNonBlocking(): boolean;
|
|
419
|
-
selectSend(value: T, id: number): Promise<SelectResult<boolean>>;
|
|
420
|
-
}
|
|
421
|
-
/**
|
|
422
|
-
* Creates a new channel reference with the specified direction.
|
|
423
|
-
*/
|
|
424
|
-
export declare function makeChannelRef<T>(channel: Channel<T>, direction: 'send' | 'receive' | 'both'): ChannelRef<T>;
|
|
425
|
-
/**
|
|
426
|
-
* Represents a case in a select statement.
|
|
427
|
-
*/
|
|
428
|
-
export interface SelectCase<T> {
|
|
429
|
-
id: number;
|
|
430
|
-
isSend: boolean;
|
|
431
|
-
channel: Channel<any> | ChannelRef<any> | null;
|
|
432
|
-
value?: any;
|
|
433
|
-
onSelected?: (result: SelectResult<T>) => Promise<void>;
|
|
434
|
-
}
|
|
435
|
-
/**
|
|
436
|
-
* Helper for 'select' statements. Takes an array of select cases
|
|
437
|
-
* and resolves when one of them completes, following Go's select rules.
|
|
438
|
-
*
|
|
439
|
-
* @param cases Array of SelectCase objects
|
|
440
|
-
* @param hasDefault Whether there is a default case
|
|
441
|
-
* @returns A promise that resolves with the result of the selected case
|
|
442
|
-
*/
|
|
443
|
-
export declare function selectStatement<T>(cases: SelectCase<T>[], hasDefault?: boolean): Promise<void>;
|
|
444
|
-
/**
|
|
445
|
-
* Creates a new channel with the specified buffer size and zero value.
|
|
446
|
-
* @param bufferSize The size of the channel buffer. If 0, creates an unbuffered channel.
|
|
447
|
-
* @param zeroValue The zero value for the channel's element type.
|
|
448
|
-
* @param direction Optional direction for the channel. Default is 'both' (bidirectional).
|
|
449
|
-
* @returns A new channel instance or channel reference.
|
|
450
|
-
*/
|
|
451
|
-
export declare const makeChannel: <T>(bufferSize: number, zeroValue: T, direction?: "send" | "receive" | "both") => Channel<T> | ChannelRef<T>;
|
|
452
|
-
/**
|
|
453
|
-
* DisposableStack manages synchronous disposable resources, mimicking Go's defer behavior.
|
|
454
|
-
* Functions added via `defer` are executed in LIFO order when the stack is disposed.
|
|
455
|
-
* Implements the `Disposable` interface for use with `using` declarations.
|
|
456
|
-
*/
|
|
457
|
-
export declare class DisposableStack implements Disposable {
|
|
458
|
-
private stack;
|
|
459
|
-
/**
|
|
460
|
-
* Adds a function to be executed when the stack is disposed.
|
|
461
|
-
* @param fn The function to defer.
|
|
462
|
-
*/
|
|
463
|
-
defer(fn: () => void): void;
|
|
464
|
-
/**
|
|
465
|
-
* Disposes of the resources in the stack by executing the deferred functions
|
|
466
|
-
* in Last-In, First-Out (LIFO) order.
|
|
467
|
-
* If a deferred function throws an error, disposal stops, and the error is rethrown,
|
|
468
|
-
* similar to Go's panic behavior during defer execution.
|
|
469
|
-
*/
|
|
470
|
-
[Symbol.dispose](): void;
|
|
471
|
-
}
|
|
472
|
-
/**
|
|
473
|
-
* AsyncDisposableStack manages asynchronous disposable resources, mimicking Go's defer behavior.
|
|
474
|
-
* Functions added via `defer` are executed sequentially in LIFO order when the stack is disposed.
|
|
475
|
-
* Implements the `AsyncDisposable` interface for use with `await using` declarations.
|
|
476
|
-
*/
|
|
477
|
-
export declare class AsyncDisposableStack implements AsyncDisposable {
|
|
478
|
-
private stack;
|
|
479
|
-
/**
|
|
480
|
-
* Adds a synchronous or asynchronous function to be executed when the stack is disposed.
|
|
481
|
-
* @param fn The function to defer. Can return void or a Promise<void>.
|
|
482
|
-
*/
|
|
483
|
-
defer(fn: () => Promise<void> | void): void;
|
|
484
|
-
/**
|
|
485
|
-
* Asynchronously disposes of the resources in the stack by executing the deferred functions
|
|
486
|
-
* sequentially in Last-In, First-Out (LIFO) order. It awaits each function if it returns a promise.
|
|
487
|
-
*/
|
|
488
|
-
[Symbol.asyncDispose](): Promise<void>;
|
|
489
|
-
}
|
|
490
|
-
/**
|
|
491
|
-
* Implementation of Go's built-in println function
|
|
492
|
-
* @param args Arguments to print
|
|
493
|
-
*/
|
|
494
|
-
export declare function println(...args: any[]): void;
|
|
495
|
-
export {};
|