Skip to content

Add heap implementation#30

Merged
Mauw94 merged 17 commits into
masterfrom
feature/heap-start
May 25, 2026
Merged

Add heap implementation#30
Mauw94 merged 17 commits into
masterfrom
feature/heap-start

Conversation

@Mauw94
Copy link
Copy Markdown
Owner

@Mauw94 Mauw94 commented May 24, 2026

  • Fix IndexArray in VirtualMachine.cs to validate the target is heap-allocated before casting, so non-heap or non-array values throw IndexTargetNotArray instead of ValueNotHeapAllocated/HeapError

Mauw94 added 7 commits May 24, 2026 15:10
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.
@Mauw94 Mauw94 requested a review from Copilot May 25, 2026 14:19
@Mauw94 Mauw94 marked this pull request as ready for review May 25, 2026 14:20
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Pop after 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.

Comment thread Lumi.VM/Value.cs Outdated
Comment thread Lumi.VM/VirtualMachine.cs
Comment thread Lumi.VM/VirtualMachine.cs
Comment thread Lumi.VM/VirtualMachine.cs
Comment thread Lumi.VM/VirtualMachine.cs
Comment thread Lumi.VM/NativeMemberDispatcher.cs Outdated
Comment thread Lumi.VM/NativeMemberDispatcher.cs Outdated
Comment thread Lumi.VM/Heap/HeapManager.cs Outdated
Comment thread Lumi.VM/Heap/HeapManager.cs
Comment thread Lumi.Engine/Program.cs
Mauw94 and others added 2 commits May 25, 2026 16:30
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>
Mauw94 and others added 2 commits May 25, 2026 16:31
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>
Copilot finished work on behalf of Mauw94 May 25, 2026 14:40
Copilot finished work on behalf of Mauw94 May 25, 2026 14:43
Copilot finished work on behalf of Mauw94 May 25, 2026 14:50
Copilot finished work on behalf of Mauw94 May 25, 2026 14:51
@Mauw94 Mauw94 merged commit 1871964 into master May 25, 2026
1 check passed
@Mauw94 Mauw94 deleted the feature/heap-start branch May 25, 2026 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants