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,326 @@
1
+ #include <assert.h>
2
+ #include "liquid.h"
3
+ #include "tokenizer.h"
4
+ #include "stringutil.h"
5
+
6
+ VALUE cLiquidTokenizer;
7
+
8
+ static void tokenizer_mark(void *ptr)
9
+ {
10
+ tokenizer_t *tokenizer = ptr;
11
+ rb_gc_mark(tokenizer->source);
12
+ rb_gc_mark(tokenizer->raw_tag_body);
13
+ rb_gc_mark(tokenizer->line_number);
14
+ }
15
+
16
+ static void tokenizer_free(void *ptr)
17
+ {
18
+ tokenizer_t *tokenizer = ptr;
19
+ xfree(tokenizer);
20
+ }
21
+
22
+ static size_t tokenizer_memsize(const void *ptr)
23
+ {
24
+ return ptr ? sizeof(tokenizer_t) : 0;
25
+ }
26
+
27
+ const rb_data_type_t tokenizer_data_type = {
28
+ "liquid_tokenizer",
29
+ { tokenizer_mark, tokenizer_free, tokenizer_memsize, },
30
+ #if defined(RUBY_TYPED_FREE_IMMEDIATELY)
31
+ NULL, NULL, RUBY_TYPED_FREE_IMMEDIATELY
32
+ #endif
33
+ };
34
+
35
+ static VALUE tokenizer_allocate(VALUE klass)
36
+ {
37
+ VALUE obj;
38
+ tokenizer_t *tokenizer;
39
+
40
+ obj = TypedData_Make_Struct(klass, tokenizer_t, &tokenizer_data_type, tokenizer);
41
+ tokenizer->source = Qnil;
42
+ tokenizer->line_number = Qnil;
43
+ tokenizer->bug_compatible_whitespace_trimming = false;
44
+ tokenizer->raw_tag_body = Qnil;
45
+ return obj;
46
+ }
47
+
48
+ static VALUE tokenizer_initialize_method(VALUE self, VALUE source, VALUE start_line_number, VALUE for_liquid_tag)
49
+ {
50
+ tokenizer_t *tokenizer;
51
+
52
+ Check_Type(source, T_STRING);
53
+ check_utf8_encoding(source, "source");
54
+
55
+ #define MAX_SOURCE_CODE_BYTES ((1 << 24) - 1)
56
+ if (RSTRING_LEN(source) > MAX_SOURCE_CODE_BYTES) {
57
+ rb_enc_raise(utf8_encoding, rb_eArgError, "Source too large, max %d bytes", MAX_SOURCE_CODE_BYTES);
58
+ }
59
+ #undef MAX_SOURCE_CODE_BYTES
60
+
61
+ Tokenizer_Get_Struct(self, tokenizer);
62
+ // A frozen substring may still borrow storage from a movable parent.
63
+ long length = RSTRING_LEN(source);
64
+ VALUE owned_source = rb_enc_str_new(NULL, length, utf8_encoding);
65
+ memcpy(RSTRING_PTR(owned_source), RSTRING_PTR(source), (size_t)length);
66
+ tokenizer->source = rb_obj_freeze(owned_source);
67
+ tokenizer->cursor = RSTRING_PTR(owned_source);
68
+ tokenizer->cursor_end = tokenizer->cursor + length;
69
+ tokenizer->lstrip_flag = false;
70
+ // nil indicates that line numbers are not being calculated. Native API
71
+ // callers retain the historical zero-as-disabled convention.
72
+ if (!RB_INTEGER_TYPE_P(start_line_number)) rb_raise(rb_eTypeError, "line number must be an Integer");
73
+ tokenizer->line_number = start_line_number == INT2FIX(0) ? Qnil : start_line_number;
74
+ tokenizer->for_liquid_tag = RTEST(for_liquid_tag);
75
+ if (tokenizer->for_liquid_tag) {
76
+ while (tokenizer->cursor < tokenizer->cursor_end && tokenizer->cursor_end[-1] == '\n')
77
+ tokenizer->cursor_end--;
78
+ }
79
+ return Qnil;
80
+ }
81
+
82
+ // Internal function to setup an existing tokenizer from C for a liquid tag.
83
+ // This overwrites the passed in tokenizer, so a copy of the struct should
84
+ // be used to reset the tokenizer after parsing the liquid tag.
85
+ void tokenizer_setup_for_liquid_tag(tokenizer_t *tokenizer, const char *cursor, const char *cursor_end, VALUE line_number)
86
+ {
87
+ tokenizer->cursor = cursor;
88
+ tokenizer->cursor_end = cursor_end;
89
+ tokenizer->lstrip_flag = false;
90
+ tokenizer->line_number = line_number;
91
+ tokenizer->for_liquid_tag = true;
92
+ while (tokenizer->cursor < tokenizer->cursor_end && tokenizer->cursor_end[-1] == '\n')
93
+ tokenizer->cursor_end--;
94
+ }
95
+
96
+ // Tokenizes contents of {% liquid ... %}
97
+ static void tokenizer_next_for_liquid_tag(tokenizer_t *tokenizer, token_t *token)
98
+ {
99
+ const char *end = tokenizer->cursor_end;
100
+ const char *start = tokenizer->cursor;
101
+ const char *start_trimmed = read_while(start, end, is_non_newline_space);
102
+
103
+ token->str_full = start;
104
+ token->str_trimmed = start_trimmed;
105
+
106
+ const char *end_full = memchr(start_trimmed, '\n', (size_t)(end - start_trimmed));
107
+ if (!end_full) end_full = end;
108
+ if (end_full < end) {
109
+ tokenizer->cursor = end_full + 1;
110
+ } else {
111
+ tokenizer->cursor = end_full;
112
+ }
113
+ if (!NIL_P(tokenizer->line_number))
114
+ tokenizer->line_number = rb_funcall(tokenizer->line_number, rb_intern("+"), 1, INT2FIX(1));
115
+
116
+ const char *end_trimmed = read_while_reverse(start_trimmed, end_full, rb_isspace);
117
+
118
+ token->len_trimmed = end_trimmed - start_trimmed;
119
+ token->len_full = end_full - token->str_full;
120
+
121
+ if (token->len_trimmed == 0) {
122
+ token->type = TOKEN_BLANK_LIQUID_TAG_LINE;
123
+ } else {
124
+ token->type = TOKEN_TAG;
125
+ }
126
+ }
127
+
128
+ // Tokenizes contents of a full Liquid template
129
+ static void tokenizer_next_for_template(tokenizer_t *tokenizer, token_t *token)
130
+ {
131
+ const char *cursor = tokenizer->cursor;
132
+ const char *last = tokenizer->cursor_end - 1;
133
+ const char *next_cursor = NULL;
134
+ bool nested_tag = false;
135
+ const char *tag_search_start = NULL;
136
+
137
+ token->str_full = cursor;
138
+ token->type = TOKEN_RAW;
139
+
140
+ while (cursor < last) {
141
+ const char *opening = memchr(cursor, '{', (size_t)(last - cursor));
142
+ if (!opening) break;
143
+ cursor = opening + 1;
144
+
145
+ char c = *cursor++;
146
+ if (c != '%' && c != '{')
147
+ continue;
148
+ if (cursor <= last && *cursor == '-') {
149
+ cursor++;
150
+ token->rstrip = 1;
151
+ }
152
+ if (cursor - tokenizer->cursor > (ptrdiff_t)(2 + token->rstrip)) {
153
+ token->type = TOKEN_RAW;
154
+ cursor -= 2 + token->rstrip;
155
+ token->lstrip = tokenizer->lstrip_flag;
156
+ tokenizer->lstrip_flag = false;
157
+ goto found;
158
+ }
159
+ tokenizer->lstrip_flag = false;
160
+ token->type = TOKEN_INVALID;
161
+ token->lstrip = token->rstrip;
162
+ token->rstrip = 0;
163
+ if (c == '%') {
164
+ scan_tag:
165
+ tag_search_start = cursor;
166
+ while (cursor < last) {
167
+ if (*cursor++ != '%')
168
+ continue;
169
+ c = *cursor++;
170
+ while (c == '%' && cursor <= last)
171
+ c = *cursor++;
172
+ if (c != '}')
173
+ continue;
174
+ token->type = nested_tag ? TOKEN_INVALID : TOKEN_TAG;
175
+ if(cursor[-3] == '-')
176
+ token->rstrip = tokenizer->lstrip_flag = true;
177
+ goto found;
178
+ }
179
+ // unterminated tag
180
+ cursor = nested_tag ? tag_search_start : tokenizer->cursor + 2;
181
+ tokenizer->lstrip_flag = false;
182
+ goto found;
183
+ } else {
184
+ while (cursor < last) {
185
+ char current = *cursor++;
186
+ if (current == '{' && *cursor == '%') {
187
+ cursor++;
188
+ nested_tag = true;
189
+ goto scan_tag;
190
+ }
191
+ if (current != '}')
192
+ continue;
193
+ if (*cursor++ != '}') {
194
+ // variable incomplete end, used to end raw tags
195
+ cursor--;
196
+ goto found;
197
+ }
198
+ token->type = TOKEN_VARIABLE;
199
+ if(cursor[-3] == '-')
200
+ token->rstrip = tokenizer->lstrip_flag = true;
201
+ goto found;
202
+ }
203
+ if (cursor == last && *cursor == '}') {
204
+ cursor++;
205
+ goto found;
206
+ }
207
+ // Ruby consumes the searched suffix even when it returns only "{{".
208
+ next_cursor = tokenizer->cursor_end;
209
+ cursor = tokenizer->cursor + 2;
210
+ tokenizer->lstrip_flag = false;
211
+ goto found;
212
+ }
213
+ }
214
+ cursor = last + 1;
215
+ token->lstrip = tokenizer->lstrip_flag;
216
+ tokenizer->lstrip_flag = false;
217
+ found:
218
+ token->len_full = cursor - token->str_full;
219
+
220
+ token->str_trimmed = token->str_full;
221
+ token->len_trimmed = token->len_full;
222
+
223
+ if (token->type == TOKEN_VARIABLE || token->type == TOKEN_TAG) {
224
+ token->str_trimmed += 2 + token->lstrip;
225
+ token->len_trimmed -= 2 + token->lstrip + 2;
226
+ if (token->rstrip && token->len_trimmed)
227
+ token->len_trimmed--;
228
+ }
229
+
230
+ assert(token->len_trimmed >= 0);
231
+
232
+ tokenizer->cursor = next_cursor ? next_cursor : cursor;
233
+
234
+ if (!NIL_P(tokenizer->line_number)) {
235
+ int lines = count_newlines(token->str_full, token->str_full + token->len_full);
236
+ if (lines) tokenizer->line_number = rb_funcall(tokenizer->line_number, rb_intern("+"), 1, INT2NUM(lines));
237
+ }
238
+ }
239
+
240
+ void tokenizer_next(tokenizer_t *tokenizer, token_t *token)
241
+ {
242
+ memset(token, 0, sizeof(*token));
243
+
244
+ if (tokenizer->cursor >= tokenizer->cursor_end) {
245
+ return;
246
+ }
247
+ if (tokenizer->for_liquid_tag) {
248
+ tokenizer_next_for_liquid_tag(tokenizer, token);
249
+ } else {
250
+ tokenizer_next_for_template(tokenizer, token);
251
+ }
252
+ }
253
+
254
+ static VALUE tokenizer_shift_method(VALUE self)
255
+ {
256
+ tokenizer_t *tokenizer;
257
+ Tokenizer_Get_Struct(self, tokenizer);
258
+
259
+ token_t token;
260
+ tokenizer_next(tokenizer, &token);
261
+ if (!token.type)
262
+ return Qnil;
263
+
264
+ // When sent back to Ruby, tokens are the raw string including whitespace
265
+ // and tag delimiters. It should be possible to reconstruct the exact
266
+ // template from the tokens.
267
+ return rb_enc_str_new(token.str_full, token.len_full, utf8_encoding);
268
+ }
269
+
270
+ static VALUE tokenizer_shift_trimmed_method(VALUE self)
271
+ {
272
+ tokenizer_t *tokenizer;
273
+ Tokenizer_Get_Struct(self, tokenizer);
274
+
275
+ token_t token;
276
+ tokenizer_next(tokenizer, &token);
277
+ if (!token.type)
278
+ return Qnil;
279
+
280
+ // This method doesn't include whitespace and tag delimiters. It allows for
281
+ // testing the output of tokenizer_next as used by rb_block_parse.
282
+ return rb_enc_str_new(token.str_trimmed, token.len_trimmed, utf8_encoding);
283
+ }
284
+
285
+ static VALUE tokenizer_line_number_method(VALUE self)
286
+ {
287
+ tokenizer_t *tokenizer;
288
+ Tokenizer_Get_Struct(self, tokenizer);
289
+
290
+ return tokenizer->line_number;
291
+ }
292
+
293
+ static VALUE tokenizer_for_liquid_tag_method(VALUE self)
294
+ {
295
+ tokenizer_t *tokenizer;
296
+ Tokenizer_Get_Struct(self, tokenizer);
297
+
298
+ return tokenizer->for_liquid_tag ? Qtrue : Qfalse;
299
+ }
300
+
301
+
302
+ // Temporary to test rollout of the fix for this bug
303
+ static VALUE tokenizer_bug_compatible_whitespace_trimming(VALUE self) {
304
+ tokenizer_t *tokenizer;
305
+ Tokenizer_Get_Struct(self, tokenizer);
306
+
307
+ tokenizer->bug_compatible_whitespace_trimming = true;
308
+ return Qnil;
309
+ }
310
+
311
+ void liquid_define_tokenizer(void)
312
+ {
313
+ cLiquidTokenizer = rb_define_class_under(mLiquidC, "Tokenizer", rb_cObject);
314
+ rb_global_variable(&cLiquidTokenizer);
315
+
316
+ rb_define_alloc_func(cLiquidTokenizer, tokenizer_allocate);
317
+ rb_define_method(cLiquidTokenizer, "initialize", tokenizer_initialize_method, 3);
318
+ rb_define_method(cLiquidTokenizer, "line_number", tokenizer_line_number_method, 0);
319
+ rb_define_method(cLiquidTokenizer, "for_liquid_tag", tokenizer_for_liquid_tag_method, 0);
320
+ rb_define_method(cLiquidTokenizer, "bug_compatible_whitespace_trimming!", tokenizer_bug_compatible_whitespace_trimming, 0);
321
+ rb_define_method(cLiquidTokenizer, "shift", tokenizer_shift_method, 0);
322
+
323
+ // For testing the internal token representation.
324
+ rb_define_private_method(cLiquidTokenizer, "shift_trimmed", tokenizer_shift_trimmed_method, 0);
325
+ }
326
+
@@ -0,0 +1,46 @@
1
+ #if !defined(LIQUID_TOKENIZER_H)
2
+ #define LIQUID_TOKENIZER_H
3
+
4
+ enum token_type {
5
+ TOKENIZER_TOKEN_NONE = 0,
6
+ TOKEN_INVALID,
7
+ TOKEN_RAW,
8
+ TOKEN_TAG,
9
+ TOKEN_VARIABLE,
10
+ TOKEN_BLANK_LIQUID_TAG_LINE
11
+ };
12
+
13
+ typedef struct token {
14
+ enum token_type type;
15
+
16
+ // str_trimmed contains no tag delimiters
17
+ const char *str_trimmed, *str_full;
18
+ long len_trimmed, len_full;
19
+
20
+ bool lstrip, rstrip;
21
+ } token_t;
22
+
23
+ typedef struct tokenizer {
24
+ VALUE source;
25
+ const char *cursor, *cursor_end;
26
+ VALUE line_number; // nil when tracking is disabled, otherwise a Ruby Integer
27
+ bool lstrip_flag;
28
+ bool for_liquid_tag;
29
+
30
+ // Temporary to test rollout of the fix for this bug
31
+ bool bug_compatible_whitespace_trimming;
32
+
33
+ VALUE raw_tag_body;
34
+ } tokenizer_t;
35
+
36
+ extern VALUE cLiquidTokenizer;
37
+ extern const rb_data_type_t tokenizer_data_type;
38
+ #define Tokenizer_Get_Struct(obj, sval) TypedData_Get_Struct(obj, tokenizer_t, &tokenizer_data_type, sval)
39
+
40
+ void liquid_define_tokenizer(void);
41
+ void tokenizer_next(tokenizer_t *tokenizer, token_t *token);
42
+
43
+ void tokenizer_setup_for_liquid_tag(tokenizer_t *tokenizer, const char *cursor, const char *cursor_end, VALUE line_number);
44
+
45
+ #endif
46
+
@@ -0,0 +1,18 @@
1
+ #include "usage.h"
2
+
3
+ static VALUE cLiquidUsage;
4
+ static ID id_increment;
5
+
6
+ void usage_increment(const char *name)
7
+ {
8
+ VALUE name_str = rb_str_new_cstr(name);
9
+ rb_funcall(cLiquidUsage, id_increment, 1, name_str);
10
+ }
11
+
12
+ void liquid_define_usage(void)
13
+ {
14
+ cLiquidUsage = rb_const_get(mLiquid, rb_intern("Usage"));
15
+ rb_global_variable(&cLiquidUsage);
16
+
17
+ id_increment = rb_intern("increment");
18
+ }
@@ -0,0 +1,9 @@
1
+ #ifndef LIQUID_USAGE_H
2
+ #define LIQUID_USAGE_H
3
+
4
+ #include "liquid.h"
5
+
6
+ void liquid_define_usage(void);
7
+ void usage_increment(const char *name);
8
+
9
+ #endif
@@ -0,0 +1,198 @@
1
+ #include "liquid.h"
2
+ #include "variable.h"
3
+ #include "expression.h"
4
+ #include "liquid_vm.h"
5
+
6
+ VALUE cLiquidCVariableExpression;
7
+
8
+ static VALUE cVariablePlaceholder;
9
+ static ID id_markup, id_context, id_line, id_name, id_filters;
10
+ static rb_alloc_func_t variable_allocator;
11
+
12
+ typedef struct {
13
+ VALUE markup, context, line, name, filters, variable;
14
+ uint32_t length;
15
+ bool object_render;
16
+ char markup_bytes[];
17
+ } variable_placeholder_t;
18
+
19
+ static void placeholder_mark(void *opaque)
20
+ {
21
+ variable_placeholder_t *node = opaque;
22
+ rb_gc_mark_movable(node->markup);
23
+ rb_gc_mark_movable(node->context);
24
+ rb_gc_mark_movable(node->line);
25
+ rb_gc_mark_movable(node->name);
26
+ rb_gc_mark_movable(node->filters);
27
+ rb_gc_mark_movable(node->variable);
28
+ }
29
+
30
+ static void placeholder_compact(void *opaque)
31
+ {
32
+ variable_placeholder_t *node = opaque;
33
+ node->markup = rb_gc_location(node->markup);
34
+ node->context = rb_gc_location(node->context);
35
+ node->line = rb_gc_location(node->line);
36
+ node->name = rb_gc_location(node->name);
37
+ node->filters = rb_gc_location(node->filters);
38
+ node->variable = rb_gc_location(node->variable);
39
+ }
40
+
41
+ static size_t placeholder_size(const void *opaque)
42
+ {
43
+ const variable_placeholder_t *node = opaque;
44
+ return node ? sizeof(*node) + node->length : 0;
45
+ }
46
+
47
+ static const rb_data_type_t placeholder_type = {
48
+ "liquid_variable_placeholder",
49
+ { placeholder_mark, RUBY_DEFAULT_FREE, placeholder_size, placeholder_compact },
50
+ NULL, NULL, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
51
+ };
52
+
53
+ bool variable_placeholder_p(VALUE node)
54
+ {
55
+ return rb_typeddata_is_kind_of(node, &placeholder_type);
56
+ }
57
+
58
+ bool variable_placeholder_object_render_p(VALUE self)
59
+ {
60
+ variable_placeholder_t *node = RTYPEDDATA_DATA(self);
61
+ return node->object_render;
62
+ }
63
+
64
+ void variable_placeholder_use_object(VALUE self)
65
+ {
66
+ variable_placeholder_t *node = RTYPEDDATA_DATA(self);
67
+ node->object_render = true;
68
+ }
69
+
70
+ VALUE variable_placeholder_new(VALUE source, size_t offset, size_t length, VALUE context, VALUE line_number)
71
+ {
72
+ // Tokenizer spans are bounded by its source byte limit.
73
+ assert(length <= UINT32_MAX && length <= SIZE_MAX - sizeof(variable_placeholder_t));
74
+ vm_assembler_allocation_checkpoint();
75
+ VALUE self = rb_data_typed_object_zalloc(cVariablePlaceholder, sizeof(variable_placeholder_t) + length, &placeholder_type);
76
+ variable_placeholder_t *node = RTYPEDDATA_DATA(self);
77
+ node->markup = node->context = node->line = node->name = node->filters = node->variable = Qnil;
78
+ node->length = (uint32_t)length;
79
+ memcpy(node->markup_bytes, RSTRING_PTR(source) + offset, length);
80
+ RB_OBJ_WRITE(self, &node->context, context);
81
+ RB_OBJ_WRITE(self, &node->line, line_number);
82
+ RB_GC_GUARD(source);
83
+ return self;
84
+ }
85
+
86
+ void variable_placeholder_set(VALUE self, VALUE name, VALUE filters)
87
+ {
88
+ variable_placeholder_t *node = RTYPEDDATA_DATA(self);
89
+ RB_OBJ_WRITE(self, &node->name, name);
90
+ RB_OBJ_WRITE(self, &node->filters, filters);
91
+ }
92
+
93
+ VALUE variable_from_parse(VALUE markup, VALUE context, VALUE line, VALUE name, VALUE filters)
94
+ {
95
+ VALUE variable = variable_allocator(cLiquidVariable);
96
+ rb_ivar_set(variable, id_markup, markup);
97
+ rb_ivar_set(variable, id_context, context);
98
+ rb_ivar_set(variable, id_line, line);
99
+ rb_ivar_set(variable, id_name, name);
100
+ rb_ivar_set(variable, id_filters, filters);
101
+ RB_GC_GUARD(variable);
102
+ return variable;
103
+ }
104
+
105
+ VALUE variable_materialize(VALUE self)
106
+ {
107
+ if (!variable_placeholder_p(self)) return self;
108
+ variable_placeholder_t *node = RTYPEDDATA_DATA(self);
109
+ if (!NIL_P(node->variable)) return node->variable;
110
+ if (NIL_P(node->markup)) {
111
+ VALUE markup = rb_enc_str_new(node->markup_bytes, node->length, utf8_encoding);
112
+ if (NIL_P(node->markup)) RB_OBJ_WRITE(self, &node->markup, markup);
113
+ }
114
+ // This is the state of the original parse, not a parse using today's hooks.
115
+ VALUE variable = variable_allocator(cLiquidVariable);
116
+ if (!NIL_P(node->variable)) return node->variable;
117
+ rb_ivar_set(variable, id_markup, node->markup);
118
+ rb_ivar_set(variable, id_context, node->context);
119
+ rb_ivar_set(variable, id_line, node->line);
120
+ rb_ivar_set(variable, id_name, node->name);
121
+ // Compact records retain parse-time expression identities. Only their
122
+ // mutable Array containers are delayed until the AST becomes observable.
123
+ VALUE filters = rb_ary_new();
124
+ if (!NIL_P(node->filters)) {
125
+ for (long i = 0; i < RARRAY_LEN(node->filters);) {
126
+ VALUE name = rb_str_dup(rb_sym2str(RARRAY_AREF(node->filters, i++)));
127
+ rb_enc_associate(name, utf8_encoding);
128
+ long count = NUM2LONG(RARRAY_AREF(node->filters, i++));
129
+ VALUE kwargs = RARRAY_AREF(node->filters, i++);
130
+ VALUE args = rb_ary_new_capa(count);
131
+ for (long j = 0; j < count; j++) rb_ary_push(args, RARRAY_AREF(node->filters, i++));
132
+ VALUE filter = rb_ary_new_from_args(2, name, args);
133
+ if (!NIL_P(kwargs)) rb_ary_push(filter, kwargs);
134
+ rb_ary_push(filters, filter);
135
+ }
136
+ }
137
+ rb_ivar_set(variable, id_filters, filters);
138
+ if (NIL_P(node->variable)) RB_OBJ_WRITE(self, &node->variable, variable);
139
+ else variable = node->variable;
140
+ RB_GC_GUARD(variable);
141
+ RB_GC_GUARD(self);
142
+ return variable;
143
+ }
144
+
145
+ void internal_variable_compile(variable_parse_args_t *parse_args, VALUE line_number)
146
+ {
147
+ vm_assembler_t *code = parse_args->code;
148
+ if (native_compile_variable(parse_args, line_number)) return;
149
+ VALUE markup = rb_enc_str_new(parse_args->markup, parse_args->markup_end - parse_args->markup, utf8_encoding);
150
+ if (parse_args->guard) parse_args->guard->valid = false;
151
+ VALUE variable_class = rb_const_get(mLiquid, rb_intern("Variable"));
152
+ if (variable_class != cLiquidVariable) {
153
+ VALUE variable = rb_funcall(variable_class, rb_intern("new"), 2, markup, parse_args->parse_context);
154
+ vm_assembler_add_write_node(code, variable);
155
+ return;
156
+ }
157
+ VALUE variable = rb_funcall(cLiquidVariable, rb_intern("c_parse"), 2, markup, parse_args->parse_context);
158
+ if (!RTEST(rb_funcall(variable, rb_intern("c_compilable?"), 0))) {
159
+ vm_assembler_add_write_node(code, variable);
160
+ return;
161
+ }
162
+ vm_assembler_add_render_variable_rescue(code, line_number, variable);
163
+
164
+ code->protected_stack_size = code->stack_size;
165
+ rb_funcall(variable, id_compile_evaluate, 1, parse_args->code_obj);
166
+ if (code->stack_size != code->protected_stack_size + 1) {
167
+ rb_raise(rb_eRuntimeError, "Liquid::Variable#compile_evaluate didn't leave exactly 1 new element on the stack");
168
+ }
169
+ vm_assembler_add_pop_write(code);
170
+ }
171
+
172
+ VALUE internal_variable_expression_evaluate(expression_t *expression, VALUE context)
173
+ {
174
+ return liquid_vm_evaluate(context, &expression->code);
175
+ }
176
+
177
+ static VALUE variable_expression_evaluate_method(VALUE self, VALUE context)
178
+ {
179
+ expression_t *expression;
180
+ Expression_Get_Struct(self, expression);
181
+ return internal_variable_expression_evaluate(expression, context);
182
+ }
183
+
184
+ void liquid_define_variable(void)
185
+ {
186
+ id_markup = rb_intern("@markup");
187
+ id_context = rb_intern("@parse_context");
188
+ id_line = rb_intern("@line_number");
189
+ id_name = rb_intern("@name");
190
+ id_filters = rb_intern("@filters");
191
+ variable_allocator = rb_get_alloc_func(rb_cObject);
192
+ cVariablePlaceholder = rb_define_class_under(mLiquidC, "VariablePlaceholder", rb_cObject);
193
+ rb_global_variable(&cVariablePlaceholder);
194
+ rb_undef_alloc_func(cVariablePlaceholder);
195
+ cLiquidCVariableExpression = rb_define_class_under(mLiquidC, "VariableExpression", cLiquidCExpression);
196
+ rb_global_variable(&cLiquidCVariableExpression);
197
+ rb_define_method(cLiquidCVariableExpression, "evaluate", variable_expression_evaluate_method, 1);
198
+ }
@@ -0,0 +1,61 @@
1
+ #if !defined(LIQUID_VARIABLE_H)
2
+ #define LIQUID_VARIABLE_H
3
+
4
+ #include "vm_assembler.h"
5
+ #include "block.h"
6
+ #include "expression.h"
7
+
8
+ extern VALUE cLiquidCVariableExpression;
9
+
10
+ // Valid only inside a C parsing loop, until Ruby code or GC can run.
11
+ typedef struct {
12
+ bool valid, control_validated, tags_enabled, control_checked, control_enabled, literals_checked;
13
+ size_t gc_count;
14
+ VALUE cache;
15
+ VALUE environment, tags, condition_literals;
16
+ } variable_compile_guard_t;
17
+
18
+ typedef struct variable_parse_args {
19
+ VALUE source;
20
+ const char *markup;
21
+ const char *markup_end;
22
+ vm_assembler_t *code;
23
+ VALUE code_obj;
24
+ VALUE parse_context;
25
+ variable_compile_guard_t *guard;
26
+ } variable_parse_args_t;
27
+
28
+ void liquid_define_variable(void);
29
+ void internal_variable_compile(variable_parse_args_t *parse_args, VALUE line_number);
30
+ VALUE internal_variable_expression_evaluate(expression_t *expression, VALUE context);
31
+ bool native_compile_variable(variable_parse_args_t *args, VALUE line_number);
32
+ VALUE variable_placeholder_new(VALUE source, size_t offset, size_t length, VALUE context, VALUE line_number);
33
+ void variable_placeholder_set(VALUE self, VALUE name, VALUE filters);
34
+ VALUE variable_materialize(VALUE node);
35
+ VALUE variable_from_parse(VALUE markup, VALUE context, VALUE line, VALUE name, VALUE filters);
36
+ bool native_include_class_p(VALUE klass);
37
+ VALUE native_parse_include(variable_parse_args_t *args, VALUE klass, VALUE name, VALUE markup);
38
+ VALUE native_parse_capture(variable_parse_args_t *args, VALUE klass, VALUE name, VALUE markup);
39
+ bool native_assignment_class_p(VALUE klass);
40
+ VALUE native_parse_assignment(variable_parse_args_t *args, VALUE klass, VALUE name, VALUE markup);
41
+ bool variable_placeholder_p(VALUE node);
42
+ bool variable_placeholder_object_render_p(VALUE node);
43
+ void variable_placeholder_use_object(VALUE node);
44
+ bool variable_render_methods_standard(void);
45
+ bool liquid_vm_render_methods_standard(void);
46
+ bool variable_render_traced(void);
47
+ bool variable_output_method_standard(void);
48
+ bool native_compiler_guard(variable_parse_args_t *args);
49
+ bool native_control_guard(variable_parse_args_t *args);
50
+ VALUE native_control_header(variable_parse_args_t *args, VALUE markup, int kind, bool commit);
51
+ void native_control_commit(variable_parse_args_t *args, VALUE header);
52
+ void native_control_for_fields(VALUE tag, VALUE payload);
53
+ void native_control_attach(variable_parse_args_t *args, VALUE condition, VALUE body);
54
+ VALUE native_registered_tag(variable_parse_args_t *args, VALUE environment, VALUE name);
55
+ int native_control_kind(VALUE klass);
56
+ VALUE native_control_else(variable_parse_args_t *args);
57
+ void native_control_finish(variable_parse_args_t *args, VALUE tag, int kind);
58
+ bool native_control_condition(VALUE node);
59
+
60
+ #endif
61
+