goscript 0.0.2 → 0.0.4
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 +41 -0
- package/builtin/builtin.ts +368 -348
- package/cmd/goscript/main.js +37 -0
- package/compiler/compile_expr.go +3 -37
- package/compiler/index.test.ts +29 -0
- package/compiler/index.ts +85 -0
- package/dist/builtin/builtin.d.ts +168 -0
- package/dist/builtin/builtin.js +372 -0
- package/dist/builtin/builtin.js.map +1 -0
- package/dist/compiler/index.d.ts +26 -0
- package/dist/compiler/index.js +64 -0
- package/dist/compiler/index.js.map +1 -0
- package/package.json +71 -3
- package/.aider-prompt +0 -11
- package/compliance/COMPLIANCE.md +0 -133
- package/compliance/compliance.go +0 -313
- package/compliance/compliance_test.go +0 -57
- package/compliance/tests/array_literal/array_literal.go +0 -15
- package/compliance/tests/array_literal/array_literal.gs.ts +0 -19
- package/compliance/tests/array_literal/expected.log +0 -3
- package/compliance/tests/async_basic/async_basic.go +0 -26
- package/compliance/tests/async_basic/async_basic.gs.ts +0 -30
- package/compliance/tests/async_basic/expected.log +0 -1
- package/compliance/tests/basic_arithmetic/basic_arithmetic.go +0 -15
- package/compliance/tests/basic_arithmetic/basic_arithmetic.gs.ts +0 -19
- package/compliance/tests/basic_arithmetic/expected.log +0 -5
- package/compliance/tests/boolean_logic/boolean_logic.go +0 -13
- package/compliance/tests/boolean_logic/boolean_logic.gs.ts +0 -17
- package/compliance/tests/boolean_logic/expected.log +0 -3
- package/compliance/tests/channel_basic/channel_basic.go +0 -12
- package/compliance/tests/channel_basic/channel_basic.gs.ts +0 -18
- package/compliance/tests/channel_basic/expected.log +0 -1
- package/compliance/tests/composite_literal_assignment/composite_literal_assignment.go +0 -20
- package/compliance/tests/composite_literal_assignment/composite_literal_assignment.gs.ts +0 -27
- package/compliance/tests/composite_literal_assignment/expected.log +0 -2
- package/compliance/tests/constants/constants.go +0 -18
- package/compliance/tests/constants/constants.gs.ts +0 -22
- package/compliance/tests/constants/expected.log +0 -3
- package/compliance/tests/copy_independence/copy_independence.go +0 -29
- package/compliance/tests/copy_independence/copy_independence.gs.ts +0 -36
- package/compliance/tests/copy_independence/expected.log +0 -4
- package/compliance/tests/float64/expected.log +0 -6
- package/compliance/tests/float64/float64.go +0 -28
- package/compliance/tests/float64/float64.gs.ts +0 -32
- package/compliance/tests/for_loop_basic/expected.log +0 -5
- package/compliance/tests/for_loop_basic/for_loop_basic.go +0 -9
- package/compliance/tests/for_loop_basic/for_loop_basic.gs.ts +0 -13
- package/compliance/tests/for_loop_condition_only/expected.log +0 -5
- package/compliance/tests/for_loop_condition_only/main.go +0 -9
- package/compliance/tests/for_loop_condition_only/main.gs.ts +0 -13
- package/compliance/tests/for_range/expected.log +0 -9
- package/compliance/tests/for_range/for_range.go +0 -26
- package/compliance/tests/for_range/for_range.gs.ts +0 -45
- package/compliance/tests/for_range_index_use/expected.log +0 -6
- package/compliance/tests/for_range_index_use/for_range_index_use.go +0 -11
- package/compliance/tests/for_range_index_use/for_range_index_use.gs.ts +0 -18
- package/compliance/tests/func_literal/expected.log +0 -1
- package/compliance/tests/func_literal/func_literal.go +0 -10
- package/compliance/tests/func_literal/func_literal.gs.ts +0 -15
- package/compliance/tests/function_call_result_assignment/expected.log +0 -2
- package/compliance/tests/function_call_result_assignment/function_call_result_assignment.go +0 -24
- package/compliance/tests/function_call_result_assignment/function_call_result_assignment.gs.ts +0 -31
- package/compliance/tests/if_statement/expected.log +0 -1
- package/compliance/tests/if_statement/if_statement.go +0 -11
- package/compliance/tests/if_statement/if_statement.gs.ts +0 -15
- package/compliance/tests/interface_to_interface_type_assertion/expected.log +0 -1
- package/compliance/tests/interface_to_interface_type_assertion/interface_to_interface_type_assertion.go +0 -30
- package/compliance/tests/interface_to_interface_type_assertion/interface_to_interface_type_assertion.gs.ts +0 -41
- package/compliance/tests/interface_type_assertion/expected.log +0 -1
- package/compliance/tests/interface_type_assertion/interface_type_assertion.go +0 -26
- package/compliance/tests/interface_type_assertion/interface_type_assertion.gs.ts +0 -36
- package/compliance/tests/map_support/expected.log +0 -13
- package/compliance/tests/map_support/map_support.go +0 -89
- package/compliance/tests/map_support/map_support.gs.ts +0 -102
- package/compliance/tests/method_call_on_pointer_receiver/expected.log +0 -1
- package/compliance/tests/method_call_on_pointer_receiver/method_call_on_pointer_receiver.go +0 -19
- package/compliance/tests/method_call_on_pointer_receiver/method_call_on_pointer_receiver.gs.ts +0 -27
- package/compliance/tests/method_call_on_pointer_via_value/expected.log +0 -1
- package/compliance/tests/method_call_on_pointer_via_value/method_call_on_pointer_via_value.go +0 -29
- package/compliance/tests/method_call_on_pointer_via_value/method_call_on_pointer_via_value.gs.ts +0 -38
- package/compliance/tests/method_call_on_value_receiver/expected.log +0 -1
- package/compliance/tests/method_call_on_value_receiver/method_call_on_value_receiver.go +0 -16
- package/compliance/tests/method_call_on_value_receiver/method_call_on_value_receiver.gs.ts +0 -24
- package/compliance/tests/method_call_on_value_via_pointer/expected.log +0 -2
- package/compliance/tests/method_call_on_value_via_pointer/method_call_on_value_via_pointer.go +0 -30
- package/compliance/tests/method_call_on_value_via_pointer/method_call_on_value_via_pointer.gs.ts +0 -38
- package/compliance/tests/multiple_return_values/expected.log +0 -6
- package/compliance/tests/multiple_return_values/multiple_return_values.go +0 -19
- package/compliance/tests/multiple_return_values/multiple_return_values.gs.ts +0 -23
- package/compliance/tests/pointer_assignment_no_copy/expected.log +0 -2
- package/compliance/tests/pointer_assignment_no_copy/pointer_assignment_no_copy.go +0 -28
- package/compliance/tests/pointer_assignment_no_copy/pointer_assignment_no_copy.gs.ts +0 -35
- package/compliance/tests/pointer_composite_literal_assignment/expected.log +0 -3
- package/compliance/tests/pointer_composite_literal_assignment/pointer_composite_literal_assignment.go +0 -23
- package/compliance/tests/pointer_composite_literal_assignment/pointer_composite_literal_assignment.gs.ts +0 -30
- package/compliance/tests/pointer_deref_multiassign/expected.log +0 -0
- package/compliance/tests/pointer_deref_multiassign/pointer_deref_multiassign.go +0 -17
- package/compliance/tests/pointer_deref_multiassign/pointer_deref_multiassign.gs.ts +0 -27
- package/compliance/tests/pointer_initialization/expected.log +0 -1
- package/compliance/tests/pointer_initialization/pointer_initialization.go +0 -16
- package/compliance/tests/pointer_initialization/pointer_initialization.gs.ts +0 -22
- package/compliance/tests/select_receive_on_closed_channel_no_default/expected.log +0 -1
- package/compliance/tests/select_receive_on_closed_channel_no_default/select_receive_on_closed_channel_no_default.go +0 -15
- package/compliance/tests/select_receive_on_closed_channel_no_default/select_receive_on_closed_channel_no_default.gs.ts +0 -31
- package/compliance/tests/select_send_on_full_buffered_channel_with_default/expected.log +0 -1
- package/compliance/tests/select_send_on_full_buffered_channel_with_default/select_send_on_full_buffered_channel_with_default.go +0 -13
- package/compliance/tests/select_send_on_full_buffered_channel_with_default/select_send_on_full_buffered_channel_with_default.gs.ts +0 -35
- package/compliance/tests/select_statement/expected.log +0 -9
- package/compliance/tests/select_statement/select_statement.go +0 -109
- package/compliance/tests/select_statement/select_statement.gs.ts +0 -239
- package/compliance/tests/simple/expected.log +0 -1
- package/compliance/tests/simple/simple.go +0 -5
- package/compliance/tests/simple/simple.gs.ts +0 -9
- package/compliance/tests/simple_deref_assignment/expected.log +0 -2
- package/compliance/tests/simple_deref_assignment/simple_deref_assignment.go +0 -19
- package/compliance/tests/simple_deref_assignment/simple_deref_assignment.gs.ts +0 -26
- package/compliance/tests/slices/expected.log +0 -7
- package/compliance/tests/slices/slices.go +0 -22
- package/compliance/tests/slices/slices.gs.ts +0 -26
- package/compliance/tests/string_rune_conversion/expected.log +0 -3
- package/compliance/tests/string_rune_conversion/string_rune_conversion.go +0 -16
- package/compliance/tests/string_rune_conversion/string_rune_conversion.gs.ts +0 -22
- package/compliance/tests/struct_field_access/expected.log +0 -2
- package/compliance/tests/struct_field_access/struct_field_access.go +0 -13
- package/compliance/tests/struct_field_access/struct_field_access.gs.ts +0 -20
- package/compliance/tests/struct_value_init_clone/expected.log +0 -5
- package/compliance/tests/struct_value_init_clone/struct_value_init_clone.go +0 -28
- package/compliance/tests/struct_value_init_clone/struct_value_init_clone.gs.ts +0 -35
- package/compliance/tests/switch_statement/expected.log +0 -14
- package/compliance/tests/switch_statement/switch_statement.go +0 -59
- package/compliance/tests/switch_statement/switch_statement.gs.ts +0 -85
- package/compliance/tests/value_type_copy_behavior/expected.log +0 -3
- package/compliance/tests/value_type_copy_behavior/value_type_copy_behavior.go +0 -25
- package/compliance/tests/value_type_copy_behavior/value_type_copy_behavior.gs.ts +0 -34
- package/design/DESIGN.md +0 -599
- package/example/simple/build.bash +0 -10
- package/example/simple/go.mod +0 -23
- package/example/simple/go.sum +0 -39
- package/example/simple/main.go +0 -138
- package/example/simple/main.gs.ts +0 -133
- package/example/simple/main.ts +0 -3
- package/example/simple/main_test.go +0 -59
- package/example/simple/main_tools.go +0 -5
- package/example/simple/package.json +0 -7
- package/example/simple/run.bash +0 -6
- package/example/simple/tsconfig.json +0 -28
- package/example/simple/yarn.lock +0 -8
- package/output/output.go +0 -10
- package/tsconfig.json +0 -10
- package/types/tokens.go +0 -65
- package/types/types.go +0 -46
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a new slice (TypeScript array) with the specified length and capacity.
|
|
3
|
+
* @param len The length of the slice.
|
|
4
|
+
* @param cap The capacity of the slice (optional).
|
|
5
|
+
* @returns A new TypeScript array representing the slice.
|
|
6
|
+
*/
|
|
7
|
+
export const makeSlice = (len, cap) => {
|
|
8
|
+
const slice = new Array(len);
|
|
9
|
+
slice.__capacity = cap !== undefined ? cap : len;
|
|
10
|
+
return slice;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new map (TypeScript Map).
|
|
14
|
+
* @returns A new TypeScript Map.
|
|
15
|
+
*/
|
|
16
|
+
export const makeMap = () => {
|
|
17
|
+
return new Map();
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Returns the length of a collection (string, array, or map).
|
|
21
|
+
* @param collection The collection to get the length of.
|
|
22
|
+
* @returns The length of the collection.
|
|
23
|
+
*/
|
|
24
|
+
export const len = (collection) => {
|
|
25
|
+
if (typeof collection === 'string' || Array.isArray(collection)) {
|
|
26
|
+
return collection.length;
|
|
27
|
+
}
|
|
28
|
+
else if (collection instanceof Map) {
|
|
29
|
+
return collection.size;
|
|
30
|
+
}
|
|
31
|
+
return 0; // Default fallback
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Returns the capacity of a slice (TypeScript array).
|
|
35
|
+
* @param slice The slice (TypeScript array).
|
|
36
|
+
* @returns The capacity of the slice.
|
|
37
|
+
*/
|
|
38
|
+
export const cap = (slice) => {
|
|
39
|
+
return slice.__capacity !== undefined ? slice.__capacity : slice.length;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Converts a string to an array of Unicode code points (runes).
|
|
43
|
+
* @param str The input string.
|
|
44
|
+
* @returns An array of numbers representing the Unicode code points.
|
|
45
|
+
*/
|
|
46
|
+
export const stringToRunes = (str) => {
|
|
47
|
+
return Array.from(str).map((c) => c.codePointAt(0) || 0);
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Gets a value from a map, with a default value if the key doesn't exist.
|
|
51
|
+
* @param map The map to get from.
|
|
52
|
+
* @param key The key to get.
|
|
53
|
+
* @param defaultValue The default value to return if the key doesn't exist (defaults to 0).
|
|
54
|
+
* @returns The value for the key, or the default value if the key doesn't exist.
|
|
55
|
+
*/
|
|
56
|
+
export const mapGet = (map, key, defaultValue = null) => {
|
|
57
|
+
return map.has(key) ? map.get(key) : defaultValue;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Sets a value in a map.
|
|
61
|
+
* @param map The map to set in.
|
|
62
|
+
* @param key The key to set.
|
|
63
|
+
* @param value The value to set.
|
|
64
|
+
*/
|
|
65
|
+
export const mapSet = (map, key, value) => {
|
|
66
|
+
map.set(key, value);
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Deletes a key from a map.
|
|
70
|
+
* @param map The map to delete from.
|
|
71
|
+
* @param key The key to delete.
|
|
72
|
+
*/
|
|
73
|
+
export const deleteMapEntry = (map, key) => {
|
|
74
|
+
map.delete(key);
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Checks if a key exists in a map.
|
|
78
|
+
* @param map The map to check in.
|
|
79
|
+
* @param key The key to check.
|
|
80
|
+
* @returns True if the key exists, false otherwise.
|
|
81
|
+
*/
|
|
82
|
+
export const mapHas = (map, key) => {
|
|
83
|
+
return map.has(key);
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Appends elements to a slice (TypeScript array).
|
|
87
|
+
* Note: In Go, append can return a new slice if the underlying array is reallocated.
|
|
88
|
+
* This helper emulates that by returning the modified array.
|
|
89
|
+
* @param slice The slice (TypeScript array) to append to.
|
|
90
|
+
* @param elements The elements to append.
|
|
91
|
+
* @returns The modified slice (TypeScript array).
|
|
92
|
+
*/
|
|
93
|
+
export const append = (slice, ...elements) => {
|
|
94
|
+
slice.push(...elements);
|
|
95
|
+
return slice;
|
|
96
|
+
};
|
|
97
|
+
// A simple implementation of buffered channels
|
|
98
|
+
class BufferedChannel {
|
|
99
|
+
buffer = [];
|
|
100
|
+
closed = false;
|
|
101
|
+
capacity;
|
|
102
|
+
senders = []; // Resolvers for blocked senders
|
|
103
|
+
receivers = []; // Resolvers for blocked receivers
|
|
104
|
+
receiversWithOk = []; // For receive with ok
|
|
105
|
+
zeroValue; // Store the zero value for the element type
|
|
106
|
+
constructor(capacity, zeroValue) {
|
|
107
|
+
this.capacity = capacity;
|
|
108
|
+
this.zeroValue = zeroValue;
|
|
109
|
+
}
|
|
110
|
+
async send(value) {
|
|
111
|
+
if (this.closed) {
|
|
112
|
+
throw new Error('send on closed channel');
|
|
113
|
+
}
|
|
114
|
+
// If there are waiting receivers, directly pass the value
|
|
115
|
+
if (this.receivers.length > 0) {
|
|
116
|
+
const receiver = this.receivers.shift();
|
|
117
|
+
receiver(value);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
// If there are waiting receivers with ok, directly pass the value and ok=true
|
|
121
|
+
if (this.receiversWithOk.length > 0) {
|
|
122
|
+
const receiver = this.receiversWithOk.shift();
|
|
123
|
+
receiver({ value, ok: true });
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
// If buffer is not full, add to buffer
|
|
127
|
+
if (this.buffer.length < this.capacity) {
|
|
128
|
+
this.buffer.push(value);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
// Buffer is full, block the sender
|
|
132
|
+
return new Promise((resolve, reject) => {
|
|
133
|
+
this.senders.push((success) => {
|
|
134
|
+
if (success) {
|
|
135
|
+
this.buffer.push(value);
|
|
136
|
+
resolve();
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
reject(new Error('send on closed channel'));
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
async receive() {
|
|
145
|
+
// If buffer has values, return from buffer
|
|
146
|
+
if (this.buffer.length > 0) {
|
|
147
|
+
const value = this.buffer.shift();
|
|
148
|
+
// If there are waiting senders, unblock one
|
|
149
|
+
if (this.senders.length > 0) {
|
|
150
|
+
const sender = this.senders.shift();
|
|
151
|
+
sender(true); // Unblock with success
|
|
152
|
+
}
|
|
153
|
+
return value;
|
|
154
|
+
}
|
|
155
|
+
// If channel is closed and buffer is empty, throw error (receive without ok)
|
|
156
|
+
if (this.closed) {
|
|
157
|
+
throw new Error('receive on closed channel');
|
|
158
|
+
}
|
|
159
|
+
// Buffer is empty, block the receiver
|
|
160
|
+
return new Promise((resolve) => {
|
|
161
|
+
this.receivers.push(resolve);
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
async receiveWithOk() {
|
|
165
|
+
// If buffer has values, return from buffer with ok=true
|
|
166
|
+
if (this.buffer.length > 0) {
|
|
167
|
+
const value = this.buffer.shift();
|
|
168
|
+
// If there are waiting senders, unblock one
|
|
169
|
+
if (this.senders.length > 0) {
|
|
170
|
+
const sender = this.senders.shift();
|
|
171
|
+
sender(true); // Unblock with success
|
|
172
|
+
}
|
|
173
|
+
return { value, ok: true };
|
|
174
|
+
}
|
|
175
|
+
// If channel is closed and buffer is empty, return zero value with ok=false
|
|
176
|
+
if (this.closed) {
|
|
177
|
+
return { value: this.zeroValue, ok: false }; // Return zeroValue
|
|
178
|
+
}
|
|
179
|
+
// Buffer is empty, block the receiver with ok
|
|
180
|
+
return new Promise((resolve) => {
|
|
181
|
+
this.receiversWithOk.push(resolve);
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
async selectReceive(id) {
|
|
185
|
+
// If buffer has values, return from buffer with ok=true
|
|
186
|
+
if (this.buffer.length > 0) {
|
|
187
|
+
const value = this.buffer.shift();
|
|
188
|
+
// If there are waiting senders, unblock one
|
|
189
|
+
if (this.senders.length > 0) {
|
|
190
|
+
const sender = this.senders.shift();
|
|
191
|
+
sender(true); // Unblock with success
|
|
192
|
+
}
|
|
193
|
+
return { value, ok: true, id };
|
|
194
|
+
}
|
|
195
|
+
// If channel is closed and buffer is empty, return zero value with ok=false
|
|
196
|
+
if (this.closed) {
|
|
197
|
+
return { value: this.zeroValue, ok: false, id }; // Return zeroValue
|
|
198
|
+
}
|
|
199
|
+
// Buffer is empty, return a promise that will be resolved when a value is available
|
|
200
|
+
return new Promise((resolve) => {
|
|
201
|
+
this.receiversWithOk.push((result) => {
|
|
202
|
+
resolve({ ...result, id });
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
async selectSend(value, id) {
|
|
207
|
+
if (this.closed) {
|
|
208
|
+
return { value: false, ok: false, id };
|
|
209
|
+
}
|
|
210
|
+
// If there are waiting receivers, directly pass the value
|
|
211
|
+
if (this.receivers.length > 0) {
|
|
212
|
+
const receiver = this.receivers.shift();
|
|
213
|
+
receiver(value);
|
|
214
|
+
return { value: true, ok: true, id };
|
|
215
|
+
}
|
|
216
|
+
// If there are waiting receivers with ok, directly pass the value and ok=true
|
|
217
|
+
if (this.receiversWithOk.length > 0) {
|
|
218
|
+
const receiver = this.receiversWithOk.shift();
|
|
219
|
+
receiver({ value, ok: true });
|
|
220
|
+
return { value: true, ok: true, id };
|
|
221
|
+
}
|
|
222
|
+
// If buffer is not full, add to buffer
|
|
223
|
+
if (this.buffer.length < this.capacity) {
|
|
224
|
+
this.buffer.push(value);
|
|
225
|
+
return { value: true, ok: true, id };
|
|
226
|
+
}
|
|
227
|
+
// Buffer is full, return a promise that will be resolved when buffer space is available
|
|
228
|
+
return new Promise((resolve) => {
|
|
229
|
+
this.senders.push((success) => {
|
|
230
|
+
if (success) {
|
|
231
|
+
this.buffer.push(value);
|
|
232
|
+
resolve({ value: true, ok: true, id });
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
resolve({ value: false, ok: false, id });
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
close() {
|
|
241
|
+
if (this.closed) {
|
|
242
|
+
throw new Error('close of closed channel');
|
|
243
|
+
}
|
|
244
|
+
this.closed = true;
|
|
245
|
+
// Unblock all waiting senders with failure
|
|
246
|
+
for (const sender of this.senders) {
|
|
247
|
+
sender(false);
|
|
248
|
+
}
|
|
249
|
+
this.senders = [];
|
|
250
|
+
// Unblock all waiting receivers with the zero value
|
|
251
|
+
for (const receiver of this.receivers) {
|
|
252
|
+
// Note: receive() without ok throws on closed channel, this unblocking should not happen in correct Go logic
|
|
253
|
+
// but for safety, we'll resolve with zero value if it somehow does.
|
|
254
|
+
receiver(this.zeroValue);
|
|
255
|
+
}
|
|
256
|
+
this.receivers = [];
|
|
257
|
+
// Unblock all waiting receivers with ok=false and zero value
|
|
258
|
+
for (const receiver of this.receiversWithOk) {
|
|
259
|
+
receiver({ value: this.zeroValue, ok: false });
|
|
260
|
+
}
|
|
261
|
+
this.receiversWithOk = [];
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Checks if the channel has data ready to be received without blocking.
|
|
265
|
+
* Used for non-blocking select operations.
|
|
266
|
+
*/
|
|
267
|
+
canReceiveNonBlocking() {
|
|
268
|
+
return this.buffer.length > 0 || this.closed;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Checks if the channel can accept a send operation without blocking.
|
|
272
|
+
* Used for non-blocking select operations.
|
|
273
|
+
*/
|
|
274
|
+
canSendNonBlocking() {
|
|
275
|
+
return !this.closed && this.buffer.length < this.capacity;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Helper for 'select' statements. Takes an array of select cases
|
|
280
|
+
* and resolves when one of them completes, following Go's select rules.
|
|
281
|
+
*
|
|
282
|
+
* @param cases Array of SelectCase objects
|
|
283
|
+
* @param hasDefault Whether there is a default case
|
|
284
|
+
* @returns A promise that resolves with the result of the selected case
|
|
285
|
+
*/
|
|
286
|
+
export async function selectStatement(cases, hasDefault = false) {
|
|
287
|
+
if (cases.length === 0 && !hasDefault) {
|
|
288
|
+
// Go spec: If there are no cases, the select statement blocks forever.
|
|
289
|
+
// Emulate blocking forever with a promise that never resolves.
|
|
290
|
+
return new Promise(() => { }); // Promise never resolves
|
|
291
|
+
}
|
|
292
|
+
// 1. Check for ready (non-blocking) operations
|
|
293
|
+
const readyCases = [];
|
|
294
|
+
for (const caseObj of cases) {
|
|
295
|
+
if (caseObj.id === -1) {
|
|
296
|
+
// Skip default case in this check
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
if (caseObj.isSend) {
|
|
300
|
+
if (caseObj.channel.canSendNonBlocking()) {
|
|
301
|
+
readyCases.push(caseObj);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
else {
|
|
305
|
+
if (caseObj.channel.canReceiveNonBlocking()) {
|
|
306
|
+
readyCases.push(caseObj);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
if (readyCases.length > 0) {
|
|
311
|
+
// If one or more cases are ready, choose one pseudo-randomly
|
|
312
|
+
const selectedCase = readyCases[Math.floor(Math.random() * readyCases.length)];
|
|
313
|
+
// Execute the selected operation and its onSelected handler
|
|
314
|
+
if (selectedCase.isSend) {
|
|
315
|
+
const result = await selectedCase.channel.selectSend(selectedCase.value, selectedCase.id);
|
|
316
|
+
if (selectedCase.onSelected) {
|
|
317
|
+
await selectedCase.onSelected(result); // Await the handler
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
const result = await selectedCase.channel.selectReceive(selectedCase.id);
|
|
322
|
+
if (selectedCase.onSelected) {
|
|
323
|
+
await selectedCase.onSelected(result); // Await the handler
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
return; // Return after executing a ready case
|
|
327
|
+
}
|
|
328
|
+
// 2. If no operations are ready and there's a default case, select default
|
|
329
|
+
if (hasDefault) {
|
|
330
|
+
// Find the default case (it will have id -1)
|
|
331
|
+
const defaultCase = cases.find((c) => c.id === -1);
|
|
332
|
+
if (defaultCase && defaultCase.onSelected) {
|
|
333
|
+
// Execute the onSelected handler for the default case
|
|
334
|
+
await defaultCase.onSelected({
|
|
335
|
+
value: undefined,
|
|
336
|
+
ok: false,
|
|
337
|
+
id: -1,
|
|
338
|
+
}); // Await the handler
|
|
339
|
+
}
|
|
340
|
+
return; // Return after executing the default case
|
|
341
|
+
}
|
|
342
|
+
// 3. If no operations are ready and no default case, block until one is ready
|
|
343
|
+
// Use Promise.race on the blocking promises
|
|
344
|
+
const blockingPromises = cases
|
|
345
|
+
.filter((c) => c.id !== -1)
|
|
346
|
+
.map((caseObj) => {
|
|
347
|
+
// Exclude default case
|
|
348
|
+
if (caseObj.isSend) {
|
|
349
|
+
return caseObj.channel.selectSend(caseObj.value, caseObj.id);
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
return caseObj.channel.selectReceive(caseObj.id);
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
const result = await Promise.race(blockingPromises);
|
|
356
|
+
// Execute onSelected handler for the selected case
|
|
357
|
+
const selectedCase = cases.find((c) => c.id === result.id);
|
|
358
|
+
if (selectedCase && selectedCase.onSelected) {
|
|
359
|
+
await selectedCase.onSelected(result); // Await the handler
|
|
360
|
+
}
|
|
361
|
+
// No explicit return needed here, as the function will implicitly return after the await
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Creates a new channel with the specified buffer size and zero value.
|
|
365
|
+
* @param bufferSize The size of the channel buffer. If 0, creates an unbuffered channel.
|
|
366
|
+
* @param zeroValue The zero value for the channel's element type.
|
|
367
|
+
* @returns A new channel instance.
|
|
368
|
+
*/
|
|
369
|
+
export const makeChannel = (bufferSize, zeroValue) => {
|
|
370
|
+
return new BufferedChannel(bufferSize, zeroValue);
|
|
371
|
+
};
|
|
372
|
+
//# sourceMappingURL=builtin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builtin.js","sourceRoot":"","sources":["../../builtin/builtin.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,CACvB,GAAW,EACX,GAAY,EACwB,EAAE;IACtC,MAAM,KAAK,GAAG,IAAI,KAAK,CAAI,GAAG,CAAuC,CAAA;IACrE,KAAK,CAAC,UAAU,GAAG,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAA;IAChD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,GAAoB,EAAE;IAC3C,OAAO,IAAI,GAAG,EAAQ,CAAA;AACxB,CAAC,CAAA;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,GAAG,GAAG,CACjB,UAAqD,EAC7C,EAAE;IACV,IAAI,OAAO,UAAU,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC;QAChE,OAAO,UAAU,C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UAAU,CAAC,MAAM,CAAC,CAAA,CAAC,oBAAoB;IAC5D,CAAC;IACD,yFAAyF;AAC3F,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,UAAkB,EAClB,SAAY,EACA,EAAE;IACd,OAAO,IAAI,eAAe,CAAI,UAAU,EAAE,SAAS,CAAC,CAAA;AACtD,CAAC,CAAA"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration options for the GoScript compiler.
|
|
3
|
+
*/
|
|
4
|
+
export interface CompileConfig {
|
|
5
|
+
/** The Go package path or pattern to compile. */
|
|
6
|
+
pkg: string;
|
|
7
|
+
/** The output directory for the generated TypeScript files. Defaults to './output'. */
|
|
8
|
+
output?: string;
|
|
9
|
+
/** The working directory for the compiler. Defaults to the current working directory. */
|
|
10
|
+
dir?: string;
|
|
11
|
+
/** The path to the goscript executable. Defaults to 'go run github.com/paralin/goscript/cmd/goscript'. */
|
|
12
|
+
goscriptPath?: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Compiles a Go package to TypeScript using the goscript compiler.
|
|
16
|
+
* @param config - The compilation configuration.
|
|
17
|
+
* @returns A promise that resolves when compilation is complete, or rejects on error.
|
|
18
|
+
*/
|
|
19
|
+
export declare function compile(config: CompileConfig): Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* Default export for convenience.
|
|
22
|
+
*/
|
|
23
|
+
declare const _default: {
|
|
24
|
+
compile: typeof compile;
|
|
25
|
+
};
|
|
26
|
+
export default _default;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import * as path from "path";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { exec } from "node:child_process";
|
|
5
|
+
import { promisify } from "node:util";
|
|
6
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
7
|
+
const execAsync = promisify(exec);
|
|
8
|
+
const __dirname = dirname(__filename);
|
|
9
|
+
const projectRoot = dirname(__dirname); // Go up one level from src/ to the project root
|
|
10
|
+
/**
|
|
11
|
+
* Compiles a Go package to TypeScript using the goscript compiler.
|
|
12
|
+
* @param config - The compilation configuration.
|
|
13
|
+
* @returns A promise that resolves when compilation is complete, or rejects on error.
|
|
14
|
+
*/
|
|
15
|
+
export async function compile(config) {
|
|
16
|
+
if (!config.pkg) {
|
|
17
|
+
throw new Error("Package path (pkg) must be specified.");
|
|
18
|
+
}
|
|
19
|
+
// Construct the go run command with the absolute path to the goscript executable
|
|
20
|
+
const goscriptCmd = config.goscriptPath ??
|
|
21
|
+
`go run "${path.join(projectRoot, "./cmd/goscript")}"`;
|
|
22
|
+
const args = ["compile", "--package", `"${config.pkg}"`];
|
|
23
|
+
if (config.output) {
|
|
24
|
+
args.push("--output", `"${path.resolve(config.output)}"`);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
// Default output path if not specified, relative to the working directory
|
|
28
|
+
args.push("--output", `"./output"`);
|
|
29
|
+
}
|
|
30
|
+
// Pass the working directory to the goscript command
|
|
31
|
+
if (config.dir) {
|
|
32
|
+
args.push("--dir", `"${path.resolve(config.dir)}"`);
|
|
33
|
+
}
|
|
34
|
+
const command = `${goscriptCmd} ${args.join(" ")}`;
|
|
35
|
+
// Execute go run from the specified working directory (or current)
|
|
36
|
+
const cwd = config.dir ? path.resolve(config.dir) : process.cwd();
|
|
37
|
+
try {
|
|
38
|
+
const { stdout, stderr } = await execAsync(command, { cwd });
|
|
39
|
+
if (stdout) {
|
|
40
|
+
console.log(`GoScript stdout:\n${stdout}`);
|
|
41
|
+
}
|
|
42
|
+
if (stderr) {
|
|
43
|
+
// Go compiler often prints status messages to stderr, treat as info unless exit code is non-zero
|
|
44
|
+
console.info(`GoScript stderr:\n${stderr}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
catch (error) { // eslint-disable-line
|
|
48
|
+
console.error(`GoScript compilation failed: ${error.message}`);
|
|
49
|
+
if (error.stderr) {
|
|
50
|
+
console.error(`GoScript stderr:\n${error.stderr}`);
|
|
51
|
+
}
|
|
52
|
+
if (error.stdout) {
|
|
53
|
+
console.error(`GoScript stdout:\n${error.stdout}`);
|
|
54
|
+
}
|
|
55
|
+
throw new Error(`GoScript compilation failed: ${error.message}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Default export for convenience.
|
|
60
|
+
*/
|
|
61
|
+
export default {
|
|
62
|
+
compile,
|
|
63
|
+
};
|
|
64
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../compiler/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAC1C,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,MAAM,UAAU,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAClD,MAAM,SAAS,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;AAClC,MAAM,SAAS,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;AACtC,MAAM,WAAW,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,gDAAgD;AAgBxF;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,OAAO,CAAC,MAAqB;IACjD,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;IAC3D,CAAC;IAED,iFAAiF;IACjF,MAAM,WAAW,GACf,MAAM,CAAC,YAAY;QACnB,WAAW,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,g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AsB;QAC3C,OAAO,CAAC,KAAK,CAAC,gCAAgC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;QAC/D,IAAI,KAAK,CAAC,MAAM,EAAE,CAAC;YACjB,OAAO,CAAC,KAAK,CAAC,qBAAqB,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC;QACrD,CAAC;QACD,IAAI,KAAK,CAAC,MAAM,EAAE,CAAC;YACjB,OAAO,CAAC,KAAK,CAAC,qBAAqB,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC;QACrD,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,gCAAgC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;IACnE,CAAC;AACH,CAAC;AAED;;GAEG;AACH,eAAe;IACb,OAAO;CACR,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,14 +1,82 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "goscript",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Go to TypeScript compiler",
|
|
5
|
+
"bin": {
|
|
6
|
+
"goscript": "./cmd/goscript/main.js"
|
|
7
|
+
},
|
|
8
|
+
"type": "module",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/compiler/index.d.ts",
|
|
13
|
+
"default": "./dist/compiler/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"./compiler": {
|
|
17
|
+
"import": {
|
|
18
|
+
"types": "./dist/compiler/index.d.ts",
|
|
19
|
+
"default": "./dist/compiler/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"./builtin": {
|
|
23
|
+
"import": {
|
|
24
|
+
"types": "./dist/builtin/builtin.d.ts",
|
|
25
|
+
"default": "./dist/builtin/builtin.js"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
},
|
|
5
29
|
"scripts": {
|
|
30
|
+
"build": "tsc",
|
|
31
|
+
"prepublishOnly": "npm run build",
|
|
6
32
|
"example": "cd ./example/simple && bash run.bash",
|
|
7
|
-
"test": "
|
|
33
|
+
"test": "npm run test:js && npm run test:go",
|
|
34
|
+
"test:go": "go test -v ./...",
|
|
35
|
+
"test:js": "vitest run",
|
|
36
|
+
"format": "npm run format:go && npm run format:js && npm run format:config",
|
|
37
|
+
"format:config": "prettier --write tsconfig.json package.json",
|
|
38
|
+
"format:go": "gofumpt -w .",
|
|
39
|
+
"format:js": "prettier --write './{src,builtin,example}/**/(*.ts|*.tsx|*.html|*.css|*.scss)'",
|
|
40
|
+
"release": "npm run release:version && npm run release:commit",
|
|
41
|
+
"release:minor": "npm run release:version:minor && npm run release:commit",
|
|
42
|
+
"release:version": "npm version patch -m \"release: v%s\" --no-git-tag-version",
|
|
43
|
+
"release:version:minor": "npm version minor -m \"release: v%s\" --no-git-tag-version",
|
|
44
|
+
"release:commit": "git reset && git add package.json && git commit -s -m \"release: v$npm_package_version\" && git tag v$npm_package_version",
|
|
45
|
+
"release:publish": "git push && git push --tags && npm run build && npm publish",
|
|
46
|
+
"lint": "npm run lint:go && npm run lint:js",
|
|
47
|
+
"lint:go": "make lint",
|
|
48
|
+
"lint:js": "eslint -c eslint.config.mjs ./",
|
|
49
|
+
"prepare": "husky",
|
|
50
|
+
"precommit": "lint-staged"
|
|
51
|
+
},
|
|
52
|
+
"files": [
|
|
53
|
+
"!**/*.tsbuildinfo",
|
|
54
|
+
"dist",
|
|
55
|
+
"cmd",
|
|
56
|
+
"compiler",
|
|
57
|
+
"builtin",
|
|
58
|
+
"go.mod",
|
|
59
|
+
"go.sum",
|
|
60
|
+
"LICENSE",
|
|
61
|
+
"README.md"
|
|
62
|
+
],
|
|
63
|
+
"lint-staged": {
|
|
64
|
+
"package.json": "prettier --config .prettierrc.yaml --write",
|
|
65
|
+
"./{src,builtin,example}/**/(*.ts|*.tsx|*.html|*.css|*.scss)": "prettier --config .prettierrc.yaml --write"
|
|
8
66
|
},
|
|
9
67
|
"devDependencies": {
|
|
68
|
+
"@eslint/js": "^9.25.1",
|
|
69
|
+
"@types/node": "^22.15.2",
|
|
70
|
+
"@typescript-eslint/eslint-plugin": "^8.31.0",
|
|
71
|
+
"@typescript-eslint/parser": "^8.31.0",
|
|
72
|
+
"eslint": "^9.25.1",
|
|
73
|
+
"eslint-config-prettier": "^10.0.2",
|
|
74
|
+
"husky": "^9.1.7",
|
|
75
|
+
"lint-staged": "^15.4.3",
|
|
10
76
|
"prettier": "^3.5.3",
|
|
11
77
|
"tsx": "^4.0.0",
|
|
12
|
-
"typescript": "^5.
|
|
78
|
+
"typescript": "^5.8.3",
|
|
79
|
+
"typescript-eslint": "^8.31.0",
|
|
80
|
+
"vitest": "^3.1.2"
|
|
13
81
|
}
|
|
14
82
|
}
|
package/.aider-prompt
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
The GoScript compiler needs to correctly handle the compilation of Go function literals (`ast.FuncLit`) into TypeScript.
|
|
2
|
-
|
|
3
|
-
There are two related failing compliance tests:
|
|
4
|
-
1. `compliance/tests/channel_basic/channel_basic.go`: This test uses a function literal within a `go func() { ... }()` statement. The compiler currently fails to compile the function literal in this context, resulting in a TypeScript syntax error. The desired output for the goroutine part is `queueMicrotask(() => { ... compiled function literal body ... })`.
|
|
5
|
-
2. `compliance/tests/func_literal/func_literal.go`: This is a new test specifically for basic function literal usage (assigning a function literal to a variable and calling it). This test is also expected to fail because the compiler does not yet have full support for compiling function literals.
|
|
6
|
-
|
|
7
|
-
The issue is considered architectural because it involves the fundamental translation of Go's function literal construct to TypeScript, which impacts how goroutines and potentially other features (like closures) will be handled.
|
|
8
|
-
|
|
9
|
-
Please analyze the provided files, including `design/DESIGN.md`, and suggest the necessary changes to the compiler, primarily in `compiler/compile_expr.go` (where expressions are compiled) and potentially `compiler/compile_stmt.go` (where statements like `go` are handled), to correctly compile `ast.FuncLit` in these contexts.
|
|
10
|
-
|
|
11
|
-
The compilation of a function literal should produce a TypeScript arrow function or equivalent, ensuring that the body of the function literal is also correctly compiled.
|