From 5c8ad2a7d5828e6c17383bf184e65bd8ef516076 Mon Sep 17 00:00:00 2001 From: Michael Go Date: Mon, 22 Jan 2024 14:41:23 -0400 Subject: [PATCH] contextualize value before invoking to_liquid --- ext/liquid_c/context.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ext/liquid_c/context.h b/ext/liquid_c/context.h index 8cd43d90..e810ee37 100644 --- a/ext/liquid_c/context.h +++ b/ext/liquid_c/context.h @@ -44,12 +44,16 @@ inline static VALUE value_to_liquid_and_set_context(VALUE value, VALUE context_t if (klass == rb_cString || klass == rb_cArray || klass == rb_cHash) return value; - value = rb_funcall(value, id_to_liquid, 0); - + // set value's context before invoking #to_liquid if (rb_respond_to(value, id_set_context)) rb_funcall(value, id_set_context, 1, context_to_set); - return value; + VALUE liquid_value = rb_funcall(value, id_to_liquid, 0); + + if (liquid_value != value && rb_respond_to(liquid_value, id_set_context)) + rb_funcall(liquid_value, id_set_context, 1, context_to_set); + + return liquid_value; }