call2.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /**
  2. * @file call2.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. * call2(string template, list args)
  33. * call2_if(string cond, string template, list args)
  34. * call2_ifelse(string cond, string template, string else_template, list args)
  35. * embcall2(string template)
  36. * embcall2_if(string cond, string template)
  37. * embcall2_ifelse(string cond, string template, string else_template)
  38. * embcall2_multif(string cond1, string template1, ..., [string else_template])
  39. */
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <misc/debug.h>
  43. #include <misc/offset.h>
  44. #include <ncd/NCDModule.h>
  45. #include <ncd/static_strings.h>
  46. #include <ncd/value_utils.h>
  47. #include <generated/blog_channel_ncd_call2.h>
  48. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  49. #define STATE_WORKING 1
  50. #define STATE_UP 2
  51. #define STATE_WAITING 3
  52. #define STATE_TERMINATING 4
  53. #define STATE_NONE 5
  54. struct instance {
  55. NCDModuleInst *i;
  56. NCDModuleProcess process;
  57. int embed;
  58. int state;
  59. };
  60. static void process_handler_event (NCDModuleProcess *process, int event);
  61. static int process_func_getspecialobj (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object);
  62. static int caller_obj_func_getobj (struct instance *o, NCD_string_id_t name, NCDObject *out_object);
  63. static void func_new_templ (void *vo, NCDModuleInst *i, NCDValRef template_name, NCDValRef args, int embed);
  64. static void instance_free (struct instance *o);
  65. enum {STRING_CALLER};
  66. static struct NCD_string_request strings[] = {
  67. {"_caller"}, {NULL}
  68. };
  69. static void process_handler_event (NCDModuleProcess *process, int event)
  70. {
  71. struct instance *o = UPPER_OBJECT(process, struct instance, process);
  72. switch (event) {
  73. case NCDMODULEPROCESS_EVENT_UP: {
  74. ASSERT(o->state == STATE_WORKING)
  75. // signal up
  76. NCDModuleInst_Backend_Up(o->i);
  77. // set state up
  78. o->state = STATE_UP;
  79. } break;
  80. case NCDMODULEPROCESS_EVENT_DOWN: {
  81. ASSERT(o->state == STATE_UP)
  82. // signal down
  83. NCDModuleInst_Backend_Down(o->i);
  84. // set state waiting
  85. o->state = STATE_WAITING;
  86. } break;
  87. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  88. ASSERT(o->state == STATE_TERMINATING)
  89. // die finally
  90. instance_free(o);
  91. return;
  92. } break;
  93. default: ASSERT(0);
  94. }
  95. }
  96. static int process_func_getspecialobj (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object)
  97. {
  98. struct instance *o = UPPER_OBJECT(process, struct instance, process);
  99. if (o->embed) {
  100. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  101. }
  102. if (name == strings[STRING_CALLER].id) {
  103. *out_object = NCDObject_Build(-1, o, NULL, (NCDObject_func_getobj)caller_obj_func_getobj);
  104. return 1;
  105. }
  106. return 0;
  107. }
  108. static int caller_obj_func_getobj (struct instance *o, NCD_string_id_t name, NCDObject *out_object)
  109. {
  110. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  111. }
  112. static void func_new_templ (void *vo, NCDModuleInst *i, NCDValRef template_name, NCDValRef args, int embed)
  113. {
  114. ASSERT(NCDVal_IsInvalid(template_name) || NCDVal_IsString(template_name))
  115. ASSERT(NCDVal_IsInvalid(args) || NCDVal_IsList(args))
  116. ASSERT(embed == !!embed)
  117. struct instance *o = vo;
  118. o->i = i;
  119. // remember embed
  120. o->embed = embed;
  121. if (NCDVal_IsInvalid(template_name) || ncd_is_none(template_name)) {
  122. // signal up
  123. NCDModuleInst_Backend_Up(o->i);
  124. // set state none
  125. o->state = STATE_NONE;
  126. } else {
  127. // create process
  128. if (!NCDModuleProcess_InitValue(&o->process, o->i, template_name, args, process_handler_event)) {
  129. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  130. goto fail0;
  131. }
  132. // set special functions
  133. NCDModuleProcess_SetSpecialFuncs(&o->process, process_func_getspecialobj);
  134. // set state working
  135. o->state = STATE_WORKING;
  136. }
  137. return;
  138. fail0:
  139. NCDModuleInst_Backend_SetError(i);
  140. NCDModuleInst_Backend_Dead(i);
  141. }
  142. static void instance_free (struct instance *o)
  143. {
  144. // free process
  145. if (o->state != STATE_NONE) {
  146. NCDModuleProcess_Free(&o->process);
  147. }
  148. NCDModuleInst_Backend_Dead(o->i);
  149. }
  150. static void func_new_call (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  151. {
  152. NCDValRef template_arg;
  153. NCDValRef args_arg;
  154. if (!NCDVal_ListRead(params->args, 2, &template_arg, &args_arg)) {
  155. ModuleLog(i, BLOG_ERROR, "wrong arity");
  156. goto fail0;
  157. }
  158. if (!NCDVal_IsString(template_arg) || !NCDVal_IsList(args_arg)) {
  159. ModuleLog(i, BLOG_ERROR, "wrong type");
  160. goto fail0;
  161. }
  162. func_new_templ(vo, i, template_arg, args_arg, 0);
  163. return;
  164. fail0:
  165. NCDModuleInst_Backend_SetError(i);
  166. NCDModuleInst_Backend_Dead(i);
  167. }
  168. static void func_new_embcall (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  169. {
  170. NCDValRef template_arg;
  171. if (!NCDVal_ListRead(params->args, 1, &template_arg)) {
  172. ModuleLog(i, BLOG_ERROR, "wrong arity");
  173. goto fail0;
  174. }
  175. if (!NCDVal_IsString(template_arg)) {
  176. ModuleLog(i, BLOG_ERROR, "wrong type");
  177. goto fail0;
  178. }
  179. func_new_templ(vo, i, template_arg, NCDVal_NewInvalid(), 1);
  180. return;
  181. fail0:
  182. NCDModuleInst_Backend_SetError(i);
  183. NCDModuleInst_Backend_Dead(i);
  184. }
  185. static void func_new_call_if (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  186. {
  187. NCDValRef cond_arg;
  188. NCDValRef template_arg;
  189. NCDValRef args_arg;
  190. if (!NCDVal_ListRead(params->args, 3, &cond_arg, &template_arg, &args_arg)) {
  191. ModuleLog(i, BLOG_ERROR, "wrong arity");
  192. goto fail0;
  193. }
  194. if (!NCDVal_IsString(cond_arg) || !NCDVal_IsString(template_arg) || !NCDVal_IsList(args_arg)) {
  195. ModuleLog(i, BLOG_ERROR, "wrong type");
  196. goto fail0;
  197. }
  198. if (ncd_read_boolean(cond_arg)) {
  199. template_arg = NCDVal_NewInvalid();
  200. }
  201. func_new_templ(vo, i, template_arg, args_arg, 0);
  202. return;
  203. fail0:
  204. NCDModuleInst_Backend_SetError(i);
  205. NCDModuleInst_Backend_Dead(i);
  206. }
  207. static void func_new_embcall_if (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  208. {
  209. NCDValRef cond_arg;
  210. NCDValRef template_arg;
  211. if (!NCDVal_ListRead(params->args, 2, &cond_arg, &template_arg)) {
  212. ModuleLog(i, BLOG_ERROR, "wrong arity");
  213. goto fail0;
  214. }
  215. if (!NCDVal_IsString(cond_arg) || !NCDVal_IsString(template_arg)) {
  216. ModuleLog(i, BLOG_ERROR, "wrong type");
  217. goto fail0;
  218. }
  219. if (ncd_read_boolean(cond_arg)) {
  220. template_arg = NCDVal_NewInvalid();
  221. }
  222. func_new_templ(vo, i, template_arg, NCDVal_NewInvalid(), 1);
  223. return;
  224. fail0:
  225. NCDModuleInst_Backend_SetError(i);
  226. NCDModuleInst_Backend_Dead(i);
  227. }
  228. static void func_new_call_ifelse (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  229. {
  230. NCDValRef cond_arg;
  231. NCDValRef template_arg;
  232. NCDValRef else_template_arg;
  233. NCDValRef args_arg;
  234. if (!NCDVal_ListRead(params->args, 4, &cond_arg, &template_arg, &else_template_arg, &args_arg)) {
  235. ModuleLog(i, BLOG_ERROR, "wrong arity");
  236. goto fail0;
  237. }
  238. if (!NCDVal_IsString(cond_arg) || !NCDVal_IsString(template_arg) || !NCDVal_IsString(else_template_arg) || !NCDVal_IsList(args_arg)) {
  239. ModuleLog(i, BLOG_ERROR, "wrong type");
  240. goto fail0;
  241. }
  242. NCDValRef template_value;
  243. if (ncd_read_boolean(cond_arg)) {
  244. template_value = template_arg;
  245. } else {
  246. template_value = else_template_arg;
  247. }
  248. func_new_templ(vo, i, template_value, args_arg, 0);
  249. return;
  250. fail0:
  251. NCDModuleInst_Backend_SetError(i);
  252. NCDModuleInst_Backend_Dead(i);
  253. }
  254. static void func_new_embcall_ifelse (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  255. {
  256. NCDValRef cond_arg;
  257. NCDValRef template_arg;
  258. NCDValRef else_template_arg;
  259. if (!NCDVal_ListRead(params->args, 3, &cond_arg, &template_arg, &else_template_arg)) {
  260. ModuleLog(i, BLOG_ERROR, "wrong arity");
  261. goto fail0;
  262. }
  263. if (!NCDVal_IsString(cond_arg) || !NCDVal_IsString(template_arg) || !NCDVal_IsString(else_template_arg)) {
  264. ModuleLog(i, BLOG_ERROR, "wrong type");
  265. goto fail0;
  266. }
  267. NCDValRef template_value;
  268. if (ncd_read_boolean(cond_arg)) {
  269. template_value = template_arg;
  270. } else {
  271. template_value = else_template_arg;
  272. }
  273. func_new_templ(vo, i, template_value, NCDVal_NewInvalid(), 1);
  274. return;
  275. fail0:
  276. NCDModuleInst_Backend_SetError(i);
  277. NCDModuleInst_Backend_Dead(i);
  278. }
  279. static void func_new_embcall_multif (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  280. {
  281. NCDValRef args = params->args;
  282. NCDValRef template_value = NCDVal_NewInvalid();
  283. size_t count = NCDVal_ListCount(args);
  284. size_t j = 0;
  285. while (j < count) {
  286. NCDValRef arg = NCDVal_ListGet(args, j);
  287. if (j == count - 1) {
  288. if (!NCDVal_IsString(arg)) {
  289. ModuleLog(i, BLOG_ERROR, "bad arguments");
  290. goto fail0;
  291. }
  292. template_value = arg;
  293. break;
  294. }
  295. NCDValRef arg2 = NCDVal_ListGet(args, j + 1);
  296. if (!NCDVal_IsString(arg) || !NCDVal_IsString(arg2)) {
  297. ModuleLog(i, BLOG_ERROR, "bad arguments");
  298. goto fail0;
  299. }
  300. if (ncd_read_boolean(arg)) {
  301. template_value = arg2;
  302. break;
  303. }
  304. j += 2;
  305. }
  306. func_new_templ(vo, i, template_value, NCDVal_NewInvalid(), 1);
  307. return;
  308. fail0:
  309. NCDModuleInst_Backend_SetError(i);
  310. NCDModuleInst_Backend_Dead(i);
  311. }
  312. static void func_die (void *vo)
  313. {
  314. struct instance *o = vo;
  315. ASSERT(o->state != STATE_TERMINATING)
  316. // if none, die now
  317. if (o->state == STATE_NONE) {
  318. instance_free(o);
  319. return;
  320. }
  321. // request process to terminate
  322. NCDModuleProcess_Terminate(&o->process);
  323. // set state terminating
  324. o->state = STATE_TERMINATING;
  325. }
  326. static void func_clean (void *vo)
  327. {
  328. struct instance *o = vo;
  329. if (o->state != STATE_WAITING) {
  330. return;
  331. }
  332. // allow process to continue
  333. NCDModuleProcess_Continue(&o->process);
  334. // set state working
  335. o->state = STATE_WORKING;
  336. }
  337. static int func_getobj (void *vo, NCD_string_id_t name, NCDObject *out_object)
  338. {
  339. struct instance *o = vo;
  340. if (o->state == STATE_NONE) {
  341. return 0;
  342. }
  343. return NCDModuleProcess_GetObj(&o->process, name, out_object);
  344. }
  345. static struct NCDModule modules[] = {
  346. {
  347. .type = "call2",
  348. .func_new2 = func_new_call,
  349. .func_die = func_die,
  350. .func_clean = func_clean,
  351. .func_getobj = func_getobj,
  352. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  353. .alloc_size = sizeof(struct instance)
  354. }, {
  355. .type = "call2_if",
  356. .func_new2 = func_new_call_if,
  357. .func_die = func_die,
  358. .func_clean = func_clean,
  359. .func_getobj = func_getobj,
  360. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  361. .alloc_size = sizeof(struct instance)
  362. }, {
  363. .type = "call2_ifelse",
  364. .func_new2 = func_new_call_ifelse,
  365. .func_die = func_die,
  366. .func_clean = func_clean,
  367. .func_getobj = func_getobj,
  368. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  369. .alloc_size = sizeof(struct instance)
  370. }, {
  371. .type = "embcall2",
  372. .func_new2 = func_new_embcall,
  373. .func_die = func_die,
  374. .func_clean = func_clean,
  375. .func_getobj = func_getobj,
  376. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  377. .alloc_size = sizeof(struct instance)
  378. }, {
  379. .type = "embcall2_if",
  380. .func_new2 = func_new_embcall_if,
  381. .func_die = func_die,
  382. .func_clean = func_clean,
  383. .func_getobj = func_getobj,
  384. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  385. .alloc_size = sizeof(struct instance)
  386. }, {
  387. .type = "embcall2_ifelse",
  388. .func_new2 = func_new_embcall_ifelse,
  389. .func_die = func_die,
  390. .func_clean = func_clean,
  391. .func_getobj = func_getobj,
  392. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  393. .alloc_size = sizeof(struct instance)
  394. }, {
  395. .type = "embcall2_multif",
  396. .func_new2 = func_new_embcall_multif,
  397. .func_die = func_die,
  398. .func_clean = func_clean,
  399. .func_getobj = func_getobj,
  400. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  401. .alloc_size = sizeof(struct instance)
  402. }, {
  403. .type = NULL
  404. }
  405. };
  406. const struct NCDModuleGroup ncdmodule_call2 = {
  407. .modules = modules,
  408. .strings = strings
  409. };