From d0aac78109963d03e1ecefa2cfdd926a5d200a97 Mon Sep 17 00:00:00 2001 From: Jerry Belich Date: Sun, 15 Nov 2026 23:53:05 -0800 Subject: [PATCH] fix(compiler): stitches of the first knot are hashed by their bare name get_current_knot() returned hash_string("dead") rather than hash_string("body.dead") for every stitch of a story's first-compiled knot, and the full path for the stitches of every other knot. Which form a caller got depended on where the knot fell in the alphabet. start_container() files every child under indexed_children as well as named_children, and every named child is filed at index -1. The map keeps only the first, so each container's first named child is in both. build_container_hash_map() walks named_children under the child's own name, then walks indexed_children again under the parent's name, so that one child's named children were hashed one level short and overwrote the right _hash. At the root, that is the first knot: its stitches came out as bare names, and the container hash table gained an entry for each bare name as well. The indexed walk now skips a child that is also named, since it was hashed in the named walk. Found by recording get_current_knot() at every ending of 70 stories: three reported hashes that matched no knot or stitch path, all stitches of the alphabetically first knot. With the fix, every ending of all 70 matches its full path. --- inkcpp_compiler/binary_emitter.cpp | 15 ++++++++++++++- inkcpp_test/MoveTo.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/inkcpp_compiler/binary_emitter.cpp b/inkcpp_compiler/binary_emitter.cpp index 1a13f53a..c9c584ed 100644 --- a/inkcpp_compiler/binary_emitter.cpp +++ b/inkcpp_compiler/binary_emitter.cpp @@ -498,7 +498,20 @@ void binary_emitter::build_container_hash_map( // Search indexed children (which duplicates named childen...) // TODO: Merge duplicate child arrays, very error-prone. for (auto child : context->indexed_children) { - build_container_hash_map(hash_map, data, name, child.second); + // A named child was hashed above, under its own name. Walking it again here would hash its + // children under this container's name instead, one level short, and overwrite the right + // _hash with that: "body.dead" became "dead". Every named child is filed under index -1, and + // the map keeps only the first, so it hit the first-compiled knot of every story. + bool named = false; + for (const auto& named_child : context->named_children) { + if (named_child.second == child.second) { + named = true; + break; + } + } + if (! named) { + build_container_hash_map(hash_map, data, name, child.second); + } } } diff --git a/inkcpp_test/MoveTo.cpp b/inkcpp_test/MoveTo.cpp index 3ce09274..f926fee7 100644 --- a/inkcpp_test/MoveTo.cpp +++ b/inkcpp_test/MoveTo.cpp @@ -93,3 +93,31 @@ SCENARIO("run a story, but jump around manually", "[move-to][runtime]") } } } + +SCENARIO("the current knot is named by its full path", "[move-to][runtime]") +{ + // Object keys come out of the compiler's JSON sorted, so "Dialog" is the first knot compiled + // and "Transformations" a later one. The first one's stitches were hashed by their bare names + // ("core"), every later one's by the full path, so which form get_current_knot() returned + // depended on where the knot fell in the alphabet. (A later knot's stitch: Tags.cpp, + // "knot2.sub".) + GIVEN("a story whose first knot has stitches") + { + std::unique_ptr ink{story::from_file(INK_TEST_RESOURCE_DIR "MoveTo.bin")}; + globals globStore = ink->new_globals(); + runner thread = ink->new_runner(globStore); + + WHEN("the story runs into a stitch of the first knot") + { + thread->getall(); + THEN("the knot is its full path") + { + REQUIRE(thread->get_current_knot() == ink::hash_string("Dialog.core")); + } + } + WHEN("a stitch is looked up by its bare name") + { + THEN("it is not found") { REQUIRE_FALSE(thread->move_to(ink::hash_string("core"))); } + } + } +}