liquid5-c 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +22 -0
  3. data/README.md +164 -0
  4. data/ext/liquid_c/block.c +970 -0
  5. data/ext/liquid_c/block.h +37 -0
  6. data/ext/liquid_c/c_buffer.c +48 -0
  7. data/ext/liquid_c/c_buffer.h +82 -0
  8. data/ext/liquid_c/context.c +262 -0
  9. data/ext/liquid_c/context.h +72 -0
  10. data/ext/liquid_c/document_body.c +181 -0
  11. data/ext/liquid_c/document_body.h +84 -0
  12. data/ext/liquid_c/expression.c +130 -0
  13. data/ext/liquid_c/expression.h +24 -0
  14. data/ext/liquid_c/extconf.rb +60 -0
  15. data/ext/liquid_c/intutil.h +36 -0
  16. data/ext/liquid_c/lexer.c +172 -0
  17. data/ext/liquid_c/lexer.h +61 -0
  18. data/ext/liquid_c/liquid.c +182 -0
  19. data/ext/liquid_c/liquid.h +112 -0
  20. data/ext/liquid_c/liquid_vm.c +907 -0
  21. data/ext/liquid_c/liquid_vm.h +35 -0
  22. data/ext/liquid_c/mri_compat.h +35 -0
  23. data/ext/liquid_c/native_parser.c +2360 -0
  24. data/ext/liquid_c/native_value.c +49 -0
  25. data/ext/liquid_c/parse_context.c +84 -0
  26. data/ext/liquid_c/parse_context.h +13 -0
  27. data/ext/liquid_c/parser.c +285 -0
  28. data/ext/liquid_c/parser.h +25 -0
  29. data/ext/liquid_c/raw.c +136 -0
  30. data/ext/liquid_c/raw.h +6 -0
  31. data/ext/liquid_c/resource_limits.c +459 -0
  32. data/ext/liquid_c/resource_limits.h +28 -0
  33. data/ext/liquid_c/stringutil.h +37 -0
  34. data/ext/liquid_c/tokenizer.c +326 -0
  35. data/ext/liquid_c/tokenizer.h +46 -0
  36. data/ext/liquid_c/usage.c +18 -0
  37. data/ext/liquid_c/usage.h +9 -0
  38. data/ext/liquid_c/variable.c +198 -0
  39. data/ext/liquid_c/variable.h +61 -0
  40. data/ext/liquid_c/variable_lookup.c +73 -0
  41. data/ext/liquid_c/variable_lookup.h +8 -0
  42. data/ext/liquid_c/vm_assembler.c +631 -0
  43. data/ext/liquid_c/vm_assembler.h +294 -0
  44. data/ext/liquid_c/vm_assembler_pool.c +117 -0
  45. data/ext/liquid_c/vm_assembler_pool.h +27 -0
  46. data/lib/liquid/c/compile_ext.rb +47 -0
  47. data/lib/liquid/c/native_compiler.rb +262 -0
  48. data/lib/liquid/c/native_parser.rb +261 -0
  49. data/lib/liquid/c/native_tags.rb +103 -0
  50. data/lib/liquid/c/native_values.rb +9 -0
  51. data/lib/liquid/c/version.rb +7 -0
  52. data/lib/liquid/c.rb +379 -0
  53. metadata +140 -0
@@ -0,0 +1,37 @@
1
+ #if !defined(LIQUID_BLOCK_H)
2
+ #define LIQUID_BLOCK_H
3
+
4
+ #include "document_body.h"
5
+ #include "vm_assembler_pool.h"
6
+
7
+ typedef struct block_body {
8
+ bool compiled;
9
+
10
+ union {
11
+ struct {
12
+ document_body_entry_t document_body_entry;
13
+ VALUE nodelist;
14
+ } compiled;
15
+ struct {
16
+ VALUE parse_context;
17
+ vm_assembler_pool_t *vm_assembler_pool;
18
+ bool blank;
19
+ unsigned int render_score;
20
+ vm_assembler_t *code;
21
+ bool discardable;
22
+ bool discarded;
23
+ size_t discard_buffer_size;
24
+ long discard_constants_len;
25
+ size_t discard_serial;
26
+ } intermediate;
27
+ } as;
28
+ } block_body_t;
29
+
30
+ void liquid_define_block_body(void);
31
+
32
+ static inline uint8_t *block_body_instructions_ptr(block_body_header_t *body)
33
+ {
34
+ return ((uint8_t *)body) + body->instructions_offset;
35
+ }
36
+
37
+ #endif
@@ -0,0 +1,48 @@
1
+ #include "c_buffer.h"
2
+
3
+ static void c_buffer_expand_for_write(c_buffer_t *buffer, size_t write_size)
4
+ {
5
+ size_t capacity = c_buffer_capacity(buffer);
6
+ size_t size = c_buffer_size(buffer);
7
+ if (write_size > PTRDIFF_MAX - size)
8
+ rb_raise(rb_eNoMemError, "Liquid buffer size overflow");
9
+ size_t required_capacity = size + write_size;
10
+
11
+ if (capacity < 1)
12
+ capacity = 1;
13
+ do {
14
+ if (capacity > PTRDIFF_MAX / 2) {
15
+ capacity = required_capacity;
16
+ break;
17
+ }
18
+ capacity *= 2;
19
+ } while (capacity < required_capacity);
20
+ buffer->data = xrealloc(buffer->data, capacity);
21
+ buffer->data_end = buffer->data + size;
22
+ buffer->capacity_end = buffer->data + capacity;
23
+ }
24
+
25
+ void c_buffer_zero_pad_for_alignment(c_buffer_t *buffer, size_t alignment)
26
+ {
27
+ size_t unaligned_bytes = c_buffer_size(buffer) % alignment;
28
+ if (unaligned_bytes) {
29
+ size_t pad_size = alignment - unaligned_bytes;
30
+ uint8_t *padding = c_buffer_extend_for_write(buffer, pad_size);
31
+ memset(padding, 0, pad_size);
32
+ }
33
+ }
34
+
35
+ void c_buffer_reserve_for_write(c_buffer_t *buffer, size_t write_size)
36
+ {
37
+ if (write_size > c_buffer_capacity(buffer) - c_buffer_size(buffer)) {
38
+ c_buffer_expand_for_write(buffer, write_size);
39
+ }
40
+ }
41
+
42
+ void c_buffer_write(c_buffer_t *buffer, void *write_data, size_t write_size)
43
+ {
44
+ if (!write_size) return;
45
+ c_buffer_reserve_for_write(buffer, write_size);
46
+ memcpy(buffer->data_end, write_data, write_size);
47
+ buffer->data_end += write_size;
48
+ }
@@ -0,0 +1,82 @@
1
+ #ifndef LIQUID_C_BUFFER_H
2
+ #define LIQUID_C_BUFFER_H
3
+
4
+ #include <ruby.h>
5
+
6
+ typedef struct c_buffer {
7
+ uint8_t *data;
8
+ uint8_t *data_end;
9
+ uint8_t *capacity_end;
10
+ } c_buffer_t;
11
+
12
+ static inline c_buffer_t c_buffer_init(void)
13
+ {
14
+ return (c_buffer_t) { NULL, NULL, NULL };
15
+ }
16
+
17
+ static inline c_buffer_t c_buffer_allocate(size_t capacity)
18
+ {
19
+ if (capacity > PTRDIFF_MAX) rb_raise(rb_eNoMemError, "Liquid buffer size overflow");
20
+ uint8_t *data = xmalloc(capacity);
21
+ return (c_buffer_t) { data, data, data + capacity };
22
+ }
23
+
24
+ static inline void c_buffer_free(c_buffer_t *buffer)
25
+ {
26
+ xfree(buffer->data);
27
+ *buffer = c_buffer_init();
28
+ }
29
+
30
+ static inline void c_buffer_reset(c_buffer_t *buffer)
31
+ {
32
+ buffer->data_end = buffer->data;
33
+ }
34
+
35
+ static inline size_t c_buffer_size(const c_buffer_t *buffer)
36
+ {
37
+ return buffer->data ? (size_t)(buffer->data_end - buffer->data) : 0;
38
+ }
39
+
40
+ static inline size_t c_buffer_capacity(const c_buffer_t *buffer)
41
+ {
42
+ return buffer->data ? (size_t)(buffer->capacity_end - buffer->data) : 0;
43
+ }
44
+
45
+ void c_buffer_zero_pad_for_alignment(c_buffer_t *buffer, size_t alignment);
46
+
47
+ void c_buffer_reserve_for_write(c_buffer_t *buffer, size_t write_size);
48
+ void c_buffer_write(c_buffer_t *buffer, void *data, size_t size);
49
+
50
+ static inline void *c_buffer_extend_for_write(c_buffer_t *buffer, size_t write_size) {
51
+ if (write_size > c_buffer_capacity(buffer) - c_buffer_size(buffer))
52
+ c_buffer_reserve_for_write(buffer, write_size);
53
+ void *write_ptr = buffer->data_end;
54
+ if (write_size) buffer->data_end += write_size;
55
+ return write_ptr;
56
+ }
57
+
58
+ static inline void c_buffer_write_byte(c_buffer_t *buffer, uint8_t byte) {
59
+ if (buffer->data_end == buffer->capacity_end) c_buffer_reserve_for_write(buffer, 1);
60
+ *buffer->data_end++ = byte;
61
+ }
62
+
63
+ static inline void c_buffer_write_ruby_value(c_buffer_t *buffer, VALUE value) {
64
+ void *destination = c_buffer_extend_for_write(buffer, sizeof(VALUE));
65
+ memcpy(destination, &value, sizeof(VALUE));
66
+ }
67
+
68
+ static inline void c_buffer_rb_gc_mark(c_buffer_t *buffer)
69
+ {
70
+ if (!buffer->data) return;
71
+ VALUE *buffer_end = (VALUE *)buffer->data_end;
72
+ for (VALUE *obj_ptr = (VALUE *)buffer->data; obj_ptr < buffer_end; obj_ptr++) {
73
+ rb_gc_mark(*obj_ptr);
74
+ }
75
+ }
76
+
77
+ static inline void c_buffer_concat(c_buffer_t *dest, const c_buffer_t *src)
78
+ {
79
+ c_buffer_write(dest, src->data, c_buffer_size(src));
80
+ }
81
+
82
+ #endif
@@ -0,0 +1,262 @@
1
+ #include "liquid.h"
2
+ #include "context.h"
3
+ #include "variable_lookup.h"
4
+ #include "variable.h"
5
+ #include "liquid_vm.h"
6
+ #include "expression.h"
7
+
8
+ static VALUE cLiquidUndefinedVariable;
9
+ ID id_aset, id_set_context;
10
+ static ID id_has_key, id_aref, id_strainer, id_filter_methods_hash, id_strict_variables, id_global_filter;
11
+ static ID id_ivar_global_filter, id_ivar_strict_filters;
12
+ static ID id_ivar_scopes, id_ivar_environments, id_ivar_static_environments, id_ivar_strict_variables, id_ivar_interrupts, id_ivar_resource_limits;
13
+
14
+ void context_internal_init(VALUE context_obj, context_t *context)
15
+ {
16
+ context->self = context_obj;
17
+
18
+ context->environments = rb_ivar_get(context_obj, id_ivar_environments);
19
+ Check_Type(context->environments, T_ARRAY);
20
+
21
+ context->static_environments = rb_ivar_get(context_obj, id_ivar_static_environments);
22
+ Check_Type(context->static_environments, T_ARRAY);
23
+
24
+ context->scopes = rb_ivar_get(context_obj, id_ivar_scopes);
25
+ Check_Type(context->scopes, T_ARRAY);
26
+
27
+ context->interrupts = rb_ivar_get(context->self, id_ivar_interrupts);
28
+ Check_Type(context->interrupts, T_ARRAY);
29
+
30
+ context->resource_limits_obj = rb_ivar_get(context->self, id_ivar_resource_limits);
31
+
32
+ context_refresh(context);
33
+ }
34
+
35
+ void context_refresh(context_t *context)
36
+ {
37
+ VALUE strainer = rb_funcall(context->self, id_strainer, 0);
38
+ Check_Type(strainer, T_OBJECT);
39
+ VALUE filter_methods = rb_funcall(rb_obj_class(strainer), id_filter_methods_hash, 0);
40
+ Check_Type(filter_methods, T_HASH);
41
+ VALUE global_filter = rb_funcall(context->self, id_global_filter, 0);
42
+
43
+ context->strainer = strainer;
44
+ context->filter_methods = filter_methods;
45
+ context->global_filter = global_filter;
46
+ }
47
+
48
+ void context_mark(context_t *context)
49
+ {
50
+ rb_gc_mark(context->self);
51
+ rb_gc_mark(context->environments);
52
+ rb_gc_mark(context->static_environments);
53
+ rb_gc_mark(context->scopes);
54
+ rb_gc_mark(context->strainer);
55
+ rb_gc_mark(context->filter_methods);
56
+ rb_gc_mark(context->interrupts);
57
+ rb_gc_mark(context->resource_limits_obj);
58
+ rb_gc_mark(context->global_filter);
59
+ }
60
+
61
+ static context_t *context_from_obj(VALUE self)
62
+ {
63
+ return &vm_from_context(self)->context;
64
+ }
65
+
66
+ static VALUE context_evaluate(VALUE self, VALUE expression)
67
+ {
68
+ return rb_respond_to(expression, id_evaluate) ? rb_funcall(expression, id_evaluate, 1, self) : expression;
69
+ }
70
+
71
+ void context_raise_undefined_variable(VALUE key)
72
+ {
73
+ rb_enc_raise(utf8_encoding, cLiquidUndefinedVariable, "undefined variable %"PRIsVALUE, key);
74
+ }
75
+
76
+ void context_maybe_raise_undefined_variable(VALUE self, VALUE key)
77
+ {
78
+ if (RTEST(rb_funcall(self, id_strict_variables, 0)))
79
+ context_raise_undefined_variable(key);
80
+ }
81
+
82
+ static bool uses_builtin_hash_lookup(VALUE hash)
83
+ {
84
+ return RB_TYPE_P(hash, T_HASH) && RBASIC_CLASS(hash) == rb_cHash &&
85
+ liquid_method_basic_definition_p(rb_cHash, id_aref) &&
86
+ liquid_method_basic_definition_p(rb_cHash, id_has_key);
87
+ }
88
+
89
+ static bool environments_find_variable(context_t *context, VALUE environments, VALUE key,
90
+ VALUE raise_on_not_found, VALUE *variable_out)
91
+ {
92
+ Check_Type(environments, T_ARRAY);
93
+
94
+ for (long i = 0; i < RARRAY_LEN(environments); i++) {
95
+ bool strict = RTEST(rb_ivar_get(context->self, id_ivar_strict_variables)) && RTEST(raise_on_not_found);
96
+ VALUE environment = RARRAY_AREF(environments, i);
97
+ VALUE variable;
98
+ if (uses_builtin_hash_lookup(environment)) {
99
+ if (strict) {
100
+ variable = rb_hash_lookup2(environment, key, Qundef);
101
+ if (variable == Qundef) {
102
+ context_raise_undefined_variable(key);
103
+ }
104
+ } else {
105
+ variable = rb_hash_aref(environment, key);
106
+ }
107
+ } else {
108
+ if (strict && rb_respond_to(environment, id_has_key) &&
109
+ !RTEST(rb_funcall(environment, id_has_key, 1, key))) {
110
+ context_raise_undefined_variable(key);
111
+ }
112
+ variable = rb_funcall(environment, id_aref, 1, key);
113
+ }
114
+
115
+ variable = materialize_proc(context->self, environment, key, variable);
116
+ if (!NIL_P(variable) || (RTEST(rb_ivar_get(context->self, id_ivar_strict_variables)) && RTEST(raise_on_not_found))) {
117
+ *variable_out = variable;
118
+ return true;
119
+ }
120
+ }
121
+ return false;
122
+ }
123
+
124
+ static VALUE context_find_variable_with_ruby(VALUE self, VALUE key, VALUE raise_on_not_found)
125
+ {
126
+ VALUE keyword_arguments = rb_hash_new();
127
+ rb_hash_aset(keyword_arguments, ID2SYM(rb_intern("raise_on_not_found")), raise_on_not_found);
128
+
129
+ VALUE arguments[] = { key, keyword_arguments };
130
+ return rb_funcallv_kw(self, rb_intern("ruby_find_variable"), 2, arguments, RB_PASS_KEYWORDS);
131
+ }
132
+
133
+ VALUE context_find_variable(context_t *context, VALUE key, VALUE raise_on_not_found)
134
+ {
135
+ VALUE self = context->self;
136
+ bool is_self_lookup = RB_TYPE_P(key, T_STRING) &&
137
+ RSTRING_LEN(key) == 4 && memcmp(RSTRING_PTR(key), "self", 4) == 0;
138
+
139
+ if (is_self_lookup) {
140
+ return context_find_variable_with_ruby(self, key, raise_on_not_found);
141
+ }
142
+
143
+ VALUE scope = Qnil, variable = Qnil;
144
+
145
+ VALUE scopes = context->scopes;
146
+ for (long i = 0; i < RARRAY_LEN(scopes); i++) {
147
+ VALUE this_scope = RARRAY_AREF(scopes, i);
148
+ if (RB_LIKELY(uses_builtin_hash_lookup(this_scope))) {
149
+ // Does not invoke any default value proc, this is equivalent in
150
+ // cost and semantics to #key? but loads the value as well
151
+ variable = rb_hash_lookup2(this_scope, key, Qundef);
152
+ if (variable != Qundef) {
153
+ scope = this_scope;
154
+ goto variable_found;
155
+ }
156
+ } else if (RTEST(rb_funcall(this_scope, id_has_key, 1, key))) {
157
+ if (RTEST(rb_ivar_get(self, id_ivar_strict_variables)) && RTEST(raise_on_not_found) &&
158
+ rb_respond_to(this_scope, id_has_key) && !RTEST(rb_funcall(this_scope, id_has_key, 1, key)))
159
+ context_raise_undefined_variable(key);
160
+ variable = rb_funcall(this_scope, id_aref, 1, key);
161
+ scope = this_scope;
162
+ goto variable_found;
163
+ }
164
+ }
165
+
166
+ if (environments_find_variable(context, context->environments, key, raise_on_not_found, &variable))
167
+ goto convert_variable;
168
+
169
+ if (environments_find_variable(context, context->static_environments, key, raise_on_not_found, &variable))
170
+ goto convert_variable;
171
+
172
+ variable = Qnil;
173
+
174
+ variable_found:
175
+ variable = materialize_proc(self, scope, key, variable);
176
+ convert_variable:
177
+ variable = value_to_liquid_and_set_context(variable, self);
178
+
179
+ return variable;
180
+ }
181
+
182
+ static VALUE context_find_variable_method(VALUE self, VALUE key, VALUE raise_on_not_found)
183
+ {
184
+ return context_find_variable(context_from_obj(self), key, raise_on_not_found);
185
+ }
186
+
187
+ static VALUE context_set_strict_variables(VALUE self, VALUE strict_variables)
188
+ {
189
+ rb_ivar_set(self, id_ivar_strict_variables, strict_variables);
190
+ return strict_variables;
191
+ }
192
+
193
+ static VALUE context_set_global_filter(VALUE self, VALUE filter)
194
+ {
195
+ rb_ivar_set(self, id_ivar_global_filter, filter);
196
+ vm_t *vm = vm_cached_for_context(self);
197
+ if (vm) vm->context.global_filter = filter;
198
+ return filter;
199
+ }
200
+
201
+ static VALUE context_set_strict_filters(VALUE self, VALUE strict_filters)
202
+ {
203
+ rb_ivar_set(self, id_ivar_strict_filters, strict_filters);
204
+ return strict_filters;
205
+ }
206
+
207
+ static VALUE context_add_filters(VALUE self, VALUE filters)
208
+ {
209
+ VALUE result = rb_funcall(self, rb_intern("ruby_add_filters"), 1, filters);
210
+ vm_t *vm = vm_cached_for_context(self);
211
+ if (vm) {
212
+ vm->context.strainer = Qnil;
213
+ vm->context.filter_methods = Qnil;
214
+ vm->context_generation = 0;
215
+ vm->context_revision++;
216
+ }
217
+ return result;
218
+ }
219
+
220
+ // Shopify requires checking if we are filtering, so provide a
221
+ // way to do that in liquid-c until we figure out how we want to
222
+ // support that longer term.
223
+ VALUE context_filtering_p(VALUE self)
224
+ {
225
+ return liquid_vm_filtering(self) ? Qtrue : Qfalse;
226
+ }
227
+
228
+ void liquid_define_context(void)
229
+ {
230
+ id_has_key = rb_intern("key?");
231
+ id_aset = rb_intern("[]=");
232
+ id_aref = rb_intern("[]");
233
+ id_set_context = rb_intern("context=");
234
+ id_strainer = rb_intern("strainer");
235
+ id_filter_methods_hash = rb_intern("filter_methods_hash");
236
+ id_global_filter = rb_intern("global_filter");
237
+ id_strict_variables = rb_intern("strict_variables");
238
+
239
+ id_ivar_global_filter = rb_intern("@global_filter");
240
+ id_ivar_strict_filters = rb_intern("@strict_filters");
241
+ id_ivar_scopes = rb_intern("@scopes");
242
+ id_ivar_environments = rb_intern("@environments");
243
+ id_ivar_static_environments = rb_intern("@static_environments");
244
+ id_ivar_strict_variables = rb_intern("@strict_variables");
245
+ id_ivar_interrupts = rb_intern("@interrupts");
246
+ id_ivar_resource_limits = rb_intern("@resource_limits");
247
+
248
+ cLiquidVariableLookup = rb_const_get(mLiquid, rb_intern("VariableLookup"));
249
+ rb_global_variable(&cLiquidVariableLookup);
250
+
251
+ cLiquidUndefinedVariable = rb_const_get(mLiquid, rb_intern("UndefinedVariable"));
252
+ rb_global_variable(&cLiquidUndefinedVariable);
253
+
254
+ VALUE cLiquidContext = rb_const_get(mLiquid, rb_intern("Context"));
255
+ rb_define_method(cLiquidContext, "c_evaluate", context_evaluate, 1);
256
+ rb_define_method(cLiquidContext, "c_find_variable", context_find_variable_method, 2);
257
+ rb_define_method(cLiquidContext, "c_strict_variables=", context_set_strict_variables, 1);
258
+ rb_define_method(cLiquidContext, "c_global_filter=", context_set_global_filter, 1);
259
+ rb_define_method(cLiquidContext, "c_strict_filters=", context_set_strict_filters, 1);
260
+ rb_define_method(cLiquidContext, "c_add_filters", context_add_filters, 1);
261
+ rb_define_private_method(cLiquidContext, "c_filtering?", context_filtering_p, 0);
262
+ }
@@ -0,0 +1,72 @@
1
+ #if !defined(LIQUID_CONTEXT_H)
2
+ #define LIQUID_CONTEXT_H
3
+
4
+ #include "resource_limits.h"
5
+
6
+ typedef struct context {
7
+ VALUE self;
8
+ VALUE environments;
9
+ VALUE static_environments;
10
+ VALUE scopes;
11
+ VALUE strainer;
12
+ VALUE filter_methods;
13
+ VALUE interrupts;
14
+ VALUE resource_limits_obj;
15
+ VALUE global_filter;
16
+ } context_t;
17
+
18
+ void liquid_define_context(void);
19
+ void liquid_define_native_values(void);
20
+ #ifdef HAVE_RB_CALLABLE_METHOD_ENTRY
21
+ VALUE liquid_value_to_liquid(VALUE value);
22
+ #else
23
+ #define liquid_value_to_liquid(value) rb_funcall((value), id_to_liquid, 0)
24
+ #endif
25
+ void context_internal_init(VALUE context_obj, context_t *context);
26
+ void context_refresh(context_t *context);
27
+ void context_mark(context_t *context);
28
+ VALUE context_find_variable(context_t *context, VALUE key, VALUE raise_on_not_found);
29
+ void context_maybe_raise_undefined_variable(VALUE self, VALUE key);
30
+ __attribute__((noreturn)) void context_raise_undefined_variable(VALUE key);
31
+
32
+ extern ID id_aset, id_set_context;
33
+
34
+ #ifndef RB_SPECIAL_CONST_P
35
+ // RB_SPECIAL_CONST_P added in Ruby 2.3
36
+ #define RB_SPECIAL_CONST_P SPECIAL_CONST_P
37
+ #endif
38
+
39
+ inline static VALUE value_to_liquid_and_set_context(VALUE value, VALUE context_to_set)
40
+ {
41
+ if (rb_respond_to(value, id_set_context))
42
+ rb_funcall(value, id_set_context, 1, context_to_set);
43
+ VALUE liquid_value = liquid_value_to_liquid(value);
44
+ if (RTEST(rb_funcall(value, rb_intern("!="), 1, liquid_value)) && rb_respond_to(liquid_value, id_set_context))
45
+ rb_funcall(liquid_value, id_set_context, 1, context_to_set);
46
+ return liquid_value;
47
+ }
48
+
49
+ inline static VALUE nested_value_to_liquid_and_set_context(VALUE value, VALUE context_to_set)
50
+ {
51
+ VALUE liquid_value = liquid_value_to_liquid(value);
52
+ if (rb_respond_to(liquid_value, id_set_context))
53
+ rb_funcall(liquid_value, id_set_context, 1, context_to_set);
54
+ return liquid_value;
55
+ }
56
+
57
+
58
+ inline static VALUE materialize_proc(VALUE context, VALUE scope, VALUE key, VALUE value)
59
+ {
60
+ if (scope != Qnil && rb_obj_is_proc(value) && rb_respond_to(scope, id_aset)) {
61
+ if (!RTEST(rb_equal(rb_funcall(value, rb_intern("arity"), 0), INT2FIX(0)))) {
62
+ value = rb_funcall(value, id_call, 1, context);
63
+ } else {
64
+ value = rb_funcall(value, id_call, 0);
65
+ }
66
+ rb_funcall(scope, id_aset, 2, key, value);
67
+ }
68
+ return value;
69
+ }
70
+
71
+ #endif
72
+
@@ -0,0 +1,181 @@
1
+ #include <ruby.h>
2
+ #include <stdalign.h>
3
+ #include "liquid.h"
4
+ #include "vm_assembler.h"
5
+ #include "document_body.h"
6
+
7
+ static VALUE cLiquidCDocumentBody;
8
+
9
+ static void document_body_mark(void *ptr)
10
+ {
11
+ document_body_t *body = ptr;
12
+ /* When Liquid::C::BlockBody#freeze is called, it calls
13
+ * document_body_write_block_body which sets the document_body_entry but
14
+ * does not yet set the compiled flag to true. During this time, the only
15
+ * reference to this Liquid::C::DocumentBody object is in the instance
16
+ * variables of the parse_context which is marked movable by Ruby. This
17
+ * causes the self reference here to be moved by compaction causing it to
18
+ * point to an incorrect object. */
19
+ rb_gc_mark(body->self);
20
+ rb_gc_mark(body->constants);
21
+ }
22
+
23
+ static void document_body_free(void *ptr)
24
+ {
25
+ document_body_t *body = ptr;
26
+ c_buffer_free(&body->buffer);
27
+ c_buffer_free(&body->invalid_serials);
28
+ xfree(body);
29
+ }
30
+
31
+ static size_t document_body_memsize(const void *ptr)
32
+ {
33
+ const document_body_t *body = ptr;
34
+ return sizeof(document_body_t) + c_buffer_capacity(&body->buffer) + c_buffer_capacity(&body->invalid_serials);
35
+ }
36
+
37
+ const rb_data_type_t document_body_data_type = {
38
+ "liquid_document_body",
39
+ { document_body_mark, document_body_free, document_body_memsize },
40
+ NULL, NULL, RUBY_TYPED_FREE_IMMEDIATELY
41
+ };
42
+
43
+ static VALUE document_body_allocate(VALUE klass)
44
+ {
45
+ document_body_t *body;
46
+
47
+ VALUE obj = TypedData_Make_Struct(klass, document_body_t, &document_body_data_type, body);
48
+ body->self = obj;
49
+ body->frontend_cache_size = -1;
50
+ body->constants = rb_ary_new();
51
+ body->buffer = c_buffer_init();
52
+ body->invalid_serials = c_buffer_init();
53
+
54
+ return obj;
55
+ }
56
+
57
+ #define DocumentBody_Get_Struct(obj, sval) TypedData_Get_Struct(obj, document_body_t, &document_body_data_type, sval)
58
+
59
+ bool document_body_cache_stable(VALUE self, long size)
60
+ {
61
+ if (RB_OBJ_FROZEN(self)) return false;
62
+ document_body_t *body;
63
+ DocumentBody_Get_Struct(self, body);
64
+ bool stable = body->frontend_cache_size == size;
65
+ body->frontend_cache_size = size;
66
+ return stable;
67
+ }
68
+
69
+ void document_body_checkpoint(VALUE self, size_t *buffer_size, long *constants_len, size_t *serial)
70
+ {
71
+ document_body_t *body;
72
+ DocumentBody_Get_Struct(self, body);
73
+ *buffer_size = c_buffer_size(&body->buffer);
74
+ *constants_len = RARRAY_LEN(body->constants);
75
+ *serial = body->next_serial;
76
+ }
77
+
78
+ void document_body_truncate(VALUE self, size_t buffer_size, long constants_len, size_t serial)
79
+ {
80
+ document_body_t *body;
81
+ DocumentBody_Get_Struct(self, body);
82
+ assert(!RB_OBJ_FROZEN(self));
83
+ assert(buffer_size <= c_buffer_size(&body->buffer));
84
+ assert(constants_len <= RARRAY_LEN(body->constants));
85
+ assert(serial <= body->next_serial);
86
+ if (serial < body->next_serial)
87
+ c_buffer_reserve_for_write(&body->invalid_serials, sizeof(serial_range_t));
88
+ rb_ary_resize(body->constants, constants_len);
89
+ if (serial < body->next_serial) {
90
+ serial_range_t range = { serial + 1, body->next_serial };
91
+ // An enclosing case invalidates every range recorded since its checkpoint.
92
+ serial_range_t *start = (serial_range_t *)body->invalid_serials.data;
93
+ serial_range_t *end = (serial_range_t *)body->invalid_serials.data_end;
94
+ while (end > start && end[-1].first >= range.first) end--;
95
+ body->invalid_serials.data_end = (uint8_t *)end;
96
+ c_buffer_write(&body->invalid_serials, &range, sizeof(range));
97
+ }
98
+ if (buffer_size == 0) c_buffer_free(&body->buffer);
99
+ else body->buffer.data_end = body->buffer.data + buffer_size;
100
+ }
101
+
102
+ void document_body_finish(VALUE self)
103
+ {
104
+ document_body_t *body;
105
+ DocumentBody_Get_Struct(self, body);
106
+ size_t size = c_buffer_size(&body->buffer);
107
+ size_t capacity = c_buffer_capacity(&body->buffer);
108
+ if (capacity - size > 65536 && capacity - size > size / 4) {
109
+ if (size) {
110
+ body->buffer.data = xrealloc(body->buffer.data, size);
111
+ body->buffer.data_end = body->buffer.data + size;
112
+ body->buffer.capacity_end = body->buffer.data_end;
113
+ } else {
114
+ c_buffer_free(&body->buffer);
115
+ }
116
+ }
117
+ rb_ary_freeze(body->constants);
118
+ rb_obj_freeze(self);
119
+ }
120
+
121
+ VALUE document_body_new_instance(void)
122
+ {
123
+ return rb_class_new_instance(0, NULL, cLiquidCDocumentBody);
124
+ }
125
+
126
+ document_body_entry_t document_body_write_block_body(VALUE self, bool blank, uint32_t render_score, vm_assembler_t *code)
127
+ {
128
+ assert(!RB_OBJ_FROZEN(self));
129
+
130
+ document_body_t *body;
131
+ DocumentBody_Get_Struct(self, body);
132
+
133
+ if (c_buffer_size(&code->instructions) > UINT32_MAX ||
134
+ c_buffer_size(&code->constants) / sizeof(VALUE) > UINT32_MAX ||
135
+ (size_t)RARRAY_LEN(body->constants) > UINT32_MAX ||
136
+ c_buffer_size(&code->constants) / sizeof(VALUE) > UINT32_MAX - (size_t)RARRAY_LEN(body->constants))
137
+ rb_raise(rb_eRangeError, "Liquid document exceeds bytecode representation");
138
+ if (body->next_serial == SIZE_MAX)
139
+ rb_raise(rb_eRangeError, "Too many Liquid block bodies");
140
+
141
+ size_t old_size = c_buffer_size(&body->buffer);
142
+ size_t alignment = alignof(block_body_header_t);
143
+ size_t padding = (alignment - old_size % alignment) % alignment;
144
+ size_t instructions_bytes = c_buffer_size(&code->instructions);
145
+ if (padding > SIZE_MAX - sizeof(block_body_header_t) ||
146
+ instructions_bytes > SIZE_MAX - padding - sizeof(block_body_header_t))
147
+ rb_raise(rb_eNoMemError, "Liquid document size overflow");
148
+ // Reserve every byte before appending constants. rb_ary_cat may raise;
149
+ // until it succeeds, the document's logical size is unchanged.
150
+ vm_assembler_allocation_checkpoint();
151
+ c_buffer_reserve_for_write(&body->buffer, padding + sizeof(block_body_header_t) + instructions_bytes);
152
+
153
+ assert(c_buffer_size(&code->constants) % sizeof(VALUE) == 0);
154
+ uint32_t constants_len = (uint32_t)(c_buffer_size(&code->constants) / sizeof(VALUE));
155
+
156
+ vm_assembler_allocation_checkpoint();
157
+ rb_ary_cat(body->constants, (VALUE *)code->constants.data, constants_len);
158
+ c_buffer_zero_pad_for_alignment(&body->buffer, alignment);
159
+ size_t buffer_offset = c_buffer_size(&body->buffer);
160
+ block_body_header_t *buf_block_body = c_buffer_extend_for_write(&body->buffer, sizeof(block_body_header_t));
161
+ buf_block_body->instructions_offset = (uint32_t)sizeof(block_body_header_t);
162
+ buf_block_body->instructions_bytes = (uint32_t)c_buffer_size(&code->instructions);
163
+ buf_block_body->constants_offset = (uint32_t)(RARRAY_LEN(body->constants) - constants_len);
164
+ buf_block_body->constants_len = constants_len;
165
+ buf_block_body->flags = 0;
166
+ if (blank) buf_block_body->flags |= BLOCK_BODY_HEADER_FLAG_BLANK;
167
+ buf_block_body->render_score = render_score;
168
+ buf_block_body->max_stack_size = code->max_stack_size;
169
+
170
+ c_buffer_concat(&body->buffer, &code->instructions);
171
+
172
+ return (document_body_entry_t) { .body = body, .buffer_offset = buffer_offset,
173
+ .serial = ++body->next_serial };
174
+ }
175
+
176
+ void liquid_define_document_body(void)
177
+ {
178
+ cLiquidCDocumentBody = rb_define_class_under(mLiquidC, "DocumentBody", rb_cObject);
179
+ rb_global_variable(&cLiquidCDocumentBody);
180
+ rb_define_alloc_func(cLiquidCDocumentBody, document_body_allocate);
181
+ }