@@ -188,6 +188,13 @@ bool valid_int_var(const char* name)
188188 return valid_suffix_var (name , '\0' );
189189}
190190
191+ static void update_string (struct basic_ctx * ctx , ub_var_string * str , size_t len , bool propagate_global , const char * varname , const char * value ) {
192+ str -> name_length = len ;
193+ str -> global = propagate_global ;
194+ str -> varname = buddy_strdup (ctx -> allocator , varname );
195+ str -> value = buddy_strdup (ctx -> allocator , value );
196+ }
197+
191198void basic_set_string_variable (const char * var , const char * value , struct basic_ctx * ctx , bool local , bool propagate_global ) {
192199 struct hashmap * locals = ctx -> local_string_variables [ctx -> call_stack_ptr ];
193200 struct hashmap * globals = ctx -> str_variables ;
@@ -203,26 +210,17 @@ void basic_set_string_variable(const char* var, const char* value, struct basic_
203210 if (local && locals && (found = hashmap_get (locals , & (ub_var_string ) { .varname = var }))) {
204211 buddy_free (ctx -> allocator , found -> varname );
205212 buddy_free (ctx -> allocator , found -> value );
206- found -> name_length = len ;
207- found -> global = propagate_global ;
208- found -> varname = buddy_strdup (ctx -> allocator , var );
209- found -> value = buddy_strdup (ctx -> allocator , value );
213+ update_string (ctx , found , len , propagate_global , var , value );
210214 oom = !hashmap_set (locals , found ) && hashmap_oom (locals );
211215 } else if ((found = hashmap_get (globals , & (ub_var_string ) { .varname = var }))) {
212216 buddy_free (ctx -> allocator , found -> varname );
213217 buddy_free (ctx -> allocator , found -> value );
214- found -> name_length = len ;
215- found -> global = propagate_global ;
216- found -> varname = buddy_strdup (ctx -> allocator , var );
217- found -> value = buddy_strdup (ctx -> allocator , value );
218+ update_string (ctx , found , len , propagate_global , var , value );
218219 oom = !hashmap_set (globals , found ) && hashmap_oom (globals );
219220 } else {
220221 struct hashmap * target = local && locals ? locals : globals ;
221222 ub_var_string new ;
222- new .name_length = len ;
223- new .global = propagate_global ;
224- new .varname = buddy_strdup (ctx -> allocator , var );
225- new .value = buddy_strdup (ctx -> allocator , value );
223+ update_string (ctx , & new , len , propagate_global , var , value );
226224 oom = !hashmap_set (target , & new ) && hashmap_oom (target );
227225 }
228226 if (oom ) {
@@ -231,6 +229,13 @@ void basic_set_string_variable(const char* var, const char* value, struct basic_
231229 }
232230}
233231
232+ static void update_int (struct basic_ctx * ctx , ub_var_int * integer , size_t len , bool propagate_global , const char * varname , int64_t value ) {
233+ integer -> name_length = len ;
234+ integer -> global = propagate_global ;
235+ integer -> varname = buddy_strdup (ctx -> allocator , varname );
236+ integer -> value = value ;
237+ }
238+
234239void basic_set_int_variable (const char * var , int64_t value , struct basic_ctx * ctx , bool local , bool propagate_global ) {
235240 struct hashmap * locals = ctx -> local_int_variables [ctx -> call_stack_ptr ];
236241 struct hashmap * globals = ctx -> int_variables ;
@@ -246,29 +251,19 @@ void basic_set_int_variable(const char* var, int64_t value, struct basic_ctx* ct
246251 if (local && locals && (found = hashmap_get (locals , & (ub_var_int ) { .varname = var }))) {
247252 basic_debug ("local set '%s' %p\n" , var , ctx -> allocator );
248253 buddy_free (ctx -> allocator , found -> varname );
249- found -> name_length = len ;
250- found -> global = propagate_global ;
251- found -> varname = buddy_strdup (ctx -> allocator , var );
252- found -> value = value ;
254+ update_int (ctx , found , len , propagate_global , var , value );
253255 oom = !hashmap_set (locals , found ) && hashmap_oom (locals );
254256 } else if ((found = hashmap_get (globals , & (ub_var_int ) { .varname = var }))) {
255257 basic_debug ("global set '%s' %p %p\n" , var , found -> varname , ctx -> allocator );
256258 buddy_free (ctx -> allocator , found -> varname );
257- found -> name_length = len ;
258- found -> global = propagate_global ;
259- found -> varname = buddy_strdup (ctx -> allocator , var );
260- basic_debug ("strdup'd\n" );
261- found -> value = value ;
259+ update_int (ctx , found , len , propagate_global , var , value );
262260 oom = !hashmap_set (globals , found ) && hashmap_oom (globals );
263261 basic_debug ("set, oom=%d\n" , oom );
264262 } else {
265263 struct hashmap * target = local && locals ? locals : globals ;
266264 basic_debug ("%s create '%s' %p\n" , target == globals ? "global" : "local" , var , ctx -> allocator );
267265 ub_var_int new ;
268- new .name_length = len ;
269- new .global = propagate_global ;
270- new .varname = buddy_strdup (ctx -> allocator , var );
271- new .value = value ;
266+ update_int (ctx , & new , len , propagate_global , var , value );
272267 oom = !hashmap_set (target , & new ) && hashmap_oom (target );
273268 }
274269 if (oom ) {
@@ -277,6 +272,13 @@ void basic_set_int_variable(const char* var, int64_t value, struct basic_ctx* ct
277272 }
278273}
279274
275+ static void update_double (struct basic_ctx * ctx , ub_var_double * dbl , size_t len , bool propagate_global , const char * varname , double value ) {
276+ dbl -> name_length = len ;
277+ dbl -> global = propagate_global ;
278+ dbl -> varname = buddy_strdup (ctx -> allocator , varname );
279+ dbl -> value = value ;
280+ }
281+
280282void basic_set_double_variable (const char * var , double value , struct basic_ctx * ctx , bool local , bool propagate_global )
281283{
282284 struct hashmap * locals = ctx -> local_double_variables [ctx -> call_stack_ptr ];
@@ -292,25 +294,16 @@ void basic_set_double_variable(const char* var, double value, struct basic_ctx*
292294 bool oom = false;
293295 if (local && locals && (found = hashmap_get (locals , & (ub_var_double ) { .varname = var }))) {
294296 buddy_free (ctx -> allocator , found -> varname );
295- found -> name_length = len ;
296- found -> global = propagate_global ;
297- found -> varname = buddy_strdup (ctx -> allocator , var );
298- found -> value = value ;
297+ update_double (ctx , found , len , propagate_global , var , value );
299298 oom = !hashmap_set (locals , found ) && hashmap_oom (locals );
300299 } else if ((found = hashmap_get (globals , & (ub_var_double ) { .varname = var }))) {
301300 buddy_free (ctx -> allocator , found -> varname );
302- found -> name_length = len ;
303- found -> global = propagate_global ;
304- found -> varname = buddy_strdup (ctx -> allocator , var );
305- found -> value = value ;
301+ update_double (ctx , found , len , propagate_global , var , value );
306302 oom = !hashmap_set (globals , found ) && hashmap_oom (globals );
307303 } else {
308304 struct hashmap * target = local && locals ? locals : globals ;
309305 ub_var_double new ;
310- new .name_length = len ;
311- new .global = propagate_global ;
312- new .varname = buddy_strdup (ctx -> allocator , var );
313- new .value = value ;
306+ update_double (ctx , & new , len , propagate_global , var , value );
314307 oom = !hashmap_set (target , & new ) && hashmap_oom (target );
315308 }
316309 if (oom ) {
0 commit comments