goscript 0.0.2
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/.aider-prompt +11 -0
- package/LICENSE +21 -0
- package/README.md +427 -0
- package/builtin/builtin.ts +507 -0
- package/cmd/goscript/cmd_compile.go +59 -0
- package/cmd/goscript/main.go +23 -0
- package/compiler/compile.go +183 -0
- package/compiler/compile_comment.go +41 -0
- package/compiler/compile_decls.go +72 -0
- package/compiler/compile_expr.go +831 -0
- package/compiler/compile_field.go +89 -0
- package/compiler/compile_spec.go +256 -0
- package/compiler/compile_stmt.go +1509 -0
- package/compiler/compiler.go +81 -0
- package/compiler/config.go +32 -0
- package/compiler/context.go +9 -0
- package/compiler/file_compiler.go +80 -0
- package/compiler/output_path.go +31 -0
- package/compiler/pkg_compiler.go +73 -0
- package/compiler/writer.go +90 -0
- package/compliance/COMPLIANCE.md +133 -0
- package/compliance/compliance.go +313 -0
- package/compliance/compliance_test.go +57 -0
- package/compliance/tests/array_literal/array_literal.go +15 -0
- package/compliance/tests/array_literal/array_literal.gs.ts +19 -0
- package/compliance/tests/array_literal/expected.log +3 -0
- package/compliance/tests/async_basic/async_basic.go +26 -0
- package/compliance/tests/async_basic/async_basic.gs.ts +30 -0
- package/compliance/tests/async_basic/expected.log +1 -0
- package/compliance/tests/basic_arithmetic/basic_arithmetic.go +15 -0
- package/compliance/tests/basic_arithmetic/basic_arithmetic.gs.ts +19 -0
- package/compliance/tests/basic_arithmetic/expected.log +5 -0
- package/compliance/tests/boolean_logic/boolean_logic.go +13 -0
- package/compliance/tests/boolean_logic/boolean_logic.gs.ts +17 -0
- package/compliance/tests/boolean_logic/expected.log +3 -0
- package/compliance/tests/channel_basic/channel_basic.go +12 -0
- package/compliance/tests/channel_basic/channel_basic.gs.ts +18 -0
- package/compliance/tests/channel_basic/expected.log +1 -0
- package/compliance/tests/composite_literal_assignment/composite_literal_assignment.go +20 -0
- package/compliance/tests/composite_literal_assignment/composite_literal_assignment.gs.ts +27 -0
- package/compliance/tests/composite_literal_assignment/expected.log +2 -0
- package/compliance/tests/constants/constants.go +18 -0
- package/compliance/tests/constants/constants.gs.ts +22 -0
- package/compliance/tests/constants/expected.log +3 -0
- package/compliance/tests/copy_independence/copy_independence.go +29 -0
- package/compliance/tests/copy_independence/copy_independence.gs.ts +36 -0
- package/compliance/tests/copy_independence/expected.log +4 -0
- package/compliance/tests/float64/expected.log +6 -0
- package/compliance/tests/float64/float64.go +28 -0
- package/compliance/tests/float64/float64.gs.ts +32 -0
- package/compliance/tests/for_loop_basic/expected.log +5 -0
- package/compliance/tests/for_loop_basic/for_loop_basic.go +9 -0
- package/compliance/tests/for_loop_basic/for_loop_basic.gs.ts +13 -0
- package/compliance/tests/for_loop_condition_only/expected.log +5 -0
- package/compliance/tests/for_loop_condition_only/main.go +9 -0
- package/compliance/tests/for_loop_condition_only/main.gs.ts +13 -0
- package/compliance/tests/for_range/expected.log +9 -0
- package/compliance/tests/for_range/for_range.go +26 -0
- package/compliance/tests/for_range/for_range.gs.ts +45 -0
- package/compliance/tests/for_range_index_use/expected.log +6 -0
- package/compliance/tests/for_range_index_use/for_range_index_use.go +11 -0
- package/compliance/tests/for_range_index_use/for_range_index_use.gs.ts +18 -0
- package/compliance/tests/func_literal/expected.log +1 -0
- package/compliance/tests/func_literal/func_literal.go +10 -0
- package/compliance/tests/func_literal/func_literal.gs.ts +15 -0
- package/compliance/tests/function_call_result_assignment/expected.log +2 -0
- package/compliance/tests/function_call_result_assignment/function_call_result_assignment.go +24 -0
- package/compliance/tests/function_call_result_assignment/function_call_result_assignment.gs.ts +31 -0
- package/compliance/tests/if_statement/expected.log +1 -0
- package/compliance/tests/if_statement/if_statement.go +11 -0
- package/compliance/tests/if_statement/if_statement.gs.ts +15 -0
- package/compliance/tests/interface_to_interface_type_assertion/expected.log +1 -0
- package/compliance/tests/interface_to_interface_type_assertion/interface_to_interface_type_assertion.go +30 -0
- package/compliance/tests/interface_to_interface_type_assertion/interface_to_interface_type_assertion.gs.ts +41 -0
- package/compliance/tests/interface_type_assertion/expected.log +1 -0
- package/compliance/tests/interface_type_assertion/interface_type_assertion.go +26 -0
- package/compliance/tests/interface_type_assertion/interface_type_assertion.gs.ts +36 -0
- package/compliance/tests/map_support/expected.log +13 -0
- package/compliance/tests/map_support/map_support.go +89 -0
- package/compliance/tests/map_support/map_support.gs.ts +102 -0
- package/compliance/tests/method_call_on_pointer_receiver/expected.log +1 -0
- package/compliance/tests/method_call_on_pointer_receiver/method_call_on_pointer_receiver.go +19 -0
- package/compliance/tests/method_call_on_pointer_receiver/method_call_on_pointer_receiver.gs.ts +27 -0
- package/compliance/tests/method_call_on_pointer_via_value/expected.log +1 -0
- package/compliance/tests/method_call_on_pointer_via_value/method_call_on_pointer_via_value.go +29 -0
- package/compliance/tests/method_call_on_pointer_via_value/method_call_on_pointer_via_value.gs.ts +38 -0
- package/compliance/tests/method_call_on_value_receiver/expected.log +1 -0
- package/compliance/tests/method_call_on_value_receiver/method_call_on_value_receiver.go +16 -0
- package/compliance/tests/method_call_on_value_receiver/method_call_on_value_receiver.gs.ts +24 -0
- package/compliance/tests/method_call_on_value_via_pointer/expected.log +2 -0
- package/compliance/tests/method_call_on_value_via_pointer/method_call_on_value_via_pointer.go +30 -0
- package/compliance/tests/method_call_on_value_via_pointer/method_call_on_value_via_pointer.gs.ts +38 -0
- package/compliance/tests/multiple_return_values/expected.log +6 -0
- package/compliance/tests/multiple_return_values/multiple_return_values.go +19 -0
- package/compliance/tests/multiple_return_values/multiple_return_values.gs.ts +23 -0
- package/compliance/tests/pointer_assignment_no_copy/expected.log +2 -0
- package/compliance/tests/pointer_assignment_no_copy/pointer_assignment_no_copy.go +28 -0
- package/compliance/tests/pointer_assignment_no_copy/pointer_assignment_no_copy.gs.ts +35 -0
- package/compliance/tests/pointer_composite_literal_assignment/expected.log +3 -0
- package/compliance/tests/pointer_composite_literal_assignment/pointer_composite_literal_assignment.go +23 -0
- package/compliance/tests/pointer_composite_literal_assignment/pointer_composite_literal_assignment.gs.ts +30 -0
- package/compliance/tests/pointer_deref_multiassign/expected.log +0 -0
- package/compliance/tests/pointer_deref_multiassign/pointer_deref_multiassign.go +17 -0
- package/compliance/tests/pointer_deref_multiassign/pointer_deref_multiassign.gs.ts +27 -0
- package/compliance/tests/pointer_initialization/expected.log +1 -0
- package/compliance/tests/pointer_initialization/pointer_initialization.go +16 -0
- package/compliance/tests/pointer_initialization/pointer_initialization.gs.ts +22 -0
- package/compliance/tests/select_receive_on_closed_channel_no_default/expected.log +1 -0
- package/compliance/tests/select_receive_on_closed_channel_no_default/select_receive_on_closed_channel_no_default.go +15 -0
- package/compliance/tests/select_receive_on_closed_channel_no_default/select_receive_on_closed_channel_no_default.gs.ts +31 -0
- package/compliance/tests/select_send_on_full_buffered_channel_with_default/expected.log +1 -0
- package/compliance/tests/select_send_on_full_buffered_channel_with_default/select_send_on_full_buffered_channel_with_default.go +13 -0
- package/compliance/tests/select_send_on_full_buffered_channel_with_default/select_send_on_full_buffered_channel_with_default.gs.ts +35 -0
- package/compliance/tests/select_statement/expected.log +9 -0
- package/compliance/tests/select_statement/select_statement.go +109 -0
- package/compliance/tests/select_statement/select_statement.gs.ts +239 -0
- package/compliance/tests/simple/expected.log +1 -0
- package/compliance/tests/simple/simple.go +5 -0
- package/compliance/tests/simple/simple.gs.ts +9 -0
- package/compliance/tests/simple_deref_assignment/expected.log +2 -0
- package/compliance/tests/simple_deref_assignment/simple_deref_assignment.go +19 -0
- package/compliance/tests/simple_deref_assignment/simple_deref_assignment.gs.ts +26 -0
- package/compliance/tests/slices/expected.log +7 -0
- package/compliance/tests/slices/slices.go +22 -0
- package/compliance/tests/slices/slices.gs.ts +26 -0
- package/compliance/tests/string_rune_conversion/expected.log +3 -0
- package/compliance/tests/string_rune_conversion/string_rune_conversion.go +16 -0
- package/compliance/tests/string_rune_conversion/string_rune_conversion.gs.ts +22 -0
- package/compliance/tests/struct_field_access/expected.log +2 -0
- package/compliance/tests/struct_field_access/struct_field_access.go +13 -0
- package/compliance/tests/struct_field_access/struct_field_access.gs.ts +20 -0
- package/compliance/tests/struct_value_init_clone/expected.log +5 -0
- package/compliance/tests/struct_value_init_clone/struct_value_init_clone.go +28 -0
- package/compliance/tests/struct_value_init_clone/struct_value_init_clone.gs.ts +35 -0
- package/compliance/tests/switch_statement/expected.log +14 -0
- package/compliance/tests/switch_statement/switch_statement.go +59 -0
- package/compliance/tests/switch_statement/switch_statement.gs.ts +85 -0
- package/compliance/tests/value_type_copy_behavior/expected.log +3 -0
- package/compliance/tests/value_type_copy_behavior/value_type_copy_behavior.go +25 -0
- package/compliance/tests/value_type_copy_behavior/value_type_copy_behavior.gs.ts +34 -0
- package/design/DESIGN.md +599 -0
- package/example/simple/build.bash +10 -0
- package/example/simple/go.mod +23 -0
- package/example/simple/go.sum +39 -0
- package/example/simple/main.go +138 -0
- package/example/simple/main.gs.ts +133 -0
- package/example/simple/main.ts +3 -0
- package/example/simple/main_test.go +59 -0
- package/example/simple/main_tools.go +5 -0
- package/example/simple/package.json +7 -0
- package/example/simple/run.bash +6 -0
- package/example/simple/tsconfig.json +28 -0
- package/example/simple/yarn.lock +8 -0
- package/go.mod +22 -0
- package/go.sum +39 -0
- package/output/output.go +10 -0
- package/package.json +14 -0
- package/tsconfig.json +10 -0
- package/types/tokens.go +65 -0
- package/types/types.go +46 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
package main
|
|
2
|
+
|
|
3
|
+
type MyStruct struct {
|
|
4
|
+
MyInt int
|
|
5
|
+
MyString string
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
func main() {
|
|
9
|
+
// === Struct Field Access ===
|
|
10
|
+
ms := MyStruct{MyInt: 42, MyString: "foo"}
|
|
11
|
+
println("MyInt: Expected: 42, Actual:", ms.MyInt)
|
|
12
|
+
println("MyString: Expected: foo, Actual:", ms.MyString)
|
|
13
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Generated file based on struct_field_access.go
|
|
2
|
+
// Updated when compliance tests are re-run, DO NOT EDIT!
|
|
3
|
+
|
|
4
|
+
import * as goscript from "@go/builtin";
|
|
5
|
+
|
|
6
|
+
class MyStruct {
|
|
7
|
+
public MyInt: number = 0;
|
|
8
|
+
public MyString: string = "";
|
|
9
|
+
|
|
10
|
+
constructor(init?: Partial<MyStruct>) { if (init) Object.assign(this, init as any); }
|
|
11
|
+
public clone(): MyStruct { return Object.assign(Object.create(MyStruct.prototype) as MyStruct, this); }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function main(): Promise<void> {
|
|
15
|
+
// === Struct Field Access ===
|
|
16
|
+
let ms = new MyStruct({ MyInt: 42, MyString: "foo" })
|
|
17
|
+
console.log("MyInt: Expected: 42, Actual:", ms.MyInt)
|
|
18
|
+
console.log("MyString: Expected: foo, Actual:", ms.MyString)
|
|
19
|
+
}
|
|
20
|
+
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
package main
|
|
2
|
+
|
|
3
|
+
type Point struct {
|
|
4
|
+
X int
|
|
5
|
+
Y int
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
func main() {
|
|
9
|
+
// Initialize directly
|
|
10
|
+
p1 := Point{X: 1, Y: 2}
|
|
11
|
+
println("p1:", p1.X, p1.Y)
|
|
12
|
+
|
|
13
|
+
// Assign to another variable (should trigger clone)
|
|
14
|
+
p2 := p1
|
|
15
|
+
p2.X = 10 // Modify the copy
|
|
16
|
+
|
|
17
|
+
// Print both to show they are independent
|
|
18
|
+
println("p1 after p2 mod:", p1.X, p1.Y)
|
|
19
|
+
println("p2:", p2.X, p2.Y)
|
|
20
|
+
|
|
21
|
+
// Initialize via variable assignment
|
|
22
|
+
v := Point{X: 3, Y: 4}
|
|
23
|
+
p3 := v // Should trigger clone
|
|
24
|
+
p3.Y = 40 // Modify the copy
|
|
25
|
+
|
|
26
|
+
println("v after p3 mod:", v.X, v.Y)
|
|
27
|
+
println("p3:", p3.X, p3.Y)
|
|
28
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Generated file based on struct_value_init_clone.go
|
|
2
|
+
// Updated when compliance tests are re-run, DO NOT EDIT!
|
|
3
|
+
|
|
4
|
+
import * as goscript from "@go/builtin";
|
|
5
|
+
|
|
6
|
+
class Point {
|
|
7
|
+
public X: number = 0;
|
|
8
|
+
public Y: number = 0;
|
|
9
|
+
|
|
10
|
+
constructor(init?: Partial<Point>) { if (init) Object.assign(this, init as any); }
|
|
11
|
+
public clone(): Point { return Object.assign(Object.create(Point.prototype) as Point, this); }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function main(): Promise<void> {
|
|
15
|
+
// Initialize directly
|
|
16
|
+
let p1 = new Point({ X: 1, Y: 2 })
|
|
17
|
+
console.log("p1:", p1.X, p1.Y)
|
|
18
|
+
|
|
19
|
+
// Assign to another variable (should trigger clone)
|
|
20
|
+
let p2 = p1.clone()
|
|
21
|
+
p2.X = 10 // Modify the copy
|
|
22
|
+
|
|
23
|
+
// Print both to show they are independent
|
|
24
|
+
console.log("p1 after p2 mod:", p1.X, p1.Y)
|
|
25
|
+
console.log("p2:", p2.X, p2.Y)
|
|
26
|
+
|
|
27
|
+
// Initialize via variable assignment
|
|
28
|
+
let v = new Point({ X: 3, Y: 4 })
|
|
29
|
+
let p3 = v.clone() // Should trigger clone
|
|
30
|
+
p3.Y = 40 // Modify the copy
|
|
31
|
+
|
|
32
|
+
console.log("v after p3 mod:", v.X, v.Y)
|
|
33
|
+
console.log("p3:", p3.X, p3.Y)
|
|
34
|
+
}
|
|
35
|
+
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
package main
|
|
2
|
+
|
|
3
|
+
func main() {
|
|
4
|
+
i := 2
|
|
5
|
+
println("Integer switch:")
|
|
6
|
+
switch i {
|
|
7
|
+
case 1:
|
|
8
|
+
println("one")
|
|
9
|
+
case 2:
|
|
10
|
+
println("two")
|
|
11
|
+
case 3:
|
|
12
|
+
println("three")
|
|
13
|
+
default:
|
|
14
|
+
println("other integer")
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
s := "hello"
|
|
18
|
+
println("\nString switch:")
|
|
19
|
+
switch s {
|
|
20
|
+
case "world":
|
|
21
|
+
println("world")
|
|
22
|
+
case "hello":
|
|
23
|
+
println("hello")
|
|
24
|
+
default:
|
|
25
|
+
println("other string")
|
|
26
|
+
}
|
|
27
|
+
x := -5
|
|
28
|
+
println("\nSwitch without expression:")
|
|
29
|
+
switch {
|
|
30
|
+
case x < 0:
|
|
31
|
+
println("negative")
|
|
32
|
+
case x == 0:
|
|
33
|
+
println("zero")
|
|
34
|
+
default: // x > 0
|
|
35
|
+
println("positive")
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
x = 0
|
|
39
|
+
println("\nSwitch without expression (zero):")
|
|
40
|
+
switch {
|
|
41
|
+
case x < 0:
|
|
42
|
+
println("negative")
|
|
43
|
+
case x == 0:
|
|
44
|
+
println("zero")
|
|
45
|
+
default: // x > 0
|
|
46
|
+
println("positive")
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
x = 10
|
|
50
|
+
println("\nSwitch without expression (positive):")
|
|
51
|
+
switch {
|
|
52
|
+
case x < 0:
|
|
53
|
+
println("negative")
|
|
54
|
+
case x == 0:
|
|
55
|
+
println("zero")
|
|
56
|
+
default: // x > 0
|
|
57
|
+
println("positive")
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Generated file based on switch_statement.go
|
|
2
|
+
// Updated when compliance tests are re-run, DO NOT EDIT!
|
|
3
|
+
|
|
4
|
+
import * as goscript from "@go/builtin";
|
|
5
|
+
|
|
6
|
+
export async function main(): Promise<void> {
|
|
7
|
+
let i = 2
|
|
8
|
+
console.log("Integer switch:")
|
|
9
|
+
switch (i) {
|
|
10
|
+
case 1:
|
|
11
|
+
console.log("one")
|
|
12
|
+
break
|
|
13
|
+
case 2:
|
|
14
|
+
console.log("two")
|
|
15
|
+
break
|
|
16
|
+
case 3:
|
|
17
|
+
console.log("three")
|
|
18
|
+
break
|
|
19
|
+
default:
|
|
20
|
+
console.log("other integer")
|
|
21
|
+
break
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
let s = "hello"
|
|
25
|
+
console.log("\nString switch:")
|
|
26
|
+
switch (s) {
|
|
27
|
+
case "world":
|
|
28
|
+
console.log("world")
|
|
29
|
+
break
|
|
30
|
+
case "hello":
|
|
31
|
+
console.log("hello")
|
|
32
|
+
break
|
|
33
|
+
default:
|
|
34
|
+
console.log("other string")
|
|
35
|
+
break
|
|
36
|
+
}
|
|
37
|
+
let x = -5
|
|
38
|
+
console.log("\nSwitch without expression:")
|
|
39
|
+
|
|
40
|
+
// x > 0
|
|
41
|
+
switch (true) {
|
|
42
|
+
case x < 0:
|
|
43
|
+
console.log("negative")
|
|
44
|
+
break
|
|
45
|
+
case x == 0:
|
|
46
|
+
console.log("zero")
|
|
47
|
+
break
|
|
48
|
+
default:
|
|
49
|
+
console.log("positive")
|
|
50
|
+
break
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
x = 0
|
|
54
|
+
console.log("\nSwitch without expression (zero):")
|
|
55
|
+
|
|
56
|
+
// x > 0
|
|
57
|
+
switch (true) {
|
|
58
|
+
case x < 0:
|
|
59
|
+
console.log("negative")
|
|
60
|
+
break
|
|
61
|
+
case x == 0:
|
|
62
|
+
console.log("zero")
|
|
63
|
+
break
|
|
64
|
+
default:
|
|
65
|
+
console.log("positive")
|
|
66
|
+
break
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
x = 10
|
|
70
|
+
console.log("\nSwitch without expression (positive):")
|
|
71
|
+
|
|
72
|
+
// x > 0
|
|
73
|
+
switch (true) {
|
|
74
|
+
case x < 0:
|
|
75
|
+
console.log("negative")
|
|
76
|
+
break
|
|
77
|
+
case x == 0:
|
|
78
|
+
console.log("zero")
|
|
79
|
+
break
|
|
80
|
+
default:
|
|
81
|
+
console.log("positive")
|
|
82
|
+
break
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
package main
|
|
2
|
+
|
|
3
|
+
type MyStruct struct {
|
|
4
|
+
MyInt int
|
|
5
|
+
MyString string
|
|
6
|
+
myBool bool
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
func main() {
|
|
10
|
+
dereferencedStructCopy := MyStruct{MyString: "original"}
|
|
11
|
+
// === Value-Type Copy Behavior ===
|
|
12
|
+
// Assigning a struct (value type) creates independent copies.
|
|
13
|
+
valueCopy1, valueCopy2, pointerCopy := dereferencedStructCopy, dereferencedStructCopy, &dereferencedStructCopy
|
|
14
|
+
// Modifications to one copy do not affect others or the original.
|
|
15
|
+
valueCopy1.MyString = "value copy 1"
|
|
16
|
+
dereferencedStructCopy.MyString = "original dereferenced copy modified" // Modify the source of the copies
|
|
17
|
+
valueCopy2.MyString = "value copy 2"
|
|
18
|
+
// Expected: "value copy 1"
|
|
19
|
+
println("valueCopy1: Expected: value copy 1, Actual: " + valueCopy1.MyString)
|
|
20
|
+
// Expected: "original dereferenced copy modified"
|
|
21
|
+
println("dereferencedStructCopy (modified after copies were made): Expected: original dereferenced copy modified, Actual: " + dereferencedStructCopy.MyString)
|
|
22
|
+
// Expected: "value copy 2"
|
|
23
|
+
println("valueCopy2: Expected: value copy 2, Actual: " + valueCopy2.MyString)
|
|
24
|
+
_ = pointerCopy
|
|
25
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Generated file based on value_type_copy_behavior.go
|
|
2
|
+
// Updated when compliance tests are re-run, DO NOT EDIT!
|
|
3
|
+
|
|
4
|
+
import * as goscript from "@go/builtin";
|
|
5
|
+
|
|
6
|
+
class MyStruct {
|
|
7
|
+
public MyInt: number = 0;
|
|
8
|
+
public MyString: string = "";
|
|
9
|
+
private myBool: boolean = false;
|
|
10
|
+
|
|
11
|
+
constructor(init?: Partial<MyStruct>) { if (init) Object.assign(this, init as any); }
|
|
12
|
+
public clone(): MyStruct { return Object.assign(Object.create(MyStruct.prototype) as MyStruct, this); }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export async function main(): Promise<void> {
|
|
16
|
+
let dereferencedStructCopy = new MyStruct({ MyString: "original" })
|
|
17
|
+
// === Value-Type Copy Behavior ===
|
|
18
|
+
// Assigning a struct (value type) creates independent copies.
|
|
19
|
+
let valueCopy1 = dereferencedStructCopy.clone()
|
|
20
|
+
let valueCopy2 = dereferencedStructCopy.clone()
|
|
21
|
+
let pointerCopy = dereferencedStructCopy
|
|
22
|
+
// Modifications to one copy do not affect others or the original.
|
|
23
|
+
valueCopy1.MyString = "value copy 1"
|
|
24
|
+
dereferencedStructCopy.MyString = "original dereferenced copy modified" // Modify the source of the copies
|
|
25
|
+
valueCopy2.MyString = "value copy 2"
|
|
26
|
+
// Expected: "value copy 1"
|
|
27
|
+
console.log("valueCopy1: Expected: value copy 1, Actual: " + valueCopy1.MyString)
|
|
28
|
+
// Expected: "original dereferenced copy modified"
|
|
29
|
+
console.log("dereferencedStructCopy (modified after copies were made): Expected: original dereferenced copy modified, Actual: " + dereferencedStructCopy.MyString)
|
|
30
|
+
// Expected: "value copy 2"
|
|
31
|
+
console.log("valueCopy2: Expected: value copy 2, Actual: " + valueCopy2.MyString)
|
|
32
|
+
pointerCopy/* discarded value */
|
|
33
|
+
}
|
|
34
|
+
|