mongory 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +30 -0
  3. data/README.md +55 -73
  4. data/docs/clang_bridge.md +2 -2
  5. data/docs/performance.md +43 -7
  6. data/ext/mongory_ext/extconf.rb +15 -0
  7. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +28 -43
  8. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +0 -1
  9. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +3 -4
  10. data/ext/mongory_ext/mongory-core/src/foundations/array.c +28 -5
  11. data/ext/mongory_ext/mongory-core/src/foundations/config.c +24 -22
  12. data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +3 -3
  13. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +22 -13
  14. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +4 -3
  15. data/ext/mongory_ext/mongory-core/src/foundations/table.c +60 -224
  16. data/ext/mongory_ext/mongory-core/src/foundations/table_private.h +36 -0
  17. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +30 -47
  18. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +2 -5
  19. data/ext/mongory_ext/mongory-core/src/foundations/value.c +18 -32
  20. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +89 -104
  21. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +18 -11
  22. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +35 -150
  23. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +244 -418
  24. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +30 -0
  25. data/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.c +15 -56
  26. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +18 -39
  27. data/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.c +14 -15
  28. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +382 -74
  29. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +373 -123
  30. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +20 -12
  31. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +1 -4
  32. data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.c +214 -171
  33. data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.h +8 -16
  34. data/ext/mongory_ext/mongory_ext.c +26 -4
  35. data/ext/mongory_ext/mongory_ext_internal.h +15 -3
  36. data/ext/mongory_ext/rb_mongory_adapters.c +52 -19
  37. data/ext/mongory_ext/rb_mongory_matcher.c +115 -36
  38. data/ext/mongory_ext/rb_mongory_value.c +124 -51
  39. data/lib/mongory/converters/abstract_converter.rb +5 -5
  40. data/lib/mongory/converters/converted.rb +0 -4
  41. data/lib/mongory/converters/key_converter.rb +13 -36
  42. data/lib/mongory/converters/value_converter.rb +4 -4
  43. data/lib/mongory/matchers.rb +8 -7
  44. data/lib/mongory/query_builder.rb +9 -0
  45. data/lib/mongory/utils/context.rb +0 -6
  46. data/lib/mongory/utils/singleton_builder.rb +1 -4
  47. data/lib/mongory/version.rb +1 -1
  48. data/lib/mongory.rb +0 -1
  49. data/script/bench.rb +77 -4
  50. data/script/coverage.rb +84 -0
  51. metadata +7 -7
  52. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +0 -117
  53. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +0 -29
@@ -0,0 +1,36 @@
1
+ #ifndef MONGORY_FOUNDATIONS_TABLE_PRIVATE_H
2
+ #define MONGORY_FOUNDATIONS_TABLE_PRIVATE_H
3
+
4
+ /**
5
+ * @file table_private.h
6
+ * @brief Internal structure definitions for `mongory_table` shared with whitebox tests.
7
+ */
8
+
9
+ #include <mongory-core/foundations/array.h>
10
+ #include <mongory-core/foundations/table.h>
11
+ #include <mongory-core/foundations/value.h>
12
+
13
+ /**
14
+ * @struct mongory_table_node
15
+ * @brief Represents a node in a hash table bucket's linked list.
16
+ * Stores a key-value pair and a pointer to the next node in the chain.
17
+ */
18
+ typedef struct mongory_table_node {
19
+ char *key; /**< The string key for this entry. */
20
+ mongory_value *value; /**< The mongory_value associated with the key. */
21
+ struct mongory_table_node *next; /**< Pointer to the next node in the collision chain. */
22
+ } mongory_table_node;
23
+
24
+ /**
25
+ * @struct mongory_table_internal
26
+ * @brief Internal representation of the hash table.
27
+ * Extends the public mongory_table structure with capacity information
28
+ * and the underlying array used for buckets.
29
+ */
30
+ typedef struct mongory_table_internal {
31
+ mongory_table base; /**< Public part of the table structure. */
32
+ size_t capacity; /**< Current number of buckets in the table. */
33
+ mongory_array *array; /**< Array of mongory_table_node pointers (the buckets). */
34
+ } mongory_table_internal;
35
+
36
+ #endif /* MONGORY_FOUNDATIONS_TABLE_PRIVATE_H */
@@ -67,7 +67,7 @@ bool mongory_try_parse_int(const char *key, int *out) {
67
67
  * allocation fails.
68
68
  */
69
69
  char *mongory_string_cpy(mongory_memory_pool *pool, char *str) {
70
- if (str == NULL) {
70
+ if (!mongory_validate_pool(pool) || str == NULL) {
71
71
  return NULL;
72
72
  }
73
73
 
@@ -83,7 +83,7 @@ char *mongory_string_cpy(mongory_memory_pool *pool, char *str) {
83
83
  }
84
84
 
85
85
  char *mongory_string_cpyf(mongory_memory_pool *pool, char *format, ...) {
86
- if (!pool || !format) {
86
+ if (!mongory_validate_pool(pool) || !format) {
87
87
  return NULL;
88
88
  }
89
89
  va_list args;
@@ -105,14 +105,14 @@ char *mongory_string_cpyf(mongory_memory_pool *pool, char *format, ...) {
105
105
  return new_str;
106
106
  }
107
107
 
108
- double mongory_log(double x, double base) {
109
- return log(x) / log(base);
110
- }
111
-
112
108
  void mongory_error_set(mongory_memory_pool *pool, mongory_error_type type, const char *message) {
113
109
  if (!pool || pool->error != NULL) {
114
110
  return;
115
111
  }
112
+ if (!pool->alloc) {
113
+ pool->error = &MONGORY_ALLOC_ERROR;
114
+ return;
115
+ }
116
116
  mongory_error *error = MG_ALLOC_PTR(pool, mongory_error);
117
117
  if (error == NULL) {
118
118
  pool->error = &MONGORY_ALLOC_ERROR;
@@ -123,8 +123,19 @@ void mongory_error_set(mongory_memory_pool *pool, mongory_error_type type, const
123
123
  pool->error = error;
124
124
  }
125
125
 
126
+ bool mongory_validate_pool(mongory_memory_pool *pool) {
127
+ if (pool == NULL || pool->error != NULL) {
128
+ return false;
129
+ }
130
+ if (pool->alloc == NULL) {
131
+ pool->error = &MONGORY_ALLOC_ERROR;
132
+ return false;
133
+ }
134
+ return true;
135
+ }
136
+
126
137
  bool mongory_validate_ptr(mongory_memory_pool *pool, char *name, void *ptr, char *file, int line) {
127
- if (!pool || pool->error != NULL) {
138
+ if (!mongory_validate_pool(pool)) {
128
139
  return false;
129
140
  }
130
141
  if (ptr == NULL) {
@@ -151,56 +162,28 @@ static bool mongory_validate_type_error(mongory_memory_pool *pool, char *name, c
151
162
  return false;
152
163
  }
153
164
 
154
- bool mongory_validate_table(mongory_memory_pool *pool, char *name, mongory_value *value, char *file, int line) {
155
- if (!pool || pool->error != NULL) {
156
- return false;
157
- }
165
+ static bool mongory_validate_value_type(mongory_memory_pool *pool, char *name, mongory_value *value,
166
+ mongory_type expected_type, const char *expected_name, void *payload_ptr,
167
+ char *file, int line) {
158
168
  if (!mongory_validate_ptr(pool, name, value, file, line)) {
159
169
  return false;
160
170
  }
161
- if (value->type != MONGORY_TYPE_TABLE) {
162
- return mongory_validate_type_error(pool, name, "Table", value, file, line);
171
+ if (value->type != expected_type) {
172
+ return mongory_validate_type_error(pool, name, expected_name, value, file, line);
163
173
  }
164
- return true;
174
+ return mongory_validate_ptr(pool, name, payload_ptr, file, line);
165
175
  }
166
176
 
167
- bool mongory_validate_array(mongory_memory_pool *pool, char *name, mongory_value *value, char *file, int line) {
168
- if (!pool || pool->error != NULL) {
169
- return false;
170
- }
171
- if (!mongory_validate_ptr(pool, name, value, file, line)) {
172
- return false;
173
- }
174
- if (value->type != MONGORY_TYPE_ARRAY) {
175
- return mongory_validate_type_error(pool, name, "Array", value, file, line);
176
- }
177
- return true;
177
+ bool mongory_validate_table(mongory_memory_pool *pool, char *name, mongory_value *value, char *file, int line) {
178
+ return mongory_validate_value_type(pool, name, value, MONGORY_TYPE_TABLE, "Table", value ? value->data.t : NULL, file, line);
178
179
  }
179
180
 
180
- bool mongory_validate_string(mongory_memory_pool *pool, char *name, mongory_value *value, char *file, int line) {
181
- if (!pool || pool->error != NULL) {
182
- return false;
183
- }
184
- if (!mongory_validate_ptr(pool, name, value, file, line)) {
185
- return false;
186
- }
187
- if (value->type != MONGORY_TYPE_STRING) {
188
- return mongory_validate_type_error(pool, name, "String", value, file, line);
189
- }
190
- return true;
181
+ bool mongory_validate_array(mongory_memory_pool *pool, char *name, mongory_value *value, char *file, int line) {
182
+ return mongory_validate_value_type(pool, name, value, MONGORY_TYPE_ARRAY, "Array", value ? value->data.a : NULL, file, line);
191
183
  }
192
184
 
193
- bool mongory_validate_number(mongory_memory_pool *pool, char *name, mongory_value *value, char *file, int line) {
194
- if (!pool || pool->error != NULL) {
195
- return false;
196
- }
197
- if (!mongory_validate_ptr(pool, name, value, file, line)) {
198
- return false;
199
- }
200
- if (value->type != MONGORY_TYPE_INT && value->type != MONGORY_TYPE_DOUBLE) {
201
- return mongory_validate_type_error(pool, name, "Number", value, file, line);
202
- }
203
- return true;
185
+ bool mongory_validate_string(mongory_memory_pool *pool, char *name, mongory_value *value, char *file, int line) {
186
+ return mongory_validate_value_type(pool, name, value, MONGORY_TYPE_STRING, "String", value ? value->data.s : NULL, file, line);
204
187
  }
205
188
 
206
189
  #endif // MONGORY_UTILS_C
@@ -50,8 +50,6 @@ char *mongory_string_cpy(mongory_memory_pool *pool, char *str);
50
50
  */
51
51
  char *mongory_string_cpyf(mongory_memory_pool *pool, char *format, ...);
52
52
 
53
- double mongory_log(double x, double base);
54
-
55
53
  /**
56
54
  * @brief Sets `pool->error` safely without dereferencing NULL on OOM.
57
55
  * If allocating `mongory_error` from `pool` fails, falls back to the static
@@ -59,6 +57,8 @@ double mongory_log(double x, double base);
59
57
  */
60
58
  void mongory_error_set(mongory_memory_pool *pool, mongory_error_type type, const char *message);
61
59
 
60
+ bool mongory_validate_pool(mongory_memory_pool *pool);
61
+
62
62
  bool mongory_validate_ptr(mongory_memory_pool *pool, char *name, void *ptr, char *file, int line);
63
63
  #define MONGORY_VALIDATE_PTR(pool, ptr) mongory_validate_ptr(pool, #ptr, ptr, __FILE__, __LINE__)
64
64
 
@@ -71,7 +71,4 @@ bool mongory_validate_array(mongory_memory_pool *pool, char *name, mongory_value
71
71
  bool mongory_validate_string(mongory_memory_pool *pool, char *name, mongory_value *value, char *file, int line);
72
72
  #define MONGORY_VALIDATE_STRING(pool, value) mongory_validate_string(pool, #value, value, __FILE__, __LINE__)
73
73
 
74
- bool mongory_validate_number(mongory_memory_pool *pool, char *name, mongory_value *value, char *file, int line);
75
- #define MONGORY_VALIDATE_NUMBER(pool, value) mongory_validate_number(pool, #value, value, __FILE__, __LINE__)
76
-
77
74
  #endif // MONGORY_UTILS_H
@@ -77,7 +77,7 @@ static char *mongory_value_generic_ptr_to_str(mongory_value *value, mongory_memo
77
77
  * @return A pointer to the new mongory_value, or NULL on allocation failure.
78
78
  */
79
79
  static inline mongory_value *mongory_value_new(mongory_memory_pool *pool) {
80
- if (!pool || !pool->alloc)
80
+ if (!mongory_validate_pool(pool))
81
81
  return NULL; // Invalid pool.
82
82
  mongory_value *value = MG_ALLOC_PTR(pool, mongory_value);
83
83
  if (!value) {
@@ -233,12 +233,6 @@ mongory_value *mongory_value_wrap_s(mongory_memory_pool *pool, char *s_val) {
233
233
  if (!value)
234
234
  return NULL;
235
235
  value->type = MONGORY_TYPE_STRING;
236
- // `mongory_string_cpy` handles the allocation and copying of the string.
237
- // If `s_val` is NULL, `value->data.s` will be NULL.
238
- // If allocation fails, `value->data.s` will also be NULL.
239
- // TODO: This function should ideally return NULL if the string copy fails
240
- // when `s_val` was not NULL, but this requires a more complex error handling
241
- // strategy with the memory pool.
242
236
  value->data.s = mongory_string_cpy(pool, s_val);
243
237
  if (s_val != NULL && value->data.s == NULL) {
244
238
  return NULL;
@@ -385,6 +379,9 @@ mongory_value *mongory_value_wrap_u(mongory_memory_pool *pool, void *u_val) {
385
379
  }
386
380
 
387
381
  static char *mongory_value_regex_to_str(mongory_value *value, mongory_memory_pool *pool) {
382
+ if (!MONGORY_VALIDATE_PTR(pool, value) || !mongory_internal_regex_adapter.stringify_func) {
383
+ return NULL;
384
+ }
388
385
  char *str = mongory_internal_regex_adapter.stringify_func(pool, value);
389
386
  if (!str)
390
387
  return NULL;
@@ -429,28 +426,28 @@ static char *mongory_value_null_to_str(mongory_value *value, mongory_memory_pool
429
426
  }
430
427
 
431
428
  static char *mongory_value_bool_to_str(mongory_value *value, mongory_memory_pool *pool) {
432
- if (!MONGORY_VALIDATE_PTR(pool, &value->data.b)) {
429
+ if (!MONGORY_VALIDATE_PTR(pool, value)) {
433
430
  return NULL;
434
431
  }
435
432
  return mongory_string_cpy(pool, value->data.b ? "true" : "false");
436
433
  }
437
434
 
438
435
  static char *mongory_value_int_to_str(mongory_value *value, mongory_memory_pool *pool) {
439
- if (!MONGORY_VALIDATE_PTR(pool, &value->data.i)) {
436
+ if (!MONGORY_VALIDATE_PTR(pool, value)) {
440
437
  return NULL;
441
438
  }
442
439
  return mongory_string_cpyf(pool, "%lld", (long long)value->data.i);
443
440
  }
444
441
 
445
442
  static char *mongory_value_double_to_str(mongory_value *value, mongory_memory_pool *pool) {
446
- if (!MONGORY_VALIDATE_PTR(pool, &value->data.d)) {
443
+ if (!MONGORY_VALIDATE_PTR(pool, value)) {
447
444
  return NULL;
448
445
  }
449
446
  return mongory_string_cpyf(pool, "%f", value->data.d);
450
447
  }
451
448
 
452
449
  static char *mongory_value_string_to_str(mongory_value *value, mongory_memory_pool *pool) {
453
- if (!MONGORY_VALIDATE_PTR(pool, value->data.s)) {
450
+ if (!MONGORY_VALIDATE_STRING(pool, value)) {
454
451
  return NULL;
455
452
  }
456
453
  return mongory_string_cpyf(pool, "\"%s\"", value->data.s);
@@ -466,9 +463,7 @@ static bool mongory_value_array_to_str_each(mongory_value *value, void *ctx) {
466
463
  mongory_value_container_to_str_ctx *context = (mongory_value_container_to_str_ctx *)ctx;
467
464
  mongory_string_buffer *buffer = context->buffer;
468
465
  mongory_memory_pool *pool = buffer->pool;
469
- MONGORY_VALIDATE_PTR(pool, value);
470
- MONGORY_VALIDATE_PTR(pool, value->to_str);
471
- if (pool->error != NULL) {
466
+ if (!MONGORY_VALIDATE_PTR(pool, value) || !MONGORY_VALIDATE_PTR(pool, value->to_str)) {
472
467
  return false;
473
468
  }
474
469
  char *str = value->to_str(value, pool);
@@ -483,16 +478,13 @@ static bool mongory_value_array_to_str_each(mongory_value *value, void *ctx) {
483
478
  }
484
479
 
485
480
  static char *mongory_value_array_to_str(mongory_value *value, mongory_memory_pool *pool) {
481
+ if (!MONGORY_VALIDATE_ARRAY(pool, value) || !MONGORY_VALIDATE_PTR(pool, value->data.a->each)) {
482
+ return NULL;
483
+ }
486
484
  mongory_string_buffer *buffer = mongory_string_buffer_new(pool);
487
485
  if (!buffer)
488
486
  return NULL;
489
487
  mongory_array *array = value->data.a;
490
- MONGORY_VALIDATE_PTR(pool, array);
491
- MONGORY_VALIDATE_PTR(pool, array->each);
492
- MONGORY_VALIDATE_PTR(pool, &array->count);
493
- if (pool->error != NULL) {
494
- return NULL;
495
- }
496
488
  mongory_string_buffer_append(buffer, "[");
497
489
  mongory_value_container_to_str_ctx ctx = {.count = 0, .total = array->count, .buffer = buffer};
498
490
  array->each(array, &ctx, mongory_value_array_to_str_each);
@@ -504,10 +496,8 @@ static bool mongory_value_table_to_str_each(char *key, mongory_value *value, voi
504
496
  mongory_value_container_to_str_ctx *context = (mongory_value_container_to_str_ctx *)ctx;
505
497
  mongory_string_buffer *buffer = context->buffer;
506
498
  mongory_memory_pool *pool = buffer->pool;
507
- MONGORY_VALIDATE_PTR(pool, key);
508
- MONGORY_VALIDATE_PTR(pool, value);
509
- MONGORY_VALIDATE_PTR(pool, value->to_str);
510
- if (pool->error != NULL) {
499
+ if (!MONGORY_VALIDATE_PTR(pool, key) || !MONGORY_VALIDATE_PTR(pool, value) ||
500
+ !MONGORY_VALIDATE_PTR(pool, value->to_str)) {
511
501
  return false;
512
502
  }
513
503
  mongory_string_buffer_appendf(buffer, "\"%s\":", key);
@@ -523,17 +513,13 @@ static bool mongory_value_table_to_str_each(char *key, mongory_value *value, voi
523
513
  }
524
514
 
525
515
  static char *mongory_value_table_to_str(mongory_value *value, mongory_memory_pool *pool) {
516
+ if (!MONGORY_VALIDATE_TABLE(pool, value) || !MONGORY_VALIDATE_PTR(pool, value->data.t->each)) {
517
+ return NULL;
518
+ }
526
519
  mongory_string_buffer *buffer = mongory_string_buffer_new(pool);
527
520
  if (!buffer)
528
521
  return NULL;
529
522
  mongory_table *table = value->data.t;
530
- if (!MONGORY_VALIDATE_PTR(pool, table) || !MONGORY_VALIDATE_PTR(pool, table->each) ||
531
- !MONGORY_VALIDATE_PTR(pool, &table->count)) {
532
- return NULL;
533
- }
534
- if (pool->error != NULL) {
535
- return NULL;
536
- }
537
523
  mongory_string_buffer_append(buffer, "{");
538
524
  mongory_value_container_to_str_ctx ctx = {.count = 0, .total = table->count, .buffer = buffer};
539
525
  table->each(table, &ctx, mongory_value_table_to_str_each);
@@ -542,7 +528,7 @@ static char *mongory_value_table_to_str(mongory_value *value, mongory_memory_poo
542
528
  }
543
529
 
544
530
  static char *mongory_value_generic_ptr_to_str(mongory_value *value, mongory_memory_pool *pool) {
545
- if (!MONGORY_VALIDATE_PTR(pool, value->data.ptr))
531
+ if (!MONGORY_VALIDATE_PTR(pool, value) || !MONGORY_VALIDATE_PTR(pool, value->data.ptr))
546
532
  return NULL;
547
533
  return mongory_string_cpyf(pool, "%p", value->data.ptr);
548
534
  }
@@ -11,7 +11,7 @@
11
11
  #include <stdbool.h>
12
12
  #include <stdio.h> // For printf
13
13
  #include <stdlib.h> // For strtol
14
- #include "matcher_explainable.h"
14
+ #include <string.h> // For memset
15
15
  #include "matcher_traversable.h"
16
16
  #include "../foundations/utils.h"
17
17
 
@@ -21,30 +21,31 @@
21
21
  # define MONGORY_THREAD_LOCAL __thread
22
22
  #endif
23
23
 
24
- static MONGORY_THREAD_LOCAL mongory_compile_budget mongory_tls_compile_budget = {0};
24
+ static MONGORY_THREAD_LOCAL mongory_compile_ctx mongory_tls_compile_ctx = {0};
25
25
 
26
- mongory_compile_budget mongory_compile_budget_begin(void) {
27
- mongory_compile_budget saved = mongory_tls_compile_budget;
28
- mongory_tls_compile_budget.active = true;
29
- mongory_tls_compile_budget.depth = 0;
30
- mongory_tls_compile_budget.nodes = 0;
31
- mongory_tls_compile_budget.max_depth = mongory_max_depth_get();
32
- mongory_tls_compile_budget.max_nodes = mongory_max_nodes_get();
26
+ mongory_compile_ctx mongory_compile_ctx_begin(void) {
27
+ mongory_compile_ctx saved = mongory_tls_compile_ctx;
28
+ mongory_tls_compile_ctx.active = true;
29
+ mongory_tls_compile_ctx.depth = 0;
30
+ mongory_tls_compile_ctx.nodes = 0;
31
+ mongory_tls_compile_ctx.max_depth = mongory_max_depth_get();
32
+ mongory_tls_compile_ctx.max_nodes = mongory_max_nodes_get();
33
+ mongory_tls_compile_ctx.field_depth = 0;
33
34
  return saved;
34
35
  }
35
36
 
36
- void mongory_compile_budget_end(mongory_compile_budget saved) {
37
- mongory_tls_compile_budget = saved;
37
+ void mongory_compile_ctx_end(mongory_compile_ctx saved) {
38
+ mongory_tls_compile_ctx = saved;
38
39
  }
39
40
 
40
- bool mongory_compile_budget_enter_depth(mongory_memory_pool *pool) {
41
- if (!mongory_tls_compile_budget.active) {
41
+ bool mongory_compile_ctx_enter_depth(mongory_memory_pool *pool) {
42
+ if (!mongory_tls_compile_ctx.active) {
42
43
  return true;
43
44
  }
44
- mongory_tls_compile_budget.depth++;
45
- if (mongory_tls_compile_budget.max_depth > 0 &&
46
- mongory_tls_compile_budget.depth > mongory_tls_compile_budget.max_depth) {
47
- mongory_tls_compile_budget.depth--;
45
+ mongory_tls_compile_ctx.depth++;
46
+ if (mongory_tls_compile_ctx.max_depth > 0 &&
47
+ mongory_tls_compile_ctx.depth > mongory_tls_compile_ctx.max_depth) {
48
+ mongory_tls_compile_ctx.depth--;
48
49
  if (pool && pool->error == NULL) {
49
50
  pool->error = &MONGORY_DEPTH_LIMIT_ERROR;
50
51
  }
@@ -53,19 +54,19 @@ bool mongory_compile_budget_enter_depth(mongory_memory_pool *pool) {
53
54
  return true;
54
55
  }
55
56
 
56
- void mongory_compile_budget_leave_depth(void) {
57
- if (mongory_tls_compile_budget.active && mongory_tls_compile_budget.depth > 0) {
58
- mongory_tls_compile_budget.depth--;
57
+ void mongory_compile_ctx_leave_depth(void) {
58
+ if (mongory_tls_compile_ctx.active && mongory_tls_compile_ctx.depth > 0) {
59
+ mongory_tls_compile_ctx.depth--;
59
60
  }
60
61
  }
61
62
 
62
- bool mongory_compile_budget_count_node(mongory_memory_pool *pool) {
63
- if (!mongory_tls_compile_budget.active) {
63
+ bool mongory_compile_ctx_count_node(mongory_memory_pool *pool) {
64
+ if (!mongory_tls_compile_ctx.active) {
64
65
  return true;
65
66
  }
66
- mongory_tls_compile_budget.nodes++;
67
- if (mongory_tls_compile_budget.max_nodes > 0 &&
68
- mongory_tls_compile_budget.nodes > mongory_tls_compile_budget.max_nodes) {
67
+ mongory_tls_compile_ctx.nodes++;
68
+ if (mongory_tls_compile_ctx.max_nodes > 0 &&
69
+ mongory_tls_compile_ctx.nodes > mongory_tls_compile_ctx.max_nodes) {
69
70
  if (pool && pool->error == NULL) {
70
71
  pool->error = &MONGORY_NODE_LIMIT_ERROR;
71
72
  }
@@ -74,6 +75,30 @@ bool mongory_compile_budget_count_node(mongory_memory_pool *pool) {
74
75
  return true;
75
76
  }
76
77
 
78
+ bool mongory_compile_ctx_in_field(void) {
79
+ return mongory_tls_compile_ctx.field_depth > 0;
80
+ }
81
+
82
+ void mongory_compile_ctx_enter_field(void) {
83
+ mongory_tls_compile_ctx.field_depth++;
84
+ }
85
+
86
+ void mongory_compile_ctx_leave_field(void) {
87
+ if (mongory_tls_compile_ctx.field_depth > 0) {
88
+ mongory_tls_compile_ctx.field_depth--;
89
+ }
90
+ }
91
+
92
+ size_t mongory_compile_ctx_reset_field(void) {
93
+ size_t saved = mongory_tls_compile_ctx.field_depth;
94
+ mongory_tls_compile_ctx.field_depth = 0;
95
+ return saved;
96
+ }
97
+
98
+ void mongory_compile_ctx_restore_field(size_t saved_field_depth) {
99
+ mongory_tls_compile_ctx.field_depth = saved_field_depth;
100
+ }
101
+
77
102
  /**
78
103
  * @brief Allocates and initializes common fields of a `mongory_matcher`.
79
104
  *
@@ -90,104 +115,64 @@ bool mongory_compile_budget_count_node(mongory_memory_pool *pool) {
90
115
  * @return mongory_matcher* Pointer to the newly allocated and partially
91
116
  * initialized matcher, or NULL if allocation fails.
92
117
  */
93
- mongory_matcher *mongory_matcher_base_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
94
- if (!pool || !pool->alloc) {
95
- return NULL; // Invalid memory pool.
118
+ void *mongory_matcher_alloc_init(mongory_memory_pool *pool, size_t size, mongory_value *condition, void *extern_ctx,
119
+ mongory_matcher_match_func match_fn, mongory_matcher_traverse_func traverse_fn,
120
+ const char *name, double priority) {
121
+ if (!mongory_validate_pool(pool)) {
122
+ return NULL;
96
123
  }
97
- if (!mongory_compile_budget_count_node(pool)) {
124
+ if (!mongory_compile_ctx_count_node(pool)) {
98
125
  return NULL;
99
126
  }
100
- mongory_matcher *matcher = MG_ALLOC_PTR(pool, mongory_matcher);
101
- if (matcher == NULL) {
102
- // Allocation failed, pool->alloc might set pool->error.
127
+ void *raw = MG_ALLOC(pool, size);
128
+ if (raw == NULL) {
103
129
  pool->error = &MONGORY_ALLOC_ERROR;
104
130
  return NULL;
105
131
  }
132
+ memset(raw, 0, size);
106
133
 
107
- // Initialize common fields
108
- matcher->original_match = NULL;
109
- matcher->sub_count = 0;
134
+ mongory_matcher *matcher = (mongory_matcher *)raw;
110
135
  matcher->pool = pool;
111
136
  matcher->condition = condition;
112
- matcher->name = NULL; // Name is not set by base_new.
113
- matcher->match = NULL; // Specific match function must be set by derived type.
114
- matcher->explain = mongory_matcher_base_explain; // Specific explain function must be set by derived type.
115
- matcher->traverse = mongory_matcher_leaf_traverse;
116
- matcher->trace_stack = NULL;
117
- matcher->trace_level = 0;
118
- matcher->extern_ctx = extern_ctx; // Set the external context.
119
- matcher->priority = 1.0; // Set the priority to 1.0.
120
- matcher->parent = NULL; // Attached later when wrapped by a parent.
121
- return matcher;
137
+ matcher->match = match_fn;
138
+ matcher->original_match = match_fn;
139
+ matcher->traverse = traverse_fn ? traverse_fn : mongory_matcher_leaf_traverse;
140
+ matcher->extern_ctx = extern_ctx;
141
+ matcher->priority = priority;
142
+ if (name != NULL) {
143
+ matcher->name = mongory_string_cpy(pool, (char *)name);
144
+ if (matcher->name == NULL) {
145
+ return NULL;
146
+ }
147
+ }
148
+ return raw;
149
+ }
150
+
151
+ mongory_matcher *mongory_matcher_base_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
152
+ return (mongory_matcher *)mongory_matcher_alloc_init(
153
+ pool, sizeof(mongory_matcher), condition, extern_ctx, NULL, mongory_matcher_leaf_traverse, NULL, 1.0);
122
154
  }
123
155
 
124
- /**
125
- * @brief The match function for a matcher that always returns true.
126
- * @param matcher Unused.
127
- * @param value Unused.
128
- * @return Always true.
129
- */
130
156
  static inline bool mongory_matcher_always_true_match(mongory_matcher *matcher, mongory_value *value) {
131
- (void)matcher; // Mark as unused to prevent compiler warnings.
132
- (void)value; // Mark as unused.
133
- return true; // This matcher always indicates a match.
157
+ (void)matcher;
158
+ (void)value;
159
+ return true;
134
160
  }
135
161
 
136
- /**
137
- * @brief Creates a matcher instance that will always evaluate to true.
138
- * Useful as a placeholder or for default cases.
139
- * @param pool Memory pool for allocation.
140
- * @param condition Condition (typically ignored by this matcher).
141
- * @return A new `mongory_matcher` or NULL on failure.
142
- */
143
162
  mongory_matcher *mongory_matcher_always_true_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
144
- mongory_matcher *matcher = mongory_matcher_base_new(pool, condition, extern_ctx);
145
- if (!matcher) {
146
- return NULL;
147
- }
148
- matcher->match = mongory_matcher_always_true_match;
149
- matcher->original_match = mongory_matcher_always_true_match;
150
- matcher->name = mongory_string_cpy(pool, "Always True");
151
- if (!matcher->name) {
152
- return NULL;
153
- }
154
- matcher->explain = mongory_matcher_base_explain;
155
- // Optionally set original_match as well if it's a strict policy
156
- // matcher->context.original_match = mongory_matcher_always_true_match;
157
- return matcher;
163
+ return (mongory_matcher *)mongory_matcher_alloc_init(
164
+ pool, sizeof(mongory_matcher), condition, extern_ctx, mongory_matcher_always_true_match,
165
+ mongory_matcher_leaf_traverse, "Always True", 1.0);
158
166
  }
159
167
 
160
- /**
161
- * @brief The match function for a matcher that always returns false.
162
- * @param matcher Unused.
163
- * @param value Unused.
164
- * @return Always false.
165
- */
166
168
  static inline bool mongory_matcher_always_false_match(mongory_matcher *matcher, mongory_value *value) {
167
- (void)matcher; // Mark as unused.
168
- (void)value; // Mark as unused.
169
- return false; // This matcher never indicates a match.
169
+ (void)matcher;
170
+ (void)value;
171
+ return false;
170
172
  }
171
173
 
172
- /**
173
- * @brief Creates a matcher instance that will always evaluate to false.
174
- * Useful for conditions that should never match.
175
- * @param pool Memory pool for allocation.
176
- * @param condition Condition (typically ignored by this matcher).
177
- * @return A new `mongory_matcher` or NULL on failure.
178
- */
179
174
  mongory_matcher *mongory_matcher_always_false_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
180
- mongory_matcher *matcher = mongory_matcher_base_new(pool, condition, extern_ctx);
181
- if (!matcher) {
182
- return NULL;
183
- }
184
- matcher->match = mongory_matcher_always_false_match;
185
- matcher->original_match = mongory_matcher_always_false_match;
186
- matcher->name = mongory_string_cpy(pool, "Always False");
187
- if (!matcher->name) {
188
- return NULL;
189
- }
190
- matcher->explain = mongory_matcher_base_explain;
191
- // matcher->context.original_match = mongory_matcher_always_false_match;
192
- return matcher;
175
+ return (mongory_matcher *)mongory_matcher_alloc_init(
176
+ pool, sizeof(mongory_matcher), condition, extern_ctx, mongory_matcher_always_false_match,
177
+ mongory_matcher_leaf_traverse, "Always False", 1.0);
193
178
  }
@@ -15,9 +15,9 @@
15
15
  #include "mongory-core/foundations/memory_pool.h"
16
16
  #include "mongory-core/foundations/value.h"
17
17
  #include "mongory-core/matchers/matcher.h" // For mongory_matcher structure
18
- #include "matcher_explainable.h"
19
18
  #include "matcher_traversable.h"
20
19
  #include <stdbool.h>
20
+ #include <stddef.h>
21
21
 
22
22
  /**
23
23
  * @brief Function pointer type for a matcher's core matching logic.
@@ -46,11 +46,8 @@ struct mongory_matcher {
46
46
  logic for this matcher type. */
47
47
  mongory_memory_pool *pool; /**< The memory pool used for allocations related
48
48
  to this matcher instance. */
49
- mongory_matcher_traverse_func explain; /**< Function pointer to the explanation
50
- logic for this matcher type. */
51
49
  mongory_matcher_match_func original_match; /**< Stores the original match function, potentially for
52
50
  restoration or delegation. */
53
- size_t sub_count; /**< The number of sub-matchers. */
54
51
  mongory_matcher_traverse_func traverse; /**< Function pointer to the traversal logic for this matcher type. */
55
52
  mongory_array *trace_stack; /**< The trace stack for this matcher. */
56
53
  int trace_level; /**< The trace level for this matcher. */
@@ -74,6 +71,10 @@ struct mongory_matcher {
74
71
  * @return mongory_matcher* A pointer to the newly allocated base matcher, or
75
72
  * NULL on allocation failure.
76
73
  */
74
+ void *mongory_matcher_alloc_init(mongory_memory_pool *pool, size_t size, mongory_value *condition, void *extern_ctx,
75
+ mongory_matcher_match_func match_fn, mongory_matcher_traverse_func traverse_fn,
76
+ const char *name, double priority);
77
+
77
78
  mongory_matcher *mongory_matcher_base_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
78
79
 
79
80
  /**
@@ -100,18 +101,24 @@ mongory_matcher *mongory_matcher_always_false_new(mongory_memory_pool *pool, mon
100
101
 
101
102
  void mongory_matcher_enable_trace_at(mongory_matcher *matcher, mongory_array *trace_stack, int level);
102
103
 
103
- typedef struct mongory_compile_budget {
104
+ typedef struct mongory_compile_ctx {
104
105
  bool active;
105
106
  size_t depth;
106
107
  size_t nodes;
107
108
  size_t max_depth;
108
109
  size_t max_nodes;
109
- } mongory_compile_budget;
110
+ size_t field_depth;
111
+ } mongory_compile_ctx;
110
112
 
111
- mongory_compile_budget mongory_compile_budget_begin(void);
112
- void mongory_compile_budget_end(mongory_compile_budget saved);
113
- bool mongory_compile_budget_enter_depth(mongory_memory_pool *pool);
114
- void mongory_compile_budget_leave_depth(void);
115
- bool mongory_compile_budget_count_node(mongory_memory_pool *pool);
113
+ mongory_compile_ctx mongory_compile_ctx_begin(void);
114
+ void mongory_compile_ctx_end(mongory_compile_ctx saved);
115
+ bool mongory_compile_ctx_enter_depth(mongory_memory_pool *pool);
116
+ void mongory_compile_ctx_leave_depth(void);
117
+ bool mongory_compile_ctx_count_node(mongory_memory_pool *pool);
118
+ bool mongory_compile_ctx_in_field(void);
119
+ void mongory_compile_ctx_enter_field(void);
120
+ void mongory_compile_ctx_leave_field(void);
121
+ size_t mongory_compile_ctx_reset_field(void);
122
+ void mongory_compile_ctx_restore_field(size_t saved_field_depth);
116
123
 
117
124
  #endif /* MONGORY_MATCHER_BASE_H */