ambrop7 15 лет назад
Родитель
Сommit
cf1e99fc57
1 измененных файлов с 81 добавлено и 1 удалено
  1. 81 1
      ncd/modules/list.c

+ 81 - 1
ncd/modules/list.c

@@ -26,6 +26,8 @@
  * Synopsis: list(elem1, ..., elemN)
  * Variables:
  *   (empty) - list containing elem1, ..., elemN
+ * 
+ * Synopsis: list::append(arg)
  */
 
 #include <stdlib.h>
@@ -39,6 +41,11 @@
 
 struct instance {
     NCDModuleInst *i;
+    NCDValue list;
+};
+
+struct append_instance {
+    NCDModuleInst *i;
 };
 
 static void func_new (NCDModuleInst *i)
@@ -54,11 +61,19 @@ static void func_new (NCDModuleInst *i)
     // init arguments
     o->i = i;
     
+    // copy list
+    if (!NCDValue_InitCopy(&o->list, o->i->args)) {
+        ModuleLog(i, BLOG_ERROR, "NCDValue_InitCopy failed");
+        goto fail1;
+    }
+    
     // signal up
     NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
     
     return;
     
+fail1:
+    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
@@ -69,6 +84,9 @@ static void func_die (void *vo)
     struct instance *o = vo;
     NCDModuleInst *i = o->i;
     
+    // free list
+    NCDValue_Free(&o->list);
+    
     // free instance
     free(o);
     
@@ -80,7 +98,7 @@ static int func_getvar (void *vo, const char *name, NCDValue *out)
     struct instance *o = vo;
     
     if (!strcmp(name, "")) {
-        if (!NCDValue_InitCopy(out, o->i->args)) {
+        if (!NCDValue_InitCopy(out, &o->list)) {
             ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
             return 0;
         }
@@ -91,12 +109,74 @@ static int func_getvar (void *vo, const char *name, NCDValue *out)
     return 0;
 }
 
+static void append_func_new (NCDModuleInst *i)
+{
+    // allocate instance
+    struct append_instance *o = malloc(sizeof(*o));
+    if (!o) {
+        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
+        goto fail0;
+    }
+    NCDModuleInst_Backend_SetUser(i, o);
+    
+    // init arguments
+    o->i = i;
+    
+    // check arguments
+    NCDValue *arg;
+    if (!NCDValue_ListRead(o->i->args, 1, &arg)) {
+        ModuleLog(o->i, BLOG_ERROR, "wrong arity");
+        goto fail1;
+    }
+    
+    // get method object
+    struct instance *mo = i->method_object->inst_user;
+    
+    // append
+    NCDValue v;
+    if (!NCDValue_InitCopy(&v, arg)) {
+        ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
+        goto fail1;
+    }
+    if (!NCDValue_ListAppend(&mo->list, v)) {
+        NCDValue_Free(&v);
+        ModuleLog(o->i, BLOG_ERROR, "NCDValue_ListAppend failed");
+        goto fail1;
+    }
+    
+    // signal up
+    NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
+    
+    return;
+    
+fail1:
+    free(o);
+fail0:
+    NCDModuleInst_Backend_SetError(i);
+    NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
+}
+
+static void append_func_die (void *vo)
+{
+    struct append_instance *o = vo;
+    NCDModuleInst *i = o->i;
+    
+    // free instance
+    free(o);
+    
+    NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
+}
+
 static const struct NCDModule modules[] = {
     {
         .type = "list",
         .func_new = func_new,
         .func_die = func_die,
         .func_getvar = func_getvar
+    }, {
+        .type = "list::append",
+        .func_new = append_func_new,
+        .func_die = append_func_die
     }, {
         .type = NULL
     }