foreach.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /**
  2. * @file foreach.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Synopsis:
  32. * foreach(list/map collection, string template, list args)
  33. *
  34. * Description:
  35. * Initializes a template process for each element of list, sequentially,
  36. * obeying to the usual execution model of NCD.
  37. * It's equivalent to (except for special variables):
  38. *
  39. * call(template, args);
  40. * ...
  41. * call(template, args); # one call() for every element of list
  42. *
  43. * Template process specials:
  44. *
  45. * _index - (lists only) index of the list element corresponding to the template,
  46. * process, as a decimal string, starting from zero
  47. * _elem - (lists only) element of list corresponding to the template process
  48. * _key - (maps only) key of the current map entry
  49. * _val - (maps only) value of the current map entry
  50. * _caller.X - X as seen from the foreach() statement
  51. *
  52. * Synopsis:
  53. * foreach_emb(list/map collection, string template, string name1 [, string name2])
  54. *
  55. * Description:
  56. * Foreach for embedded templates; the desugaring process converts Foreach
  57. * clauses into this statement. The called templates have direct access to
  58. * objects as seen from this statement, and also some kind of access to the
  59. * current element of the iteration, depending on the type of collection
  60. * being iterated, and whether 'name2' is provided:
  61. * List and one name: current element is named 'name1'.
  62. * List and both names: current index is named 'name1', current element 'name2'.
  63. * Map and one name: current key is named 'name1'.
  64. * Map and both names: current key is named 'name1', current value 'name2'.
  65. */
  66. #include <stdlib.h>
  67. #include <string.h>
  68. #include <limits.h>
  69. #include <misc/balloc.h>
  70. #include <misc/string_begins_with.h>
  71. #include <misc/debug.h>
  72. #include <misc/offset.h>
  73. #include <system/BReactor.h>
  74. #include <ncd/module_common.h>
  75. #include <generated/blog_channel_ncd_foreach.h>
  76. #define ISTATE_WORKING 1
  77. #define ISTATE_UP 2
  78. #define ISTATE_WAITING 3
  79. #define ISTATE_TERMINATING 4
  80. #define ESTATE_FORGOTTEN 1
  81. #define ESTATE_DOWN 2
  82. #define ESTATE_UP 3
  83. #define ESTATE_WAITING 4
  84. #define ESTATE_TERMINATING 5
  85. struct element;
  86. struct instance {
  87. NCDModuleInst *i;
  88. NCDValRef template_name;
  89. NCDValRef args;
  90. NCD_string_id_t name1;
  91. NCD_string_id_t name2;
  92. BTimer timer;
  93. struct element *elems;
  94. int type;
  95. int num_elems;
  96. int gp; // good pointer
  97. int ip; // initialized pointer
  98. int state;
  99. };
  100. struct element {
  101. struct instance *inst;
  102. union {
  103. struct {
  104. NCDValRef list_elem;
  105. };
  106. struct {
  107. NCDValRef map_key;
  108. NCDValRef map_val;
  109. };
  110. };
  111. NCDModuleProcess process;
  112. int i;
  113. int state;
  114. };
  115. static void assert_state (struct instance *o);
  116. static void work (struct instance *o);
  117. static void advance (struct instance *o);
  118. static void timer_handler (struct instance *o);
  119. static void element_process_handler_event (NCDModuleProcess *process, int event);
  120. static int element_process_func_getspecialobj (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object);
  121. static int element_caller_object_func_getobj (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object);
  122. static int element_list_index_object_func_getvar (const NCDObject *obj, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out);
  123. static int element_list_elem_object_func_getvar (const NCDObject *obj, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out);
  124. static int element_map_key_object_func_getvar (const NCDObject *obj, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out);
  125. static int element_map_val_object_func_getvar (const NCDObject *obj, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out);
  126. static void instance_free (struct instance *o);
  127. enum {STRING_INDEX, STRING_ELEM, STRING_KEY, STRING_VAL};
  128. static const char *strings[] = {
  129. "_index", "_elem", "_key", "_val", NULL
  130. };
  131. static void assert_state (struct instance *o)
  132. {
  133. ASSERT(o->num_elems >= 0)
  134. ASSERT(o->gp >= 0)
  135. ASSERT(o->ip >= 0)
  136. ASSERT(o->gp <= o->num_elems)
  137. ASSERT(o->ip <= o->num_elems)
  138. ASSERT(o->gp <= o->ip)
  139. #ifndef NDEBUG
  140. // check GP
  141. for (int i = 0; i < o->gp; i++) {
  142. if (i == o->gp - 1) {
  143. ASSERT(o->elems[i].state == ESTATE_UP || o->elems[i].state == ESTATE_DOWN ||
  144. o->elems[i].state == ESTATE_WAITING)
  145. } else {
  146. ASSERT(o->elems[i].state == ESTATE_UP)
  147. }
  148. }
  149. // check IP
  150. int ip = o->num_elems;
  151. while (ip > 0 && o->elems[ip - 1].state == ESTATE_FORGOTTEN) {
  152. ip--;
  153. }
  154. ASSERT(o->ip == ip)
  155. // check gap
  156. for (int i = o->gp; i < o->ip; i++) {
  157. if (i == o->ip - 1) {
  158. ASSERT(o->elems[i].state == ESTATE_UP || o->elems[i].state == ESTATE_DOWN ||
  159. o->elems[i].state == ESTATE_WAITING || o->elems[i].state == ESTATE_TERMINATING)
  160. } else {
  161. ASSERT(o->elems[i].state == ESTATE_UP || o->elems[i].state == ESTATE_DOWN ||
  162. o->elems[i].state == ESTATE_WAITING)
  163. }
  164. }
  165. #endif
  166. }
  167. static void work (struct instance *o)
  168. {
  169. assert_state(o);
  170. // stop timer
  171. BReactor_RemoveTimer(o->i->params->iparams->reactor, &o->timer);
  172. if (o->state == ISTATE_WAITING) {
  173. return;
  174. }
  175. if (o->state == ISTATE_UP && !(o->gp == o->ip && o->gp == o->num_elems && (o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP))) {
  176. // signal down
  177. NCDModuleInst_Backend_Down(o->i);
  178. // set state waiting
  179. o->state = ISTATE_WAITING;
  180. return;
  181. }
  182. if (o->gp < o->ip) {
  183. // get last element
  184. struct element *le = &o->elems[o->ip - 1];
  185. ASSERT(le->state != ESTATE_FORGOTTEN)
  186. // start terminating if not already
  187. if (le->state != ESTATE_TERMINATING) {
  188. // request termination
  189. NCDModuleProcess_Terminate(&le->process);
  190. // set element state terminating
  191. le->state = ESTATE_TERMINATING;
  192. }
  193. return;
  194. }
  195. if (o->state == ISTATE_TERMINATING) {
  196. // finally die
  197. instance_free(o);
  198. return;
  199. }
  200. if (o->gp == o->num_elems && (o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)) {
  201. if (o->state == ISTATE_WORKING) {
  202. // signal up
  203. NCDModuleInst_Backend_Up(o->i);
  204. // set state up
  205. o->state = ISTATE_UP;
  206. }
  207. return;
  208. }
  209. if (o->gp > 0 && o->elems[o->gp - 1].state == ESTATE_WAITING) {
  210. // get last element
  211. struct element *le = &o->elems[o->gp - 1];
  212. // continue process
  213. NCDModuleProcess_Continue(&le->process);
  214. // set state down
  215. le->state = ESTATE_DOWN;
  216. return;
  217. }
  218. if (o->gp > 0 && o->elems[o->gp - 1].state == ESTATE_DOWN) {
  219. return;
  220. }
  221. ASSERT(o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)
  222. advance(o);
  223. return;
  224. }
  225. static void advance (struct instance *o)
  226. {
  227. assert_state(o);
  228. ASSERT(o->gp == o->ip)
  229. ASSERT(o->gp < o->num_elems)
  230. ASSERT(o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)
  231. ASSERT(o->elems[o->gp].state == ESTATE_FORGOTTEN)
  232. // get next element
  233. struct element *e = &o->elems[o->gp];
  234. // init process
  235. if (!NCDModuleProcess_InitValue(&e->process, o->i, o->template_name, o->args, element_process_handler_event)) {
  236. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  237. goto fail;
  238. }
  239. // set special functions
  240. NCDModuleProcess_SetSpecialFuncs(&e->process, element_process_func_getspecialobj);
  241. // set element state down
  242. e->state = ESTATE_DOWN;
  243. // increment GP and IP
  244. o->gp++;
  245. o->ip++;
  246. return;
  247. fail:
  248. // set timer
  249. BReactor_SetTimer(o->i->params->iparams->reactor, &o->timer);
  250. }
  251. static void timer_handler (struct instance *o)
  252. {
  253. assert_state(o);
  254. ASSERT(o->gp == o->ip)
  255. ASSERT(o->gp < o->num_elems)
  256. ASSERT(o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)
  257. ASSERT(o->elems[o->gp].state == ESTATE_FORGOTTEN)
  258. advance(o);
  259. return;
  260. }
  261. static void element_process_handler_event (NCDModuleProcess *process, int event)
  262. {
  263. struct element *e = UPPER_OBJECT(process, struct element, process);
  264. struct instance *o = e->inst;
  265. assert_state(o);
  266. ASSERT(e->i < o->ip)
  267. ASSERT(e->state != ESTATE_FORGOTTEN)
  268. switch (event) {
  269. case NCDMODULEPROCESS_EVENT_UP: {
  270. ASSERT(e->state == ESTATE_DOWN)
  271. ASSERT(o->gp == o->ip)
  272. ASSERT(o->gp == e->i + 1)
  273. // set element state up
  274. e->state = ESTATE_UP;
  275. } break;
  276. case NCDMODULEPROCESS_EVENT_DOWN: {
  277. ASSERT(e->state == ESTATE_UP)
  278. // set element state waiting
  279. e->state = ESTATE_WAITING;
  280. // bump down GP
  281. if (o->gp > e->i + 1) {
  282. o->gp = e->i + 1;
  283. }
  284. } break;
  285. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  286. ASSERT(e->state == ESTATE_TERMINATING)
  287. ASSERT(o->gp < o->ip)
  288. ASSERT(o->ip == e->i + 1)
  289. // free process
  290. NCDModuleProcess_Free(&e->process);
  291. // set element state forgotten
  292. e->state = ESTATE_FORGOTTEN;
  293. // decrement IP
  294. o->ip--;
  295. } break;
  296. default: ASSERT(0);
  297. }
  298. work(o);
  299. return;
  300. }
  301. static int element_process_func_getspecialobj (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object)
  302. {
  303. struct element *e = UPPER_OBJECT(process, struct element, process);
  304. struct instance *o = e->inst;
  305. ASSERT(e->state != ESTATE_FORGOTTEN)
  306. switch (o->type) {
  307. case NCDVAL_LIST: {
  308. NCD_string_id_t index_name = (o->name2 >= 0 ? o->name1 : -1);
  309. NCD_string_id_t elem_name = (o->name2 >= 0 ? o->name2 : o->name1);
  310. if (index_name >= 0 && name == index_name) {
  311. *out_object = NCDObject_Build(-1, e, element_list_index_object_func_getvar, NCDObject_no_getobj);
  312. return 1;
  313. }
  314. if (name == elem_name) {
  315. *out_object = NCDObject_Build(-1, e, element_list_elem_object_func_getvar, NCDObject_no_getobj);
  316. return 1;
  317. }
  318. } break;
  319. case NCDVAL_MAP: {
  320. NCD_string_id_t key_name = o->name1;
  321. NCD_string_id_t val_name = o->name2;
  322. if (name == key_name) {
  323. *out_object = NCDObject_Build(-1, e, element_map_key_object_func_getvar, NCDObject_no_getobj);
  324. return 1;
  325. }
  326. if (val_name >= 0 && name == val_name) {
  327. *out_object = NCDObject_Build(-1, e, element_map_val_object_func_getvar, NCDObject_no_getobj);
  328. return 1;
  329. }
  330. } break;
  331. }
  332. if (NCDVal_IsInvalid(o->args)) {
  333. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  334. }
  335. if (name == NCD_STRING_CALLER) {
  336. *out_object = NCDObject_Build(-1, e, NCDObject_no_getvar, element_caller_object_func_getobj);
  337. return 1;
  338. }
  339. return 0;
  340. }
  341. static int element_caller_object_func_getobj (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object)
  342. {
  343. struct element *e = NCDObject_DataPtr(obj);
  344. struct instance *o = e->inst;
  345. ASSERT(e->state != ESTATE_FORGOTTEN)
  346. ASSERT(!NCDVal_IsInvalid(o->args))
  347. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  348. }
  349. static int element_list_index_object_func_getvar (const NCDObject *obj, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  350. {
  351. struct element *e = NCDObject_DataPtr(obj);
  352. struct instance *o = e->inst;
  353. B_USE(o)
  354. ASSERT(e->state != ESTATE_FORGOTTEN)
  355. ASSERT(o->type == NCDVAL_LIST)
  356. if (name != NCD_STRING_EMPTY) {
  357. return 0;
  358. }
  359. *out = ncd_make_uintmax(mem, e->i);
  360. return 1;
  361. }
  362. static int element_list_elem_object_func_getvar (const NCDObject *obj, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  363. {
  364. struct element *e = NCDObject_DataPtr(obj);
  365. struct instance *o = e->inst;
  366. B_USE(o)
  367. ASSERT(e->state != ESTATE_FORGOTTEN)
  368. ASSERT(o->type == NCDVAL_LIST)
  369. if (name != NCD_STRING_EMPTY) {
  370. return 0;
  371. }
  372. *out = NCDVal_NewCopy(mem, e->list_elem);
  373. return 1;
  374. }
  375. static int element_map_key_object_func_getvar (const NCDObject *obj, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  376. {
  377. struct element *e = NCDObject_DataPtr(obj);
  378. struct instance *o = e->inst;
  379. B_USE(o)
  380. ASSERT(e->state != ESTATE_FORGOTTEN)
  381. ASSERT(o->type == NCDVAL_MAP)
  382. if (name != NCD_STRING_EMPTY) {
  383. return 0;
  384. }
  385. *out = NCDVal_NewCopy(mem, e->map_key);
  386. return 1;
  387. }
  388. static int element_map_val_object_func_getvar (const NCDObject *obj, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  389. {
  390. struct element *e = NCDObject_DataPtr(obj);
  391. struct instance *o = e->inst;
  392. B_USE(o)
  393. ASSERT(e->state != ESTATE_FORGOTTEN)
  394. ASSERT(o->type == NCDVAL_MAP)
  395. if (name != NCD_STRING_EMPTY) {
  396. return 0;
  397. }
  398. *out = NCDVal_NewCopy(mem, e->map_val);
  399. return 1;
  400. }
  401. static void func_new_common (void *vo, NCDModuleInst *i, NCDValRef collection, NCDValRef template_name, NCDValRef args, NCD_string_id_t name1, NCD_string_id_t name2)
  402. {
  403. ASSERT(!NCDVal_IsInvalid(collection))
  404. ASSERT(NCDVal_IsString(template_name))
  405. ASSERT(NCDVal_IsInvalid(args) || NCDVal_IsList(args))
  406. ASSERT(name1 >= 0)
  407. struct instance *o = vo;
  408. o->i = i;
  409. o->type = NCDVal_Type(collection);
  410. o->template_name = template_name;
  411. o->args = args;
  412. o->name1 = name1;
  413. o->name2 = name2;
  414. // init timer
  415. btime_t retry_time = NCDModuleInst_Backend_InterpGetRetryTime(i);
  416. BTimer_Init(&o->timer, retry_time, (BTimer_handler)timer_handler, o);
  417. size_t num_elems;
  418. NCDValMapElem cur_map_elem;
  419. switch (o->type) {
  420. case NCDVAL_LIST: {
  421. num_elems = NCDVal_ListCount(collection);
  422. } break;
  423. case NCDVAL_MAP: {
  424. num_elems = NCDVal_MapCount(collection);
  425. cur_map_elem = NCDVal_MapOrderedFirst(collection);
  426. } break;
  427. default:
  428. ModuleLog(i, BLOG_ERROR, "invalid collection type");
  429. goto fail0;
  430. }
  431. if (num_elems > INT_MAX) {
  432. ModuleLog(i, BLOG_ERROR, "too many elements");
  433. goto fail0;
  434. }
  435. o->num_elems = num_elems;
  436. // allocate elements
  437. if (!(o->elems = BAllocArray(o->num_elems, sizeof(o->elems[0])))) {
  438. ModuleLog(i, BLOG_ERROR, "BAllocArray failed");
  439. goto fail0;
  440. }
  441. for (int j = 0; j < o->num_elems; j++) {
  442. struct element *e = &o->elems[j];
  443. // set instance
  444. e->inst = o;
  445. // set index
  446. e->i = j;
  447. // set state forgotten
  448. e->state = ESTATE_FORGOTTEN;
  449. // set values
  450. switch (o->type) {
  451. case NCDVAL_LIST: {
  452. e->list_elem = NCDVal_ListGet(collection, j);
  453. } break;
  454. case NCDVAL_MAP: {
  455. e->map_key = NCDVal_MapElemKey(collection, cur_map_elem);
  456. e->map_val = NCDVal_MapElemVal(collection, cur_map_elem);
  457. cur_map_elem = NCDVal_MapOrderedNext(collection, cur_map_elem);
  458. } break;
  459. }
  460. }
  461. // set GP and IP zero
  462. o->gp = 0;
  463. o->ip = 0;
  464. // set state working
  465. o->state = ISTATE_WORKING;
  466. work(o);
  467. return;
  468. fail0:
  469. NCDModuleInst_Backend_DeadError(i);
  470. }
  471. static void func_new_foreach (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  472. {
  473. // read arguments
  474. NCDValRef arg_collection;
  475. NCDValRef arg_template;
  476. NCDValRef arg_args;
  477. if (!NCDVal_ListRead(params->args, 3, &arg_collection, &arg_template, &arg_args)) {
  478. ModuleLog(i, BLOG_ERROR, "wrong arity");
  479. goto fail0;
  480. }
  481. if (!NCDVal_IsString(arg_template) || !NCDVal_IsList(arg_args)) {
  482. ModuleLog(i, BLOG_ERROR, "wrong type");
  483. goto fail0;
  484. }
  485. NCD_string_id_t name1;
  486. NCD_string_id_t name2;
  487. switch (NCDVal_Type(arg_collection)) {
  488. case NCDVAL_LIST: {
  489. name1 = ModuleString(i, STRING_INDEX);
  490. name2 = ModuleString(i, STRING_ELEM);
  491. } break;
  492. case NCDVAL_MAP: {
  493. name1 = ModuleString(i, STRING_KEY);
  494. name2 = ModuleString(i, STRING_VAL);
  495. } break;
  496. default:
  497. ModuleLog(i, BLOG_ERROR, "invalid collection type");
  498. goto fail0;
  499. }
  500. func_new_common(vo, i, arg_collection, arg_template, arg_args, name1, name2);
  501. return;
  502. fail0:
  503. NCDModuleInst_Backend_DeadError(i);
  504. }
  505. static void func_new_foreach_emb (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  506. {
  507. // read arguments
  508. NCDValRef arg_collection;
  509. NCDValRef arg_template;
  510. NCDValRef arg_name1;
  511. NCDValRef arg_name2 = NCDVal_NewInvalid();
  512. if (!NCDVal_ListRead(params->args, 3, &arg_collection, &arg_template, &arg_name1) && !NCDVal_ListRead(params->args, 4, &arg_collection, &arg_template, &arg_name1, &arg_name2)) {
  513. ModuleLog(i, BLOG_ERROR, "wrong arity");
  514. goto fail0;
  515. }
  516. if (!NCDVal_IsString(arg_template) || !NCDVal_IsString(arg_name1) || (!NCDVal_IsInvalid(arg_name2) && !NCDVal_IsString(arg_name2))) {
  517. ModuleLog(i, BLOG_ERROR, "wrong type");
  518. goto fail0;
  519. }
  520. NCD_string_id_t name1 = ncd_get_string_id(arg_name1, i->params->iparams->string_index);
  521. if (name1 < 0) {
  522. ModuleLog(i, BLOG_ERROR, "ncd_get_string_id failed");
  523. goto fail0;
  524. }
  525. NCD_string_id_t name2 = -1;
  526. if (!NCDVal_IsInvalid(arg_name2)) {
  527. name2 = ncd_get_string_id(arg_name2, i->params->iparams->string_index);
  528. if (name2 < 0) {
  529. ModuleLog(i, BLOG_ERROR, "ncd_get_string_id failed");
  530. goto fail0;
  531. }
  532. }
  533. func_new_common(vo, i, arg_collection, arg_template, NCDVal_NewInvalid(), name1, name2);
  534. return;
  535. fail0:
  536. NCDModuleInst_Backend_DeadError(i);
  537. }
  538. static void instance_free (struct instance *o)
  539. {
  540. ASSERT(o->gp == 0)
  541. ASSERT(o->ip == 0)
  542. // free elements
  543. BFree(o->elems);
  544. // free timer
  545. BReactor_RemoveTimer(o->i->params->iparams->reactor, &o->timer);
  546. NCDModuleInst_Backend_Dead(o->i);
  547. }
  548. static void func_die (void *vo)
  549. {
  550. struct instance *o = vo;
  551. assert_state(o);
  552. ASSERT(o->state != ISTATE_TERMINATING)
  553. // set GP zero
  554. o->gp = 0;
  555. // set state terminating
  556. o->state = ISTATE_TERMINATING;
  557. work(o);
  558. return;
  559. }
  560. static void func_clean (void *vo)
  561. {
  562. struct instance *o = vo;
  563. if (o->state != ISTATE_WAITING) {
  564. return;
  565. }
  566. // set state working
  567. o->state = ISTATE_WORKING;
  568. work(o);
  569. return;
  570. }
  571. static struct NCDModule modules[] = {
  572. {
  573. .type = "foreach",
  574. .func_new2 = func_new_foreach,
  575. .func_die = func_die,
  576. .func_clean = func_clean,
  577. .alloc_size = sizeof(struct instance)
  578. }, {
  579. .type = "foreach_emb",
  580. .func_new2 = func_new_foreach_emb,
  581. .func_die = func_die,
  582. .func_clean = func_clean,
  583. .alloc_size = sizeof(struct instance)
  584. }, {
  585. .type = NULL
  586. }
  587. };
  588. const struct NCDModuleGroup ncdmodule_foreach = {
  589. .modules = modules,
  590. .strings = strings
  591. };