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.
- checksums.yaml +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +164 -0
- data/ext/liquid_c/block.c +970 -0
- data/ext/liquid_c/block.h +37 -0
- data/ext/liquid_c/c_buffer.c +48 -0
- data/ext/liquid_c/c_buffer.h +82 -0
- data/ext/liquid_c/context.c +262 -0
- data/ext/liquid_c/context.h +72 -0
- data/ext/liquid_c/document_body.c +181 -0
- data/ext/liquid_c/document_body.h +84 -0
- data/ext/liquid_c/expression.c +130 -0
- data/ext/liquid_c/expression.h +24 -0
- data/ext/liquid_c/extconf.rb +60 -0
- data/ext/liquid_c/intutil.h +36 -0
- data/ext/liquid_c/lexer.c +172 -0
- data/ext/liquid_c/lexer.h +61 -0
- data/ext/liquid_c/liquid.c +182 -0
- data/ext/liquid_c/liquid.h +112 -0
- data/ext/liquid_c/liquid_vm.c +907 -0
- data/ext/liquid_c/liquid_vm.h +35 -0
- data/ext/liquid_c/mri_compat.h +35 -0
- data/ext/liquid_c/native_parser.c +2360 -0
- data/ext/liquid_c/native_value.c +49 -0
- data/ext/liquid_c/parse_context.c +84 -0
- data/ext/liquid_c/parse_context.h +13 -0
- data/ext/liquid_c/parser.c +285 -0
- data/ext/liquid_c/parser.h +25 -0
- data/ext/liquid_c/raw.c +136 -0
- data/ext/liquid_c/raw.h +6 -0
- data/ext/liquid_c/resource_limits.c +459 -0
- data/ext/liquid_c/resource_limits.h +28 -0
- data/ext/liquid_c/stringutil.h +37 -0
- data/ext/liquid_c/tokenizer.c +326 -0
- data/ext/liquid_c/tokenizer.h +46 -0
- data/ext/liquid_c/usage.c +18 -0
- data/ext/liquid_c/usage.h +9 -0
- data/ext/liquid_c/variable.c +198 -0
- data/ext/liquid_c/variable.h +61 -0
- data/ext/liquid_c/variable_lookup.c +73 -0
- data/ext/liquid_c/variable_lookup.h +8 -0
- data/ext/liquid_c/vm_assembler.c +631 -0
- data/ext/liquid_c/vm_assembler.h +294 -0
- data/ext/liquid_c/vm_assembler_pool.c +117 -0
- data/ext/liquid_c/vm_assembler_pool.h +27 -0
- data/lib/liquid/c/compile_ext.rb +47 -0
- data/lib/liquid/c/native_compiler.rb +262 -0
- data/lib/liquid/c/native_parser.rb +261 -0
- data/lib/liquid/c/native_tags.rb +103 -0
- data/lib/liquid/c/native_values.rb +9 -0
- data/lib/liquid/c/version.rb +7 -0
- data/lib/liquid/c.rb +379 -0
- metadata +140 -0
|
@@ -0,0 +1,907 @@
|
|
|
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
|
+
#include "variable.h"
|
|
10
|
+
|
|
11
|
+
ID id_render_node;
|
|
12
|
+
static ID id_vm;
|
|
13
|
+
|
|
14
|
+
static VALUE cLiquidCVM;
|
|
15
|
+
static VALUE mLiquidUtils;
|
|
16
|
+
static unsigned long context_generation = 1;
|
|
17
|
+
static bool native_enabled;
|
|
18
|
+
static ID id_render, id_render_to_output_buffer, id_render_obj_to_output, id_ruby_render;
|
|
19
|
+
static ID id_each;
|
|
20
|
+
static VALUE portable_renderer_methods[3];
|
|
21
|
+
|
|
22
|
+
bool liquid_vm_render_methods_standard(void)
|
|
23
|
+
{
|
|
24
|
+
if (!liquid_method_basic_definition_p(CLASS_OF(cLiquidVariable), rb_intern("instance_method")) ||
|
|
25
|
+
!liquid_method_basic_definition_p(CLASS_OF(portable_renderer_methods[0]), rb_intern("=="))) return false;
|
|
26
|
+
size_t gc = liquid_gc_count();
|
|
27
|
+
const char *names[] = { "render", "render_to_output_buffer", "render_obj_to_output" };
|
|
28
|
+
for (size_t i = 0; i < 3; i++) {
|
|
29
|
+
VALUE current = rb_funcall(cLiquidVariable, rb_intern("instance_method"), 1, ID2SYM(rb_intern(names[i])));
|
|
30
|
+
if (!rb_equal(current, portable_renderer_methods[i])) return false;
|
|
31
|
+
}
|
|
32
|
+
return gc == liquid_gc_count();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
static void vm_mark(void *ptr)
|
|
37
|
+
{
|
|
38
|
+
vm_t *vm = ptr;
|
|
39
|
+
|
|
40
|
+
c_buffer_rb_gc_mark(&vm->stack);
|
|
41
|
+
context_mark(&vm->context);
|
|
42
|
+
for (vm_variable_frame_t *frame = vm->variable_frame; frame; frame = frame->previous) {
|
|
43
|
+
rb_gc_mark(frame->variable);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
static void vm_free(void *ptr)
|
|
48
|
+
{
|
|
49
|
+
vm_t *vm = ptr;
|
|
50
|
+
c_buffer_free(&vm->stack);
|
|
51
|
+
xfree(vm);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
static size_t vm_memsize(const void *ptr)
|
|
55
|
+
{
|
|
56
|
+
const vm_t *vm = ptr;
|
|
57
|
+
return sizeof(vm_t) + c_buffer_capacity(&vm->stack);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const rb_data_type_t vm_data_type = {
|
|
61
|
+
"liquid_vm",
|
|
62
|
+
{ vm_mark, vm_free, vm_memsize, },
|
|
63
|
+
NULL, NULL, RUBY_TYPED_FREE_IMMEDIATELY
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
static VALUE vm_internal_new(VALUE context)
|
|
67
|
+
{
|
|
68
|
+
vm_t *vm;
|
|
69
|
+
VALUE obj = TypedData_Make_Struct(cLiquidCVM, vm_t, &vm_data_type, vm);
|
|
70
|
+
vm->stack = c_buffer_init();
|
|
71
|
+
|
|
72
|
+
vm->invoking_filter = false;
|
|
73
|
+
|
|
74
|
+
vm->context_generation = context_generation;
|
|
75
|
+
context_internal_init(context, &vm->context);
|
|
76
|
+
|
|
77
|
+
return obj;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
static void vm_refresh_context(vm_t *vm)
|
|
81
|
+
{
|
|
82
|
+
if (vm->context_generation != context_generation || !native_enabled) {
|
|
83
|
+
unsigned long generation = context_generation;
|
|
84
|
+
unsigned long revision = vm->context_revision;
|
|
85
|
+
context_refresh(&vm->context);
|
|
86
|
+
vm->context_generation = revision == vm->context_revision ? generation : 0;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
vm_t *vm_cached_for_context(VALUE context)
|
|
91
|
+
{
|
|
92
|
+
VALUE cached = rb_attr_get(context, id_vm);
|
|
93
|
+
if (NIL_P(cached)) return NULL;
|
|
94
|
+
|
|
95
|
+
vm_t *vm;
|
|
96
|
+
TypedData_Get_Struct(cached, vm_t, &vm_data_type, vm);
|
|
97
|
+
return vm->context.self == context ? vm : NULL;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
vm_t *vm_from_context(VALUE context)
|
|
101
|
+
{
|
|
102
|
+
vm_t *vm = vm_cached_for_context(context);
|
|
103
|
+
if (!vm) {
|
|
104
|
+
VALUE obj = vm_internal_new(context);
|
|
105
|
+
rb_ivar_set(context, id_vm, obj);
|
|
106
|
+
vm = RTYPEDDATA_DATA(obj);
|
|
107
|
+
}
|
|
108
|
+
vm_refresh_context(vm);
|
|
109
|
+
return vm;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
bool liquid_vm_filtering(VALUE context)
|
|
113
|
+
{
|
|
114
|
+
vm_t *vm = vm_cached_for_context(context);
|
|
115
|
+
return vm && vm->invoking_filter;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
static void write_fixnum(VALUE output, VALUE fixnum)
|
|
119
|
+
{
|
|
120
|
+
char buffer[32];
|
|
121
|
+
int length = snprintf(buffer, sizeof(buffer), "%lld", RB_NUM2LL(fixnum));
|
|
122
|
+
rb_enc_str_buf_cat(output, buffer, length, utf8_encoding);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
static VALUE obj_to_s(VALUE obj)
|
|
126
|
+
{
|
|
127
|
+
VALUE str = rb_funcall(obj, id_to_s, 0);
|
|
128
|
+
|
|
129
|
+
if (RB_LIKELY(RB_TYPE_P(str, T_STRING)))
|
|
130
|
+
return str;
|
|
131
|
+
|
|
132
|
+
rb_raise(rb_eTypeError, "%"PRIsVALUE"#to_s returned a non-String convertible value of type %"PRIsVALUE,
|
|
133
|
+
rb_obj_class(obj), rb_obj_class(str));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
static void write_obj(VALUE output, VALUE obj);
|
|
137
|
+
|
|
138
|
+
typedef struct {
|
|
139
|
+
VALUE variable;
|
|
140
|
+
VALUE output;
|
|
141
|
+
} array_writer_args_t;
|
|
142
|
+
|
|
143
|
+
static VALUE write_array_element(RB_BLOCK_CALL_FUNC_ARGLIST(value, uncast_args))
|
|
144
|
+
{
|
|
145
|
+
array_writer_args_t *args = (void *)uncast_args;
|
|
146
|
+
if (NIL_P(args->variable)) {
|
|
147
|
+
write_obj(args->output, value);
|
|
148
|
+
return args->output;
|
|
149
|
+
}
|
|
150
|
+
return rb_funcall(args->variable, id_render_obj_to_output, 2, value, args->output);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
static VALUE write_array(VALUE array, VALUE uncast_args, int recursive)
|
|
154
|
+
{
|
|
155
|
+
if (recursive || liquid_stack_check()) {
|
|
156
|
+
rb_raise(rb_eSysStackError, "stack level too deep");
|
|
157
|
+
}
|
|
158
|
+
return rb_block_call(array, id_each, 0, NULL, write_array_element, uncast_args);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
static void write_obj(VALUE output, VALUE obj)
|
|
162
|
+
{
|
|
163
|
+
switch (TYPE(obj)) {
|
|
164
|
+
case T_STRING:
|
|
165
|
+
{
|
|
166
|
+
rb_str_buf_append(output, obj_to_s(obj));
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
case T_FIXNUM:
|
|
170
|
+
write_fixnum(output, obj);
|
|
171
|
+
break;
|
|
172
|
+
case T_ARRAY:
|
|
173
|
+
{
|
|
174
|
+
array_writer_args_t args = { Qnil, output };
|
|
175
|
+
rb_exec_recursive(write_array, obj, (VALUE)&args);
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
case T_NIL:
|
|
179
|
+
break;
|
|
180
|
+
default:
|
|
181
|
+
{
|
|
182
|
+
VALUE string = rb_funcall(mLiquidUtils, id_to_s, 1, obj);
|
|
183
|
+
if (!RB_TYPE_P(string, T_STRING)) {
|
|
184
|
+
string = obj_to_s(obj);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
rb_str_buf_append(output, string);
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
static inline void vm_stack_push(vm_t *vm, VALUE value)
|
|
194
|
+
{
|
|
195
|
+
VALUE *stack_ptr = (VALUE *)vm->stack.data_end;
|
|
196
|
+
assert(stack_ptr < (VALUE *)vm->stack.capacity_end);
|
|
197
|
+
*stack_ptr++ = value;
|
|
198
|
+
vm->stack.data_end = (uint8_t *)stack_ptr;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
static inline VALUE *vm_stack_peek_n(vm_t *vm, size_t n)
|
|
202
|
+
{
|
|
203
|
+
VALUE *stack_ptr = (VALUE *)vm->stack.data_end;
|
|
204
|
+
stack_ptr -= n;
|
|
205
|
+
assert((VALUE *)vm->stack.data <= stack_ptr);
|
|
206
|
+
return stack_ptr;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
static inline VALUE *vm_stack_pop_n(vm_t *vm, size_t n)
|
|
210
|
+
{
|
|
211
|
+
VALUE *stack_ptr = vm_stack_peek_n(vm, n);
|
|
212
|
+
vm->stack.data_end = (uint8_t *)stack_ptr;
|
|
213
|
+
return stack_ptr;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
static inline VALUE vm_stack_pop(vm_t *vm)
|
|
217
|
+
{
|
|
218
|
+
return *vm_stack_pop_n(vm, 1);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
static inline void vm_stack_reserve_for_write(vm_t *vm, size_t num_values)
|
|
222
|
+
{
|
|
223
|
+
if (num_values > SIZE_MAX / sizeof(VALUE)) rb_raise(rb_eNoMemError, "Liquid stack size overflow");
|
|
224
|
+
c_buffer_reserve_for_write(&vm->stack, num_values * sizeof(VALUE));
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
typedef struct {
|
|
228
|
+
vm_t *vm;
|
|
229
|
+
VALUE name;
|
|
230
|
+
VALUE *args;
|
|
231
|
+
int count;
|
|
232
|
+
bool previous;
|
|
233
|
+
} vm_filter_args_t;
|
|
234
|
+
|
|
235
|
+
static VALUE vm_call_filter(VALUE opaque)
|
|
236
|
+
{
|
|
237
|
+
vm_filter_args_t *args = (void *)opaque;
|
|
238
|
+
return rb_funcallv(args->vm->context.strainer, RB_SYM2ID(args->name), args->count, args->args);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
__attribute__((noreturn)) static VALUE vm_filter_argument_error(VALUE unused, VALUE exception)
|
|
242
|
+
{
|
|
243
|
+
VALUE klass = rb_const_get(mLiquid, rb_intern("StrainerTemplate"));
|
|
244
|
+
rb_exc_raise(rb_funcall(klass, rb_intern("arg_exc_to_liquid_exc"), 1, exception));
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
static VALUE vm_try_filter(VALUE opaque)
|
|
248
|
+
{
|
|
249
|
+
return rb_rescue2(vm_call_filter, opaque, vm_filter_argument_error, Qnil, rb_eArgError, (VALUE)0);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
static VALUE vm_restore_filter(VALUE opaque)
|
|
253
|
+
{
|
|
254
|
+
vm_filter_args_t *args = (void *)opaque;
|
|
255
|
+
args->vm->invoking_filter = args->previous;
|
|
256
|
+
return Qnil;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
static void vm_increment_write_score(vm_t *vm, VALUE output)
|
|
260
|
+
{
|
|
261
|
+
rb_funcall(vm->context.resource_limits_obj, rb_intern("increment_write_score"), 1, output);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
static VALUE vm_reset_context(VALUE klass, VALUE context)
|
|
265
|
+
{
|
|
266
|
+
if (!vm_cached_for_context(context)) rb_ivar_set(context, id_vm, Qnil);
|
|
267
|
+
return Qnil;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
static VALUE vm_invoke_filter(vm_t *vm, VALUE filter_name, size_t num_args)
|
|
271
|
+
{
|
|
272
|
+
vm_refresh_context(vm);
|
|
273
|
+
VALUE *popped_args = vm_stack_pop_n(vm, num_args);
|
|
274
|
+
/* We have to copy popped_args_ptr to the stack because the VM
|
|
275
|
+
* no longer holds onto these objects, so they have to exist on
|
|
276
|
+
* the stack to ensure they don't get garbage collected. */
|
|
277
|
+
VALUE *args = alloca(sizeof(VALUE *) * num_args);
|
|
278
|
+
memcpy(args, popped_args, sizeof(VALUE *) * num_args);
|
|
279
|
+
|
|
280
|
+
bool not_invokable = rb_hash_lookup(vm->context.filter_methods, filter_name) != Qtrue;
|
|
281
|
+
if (RB_UNLIKELY(not_invokable)) {
|
|
282
|
+
if (RTEST(rb_funcall(vm->context.self, rb_intern("strict_filters"), 0))) {
|
|
283
|
+
VALUE error_class = rb_const_get(mLiquid, rb_intern("UndefinedFilter"));
|
|
284
|
+
rb_raise(error_class, "undefined filter %"PRIsVALUE, rb_sym2str(filter_name));
|
|
285
|
+
}
|
|
286
|
+
return args[0];
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
vm_filter_args_t call = { vm, filter_name, args, (int)num_args, vm->invoking_filter };
|
|
290
|
+
vm->invoking_filter = true;
|
|
291
|
+
VALUE result = rb_ensure(vm_try_filter, (VALUE)&call, vm_restore_filter, (VALUE)&call);
|
|
292
|
+
return liquid_value_to_liquid(result);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
typedef struct vm_render_until_error_args {
|
|
296
|
+
vm_t *vm;
|
|
297
|
+
const uint8_t *ip; // use for initial address and to save an address for rescuing
|
|
298
|
+
const VALUE *const_ptr;
|
|
299
|
+
|
|
300
|
+
/* rendering fields */
|
|
301
|
+
VALUE output;
|
|
302
|
+
const uint8_t *node_line_number;
|
|
303
|
+
const VALUE *nodelist;
|
|
304
|
+
bool evaluate_variable;
|
|
305
|
+
size_t max_variable_stack_size;
|
|
306
|
+
VALUE variable_node;
|
|
307
|
+
} vm_render_until_error_args_t;
|
|
308
|
+
|
|
309
|
+
static VALUE raise_invalid_integer(VALUE unused_arg, VALUE exc)
|
|
310
|
+
{
|
|
311
|
+
rb_raise(cLiquidArgumentError, "invalid integer");
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Equivalent to Integer(string) if string is an instance of String
|
|
315
|
+
static VALUE try_string_to_integer(VALUE string)
|
|
316
|
+
{
|
|
317
|
+
return rb_str_to_inum(string, 0, true);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
static VALUE range_value_to_integer(VALUE value)
|
|
321
|
+
{
|
|
322
|
+
if (RB_INTEGER_TYPE_P(value)) {
|
|
323
|
+
return value;
|
|
324
|
+
} else if (value == Qnil) {
|
|
325
|
+
return INT2FIX(0);
|
|
326
|
+
} else if (RB_TYPE_P(value, T_STRING)) {
|
|
327
|
+
return rb_funcall(value, rb_intern("to_i"), 0);
|
|
328
|
+
} else {
|
|
329
|
+
value = obj_to_s(value);
|
|
330
|
+
return rb_rescue2(try_string_to_integer, value, raise_invalid_integer, Qnil, rb_eArgError, (VALUE)0);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
static const uint8_t *vm_skip_variable_instructions(const uint8_t *ip)
|
|
335
|
+
{
|
|
336
|
+
enum opcode opcode;
|
|
337
|
+
do {
|
|
338
|
+
opcode = *ip;
|
|
339
|
+
liquid_vm_next_instruction(&ip);
|
|
340
|
+
} while (opcode != OP_POP_WRITE);
|
|
341
|
+
|
|
342
|
+
return ip;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
typedef struct {
|
|
346
|
+
vm_t *vm;
|
|
347
|
+
vm_variable_frame_t frame;
|
|
348
|
+
VALUE output;
|
|
349
|
+
} variable_render_args_t;
|
|
350
|
+
|
|
351
|
+
static VALUE vm_call_variable_render(VALUE uncast_args)
|
|
352
|
+
{
|
|
353
|
+
variable_render_args_t *args = (void *)uncast_args;
|
|
354
|
+
return rb_funcall(args->frame.variable, id_render_to_output_buffer, 2, args->vm->context.self, args->output);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
static VALUE vm_restore_variable_frame(VALUE uncast_args)
|
|
358
|
+
{
|
|
359
|
+
variable_render_args_t *args = (void *)uncast_args;
|
|
360
|
+
args->vm->variable_frame = args->frame.previous;
|
|
361
|
+
return Qnil;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// Actually returns a bool resume_rendering value
|
|
365
|
+
static VALUE vm_render_until_error(VALUE uncast_args)
|
|
366
|
+
{
|
|
367
|
+
vm_render_until_error_args_t *args = (void *)uncast_args;
|
|
368
|
+
const VALUE *constants = args->const_ptr;
|
|
369
|
+
const uint8_t *ip = args->ip;
|
|
370
|
+
vm_t *vm = args->vm;
|
|
371
|
+
VALUE output = args->output;
|
|
372
|
+
uint32_t constant_index;
|
|
373
|
+
VALUE constant = Qnil;
|
|
374
|
+
bool lookup_missing = false;
|
|
375
|
+
args->ip = NULL; // used by vm_render_rescue, NULL to indicate that it isn't in a rescue block
|
|
376
|
+
|
|
377
|
+
while (true) {
|
|
378
|
+
uint8_t opcode = *ip++;
|
|
379
|
+
switch (opcode) {
|
|
380
|
+
case OP_LEAVE:
|
|
381
|
+
return false;
|
|
382
|
+
case OP_PUSH_NIL:
|
|
383
|
+
vm_stack_push(vm, Qnil);
|
|
384
|
+
break;
|
|
385
|
+
case OP_PUSH_TRUE:
|
|
386
|
+
vm_stack_push(vm, Qtrue);
|
|
387
|
+
break;
|
|
388
|
+
case OP_PUSH_FALSE:
|
|
389
|
+
vm_stack_push(vm, Qfalse);
|
|
390
|
+
break;
|
|
391
|
+
case OP_PUSH_INT8:
|
|
392
|
+
{
|
|
393
|
+
int num = *(int8_t *)ip++; // signed
|
|
394
|
+
vm_stack_push(vm, RB_INT2FIX(num));
|
|
395
|
+
break;
|
|
396
|
+
}
|
|
397
|
+
case OP_PUSH_INT16:
|
|
398
|
+
{
|
|
399
|
+
unsigned int bits = ((unsigned int)ip[0] << 8) | ip[1];
|
|
400
|
+
int num = bits >= 0x8000 ? (int)bits - 0x10000 : (int)bits;
|
|
401
|
+
ip += 2;
|
|
402
|
+
vm_stack_push(vm, RB_INT2FIX(num));
|
|
403
|
+
break;
|
|
404
|
+
}
|
|
405
|
+
case OP_FIND_STATIC_VAR:
|
|
406
|
+
{
|
|
407
|
+
vm_refresh_context(vm);
|
|
408
|
+
constant_index = bytes_to_uint32(ip);
|
|
409
|
+
constant = constants[constant_index];
|
|
410
|
+
ip += 4;
|
|
411
|
+
VALUE value = context_find_variable(&vm->context, constant, Qtrue);
|
|
412
|
+
vm_stack_push(vm, value);
|
|
413
|
+
break;
|
|
414
|
+
}
|
|
415
|
+
case OP_FIND_VAR:
|
|
416
|
+
{
|
|
417
|
+
vm_refresh_context(vm);
|
|
418
|
+
VALUE key = vm_stack_pop(vm);
|
|
419
|
+
VALUE value = context_find_variable(&vm->context, key, Qtrue);
|
|
420
|
+
vm_stack_push(vm, value);
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
case OP_LOOKUP_CONST_KEY:
|
|
424
|
+
case OP_LOOKUP_COMMAND:
|
|
425
|
+
{
|
|
426
|
+
constant_index = bytes_to_uint32(ip);
|
|
427
|
+
constant = constants[constant_index];
|
|
428
|
+
ip += 4;
|
|
429
|
+
vm_stack_push(vm, constant);
|
|
430
|
+
}
|
|
431
|
+
/* fallthrough */
|
|
432
|
+
case OP_LOOKUP_KEY:
|
|
433
|
+
{
|
|
434
|
+
vm_refresh_context(vm);
|
|
435
|
+
bool is_command = opcode == OP_LOOKUP_COMMAND;
|
|
436
|
+
VALUE key = vm_stack_pop(vm);
|
|
437
|
+
VALUE object = vm_stack_pop(vm);
|
|
438
|
+
VALUE result = variable_lookup_key(vm->context.self, object, key, is_command);
|
|
439
|
+
lookup_missing = result == Qundef;
|
|
440
|
+
vm_stack_push(vm, lookup_missing ? Qnil : result);
|
|
441
|
+
break;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
case OP_LOOKUP_GUARD:
|
|
445
|
+
{
|
|
446
|
+
size_t distance = bytes_to_uint32(ip);
|
|
447
|
+
ip += 4;
|
|
448
|
+
if (lookup_missing)
|
|
449
|
+
ip += distance;
|
|
450
|
+
break;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
case OP_NEW_INT_RANGE:
|
|
454
|
+
{
|
|
455
|
+
VALUE end = range_value_to_integer(vm_stack_pop(vm));
|
|
456
|
+
VALUE begin = range_value_to_integer(vm_stack_pop(vm));
|
|
457
|
+
bool exclude_end = false;
|
|
458
|
+
vm_stack_push(vm, rb_range_new(begin, end, exclude_end));
|
|
459
|
+
break;
|
|
460
|
+
}
|
|
461
|
+
case OP_HASH_NEW:
|
|
462
|
+
{
|
|
463
|
+
size_t hash_size = *ip++;
|
|
464
|
+
size_t num_keys_and_values = hash_size * 2;
|
|
465
|
+
VALUE hash = rb_hash_new();
|
|
466
|
+
|
|
467
|
+
for (size_t i = 0; i < num_keys_and_values; i += 2) {
|
|
468
|
+
VALUE *pair = vm_stack_peek_n(vm, num_keys_and_values - i);
|
|
469
|
+
rb_hash_aset(hash, pair[0], pair[1]);
|
|
470
|
+
}
|
|
471
|
+
vm_stack_pop_n(vm, num_keys_and_values);
|
|
472
|
+
|
|
473
|
+
vm_stack_push(vm, hash);
|
|
474
|
+
break;
|
|
475
|
+
}
|
|
476
|
+
case OP_FILTER:
|
|
477
|
+
case OP_BUILTIN_FILTER:
|
|
478
|
+
{
|
|
479
|
+
VALUE filter_name;
|
|
480
|
+
unsigned long num_args;
|
|
481
|
+
|
|
482
|
+
if (ip[-1] == OP_FILTER) {
|
|
483
|
+
constant_index = bytes_to_uint32(ip);
|
|
484
|
+
constant = constants[constant_index];
|
|
485
|
+
filter_name = RARRAY_AREF(constant, 0);
|
|
486
|
+
num_args = FIX2ULONG(RARRAY_AREF(constant, 1));
|
|
487
|
+
ip += 4;
|
|
488
|
+
} else {
|
|
489
|
+
assert(ip[-1] == OP_BUILTIN_FILTER);
|
|
490
|
+
filter_name = builtin_filters[*ip++].sym;
|
|
491
|
+
num_args = *ip++; // includes input argument
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
VALUE result = vm_invoke_filter(vm, filter_name, num_args);
|
|
495
|
+
vm_stack_push(vm, result);
|
|
496
|
+
break;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
// Rendering instructions
|
|
500
|
+
|
|
501
|
+
case OP_WRITE_RAW_W:
|
|
502
|
+
case OP_WRITE_RAW:
|
|
503
|
+
{
|
|
504
|
+
const char *text;
|
|
505
|
+
size_t size;
|
|
506
|
+
if (ip[-1] == OP_WRITE_RAW_W) {
|
|
507
|
+
size = bytes_to_uint24(ip);
|
|
508
|
+
text = (const char *)&ip[3];
|
|
509
|
+
ip += 3 + size;
|
|
510
|
+
} else {
|
|
511
|
+
size = *ip;
|
|
512
|
+
text = (const char *)&ip[1];
|
|
513
|
+
ip += 1 + size;
|
|
514
|
+
}
|
|
515
|
+
rb_enc_str_buf_cat(output, text, size, utf8_encoding);
|
|
516
|
+
vm_increment_write_score(vm, output);
|
|
517
|
+
break;
|
|
518
|
+
}
|
|
519
|
+
case OP_JUMP_FWD_W:
|
|
520
|
+
{
|
|
521
|
+
size_t size = bytes_to_uint24(ip);
|
|
522
|
+
ip += 3 + size;
|
|
523
|
+
break;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
case OP_JUMP_FWD:
|
|
527
|
+
{
|
|
528
|
+
uint8_t size = *ip;
|
|
529
|
+
ip += 1 + size;
|
|
530
|
+
break;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
case OP_EVALUATE:
|
|
534
|
+
{
|
|
535
|
+
constant_index = bytes_to_uint32(ip);
|
|
536
|
+
ip += 4;
|
|
537
|
+
VALUE value = rb_funcall(vm->context.self, id_evaluate, 1, constants[constant_index]);
|
|
538
|
+
vm_stack_push(vm, value);
|
|
539
|
+
break;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
case OP_PUSH_CONST:
|
|
543
|
+
{
|
|
544
|
+
constant_index = bytes_to_uint32(ip);
|
|
545
|
+
constant = constants[constant_index];
|
|
546
|
+
ip += 4;
|
|
547
|
+
vm_stack_push(vm, constant);
|
|
548
|
+
break;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
case OP_WRITE_NODE:
|
|
552
|
+
{
|
|
553
|
+
constant_index = bytes_to_uint32(ip);
|
|
554
|
+
constant = constants[constant_index];
|
|
555
|
+
ip += 4;
|
|
556
|
+
rb_funcall(cLiquidBlockBody, id_render_node, 3, vm->context.self, output, constant);
|
|
557
|
+
|
|
558
|
+
if (RARRAY_LEN(vm->context.interrupts)) {
|
|
559
|
+
return false;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
vm_increment_write_score(vm, output);
|
|
563
|
+
break;
|
|
564
|
+
}
|
|
565
|
+
case OP_RENDER_VARIABLE_RESCUE:
|
|
566
|
+
case OP_RENDER_VARIABLE_RESCUE_W:
|
|
567
|
+
{
|
|
568
|
+
// Save state used by vm_render_rescue to rescue from a variable rendering exception
|
|
569
|
+
args->node_line_number = ip;
|
|
570
|
+
bool wide = ip[-1] == OP_RENDER_VARIABLE_RESCUE_W;
|
|
571
|
+
// vm_render_rescue will iterate from this instruction to the instruction
|
|
572
|
+
// following OP_POP_WRITE_VARIABLE to resume rendering from
|
|
573
|
+
size_t line_size = wide ? 4 : VM_VARIABLE_LINE_NUMBER_SIZE;
|
|
574
|
+
uint32_t variable_index = bytes_to_uint32(ip + line_size);
|
|
575
|
+
ip += line_size + 4;
|
|
576
|
+
|
|
577
|
+
args->ip = ip;
|
|
578
|
+
VALUE node = constants[variable_index];
|
|
579
|
+
bool materialized_variable = variable_placeholder_p(node) && variable_placeholder_object_render_p(node);
|
|
580
|
+
if (variable_placeholder_p(node) && !materialized_variable && native_enabled &&
|
|
581
|
+
*args->nodelist == Qundef && !variable_render_traced() && variable_render_methods_standard()) {
|
|
582
|
+
args->variable_node = node;
|
|
583
|
+
break;
|
|
584
|
+
}
|
|
585
|
+
bool custom_renderer = !variable_render_methods_standard();
|
|
586
|
+
if (variable_placeholder_p(node) && custom_renderer) variable_placeholder_use_object(node);
|
|
587
|
+
node = variable_materialize(node);
|
|
588
|
+
VALUE materialized = Qnil;
|
|
589
|
+
variable_render_args_t render_args = {
|
|
590
|
+
.vm = vm,
|
|
591
|
+
.frame = {
|
|
592
|
+
.previous = vm->variable_frame,
|
|
593
|
+
.variable = node,
|
|
594
|
+
.instructions = ip,
|
|
595
|
+
.constants = constants,
|
|
596
|
+
.nodelist = materialized_variable || custom_renderer ? &materialized : args->nodelist,
|
|
597
|
+
.max_stack_size = args->max_variable_stack_size,
|
|
598
|
+
},
|
|
599
|
+
.output = output,
|
|
600
|
+
};
|
|
601
|
+
vm->variable_frame = &render_args.frame;
|
|
602
|
+
rb_ensure(vm_call_variable_render, (VALUE)&render_args, vm_restore_variable_frame, (VALUE)&render_args);
|
|
603
|
+
ip = vm_skip_variable_instructions(ip);
|
|
604
|
+
args->ip = NULL;
|
|
605
|
+
if (RARRAY_LEN(vm->context.interrupts))
|
|
606
|
+
return false;
|
|
607
|
+
vm_increment_write_score(vm, output);
|
|
608
|
+
break;
|
|
609
|
+
}
|
|
610
|
+
case OP_POP_WRITE:
|
|
611
|
+
{
|
|
612
|
+
if (args->evaluate_variable) {
|
|
613
|
+
return false;
|
|
614
|
+
}
|
|
615
|
+
VALUE var_result = vm_stack_pop(vm);
|
|
616
|
+
vm_refresh_context(vm);
|
|
617
|
+
if (vm->context.global_filter != Qnil)
|
|
618
|
+
var_result = rb_funcall(vm->context.global_filter, id_call, 1, var_result);
|
|
619
|
+
if (RB_TYPE_P(var_result, T_ARRAY) || !variable_output_method_standard() ||
|
|
620
|
+
!liquid_method_basic_definition_p(CLASS_OF(output), rb_intern("<<"))) {
|
|
621
|
+
if (!variable_render_methods_standard()) variable_placeholder_use_object(args->variable_node);
|
|
622
|
+
VALUE variable = variable_materialize(args->variable_node);
|
|
623
|
+
rb_funcall(variable, id_render_obj_to_output, 2, var_result, output);
|
|
624
|
+
} else write_obj(output, var_result);
|
|
625
|
+
args->ip = NULL; // mark the end of a rescue block, used by vm_render_rescue
|
|
626
|
+
vm_increment_write_score(vm, output);
|
|
627
|
+
if (RARRAY_LEN(vm->context.interrupts)) return false;
|
|
628
|
+
break;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
default:
|
|
632
|
+
rb_bug("invalid opcode: %u", ip[-1]);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
typedef struct vm_execution_args {
|
|
638
|
+
vm_render_until_error_args_t *render_args;
|
|
639
|
+
size_t old_stack_byte_size;
|
|
640
|
+
} vm_execution_args_t;
|
|
641
|
+
|
|
642
|
+
static VALUE vm_restore_stack(VALUE uncast_args)
|
|
643
|
+
{
|
|
644
|
+
vm_execution_args_t *args = (void *)uncast_args;
|
|
645
|
+
vm_render_until_error_args_t *render_args = args->render_args;
|
|
646
|
+
vm_t *vm = render_args->vm;
|
|
647
|
+
|
|
648
|
+
vm->stack.data_end = vm->stack.data ? vm->stack.data + args->old_stack_byte_size : NULL;
|
|
649
|
+
|
|
650
|
+
return Qnil;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
static VALUE vm_evaluate_body(VALUE opaque)
|
|
654
|
+
{
|
|
655
|
+
vm_execution_args_t *args = (void *)opaque;
|
|
656
|
+
vm_render_until_error((VALUE)args->render_args);
|
|
657
|
+
VALUE result = vm_stack_pop(args->render_args->vm);
|
|
658
|
+
assert(args->old_stack_byte_size == c_buffer_size(&args->render_args->vm->stack));
|
|
659
|
+
return result;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
// Evaluate instructions that avoid using rendering instructions and leave with the result on
|
|
663
|
+
// the top of the stack
|
|
664
|
+
VALUE liquid_vm_evaluate(VALUE context, vm_assembler_t *code)
|
|
665
|
+
{
|
|
666
|
+
vm_t *vm = vm_from_context(context);
|
|
667
|
+
vm_stack_reserve_for_write(vm, code->max_stack_size);
|
|
668
|
+
|
|
669
|
+
vm_render_until_error_args_t args = {
|
|
670
|
+
.vm = vm,
|
|
671
|
+
.const_ptr = (const VALUE *)code->constants.data,
|
|
672
|
+
.ip = code->instructions.data
|
|
673
|
+
};
|
|
674
|
+
vm_execution_args_t execution_args = {
|
|
675
|
+
.render_args = &args,
|
|
676
|
+
.old_stack_byte_size = c_buffer_size(&vm->stack),
|
|
677
|
+
};
|
|
678
|
+
return rb_ensure(vm_evaluate_body, (VALUE)&execution_args, vm_restore_stack, (VALUE)&execution_args);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
void liquid_vm_next_instruction(const uint8_t **ip_ptr)
|
|
682
|
+
{
|
|
683
|
+
const uint8_t *ip = *ip_ptr;
|
|
684
|
+
|
|
685
|
+
switch (*ip++) {
|
|
686
|
+
case OP_LEAVE:
|
|
687
|
+
case OP_POP_WRITE:
|
|
688
|
+
case OP_PUSH_NIL:
|
|
689
|
+
case OP_PUSH_TRUE:
|
|
690
|
+
case OP_PUSH_FALSE:
|
|
691
|
+
case OP_FIND_VAR:
|
|
692
|
+
case OP_LOOKUP_KEY:
|
|
693
|
+
case OP_NEW_INT_RANGE:
|
|
694
|
+
break;
|
|
695
|
+
|
|
696
|
+
case OP_HASH_NEW:
|
|
697
|
+
case OP_PUSH_INT8:
|
|
698
|
+
ip++;
|
|
699
|
+
break;
|
|
700
|
+
|
|
701
|
+
case OP_BUILTIN_FILTER:
|
|
702
|
+
case OP_PUSH_INT16:
|
|
703
|
+
ip += 2;
|
|
704
|
+
break;
|
|
705
|
+
|
|
706
|
+
case OP_EVALUATE:
|
|
707
|
+
case OP_PUSH_CONST:
|
|
708
|
+
case OP_WRITE_NODE:
|
|
709
|
+
case OP_FIND_STATIC_VAR:
|
|
710
|
+
case OP_LOOKUP_CONST_KEY:
|
|
711
|
+
case OP_LOOKUP_COMMAND:
|
|
712
|
+
case OP_FILTER:
|
|
713
|
+
case OP_LOOKUP_GUARD:
|
|
714
|
+
ip += 4;
|
|
715
|
+
break;
|
|
716
|
+
|
|
717
|
+
case OP_RENDER_VARIABLE_RESCUE:
|
|
718
|
+
ip += VM_VARIABLE_METADATA_SIZE;
|
|
719
|
+
break;
|
|
720
|
+
case OP_RENDER_VARIABLE_RESCUE_W:
|
|
721
|
+
ip += 8;
|
|
722
|
+
break;
|
|
723
|
+
|
|
724
|
+
case OP_WRITE_RAW_W:
|
|
725
|
+
case OP_JUMP_FWD_W:
|
|
726
|
+
{
|
|
727
|
+
size_t size = bytes_to_uint24(ip);
|
|
728
|
+
ip += 3 + size;
|
|
729
|
+
break;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
case OP_WRITE_RAW:
|
|
733
|
+
case OP_JUMP_FWD:
|
|
734
|
+
{
|
|
735
|
+
uint8_t size = *ip;
|
|
736
|
+
ip += 1 + size;
|
|
737
|
+
break;
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
default:
|
|
741
|
+
rb_bug("invalid opcode: %u", ip[-1]);
|
|
742
|
+
}
|
|
743
|
+
*ip_ptr = ip;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
// Actually returns a bool resume_rendering value
|
|
747
|
+
static VALUE vm_render_rescue(VALUE uncast_args, VALUE exception)
|
|
748
|
+
{
|
|
749
|
+
vm_execution_args_t *args = (void *)uncast_args;
|
|
750
|
+
VALUE blank_tag = Qfalse; // tags are still rendered using Liquid::BlockBody.render_node
|
|
751
|
+
vm_render_until_error_args_t *render_args = args->render_args;
|
|
752
|
+
vm_t *vm = render_args->vm;
|
|
753
|
+
|
|
754
|
+
const uint8_t *ip = render_args->ip;
|
|
755
|
+
if (!ip)
|
|
756
|
+
rb_exc_raise(exception);
|
|
757
|
+
|
|
758
|
+
// rescue for variable render, where ip is at the start of the render and we need to
|
|
759
|
+
// skip to the end of the variable render to resume rendering if the error is handled
|
|
760
|
+
render_args->ip = vm_skip_variable_instructions(ip);
|
|
761
|
+
// remove temporary stack values from variable evaluation
|
|
762
|
+
vm->stack.data_end = vm->stack.data ? vm->stack.data + args->old_stack_byte_size : NULL;
|
|
763
|
+
|
|
764
|
+
const uint8_t *line_metadata = render_args->node_line_number;
|
|
765
|
+
assert(line_metadata);
|
|
766
|
+
VALUE line_number;
|
|
767
|
+
if (line_metadata[-1] == OP_RENDER_VARIABLE_RESCUE_W) {
|
|
768
|
+
line_number = render_args->const_ptr[bytes_to_uint32(line_metadata)];
|
|
769
|
+
} else {
|
|
770
|
+
unsigned int line = bytes_to_uint24(line_metadata);
|
|
771
|
+
line_number = line ? UINT2NUM(line) : Qnil;
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
rb_funcall(cLiquidBlockBody, rb_intern("c_rescue_render_node"), 5,
|
|
775
|
+
vm->context.self, render_args->output, line_number, exception, blank_tag);
|
|
776
|
+
if (RARRAY_LEN(vm->context.interrupts))
|
|
777
|
+
return false;
|
|
778
|
+
vm_increment_write_score(vm, render_args->output);
|
|
779
|
+
return true;
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
static VALUE vm_render_loop(VALUE opaque)
|
|
783
|
+
{
|
|
784
|
+
vm_execution_args_t *args = (void *)opaque;
|
|
785
|
+
while (rb_rescue(vm_render_until_error, (VALUE)args->render_args, vm_render_rescue, opaque)) {
|
|
786
|
+
}
|
|
787
|
+
assert(args->old_stack_byte_size == c_buffer_size(&args->render_args->vm->stack));
|
|
788
|
+
return Qnil;
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
void liquid_vm_render(block_body_header_t *body, const VALUE *const_ptr, VALUE context, VALUE output, const VALUE *nodelist)
|
|
792
|
+
{
|
|
793
|
+
vm_t *vm = vm_from_context(context);
|
|
794
|
+
|
|
795
|
+
vm_stack_reserve_for_write(vm, body->max_stack_size);
|
|
796
|
+
rb_funcall(vm->context.resource_limits_obj, rb_intern("increment_render_score"), 1, UINT2NUM(body->render_score));
|
|
797
|
+
|
|
798
|
+
vm_render_until_error_args_t render_args = {
|
|
799
|
+
.vm = vm,
|
|
800
|
+
.const_ptr = const_ptr,
|
|
801
|
+
.ip = block_body_instructions_ptr(body),
|
|
802
|
+
.output = output,
|
|
803
|
+
.nodelist = nodelist,
|
|
804
|
+
.max_variable_stack_size = body->max_stack_size,
|
|
805
|
+
};
|
|
806
|
+
vm_execution_args_t execution_args = {
|
|
807
|
+
.render_args = &render_args,
|
|
808
|
+
.old_stack_byte_size = c_buffer_size(&vm->stack),
|
|
809
|
+
};
|
|
810
|
+
|
|
811
|
+
rb_ensure(vm_render_loop, (VALUE)&execution_args, vm_restore_stack, (VALUE)&execution_args);
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
|
|
815
|
+
static VALUE variable_render(VALUE self, VALUE context)
|
|
816
|
+
{
|
|
817
|
+
if (!native_enabled) {
|
|
818
|
+
return rb_funcall(self, id_ruby_render, 1, context);
|
|
819
|
+
}
|
|
820
|
+
vm_t *vm = vm_from_context(context);
|
|
821
|
+
vm_variable_frame_t *frame = vm->variable_frame;
|
|
822
|
+
if (!frame || frame->variable != self || *frame->nodelist != Qundef) {
|
|
823
|
+
return rb_funcall(self, id_ruby_render, 1, context);
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
vm_stack_reserve_for_write(vm, frame->max_stack_size);
|
|
827
|
+
vm_render_until_error_args_t render_args = {
|
|
828
|
+
.vm = vm,
|
|
829
|
+
.const_ptr = frame->constants,
|
|
830
|
+
.ip = frame->instructions,
|
|
831
|
+
.evaluate_variable = true,
|
|
832
|
+
};
|
|
833
|
+
vm_execution_args_t execution_args = {
|
|
834
|
+
.render_args = &render_args,
|
|
835
|
+
.old_stack_byte_size = c_buffer_size(&vm->stack),
|
|
836
|
+
};
|
|
837
|
+
VALUE result = rb_ensure(vm_evaluate_body, (VALUE)&execution_args, vm_restore_stack, (VALUE)&execution_args);
|
|
838
|
+
vm_refresh_context(vm);
|
|
839
|
+
if (!NIL_P(vm->context.global_filter)) {
|
|
840
|
+
result = rb_funcall(vm->context.global_filter, id_call, 1, result);
|
|
841
|
+
}
|
|
842
|
+
return result;
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
static VALUE variable_render_to_output_buffer(VALUE self, VALUE context, VALUE output)
|
|
846
|
+
{
|
|
847
|
+
if (!native_enabled) {
|
|
848
|
+
return rb_funcall(self, rb_intern("ruby_render_to_output_buffer"), 2, context, output);
|
|
849
|
+
}
|
|
850
|
+
VALUE result = rb_funcall(self, id_render, 1, context);
|
|
851
|
+
rb_funcall(self, id_render_obj_to_output, 2, result, output);
|
|
852
|
+
return output;
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
static VALUE variable_render_obj_to_output(VALUE self, VALUE object, VALUE output)
|
|
856
|
+
{
|
|
857
|
+
if (!native_enabled || !RB_TYPE_P(output, T_STRING) ||
|
|
858
|
+
!liquid_method_basic_definition_p(CLASS_OF(output), rb_intern("<<"))) {
|
|
859
|
+
return rb_funcall(self, rb_intern("ruby_render_obj_to_output"), 2, object, output);
|
|
860
|
+
}
|
|
861
|
+
if (RB_TYPE_P(object, T_ARRAY)) {
|
|
862
|
+
array_writer_args_t args = { self, output };
|
|
863
|
+
return rb_exec_recursive(write_array, object, (VALUE)&args);
|
|
864
|
+
}
|
|
865
|
+
if (NIL_P(object)) {
|
|
866
|
+
return Qnil;
|
|
867
|
+
}
|
|
868
|
+
write_obj(output, object);
|
|
869
|
+
return output;
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
static VALUE vm_invalidate_contexts(VALUE self)
|
|
873
|
+
{
|
|
874
|
+
context_generation++;
|
|
875
|
+
native_enabled = RTEST(rb_ivar_get(mLiquidC, rb_intern("@enabled")));
|
|
876
|
+
return Qnil;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
void liquid_define_vm(void)
|
|
880
|
+
{
|
|
881
|
+
mLiquidUtils = rb_const_get(mLiquid, rb_intern("Utils"));
|
|
882
|
+
rb_global_variable(&mLiquidUtils);
|
|
883
|
+
|
|
884
|
+
id_render = rb_intern("render");
|
|
885
|
+
id_render_to_output_buffer = rb_intern("render_to_output_buffer");
|
|
886
|
+
id_render_obj_to_output = rb_intern("render_obj_to_output");
|
|
887
|
+
id_ruby_render = rb_intern("ruby_render");
|
|
888
|
+
id_each = rb_intern("each");
|
|
889
|
+
rb_define_method(cLiquidVariable, "c_render", variable_render, 1);
|
|
890
|
+
rb_define_method(cLiquidVariable, "c_render_to_output_buffer", variable_render_to_output_buffer, 2);
|
|
891
|
+
rb_define_method(cLiquidVariable, "c_render_obj_to_output", variable_render_obj_to_output, 2);
|
|
892
|
+
|
|
893
|
+
id_render_node = rb_intern("render_node");
|
|
894
|
+
id_vm = rb_intern("vm");
|
|
895
|
+
|
|
896
|
+
cLiquidCVM = rb_define_class_under(mLiquidC, "VM", rb_cObject);
|
|
897
|
+
rb_undef_alloc_func(cLiquidCVM);
|
|
898
|
+
rb_define_singleton_method(cLiquidCVM, "invalidate_contexts", vm_invalidate_contexts, 0);
|
|
899
|
+
rb_define_singleton_method(cLiquidCVM, "reset_context", vm_reset_context, 1);
|
|
900
|
+
rb_global_variable(&cLiquidCVM);
|
|
901
|
+
const char *renderers[] = { "c_render", "c_render_to_output_buffer", "c_render_obj_to_output" };
|
|
902
|
+
for (size_t i = 0; i < 3; i++) {
|
|
903
|
+
portable_renderer_methods[i] = rb_funcall(cLiquidVariable, rb_intern("instance_method"), 1, ID2SYM(rb_intern(renderers[i])));
|
|
904
|
+
rb_global_variable(&portable_renderer_methods[i]);
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
}
|