Skip to content

Commit 771cecc

Browse files
committed
Cache SelfDrop per context
1 parent b18930f commit 771cecc

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

lib/liquid/context.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def find_variable(key, raise_on_not_found: true)
208208

209209
# `self` resolves to a SelfDrop (enabling `self['var']` lookups),
210210
# but only when it hasn't been explicitly assigned as a local variable.
211-
return SelfDrop.new(self) if key == Expression::SELF && !index
211+
return @self_drop ||= SelfDrop.new(self) if key == Expression::SELF && !index
212212

213213
variable = if index
214214
lookup_and_evaluate(@scopes[index], key, raise_on_not_found: raise_on_not_found)

test/integration/self_drop_context_test.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,12 @@ def test_self_drop_context_setter_is_undefined
7979

8080
def test_self_drop_repeated_lookups_compare_equal_for_same_context
8181
context = Context.new
82+
drop = context.find_variable("self")
83+
cached_drop = context.find_variable("self")
8284

83-
assert_equal(context.find_variable("self"), context.find_variable("self"))
85+
assert_same(drop, cached_drop)
86+
assert_equal(drop.object_id, cached_drop.object_id)
87+
assert_equal(drop, cached_drop)
8488
end
8589

8690
def test_assigned_self_drop_compares_equal_to_itself

0 commit comments

Comments
 (0)