foreach.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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 <system/BReactor.h>
  73. #include <ncd/NCDModule.h>
  74. #include <ncd/static_strings.h>
  75. #include <generated/blog_channel_ncd_foreach.h>
  76. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  77. #define ISTATE_WORKING 1
  78. #define ISTATE_UP 2
  79. #define ISTATE_WAITING 3
  80. #define ISTATE_TERMINATING 4
  81. #define ESTATE_FORGOTTEN 1
  82. #define ESTATE_DOWN 2
  83. #define ESTATE_UP 3
  84. #define ESTATE_WAITING 4
  85. #define ESTATE_TERMINATING 5
  86. struct element;
  87. struct instance {
  88. NCDModuleInst *i;
  89. const char *template_name;
  90. NCDValRef args;
  91. NCD_string_id_t name1;
  92. NCD_string_id_t name2;
  93. BTimer timer;
  94. struct element *elems;
  95. int type;
  96. int num_elems;
  97. int gp; // good pointer
  98. int ip; // initialized pointer
  99. int state;
  100. };
  101. struct element {
  102. struct instance *inst;
  103. union {
  104. struct {
  105. NCDValRef list_elem;
  106. };
  107. struct {
  108. NCDValRef map_key;
  109. NCDValRef map_val;
  110. };
  111. };
  112. NCDModuleProcess process;
  113. int i;
  114. int state;
  115. };
  116. static void assert_state (struct instance *o);
  117. static void work (struct instance *o);
  118. static void advance (struct instance *o);
  119. static void timer_handler (struct instance *o);
  120. static void element_process_handler_event (struct element *e, int event);
  121. static int element_process_func_getspecialobj (struct element *e, NCD_string_id_t name, NCDObject *out_object);
  122. static int element_caller_object_func_getobj (struct element *e, NCD_string_id_t name, NCDObject *out_object);
  123. static int element_list_index_object_func_getvar (struct element *e, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out);
  124. static int element_list_elem_object_func_getvar (struct element *e, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out);
  125. static int element_map_key_object_func_getvar (struct element *e, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out);
  126. static int element_map_val_object_func_getvar (struct element *e, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out);
  127. static void instance_free (struct instance *o);
  128. enum {STRING_CALLER, STRING_INDEX, STRING_ELEM, STRING_KEY, STRING_VAL};
  129. static struct NCD_string_request strings[] = {
  130. {"_caller"}, {"_index"}, {"_elem"}, {"_key"}, {"_val"}, {NULL}
  131. };
  132. static void assert_state (struct instance *o)
  133. {
  134. ASSERT(o->num_elems >= 0)
  135. ASSERT(o->gp >= 0)
  136. ASSERT(o->ip >= 0)
  137. ASSERT(o->gp <= o->num_elems)
  138. ASSERT(o->ip <= o->num_elems)
  139. ASSERT(o->gp <= o->ip)
  140. #ifndef NDEBUG
  141. // check GP
  142. for (int i = 0; i < o->gp; i++) {
  143. if (i == o->gp - 1) {
  144. ASSERT(o->elems[i].state == ESTATE_UP || o->elems[i].state == ESTATE_DOWN ||
  145. o->elems[i].state == ESTATE_WAITING)
  146. } else {
  147. ASSERT(o->elems[i].state == ESTATE_UP)
  148. }
  149. }
  150. // check IP
  151. int ip = o->num_elems;
  152. while (ip > 0 && o->elems[ip - 1].state == ESTATE_FORGOTTEN) {
  153. ip--;
  154. }
  155. ASSERT(o->ip == ip)
  156. // check gap
  157. for (int i = o->gp; i < o->ip; i++) {
  158. if (i == o->ip - 1) {
  159. ASSERT(o->elems[i].state == ESTATE_UP || o->elems[i].state == ESTATE_DOWN ||
  160. o->elems[i].state == ESTATE_WAITING || o->elems[i].state == ESTATE_TERMINATING)
  161. } else {
  162. ASSERT(o->elems[i].state == ESTATE_UP || o->elems[i].state == ESTATE_DOWN ||
  163. o->elems[i].state == ESTATE_WAITING)
  164. }
  165. }
  166. #endif
  167. }
  168. static void work (struct instance *o)
  169. {
  170. assert_state(o);
  171. // stop timer
  172. BReactor_RemoveTimer(o->i->params->iparams->reactor, &o->timer);
  173. if (o->state == ISTATE_WAITING) {
  174. return;
  175. }
  176. 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))) {
  177. // signal down
  178. NCDModuleInst_Backend_Down(o->i);
  179. // set state waiting
  180. o->state = ISTATE_WAITING;
  181. return;
  182. }
  183. if (o->gp < o->ip) {
  184. // get last element
  185. struct element *le = &o->elems[o->ip - 1];
  186. ASSERT(le->state != ESTATE_FORGOTTEN)
  187. // start terminating if not already
  188. if (le->state != ESTATE_TERMINATING) {
  189. // request termination
  190. NCDModuleProcess_Terminate(&le->process);
  191. // set element state terminating
  192. le->state = ESTATE_TERMINATING;
  193. }
  194. return;
  195. }
  196. if (o->state == ISTATE_TERMINATING) {
  197. // finally die
  198. instance_free(o);
  199. return;
  200. }
  201. if (o->gp == o->num_elems && (o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)) {
  202. if (o->state == ISTATE_WORKING) {
  203. // signal up
  204. NCDModuleInst_Backend_Up(o->i);
  205. // set state up
  206. o->state = ISTATE_UP;
  207. }
  208. return;
  209. }
  210. if (o->gp > 0 && o->elems[o->gp - 1].state == ESTATE_WAITING) {
  211. // get last element
  212. struct element *le = &o->elems[o->gp - 1];
  213. // continue process
  214. NCDModuleProcess_Continue(&le->process);
  215. // set state down
  216. le->state = ESTATE_DOWN;
  217. return;
  218. }
  219. if (o->gp > 0 && o->elems[o->gp - 1].state == ESTATE_DOWN) {
  220. return;
  221. }
  222. ASSERT(o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)
  223. advance(o);
  224. return;
  225. }
  226. static void advance (struct instance *o)
  227. {
  228. assert_state(o);
  229. ASSERT(o->gp == o->ip)
  230. ASSERT(o->gp < o->num_elems)
  231. ASSERT(o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)
  232. ASSERT(o->elems[o->gp].state == ESTATE_FORGOTTEN)
  233. // get next element
  234. struct element *e = &o->elems[o->gp];
  235. // init process
  236. if (!NCDModuleProcess_Init(&e->process, o->i, o->template_name, o->args, e, (NCDModuleProcess_handler_event)element_process_handler_event)) {
  237. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  238. goto fail;
  239. }
  240. // set special functions
  241. NCDModuleProcess_SetSpecialFuncs(&e->process, (NCDModuleProcess_func_getspecialobj)element_process_func_getspecialobj);
  242. // set element state down
  243. e->state = ESTATE_DOWN;
  244. // increment GP and IP
  245. o->gp++;
  246. o->ip++;
  247. return;
  248. fail:
  249. // set timer
  250. BReactor_SetTimer(o->i->params->iparams->reactor, &o->timer);
  251. }
  252. static void timer_handler (struct instance *o)
  253. {
  254. assert_state(o);
  255. ASSERT(o->gp == o->ip)
  256. ASSERT(o->gp < o->num_elems)
  257. ASSERT(o->gp == 0 || o->elems[o->gp - 1].state == ESTATE_UP)
  258. ASSERT(o->elems[o->gp].state == ESTATE_FORGOTTEN)
  259. advance(o);
  260. return;
  261. }
  262. static void element_process_handler_event (struct element *e, int event)
  263. {
  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 (struct element *e, NCD_string_id_t name, NCDObject *out_object)
  302. {
  303. struct instance *o = e->inst;
  304. ASSERT(e->state != ESTATE_FORGOTTEN)
  305. switch (o->type) {
  306. case NCDVAL_LIST: {
  307. NCD_string_id_t index_name = (o->name2 >= 0 ? o->name1 : -1);
  308. NCD_string_id_t elem_name = (o->name2 >= 0 ? o->name2 : o->name1);
  309. if (index_name >= 0 && name == index_name) {
  310. *out_object = NCDObject_Build(NULL, e, (NCDObject_func_getvar)element_list_index_object_func_getvar, NULL);
  311. return 1;
  312. }
  313. if (name == elem_name) {
  314. *out_object = NCDObject_Build(NULL, e, (NCDObject_func_getvar)element_list_elem_object_func_getvar, NULL);
  315. return 1;
  316. }
  317. } break;
  318. case NCDVAL_MAP: {
  319. NCD_string_id_t key_name = o->name1;
  320. NCD_string_id_t val_name = o->name2;
  321. if (name == key_name) {
  322. *out_object = NCDObject_Build(NULL, e, (NCDObject_func_getvar)element_map_key_object_func_getvar, NULL);
  323. return 1;
  324. }
  325. if (val_name >= 0 && name == val_name) {
  326. *out_object = NCDObject_Build(NULL, e, (NCDObject_func_getvar)element_map_val_object_func_getvar, NULL);
  327. return 1;
  328. }
  329. } break;
  330. }
  331. if (NCDVal_IsInvalid(o->args)) {
  332. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  333. }
  334. if (name == strings[STRING_CALLER].id) {
  335. *out_object = NCDObject_Build(NULL, e, NULL, (NCDObject_func_getobj)element_caller_object_func_getobj);
  336. return 1;
  337. }
  338. return 0;
  339. }
  340. static int element_caller_object_func_getobj (struct element *e, NCD_string_id_t name, NCDObject *out_object)
  341. {
  342. struct instance *o = e->inst;
  343. ASSERT(e->state != ESTATE_FORGOTTEN)
  344. ASSERT(!NCDVal_IsInvalid(o->args))
  345. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  346. }
  347. static int element_list_index_object_func_getvar (struct element *e, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  348. {
  349. struct instance *o = e->inst;
  350. ASSERT(e->state != ESTATE_FORGOTTEN)
  351. ASSERT(o->type == NCDVAL_LIST)
  352. if (name != NCD_STRING_EMPTY) {
  353. return 0;
  354. }
  355. char str[64];
  356. snprintf(str, sizeof(str), "%d", e->i);
  357. *out = NCDVal_NewString(mem, str);
  358. if (NCDVal_IsInvalid(*out)) {
  359. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  360. }
  361. return 1;
  362. }
  363. static int element_list_elem_object_func_getvar (struct element *e, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  364. {
  365. struct instance *o = e->inst;
  366. ASSERT(e->state != ESTATE_FORGOTTEN)
  367. ASSERT(o->type == NCDVAL_LIST)
  368. if (name != NCD_STRING_EMPTY) {
  369. return 0;
  370. }
  371. *out = NCDVal_NewCopy(mem, e->list_elem);
  372. if (NCDVal_IsInvalid(*out)) {
  373. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
  374. }
  375. return 1;
  376. }
  377. static int element_map_key_object_func_getvar (struct element *e, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  378. {
  379. struct instance *o = e->inst;
  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. if (NCDVal_IsInvalid(*out)) {
  387. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
  388. }
  389. return 1;
  390. }
  391. static int element_map_val_object_func_getvar (struct element *e, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  392. {
  393. struct instance *o = e->inst;
  394. ASSERT(e->state != ESTATE_FORGOTTEN)
  395. ASSERT(o->type == NCDVAL_MAP)
  396. if (name != NCD_STRING_EMPTY) {
  397. return 0;
  398. }
  399. *out = NCDVal_NewCopy(mem, e->map_val);
  400. if (NCDVal_IsInvalid(*out)) {
  401. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
  402. }
  403. return 1;
  404. }
  405. static void func_new_common (void *vo, NCDModuleInst *i, NCDValRef collection, const char *template_name, NCDValRef args, NCD_string_id_t name1, NCD_string_id_t name2)
  406. {
  407. ASSERT(!NCDVal_IsInvalid(collection))
  408. ASSERT(template_name)
  409. ASSERT(NCDVal_IsInvalid(args) || NCDVal_IsList(args))
  410. ASSERT(name1 >= 0)
  411. struct instance *o = vo;
  412. o->i = i;
  413. o->type = NCDVal_Type(collection);
  414. o->template_name = template_name;
  415. o->args = args;
  416. o->name1 = name1;
  417. o->name2 = name2;
  418. // init timer
  419. btime_t retry_time = NCDModuleInst_Backend_InterpGetRetryTime(i);
  420. BTimer_Init(&o->timer, retry_time, (BTimer_handler)timer_handler, o);
  421. size_t num_elems;
  422. NCDValMapElem cur_map_elem;
  423. switch (o->type) {
  424. case NCDVAL_LIST: {
  425. num_elems = NCDVal_ListCount(collection);
  426. } break;
  427. case NCDVAL_MAP: {
  428. num_elems = NCDVal_MapCount(collection);
  429. cur_map_elem = NCDVal_MapOrderedFirst(collection);
  430. } break;
  431. default:
  432. ModuleLog(i, BLOG_ERROR, "invalid collection type");
  433. goto fail0;
  434. }
  435. if (num_elems > INT_MAX) {
  436. ModuleLog(i, BLOG_ERROR, "too many elements");
  437. goto fail0;
  438. }
  439. o->num_elems = num_elems;
  440. // allocate elements
  441. if (!(o->elems = BAllocArray(o->num_elems, sizeof(o->elems[0])))) {
  442. ModuleLog(i, BLOG_ERROR, "BAllocArray failed");
  443. goto fail0;
  444. }
  445. for (int j = 0; j < o->num_elems; j++) {
  446. struct element *e = &o->elems[j];
  447. // set instance
  448. e->inst = o;
  449. // set index
  450. e->i = j;
  451. // set state forgotten
  452. e->state = ESTATE_FORGOTTEN;
  453. // set values
  454. switch (o->type) {
  455. case NCDVAL_LIST: {
  456. e->list_elem = NCDVal_ListGet(collection, j);
  457. } break;
  458. case NCDVAL_MAP: {
  459. e->map_key = NCDVal_MapElemKey(collection, cur_map_elem);
  460. e->map_val = NCDVal_MapElemVal(collection, cur_map_elem);
  461. cur_map_elem = NCDVal_MapOrderedNext(collection, cur_map_elem);
  462. } break;
  463. }
  464. }
  465. // set GP and IP zero
  466. o->gp = 0;
  467. o->ip = 0;
  468. // set state working
  469. o->state = ISTATE_WORKING;
  470. work(o);
  471. return;
  472. fail0:
  473. NCDModuleInst_Backend_SetError(i);
  474. NCDModuleInst_Backend_Dead(i);
  475. }
  476. static void func_new_foreach (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  477. {
  478. // read arguments
  479. NCDValRef arg_collection;
  480. NCDValRef arg_template;
  481. NCDValRef arg_args;
  482. if (!NCDVal_ListRead(params->args, 3, &arg_collection, &arg_template, &arg_args)) {
  483. ModuleLog(i, BLOG_ERROR, "wrong arity");
  484. goto fail0;
  485. }
  486. if (!NCDVal_IsStringNoNulls(arg_template) || !NCDVal_IsList(arg_args)) {
  487. ModuleLog(i, BLOG_ERROR, "wrong type");
  488. goto fail0;
  489. }
  490. const char *template_name = NCDVal_StringValue(arg_template);
  491. NCD_string_id_t name1;
  492. NCD_string_id_t name2;
  493. switch (NCDVal_Type(arg_collection)) {
  494. case NCDVAL_LIST: {
  495. name1 = strings[STRING_INDEX].id;
  496. name2 = strings[STRING_ELEM].id;
  497. } break;
  498. case NCDVAL_MAP: {
  499. name1 = strings[STRING_KEY].id;
  500. name2 = strings[STRING_VAL].id;
  501. } break;
  502. default:
  503. ModuleLog(i, BLOG_ERROR, "invalid collection type");
  504. goto fail0;
  505. }
  506. func_new_common(vo, i, arg_collection, template_name, arg_args, name1, name2);
  507. return;
  508. fail0:
  509. NCDModuleInst_Backend_SetError(i);
  510. NCDModuleInst_Backend_Dead(i);
  511. }
  512. static void func_new_foreach_emb (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  513. {
  514. // read arguments
  515. NCDValRef arg_collection;
  516. NCDValRef arg_template;
  517. NCDValRef arg_name1;
  518. NCDValRef arg_name2 = NCDVal_NewInvalid();
  519. 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)) {
  520. ModuleLog(i, BLOG_ERROR, "wrong arity");
  521. goto fail0;
  522. }
  523. if (!NCDVal_IsStringNoNulls(arg_template) || !NCDVal_IsStringNoNulls(arg_name1) || (!NCDVal_IsInvalid(arg_name2) && !NCDVal_IsStringNoNulls(arg_name2))) {
  524. ModuleLog(i, BLOG_ERROR, "wrong type");
  525. goto fail0;
  526. }
  527. const char *template_name = NCDVal_StringValue(arg_template);
  528. const char *name1_str = NCDVal_StringValue(arg_name1);
  529. const char *name2_str = (NCDVal_IsInvalid(arg_name2) ? NULL : NCDVal_StringValue(arg_name2));
  530. NCD_string_id_t name1 = NCDStringIndex_Get(i->params->iparams->string_index, name1_str);
  531. if (name1 < 0) {
  532. ModuleLog(i, BLOG_ERROR, "NCDStringIndex_Get failed");
  533. goto fail0;
  534. }
  535. NCD_string_id_t name2 = -1;
  536. if (name2_str && (name2 = NCDStringIndex_Get(i->params->iparams->string_index, name2_str)) < 0) {
  537. ModuleLog(i, BLOG_ERROR, "NCDStringIndex_Get failed");
  538. goto fail0;
  539. }
  540. func_new_common(vo, i, arg_collection, template_name, NCDVal_NewInvalid(), name1, name2);
  541. return;
  542. fail0:
  543. NCDModuleInst_Backend_SetError(i);
  544. NCDModuleInst_Backend_Dead(i);
  545. }
  546. static void instance_free (struct instance *o)
  547. {
  548. ASSERT(o->gp == 0)
  549. ASSERT(o->ip == 0)
  550. // free elements
  551. BFree(o->elems);
  552. // free timer
  553. BReactor_RemoveTimer(o->i->params->iparams->reactor, &o->timer);
  554. NCDModuleInst_Backend_Dead(o->i);
  555. }
  556. static void func_die (void *vo)
  557. {
  558. struct instance *o = vo;
  559. assert_state(o);
  560. ASSERT(o->state != ISTATE_TERMINATING)
  561. // set GP zero
  562. o->gp = 0;
  563. // set state terminating
  564. o->state = ISTATE_TERMINATING;
  565. work(o);
  566. return;
  567. }
  568. static void func_clean (void *vo)
  569. {
  570. struct instance *o = vo;
  571. if (o->state != ISTATE_WAITING) {
  572. return;
  573. }
  574. // set state working
  575. o->state = ISTATE_WORKING;
  576. work(o);
  577. return;
  578. }
  579. static struct NCDModule modules[] = {
  580. {
  581. .type = "foreach",
  582. .func_new2 = func_new_foreach,
  583. .func_die = func_die,
  584. .func_clean = func_clean,
  585. .alloc_size = sizeof(struct instance)
  586. }, {
  587. .type = "foreach_emb",
  588. .func_new2 = func_new_foreach_emb,
  589. .func_die = func_die,
  590. .func_clean = func_clean,
  591. .alloc_size = sizeof(struct instance)
  592. }, {
  593. .type = NULL
  594. }
  595. };
  596. const struct NCDModuleGroup ncdmodule_foreach = {
  597. .modules = modules,
  598. .strings = strings
  599. };