liquid5-c 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +22 -0
  3. data/README.md +164 -0
  4. data/ext/liquid_c/block.c +970 -0
  5. data/ext/liquid_c/block.h +37 -0
  6. data/ext/liquid_c/c_buffer.c +48 -0
  7. data/ext/liquid_c/c_buffer.h +82 -0
  8. data/ext/liquid_c/context.c +262 -0
  9. data/ext/liquid_c/context.h +72 -0
  10. data/ext/liquid_c/document_body.c +181 -0
  11. data/ext/liquid_c/document_body.h +84 -0
  12. data/ext/liquid_c/expression.c +130 -0
  13. data/ext/liquid_c/expression.h +24 -0
  14. data/ext/liquid_c/extconf.rb +60 -0
  15. data/ext/liquid_c/intutil.h +36 -0
  16. data/ext/liquid_c/lexer.c +172 -0
  17. data/ext/liquid_c/lexer.h +61 -0
  18. data/ext/liquid_c/liquid.c +182 -0
  19. data/ext/liquid_c/liquid.h +112 -0
  20. data/ext/liquid_c/liquid_vm.c +907 -0
  21. data/ext/liquid_c/liquid_vm.h +35 -0
  22. data/ext/liquid_c/mri_compat.h +35 -0
  23. data/ext/liquid_c/native_parser.c +2360 -0
  24. data/ext/liquid_c/native_value.c +49 -0
  25. data/ext/liquid_c/parse_context.c +84 -0
  26. data/ext/liquid_c/parse_context.h +13 -0
  27. data/ext/liquid_c/parser.c +285 -0
  28. data/ext/liquid_c/parser.h +25 -0
  29. data/ext/liquid_c/raw.c +136 -0
  30. data/ext/liquid_c/raw.h +6 -0
  31. data/ext/liquid_c/resource_limits.c +459 -0
  32. data/ext/liquid_c/resource_limits.h +28 -0
  33. data/ext/liquid_c/stringutil.h +37 -0
  34. data/ext/liquid_c/tokenizer.c +326 -0
  35. data/ext/liquid_c/tokenizer.h +46 -0
  36. data/ext/liquid_c/usage.c +18 -0
  37. data/ext/liquid_c/usage.h +9 -0
  38. data/ext/liquid_c/variable.c +198 -0
  39. data/ext/liquid_c/variable.h +61 -0
  40. data/ext/liquid_c/variable_lookup.c +73 -0
  41. data/ext/liquid_c/variable_lookup.h +8 -0
  42. data/ext/liquid_c/vm_assembler.c +631 -0
  43. data/ext/liquid_c/vm_assembler.h +294 -0
  44. data/ext/liquid_c/vm_assembler_pool.c +117 -0
  45. data/ext/liquid_c/vm_assembler_pool.h +27 -0
  46. data/lib/liquid/c/compile_ext.rb +47 -0
  47. data/lib/liquid/c/native_compiler.rb +262 -0
  48. data/lib/liquid/c/native_parser.rb +261 -0
  49. data/lib/liquid/c/native_tags.rb +103 -0
  50. data/lib/liquid/c/native_values.rb +9 -0
  51. data/lib/liquid/c/version.rb +7 -0
  52. data/lib/liquid/c.rb +379 -0
  53. metadata +140 -0
@@ -0,0 +1,182 @@
1
+ #include "liquid.h"
2
+ #include "tokenizer.h"
3
+ #include "variable.h"
4
+ #include "lexer.h"
5
+ #include "parser.h"
6
+ #include "raw.h"
7
+ #include "resource_limits.h"
8
+ #include "expression.h"
9
+ #include "document_body.h"
10
+ #include "block.h"
11
+ #include "context.h"
12
+ #include "parse_context.h"
13
+ #include "variable_lookup.h"
14
+ #include "vm_assembler_pool.h"
15
+ #include "liquid_vm.h"
16
+ #include "usage.h"
17
+
18
+ ID id_evaluate;
19
+ ID id_to_liquid;
20
+ ID id_to_s;
21
+ ID id_call;
22
+ ID id_compile_evaluate;
23
+ ID id_ivar_line_number;
24
+
25
+ VALUE mLiquid, mLiquidC, cLiquidVariable, cLiquidTemplate, cLiquidBlockBody;
26
+ VALUE cLiquidVariableLookup, cLiquidRangeLookup;
27
+ VALUE cLiquidArgumentError, cLiquidSyntaxError, cMemoryError;
28
+
29
+ rb_encoding *utf8_encoding;
30
+ int utf8_encoding_index;
31
+
32
+ #ifndef HAVE_RB_METHOD_BASIC_DEFINITION_P
33
+ static VALUE portable_instance_method, portable_source_location, portable_original_name;
34
+
35
+ static VALUE portable_method_reader(VALUE owner, const char *name)
36
+ {
37
+ VALUE method = rb_funcall(owner, rb_intern("instance_method"), 1, ID2SYM(rb_intern(name)));
38
+ VALUE reader = liquid_method_handle(rb_obj_method(method, ID2SYM(rb_intern("bind_call"))));
39
+ RB_GC_GUARD(method);
40
+ return reader;
41
+ }
42
+
43
+ static void portable_method_readers_init(void)
44
+ {
45
+ rb_global_variable(&portable_instance_method);
46
+ rb_global_variable(&portable_source_location);
47
+ rb_global_variable(&portable_original_name);
48
+ portable_instance_method = portable_method_reader(rb_cModule, "instance_method");
49
+ VALUE unbound_class = rb_obj_class(rb_funcall(rb_cModule, rb_intern("instance_method"), 1, ID2SYM(rb_intern("autoload?"))));
50
+ portable_source_location = portable_method_reader(unbound_class, "source_location");
51
+ portable_original_name = portable_method_reader(unbound_class, "original_name");
52
+ }
53
+
54
+ bool liquid_method_basic_definition_p(VALUE klass, ID method)
55
+ {
56
+ if (!rb_method_boundp(klass, method, 0)) return false;
57
+ VALUE args[] = { klass, ID2SYM(method) };
58
+ VALUE definition = liquid_method_call(2, args, portable_instance_method);
59
+ VALUE location = liquid_method_call(1, &definition, portable_source_location);
60
+ VALUE name = liquid_method_call(1, &definition, portable_original_name);
61
+ bool builtin = NIL_P(location);
62
+ if (RB_TYPE_P(location, T_ARRAY) && RARRAY_LEN(location)) {
63
+ VALUE source = RARRAY_AREF(location, 0);
64
+ const char prefix[] = "<internal:core> ";
65
+ builtin = RB_TYPE_P(source, T_STRING) && RSTRING_LEN(source) >= (long)(sizeof(prefix) - 1) &&
66
+ !memcmp(RSTRING_PTR(source), prefix, sizeof(prefix) - 1);
67
+ }
68
+ RB_GC_GUARD(definition);
69
+ RB_GC_GUARD(location);
70
+ bool original_name_matches = name == ID2SYM(method) ||
71
+ (method == rb_intern("send") && name == ID2SYM(rb_intern("__send__")));
72
+ return builtin && original_name_matches;
73
+ }
74
+ #endif
75
+
76
+ #ifndef HAVE_RB_GC_COUNT
77
+ static VALUE portable_gc_count;
78
+
79
+ size_t liquid_gc_count(void)
80
+ {
81
+ return NUM2SIZET(liquid_method_call(0, NULL, portable_gc_count));
82
+ }
83
+ #endif
84
+
85
+ #ifndef HAVE_RUBY_STACK_CHECK
86
+ #include <pthread.h>
87
+ static _Thread_local uintptr_t portable_stack_bottom;
88
+
89
+ bool liquid_stack_check(void)
90
+ {
91
+ char marker;
92
+ if (!portable_stack_bottom) {
93
+ #ifdef HAVE_PTHREAD_GETATTR_NP
94
+ pthread_attr_t attributes;
95
+ void *base;
96
+ size_t size;
97
+ if (pthread_getattr_np(pthread_self(), &attributes))
98
+ rb_raise(rb_eRuntimeError, "cannot read native stack bounds");
99
+ int error = pthread_attr_getstack(&attributes, &base, &size);
100
+ pthread_attr_destroy(&attributes);
101
+ if (error) rb_raise(rb_eRuntimeError, "cannot read native stack bounds");
102
+ portable_stack_bottom = (uintptr_t)base;
103
+ #else
104
+ portable_stack_bottom = (uintptr_t)pthread_get_stackaddr_np(pthread_self()) - pthread_get_stacksize_np(pthread_self());
105
+ #endif
106
+ }
107
+ return (uintptr_t)&marker < portable_stack_bottom + 128 * 1024;
108
+ }
109
+ #endif
110
+
111
+ __attribute__((noreturn)) void raise_non_utf8_encoding_error(VALUE string, const char *value_name)
112
+ {
113
+ rb_raise(rb_eEncCompatError, "non-UTF8 encoded %s (%"PRIsVALUE") not supported", value_name, rb_obj_encoding(string));
114
+ }
115
+
116
+ RUBY_FUNC_EXPORTED void Init_liquid_c(void)
117
+ {
118
+ #ifndef HAVE_RB_GC_COUNT
119
+ rb_global_variable(&portable_gc_count);
120
+ portable_gc_count = liquid_method_handle(rb_obj_method(rb_mGC, ID2SYM(rb_intern("count"))));
121
+ #endif
122
+ #ifndef HAVE_RB_METHOD_BASIC_DEFINITION_P
123
+ portable_method_readers_init();
124
+ #endif
125
+ id_evaluate = rb_intern("evaluate");
126
+ id_to_liquid = rb_intern("to_liquid");
127
+ id_to_s = rb_intern("to_s");
128
+ id_call = rb_intern("call");
129
+ id_compile_evaluate = rb_intern("compile_evaluate");
130
+ id_ivar_line_number = rb_intern("@line_number");
131
+
132
+ utf8_encoding = rb_utf8_encoding();
133
+ utf8_encoding_index = rb_enc_to_index(utf8_encoding);
134
+
135
+ mLiquid = rb_define_module("Liquid");
136
+ rb_global_variable(&mLiquid);
137
+
138
+ mLiquidC = rb_define_module_under(mLiquid, "C");
139
+ rb_global_variable(&mLiquidC);
140
+
141
+ cLiquidArgumentError = rb_const_get(mLiquid, rb_intern("ArgumentError"));
142
+ rb_global_variable(&cLiquidArgumentError);
143
+
144
+ cLiquidSyntaxError = rb_const_get(mLiquid, rb_intern("SyntaxError"));
145
+ rb_global_variable(&cLiquidSyntaxError);
146
+
147
+ cMemoryError = rb_const_get(mLiquid, rb_intern("MemoryError"));
148
+ rb_global_variable(&cMemoryError);
149
+
150
+ cLiquidVariable = rb_const_get(mLiquid, rb_intern("Variable"));
151
+ rb_global_variable(&cLiquidVariable);
152
+
153
+ cLiquidTemplate = rb_const_get(mLiquid, rb_intern("Template"));
154
+ rb_global_variable(&cLiquidTemplate);
155
+
156
+ cLiquidBlockBody = rb_const_get(mLiquid, rb_intern("BlockBody"));
157
+ rb_global_variable(&cLiquidBlockBody);
158
+
159
+ cLiquidVariableLookup = rb_const_get(mLiquid, rb_intern("VariableLookup"));
160
+ rb_global_variable(&cLiquidVariableLookup);
161
+
162
+ cLiquidRangeLookup = rb_const_get(mLiquid, rb_intern("RangeLookup"));
163
+ rb_global_variable(&cLiquidRangeLookup);
164
+
165
+ liquid_define_tokenizer();
166
+ liquid_define_parser();
167
+ liquid_define_native_parser();
168
+ liquid_define_raw();
169
+ liquid_define_resource_limits();
170
+ liquid_define_expression();
171
+ liquid_define_variable();
172
+ liquid_define_document_body();
173
+ liquid_define_block_body();
174
+ liquid_define_context();
175
+ liquid_define_native_values();
176
+ liquid_define_parse_context();
177
+ liquid_define_variable_lookup();
178
+ liquid_define_vm_assembler_pool();
179
+ liquid_define_vm_assembler();
180
+ liquid_define_vm();
181
+ liquid_define_usage();
182
+ }
@@ -0,0 +1,112 @@
1
+ #if !defined(LIQUID_H)
2
+ #define LIQUID_H
3
+
4
+ #include <ruby.h>
5
+ #include <ruby/encoding.h>
6
+ #include <stdbool.h>
7
+
8
+ #ifdef HAVE_RB_METHOD_BASIC_DEFINITION_P
9
+ static inline bool liquid_method_basic_definition_p(VALUE klass, ID method)
10
+ {
11
+ return rb_method_basic_definition_p(klass, method);
12
+ }
13
+ #else
14
+ bool liquid_method_basic_definition_p(VALUE klass, ID method);
15
+ #endif
16
+
17
+ static inline VALUE liquid_method_handle(VALUE method)
18
+ {
19
+ #ifdef HAVE_RB_METHOD_CALL
20
+ return method;
21
+ #else
22
+ return rb_funcall(method, rb_intern("to_proc"), 0);
23
+ #endif
24
+ }
25
+
26
+ static inline VALUE liquid_method_call(int argc, const VALUE *argv, VALUE method)
27
+ {
28
+ #ifdef HAVE_RB_METHOD_CALL
29
+ return rb_method_call(argc, argv, method);
30
+ #else
31
+ return rb_proc_call(method, rb_ary_new_from_values(argc, argv));
32
+ #endif
33
+ }
34
+
35
+ #ifdef HAVE_RB_GC_COUNT
36
+ static inline size_t liquid_gc_count(void) { return rb_gc_count(); }
37
+ #else
38
+ size_t liquid_gc_count(void);
39
+ #endif
40
+
41
+ #ifdef HAVE_RUBY_STACK_CHECK
42
+ static inline bool liquid_stack_check(void) { return ruby_stack_check(); }
43
+ #else
44
+ bool liquid_stack_check(void);
45
+ #endif
46
+
47
+ static inline VALUE liquid_big_lshift(VALUE value, VALUE shift)
48
+ {
49
+ #ifdef HAVE_RB_BIG_LSHIFT
50
+ return rb_big_lshift(value, shift);
51
+ #else
52
+ return rb_funcall(value, rb_intern("<<"), 1, shift);
53
+ #endif
54
+ }
55
+
56
+ static inline VALUE liquid_big_or(VALUE value, VALUE mask)
57
+ {
58
+ #ifdef HAVE_RB_BIG_OR
59
+ return rb_big_or(value, mask);
60
+ #else
61
+ return rb_funcall(value, rb_intern("|"), 1, mask);
62
+ #endif
63
+ }
64
+
65
+ static inline double liquid_str_to_dbl(VALUE string, int strict)
66
+ {
67
+ #ifdef HAVE_RB_STR_TO_DBL
68
+ return rb_str_to_dbl(string, strict);
69
+ #else
70
+ return rb_cstr_to_dbl(StringValueCStr(string), strict);
71
+ #endif
72
+ }
73
+
74
+ static inline VALUE liquid_obj_init_copy(VALUE self, VALUE other)
75
+ {
76
+ #ifdef HAVE_RB_OBJ_INIT_COPY
77
+ return rb_obj_init_copy(self, other);
78
+ #else
79
+ rb_check_frozen(self);
80
+ if (TYPE(self) != TYPE(other) || rb_obj_class(self) != rb_obj_class(other))
81
+ rb_raise(rb_eTypeError, "initialize_copy should take same class object");
82
+ return self;
83
+ #endif
84
+ }
85
+
86
+ extern ID id_evaluate;
87
+ extern ID id_to_liquid;
88
+ extern ID id_to_s;
89
+ extern ID id_call;
90
+ extern ID id_compile_evaluate;
91
+ extern ID id_ivar_line_number;
92
+
93
+ extern VALUE mLiquid, mLiquidC, cLiquidVariable, cLiquidTemplate, cLiquidBlockBody;
94
+ extern VALUE cLiquidVariableLookup, cLiquidRangeLookup;
95
+ extern VALUE cLiquidArgumentError, cLiquidSyntaxError, cMemoryError;
96
+ extern rb_encoding *utf8_encoding;
97
+ extern int utf8_encoding_index;
98
+
99
+ __attribute__((noreturn)) void raise_non_utf8_encoding_error(VALUE string, const char *string_name);
100
+
101
+ static inline void check_utf8_encoding(VALUE string, const char *string_name)
102
+ {
103
+ if (RB_UNLIKELY(RB_ENCODING_GET_INLINED(string) != utf8_encoding_index))
104
+ raise_non_utf8_encoding_error(string, string_name);
105
+ }
106
+
107
+ #ifndef RB_LIKELY
108
+ // RB_LIKELY added in Ruby 2.4
109
+ #define RB_LIKELY(x) (__builtin_expect(!!(x), 1))
110
+ #endif
111
+
112
+ #endif