Runtime: add method dispatch for array/string/dict/number (.len, .push, .pop, .keys, .values, .has, .toInt) Stdlib: delete global len/push/pop/keys/values/has Tests/docs/examples: migrate to method style; add tests/test_builtin_methods_style.bob All tests pass Breaking: global len/push/pop/keys/values/has removed; use methods instead Parser/AST: add class/extends/extension/super, field initializers Runtime: shared methods with this injection; classParents/classTemplates; super resolution; ownerClass/currentClass; extension lookup order Builtins: method dispatch for array/string/dict/number (.len/.push/.pop/.keys/.values/.has/.toInt); remove global forms Tests/docs/examples: add/refresh for classes, inheritance, super, polymorphism; migrate to method style; all tests pass VS Code extension: update grammar/readme/snippets for new features
86 lines
2.1 KiB
Plaintext
86 lines
2.1 KiB
Plaintext
print("\n--- Test: Class System Stress + Memory Observation ---");
|
|
|
|
// Class with fields and methods using `this`
|
|
class Node {
|
|
var id;
|
|
var payload;
|
|
|
|
func setId(v) { this.id = v; }
|
|
func setPayload(p) { this.payload = p; }
|
|
|
|
func sum() {
|
|
if (this.payload == none) return 0;
|
|
if (this.payload.len() == 0) return 0;
|
|
var i = 0;
|
|
var s = 0;
|
|
while (i < this.payload.len()) {
|
|
s = s + this.payload[i];
|
|
i = i + 1;
|
|
}
|
|
return s;
|
|
}
|
|
|
|
func describe() {
|
|
return "Node(" + toString(this.id) + ", sum=" + toString(this.sum()) + ")";
|
|
}
|
|
}
|
|
|
|
// Basic functional checks
|
|
var a = Node();
|
|
a.setId(7);
|
|
a.setPayload([1, 2, 4]);
|
|
assert(a.sum() == 7, "sum() should sum payload");
|
|
assert(type(a.describe()) == "string", "describe() returns string");
|
|
|
|
// Memory observation before creating many objects
|
|
var mbBefore = memoryUsage();
|
|
print("Memory before allocations (MB): " + toString(mbBefore));
|
|
|
|
// Create many objects and store them
|
|
var COUNT = 40000; // adjust as needed for your machine
|
|
var nodes = [];
|
|
var i = 0;
|
|
while (i < COUNT) {
|
|
var n = Node();
|
|
n.setId(i);
|
|
// small payload to keep variety, not too heavy
|
|
n.setPayload([i % 10, (i * 2) % 10, (i * 3) % 10]);
|
|
nodes.push(n);
|
|
i = i + 1;
|
|
}
|
|
|
|
// Spot-check a few nodes
|
|
assert(nodes[0].sum() == (0 + 0 + 0), "node0 sum");
|
|
assert(nodes[1].sum() == (1 + 2 + 3), "node1 sum");
|
|
assert(type(nodes[100].describe()) == "string", "describe() type");
|
|
|
|
// Memory after allocation
|
|
var mbAfter = memoryUsage();
|
|
print("Memory after allocations (MB): " + toString(mbAfter));
|
|
|
|
// Verify each object is callable and alive: call sum() on every node
|
|
var verify = 0;
|
|
i = 0;
|
|
while (i < COUNT) {
|
|
verify = verify + nodes[i].sum();
|
|
print(nodes[i].describe());
|
|
|
|
i = i + 1;
|
|
}
|
|
print("Verification sum: " + toString(verify));
|
|
|
|
// Pause to allow manual inspection via Activity Monitor, etc.
|
|
print("Press Enter to clear references and observe memory...");
|
|
input();
|
|
|
|
// Clear references to allow GC/release by refcounts
|
|
nodes = [];
|
|
|
|
// Observe memory after clearing
|
|
var mbCleared = memoryUsage();
|
|
print("Memory after clear (MB): " + toString(mbCleared));
|
|
|
|
print("Class stress + memory observation: DONE");
|
|
|
|
|