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"))); } + } + } +}