liquid-c 4.0.1 → 4.2.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 (71) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/cla.yml +23 -0
  3. data/.github/workflows/liquid.yml +36 -11
  4. data/.gitignore +4 -0
  5. data/.rubocop.yml +14 -0
  6. data/Gemfile +15 -5
  7. data/README.md +32 -8
  8. data/Rakefile +12 -63
  9. data/ext/liquid_c/block.c +493 -60
  10. data/ext/liquid_c/block.h +28 -2
  11. data/ext/liquid_c/c_buffer.c +42 -0
  12. data/ext/liquid_c/c_buffer.h +76 -0
  13. data/ext/liquid_c/context.c +233 -0
  14. data/ext/liquid_c/context.h +70 -0
  15. data/ext/liquid_c/document_body.c +97 -0
  16. data/ext/liquid_c/document_body.h +59 -0
  17. data/ext/liquid_c/expression.c +116 -0
  18. data/ext/liquid_c/expression.h +24 -0
  19. data/ext/liquid_c/extconf.rb +21 -9
  20. data/ext/liquid_c/intutil.h +22 -0
  21. data/ext/liquid_c/lexer.c +39 -3
  22. data/ext/liquid_c/lexer.h +18 -3
  23. data/ext/liquid_c/liquid.c +76 -6
  24. data/ext/liquid_c/liquid.h +24 -1
  25. data/ext/liquid_c/liquid_vm.c +618 -0
  26. data/ext/liquid_c/liquid_vm.h +25 -0
  27. data/ext/liquid_c/parse_context.c +76 -0
  28. data/ext/liquid_c/parse_context.h +13 -0
  29. data/ext/liquid_c/parser.c +153 -65
  30. data/ext/liquid_c/parser.h +4 -2
  31. data/ext/liquid_c/raw.c +136 -0
  32. data/ext/liquid_c/raw.h +6 -0
  33. data/ext/liquid_c/resource_limits.c +279 -0
  34. data/ext/liquid_c/resource_limits.h +23 -0
  35. data/ext/liquid_c/stringutil.h +44 -0
  36. data/ext/liquid_c/tokenizer.c +149 -35
  37. data/ext/liquid_c/tokenizer.h +20 -9
  38. data/ext/liquid_c/usage.c +18 -0
  39. data/ext/liquid_c/usage.h +9 -0
  40. data/ext/liquid_c/variable.c +196 -20
  41. data/ext/liquid_c/variable.h +18 -1
  42. data/ext/liquid_c/variable_lookup.c +44 -0
  43. data/ext/liquid_c/variable_lookup.h +8 -0
  44. data/ext/liquid_c/vm_assembler.c +491 -0
  45. data/ext/liquid_c/vm_assembler.h +240 -0
  46. data/ext/liquid_c/vm_assembler_pool.c +99 -0
  47. data/ext/liquid_c/vm_assembler_pool.h +26 -0
  48. data/lib/liquid/c/compile_ext.rb +44 -0
  49. data/lib/liquid/c/version.rb +3 -1
  50. data/lib/liquid/c.rb +226 -48
  51. data/liquid-c.gemspec +16 -10
  52. data/performance/c_profile.rb +23 -0
  53. data/performance.rb +6 -4
  54. data/rakelib/compile.rake +15 -0
  55. data/rakelib/integration_test.rake +43 -0
  56. data/rakelib/performance.rake +43 -0
  57. data/rakelib/rubocop.rake +6 -0
  58. data/rakelib/unit_test.rake +14 -0
  59. data/test/integration_test.rb +11 -0
  60. data/test/liquid_test_helper.rb +21 -0
  61. data/test/test_helper.rb +21 -2
  62. data/test/unit/block_test.rb +137 -0
  63. data/test/unit/context_test.rb +85 -0
  64. data/test/unit/expression_test.rb +191 -0
  65. data/test/unit/gc_stress_test.rb +28 -0
  66. data/test/unit/raw_test.rb +93 -0
  67. data/test/unit/resource_limits_test.rb +50 -0
  68. data/test/unit/tokenizer_test.rb +90 -20
  69. data/test/unit/variable_test.rb +279 -60
  70. metadata +60 -11
  71. data/test/liquid_test.rb +0 -11
@@ -0,0 +1,618 @@
1
+ #include <stdint.h>
2
+ #include <assert.h>
3
+
4
+ #include "liquid.h"
5
+ #include "liquid_vm.h"
6
+ #include "variable_lookup.h"
7
+ #include "intutil.h"
8
+ #include "document_body.h"
9
+
10
+ ID id_render_node;
11
+ ID id_vm;
12
+
13
+ static VALUE cLiquidCVM;
14
+
15
+ static void vm_mark(void *ptr)
16
+ {
17
+ vm_t *vm = ptr;
18
+
19
+ c_buffer_rb_gc_mark(&vm->stack);
20
+ context_mark(&vm->context);
21
+ }
22
+
23
+ static void vm_free(void *ptr)
24
+ {
25
+ vm_t *vm = ptr;
26
+ c_buffer_free(&vm->stack);
27
+ xfree(vm);
28
+ }
29
+
30
+ static size_t vm_memsize(const void *ptr)
31
+ {
32
+ const vm_t *vm = ptr;
33
+ return sizeof(vm_t) + c_buffer_capacity(&vm->stack);
34
+ }
35
+
36
+ const rb_data_type_t vm_data_type = {
37
+ "liquid_vm",
38
+ { vm_mark, vm_free, vm_memsize, },
39
+ NULL, NULL, RUBY_TYPED_FREE_IMMEDIATELY
40
+ };
41
+
42
+ static VALUE vm_internal_new(VALUE context)
43
+ {
44
+ vm_t *vm;
45
+ VALUE obj = TypedData_Make_Struct(cLiquidCVM, vm_t, &vm_data_type, vm);
46
+ vm->stack = c_buffer_init();
47
+
48
+ vm->invoking_filter = false;
49
+
50
+ context_internal_init(context, &vm->context);
51
+
52
+ return obj;
53
+ }
54
+
55
+ vm_t *vm_from_context(VALUE context)
56
+ {
57
+ VALUE vm_obj = rb_attr_get(context, id_vm);
58
+ if (vm_obj == Qnil) {
59
+ vm_obj = vm_internal_new(context);
60
+ rb_ivar_set(context, id_vm, vm_obj);
61
+ }
62
+ // instance variable is hidden from ruby so should be safe to unwrap it without type checking
63
+ return DATA_PTR(vm_obj);
64
+ }
65
+
66
+ bool liquid_vm_filtering(VALUE context)
67
+ {
68
+ VALUE vm_obj = rb_attr_get(context, id_vm);
69
+ if (vm_obj == Qnil)
70
+ return false;
71
+ vm_t *vm = DATA_PTR(vm_obj);
72
+ return vm->invoking_filter;
73
+ }
74
+
75
+ static void write_fixnum(VALUE output, VALUE fixnum)
76
+ {
77
+ long long number = RB_NUM2LL(fixnum);
78
+ int write_length = snprintf(NULL, 0, "%lld", number);
79
+ long old_size = RSTRING_LEN(output);
80
+ long new_size = old_size + write_length;
81
+ long capacity = rb_str_capacity(output);
82
+
83
+ if (new_size > capacity) {
84
+ do {
85
+ capacity *= 2;
86
+ } while (new_size > capacity);
87
+ rb_str_resize(output, capacity);
88
+ }
89
+ rb_str_set_len(output, new_size);
90
+
91
+ snprintf(RSTRING_PTR(output) + old_size, write_length + 1, "%lld", number);
92
+ }
93
+
94
+ static VALUE obj_to_s(VALUE obj)
95
+ {
96
+ VALUE str = rb_funcall(obj, id_to_s, 0);
97
+
98
+ if (RB_LIKELY(RB_TYPE_P(str, T_STRING)))
99
+ return str;
100
+
101
+ rb_raise(rb_eTypeError, "%"PRIsVALUE"#to_s returned a non-String convertible value of type %"PRIsVALUE,
102
+ rb_obj_class(obj), rb_obj_class(str));
103
+ }
104
+
105
+ static void write_obj(VALUE output, VALUE obj)
106
+ {
107
+ switch (TYPE(obj)) {
108
+ default:
109
+ obj = obj_to_s(obj);
110
+ // fallthrough
111
+ case T_STRING:
112
+ rb_str_buf_append(output, obj);
113
+ break;
114
+ case T_FIXNUM:
115
+ write_fixnum(output, obj);
116
+ break;
117
+ case T_ARRAY:
118
+ for (long i = 0; i < RARRAY_LEN(obj); i++)
119
+ {
120
+ VALUE item = RARRAY_AREF(obj, i);
121
+
122
+ if (RB_UNLIKELY(RB_TYPE_P(item, T_ARRAY))) {
123
+ // Normally liquid arrays are flat, but for safety and simplicity we
124
+ // leverage ruby's join that detects and raises on a recursion loop
125
+ rb_str_buf_append(output, rb_ary_join(item, Qnil));
126
+ } else {
127
+ write_obj(output, item);
128
+ }
129
+ }
130
+ break;
131
+ case T_NIL:
132
+ break;
133
+ }
134
+ }
135
+
136
+ static inline void vm_stack_push(vm_t *vm, VALUE value)
137
+ {
138
+ VALUE *stack_ptr = (VALUE *)vm->stack.data_end;
139
+ assert(stack_ptr < (VALUE *)vm->stack.capacity_end);
140
+ *stack_ptr++ = value;
141
+ vm->stack.data_end = (uint8_t *)stack_ptr;
142
+ }
143
+
144
+ static inline VALUE *vm_stack_peek_n(vm_t *vm, size_t n)
145
+ {
146
+ VALUE *stack_ptr = (VALUE *)vm->stack.data_end;
147
+ stack_ptr -= n;
148
+ assert((VALUE *)vm->stack.data <= stack_ptr);
149
+ return stack_ptr;
150
+ }
151
+
152
+ static inline VALUE *vm_stack_pop_n(vm_t *vm, size_t n)
153
+ {
154
+ VALUE *stack_ptr = vm_stack_peek_n(vm, n);
155
+ vm->stack.data_end = (uint8_t *)stack_ptr;
156
+ return stack_ptr;
157
+ }
158
+
159
+ static inline VALUE vm_stack_pop(vm_t *vm)
160
+ {
161
+ return *vm_stack_pop_n(vm, 1);
162
+ }
163
+
164
+ static inline void vm_stack_reserve_for_write(vm_t *vm, size_t num_values)
165
+ {
166
+ c_buffer_reserve_for_write(&vm->stack, num_values * sizeof(VALUE));
167
+ }
168
+
169
+ static VALUE vm_invoke_filter(vm_t *vm, VALUE filter_name, int num_args)
170
+ {
171
+ VALUE *popped_args = vm_stack_pop_n(vm, num_args);
172
+ /* We have to copy popped_args_ptr to the stack because the VM
173
+ * no longer holds onto these objects, so they have to exist on
174
+ * the stack to ensure they don't get garbage collected. */
175
+ VALUE *args = alloca(sizeof(VALUE *) * num_args);
176
+ memcpy(args, popped_args, sizeof(VALUE *) * num_args);
177
+
178
+ bool not_invokable = rb_hash_lookup(vm->context.filter_methods, filter_name) != Qtrue;
179
+ if (RB_UNLIKELY(not_invokable)) {
180
+ if (vm->context.strict_filters) {
181
+ VALUE error_class = rb_const_get(mLiquid, rb_intern("UndefinedFilter"));
182
+ rb_raise(error_class, "undefined filter %"PRIsVALUE, rb_sym2str(filter_name));
183
+ }
184
+ return args[0];
185
+ }
186
+
187
+ vm->invoking_filter = true;
188
+ VALUE result = rb_funcallv(vm->context.strainer, RB_SYM2ID(filter_name), num_args, args);
189
+ vm->invoking_filter = false;
190
+ return rb_funcall(result, id_to_liquid, 0);
191
+ }
192
+
193
+ typedef struct vm_render_until_error_args {
194
+ vm_t *vm;
195
+ const uint8_t *ip; // use for initial address and to save an address for rescuing
196
+ const size_t *const_ptr;
197
+
198
+ /* rendering fields */
199
+ VALUE output;
200
+ const uint8_t *node_line_number;
201
+ } vm_render_until_error_args_t;
202
+
203
+ static VALUE raise_invalid_integer(VALUE unused_arg, VALUE exc)
204
+ {
205
+ rb_raise(cLiquidArgumentError, "invalid integer");
206
+ }
207
+
208
+ // Equivalent to Integer(string) if string is an instance of String
209
+ static VALUE try_string_to_integer(VALUE string)
210
+ {
211
+ return rb_str_to_inum(string, 0, true);
212
+ }
213
+
214
+ static VALUE range_value_to_integer(VALUE value)
215
+ {
216
+ if (RB_INTEGER_TYPE_P(value)) {
217
+ return value;
218
+ } else if (value == Qnil) {
219
+ return INT2FIX(0);
220
+ } else if (RB_TYPE_P(value, T_STRING)) {
221
+ return rb_str_to_inum(value, 0, false); // equivalent to String#to_i
222
+ } else {
223
+ value = obj_to_s(value);
224
+ return rb_rescue2(try_string_to_integer, value, raise_invalid_integer, Qnil, rb_eArgError, (VALUE)0);
225
+ }
226
+ }
227
+
228
+ #ifdef HAVE_RB_HASH_BULK_INSERT
229
+ #define hash_bulk_insert rb_hash_bulk_insert
230
+ #else
231
+ static void hash_bulk_insert(long argc, const VALUE *argv, VALUE hash)
232
+ {
233
+ for (long i = 0; i < argc; i += 2) {
234
+ rb_hash_aset(hash, argv[i], argv[i + 1]);
235
+ }
236
+ }
237
+ #endif
238
+
239
+ // Actually returns a bool resume_rendering value
240
+ static VALUE vm_render_until_error(VALUE uncast_args)
241
+ {
242
+ vm_render_until_error_args_t *args = (void *)uncast_args;
243
+ const VALUE *constants = args->const_ptr;
244
+ const uint8_t *ip = args->ip;
245
+ vm_t *vm = args->vm;
246
+ VALUE output = args->output;
247
+ uint16_t constant_index;
248
+ VALUE constant = Qnil;
249
+ args->ip = NULL; // used by vm_render_rescue, NULL to indicate that it isn't in a rescue block
250
+
251
+ while (true) {
252
+ switch (*ip++) {
253
+ case OP_LEAVE:
254
+ return false;
255
+ case OP_PUSH_NIL:
256
+ vm_stack_push(vm, Qnil);
257
+ break;
258
+ case OP_PUSH_TRUE:
259
+ vm_stack_push(vm, Qtrue);
260
+ break;
261
+ case OP_PUSH_FALSE:
262
+ vm_stack_push(vm, Qfalse);
263
+ break;
264
+ case OP_PUSH_INT8:
265
+ {
266
+ int num = *(int8_t *)ip++; // signed
267
+ vm_stack_push(vm, RB_INT2FIX(num));
268
+ break;
269
+ }
270
+ case OP_PUSH_INT16:
271
+ {
272
+ int num = *(int8_t *)ip++; // big endian encoding, so first byte has sign
273
+ num = (num << 8) | *ip++;
274
+ vm_stack_push(vm, RB_INT2FIX(num));
275
+ break;
276
+ }
277
+ case OP_FIND_STATIC_VAR:
278
+ {
279
+ constant_index = (ip[0] << 8) | ip[1];
280
+ constant = constants[constant_index];
281
+ ip += 2;
282
+ VALUE value = context_find_variable(&vm->context, constant, Qtrue);
283
+ vm_stack_push(vm, value);
284
+ break;
285
+ }
286
+ case OP_FIND_VAR:
287
+ {
288
+ VALUE key = vm_stack_pop(vm);
289
+ VALUE value = context_find_variable(&vm->context, key, Qtrue);
290
+ vm_stack_push(vm, value);
291
+ break;
292
+ }
293
+ case OP_LOOKUP_CONST_KEY:
294
+ case OP_LOOKUP_COMMAND:
295
+ {
296
+ constant_index = (ip[0] << 8) | ip[1];
297
+ constant = constants[constant_index];
298
+ ip += 2;
299
+ vm_stack_push(vm, constant);
300
+ }
301
+ /* fallthrough */
302
+ case OP_LOOKUP_KEY:
303
+ {
304
+ bool is_command = ip[-3] == OP_LOOKUP_COMMAND;
305
+ VALUE key = vm_stack_pop(vm);
306
+ VALUE object = vm_stack_pop(vm);
307
+ VALUE result = variable_lookup_key(vm->context.self, object, key, is_command);
308
+ vm_stack_push(vm, result);
309
+ break;
310
+ }
311
+
312
+ case OP_NEW_INT_RANGE:
313
+ {
314
+ VALUE end = range_value_to_integer(vm_stack_pop(vm));
315
+ VALUE begin = range_value_to_integer(vm_stack_pop(vm));
316
+ bool exclude_end = false;
317
+ vm_stack_push(vm, rb_range_new(begin, end, exclude_end));
318
+ break;
319
+ }
320
+ case OP_HASH_NEW:
321
+ {
322
+ size_t hash_size = *ip++;
323
+ size_t num_keys_and_values = hash_size * 2;
324
+ VALUE hash = rb_hash_new();
325
+
326
+ VALUE *args_ptr = vm_stack_peek_n(vm, num_keys_and_values);
327
+ hash_bulk_insert(num_keys_and_values, args_ptr, hash);
328
+ vm_stack_pop_n(vm, num_keys_and_values);
329
+
330
+ vm_stack_push(vm, hash);
331
+ break;
332
+ }
333
+ case OP_FILTER:
334
+ case OP_BUILTIN_FILTER:
335
+ {
336
+ VALUE filter_name;
337
+ uint8_t num_args;
338
+
339
+ if (ip[-1] == OP_FILTER) {
340
+ constant_index = (ip[0] << 8) | ip[1];
341
+ constant = constants[constant_index];
342
+ filter_name = RARRAY_AREF(constant, 0);
343
+ num_args = RARRAY_AREF(constant, 1);
344
+ ip += 2;
345
+ } else {
346
+ assert(ip[-1] == OP_BUILTIN_FILTER);
347
+ filter_name = builtin_filters[*ip++].sym;
348
+ num_args = *ip++; // includes input argument
349
+ }
350
+
351
+ VALUE result = vm_invoke_filter(vm, filter_name, num_args);
352
+ vm_stack_push(vm, result);
353
+ break;
354
+ }
355
+
356
+ // Rendering instructions
357
+
358
+ case OP_WRITE_RAW_W:
359
+ case OP_WRITE_RAW:
360
+ {
361
+ const char *text;
362
+ size_t size;
363
+ if (ip[-1] == OP_WRITE_RAW_W) {
364
+ size = bytes_to_uint24(ip);
365
+ text = (const char *)&ip[3];
366
+ ip += 3 + size;
367
+ } else {
368
+ size = *ip;
369
+ text = (const char *)&ip[1];
370
+ ip += 1 + size;
371
+ }
372
+ rb_str_cat(output, text, size);
373
+ resource_limits_increment_write_score(vm->context.resource_limits, output);
374
+ break;
375
+ }
376
+ case OP_JUMP_FWD_W:
377
+ {
378
+ size_t size = bytes_to_uint24(ip);
379
+ ip += 3 + size;
380
+ break;
381
+ }
382
+
383
+ case OP_JUMP_FWD:
384
+ {
385
+ uint8_t size = *ip;
386
+ ip += 1 + size;
387
+ break;
388
+ }
389
+
390
+ case OP_PUSH_CONST:
391
+ {
392
+ constant_index = (ip[0] << 8) | ip[1];
393
+ constant = constants[constant_index];
394
+ ip += 2;
395
+ vm_stack_push(vm, constant);
396
+ break;
397
+ }
398
+
399
+ case OP_WRITE_NODE:
400
+ {
401
+ constant_index = (ip[0] << 8) | ip[1];
402
+ constant = constants[constant_index];
403
+ ip += 2;
404
+ rb_funcall(cLiquidBlockBody, id_render_node, 3, vm->context.self, output, constant);
405
+
406
+ if (RARRAY_LEN(vm->context.interrupts)) {
407
+ return false;
408
+ }
409
+
410
+ resource_limits_increment_write_score(vm->context.resource_limits, output);
411
+ break;
412
+ }
413
+ case OP_RENDER_VARIABLE_RESCUE:
414
+ // Save state used by vm_render_rescue to rescue from a variable rendering exception
415
+ args->node_line_number = ip;
416
+ // vm_render_rescue will iterate from this instruction to the instruction
417
+ // following OP_POP_WRITE_VARIABLE to resume rendering from
418
+ ip += 3;
419
+ args->ip = ip;
420
+ break;
421
+ case OP_POP_WRITE:
422
+ {
423
+ VALUE var_result = vm_stack_pop(vm);
424
+ if (vm->context.global_filter != Qnil)
425
+ var_result = rb_funcall(vm->context.global_filter, id_call, 1, var_result);
426
+ write_obj(output, var_result);
427
+ args->ip = NULL; // mark the end of a rescue block, used by vm_render_rescue
428
+ resource_limits_increment_write_score(vm->context.resource_limits, output);
429
+ break;
430
+ }
431
+
432
+ default:
433
+ rb_bug("invalid opcode: %u", ip[-1]);
434
+ }
435
+ }
436
+ }
437
+
438
+ typedef struct vm_evaluate_rescue_args {
439
+ vm_render_until_error_args_t *render_args;
440
+ size_t old_stack_byte_size;
441
+ } vm_evaluate_rescue_args_t;
442
+
443
+ static VALUE vm_evaluate_rescue(VALUE uncast_args, VALUE exception)
444
+ {
445
+ vm_evaluate_rescue_args_t *args = (void *)uncast_args;
446
+ vm_render_until_error_args_t *render_args = args->render_args;
447
+ vm_t *vm = render_args->vm;
448
+
449
+ vm->stack.data_end = vm->stack.data + args->old_stack_byte_size;
450
+
451
+ rb_exc_raise(exception);
452
+ return Qnil;
453
+ }
454
+
455
+ // Evaluate instructions that avoid using rendering instructions and leave with the result on
456
+ // the top of the stack
457
+ VALUE liquid_vm_evaluate(VALUE context, vm_assembler_t *code)
458
+ {
459
+ vm_t *vm = vm_from_context(context);
460
+ vm_stack_reserve_for_write(vm, code->max_stack_size);
461
+
462
+ vm_render_until_error_args_t args = {
463
+ .vm = vm,
464
+ .const_ptr = (const size_t *)code->constants.data,
465
+ .ip = code->instructions.data
466
+ };
467
+ vm_evaluate_rescue_args_t rescue_args = {
468
+ .render_args = &args,
469
+ .old_stack_byte_size = c_buffer_size(&vm->stack),
470
+ };
471
+ rb_rescue(vm_render_until_error, (VALUE)&args, vm_evaluate_rescue, (VALUE)&rescue_args);
472
+
473
+ VALUE ret = vm_stack_pop(vm);
474
+ assert(rescue_args.old_stack_byte_size == c_buffer_size(&vm->stack));
475
+ return ret;
476
+ }
477
+
478
+ void liquid_vm_next_instruction(const uint8_t **ip_ptr)
479
+ {
480
+ const uint8_t *ip = *ip_ptr;
481
+
482
+ switch (*ip++) {
483
+ case OP_LEAVE:
484
+ case OP_POP_WRITE:
485
+ case OP_PUSH_NIL:
486
+ case OP_PUSH_TRUE:
487
+ case OP_PUSH_FALSE:
488
+ case OP_FIND_VAR:
489
+ case OP_LOOKUP_KEY:
490
+ case OP_NEW_INT_RANGE:
491
+ break;
492
+
493
+ case OP_HASH_NEW:
494
+ case OP_PUSH_INT8:
495
+ ip++;
496
+ break;
497
+
498
+ case OP_BUILTIN_FILTER:
499
+ case OP_PUSH_INT16:
500
+ case OP_PUSH_CONST:
501
+ case OP_WRITE_NODE:
502
+ case OP_FIND_STATIC_VAR:
503
+ case OP_LOOKUP_CONST_KEY:
504
+ case OP_LOOKUP_COMMAND:
505
+ case OP_FILTER:
506
+ ip += 2;
507
+ break;
508
+
509
+ case OP_RENDER_VARIABLE_RESCUE:
510
+ ip += 3;
511
+ break;
512
+
513
+ case OP_WRITE_RAW_W:
514
+ case OP_JUMP_FWD_W:
515
+ {
516
+ size_t size = bytes_to_uint24(ip);
517
+ ip += 3 + size;
518
+ break;
519
+ }
520
+
521
+ case OP_WRITE_RAW:
522
+ case OP_JUMP_FWD:
523
+ {
524
+ uint8_t size = *ip;
525
+ ip += 1 + size;
526
+ break;
527
+ }
528
+
529
+ default:
530
+ rb_bug("invalid opcode: %u", ip[-1]);
531
+ }
532
+ *ip_ptr = ip;
533
+ }
534
+
535
+ VALUE vm_translate_if_filter_argument_error(vm_t *vm, VALUE exception)
536
+ {
537
+ if (vm->invoking_filter) {
538
+ if (rb_obj_is_kind_of(exception, rb_eArgError)) {
539
+ VALUE cLiquidStrainerTemplate = rb_const_get(mLiquid, rb_intern("StrainerTemplate"));
540
+ exception = rb_funcall(cLiquidStrainerTemplate, rb_intern("arg_exc_to_liquid_exc"), 1, exception);
541
+ }
542
+ vm->invoking_filter = false;
543
+ }
544
+ return exception;
545
+ }
546
+
547
+ typedef struct vm_render_rescue_args {
548
+ vm_render_until_error_args_t *render_args;
549
+ size_t old_stack_byte_size;
550
+ } vm_render_rescue_args_t;
551
+
552
+ // Actually returns a bool resume_rendering value
553
+ static VALUE vm_render_rescue(VALUE uncast_args, VALUE exception)
554
+ {
555
+ vm_render_rescue_args_t *args = (void *)uncast_args;
556
+ VALUE blank_tag = Qfalse; // tags are still rendered using Liquid::BlockBody.render_node
557
+ vm_render_until_error_args_t *render_args = args->render_args;
558
+ vm_t *vm = render_args->vm;
559
+
560
+ exception = vm_translate_if_filter_argument_error(vm, exception);
561
+
562
+ const uint8_t *ip = render_args->ip;
563
+ if (!ip)
564
+ rb_exc_raise(exception);
565
+
566
+ // rescue for variable render, where ip is at the start of the render and we need to
567
+ // skip to the end of the variable render to resume rendering if the error is handled
568
+ enum opcode last_op;
569
+ do {
570
+ last_op = *ip;
571
+ liquid_vm_next_instruction(&ip);
572
+ } while (last_op != OP_POP_WRITE);
573
+ render_args->ip = ip;
574
+ // remove temporary stack values from variable evaluation
575
+ vm->stack.data_end = vm->stack.data + args->old_stack_byte_size;
576
+
577
+ assert(render_args->node_line_number);
578
+ unsigned int node_line_number = bytes_to_uint24(render_args->node_line_number);
579
+ VALUE line_number = node_line_number != 0 ? UINT2NUM(node_line_number) : Qnil;
580
+
581
+ rb_funcall(cLiquidBlockBody, rb_intern("c_rescue_render_node"), 5,
582
+ vm->context.self, render_args->output, line_number, exception, blank_tag);
583
+ return true;
584
+ }
585
+
586
+ void liquid_vm_render(block_body_header_t *body, const VALUE *const_ptr, VALUE context, VALUE output)
587
+ {
588
+ vm_t *vm = vm_from_context(context);
589
+
590
+ vm_stack_reserve_for_write(vm, body->max_stack_size);
591
+ resource_limits_increment_render_score(vm->context.resource_limits, body->render_score);
592
+
593
+ vm_render_until_error_args_t render_args = {
594
+ .vm = vm,
595
+ .const_ptr = const_ptr,
596
+ .ip = block_body_instructions_ptr(body),
597
+ .output = output,
598
+ };
599
+ vm_render_rescue_args_t rescue_args = {
600
+ .render_args = &render_args,
601
+ .old_stack_byte_size = c_buffer_size(&vm->stack),
602
+ };
603
+
604
+ while (rb_rescue(vm_render_until_error, (VALUE)&render_args, vm_render_rescue, (VALUE)&rescue_args)) {
605
+ }
606
+ assert(rescue_args.old_stack_byte_size == c_buffer_size(&vm->stack));
607
+ }
608
+
609
+
610
+ void liquid_define_vm(void)
611
+ {
612
+ id_render_node = rb_intern("render_node");
613
+ id_vm = rb_intern("vm");
614
+
615
+ cLiquidCVM = rb_define_class_under(mLiquidC, "VM", rb_cObject);
616
+ rb_undef_alloc_func(cLiquidCVM);
617
+ rb_global_variable(&cLiquidCVM);
618
+ }
@@ -0,0 +1,25 @@
1
+ #ifndef VM_H
2
+ #define VM_H
3
+
4
+ #include <ruby.h>
5
+ #include "block.h"
6
+ #include "vm_assembler.h"
7
+ #include "context.h"
8
+
9
+ typedef struct vm {
10
+ c_buffer_t stack;
11
+ bool invoking_filter;
12
+ context_t context;
13
+ } vm_t;
14
+
15
+ void liquid_define_vm(void);
16
+ vm_t *vm_from_context(VALUE context);
17
+ void liquid_vm_render(block_body_header_t *block, const VALUE *const_ptr, VALUE context, VALUE output);
18
+ void liquid_vm_next_instruction(const uint8_t **ip_ptr);
19
+ bool liquid_vm_filtering(VALUE context);
20
+ VALUE liquid_vm_evaluate(VALUE context, vm_assembler_t *code);
21
+
22
+ vm_t *vm_from_context(VALUE context);
23
+ VALUE vm_translate_if_filter_argument_error(vm_t *vm, VALUE exception);
24
+
25
+ #endif
@@ -0,0 +1,76 @@
1
+ #include "parse_context.h"
2
+ #include "document_body.h"
3
+
4
+ static ID id_document_body, id_vm_assembler_pool;
5
+
6
+ static bool parse_context_document_body_initialized_p(VALUE self)
7
+ {
8
+ return RTEST(rb_attr_get(self, id_document_body));
9
+ }
10
+
11
+ static void parse_context_init_document_body(VALUE self)
12
+ {
13
+ VALUE document_body = document_body_new_instance();
14
+ rb_ivar_set(self, id_document_body, document_body);
15
+ }
16
+
17
+ VALUE parse_context_get_document_body(VALUE self)
18
+ {
19
+ assert(parse_context_document_body_initialized_p(self));
20
+
21
+ return rb_ivar_get(self, id_document_body);
22
+ }
23
+
24
+ vm_assembler_pool_t *parse_context_init_vm_assembler_pool(VALUE self)
25
+ {
26
+ assert(!RTEST(rb_attr_get(self, id_vm_assembler_pool)));
27
+
28
+ VALUE vm_assembler_pool_obj = vm_assembler_pool_new();
29
+ rb_ivar_set(self, id_vm_assembler_pool, vm_assembler_pool_obj);
30
+
31
+ vm_assembler_pool_t *vm_assembler_pool;
32
+ VMAssemblerPool_Get_Struct(vm_assembler_pool_obj, vm_assembler_pool);
33
+
34
+ return vm_assembler_pool;
35
+ }
36
+
37
+ vm_assembler_pool_t *parse_context_get_vm_assembler_pool(VALUE self)
38
+ {
39
+ VALUE obj = rb_ivar_get(self, id_vm_assembler_pool);
40
+
41
+ if (obj == Qnil) {
42
+ rb_raise(rb_eRuntimeError, "Liquid::ParseContext#start_liquid_c_parsing has not yet been called");
43
+ }
44
+
45
+ vm_assembler_pool_t *vm_assembler_pool;
46
+ VMAssemblerPool_Get_Struct(obj, vm_assembler_pool);
47
+ return vm_assembler_pool;
48
+ }
49
+
50
+ static VALUE parse_context_start_liquid_c_parsing(VALUE self)
51
+ {
52
+ if (RB_UNLIKELY(parse_context_document_body_initialized_p(self))) {
53
+ rb_raise(rb_eRuntimeError, "liquid-c parsing already started for this parse context");
54
+ }
55
+ parse_context_init_document_body(self);
56
+ parse_context_init_vm_assembler_pool(self);
57
+ return Qnil;
58
+ }
59
+
60
+ static VALUE parse_context_cleanup_liquid_c_parsing(VALUE self)
61
+ {
62
+ rb_obj_freeze(rb_ivar_get(self, id_document_body));
63
+ rb_ivar_set(self, id_document_body, Qnil);
64
+ rb_ivar_set(self, id_vm_assembler_pool, Qnil);
65
+ return Qnil;
66
+ }
67
+
68
+ void liquid_define_parse_context(void)
69
+ {
70
+ id_document_body = rb_intern("document_body");
71
+ id_vm_assembler_pool = rb_intern("vm_assembler_pool");
72
+
73
+ VALUE cLiquidParseContext = rb_const_get(mLiquid, rb_intern("ParseContext"));
74
+ rb_define_method(cLiquidParseContext, "start_liquid_c_parsing", parse_context_start_liquid_c_parsing, 0);
75
+ rb_define_method(cLiquidParseContext, "cleanup_liquid_c_parsing", parse_context_cleanup_liquid_c_parsing, 0);
76
+ }