txlyre 1 天之前
父节点
当前提交
a1f0df35b4
共有 1 个文件被更改,包括 25 次插入5 次删除
  1. 25 5
      qirt.c

+ 25 - 5
qirt.c

@@ -1484,20 +1484,40 @@ qi_value_t *_qi_set(qi_state_t *state, qi_bool is_pf, qi_bool is_constant,
 
         qi_mutex_unlock(scope->mutex);
 
-        return !is_pf && old ? old : value;
+        return !is_pf? old : value;
       }
 
       qi_mutex_unlock(scope->mutex);
     }
 
-  qi_symbol_t *symbol = qi_malloc(sizeof(qi_symbol_t));
-  symbol->value = value;
-  symbol->is_constant = is_constant;
-
+  qi_symbol_t *symbol = NULL;
   qi_scope_t *scope = qi_list_last(state->scopes);
 
   qi_mutex_lock(scope->mutex);
 
+  symbol = qi_table_get(scope->scope, name);
+
+  if (symbol) {
+    if (is_constant || symbol->is_constant) {
+      qi_mutex_unlock(scope->mutex);
+
+      qi_throw_format(state, "redeclaration of constant symbol: '%s'",
+                      name);
+    }
+
+    qi_value_t *old = symbol->value;
+
+    symbol->value = value;
+
+    qi_mutex_unlock(scope->mutex);
+
+    return !is_pf? old : value;
+  }
+
+  symbol = qi_malloc(sizeof(qi_symbol_t));
+  symbol->value = value;
+  symbol->is_constant = is_constant;
+
   qi_table_set(scope->scope, name, symbol);
 
   qi_mutex_unlock(scope->mutex);