diff --git a/lib/liquid/context.rb b/lib/liquid/context.rb index 7902ebcf..26ff7285 100644 --- a/lib/liquid/context.rb +++ b/lib/liquid/context.rb @@ -208,7 +208,7 @@ module Liquid # `self` resolves to a SelfDrop (enabling `self['var']` lookups), # but only when it hasn't been explicitly assigned as a local variable. - return SelfDrop.new(self) if key == Expression::SELF && !index + return @self_drop ||= SelfDrop.new(self) if key == Expression::SELF && !index variable = if index lookup_and_evaluate(@scopes[index], key, raise_on_not_found: raise_on_not_found) diff --git a/test/integration/self_drop_context_test.rb b/test/integration/self_drop_context_test.rb index 6fd7ec88..f57fdc39 100644 --- a/test/integration/self_drop_context_test.rb +++ b/test/integration/self_drop_context_test.rb @@ -79,8 +79,12 @@ class SelfDropContextTest < Minitest::Test def test_self_drop_repeated_lookups_compare_equal_for_same_context context = Context.new + drop = context.find_variable("self") + cached_drop = context.find_variable("self") - assert_equal(context.find_variable("self"), context.find_variable("self")) + assert_same(drop, cached_drop) + assert_equal(drop.object_id, cached_drop.object_id) + assert_equal(drop, cached_drop) end def test_assigned_self_drop_compares_equal_to_itself