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,73 @@
|
|
|
1
|
+
#include "liquid.h"
|
|
2
|
+
#include "context.h"
|
|
3
|
+
|
|
4
|
+
static ID id_has_key, id_aref, id_fetch, id_to_liquid_value;
|
|
5
|
+
static ID id_first, id_last, id_ivar_strict_variables;
|
|
6
|
+
|
|
7
|
+
static VALUE string_lookup_first_or_last(VALUE string, VALUE key, ID command)
|
|
8
|
+
{
|
|
9
|
+
VALUE index;
|
|
10
|
+
if (command == id_first && RSTRING_LEN(key) == 5) {
|
|
11
|
+
index = INT2FIX(0);
|
|
12
|
+
} else if (command == id_last && RSTRING_LEN(key) == 4) {
|
|
13
|
+
index = INT2FIX(-1);
|
|
14
|
+
} else {
|
|
15
|
+
return Qundef;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
VALUE character = rb_funcall(string, id_aref, 1, index);
|
|
19
|
+
if (!RTEST(character)) {
|
|
20
|
+
return rb_utf8_str_new_cstr("");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return character;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
VALUE variable_lookup_key(VALUE context, VALUE object, VALUE key, bool is_command)
|
|
27
|
+
{
|
|
28
|
+
if (rb_respond_to(key, id_to_liquid_value))
|
|
29
|
+
key = rb_funcall(key, id_to_liquid_value, 0);
|
|
30
|
+
|
|
31
|
+
if (rb_respond_to(object, id_aref) && (
|
|
32
|
+
(rb_respond_to(object, id_has_key) && RTEST(rb_funcall(object, id_has_key, 1, key))) ||
|
|
33
|
+
(rb_obj_is_kind_of(key, rb_cInteger) && rb_respond_to(object, id_fetch))
|
|
34
|
+
)) {
|
|
35
|
+
if (RTEST(rb_ivar_get(context, id_ivar_strict_variables)) &&
|
|
36
|
+
rb_respond_to(object, id_has_key) && !RTEST(rb_funcall(object, id_has_key, 1, key)))
|
|
37
|
+
context_raise_undefined_variable(key);
|
|
38
|
+
VALUE next_object = rb_funcall(object, id_aref, 1, key);
|
|
39
|
+
next_object = materialize_proc(context, object, key, next_object);
|
|
40
|
+
return nested_value_to_liquid_and_set_context(next_object, context);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (is_command) {
|
|
44
|
+
ID intern_key = rb_to_id(key);
|
|
45
|
+
if (rb_respond_to(object, intern_key)) {
|
|
46
|
+
VALUE next_object = rb_funcall(object, intern_key, 0);
|
|
47
|
+
return nested_value_to_liquid_and_set_context(next_object, context);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (RB_TYPE_P(object, T_STRING) && RB_TYPE_P(key, T_STRING)) {
|
|
51
|
+
VALUE character = string_lookup_first_or_last(object, key, intern_key);
|
|
52
|
+
if (character != Qundef) {
|
|
53
|
+
if (rb_respond_to(character, id_set_context))
|
|
54
|
+
rb_funcall(character, id_set_context, 1, context);
|
|
55
|
+
return character;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
context_maybe_raise_undefined_variable(context, key);
|
|
61
|
+
return Qundef;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
void liquid_define_variable_lookup(void)
|
|
65
|
+
{
|
|
66
|
+
id_has_key = rb_intern("key?");
|
|
67
|
+
id_aref = rb_intern("[]");
|
|
68
|
+
id_fetch = rb_intern("fetch");
|
|
69
|
+
id_to_liquid_value = rb_intern("to_liquid_value");
|
|
70
|
+
id_first = rb_intern("first");
|
|
71
|
+
id_last = rb_intern("last");
|
|
72
|
+
id_ivar_strict_variables = rb_intern("@strict_variables");
|
|
73
|
+
}
|
|
@@ -0,0 +1,631 @@
|
|
|
1
|
+
#include "liquid.h"
|
|
2
|
+
#include "vm_assembler.h"
|
|
3
|
+
#include "expression.h"
|
|
4
|
+
#include "liquid_vm.h"
|
|
5
|
+
|
|
6
|
+
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
|
|
7
|
+
|
|
8
|
+
static st_table *builtin_filter_table;
|
|
9
|
+
|
|
10
|
+
#ifdef LIQUID_C_TEST_ALLOC_FAILURES
|
|
11
|
+
static long allocation_failure_after = -1;
|
|
12
|
+
|
|
13
|
+
void vm_assembler_allocation_checkpoint(void)
|
|
14
|
+
{
|
|
15
|
+
if (allocation_failure_after == 0) rb_raise(rb_eNoMemError, "injected assembler allocation failure");
|
|
16
|
+
if (allocation_failure_after > 0) allocation_failure_after--;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
static VALUE test_fail_assembler_allocation(VALUE self, VALUE after)
|
|
20
|
+
{
|
|
21
|
+
allocation_failure_after = NUM2LONG(after);
|
|
22
|
+
return Qnil;
|
|
23
|
+
}
|
|
24
|
+
#endif
|
|
25
|
+
|
|
26
|
+
// methods from Liquid::StandardFilters
|
|
27
|
+
filter_desc_t builtin_filters[] = {
|
|
28
|
+
{ .name = "size" },
|
|
29
|
+
{ .name = "downcase" },
|
|
30
|
+
{ .name = "upcase" },
|
|
31
|
+
{ .name = "capitalize" },
|
|
32
|
+
{ .name = "h" },
|
|
33
|
+
{ .name = "escape" },
|
|
34
|
+
{ .name = "escape_once" },
|
|
35
|
+
{ .name = "url_encode" },
|
|
36
|
+
{ .name = "url_decode" },
|
|
37
|
+
{ .name = "slice" },
|
|
38
|
+
{ .name = "truncate" },
|
|
39
|
+
{ .name = "truncatewords" },
|
|
40
|
+
{ .name = "split" },
|
|
41
|
+
{ .name = "strip" },
|
|
42
|
+
{ .name = "lstrip" },
|
|
43
|
+
{ .name = "rstrip" },
|
|
44
|
+
{ .name = "strip_html" },
|
|
45
|
+
{ .name = "strip_newlines" },
|
|
46
|
+
{ .name = "join" },
|
|
47
|
+
{ .name = "sort" },
|
|
48
|
+
{ .name = "sort_natural" },
|
|
49
|
+
{ .name = "where" },
|
|
50
|
+
{ .name = "uniq" },
|
|
51
|
+
{ .name = "reverse" },
|
|
52
|
+
{ .name = "map" },
|
|
53
|
+
{ .name = "compact" },
|
|
54
|
+
{ .name = "replace" },
|
|
55
|
+
{ .name = "replace_first" },
|
|
56
|
+
{ .name = "remove" },
|
|
57
|
+
{ .name = "remove_first" },
|
|
58
|
+
{ .name = "append" },
|
|
59
|
+
{ .name = "concat" },
|
|
60
|
+
{ .name = "prepend" },
|
|
61
|
+
{ .name = "newline_to_br" },
|
|
62
|
+
{ .name = "date" },
|
|
63
|
+
{ .name = "first" },
|
|
64
|
+
{ .name = "last" },
|
|
65
|
+
{ .name = "abs" },
|
|
66
|
+
{ .name = "plus" },
|
|
67
|
+
{ .name = "minus" },
|
|
68
|
+
{ .name = "times" },
|
|
69
|
+
{ .name = "divided_by" },
|
|
70
|
+
{ .name = "modulo" },
|
|
71
|
+
{ .name = "round" },
|
|
72
|
+
{ .name = "ceil" },
|
|
73
|
+
{ .name = "floor" },
|
|
74
|
+
{ .name = "at_least" },
|
|
75
|
+
{ .name = "at_most" },
|
|
76
|
+
{ .name = "default" },
|
|
77
|
+
};
|
|
78
|
+
static_assert(ARRAY_LENGTH(builtin_filters) < 256,
|
|
79
|
+
"support for larger than byte sized indexing of filters has not yet been implemented");
|
|
80
|
+
|
|
81
|
+
static void vm_assembler_common_init(vm_assembler_t *code)
|
|
82
|
+
{
|
|
83
|
+
code->max_stack_size = 0;
|
|
84
|
+
code->stack_size = 0;
|
|
85
|
+
code->protected_stack_size = 0;
|
|
86
|
+
code->parsing = true;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
void vm_assembler_init(vm_assembler_t *code)
|
|
90
|
+
{
|
|
91
|
+
memset(code, 0, sizeof(*code));
|
|
92
|
+
vm_assembler_allocation_checkpoint();
|
|
93
|
+
code->instructions = c_buffer_allocate(8);
|
|
94
|
+
vm_assembler_allocation_checkpoint();
|
|
95
|
+
code->constants = c_buffer_allocate(8 * sizeof(VALUE));
|
|
96
|
+
vm_assembler_allocation_checkpoint();
|
|
97
|
+
code->constants_table = st_init_numtable();
|
|
98
|
+
vm_assembler_common_init(code);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
void vm_assembler_reset(vm_assembler_t *code)
|
|
102
|
+
{
|
|
103
|
+
c_buffer_reset(&code->instructions);
|
|
104
|
+
c_buffer_reset(&code->constants);
|
|
105
|
+
st_clear(code->constants_table);
|
|
106
|
+
vm_assembler_common_init(code);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
void vm_assembler_free(vm_assembler_t *code)
|
|
110
|
+
{
|
|
111
|
+
c_buffer_free(&code->instructions);
|
|
112
|
+
c_buffer_free(&code->constants);
|
|
113
|
+
if (code->constants_table) st_free_table(code->constants_table);
|
|
114
|
+
code->constants_table = NULL;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
void vm_assembler_gc_mark(vm_assembler_t *code)
|
|
118
|
+
{
|
|
119
|
+
c_buffer_rb_gc_mark(&code->constants);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
VALUE vm_assembler_disassemble(const uint8_t *start_ip, const uint8_t *end_ip, const VALUE *constants, size_t constants_offset)
|
|
123
|
+
{
|
|
124
|
+
const uint8_t *ip = start_ip;
|
|
125
|
+
VALUE output = rb_str_buf_new(32);
|
|
126
|
+
VALUE constant = Qnil;
|
|
127
|
+
|
|
128
|
+
while (ip < end_ip) {
|
|
129
|
+
rb_str_catf(output, "0x%04lx: ", ip - start_ip);
|
|
130
|
+
|
|
131
|
+
if (vm_assembler_opcode_has_constant(*ip)) {
|
|
132
|
+
uint32_t constant_index = bytes_to_uint32(ip + 1);
|
|
133
|
+
constant = RARRAY_AREF(*constants, constants_offset + constant_index);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
switch (*ip) {
|
|
137
|
+
case OP_LEAVE:
|
|
138
|
+
rb_str_catf(output, "leave\n");
|
|
139
|
+
break;
|
|
140
|
+
|
|
141
|
+
case OP_POP_WRITE:
|
|
142
|
+
rb_str_catf(output, "pop_write\n");
|
|
143
|
+
break;
|
|
144
|
+
|
|
145
|
+
case OP_PUSH_NIL:
|
|
146
|
+
rb_str_catf(output, "push_nil\n");
|
|
147
|
+
break;
|
|
148
|
+
|
|
149
|
+
case OP_PUSH_TRUE:
|
|
150
|
+
rb_str_catf(output, "push_true\n");
|
|
151
|
+
break;
|
|
152
|
+
|
|
153
|
+
case OP_PUSH_FALSE:
|
|
154
|
+
rb_str_catf(output, "push_false\n");
|
|
155
|
+
break;
|
|
156
|
+
|
|
157
|
+
case OP_FIND_VAR:
|
|
158
|
+
rb_str_catf(output, "find_var\n");
|
|
159
|
+
break;
|
|
160
|
+
|
|
161
|
+
case OP_LOOKUP_KEY:
|
|
162
|
+
rb_str_catf(output, "lookup_key\n");
|
|
163
|
+
break;
|
|
164
|
+
|
|
165
|
+
case OP_LOOKUP_GUARD:
|
|
166
|
+
rb_str_catf(output, "lookup_guard(%u)\n", bytes_to_uint32(ip + 1));
|
|
167
|
+
break;
|
|
168
|
+
|
|
169
|
+
case OP_NEW_INT_RANGE:
|
|
170
|
+
rb_str_catf(output, "new_int_range\n");
|
|
171
|
+
break;
|
|
172
|
+
|
|
173
|
+
case OP_HASH_NEW:
|
|
174
|
+
rb_str_catf(output, "hash_new(%u)\n", ip[1]);
|
|
175
|
+
break;
|
|
176
|
+
|
|
177
|
+
case OP_PUSH_INT8:
|
|
178
|
+
rb_str_catf(output, "push_int8(%u)\n", ip[1]);
|
|
179
|
+
break;
|
|
180
|
+
|
|
181
|
+
case OP_PUSH_INT16:
|
|
182
|
+
{
|
|
183
|
+
int num = (ip[1] << 8) | ip[2];
|
|
184
|
+
rb_str_catf(output, "push_int16(%u)\n", num);
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
case OP_RENDER_VARIABLE_RESCUE:
|
|
189
|
+
{
|
|
190
|
+
unsigned int line_number = bytes_to_uint24(ip + 1);
|
|
191
|
+
rb_str_catf(output, "render_variable_rescue(line_number: %u)\n", line_number);
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
case OP_RENDER_VARIABLE_RESCUE_W:
|
|
195
|
+
rb_str_catf(output, "render_variable_rescue(line_number: %"PRIsVALUE")\n", constant);
|
|
196
|
+
break;
|
|
197
|
+
|
|
198
|
+
case OP_WRITE_RAW_W:
|
|
199
|
+
case OP_WRITE_RAW:
|
|
200
|
+
{
|
|
201
|
+
const char *text;
|
|
202
|
+
size_t size;
|
|
203
|
+
const char *name;
|
|
204
|
+
if (*ip == OP_WRITE_RAW_W) {
|
|
205
|
+
name = "write_raw_w";
|
|
206
|
+
size = bytes_to_uint24(&ip[1]);
|
|
207
|
+
text = (const char *)&ip[4];
|
|
208
|
+
} else {
|
|
209
|
+
name = "write_raw";
|
|
210
|
+
size = ip[1];
|
|
211
|
+
text = (const char *)&ip[2];
|
|
212
|
+
}
|
|
213
|
+
VALUE string = rb_enc_str_new(text, size, utf8_encoding);
|
|
214
|
+
rb_str_catf(output, "%s(%+"PRIsVALUE")\n", name, string);
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
case OP_WRITE_NODE:
|
|
219
|
+
rb_str_catf(output, "write_node(%+"PRIsVALUE")\n", constant);
|
|
220
|
+
break;
|
|
221
|
+
|
|
222
|
+
case OP_PUSH_CONST:
|
|
223
|
+
rb_str_catf(output, "push_const(%+"PRIsVALUE")\n", constant);
|
|
224
|
+
break;
|
|
225
|
+
|
|
226
|
+
case OP_EVALUATE:
|
|
227
|
+
rb_str_catf(output, "evaluate(%+"PRIsVALUE")\n", constant);
|
|
228
|
+
break;
|
|
229
|
+
|
|
230
|
+
case OP_FIND_STATIC_VAR:
|
|
231
|
+
rb_str_catf(output, "find_static_var(%+"PRIsVALUE")\n", constant);
|
|
232
|
+
break;
|
|
233
|
+
|
|
234
|
+
case OP_LOOKUP_CONST_KEY:
|
|
235
|
+
rb_str_catf(output, "lookup_const_key(%+"PRIsVALUE")\n", constant);
|
|
236
|
+
break;
|
|
237
|
+
|
|
238
|
+
case OP_LOOKUP_COMMAND:
|
|
239
|
+
rb_str_catf(output, "lookup_command(%+"PRIsVALUE")\n", constant);
|
|
240
|
+
break;
|
|
241
|
+
|
|
242
|
+
case OP_FILTER:
|
|
243
|
+
{
|
|
244
|
+
VALUE filter_name = RARRAY_AREF(constant, 0);
|
|
245
|
+
unsigned long num_args = NUM2ULONG(RARRAY_AREF(constant, 1));
|
|
246
|
+
rb_str_catf(output, "filter(name: %+"PRIsVALUE", num_args: %lu)\n", filter_name, num_args);
|
|
247
|
+
break;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
case OP_BUILTIN_FILTER:
|
|
251
|
+
rb_str_catf(output, "builtin_filter(name: :%s, num_args: %u)\n", builtin_filters[ip[1]].name, ip[2]);
|
|
252
|
+
break;
|
|
253
|
+
|
|
254
|
+
default:
|
|
255
|
+
rb_str_catf(output, "<opcode number %d disassembly not implemented>\n", ip[0]);
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
liquid_vm_next_instruction(&ip);
|
|
259
|
+
}
|
|
260
|
+
return output;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
struct merge_constants_table_func_args {
|
|
264
|
+
st_table *hash;
|
|
265
|
+
size_t increment_amount;
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
static int merge_constants_table(st_data_t key, st_data_t value, VALUE _arg)
|
|
269
|
+
{
|
|
270
|
+
struct merge_constants_table_func_args *arg = (struct merge_constants_table_func_args *)_arg;
|
|
271
|
+
st_table *dest_hash = arg->hash;
|
|
272
|
+
if (value > UINT32_MAX || arg->increment_amount > UINT32_MAX - value) {
|
|
273
|
+
rb_raise(rb_eRangeError, "Too many bytecode constants");
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
st_insert(dest_hash, key, value + arg->increment_amount);
|
|
277
|
+
|
|
278
|
+
return ST_CONTINUE;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
static void offset_constant_index(uint8_t *index_bytes, size_t increment_amount)
|
|
282
|
+
{
|
|
283
|
+
uint32_t constant_index = bytes_to_uint32(index_bytes);
|
|
284
|
+
if (increment_amount > UINT32_MAX - constant_index) {
|
|
285
|
+
rb_raise(rb_eRangeError, "Too many bytecode constants");
|
|
286
|
+
}
|
|
287
|
+
size_t new_constant_index = constant_index + increment_amount;
|
|
288
|
+
uint32_to_bytes((uint32_t)new_constant_index, index_bytes);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
void update_instructions_constants_table_index_ref(c_buffer_t *instructions, size_t increment_amount, c_buffer_t *constants)
|
|
292
|
+
{
|
|
293
|
+
uint8_t *ip = instructions->data;
|
|
294
|
+
|
|
295
|
+
while (ip < instructions->data_end) {
|
|
296
|
+
if (vm_assembler_opcode_has_constant(*ip)) {
|
|
297
|
+
offset_constant_index(ip + 1, increment_amount);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (*ip == OP_RENDER_VARIABLE_RESCUE) {
|
|
301
|
+
offset_constant_index(ip + 1 + VM_VARIABLE_LINE_NUMBER_SIZE, increment_amount);
|
|
302
|
+
}
|
|
303
|
+
if (*ip == OP_RENDER_VARIABLE_RESCUE_W)
|
|
304
|
+
offset_constant_index(ip + 5, increment_amount);
|
|
305
|
+
|
|
306
|
+
liquid_vm_next_instruction((const uint8_t **)&ip);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
void vm_assembler_concat(vm_assembler_t *dest, const vm_assembler_t *src)
|
|
311
|
+
{
|
|
312
|
+
size_t dest_element_count = c_buffer_size(&dest->constants) / sizeof(VALUE);
|
|
313
|
+
|
|
314
|
+
// merge src constants table into dest constants table with new index
|
|
315
|
+
struct merge_constants_table_func_args arg;
|
|
316
|
+
arg.hash = dest->constants_table;
|
|
317
|
+
arg.increment_amount = dest_element_count;
|
|
318
|
+
st_foreach(src->constants_table, merge_constants_table, (VALUE)&arg);
|
|
319
|
+
|
|
320
|
+
// merge constants array
|
|
321
|
+
c_buffer_concat(&dest->constants, &src->constants);
|
|
322
|
+
|
|
323
|
+
size_t offset = c_buffer_size(&dest->instructions);
|
|
324
|
+
c_buffer_concat(&dest->instructions, &src->instructions);
|
|
325
|
+
c_buffer_t appended = dest->instructions;
|
|
326
|
+
appended.data += offset;
|
|
327
|
+
update_instructions_constants_table_index_ref(&appended, dest_element_count, &dest->constants);
|
|
328
|
+
|
|
329
|
+
if (src->max_stack_size > SIZE_MAX - dest->stack_size || src->stack_size > SIZE_MAX - dest->stack_size)
|
|
330
|
+
rb_raise(rb_eNoMemError, "Liquid stack size overflow");
|
|
331
|
+
size_t max_src_stack_size = dest->stack_size + src->max_stack_size;
|
|
332
|
+
if (max_src_stack_size > dest->max_stack_size)
|
|
333
|
+
dest->max_stack_size = max_src_stack_size;
|
|
334
|
+
|
|
335
|
+
dest->stack_size += src->stack_size;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
size_t vm_assembler_add_lookup_guard(vm_assembler_t *code)
|
|
339
|
+
{
|
|
340
|
+
size_t offset = c_buffer_size(&code->instructions);
|
|
341
|
+
uint8_t *ip = c_buffer_extend_for_write(&code->instructions, 5);
|
|
342
|
+
ip[0] = OP_LOOKUP_GUARD;
|
|
343
|
+
uint32_to_bytes(0, ip + 1);
|
|
344
|
+
return offset;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
void vm_assembler_patch_lookup_guard(vm_assembler_t *code, size_t offset)
|
|
348
|
+
{
|
|
349
|
+
size_t size = c_buffer_size(&code->instructions);
|
|
350
|
+
if (offset > size || size - offset < 5 || code->instructions.data[offset] != OP_LOOKUP_GUARD)
|
|
351
|
+
rb_raise(rb_eArgError, "Invalid lookup guard offset");
|
|
352
|
+
size_t distance = size - offset - 5;
|
|
353
|
+
if (distance > UINT32_MAX)
|
|
354
|
+
rb_raise(rb_eRangeError, "Lookup chain too large");
|
|
355
|
+
uint32_to_bytes((uint32_t)distance, code->instructions.data + offset + 1);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
static size_t blank_raw_bytes(const uint8_t *ip)
|
|
359
|
+
{
|
|
360
|
+
if (*ip == OP_WRITE_RAW) return (size_t)ip[1] + 2;
|
|
361
|
+
if (*ip == OP_WRITE_RAW_W) return (size_t)bytes_to_uint24(ip + 1) + 4;
|
|
362
|
+
return 0;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
size_t vm_assembler_remove_blank_strings(vm_assembler_t *code)
|
|
366
|
+
{
|
|
367
|
+
uint8_t *read = code->instructions.data;
|
|
368
|
+
uint8_t *write = read;
|
|
369
|
+
uint8_t *end = code->instructions.data_end;
|
|
370
|
+
size_t removed_nodes = 0;
|
|
371
|
+
while (read < end) {
|
|
372
|
+
const uint8_t *next = read;
|
|
373
|
+
liquid_vm_next_instruction(&next);
|
|
374
|
+
size_t raw_bytes = blank_raw_bytes(read);
|
|
375
|
+
if (raw_bytes) {
|
|
376
|
+
if (raw_bytes > (*read == OP_WRITE_RAW ? 2U : 4U)) removed_nodes++;
|
|
377
|
+
read = (uint8_t *)next;
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
size_t size = (size_t)(next - read);
|
|
381
|
+
if (*read == OP_LOOKUP_GUARD) {
|
|
382
|
+
uint32_t distance = bytes_to_uint32(read + 1);
|
|
383
|
+
const uint8_t *target = next + distance;
|
|
384
|
+
size_t removed = 0;
|
|
385
|
+
for (const uint8_t *scan = next; scan < target;) {
|
|
386
|
+
const uint8_t *following = scan;
|
|
387
|
+
liquid_vm_next_instruction(&following);
|
|
388
|
+
if (blank_raw_bytes(scan)) removed += (size_t)(following - scan);
|
|
389
|
+
scan = following;
|
|
390
|
+
}
|
|
391
|
+
distance -= (uint32_t)removed;
|
|
392
|
+
uint32_to_bytes(distance, read + 1);
|
|
393
|
+
}
|
|
394
|
+
memmove(write, read, size);
|
|
395
|
+
write += size;
|
|
396
|
+
read = (uint8_t *)next;
|
|
397
|
+
}
|
|
398
|
+
code->instructions.data_end = write;
|
|
399
|
+
return removed_nodes;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
void vm_assembler_require_stack_args(vm_assembler_t *code, unsigned int count)
|
|
403
|
+
{
|
|
404
|
+
if (code->stack_size < code->protected_stack_size || code->stack_size - code->protected_stack_size < count) {
|
|
405
|
+
rb_raise(rb_eRuntimeError, "insufficient number of values on the stack");
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
void vm_assembler_add_write_raw(vm_assembler_t *code, const char *string, size_t size)
|
|
411
|
+
{
|
|
412
|
+
if (size > UINT8_MAX) {
|
|
413
|
+
uint8_t *instructions = c_buffer_extend_for_write(&code->instructions, 4);
|
|
414
|
+
instructions[0] = OP_WRITE_RAW_W;
|
|
415
|
+
uint24_to_bytes((unsigned int)size, &instructions[1]);
|
|
416
|
+
} else {
|
|
417
|
+
uint8_t *instructions = c_buffer_extend_for_write(&code->instructions, 2);
|
|
418
|
+
instructions[0] = OP_WRITE_RAW;
|
|
419
|
+
instructions[1] = size;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
c_buffer_write(&code->instructions, (char *)string, size);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
void vm_assembler_add_write_raw_value(vm_assembler_t *code, VALUE string)
|
|
426
|
+
{
|
|
427
|
+
size_t size = (size_t)RSTRING_LEN(string);
|
|
428
|
+
if (!size) return;
|
|
429
|
+
if (size >= (1U << 24)) rb_raise(rb_eRangeError, "Raw body too large");
|
|
430
|
+
// Reserve before taking a pointer into an embedded Ruby string: allocation
|
|
431
|
+
// may compact the heap, but the subsequent writes cannot allocate.
|
|
432
|
+
c_buffer_reserve_for_write(&code->instructions, size + 4);
|
|
433
|
+
vm_assembler_add_write_raw(code, RSTRING_PTR(string), size);
|
|
434
|
+
RB_GC_GUARD(string);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
void vm_assembler_add_write_node(vm_assembler_t *code, VALUE node)
|
|
438
|
+
{
|
|
439
|
+
vm_assembler_add_op_with_constant(code, node, OP_WRITE_NODE);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
void vm_assembler_add_push_fixnum(vm_assembler_t *code, VALUE num)
|
|
443
|
+
{
|
|
444
|
+
long x = FIX2LONG(num);
|
|
445
|
+
if (x >= INT8_MIN && x <= INT8_MAX) {
|
|
446
|
+
vm_assembler_add_push_int8(code, x);
|
|
447
|
+
} else if (x >= INT16_MIN && x <= INT16_MAX) {
|
|
448
|
+
vm_assembler_add_push_int16(code, x);
|
|
449
|
+
} else {
|
|
450
|
+
vm_assembler_add_push_const(code, num);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
void vm_assembler_add_push_literal(vm_assembler_t *code, VALUE literal)
|
|
455
|
+
{
|
|
456
|
+
switch (literal) {
|
|
457
|
+
case Qnil:
|
|
458
|
+
vm_assembler_add_push_nil(code);
|
|
459
|
+
break;
|
|
460
|
+
case Qtrue:
|
|
461
|
+
vm_assembler_add_push_true(code);
|
|
462
|
+
break;
|
|
463
|
+
case Qfalse:
|
|
464
|
+
vm_assembler_add_push_false(code);
|
|
465
|
+
break;
|
|
466
|
+
default:
|
|
467
|
+
if (RB_FIXNUM_P(literal)) {
|
|
468
|
+
vm_assembler_add_push_fixnum(code, literal);
|
|
469
|
+
} else {
|
|
470
|
+
vm_assembler_add_push_const(code, literal);
|
|
471
|
+
}
|
|
472
|
+
break;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
void vm_assembler_add_filter(vm_assembler_t *code, VALUE filter_name, size_t arg_count)
|
|
477
|
+
{
|
|
478
|
+
if (arg_count > 254) {
|
|
479
|
+
rb_enc_raise(utf8_encoding, cLiquidSyntaxError, "Too many filter arguments");
|
|
480
|
+
}
|
|
481
|
+
code->stack_size -= arg_count; // pop arg_count + 1, push 1
|
|
482
|
+
|
|
483
|
+
st_data_t builtin_index;
|
|
484
|
+
bool is_builtin = st_lookup(builtin_filter_table, filter_name, &builtin_index);
|
|
485
|
+
|
|
486
|
+
if (is_builtin) {
|
|
487
|
+
uint8_t *instructions = c_buffer_extend_for_write(&code->instructions, 3);
|
|
488
|
+
*instructions++ = OP_BUILTIN_FILTER;
|
|
489
|
+
*instructions++ = builtin_index;
|
|
490
|
+
*instructions++ = arg_count + 1; // include input
|
|
491
|
+
} else {
|
|
492
|
+
VALUE filter_args = rb_ary_new_capa(2);
|
|
493
|
+
rb_ary_push(filter_args, filter_name);
|
|
494
|
+
rb_ary_push(filter_args, LONG2FIX((long)(arg_count + 1)));
|
|
495
|
+
vm_assembler_add_op_with_constant(code, filter_args, OP_FILTER);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
static void ensure_parsing(vm_assembler_t *code)
|
|
500
|
+
{
|
|
501
|
+
if (!code || !code->parsing)
|
|
502
|
+
rb_raise(rb_eRuntimeError, "cannot extend code after it has finished being compiled");
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
void vm_assembler_add_evaluate_expression_from_ruby(vm_assembler_t *code, VALUE code_obj, VALUE expression)
|
|
506
|
+
{
|
|
507
|
+
ensure_parsing(code);
|
|
508
|
+
|
|
509
|
+
if (RB_SPECIAL_CONST_P(expression)) {
|
|
510
|
+
vm_assembler_add_push_literal(code, expression);
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
switch (RB_BUILTIN_TYPE(expression)) {
|
|
515
|
+
case T_DATA:
|
|
516
|
+
if (RBASIC_CLASS(expression) == cLiquidCExpression) {
|
|
517
|
+
vm_assembler_concat(code, &((expression_t *)DATA_PTR(expression))->code);
|
|
518
|
+
vm_assembler_remove_leave(code);
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
break;
|
|
522
|
+
case T_OBJECT:
|
|
523
|
+
{
|
|
524
|
+
VALUE klass = RBASIC_CLASS(expression);
|
|
525
|
+
if (klass == cLiquidVariableLookup || klass == cLiquidRangeLookup) {
|
|
526
|
+
rb_funcall(expression, id_compile_evaluate, 1, code_obj);
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
break;
|
|
530
|
+
}
|
|
531
|
+
default:
|
|
532
|
+
break;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
if (rb_respond_to(expression, id_evaluate)) {
|
|
536
|
+
vm_assembler_increment_stack_size(code, 1);
|
|
537
|
+
vm_assembler_add_op_with_constant(code, expression, OP_EVALUATE);
|
|
538
|
+
} else {
|
|
539
|
+
vm_assembler_add_push_const(code, expression);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
void vm_assembler_add_find_variable_from_ruby(vm_assembler_t *code, VALUE code_obj, VALUE expression)
|
|
544
|
+
{
|
|
545
|
+
ensure_parsing(code);
|
|
546
|
+
|
|
547
|
+
if (RB_TYPE_P(expression, T_STRING)) {
|
|
548
|
+
vm_assembler_add_find_static_variable(code, expression);
|
|
549
|
+
} else {
|
|
550
|
+
vm_assembler_add_evaluate_expression_from_ruby(code, code_obj, expression);
|
|
551
|
+
vm_assembler_add_find_variable(code);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
void vm_assembler_add_lookup_command_from_ruby(vm_assembler_t *code, VALUE command)
|
|
556
|
+
{
|
|
557
|
+
StringValue(command);
|
|
558
|
+
ensure_parsing(code);
|
|
559
|
+
vm_assembler_require_stack_args(code, 1);
|
|
560
|
+
|
|
561
|
+
vm_assembler_add_lookup_command(code, command);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
void vm_assembler_add_lookup_key_from_ruby(vm_assembler_t *code, VALUE code_obj, VALUE expression)
|
|
565
|
+
{
|
|
566
|
+
ensure_parsing(code);
|
|
567
|
+
vm_assembler_require_stack_args(code, 1);
|
|
568
|
+
|
|
569
|
+
if (RB_TYPE_P(expression, T_STRING)) {
|
|
570
|
+
vm_assembler_add_lookup_const_key(code, expression);
|
|
571
|
+
} else {
|
|
572
|
+
vm_assembler_add_evaluate_expression_from_ruby(code, code_obj, expression);
|
|
573
|
+
vm_assembler_add_lookup_key(code);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
void vm_assembler_add_new_int_range_from_ruby(vm_assembler_t *code)
|
|
578
|
+
{
|
|
579
|
+
ensure_parsing(code);
|
|
580
|
+
vm_assembler_require_stack_args(code, 2);
|
|
581
|
+
vm_assembler_add_new_int_range(code);
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
void vm_assembler_add_hash_new_from_ruby(vm_assembler_t *code, VALUE hash_size_obj)
|
|
585
|
+
{
|
|
586
|
+
ensure_parsing(code);
|
|
587
|
+
unsigned int hash_size = NUM2USHORT(hash_size_obj);
|
|
588
|
+
vm_assembler_require_stack_args(code, hash_size * 2);
|
|
589
|
+
|
|
590
|
+
vm_assembler_add_hash_new(code, hash_size);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
void vm_assembler_add_filter_from_ruby(vm_assembler_t *code, VALUE filter_name, VALUE arg_count_obj)
|
|
594
|
+
{
|
|
595
|
+
StringValue(filter_name);
|
|
596
|
+
ensure_parsing(code);
|
|
597
|
+
unsigned int arg_count = NUM2USHORT(arg_count_obj);
|
|
598
|
+
vm_assembler_require_stack_args(code, arg_count + 1);
|
|
599
|
+
filter_name = rb_str_intern(filter_name);
|
|
600
|
+
|
|
601
|
+
vm_assembler_add_filter(code, filter_name, arg_count);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
bool vm_assembler_opcode_has_constant(uint8_t ip) {
|
|
605
|
+
if (
|
|
606
|
+
ip == OP_PUSH_CONST ||
|
|
607
|
+
ip == OP_RENDER_VARIABLE_RESCUE_W ||
|
|
608
|
+
ip == OP_EVALUATE ||
|
|
609
|
+
ip == OP_WRITE_NODE ||
|
|
610
|
+
ip == OP_FIND_STATIC_VAR ||
|
|
611
|
+
ip == OP_LOOKUP_CONST_KEY ||
|
|
612
|
+
ip == OP_LOOKUP_COMMAND ||
|
|
613
|
+
ip == OP_FILTER
|
|
614
|
+
) {
|
|
615
|
+
return true;
|
|
616
|
+
}
|
|
617
|
+
return false;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
void liquid_define_vm_assembler(void)
|
|
621
|
+
{
|
|
622
|
+
#ifdef LIQUID_C_TEST_ALLOC_FAILURES
|
|
623
|
+
rb_define_singleton_method(mLiquidC, "test_fail_assembler_allocation", test_fail_assembler_allocation, 1);
|
|
624
|
+
#endif
|
|
625
|
+
builtin_filter_table = st_init_numtable_with_size(ARRAY_LENGTH(builtin_filters));
|
|
626
|
+
for (unsigned int i = 0; i < ARRAY_LENGTH(builtin_filters); i++) {
|
|
627
|
+
filter_desc_t *filter = &builtin_filters[i];
|
|
628
|
+
filter->sym = ID2SYM(rb_intern(filter->name));
|
|
629
|
+
st_insert(builtin_filter_table, filter->sym, i);
|
|
630
|
+
}
|
|
631
|
+
}
|