goscript 0.0.23 → 0.0.25
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 +2 -2
- package/cmd/goscript/cmd_compile.go +18 -2
- package/compiler/analysis.go +74 -132
- package/compiler/analysis_test.go +220 -0
- package/compiler/assignment.go +37 -43
- package/compiler/builtin_test.go +90 -0
- package/compiler/compiler.go +307 -22
- package/compiler/composite-lit.go +108 -43
- package/compiler/config.go +7 -3
- package/compiler/config_test.go +6 -33
- package/compiler/decl.go +7 -1
- package/compiler/expr-call.go +212 -2
- 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 +125 -20
- package/compiler/field.go +4 -4
- 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 +314 -1
- package/compiler/stmt.go +52 -0
- package/compiler/type.go +83 -15
- package/dist/gs/builtin/builtin.d.ts +9 -0
- package/dist/gs/builtin/builtin.js +46 -0
- package/dist/gs/builtin/builtin.js.map +1 -0
- package/dist/gs/builtin/channel.d.ts +193 -0
- package/dist/gs/builtin/channel.js +471 -0
- package/dist/gs/builtin/channel.js.map +1 -0
- package/dist/gs/builtin/defer.d.ts +38 -0
- package/dist/gs/builtin/defer.js +54 -0
- package/dist/gs/builtin/defer.js.map +1 -0
- 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 +15 -0
- package/dist/gs/builtin/io.js.map +1 -0
- package/dist/gs/builtin/map.d.ts +33 -0
- package/dist/gs/builtin/map.js +44 -0
- package/dist/gs/builtin/map.js.map +1 -0
- package/dist/gs/builtin/slice.d.ts +173 -0
- package/dist/gs/builtin/slice.js +799 -0
- package/dist/gs/builtin/slice.js.map +1 -0
- package/dist/gs/builtin/type.d.ts +203 -0
- package/dist/gs/builtin/type.js +744 -0
- package/dist/gs/builtin/type.js.map +1 -0
- package/dist/gs/builtin/varRef.d.ts +14 -0
- package/dist/gs/builtin/varRef.js +14 -0
- package/dist/gs/builtin/varRef.js.map +1 -0
- 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 +305 -0
- package/dist/gs/context/context.js.map +1 -0
- package/dist/gs/context/index.d.ts +1 -0
- package/dist/gs/context/index.js +2 -0
- package/dist/gs/context/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 +298 -0
- package/dist/gs/math/bits/index.js.map +1 -0
- package/dist/gs/runtime/index.d.ts +1 -0
- package/dist/gs/runtime/index.js +2 -0
- package/dist/gs/runtime/index.js.map +1 -0
- package/dist/gs/runtime/runtime.d.ts +41 -0
- package/dist/gs/runtime/runtime.js +158 -0
- package/dist/gs/runtime/runtime.js.map +1 -0
- 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/index.js +2 -0
- package/dist/gs/time/index.js.map +1 -0
- package/dist/gs/time/time.d.ts +57 -0
- package/dist/gs/time/time.js +208 -0
- package/dist/gs/time/time.js.map +1 -0
- package/package.json +3 -2
|
@@ -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;IAC3C,OAAO,IAAI,GAAG,EAAQ,CAAA;AACxB,CAAC,CAAA;AACD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,CACpB,GAAc,EACd,GAAM,EACN,eAAyB,IAAI,EACnB,EAAE;IACZ,OAAO,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAE,CAAC,CAAC,CAAC,YAAY,CAAA;AACpD,CAAC,CAAA;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,CAAO,GAAc,EAAE,GAAM,EAAE,KAAQ,EAAQ,EAAE;IACrE,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;AACrB,CAAC,CAAA;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAO,GAAc,EAAE,GAAM,EAAQ,EAAE;IACnE,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;AACjB,CAAC,CAAA;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,CAAO,GAAc,EAAE,GAAM,EAAW,EAAE;IAC9D,OAAO,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;AACrB,CAAC,CAAA"}
|
|
@@ -0,0 +1,173 @@
|
|
|
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
|
+
* Slice<number> can be represented as Uint8Array.
|
|
21
|
+
*/
|
|
22
|
+
export type Slice<T> = T[] | SliceProxy<T> | null | (T extends number ? Uint8Array : never);
|
|
23
|
+
export declare function asArray<T>(slice: Slice<T>): T[];
|
|
24
|
+
/**
|
|
25
|
+
* Creates a new slice with the specified length and capacity.
|
|
26
|
+
* @param length The length of the slice.
|
|
27
|
+
* @param capacity The capacity of the slice (optional).
|
|
28
|
+
* @returns A new slice.
|
|
29
|
+
*/
|
|
30
|
+
export declare const makeSlice: <T>(length: number, capacity?: number, typeHint?: string) => Slice<T>;
|
|
31
|
+
/**
|
|
32
|
+
* goSlice creates a slice from s[low:high:max]
|
|
33
|
+
* Arguments mirror Go semantics; omitted indices are undefined.
|
|
34
|
+
*
|
|
35
|
+
* @param s The original slice
|
|
36
|
+
* @param low Starting index (defaults to 0)
|
|
37
|
+
* @param high Ending index (defaults to s.length)
|
|
38
|
+
* @param max Capacity limit (defaults to original capacity)
|
|
39
|
+
*/
|
|
40
|
+
export declare const goSlice: <T>(// T can be number for Uint8Array case
|
|
41
|
+
s: Slice<T>, low?: number, high?: number, max?: number) => Slice<T>;
|
|
42
|
+
/**
|
|
43
|
+
* Converts a JavaScript array to a Go slice.
|
|
44
|
+
* For multi-dimensional arrays, recursively converts nested arrays to slices.
|
|
45
|
+
* @param arr The JavaScript array to convert
|
|
46
|
+
* @param depth How many levels of nesting to convert (default: 1, use Infinity for all levels)
|
|
47
|
+
* @returns A Go slice containing the same elements
|
|
48
|
+
*/
|
|
49
|
+
export declare const arrayToSlice: <T>(arr: T[] | null | undefined, depth?: number) => Slice<T>;
|
|
50
|
+
/**
|
|
51
|
+
* Returns the length of a collection (string, array, slice, map, or set).
|
|
52
|
+
* @param obj The collection to get the length of.
|
|
53
|
+
* @returns The length of the collection.
|
|
54
|
+
*/
|
|
55
|
+
export declare const len: <T = unknown, V = unknown>(obj: string | Array<T> | Slice<T> | Map<T, V> | Set<T> | Uint8Array | null | undefined) => number;
|
|
56
|
+
/**
|
|
57
|
+
* Returns the capacity of a slice.
|
|
58
|
+
* @param obj The slice.
|
|
59
|
+
* @returns The capacity of the slice.
|
|
60
|
+
*/
|
|
61
|
+
export declare const cap: <T>(obj: Slice<T> | Uint8Array) => number;
|
|
62
|
+
/**
|
|
63
|
+
* Appends elements to a slice.
|
|
64
|
+
* Note: In Go, append can return a new slice if the underlying array is reallocated.
|
|
65
|
+
* This helper emulates that by returning the modified or new slice.
|
|
66
|
+
* @param slice The slice to append to.
|
|
67
|
+
* @param elements The elements to append.
|
|
68
|
+
* @returns The modified or new slice.
|
|
69
|
+
*/
|
|
70
|
+
export declare const append: <T>(slice: Slice<T> | Uint8Array, ...elements: any[]) => Slice<T>;
|
|
71
|
+
/**
|
|
72
|
+
* Copies elements from src to dst.
|
|
73
|
+
* @param dst The destination slice.
|
|
74
|
+
* @param src The source slice.
|
|
75
|
+
* @returns The number of elements copied.
|
|
76
|
+
*/
|
|
77
|
+
export declare const copy: <T>(dst: Slice<T>, src: Slice<T>) => number;
|
|
78
|
+
/**
|
|
79
|
+
* Accesses an element at a specific index for various Go-like types (string, slice, array).
|
|
80
|
+
* Mimics Go's indexing behavior: `myCollection[index]`
|
|
81
|
+
* For strings, it returns the byte value at the specified byte index.
|
|
82
|
+
* For slices/arrays, it returns the element at the specified index.
|
|
83
|
+
* This is used when dealing with types like "string | []byte"
|
|
84
|
+
* @param collection The string, Slice, or Array to access.
|
|
85
|
+
* @param index The index.
|
|
86
|
+
* @returns The element or byte value at the specified index.
|
|
87
|
+
* @throws Error if index is out of bounds or type is unsupported.
|
|
88
|
+
*/
|
|
89
|
+
export declare function index<T>(collection: string | Slice<T> | T[], index: number): T | number;
|
|
90
|
+
/**
|
|
91
|
+
* Converts a string to an array of Unicode code points (runes).
|
|
92
|
+
* @param str The input string.
|
|
93
|
+
* @returns An array of numbers representing the Unicode code points.
|
|
94
|
+
*/
|
|
95
|
+
export declare const stringToRunes: (str: string) => number[];
|
|
96
|
+
/**
|
|
97
|
+
* Converts an array of Unicode code points (runes) to a string.
|
|
98
|
+
* @param runes The input array of numbers representing Unicode code points.
|
|
99
|
+
* @returns The resulting string.
|
|
100
|
+
*/
|
|
101
|
+
export declare const runesToString: (runes: Slice<number>) => string;
|
|
102
|
+
/**
|
|
103
|
+
* Converts a number to a byte (uint8) by truncating to the range 0-255.
|
|
104
|
+
* Equivalent to Go's byte() conversion.
|
|
105
|
+
* @param n The number to convert to a byte.
|
|
106
|
+
* @returns The byte value (0-255).
|
|
107
|
+
*/
|
|
108
|
+
export declare const byte: (n: number) => number;
|
|
109
|
+
/**
|
|
110
|
+
* Accesses the byte value at a specific index of a UTF-8 encoded string.
|
|
111
|
+
* Mimics Go's string indexing behavior: `myString[index]`
|
|
112
|
+
* @param str The string to access.
|
|
113
|
+
* @param index The byte index.
|
|
114
|
+
* @returns The byte value (0-255) at the specified index.
|
|
115
|
+
* @throws Error if index is out of bounds.
|
|
116
|
+
*/
|
|
117
|
+
export declare const indexString: (str: string, index: number) => number;
|
|
118
|
+
/**
|
|
119
|
+
* Returns the byte length of a string.
|
|
120
|
+
* Mimics Go's `len(string)` behavior.
|
|
121
|
+
* @param str The string.
|
|
122
|
+
* @returns The number of bytes in the UTF-8 representation of the string.
|
|
123
|
+
*/
|
|
124
|
+
export declare const stringLen: (str: string) => number;
|
|
125
|
+
/**
|
|
126
|
+
* Slices a string based on byte indices.
|
|
127
|
+
* Mimics Go's string slicing behavior: `myString[low:high]` for valid UTF-8 slices only.
|
|
128
|
+
* @param str The string to slice.
|
|
129
|
+
* @param low The starting byte index (inclusive). Defaults to 0.
|
|
130
|
+
* @param high The ending byte index (exclusive). Defaults to string byte length.
|
|
131
|
+
* @returns The sliced string.
|
|
132
|
+
* @throws Error if the slice would create invalid UTF-8.
|
|
133
|
+
*/
|
|
134
|
+
export declare const sliceString: (str: string, low?: number, high?: number) => string;
|
|
135
|
+
/**
|
|
136
|
+
* Converts a Slice<number> (byte array) to a string using TextDecoder.
|
|
137
|
+
* @param bytes The Slice<number> to convert.
|
|
138
|
+
* @returns The resulting string.
|
|
139
|
+
*/
|
|
140
|
+
export declare const bytesToString: (bytes: Slice<number> | Uint8Array) => string;
|
|
141
|
+
/**
|
|
142
|
+
* Converts a string to a Uint8Array (byte slice).
|
|
143
|
+
* @param s The input string.
|
|
144
|
+
* @returns A Uint8Array representing the UTF-8 bytes of the string.
|
|
145
|
+
*/
|
|
146
|
+
export declare function stringToBytes(s: string): Uint8Array;
|
|
147
|
+
/**
|
|
148
|
+
* Handles string() conversion for values that could be either string or []byte.
|
|
149
|
+
* Used for generic type parameters with constraint []byte|string.
|
|
150
|
+
* @param value Value that is either a string or Uint8Array
|
|
151
|
+
* @returns The string representation
|
|
152
|
+
*/
|
|
153
|
+
export declare function genericBytesOrStringToString(value: string | Uint8Array): string;
|
|
154
|
+
/**
|
|
155
|
+
* Indexes into a value that could be either a string or Uint8Array.
|
|
156
|
+
* Used for generic type parameters with constraint string | []byte.
|
|
157
|
+
* Both cases return a byte value (number).
|
|
158
|
+
* @param value Value that is either a string or Uint8Array
|
|
159
|
+
* @param index The index to access
|
|
160
|
+
* @returns The byte value at the specified index
|
|
161
|
+
*/
|
|
162
|
+
export declare function indexStringOrBytes(value: string | Uint8Array, index: number): number;
|
|
163
|
+
/**
|
|
164
|
+
* Slices a value that could be either a string or Uint8Array.
|
|
165
|
+
* Used for generic type parameters with constraint string | []byte.
|
|
166
|
+
* @param value Value that is either a string or Uint8Array
|
|
167
|
+
* @param low Starting index (inclusive). Defaults to 0.
|
|
168
|
+
* @param high Ending index (exclusive). Defaults to length.
|
|
169
|
+
* @param max Capacity limit (only used for Uint8Array, ignored for strings)
|
|
170
|
+
* @returns The sliced value of the same type as input
|
|
171
|
+
*/
|
|
172
|
+
export declare function sliceStringOrBytes<T extends string | Uint8Array>(value: T, low?: number, high?: number, max?: number): T;
|
|
173
|
+
export {};
|