Commit d576a7962b2ed3d752ee5e690c6995b8c72a2358

Thomas de Grivel 2024-06-27T09:47:56

s_struct and s_struct_type support

diff --git a/libc3/env.c b/libc3/env.c
index c14a0b0..ecc075b 100644
--- a/libc3/env.c
+++ b/libc3/env.c
@@ -2191,8 +2191,8 @@ s_tag * env_module_load_time (s_env *env, const s_sym *module,
     facts_with_cursor_clean(&cursor);
     return NULL;
   }
-  facts_with_cursor_clean(&cursor);
   *dest = tag_time_var;
+  facts_with_cursor_clean(&cursor);
   return dest;
 }
 
@@ -2202,7 +2202,8 @@ bool env_module_maybe_reload (s_env *env, const s_sym *module)
   bool r = false;
   s_tag tag_load_time = {0};
   s_tag tag_mtime;
-  env_module_load_time(env, module, &tag_load_time);
+  if (! env_module_load_time(env, module, &tag_load_time))
+    return env_module_load(env, module);
   if (module_path(module, &env->module_path, C3_EXT, &path)) {
     if (file_access(&path, &g_sym_r))
       r = true;
@@ -2215,15 +2216,19 @@ bool env_module_maybe_reload (s_env *env, const s_sym *module)
     else
       str_clean(&path);
   }
-  if (! r)
+  if (! r) {
+    err_write_1("env_module_maybe_reload: module not found: ");
+    err_inspect_sym(&module);
+    err_write_1("\n");
     return false;
+  }
   if (! file_mtime(&path, &tag_mtime)) {
     str_clean(&path);
     return false;
   }
+  str_clean(&path);
   if (compare_tag(&tag_load_time, &tag_mtime) < 0)
     r = env_module_load(env, module);
-  str_clean(&path);
   tag_clean(&tag_mtime);
   return r;
 }
@@ -2555,15 +2560,20 @@ bool * env_struct_type_exists (s_env *env, const s_sym *module,
   s_tag tag_var;
   assert(env);
   assert(module);
+  assert(dest);
   tag_init_sym(&tag_module, module);
   tag_init_sym(&tag_defstruct, &g_sym_defstruct);
   tag_init_var(&tag_var, &g_sym_Tag);
-  env_module_maybe_reload(env, module);
+  if (! env_module_maybe_reload(env, module))
+    return NULL;
   if (! facts_with_tags(&env->facts, &cursor, &tag_module,
                         &tag_defstruct, &tag_var))
     return NULL;
   if (! facts_cursor_next(&cursor, &fact))
     return NULL;
+  if (! fact) {
+    facts_save_file(&env->facts, "env_struct_type_exists.facts");
+  }
   *dest = fact ? true : false;
   facts_cursor_clean(&cursor);
   return dest;
@@ -2584,9 +2594,12 @@ const s_struct_type * env_struct_type_find (s_env *env,
   tag_init_sym(&tag_module, module);
   tag_init_sym(&tag_struct_type, &g_sym_struct_type);
   tag_init_var(&tag_var, &g_sym_Tag);
-  env_module_maybe_reload(env, module);
-  facts_with(&env->facts, &cursor, (t_facts_spec) {
-      &tag_module, &tag_struct_type, &tag_var, NULL, NULL });
+  if (! env_module_maybe_reload(env, module))
+    return NULL;
+  if (! facts_with(&env->facts, &cursor, (t_facts_spec) {
+                   &tag_module, &tag_struct_type, &tag_var,
+                   NULL, NULL }))
+    return NULL;
   if (! facts_with_cursor_next(&cursor, &found))
     return NULL;
   if (! found) {
diff --git a/libc3/struct.c b/libc3/struct.c
index 6912c8e..62dfb9b 100644
--- a/libc3/struct.c
+++ b/libc3/struct.c
@@ -174,10 +174,10 @@ s_struct * struct_init_copy (s_struct *s, const s_struct *src)
   assert(src->type);
   tmp.type = src->type;
   if (src->data) {
-    tmp.free_data = true;
     tmp.data = alloc(tmp.type->size);
     if (! tmp.data)
       return NULL;
+    tmp.free_data = true;
     i = 0;
     while (i < tmp.type->map.count) {
       if (! tag_type(tmp.type->map.value + i, &sym) ||
diff --git a/libc3/struct_type.c b/libc3/struct_type.c
index 4e6f180..6a65922 100644
--- a/libc3/struct_type.c
+++ b/libc3/struct_type.c
@@ -53,7 +53,7 @@ s_struct_type * struct_type_init (s_struct_type *st,
 {
   uw count;
   uw i;
-  bool must_clean;
+  bool must_clean = false;
   uw offset;
   const s_list *s;
   uw size;
diff --git a/libc3/sym.c b/libc3/sym.c
index 8218b81..025b01d 100644
--- a/libc3/sym.c
+++ b/libc3/sym.c
@@ -1149,7 +1149,7 @@ bool sym_type_size (const s_sym *type, uw *dest)
     return true;
   }
   if (type == &g_sym_Var) {
-    *dest = 0;
+    *dest = sizeof(s_tag);
     return true;
   }
   if (type == &g_sym_Void) {
diff --git a/libc3/tag.c b/libc3/tag.c
index 5423c6b..3c5d5c0 100644
--- a/libc3/tag.c
+++ b/libc3/tag.c
@@ -1280,7 +1280,7 @@ const s_sym ** tag_type (const s_tag *tag, const s_sym **dest)
   case TAG_RATIO:       *dest = &g_sym_Ratio;      return dest;
   case TAG_STR:         *dest = &g_sym_Str;        return dest;
   case TAG_STRUCT:      *dest = tag->data.struct_.type->module;
-                                                     return dest;
+                                                   return dest;
   case TAG_STRUCT_TYPE: *dest = &g_sym_StructType; return dest;
   case TAG_SYM:         *dest = &g_sym_Sym;        return dest;
   case TAG_TUPLE:       *dest = &g_sym_Tuple;      return dest;