Week 15 of 125

Crafting Interpreters, chapters 27 and 28

Sign up or sign in to tick these off and give this week a due date.

  • Build Ch 27: Classes and Instances Verified by tests

    Classes and instances in clox: class objects, instance objects with a fields hash table, and get and set property instructions.

    Time About 6 hours this week

    Why it matters

    Shows the real cost of dynamic objects: every field access is a hash lookup, which is why engines like V8 invented hidden classes.

    How to approach it

    Remember to mark new object types in the GC. Run under stress GC again.

    Challenges

    4 challenges at the end of the chapter.

    Challenges 1 (handling missing fields gracefully), 2 (dynamic field access by string) and 3 (deleting fields) all involve language design choices; pick one. Challenge 4 asks about faster field access, which connects to hidden classes.

    Traps

    • Forgetting to blacken new object types in the GC, which causes use-after-free bugs.

    Reading guide

  • Build Ch 28: Methods and Initializers Verified by tests

    Methods, bound methods, this, initializers, and the OP_INVOKE optimization that combines property lookup and call.

    Time About 7 hours this week

    Why it matters

    The invoke optimization is a real technique that production VMs use, and you get to measure its effect.

    How to approach it

    Benchmark method calls before and after OP_INVOKE.

    Challenges

    3 challenges at the end of the chapter.

    Challenge 1 (faster init lookup) is quick. Challenge 2 is about inline caching, which is how fast JS engines work; reading about it is worthwhile even if you do not implement it.

    Traps

    • Not handling the case where a field shadows a method in OP_INVOKE.
    • Leaking bound method objects or forgetting to mark them.

    Reading guide

  • Two problems from the NeetCode 150, in order. One box for the pair.