mongory 0.8.2-aarch64-linux → 0.9.0-aarch64-linux

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 (49) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +31 -0
  3. data/docs/advanced_usage.md +7 -0
  4. data/docs/clang_bridge.md +6 -0
  5. data/ext/mongory_ext/extconf.rb +6 -1
  6. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +25 -0
  7. data/ext/mongory_ext/mongory-core/src/foundations/config.c +64 -27
  8. data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +1 -0
  9. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +49 -0
  10. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool_private.h +45 -0
  11. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +2 -5
  12. data/ext/mongory_ext/mongory-core/src/foundations/value.c +44 -7
  13. data/ext/mongory_ext/mongory-core/src/matchers/array_record_matcher.c +31 -10
  14. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +2 -0
  15. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +32 -13
  16. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +34 -13
  17. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +76 -15
  18. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.h +12 -0
  19. data/ext/mongory_ext/mongory-core/src/matchers/literal_matcher.c +55 -17
  20. data/ext/mongory_ext/mongory_ext.c +35 -724
  21. data/ext/mongory_ext/mongory_ext_internal.h +75 -0
  22. data/ext/mongory_ext/rb_mongory_adapters.c +140 -0
  23. data/ext/mongory_ext/rb_mongory_cmatcher.c +386 -0
  24. data/ext/mongory_ext/rb_mongory_value.c +260 -0
  25. data/lib/core/2.6/mongory_ext.so +0 -0
  26. data/lib/core/2.7/mongory_ext.so +0 -0
  27. data/lib/core/3.0/mongory_ext.so +0 -0
  28. data/lib/core/3.1/mongory_ext.so +0 -0
  29. data/lib/core/3.2/mongory_ext.so +0 -0
  30. data/lib/core/3.3/mongory_ext.so +0 -0
  31. data/lib/core/3.4/mongory_ext.so +0 -0
  32. data/lib/core/4.0/mongory_ext.so +0 -0
  33. data/lib/generators/mongory/install/templates/initializer.rb.erb +5 -0
  34. data/lib/mongory/converters/abstract_converter.rb +6 -5
  35. data/lib/mongory/converters/condition_converter.rb +2 -2
  36. data/lib/mongory/matchers/abstract_matcher.rb +1 -1
  37. data/lib/mongory/matchers/abstract_multi_matcher.rb +2 -2
  38. data/lib/mongory/matchers/array_record_matcher.rb +23 -11
  39. data/lib/mongory/matchers/elem_match_matcher.rb +2 -0
  40. data/lib/mongory/matchers/eq_matcher.rb +4 -2
  41. data/lib/mongory/matchers/hash_condition_matcher.rb +23 -6
  42. data/lib/mongory/matchers/in_matcher.rb +17 -1
  43. data/lib/mongory/matchers/ne_matcher.rb +7 -6
  44. data/lib/mongory/matchers/nin_matcher.rb +2 -6
  45. data/lib/mongory/matchers/regex_matcher.rb +29 -0
  46. data/lib/mongory/matchers.rb +7 -4
  47. data/lib/mongory/version.rb +1 -1
  48. data/lib/mongory.rb +57 -5
  49. metadata +7 -2
@@ -1,755 +1,66 @@
1
1
  /**
2
2
  * @file mongory_ext.c
3
- * @brief Ruby C extension wrapper for mongory-core
4
- *
5
- * This file provides Ruby bindings for the mongory-core C library,
6
- * allowing Ruby applications to use high-performance C-based matching
7
- * operations while maintaining the elegant Ruby DSL.
3
+ * @brief Extension entry point and shared pool/error helpers for mongory-rb.
8
4
  */
9
- #include "mongory-core.h"
10
- #include <ruby.h>
11
- #include <ruby/encoding.h>
5
+ #include "mongory_ext_internal.h"
6
+ #include <stdlib.h>
12
7
  #include <string.h>
13
8
 
14
- // Ruby module and class definitions
15
- static VALUE mMongory;
16
- static VALUE cMongoryMatcher;
17
- static VALUE cMongoryMatcherContext;
18
- static VALUE mMongoryMatchers;
9
+ VALUE rb_mongory_m_mongory;
10
+ VALUE rb_mongory_m_matchers;
11
+ VALUE rb_mongory_c_matcher;
12
+ VALUE rb_mongory_c_matcher_context;
13
+ VALUE rb_mongory_e_error;
14
+ VALUE rb_mongory_e_type_error;
15
+ VALUE rb_mongory_data_converter;
16
+ VALUE rb_mongory_condition_converter;
19
17
 
20
- // Error classes
21
- static VALUE eMongoryError;
22
- static VALUE eMongoryTypeError;
23
-
24
- // Converter instance
25
- static VALUE inMongoryDataConverter;
26
- static VALUE inMongoryConditionConverter;
27
-
28
- // Matcher wrapper structure
29
- typedef struct rb_mongory_matcher_t {
30
- mongory_matcher *matcher;
31
- mongory_value *condition;
32
- mongory_memory_pool *pool;
33
- mongory_memory_pool *scratch_pool;
34
- mongory_memory_pool *trace_pool;
35
- mongory_table *string_map;
36
- mongory_table *symbol_map;
37
- mongory_array *mark_list;
38
- VALUE ctx;
39
- } rb_mongory_matcher_t;
40
-
41
- typedef struct rb_mongory_memory_pool_t {
42
- mongory_memory_pool base;
43
- rb_mongory_matcher_t *owner;
44
- } rb_mongory_memory_pool_t;
45
-
46
- typedef struct rb_mongory_table_t {
47
- mongory_table base;
48
- VALUE rb_hash;
49
- rb_mongory_matcher_t *owner;
50
- } rb_mongory_table_t;
51
-
52
- typedef struct rb_mongory_array_t {
53
- mongory_array base;
54
- VALUE rb_array;
55
- rb_mongory_matcher_t *owner;
56
- } rb_mongory_array_t;
57
-
58
- typedef struct {
59
- mongory_table *table;
60
- mongory_memory_pool *pool;
61
- } hash_conv_ctx;
62
-
63
- // Forward declarations
64
- static void rb_mongory_matcher_mark(void *ptr);
65
- static void rb_mongory_matcher_free(void *ptr);
66
- mongory_value *rb_to_mongory_value_deep(mongory_memory_pool *pool, VALUE rb_value);
67
- mongory_value *rb_to_mongory_value_shallow(mongory_memory_pool *pool, VALUE rb_value);
68
- mongory_value *rb_mongory_table_wrap(mongory_memory_pool *pool, VALUE rb_hash);
69
- mongory_value *rb_mongory_array_wrap(mongory_memory_pool *pool, VALUE rb_array);
70
- static VALUE cache_fetch_string(rb_mongory_matcher_t *owner, const char *key);
71
- static VALUE cache_fetch_symbol(rb_mongory_matcher_t *owner, const char *key);
72
- static rb_mongory_memory_pool_t *rb_mongory_memory_pool_new();
73
- static void rb_mongory_matcher_parse_argv(rb_mongory_matcher_t *wrapper, int argc, VALUE *argv);
74
- static bool rb_mongory_error_handling(mongory_memory_pool *pool, char *error_message);
75
-
76
- static const rb_data_type_t rb_mongory_matcher_type = {
77
- .wrap_struct_name = "mongory_matcher",
78
- .function = {
79
- .dmark = rb_mongory_matcher_mark,
80
- .dfree = rb_mongory_matcher_free,
81
- .dsize = NULL,
82
- },
83
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
84
- };
85
-
86
- /**
87
- * Ruby method implementations
88
- */
89
-
90
- // Mongory::CMatcher.new(condition)
91
- static VALUE rb_mongory_matcher_new(int argc, VALUE *argv, VALUE class) {
92
- rb_mongory_memory_pool_t *matcher_pool = rb_mongory_memory_pool_new();
93
- mongory_memory_pool *matcher_pool_base = &matcher_pool->base;
94
- rb_mongory_memory_pool_t *scratch_pool = rb_mongory_memory_pool_new();
95
- mongory_memory_pool *scratch_pool_base = &scratch_pool->base;
96
- rb_mongory_matcher_t *wrapper = ALLOC(rb_mongory_matcher_t);
97
- wrapper->pool = matcher_pool_base;
98
- wrapper->scratch_pool = scratch_pool_base;
99
- wrapper->trace_pool = NULL;
100
- wrapper->ctx = NULL;
101
- wrapper->string_map = mongory_table_new(matcher_pool_base);
102
- wrapper->symbol_map = mongory_table_new(matcher_pool_base);
103
- wrapper->mark_list = mongory_array_new(matcher_pool_base);
104
- wrapper->condition = NULL;
105
- wrapper->matcher = NULL;
106
- matcher_pool->owner = wrapper;
107
- scratch_pool->owner = wrapper;
108
- rb_mongory_matcher_parse_argv(wrapper, argc, argv);
109
-
110
- mongory_matcher *matcher = mongory_matcher_new(matcher_pool_base, wrapper->condition, wrapper->ctx);
111
- if (matcher_pool_base->error) {
112
- char err_msg[256];
113
- snprintf(err_msg, sizeof(err_msg), "%s",
114
- matcher_pool_base->error->message ? matcher_pool_base->error->message : "Unknown error");
115
- matcher_pool_base->free(matcher_pool_base);
116
- scratch_pool_base->free(scratch_pool_base);
117
- xfree(wrapper);
118
- rb_raise(eMongoryTypeError, "Failed to create matcher: %s", err_msg);
119
- }
120
-
121
- wrapper->matcher = matcher;
122
- return TypedData_Wrap_Struct(class, &rb_mongory_matcher_type, wrapper);
123
- }
124
-
125
- static void rb_mongory_matcher_parse_argv(rb_mongory_matcher_t *self, int argc, VALUE *argv) {
126
- VALUE condition, kw_hash;
127
- rb_scan_args(argc, argv, "1:", &condition, &kw_hash);
128
- const ID ctx_id[1] = { rb_intern("context") };
129
- VALUE kw_vals[1] = { Qundef };
130
- if (kw_hash != Qnil) {
131
- rb_get_kwargs(kw_hash, ctx_id, 1, 0, kw_vals);
132
- }
133
- if (kw_vals[0] != Qundef) {
134
- self->ctx = kw_vals[0];
135
- } else {
136
- self->ctx = rb_funcall(cMongoryMatcherContext, rb_intern("new"), 0);
137
- }
138
- VALUE converted_condition = rb_funcall(inMongoryConditionConverter, rb_intern("convert"), 1, condition);
139
- self->condition = rb_to_mongory_value_deep(self->pool, converted_condition);
140
- self->mark_list->push(self->mark_list, self->condition);
141
- mongory_value *store_ctx = mongory_value_wrap_u(self->pool, (void *)self->ctx);
142
- store_ctx->origin = (void *)self->ctx;
143
- self->mark_list->push(self->mark_list, store_ctx);
144
- }
145
-
146
- // Mongory::CMatcher#match(data)
147
- static VALUE rb_mongory_matcher_match(VALUE self, VALUE data) {
148
- rb_mongory_matcher_t *self_wrapper;
149
- TypedData_Get_Struct(self, rb_mongory_matcher_t, &rb_mongory_matcher_type, self_wrapper);
150
- mongory_matcher *matcher = self_wrapper->matcher;
151
- mongory_memory_pool *scratch_pool = self_wrapper->scratch_pool;
152
- mongory_memory_pool *trace_pool = self_wrapper->trace_pool;
153
- mongory_value *data_value = rb_to_mongory_value_shallow(scratch_pool, data);
154
-
155
- if (rb_mongory_error_handling(scratch_pool, "Match failed")) {
156
- return Qnil;
157
- }
158
-
159
- bool result = mongory_matcher_match(matcher, data_value);
160
-
161
- if (trace_pool) {
162
- mongory_matcher_print_trace(matcher);
163
- trace_pool->reset(trace_pool);
164
- mongory_matcher_enable_trace(matcher, trace_pool);
165
- }
166
-
167
- scratch_pool->reset(scratch_pool);
168
-
169
- return result ? Qtrue : Qfalse;
170
- }
171
-
172
- // Mongory::CMatcher#explain
173
- static VALUE rb_mongory_matcher_explain(VALUE self) {
174
- rb_mongory_matcher_t *wrapper;
175
- TypedData_Get_Struct(self, rb_mongory_matcher_t, &rb_mongory_matcher_type, wrapper);
176
- mongory_matcher_explain(wrapper->matcher, wrapper->scratch_pool);
177
- if (rb_mongory_error_handling(wrapper->scratch_pool, "Explain failed")) {
178
- return Qnil;
179
- }
180
- wrapper->scratch_pool->reset(wrapper->scratch_pool);
181
- return Qnil;
182
- }
183
-
184
- // Mongory::CMatcher#trace(data)
185
- static VALUE rb_mongory_matcher_trace(VALUE self, VALUE data) {
186
- rb_mongory_matcher_t *wrapper;
187
- TypedData_Get_Struct(self, rb_mongory_matcher_t, &rb_mongory_matcher_type, wrapper);
188
- rb_mongory_memory_pool_t *trace_pool = rb_mongory_memory_pool_new();
189
- trace_pool->owner = wrapper;
190
- mongory_memory_pool *trace_pool_base = &trace_pool->base;
191
- mongory_value *data_value = rb_to_mongory_value_shallow(trace_pool_base, data);
192
-
193
- if (trace_pool_base->error) {
194
- char err_msg[256];
195
- snprintf(err_msg, sizeof(err_msg), "%s",
196
- trace_pool_base->error->message ? trace_pool_base->error->message : "Unknown error");
197
- trace_pool_base->free(trace_pool_base);
198
- rb_raise(eMongoryTypeError, "Trace failed: %s", err_msg);
199
- }
200
-
201
- bool matched = mongory_matcher_trace(wrapper->matcher, data_value);
202
- trace_pool_base->free(trace_pool_base);
203
-
204
- return matched ? Qtrue : Qfalse;
205
- }
206
-
207
- // Mongory::CMatcher#enable_trace
208
- static VALUE rb_mongory_matcher_enable_trace(VALUE self) {
209
- rb_mongory_matcher_t *wrapper;
210
- TypedData_Get_Struct(self, rb_mongory_matcher_t, &rb_mongory_matcher_type, wrapper);
211
- if (wrapper->trace_pool) {
212
- wrapper->trace_pool->free(wrapper->trace_pool);
213
- wrapper->trace_pool = NULL;
214
- }
215
- rb_mongory_memory_pool_t *trace_pool = rb_mongory_memory_pool_new();
216
- trace_pool->owner = wrapper;
217
- mongory_memory_pool *trace_pool_base = &trace_pool->base;
218
- mongory_matcher_enable_trace(wrapper->matcher, trace_pool_base);
219
-
220
- if (trace_pool_base->error) {
221
- char err_msg[256];
222
- snprintf(err_msg, sizeof(err_msg), "%s",
223
- trace_pool_base->error->message ? trace_pool_base->error->message : "Unknown error");
224
- trace_pool_base->free(trace_pool_base);
225
- rb_raise(eMongoryTypeError, "Enable trace failed: %s", err_msg);
226
- }
227
- wrapper->trace_pool = trace_pool_base;
228
-
229
- return Qnil;
230
- }
231
-
232
- // Mongory::CMatcher#disable_trace
233
- static VALUE rb_mongory_matcher_disable_trace(VALUE self) {
234
- rb_mongory_matcher_t *wrapper;
235
- TypedData_Get_Struct(self, rb_mongory_matcher_t, &rb_mongory_matcher_type, wrapper);
236
- mongory_memory_pool *trace_pool = wrapper->trace_pool;
237
- mongory_matcher_disable_trace(wrapper->matcher);
238
- if (trace_pool) {
239
- wrapper->trace_pool = NULL;
240
- if (trace_pool->error) {
241
- char err_msg[256];
242
- snprintf(err_msg, sizeof(err_msg), "%s",
243
- trace_pool->error->message ? trace_pool->error->message : "Unknown error");
244
- trace_pool->free(trace_pool);
245
- rb_raise(eMongoryTypeError, "Disable trace failed: %s", err_msg);
246
- }
247
- trace_pool->free(trace_pool);
248
- }
249
-
250
- return Qnil;
251
- }
252
-
253
- // Mongory::CMatcher#print_trace
254
- static VALUE rb_mongory_matcher_print_trace(VALUE self) {
255
- rb_mongory_matcher_t *wrapper;
256
- TypedData_Get_Struct(self, rb_mongory_matcher_t, &rb_mongory_matcher_type, wrapper);
257
- if (!wrapper->trace_pool) {
258
- return Qnil;
259
- }
260
- mongory_matcher_print_trace(wrapper->matcher);
261
- rb_mongory_error_handling(wrapper->trace_pool, "Print trace failed");
262
-
263
- return Qnil;
264
- }
265
-
266
- // Mongory::CMatcher#condition
267
- static VALUE rb_mongory_matcher_condition(VALUE self) {
268
- rb_mongory_matcher_t *wrapper;
269
- TypedData_Get_Struct(self, rb_mongory_matcher_t, &rb_mongory_matcher_type, wrapper);
270
-
271
- return (VALUE)wrapper->condition->origin;
272
- }
273
-
274
- // Mongory::CMatcher#context
275
- static VALUE rb_mongory_matcher_context(VALUE self) {
276
- rb_mongory_matcher_t *wrapper;
277
- TypedData_Get_Struct(self, rb_mongory_matcher_t, &rb_mongory_matcher_type, wrapper);
278
-
279
- return wrapper->ctx ? wrapper->ctx : Qnil;
280
- }
281
-
282
- // Mongory::CMatcher.trace_result_colorful=(colorful)
283
- static VALUE rb_mongory_matcher_trace_result_colorful(VALUE self, VALUE colorful) {
284
- (void)self;
285
- mongory_matcher_trace_result_colorful_set(RTEST(colorful));
286
-
287
- return Qnil;
288
- }
289
-
290
- /**
291
- * Create a new memory pool
292
- */
293
- static rb_mongory_memory_pool_t *rb_mongory_memory_pool_new() {
18
+ rb_mongory_memory_pool_t *rb_mongory_memory_pool_new(void) {
294
19
  rb_mongory_memory_pool_t *pool = malloc(sizeof(rb_mongory_memory_pool_t));
20
+ if (!pool) {
21
+ return NULL;
22
+ }
295
23
  mongory_memory_pool *base = mongory_memory_pool_new();
24
+ if (!base) {
25
+ free(pool);
26
+ return NULL;
27
+ }
296
28
  memcpy(&pool->base, base, sizeof(mongory_memory_pool));
297
29
  pool->owner = NULL;
30
+ pool->next_scratch = NULL;
298
31
  free(base);
299
32
 
300
33
  return pool;
301
34
  }
302
35
 
303
- /**
304
- * Ruby GC management functions
305
- */
306
- static void rb_mongory_matcher_free(void *ptr) {
307
- rb_mongory_matcher_t *wrapper = (rb_mongory_matcher_t *)ptr;
308
- mongory_memory_pool *pool = wrapper->pool;
309
- mongory_memory_pool *scratch_pool = wrapper->scratch_pool;
310
- mongory_memory_pool *trace_pool = wrapper->trace_pool;
311
- pool->free(pool);
312
- scratch_pool->free(scratch_pool);
313
- if (trace_pool) {
314
- trace_pool->free(trace_pool);
315
- }
316
- xfree(wrapper);
317
- }
318
-
319
- /**
320
- * GC marking callback for mongory_array
321
- */
322
- static bool gc_mark_array_cb(mongory_value *value, void *acc) {
323
- (void)acc;
324
- if (value && value->origin) rb_gc_mark((VALUE)value->origin);
325
- return true;
326
- }
327
-
328
- /**
329
- * GC marking callback for mongory_matcher
330
- */
331
- static void rb_mongory_matcher_mark(void *ptr) {
332
- rb_mongory_matcher_t *self = (rb_mongory_matcher_t *)ptr;
333
- if (!self) return;
334
- self->mark_list->each(self->mark_list, NULL, gc_mark_array_cb);
335
- }
336
-
337
- /**
338
- * Helper functions for Ruby/C conversion
339
- */
340
-
341
- // Helper function to convert Ruby value to C string
342
- static char *rb_mongory_value_to_cstr(mongory_value *value, mongory_memory_pool *pool) {
343
- (void)pool;
344
- VALUE rb_value = (VALUE)value->origin;
345
- VALUE rb_str = rb_funcall(rb_value, rb_intern("inspect"), 0);
346
- return StringValueCStr(rb_str);
347
- }
348
-
349
- // Helper function for primitive conversion of Ruby value to mongory_value
350
- static mongory_value *rb_to_mongory_value_primitive(mongory_memory_pool *pool, VALUE rb_value) {
351
- mongory_value *mg_value = NULL;
352
- switch (TYPE(rb_value)) {
353
- case T_NIL:
354
- mg_value = mongory_value_wrap_n(pool, NULL);
355
- break;
356
-
357
- case T_TRUE:
358
- mg_value = mongory_value_wrap_b(pool, true);
359
- break;
360
-
361
- case T_FALSE:
362
- mg_value = mongory_value_wrap_b(pool, false);
363
- break;
364
-
365
- case T_FIXNUM:
366
- mg_value = mongory_value_wrap_i(pool, rb_num2long_inline(rb_value));
367
- break;
368
-
369
- case T_BIGNUM:
370
- mg_value = mongory_value_wrap_i(pool, rb_num2ll_inline(rb_value));
371
- break;
372
-
373
- case T_FLOAT:
374
- mg_value = mongory_value_wrap_d(pool, rb_num2dbl(rb_value));
375
- break;
376
-
377
- case T_STRING:
378
- mg_value = mongory_value_wrap_s(pool, StringValueCStr(rb_value));
379
- break;
380
-
381
- case T_SYMBOL:
382
- mg_value = mongory_value_wrap_s(pool, (char *)rb_id2name(rb_sym2id(rb_value)));
383
- break;
384
-
385
- case T_REGEXP:
386
- mg_value = mongory_value_wrap_regex(pool, (void *)rb_value);
387
- break;
388
- }
389
- return mg_value;
390
- }
391
- // Shallow conversion: Convert Ruby value to mongory_value (fully materialize arrays/tables)
392
- static mongory_value *rb_to_mongory_value_shallow_rec(mongory_memory_pool *pool, VALUE rb_value, bool converted) {
393
- mongory_value *mg_value = rb_to_mongory_value_primitive(pool, rb_value);
394
- if (mg_value) {
395
- mg_value->origin = rb_value;
396
- return mg_value;
397
- }
398
-
399
- switch (TYPE(rb_value)) {
400
- case T_ARRAY: {
401
- mg_value = rb_mongory_array_wrap(pool, rb_value);
402
- break;
403
- }
404
-
405
- case T_HASH: {
406
- mg_value = rb_mongory_table_wrap(pool, rb_value);
407
- break;
408
- }
409
-
410
- default:
411
- if (converted) {
412
- mg_value = mongory_value_wrap_u(pool, (void *)rb_value);
413
- break;
414
- } else {
415
- VALUE converted_value = rb_funcall(inMongoryDataConverter, rb_intern("convert"), 1, rb_value);
416
- return rb_to_mongory_value_shallow_rec(pool, converted_value, true);
417
- }
418
- }
419
- mg_value->origin = rb_value;
420
- return mg_value;
421
- }
422
-
423
- // Shallow conversion: Convert Ruby value to mongory_value (fully materialize arrays/tables)
424
- mongory_value *rb_to_mongory_value_shallow(mongory_memory_pool *pool, VALUE rb_value) {
425
- return rb_to_mongory_value_shallow_rec(pool, rb_value, false);
426
- }
427
-
428
- // Helper function for deep conversion of hash values
429
- static int hash_foreach_deep_convert_cb(VALUE key, VALUE val, VALUE ptr) {
430
- hash_conv_ctx *ctx = (hash_conv_ctx *)ptr;
431
- rb_mongory_memory_pool_t *rb_pool = (rb_mongory_memory_pool_t *)ctx->pool;
432
- rb_mongory_matcher_t *owner = rb_pool->owner;
433
- mongory_table *store_map;
434
- char *key_str;
435
- if (SYMBOL_P(key)) {
436
- key_str = (char *)rb_id2name(SYM2ID(key));
437
- store_map = owner->symbol_map;
438
- } else {
439
- if (!RB_TYPE_P(key, T_STRING)) {
440
- key = rb_obj_as_string(key);
441
- }
442
- key_str = StringValueCStr(key);
443
- store_map = owner->string_map;
444
- }
445
- mongory_value *store = mongory_value_wrap_u(ctx->pool, NULL);
446
- store->origin = (void *)key;
447
- store_map->set(store_map, key_str, store);
448
- owner->mark_list->push(owner->mark_list, store);
449
- mongory_value *cval = rb_to_mongory_value_deep(ctx->pool, val);
450
- ctx->table->set(ctx->table, key_str, cval);
451
- return ST_CONTINUE;
452
- }
453
-
454
- // Deep conversion: Convert Ruby value to mongory_value (fully materialize arrays/tables)
455
- static mongory_value *rb_to_mongory_value_deep_rec(mongory_memory_pool *pool, VALUE rb_value, bool converted) {
456
- mongory_value *mg_value = rb_to_mongory_value_primitive(pool, rb_value);
457
- if (mg_value) {
458
- mg_value->origin = rb_value;
459
- return mg_value;
460
- }
461
- switch (TYPE(rb_value)) {
462
- case T_ARRAY: {
463
- mongory_array *array = mongory_array_new(pool);
464
-
465
- for (long i = 0; i < RARRAY_LEN(rb_value); i++) {
466
- array->push(array, rb_to_mongory_value_deep(pool, RARRAY_AREF(rb_value, i)));
467
- }
468
- mg_value = mongory_value_wrap_a(pool, array);
469
- break;
470
- }
471
-
472
- case T_HASH: {
473
- mongory_table *table = mongory_table_new(pool);
474
-
475
- hash_conv_ctx ctx = {table, pool};
476
- rb_hash_foreach(rb_value, hash_foreach_deep_convert_cb, (VALUE)&ctx);
477
-
478
- mg_value = mongory_value_wrap_t(pool, table);
479
- break;
480
- }
481
-
482
- default:
483
- if (converted) {
484
- mg_value = mongory_value_wrap_u(pool, (void *)rb_value);
485
- break;
486
- } else {
487
- VALUE converted_value = rb_funcall(inMongoryDataConverter, rb_intern("convert"), 1, rb_value);
488
- mg_value = rb_to_mongory_value_deep_rec(pool, converted_value, true);
489
- break;
490
- }
491
- }
492
- mg_value->origin = rb_value;
493
- return mg_value;
494
- }
495
-
496
- // Deep conversion: Convert Ruby value to mongory_value (fully materialize arrays/tables)
497
- mongory_value *rb_to_mongory_value_deep(mongory_memory_pool *pool, VALUE rb_value) {
498
- return rb_to_mongory_value_deep_rec(pool, rb_value, false);
499
- }
500
-
501
- // Get value from Ruby hash via mongory_table
502
- mongory_value *rb_mongory_table_get(mongory_table *self, char *key) {
503
- rb_mongory_table_t *table = (rb_mongory_table_t *)self;
504
- VALUE rb_hash = table->rb_hash;
505
- VALUE rb_value = Qundef;
506
-
507
- // Use cached Ruby String key if possible
508
- VALUE key_str = cache_fetch_string(table->owner, key);
509
- rb_value = rb_hash_lookup2(rb_hash, key_str, Qundef);
510
-
511
- if (rb_value == Qundef) {
512
- // Fallback to Symbol key, using cache
513
- VALUE key_sym = cache_fetch_symbol(table->owner, key);
514
- rb_value = rb_hash_lookup2(rb_hash, key_sym, Qundef);
515
- }
516
-
517
- if (rb_value == Qundef) {
518
- return NULL;
519
- }
520
- return rb_to_mongory_value_shallow(table->base.pool, rb_value);
521
- }
522
-
523
- // Shallow conversion: Wrap Ruby hash as mongory_table
524
- mongory_value *rb_mongory_table_wrap(mongory_memory_pool *pool, VALUE rb_hash) {
525
- rb_mongory_table_t *table = MG_ALLOC_PTR(pool, rb_mongory_table_t);
526
- rb_mongory_memory_pool_t *rb_pool = (rb_mongory_memory_pool_t *)pool;
527
- table->base.pool = pool;
528
- table->base.get = rb_mongory_table_get;
529
- table->rb_hash = rb_hash;
530
- table->base.count = RHASH_SIZE(rb_hash);
531
- table->owner = rb_pool->owner;
532
- mongory_value *mg_value = mongory_value_wrap_t(pool, &table->base);
533
- mg_value->origin = (void *)rb_hash;
534
- mg_value->to_str = rb_mongory_value_to_cstr;
535
- return mg_value;
536
- }
537
-
538
- // Get value from Ruby array via mongory_array
539
- static mongory_value *rb_mongory_array_get(mongory_array *self, size_t index) {
540
- rb_mongory_array_t *array = (rb_mongory_array_t *)self;
541
- VALUE rb_array = array->rb_array;
542
- if (index >= (size_t)RARRAY_LEN(rb_array)) {
543
- return NULL;
544
- }
545
- VALUE rb_value = rb_ary_entry(rb_array, index);
546
- return rb_to_mongory_value_shallow(self->pool, rb_value);
547
- }
548
-
549
- // Shallow conversion: Wrap Ruby array as mongory_array
550
- mongory_value *rb_mongory_array_wrap(mongory_memory_pool *pool, VALUE rb_array) {
551
- rb_mongory_array_t *array = MG_ALLOC_PTR(pool, rb_mongory_array_t);
552
- rb_mongory_memory_pool_t *rb_pool = (rb_mongory_memory_pool_t *)pool;
553
- array->base.pool = pool;
554
- array->base.get = rb_mongory_array_get;
555
- array->rb_array = rb_array;
556
- array->base.count = RARRAY_LEN(rb_array);
557
- array->owner = rb_pool->owner;
558
- mongory_value *mg_value = mongory_value_wrap_a(pool, &array->base);
559
- mg_value->origin = (void *)rb_array;
560
- mg_value->to_str = rb_mongory_value_to_cstr;
561
- return mg_value;
562
- }
563
-
564
- // Convert mongory_value to Ruby value (returns the original Ruby value where binding in below conversion is needed)
565
- void *mongory_value_to_ruby(mongory_memory_pool *pool, mongory_value *value) {
566
- (void)pool;
567
- if (!value)
568
- return NULL;
569
- return value->origin;
570
- }
571
-
572
- // ===== Cache helper implementations =====
573
- static VALUE cache_fetch_string(rb_mongory_matcher_t *owner, const char *key) {
574
- if (!owner || !owner->string_map) return rb_utf8_str_new_cstr(key);
575
- mongory_value *v = owner->string_map->get(owner->string_map, (char *)key);
576
- if (v && v->origin) return (VALUE)v->origin;
577
- VALUE s = rb_utf8_str_new_cstr(key);
578
- mongory_value *store = mongory_value_wrap_u(owner->pool, NULL);
579
- store->origin = (void *)s;
580
- owner->string_map->set(owner->string_map, (char *)key, store);
581
- owner->mark_list->push(owner->mark_list, store);
582
- return s;
583
- }
584
-
585
- // Helper function to convert char* key to Ruby symbol
586
- static inline VALUE char_key_to_symbol(const char *key, rb_encoding *enc) {
587
- ID id = rb_check_id_cstr(key, (long)strlen(key), enc);
588
- if (!id) id = rb_intern3(key, (long)strlen(key), enc);
589
- return ID2SYM(id);
590
- }
591
-
592
- // Cache helper for Ruby symbol keys
593
- static VALUE cache_fetch_symbol(rb_mongory_matcher_t *owner, const char *key) {
594
- rb_encoding *enc = rb_utf8_encoding();
595
- if (!owner || !owner->symbol_map) {
596
- return char_key_to_symbol(key, enc);
597
- }
598
- mongory_value *v = owner->symbol_map->get(owner->symbol_map, (char *)key);
599
- if (v && v->origin) return (VALUE)v->origin;
600
- VALUE sym = char_key_to_symbol(key, enc);
601
- mongory_value *store = mongory_value_wrap_u(owner->pool, NULL);
602
- store->origin = (void *)sym;
603
- owner->symbol_map->set(owner->symbol_map, (char *)key, store);
604
- owner->mark_list->push(owner->mark_list, store);
605
- return sym;
606
- }
607
-
608
- // Regex adapter bridging to Ruby's Regexp
609
- static bool rb_mongory_regex_match_adapter(mongory_memory_pool *pool, mongory_value *pattern, mongory_value *value) {
610
- if (!pattern || !value) {
611
- return false;
612
- }
613
- if (value->type != MONGORY_TYPE_STRING) {
614
- return false;
615
- }
616
-
617
- VALUE rb_str = (VALUE)value->origin;
618
- VALUE rb_re = Qnil;
619
-
620
- if (pattern->type == MONGORY_TYPE_REGEX && pattern->data.regex) {
621
- rb_re = (VALUE)pattern->data.regex;
622
- } else if (pattern->type == MONGORY_TYPE_STRING) {
623
- rb_re = rb_funcall(rb_cRegexp, rb_intern("new"), 1, (VALUE)pattern->origin);
624
- mongory_value *temp_value = mongory_value_wrap_regex(pool, (void *)rb_re);
625
- memcpy(pattern, temp_value, sizeof(mongory_value));
626
- pattern->origin = (void *)rb_re;
627
- rb_mongory_memory_pool_t *rb_pool = (rb_mongory_memory_pool_t *)pool;
628
- if (rb_pool->owner) {
629
- rb_pool->owner->mark_list->push(rb_pool->owner->mark_list, pattern);
630
- }
631
- } else {
632
- return false;
633
- }
634
-
635
- VALUE matched = rb_funcall(rb_re, rb_intern("match?"), 1, rb_str);
636
- return RTEST(matched);
637
- }
638
-
639
- // Regex adapter bridging to Ruby's Regexp
640
- static char *rb_mongory_regex_stringify_adapter(mongory_memory_pool *pool, mongory_value *pattern) {
641
- (void)pool;
642
- if (pattern->type != MONGORY_TYPE_REGEX) {
643
- return NULL;
644
- }
645
- VALUE rb_re = (VALUE)pattern->data.regex;
646
- VALUE rb_str = rb_funcall(rb_re, rb_intern("inspect"), 0);
647
- return StringValueCStr(rb_str);
648
- }
649
-
650
- // Custom matcher adapter bridging to Ruby's custom matcher
651
- static mongory_matcher_custom_context *rb_mongory_custom_matcher_build(char *key, mongory_value *condition, void *ctx) {
652
- mongory_memory_pool *pool = condition->pool;
653
- rb_mongory_memory_pool_t *rb_pool = (rb_mongory_memory_pool_t *)pool;
654
- rb_mongory_matcher_t *owner = rb_pool->owner;
655
- VALUE matcher_class = rb_funcall(mMongoryMatchers, rb_intern("lookup"), 1, cache_fetch_string(owner, key));
656
- if (matcher_class == Qnil) {
657
- return NULL;
658
- }
659
- VALUE kw_hash = rb_hash_new();
660
- rb_hash_aset(kw_hash, ID2SYM(rb_intern("context")), (VALUE)ctx);
661
-
662
- #ifdef RB_PASS_KEYWORDS
663
- VALUE argv_new[2] = { (VALUE)condition->origin, kw_hash };
664
- VALUE matcher = rb_funcallv_kw(matcher_class, rb_intern("new"), 2, argv_new, RB_PASS_KEYWORDS);
665
- #else
666
- VALUE matcher = rb_funcall(matcher_class, rb_intern("new"), 2, (VALUE)condition->origin, kw_hash);
667
- #endif
668
-
669
- if (matcher == Qnil) {
670
- return NULL;
671
- }
672
- mongory_value *matcher_value = mongory_value_wrap_u(pool, NULL);
673
- matcher_value->origin = (void *)matcher;
674
- owner->mark_list->push(owner->mark_list, matcher_value);
675
- VALUE class_name = rb_funcall(matcher_class, rb_intern("name"), 0);
676
- mongory_matcher_custom_context *return_ctx = MG_ALLOC_PTR(pool, mongory_matcher_custom_context);
677
- if (return_ctx == NULL) {
678
- return NULL;
679
- }
680
- return_ctx->name = StringValueCStr(class_name);
681
- return_ctx->external_matcher = (void *)matcher;
682
- return return_ctx;
683
- }
684
-
685
- // Custom matcher adapter bridging to Ruby's custom matcher
686
- static bool rb_mongory_custom_matcher_match(void *ruby_matcher, mongory_value *value) {
687
- VALUE matcher = (VALUE)ruby_matcher;
688
- VALUE match_result = rb_funcall(matcher, rb_intern("match?"), 1, value->origin);
689
- return RTEST(match_result);
690
- }
691
-
692
- // Custom matcher adapter bridging to Ruby's custom matcher
693
- static bool rb_mongory_custom_matcher_lookup(char *key) {
694
- VALUE matcher_class = rb_funcall(mMongoryMatchers, rb_intern("lookup"), 1, rb_str_new_cstr(key));
695
- return RTEST(matcher_class);
696
- }
697
-
698
- // Error handling for mongory_memory_pool
699
- static bool rb_mongory_error_handling(mongory_memory_pool *pool, char *error_message) {
36
+ bool rb_mongory_error_handling(mongory_memory_pool *pool, const char *error_message) {
700
37
  if (pool->error) {
701
- rb_raise(eMongoryTypeError, "%s: %s", error_message, pool->error->message);
38
+ const char *msg = pool->error->message;
702
39
  pool->error = NULL;
703
- return true;
40
+ rb_raise(rb_mongory_e_type_error, "%s: %s", error_message, msg);
704
41
  }
705
42
  return false;
706
43
  }
707
44
 
708
- /**
709
- * Extension initialization
710
- */
711
45
  void Init_mongory_ext(void) {
712
46
  // Initialize mongory core
713
47
  mongory_init();
714
48
 
715
49
  // Define modules and classes
716
- mMongory = rb_define_module("Mongory");
717
- cMongoryMatcher = rb_define_class_under(mMongory, "CMatcher", rb_cObject);
718
- rb_undef_alloc_func(cMongoryMatcher);
719
- mMongoryMatchers = rb_define_module_under(mMongory, "Matchers");
720
- VALUE mMongoryUtils = rb_define_module_under(mMongory, "Utils");
721
- cMongoryMatcherContext = rb_define_class_under(mMongoryUtils, "Context", rb_cObject);
50
+ rb_mongory_m_mongory = rb_define_module("Mongory");
51
+ rb_mongory_m_matchers = rb_define_module_under(rb_mongory_m_mongory, "Matchers");
52
+ VALUE mMongoryUtils = rb_define_module_under(rb_mongory_m_mongory, "Utils");
53
+ rb_mongory_c_matcher_context = rb_define_class_under(mMongoryUtils, "Context", rb_cObject);
54
+
722
55
  // Mongory converters
723
- inMongoryDataConverter = rb_funcall(mMongory, rb_intern("data_converter"), 0);
724
- inMongoryConditionConverter = rb_funcall(mMongory, rb_intern("condition_converter"), 0);
56
+ rb_mongory_data_converter = rb_funcall(rb_mongory_m_mongory, rb_intern("data_converter"), 0);
57
+ rb_mongory_condition_converter = rb_funcall(rb_mongory_m_mongory, rb_intern("condition_converter"), 0);
725
58
 
726
59
  // Define error classes
727
- eMongoryError = rb_define_class_under(mMongory, "Error", rb_eStandardError);
728
- eMongoryTypeError = rb_define_class_under(mMongory, "TypeError", eMongoryError);
729
-
730
- // Define Matcher methods
731
- rb_define_singleton_method(cMongoryMatcher, "new", rb_mongory_matcher_new, -1);
732
- rb_define_singleton_method(cMongoryMatcher, "trace_result_colorful=", rb_mongory_matcher_trace_result_colorful, 1);
733
- rb_define_method(cMongoryMatcher, "match?", rb_mongory_matcher_match, 1);
734
- rb_define_method(cMongoryMatcher, "explain", rb_mongory_matcher_explain, 0);
735
- rb_define_method(cMongoryMatcher, "condition", rb_mongory_matcher_condition, 0);
736
- rb_define_method(cMongoryMatcher, "context", rb_mongory_matcher_context, 0);
737
- rb_define_method(cMongoryMatcher, "trace", rb_mongory_matcher_trace, 1);
738
- rb_define_method(cMongoryMatcher, "enable_trace", rb_mongory_matcher_enable_trace, 0);
739
- rb_define_method(cMongoryMatcher, "disable_trace", rb_mongory_matcher_disable_trace, 0);
740
- rb_define_method(cMongoryMatcher, "print_trace", rb_mongory_matcher_print_trace, 0);
741
-
742
- // Set regex adapter to use Ruby's Regexp
743
- mongory_regex_func_set(rb_mongory_regex_match_adapter);
744
- mongory_regex_stringify_func_set(rb_mongory_regex_stringify_adapter);
745
-
746
- // Set value converter functions
747
- mongory_value_converter_deep_convert_set(rb_to_mongory_value_deep);
748
- mongory_value_converter_shallow_convert_set(rb_to_mongory_value_shallow);
749
- mongory_value_converter_recover_set(mongory_value_to_ruby);
60
+ rb_mongory_e_error = rb_define_class_under(rb_mongory_m_mongory, "Error", rb_eStandardError);
61
+ rb_mongory_e_type_error = rb_define_class_under(rb_mongory_m_mongory, "TypeError", rb_mongory_e_error);
750
62
 
751
- // Set custom matcher adapter
752
- mongory_custom_matcher_match_func_set(rb_mongory_custom_matcher_match);
753
- mongory_custom_matcher_build_func_set(rb_mongory_custom_matcher_build);
754
- mongory_custom_matcher_lookup_func_set(rb_mongory_custom_matcher_lookup);
63
+ // Register CMatcher class, methods, and core bridges
64
+ rb_mongory_init_cmatcher();
65
+ rb_mongory_register_adapters();
755
66
  }