23
23
#include "filterx-cache-json-file.h"
24
24
#include "filterx/object-json.h"
25
25
#include "filterx/object-string.h"
26
+ #include "filterx/object-list-interface.h"
27
+ #include "filterx/object-dict-interface.h"
26
28
#include "filterx/expr-literal.h"
27
29
#include "filterx/filterx-eval.h"
28
30
#include "scratch-buffers.h"
@@ -54,6 +56,7 @@ typedef struct FilterXFunctionCacheJsonFile_
54
56
FilterXFunction super ;
55
57
gchar * filepath ;
56
58
FilterXObject * cached_json ;
59
+ GPtrArray * frozen_objects ;
57
60
} FilterXFuntionCacheJsonFile ;
58
61
59
62
static gchar *
@@ -143,11 +146,56 @@ _free(FilterXExpr *s)
143
146
FilterXFuntionCacheJsonFile * self = (FilterXFuntionCacheJsonFile * ) s ;
144
147
145
148
g_free (self -> filepath );
146
- if (self -> cached_json )
147
- filterx_object_unfreeze_and_free (self -> cached_json );
149
+ g_ptr_array_unref (self -> frozen_objects );
148
150
filterx_function_free_method (& self -> super );
149
151
}
150
152
153
+ static void _deep_freeze (FilterXFuntionCacheJsonFile * self , FilterXObject * object );
154
+
155
+ static void
156
+ _deep_freeze_dict (FilterXFuntionCacheJsonFile * self , FilterXObject * object )
157
+ {
158
+ struct json_object_iter itr ;
159
+ json_object_object_foreachC (filterx_json_object_get_value (object ), itr )
160
+ {
161
+ struct json_object * elem_jso = itr .val ;
162
+ FilterXObject * elem_object = filterx_json_convert_json_to_object (self -> cached_json , NULL , elem_jso );
163
+ _deep_freeze (self , elem_object );
164
+ filterx_json_associate_cached_object (elem_jso , elem_object );
165
+ }
166
+ }
167
+
168
+ static void
169
+ _deep_freeze_list (FilterXFuntionCacheJsonFile * self , FilterXObject * object )
170
+ {
171
+ struct json_object * jso = filterx_json_object_get_value (object );
172
+ guint64 len = json_object_array_length (jso );
173
+
174
+ for (guint64 i = 0 ; i < len ; i ++ )
175
+ {
176
+ struct json_object * elem_jso = json_object_array_get_idx (jso , i );
177
+ FilterXObject * elem_object = filterx_json_convert_json_to_object (self -> cached_json , NULL , elem_jso );
178
+ _deep_freeze (self , elem_object );
179
+ filterx_json_associate_cached_object (elem_jso , elem_object );
180
+ }
181
+ }
182
+
183
+ static void
184
+ _deep_freeze (FilterXFuntionCacheJsonFile * self , FilterXObject * object )
185
+ {
186
+ if (!object )
187
+ return ;
188
+
189
+ if (filterx_object_freeze (object ))
190
+ g_ptr_array_add (self -> frozen_objects , object );
191
+
192
+ if (filterx_object_is_type (object , & FILTERX_TYPE_NAME (json_object )))
193
+ _deep_freeze_dict (self , object );
194
+
195
+ if (filterx_object_is_type (object , & FILTERX_TYPE_NAME (json_array )))
196
+ _deep_freeze_list (self , object );
197
+ }
198
+
151
199
FilterXFunction *
152
200
filterx_function_cache_json_file_new (const gchar * function_name , FilterXFunctionArgs * args , GError * * error )
153
201
{
@@ -157,6 +205,8 @@ filterx_function_cache_json_file_new(const gchar *function_name, FilterXFunction
157
205
self -> super .super .eval = _eval ;
158
206
self -> super .super .free_fn = _free ;
159
207
208
+ self -> frozen_objects = g_ptr_array_new_with_free_func ((GDestroyNotify ) filterx_object_unfreeze_and_free );
209
+
160
210
self -> filepath = _extract_filepath (args , error );
161
211
if (!self -> filepath )
162
212
goto error ;
@@ -165,7 +215,8 @@ filterx_function_cache_json_file_new(const gchar *function_name, FilterXFunction
165
215
if (!self -> cached_json )
166
216
goto error ;
167
217
168
- filterx_object_freeze (self -> cached_json );
218
+ _deep_freeze (self , self -> cached_json );
219
+
169
220
filterx_function_args_free (args );
170
221
return & self -> super ;
171
222
0 commit comments