Skip to content

Add method dispatch and self injection to bytecode VM #53

@synapticvoid

Description

@synapticvoid

Summary

Support method calls on struct/enum instances with automatic self injection, including the special equals and toString methods.

Strategy: bound method value

Rather than a dedicated call_method opcode, use a .bound_method value variant. get_field resolves the method and bundles the receiver + function into a single typed value; call dispatches on it like any other callable. Keeps opcodes orthogonal, and bound methods become first-class values for free.

Checklist

VM — value

  • Add .bound_method = { fn_idx, receiver } variant to vm/value.zig

VM — execution

  • get_field fallback: if field not found as data, look up TypeName.fieldName in globals → push .bound_method
  • call new arm: .bound_method → inject receiver as first arg, build frame

Compiler

  • Compile struct method declarations: emit each method in struct.methods as TypeName.methodName in globals

Special methods

Tests

  • Method call on a struct instance
  • self correctly refers to the receiver inside the method body
  • Method with additional arguments beyond self
  • Custom equals method used by == operator
  • Custom toString method used by print
  • Method on enum variant instance

Depends on #43 (call frames), #49 (struct instances), and #51 (builtins).

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions