TAILQ_REMOVE(&list->mojo_list_head, e, mojo_lists);
/* Also free the associated object : */
- free(e->obj);
+ free_object(e->obj);
free(e);
}
if (list == NULL)
return -1;
- struct mojo_list_elem *e;
+ struct mojo_list_elem *e = (struct mojo_list_elem *) TAILQ_FIRST(&list->mojo_list_head);
int i = 0;
+
+
TAILQ_FOREACH(e, &list->mojo_list_head, mojo_lists) {
i++;
}
return i;
}
+
+void mojo_list_free(struct mojo_list *list) {
+ if (list == NULL)
+ return;
+
+ struct mojo_list_elem *e = (struct mojo_list_elem *) TAILQ_FIRST(&list->mojo_list_head);
+
+ TAILQ_FOREACH(e, &list->mojo_list_head, mojo_lists) {
+ TAILQ_REMOVE(&list->mojo_list_head, e, mojo_lists);
+ free_object(e->obj);
+ free(e);
+ }
+}
+
/*
Here begins the public facing list api.
list_object->super = nil_object;
list_object->type = T_LIST;
- list_object->name = "List";
+ list_object->name = strdup("List");
list_object->value.l_value = mojo_list_init();
/* Create a second list object that contains the methods of the list object */
l1->super = nil_object;
l1->type = T_LIST;
- l1->name = "List";
+ l1->name = strdup("List");
l1->value.l_value = mojo_list_init();
l1->methods = nil_object; /* the methods of l1 are in l2, which is an empty list. This way, we don't fall into
an infinite loop in clone_object()
fail("Unable to create a method %d %d", __FILE__, __LINE__);
met->type = T_BUILTIN;
- met->name = "+";
+ met->name = strdup("+");
met->value.c_method = BUILTIN_CAST list_append;
list_append(l1, met);
fail("Unable to create a method %d %d", __FILE__, __LINE__);
met->type = T_BUILTIN;
- met->name = "remove";
+ met->name = strdup("remove");
met->value.c_method = BUILTIN_CAST list_remove;
list_append(l1, met);
fail("Unable to create a method %d %d", __FILE__, __LINE__);
met->type = T_BUILTIN;
- met->name = "nth:";
+ met->name = strdup("nth:");
met->value.c_method = BUILTIN_CAST list_nth;
list_append(l1, met);
fail("Unable to create a method %d %d", __FILE__, __LINE__);
met->type = T_BUILTIN;
- met->name = "last";
+ met->name = strdup("last");
met->value.c_method = BUILTIN_CAST list_last;
list_append(l1, met);
break;
case T_LIST:
- new->value.l_value = mojo_list_init();
+ new->value.l_value = (struct mojo_list *) mojo_list_init();
for (i = 0; i < mojo_list_length(o->value.l_value); i++) {
struct Object *t = clone_object((struct Object *) list_nth(o, i));
if (o == NULL)
return;
+ /*
+ * FIXME: use our own lib for strings and don't use
+ * BSS strings which can't be freed.
free(o->name);
+ */
+ if (o->methods != NULL)
+ free_object(o->methods);
+
+ if (o->symtab != NULL)
+ free_object(o->symtab);
+
+ if (o->type == T_LIST && o->value.l_value != NULL) {
+ mojo_list_free(o->value.l_value);
+ }
/* FIXME : free : the methods list, the methods and finally the object */
free(o);