ui/common/context.c

changeset 142
46448d38885c
parent 141
cc2170ea05ad
child 143
d499b29d7cb6
--- a/ui/common/context.c	Fri Nov 10 18:04:40 2017 +0100
+++ b/ui/common/context.c	Sat Nov 11 08:34:06 2017 +0100
@@ -150,13 +150,29 @@
     // otherweise add it to the bound map
     UiVar *cv = ucx_map_cstr_get(ctx->vars, name);
     if(cv) {
-        return cv; // I'm not sure if this can actually happen, lol
+        if(cv->type == type) {
+            return cv;
+        } else {
+            fprintf(stderr, "UiError: var '%s' already exists with different type\n", name);
+        }
+    }
+    
+    UiVar *var;
+    if(ctx->bound) {
+        var = ucx_map_cstr_get(ctx->bound, name);
+        if(var) {
+            if(var->type != type) {
+                fprintf(stderr, "UiError: var '%s' already bound with different type\n", name);
+            } else {
+                return var;
+            }
+        }
     }
     
     // create var and add it to the bound map
     // this map contains vars that are created by widgets
     // the vars can later be moved to subdocuments
-    UiVar *var = ui_malloc(ctx, sizeof(UiVar));
+    var = ui_malloc(ctx, sizeof(UiVar));
     var->type = type;
     var->value = uic_create_value(ctx, type);
     var->orig_val = NULL;
@@ -165,11 +181,7 @@
     if(!ctx->bound) {
         ctx->bound = ucx_map_new_a(ctx->mempool->allocator, 16);
     }
-    size_t oldcount = ctx->bound->count;
     ucx_map_cstr_put(ctx->bound, name, var);
-    if(ctx->bound->count != oldcount + 1) {
-        fprintf(stderr, "UiError: var '%s' already bound\n", name);
-    }
     
     // if a subdocument has a variable with this name, we must copy
     // the binding to the doc var
@@ -234,9 +246,7 @@
             UiInteger *f = from->value;
             UiInteger *t = to->value;
             uic_int_copy(f, t);
-            if(t->value != 0) {
-                t->set(t, t->value);
-            }
+            t->set(t, t->value);
             break;
         }
         case UI_VAR_STRING: {

mercurial