Add heap implementation#30
Merged
Merged
Conversation
Replaced inline array, struct, and native object representations with heap-allocated objects. Introduced `HeapArrayObject`, `HeapStructObject`, `HeapNativeObject`, and `HeapStringObject` classes. Refactored `HeapObject` as an abstract base class with shared properties and methods. Updated `Value` struct to use `HeapHandle` for heap-allocated objects, removing redundant fields. Extended `ValueKind` enum with `HeapObject` and `Reference`. Refactored `NativeMemberDispatcher` and `VirtualMachine` to support heap-allocated objects for method invocation, field access, and array indexing. Revised file I/O methods to use heap-backed arrays. Added new test cases in `ExecutionPipelineTests` for heap-backed list behavior. Modularized heap object classes into separate files for maintainability. Improved garbage collection infrastructure with a placeholder `CollectGarbage` method. Replaced `FileNotFoundException` with a console error in `LoadScript`. Updated `Print` instruction to handle heap-allocated objects. Removed redundant `FromArray`, `FromStruct`, and `FromNativeObject` methods from `Value`.
Refactored the `Heap` class to improve memory management: - Added garbage collection methods (`MaybeCollect`, `CollectGarbage`, etc.). - Replaced `Deallocate` with a generic `Get<T>` method. - Introduced `FreeCount` property and private `Get` method. Updated `HeapObject` hierarchy: - Made `Kind` an abstract property. - Simplified `VisitReferences` and improved type safety. Enhanced `VirtualMachine` and `NativeMemberDispatcher`: - Integrated garbage collection with `MaybeCollect`. - Used `Get<T>` for safer heap object retrieval. Removed unused code, improved documentation, and enhanced error handling.
Refactored `Heap` to `HeapManager` with string interning and improved garbage collection. Updated `debug.lumi` and `heap2.lumi` to demonstrate object-oriented features. Added `HeapTests.cs` with comprehensive unit tests. Introduced `Lumi.VM.Heap.Tests` project and updated solution structure. Refactored related files to align with `HeapManager`.
Added the `HeapReferenceWorkloadSource` constant to the `SourceStrings` class, which includes a workload involving bucket operations and total calculations. Introduced the `Test_Heap_Reference_Workload` method in `BenchmarkTests` to benchmark this workload with 1000 iterations.
There was a problem hiding this comment.
Pull request overview
This PR introduces a heap-backed object model to the Lumi VM (arrays/structs/native prelude objects) along with a basic mark/sweep heap manager and test coverage to validate allocation, interning, and GC behavior. It also updates bytecode generation to pop unused expression results to prevent stack growth in loops.
Changes:
- Add
HeapManager+ heap object types (array/struct/string/native) and wire the VM to allocate/operate on heap handles. - Update native member dispatch (array methods + File prelude methods) to work with heap objects and return heap-backed lists for
File.readLines. - Emit
Popafter value-producing expression statements and update tests/benchmarks/examples accordingly.
Reviewed changes
Copilot reviewed 32 out of 34 changed files in this pull request and generated 10 comments.
Show a summary per file
| File | Description |
|---|---|
| Lumi.VM/VirtualMachineError.cs | Adds a new VM error for non-heap values missing a heap handle. |
| Lumi.VM/VirtualMachine.cs | Routes array/struct/native objects through heap allocation, root enumeration, and heap-aware printing. |
| Lumi.VM/ValueKind.cs | Introduces HeapObject (and Reference) value kinds. |
| Lumi.VM/Value.cs | Replaces inline array/struct/native payloads with HeapHandle storage helpers. |
| Lumi.VM/NativeMemberDispatcher.cs | Dispatches methods against HeapObject types; updates File prelude list handling to heap arrays. |
| Lumi.VM/Heap/HeapStructObject.cs | Adds heap representation of struct instances and GC reference visiting. |
| Lumi.VM/Heap/HeapStringObject.cs | Adds heap representation of strings (interning support). |
| Lumi.VM/Heap/HeapObject.cs | Defines heap object base type (mark bit, kind, references, printing). |
| Lumi.VM/Heap/HeapNativeObject.cs | Adds heap representation for prelude/native globals. |
| Lumi.VM/Heap/HeapManager.cs | Implements slot-based heap + mark/sweep GC + string interning. |
| Lumi.VM/Heap/HeapHandle.cs | Adds value-type handle used by Value to reference heap slots. |
| Lumi.VM/Heap/HeapError.cs | Adds heap-specific exception types for OOM/dangling handles. |
| Lumi.VM/Heap/HeapArrayObject.cs | Adds heap representation of arrays and GC reference visiting. |
| Lumi.VM/AssemblyInfo.cs | Exposes internals to the new heap test project. |
| Lumi.VM.Heap.Tests/MSTestSettings.cs | Enables method-level parallelization for heap tests. |
| Lumi.VM.Heap.Tests/Lumi.VM.Heap.Tests.csproj | Adds a dedicated heap test project. |
| Lumi.VM.Heap.Tests/HeapTests.cs | Adds tests for allocation, interning, and GC correctness. |
| Lumi.slnx | Includes the new heap test project in the solution. |
| Lumi.Engine/Program.cs | Changes script loading behavior when script is missing (now prints error + returns empty source). |
| Lumi.Engine.Tests/ExecutionPipelineTests.cs | Adds end-to-end tests for heap-backed File.readLines / File.writeLines. |
| Lumi.Engine.Tests/BenchmarkTests.cs | Adds a heap reference workload benchmark. |
| Lumi.Engine.Tests/BenchmarkSources.cs | Adds heap reference workload source text. |
| Lumi.Engine.Tests/ArrayTests.cs | Adds a regression test ensuring list.add in loops doesn’t overflow the VM stack. |
| Lumi.Bytecode/Instructions/Instruction.cs | Adds Instruction.Pop() factory method. |
| Lumi.Bytecode/BytecodeGenerator.cs | Emits Pop for value-producing expression statements. |
| Lumi.Bytecode.Tests/BytecodeTests.cs | Updates expected instruction sequences to include Pop. |
| Examples/stdlib/fileio2.lumi | Adds example using File.readLines/writeText. |
| Examples/stdlib/fileio1.lumi | Updates example syntax for variable declaration. |
| Examples/heap/heap4.lumi | Adds heap stress example (buckets/items aliasing). |
| Examples/heap/heap3.lumi | Adds heap stress example (many structs in arrays). |
| Examples/heap/heap2.lumi | Adds heap example showing struct holds list reference. |
| Examples/heap/heap1.lumi | Adds heap example showing list alias mutation. |
| Examples/debug.lumi | Adds a debug script that exercises heap behavior with structs/lists. |
Comments suppressed due to low confidence (1)
Lumi.VM/Value.cs:71
- Value.PrintValue() doesn't handle the newly introduced ValueKind.HeapObject / ValueKind.Reference kinds. Any path that calls Value.PrintValue() on a heap-backed value (e.g., HeapArrayObject.PrintValue() on nested values, string interpolation, etc.) will currently throw UnkownValueKind.
public string PrintValue() => Kind switch
{
ValueKind.Number => Number.ToString(),
ValueKind.String => "\"" + String + "\"" ?? string.Empty, // TODO: will be obsolute once we move string to the heap
ValueKind.Boolean => Bool.ToString(),
ValueKind.Null => "null",
ValueKind.Undefined => "undefined",
_ => throw VirtualMachineError.UnkownValueKind(Kind),
};
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
IndexArrayinVirtualMachine.csto validate the target is heap-allocated before casting, so non-heap or non-array values throwIndexTargetNotArrayinstead ofValueNotHeapAllocated/HeapError