-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathtest_class_field_layout.ts
More file actions
161 lines (139 loc) · 3.78 KB
/
test_class_field_layout.ts
File metadata and controls
161 lines (139 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Test class field layout: access ordering, inheritance, private fields at fixed offsets
// Critical for static typing migration: fields must be at correct struct offsets
// === Basic class with multiple fields ===
class Point {
x: number;
y: number;
z: number;
constructor(x: number, y: number, z: number) {
this.x = x;
this.y = y;
this.z = z;
}
magnitude(): number {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
}
}
const p = new Point(3, 4, 0);
console.log(p.x); // 3
console.log(p.y); // 4
console.log(p.z); // 0
// Modify fields
p.x = 10;
p.y = 20;
console.log(p.x + p.y); // 30
// === Inheritance field ordering ===
class Animal {
name: string;
legs: number;
constructor(name: string, legs: number) {
this.name = name;
this.legs = legs;
}
describe(): string {
return this.name + " has " + this.legs + " legs";
}
}
class Dog extends Animal {
breed: string;
constructor(name: string, breed: string) {
super(name, 4);
this.breed = breed;
}
fullDesc(): string {
return this.describe() + ", breed: " + this.breed;
}
}
const d = new Dog("Rex", "Labrador");
console.log(d.name); // Rex
console.log(d.legs); // 4
console.log(d.breed); // Labrador
console.log(d.describe()); // Rex has 4 legs
console.log(d.fullDesc()); // Rex has 4 legs, breed: Labrador
// === Private fields don't corrupt public field offsets ===
class Account {
#balance: number;
owner: string;
#pin: number;
constructor(owner: string, balance: number, pin: number) {
this.owner = owner;
this.#balance = balance;
this.#pin = pin;
}
getBalance(): number { return this.#balance; }
deposit(amount: number): void { this.#balance += amount; }
checkPin(pin: number): boolean { return this.#pin === pin; }
getOwner(): string { return this.owner; }
}
const acct = new Account("Alice", 100, 1234);
console.log(acct.owner); // Alice
console.log(acct.getBalance()); // 100
acct.deposit(50);
console.log(acct.getBalance()); // 150
console.log(acct.checkPin(1234)); // true
console.log(acct.checkPin(0000)); // false
// === Multiple inheritance levels ===
class Base {
a: number;
constructor(a: number) { this.a = a; }
}
class Middle extends Base {
b: string;
constructor(a: number, b: string) {
super(a);
this.b = b;
}
}
class Leaf extends Middle {
c: boolean;
constructor(a: number, b: string, c: boolean) {
super(a, b);
this.c = c;
}
show(): string {
return this.a + " " + this.b + " " + this.c;
}
}
const leaf = new Leaf(42, "hello", true);
console.log(leaf.a); // 42
console.log(leaf.b); // hello
console.log(leaf.c); // true
console.log(leaf.show()); // 42 hello true
// === Mixed field types in one class ===
class Record {
id: number;
name: string;
active: boolean;
tags: string[];
constructor(id: number, name: string, active: boolean) {
this.id = id;
this.name = name;
this.active = active;
this.tags = [];
}
addTag(tag: string): void { this.tags.push(tag); }
summary(): string {
return this.id + ": " + this.name + " (" + (this.active ? "active" : "inactive") + ") [" + this.tags.join(", ") + "]";
}
}
const r = new Record(1, "Test", true);
r.addTag("important");
r.addTag("v2");
console.log(r.summary()); // 1: Test (active) [important, v2]
// === Class instance in array ===
const points: Point[] = [new Point(1, 2, 3), new Point(4, 5, 6)];
console.log(points[0].x); // 1
console.log(points[1].z); // 6
console.log(points.length); // 2
// === Class field mutation through method ===
class Counter {
count: number;
constructor() { this.count = 0; }
increment(): void { this.count++; }
getCount(): number { return this.count; }
}
const c = new Counter();
c.increment();
c.increment();
c.increment();
console.log(c.getCount()); // 3