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,37 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { spawn } from 'node:child_process';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { dirname } from 'node:path';
|
|
7
|
+
|
|
8
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
9
|
+
const __dirname = dirname(__filename);
|
|
10
|
+
// Go up two levels from cmd/goscript/ to the project root
|
|
11
|
+
const projectRoot = path.join(__dirname, '..', '..');
|
|
12
|
+
|
|
13
|
+
// Get arguments passed to the script, excluding node executable and script path
|
|
14
|
+
const args = process.argv.slice(2);
|
|
15
|
+
|
|
16
|
+
// Construct the go run command with the absolute path to the goscript executable
|
|
17
|
+
// Use path.join for robustness
|
|
18
|
+
const goscriptCmd = `go run "${path.join(projectRoot, "cmd", "goscript")}"`;
|
|
19
|
+
|
|
20
|
+
// Combine the goscript command with the arguments
|
|
21
|
+
const command = `${goscriptCmd} ${args.join(" ")}`;
|
|
22
|
+
|
|
23
|
+
// Execute the command
|
|
24
|
+
const child = spawn(command, {
|
|
25
|
+
shell: true, // Use shell to correctly parse the command string
|
|
26
|
+
stdio: 'inherit', // Inherit stdin, stdout, and stderr
|
|
27
|
+
cwd: process.cwd(), // Execute in the current working directory where the script is run
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
child.on('error', (error) => {
|
|
31
|
+
console.error(`Failed to start subprocess: ${error.message}`);
|
|
32
|
+
process.exit(1);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
child.on('exit', (code) => {
|
|
36
|
+
process.exit(code ?? 0); // Exit with the child process's exit code
|
|
37
|
+
});
|
package/compiler/compile_expr.go
CHANGED
|
@@ -359,48 +359,14 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
359
359
|
// Handle make for slices: make([]T, len, cap) or make([]T, len)
|
|
360
360
|
if len(exp.Args) >= 1 {
|
|
361
361
|
// Handle map creation: make(map[K]V)
|
|
362
|
-
if
|
|
363
|
-
c.tsw.WriteLiterally("goscript.makeMap(")
|
|
364
|
-
|
|
365
|
-
// Get key type
|
|
366
|
-
if keyType := c.pkg.TypesInfo.TypeOf(mapType.Key); keyType != nil {
|
|
367
|
-
// Use the underlying type for basic types
|
|
368
|
-
underlyingKeyType := keyType.Underlying()
|
|
369
|
-
c.tsw.WriteLiterally(fmt.Sprintf("%q", underlyingKeyType.String()))
|
|
370
|
-
} else {
|
|
371
|
-
// If type info is not available, this is an error condition
|
|
372
|
-
return errors.New("could not determine key type for makeMap")
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
c.tsw.WriteLiterally(", ")
|
|
376
|
-
|
|
377
|
-
// Get value type
|
|
378
|
-
if valueType := c.pkg.TypesInfo.TypeOf(mapType.Value); valueType != nil {
|
|
379
|
-
// Use the underlying type for basic types
|
|
380
|
-
underlyingValueType := valueType.Underlying()
|
|
381
|
-
c.tsw.WriteLiterally(fmt.Sprintf("%q", underlyingValueType.String()))
|
|
382
|
-
} else {
|
|
383
|
-
// If type info is not available, this is an error condition
|
|
384
|
-
return errors.New("could not determine value type for makeMap")
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
c.tsw.WriteLiterally(")")
|
|
362
|
+
if _, ok := exp.Args[0].(*ast.MapType); ok {
|
|
363
|
+
c.tsw.WriteLiterally("goscript.makeMap()")
|
|
388
364
|
return nil // Handled make for map
|
|
389
365
|
}
|
|
390
366
|
|
|
391
367
|
// Handle slice creation
|
|
392
|
-
if
|
|
368
|
+
if _, ok := exp.Args[0].(*ast.ArrayType); ok {
|
|
393
369
|
c.tsw.WriteLiterally("goscript.makeSlice(")
|
|
394
|
-
// Get and write the string representation of the element type
|
|
395
|
-
if typ := c.pkg.TypesInfo.TypeOf(arrayType.Elt); typ != nil {
|
|
396
|
-
// Use the underlying type for basic types like int, string, etc.
|
|
397
|
-
underlyingType := typ.Underlying()
|
|
398
|
-
c.tsw.WriteLiterally(fmt.Sprintf("%q", underlyingType.String()))
|
|
399
|
-
} else {
|
|
400
|
-
// If type info is not available, this is an error condition for makeSlice
|
|
401
|
-
return errors.New("could not determine element type for makeSlice")
|
|
402
|
-
}
|
|
403
|
-
c.tsw.WriteLiterally(", ")
|
|
404
370
|
if len(exp.Args) >= 2 {
|
|
405
371
|
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
406
372
|
return err
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { describe, it, expect, beforeAll, afterAll } from "vitest";
|
|
2
|
+
import { compile } from "./index";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
import * as fs from "fs/promises";
|
|
5
|
+
|
|
6
|
+
const exampleDir = path.resolve(__dirname, "../example/simple");
|
|
7
|
+
const outputDir = path.join(exampleDir, "output"); // Use a separate output dir for tests
|
|
8
|
+
const expectedOutputFile = path.join(outputDir, "@go", "example", "main.gs.ts");
|
|
9
|
+
|
|
10
|
+
describe("GoScript Compiler API", () => {
|
|
11
|
+
// Clean up before and after tests
|
|
12
|
+
beforeAll(async () => {
|
|
13
|
+
await fs.rm(outputDir, { recursive: true, force: true });
|
|
14
|
+
});
|
|
15
|
+
afterAll(async () => {
|
|
16
|
+
await fs.rm(outputDir, { recursive: true, force: true });
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("should compile the simple example package", async () => {
|
|
20
|
+
const config = {
|
|
21
|
+
pkg: ".", // Compile the package in the exampleDir
|
|
22
|
+
dir: exampleDir,
|
|
23
|
+
output: outputDir,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
await expect(compile(config)).resolves.toBeUndefined();
|
|
27
|
+
await expect(fs.access(expectedOutputFile)).resolves.toBeUndefined();
|
|
28
|
+
}, 30000); // 30 second timeout for compilation
|
|
29
|
+
});
|
|
@@ -0,0 +1,85 @@
|
|
|
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
|
+
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const execAsync = promisify(exec);
|
|
9
|
+
const __dirname = dirname(__filename);
|
|
10
|
+
const projectRoot = dirname(__dirname); // Go up one level from src/ to the project root
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Configuration options for the GoScript compiler.
|
|
14
|
+
*/
|
|
15
|
+
export interface CompileConfig {
|
|
16
|
+
/** The Go package path or pattern to compile. */
|
|
17
|
+
pkg: string;
|
|
18
|
+
/** The output directory for the generated TypeScript files. Defaults to './output'. */
|
|
19
|
+
output?: string;
|
|
20
|
+
/** The working directory for the compiler. Defaults to the current working directory. */
|
|
21
|
+
dir?: string;
|
|
22
|
+
/** The path to the goscript executable. Defaults to 'go run github.com/paralin/goscript/cmd/goscript'. */
|
|
23
|
+
goscriptPath?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Compiles a Go package to TypeScript using the goscript compiler.
|
|
28
|
+
* @param config - The compilation configuration.
|
|
29
|
+
* @returns A promise that resolves when compilation is complete, or rejects on error.
|
|
30
|
+
*/
|
|
31
|
+
export async function compile(config: CompileConfig): Promise<void> {
|
|
32
|
+
if (!config.pkg) {
|
|
33
|
+
throw new Error("Package path (pkg) must be specified.");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Construct the go run command with the absolute path to the goscript executable
|
|
37
|
+
const goscriptCmd =
|
|
38
|
+
config.goscriptPath ??
|
|
39
|
+
`go run "${path.join(projectRoot, "./cmd/goscript")}"`;
|
|
40
|
+
|
|
41
|
+
const args: string[] = ["compile", "--package", `"${config.pkg}"`];
|
|
42
|
+
|
|
43
|
+
if (config.output) {
|
|
44
|
+
args.push("--output", `"${path.resolve(config.output)}"`);
|
|
45
|
+
} else {
|
|
46
|
+
// Default output path if not specified, relative to the working directory
|
|
47
|
+
args.push("--output", `"./output"`);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Pass the working directory to the goscript command
|
|
51
|
+
if (config.dir) {
|
|
52
|
+
args.push("--dir", `"${path.resolve(config.dir)}"`);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const command = `${goscriptCmd} ${args.join(" ")}`;
|
|
56
|
+
// Execute go run from the specified working directory (or current)
|
|
57
|
+
const cwd = config.dir ? path.resolve(config.dir) : process.cwd();
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
const { stdout, stderr } = await execAsync(command, { cwd });
|
|
61
|
+
if (stdout) {
|
|
62
|
+
console.log(`GoScript stdout:\n${stdout}`);
|
|
63
|
+
}
|
|
64
|
+
if (stderr) {
|
|
65
|
+
// Go compiler often prints status messages to stderr, treat as info unless exit code is non-zero
|
|
66
|
+
console.info(`GoScript stderr:\n${stderr}`);
|
|
67
|
+
}
|
|
68
|
+
} catch (error: any) { // eslint-disable-line
|
|
69
|
+
console.error(`GoScript compilation failed: ${error.message}`);
|
|
70
|
+
if (error.stderr) {
|
|
71
|
+
console.error(`GoScript stderr:\n${error.stderr}`);
|
|
72
|
+
}
|
|
73
|
+
if (error.stdout) {
|
|
74
|
+
console.error(`GoScript stdout:\n${error.stdout}`);
|
|
75
|
+
}
|
|
76
|
+
throw new Error(`GoScript compilation failed: ${error.message}`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Default export for convenience.
|
|
82
|
+
*/
|
|
83
|
+
export default {
|
|
84
|
+
compile,
|
|
85
|
+
};
|
|
@@ -0,0 +1,168 @@
|
|
|
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 declare const makeSlice: <T>(len: number, cap?: number) => Array<T> & {
|
|
8
|
+
__capacity?: number;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new map (TypeScript Map).
|
|
12
|
+
* @returns A new TypeScript Map.
|
|
13
|
+
*/
|
|
14
|
+
export declare const makeMap: <K, V>() => Map<K, V>;
|
|
15
|
+
/**
|
|
16
|
+
* Returns the length of a collection (string, array, or map).
|
|
17
|
+
* @param collection The collection to get the length of.
|
|
18
|
+
* @returns The length of the collection.
|
|
19
|
+
*/
|
|
20
|
+
export declare const len: <T>(collection: string | Array<T> | Map<unknown, unknown>) => number;
|
|
21
|
+
/**
|
|
22
|
+
* Returns the capacity of a slice (TypeScript array).
|
|
23
|
+
* @param slice The slice (TypeScript array).
|
|
24
|
+
* @returns The capacity of the slice.
|
|
25
|
+
*/
|
|
26
|
+
export declare const cap: <T>(slice: Array<T> & {
|
|
27
|
+
__capacity?: number;
|
|
28
|
+
}) => number;
|
|
29
|
+
/**
|
|
30
|
+
* Converts a string to an array of Unicode code points (runes).
|
|
31
|
+
* @param str The input string.
|
|
32
|
+
* @returns An array of numbers representing the Unicode code points.
|
|
33
|
+
*/
|
|
34
|
+
export declare const stringToRunes: (str: string) => number[];
|
|
35
|
+
/**
|
|
36
|
+
* Gets a value from a map, with a default value if the key doesn't exist.
|
|
37
|
+
* @param map The map to get from.
|
|
38
|
+
* @param key The key to get.
|
|
39
|
+
* @param defaultValue The default value to return if the key doesn't exist (defaults to 0).
|
|
40
|
+
* @returns The value for the key, or the default value if the key doesn't exist.
|
|
41
|
+
*/
|
|
42
|
+
export declare const mapGet: <K, V>(map: Map<K, V>, key: K, defaultValue?: V | null) => V | null;
|
|
43
|
+
/**
|
|
44
|
+
* Sets a value in a map.
|
|
45
|
+
* @param map The map to set in.
|
|
46
|
+
* @param key The key to set.
|
|
47
|
+
* @param value The value to set.
|
|
48
|
+
*/
|
|
49
|
+
export declare const mapSet: <K, V>(map: Map<K, V>, key: K, value: V) => void;
|
|
50
|
+
/**
|
|
51
|
+
* Deletes a key from a map.
|
|
52
|
+
* @param map The map to delete from.
|
|
53
|
+
* @param key The key to delete.
|
|
54
|
+
*/
|
|
55
|
+
export declare const deleteMapEntry: <K, V>(map: Map<K, V>, key: K) => void;
|
|
56
|
+
/**
|
|
57
|
+
* Checks if a key exists in a map.
|
|
58
|
+
* @param map The map to check in.
|
|
59
|
+
* @param key The key to check.
|
|
60
|
+
* @returns True if the key exists, false otherwise.
|
|
61
|
+
*/
|
|
62
|
+
export declare const mapHas: <K, V>(map: Map<K, V>, key: K) => boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Appends elements to a slice (TypeScript array).
|
|
65
|
+
* Note: In Go, append can return a new slice if the underlying array is reallocated.
|
|
66
|
+
* This helper emulates that by returning the modified array.
|
|
67
|
+
* @param slice The slice (TypeScript array) to append to.
|
|
68
|
+
* @param elements The elements to append.
|
|
69
|
+
* @returns The modified slice (TypeScript array).
|
|
70
|
+
*/
|
|
71
|
+
export declare const append: <T>(slice: Array<T>, ...elements: T[]) => Array<T>;
|
|
72
|
+
/**
|
|
73
|
+
* Represents the result of a channel receive operation with 'ok' value
|
|
74
|
+
*/
|
|
75
|
+
export interface ChannelReceiveResult<T> {
|
|
76
|
+
value: T;
|
|
77
|
+
ok: boolean;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Represents a result from a select operation
|
|
81
|
+
*/
|
|
82
|
+
export interface SelectResult<T> {
|
|
83
|
+
value: T;
|
|
84
|
+
ok: boolean;
|
|
85
|
+
id: number;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Represents a Go channel in TypeScript.
|
|
89
|
+
* Supports asynchronous sending and receiving of values.
|
|
90
|
+
*/
|
|
91
|
+
export interface Channel<T> {
|
|
92
|
+
/**
|
|
93
|
+
* Sends a value to the channel.
|
|
94
|
+
* Returns a promise that resolves when the value is accepted by the channel.
|
|
95
|
+
* @param value The value to send.
|
|
96
|
+
*/
|
|
97
|
+
send(value: T): Promise<void>;
|
|
98
|
+
/**
|
|
99
|
+
* Receives a value from the channel.
|
|
100
|
+
* Returns a promise that resolves with the received value.
|
|
101
|
+
* If the channel is closed, it throws an error.
|
|
102
|
+
*/
|
|
103
|
+
receive(): Promise<T>;
|
|
104
|
+
/**
|
|
105
|
+
* Receives a value from the channel along with a boolean indicating
|
|
106
|
+
* whether the channel is still open.
|
|
107
|
+
* Returns a promise that resolves with {value, ok}.
|
|
108
|
+
* - If channel is open and has data: {value: <data>, ok: true}
|
|
109
|
+
* - If channel is closed and empty: {value: <zero value>, ok: false}
|
|
110
|
+
* - If channel is closed but has remaining buffered data: {value: <data>, ok: true}
|
|
111
|
+
*/
|
|
112
|
+
receiveWithOk(): Promise<ChannelReceiveResult<T>>;
|
|
113
|
+
/**
|
|
114
|
+
* Closes the channel.
|
|
115
|
+
* No more values can be sent to a closed channel.
|
|
116
|
+
* Receive operations on a closed channel return the zero value and ok=false.
|
|
117
|
+
*/
|
|
118
|
+
close(): void;
|
|
119
|
+
/**
|
|
120
|
+
* Used in select statements to create a receive operation promise.
|
|
121
|
+
* @param id An identifier for this case in the select statement
|
|
122
|
+
* @returns Promise that resolves when this case is selected
|
|
123
|
+
*/
|
|
124
|
+
selectReceive(id: number): Promise<SelectResult<T>>;
|
|
125
|
+
/**
|
|
126
|
+
* Used in select statements to create a send operation promise.
|
|
127
|
+
* @param value The value to send
|
|
128
|
+
* @param id An identifier for this case in the select statement
|
|
129
|
+
* @returns Promise that resolves when this case is selected
|
|
130
|
+
*/
|
|
131
|
+
selectSend(value: T, id: number): Promise<SelectResult<boolean>>;
|
|
132
|
+
/**
|
|
133
|
+
* Checks if the channel has data ready to be received without blocking.
|
|
134
|
+
* Used for non-blocking select operations.
|
|
135
|
+
*/
|
|
136
|
+
canReceiveNonBlocking(): boolean;
|
|
137
|
+
/**
|
|
138
|
+
* Checks if the channel can accept a send operation without blocking.
|
|
139
|
+
* Used for non-blocking select operations.
|
|
140
|
+
*/
|
|
141
|
+
canSendNonBlocking(): boolean;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Represents a case in a select statement.
|
|
145
|
+
*/
|
|
146
|
+
export interface SelectCase<T> {
|
|
147
|
+
id: number;
|
|
148
|
+
isSend: boolean;
|
|
149
|
+
channel: Channel<any>;
|
|
150
|
+
value?: any;
|
|
151
|
+
onSelected?: (result: SelectResult<T>) => Promise<void>;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Helper for 'select' statements. Takes an array of select cases
|
|
155
|
+
* and resolves when one of them completes, following Go's select rules.
|
|
156
|
+
*
|
|
157
|
+
* @param cases Array of SelectCase objects
|
|
158
|
+
* @param hasDefault Whether there is a default case
|
|
159
|
+
* @returns A promise that resolves with the result of the selected case
|
|
160
|
+
*/
|
|
161
|
+
export declare function selectStatement<T>(cases: SelectCase<T>[], hasDefault?: boolean): Promise<void>;
|
|
162
|
+
/**
|
|
163
|
+
* Creates a new channel with the specified buffer size and zero value.
|
|
164
|
+
* @param bufferSize The size of the channel buffer. If 0, creates an unbuffered channel.
|
|
165
|
+
* @param zeroValue The zero value for the channel's element type.
|
|
166
|
+
* @returns A new channel instance.
|
|
167
|
+
*/
|
|
168
|
+
export declare const makeChannel: <T>(bufferSize: number, zeroValue: T) => Channel<T>;
|