call2.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. * call(string template, list args)
  33. *
  34. * Description:
  35. * Calls a process template. The 'template' argument is the name of the process
  36. * template to call, and the 'list' argument is a list of arguments for the
  37. * process template. Calling a process template is roughly equivalent to placing
  38. * the statements within that template into the place of call(), except for the
  39. * points presented next. The 'template' argument can be a special value "<none>",
  40. * which makes call() a no-op.
  41. *
  42. * The process created from the called template will be able to access the arguments
  43. * that were given in the 'args' argument to call() via the '_argN' predefined\
  44. * objects (e.g. _arg0 for the first argumens), and also via '_args' for the entire
  45. * argument list.
  46. *
  47. * The called process also will be able to access objects within the calling
  48. * process as seen by the call() statement. However such any access needs to happen
  49. * via a special '_caller' predefined object. For example, if there is a statement
  50. * 'var("a") x;' somewhere above the call() statement, the called process can access
  51. * it as '_caller.x'.
  52. *
  53. * Note that call() preserves backtracking semantics, i.e. when a statement within
  54. * the called process goes down after having gone up, the behaviour really is as
  55. * if the call() statement was replaced with the statements in the called template,
  56. * (disregarding variable resolution).
  57. *
  58. * Because the template name is an argument, call() can be used for branching.
  59. * For example, if we have an object 'x' with the value "true" or "false", a
  60. * branch can be performed by defining two process templates, 'branch_true'
  61. * and 'branch_false', and branching with the following code:
  62. *
  63. * concat("branch_", x) name;
  64. * call(name, {});
  65. *
  66. * Synopsis:
  67. * call_with_caller_target(string template, list args, string caller_target)
  68. *
  69. * Description:
  70. * Like call(), except that the target of the '_caller' predefined object is
  71. * specified by the 'caller_target' argument. This is indented to be used from
  72. * generic code for user-specified callbacks, allowing the user to easily refer to
  73. * his own objects from inside the callback.
  74. *
  75. * The 'caller_target' must be a non-empty string referring to an actual object;
  76. * there is no choice of 'caller_target' that would make call_with_caller_target()
  77. * equivalent to call().
  78. *
  79. * Synopsis:
  80. * embcall2_multif(string cond1, string template1, ..., [string else_template])
  81. *
  82. * Description:
  83. * This is an internal command used to implement the 'If' clause. The arguments
  84. * are pairs of (cond, template), where 'cond' is a condition in form of a string,
  85. * and 'template' is the name of the process template for this condition. The
  86. * template corresponding to the first condition equal to "true" is called; if
  87. * there is no true condition, either the template 'else_template' is called,
  88. * if it is provided, or nothing is performed, if 'else_template' is not provided.
  89. */
  90. #include <stdlib.h>
  91. #include <string.h>
  92. #include <misc/debug.h>
  93. #include <misc/offset.h>
  94. #include <ncd/module_common.h>
  95. #include <generated/blog_channel_ncd_call2.h>
  96. #define STATE_WORKING 1
  97. #define STATE_UP 2
  98. #define STATE_WAITING 3
  99. #define STATE_TERMINATING 4
  100. #define STATE_NONE 5
  101. #define NUM_STATIC_NAMES 4
  102. struct instance {
  103. NCDModuleInst *i;
  104. NCDModuleProcess process;
  105. int state;
  106. };
  107. struct instance_with_caller_target {
  108. struct instance base;
  109. NCD_string_id_t *dynamic_names;
  110. size_t num_names;
  111. NCD_string_id_t static_names[NUM_STATIC_NAMES];
  112. };
  113. #define NAMES_PARAM_NAME CallNames
  114. #define NAMES_PARAM_TYPE struct instance_with_caller_target
  115. #define NAMES_PARAM_MEMBER_DYNAMIC_NAMES dynamic_names
  116. #define NAMES_PARAM_MEMBER_STATIC_NAMES static_names
  117. #define NAMES_PARAM_MEMBER_NUM_NAMES num_names
  118. #define NAMES_PARAM_NUM_STATIC_NAMES NUM_STATIC_NAMES
  119. #include <ncd/extra/make_fast_names.h>
  120. static void process_handler_event (NCDModuleProcess *process, int event);
  121. static int process_func_getspecialobj_embed (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object);
  122. static int process_func_getspecialobj_noembed (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object);
  123. static int process_func_getspecialobj_with_caller_target (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object);
  124. static int caller_obj_func_getobj (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object);
  125. static int caller_obj_func_getobj_with_caller_target (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object);
  126. static void func_new_templ (void *vo, NCDModuleInst *i, NCDValRef template_name, NCDValRef args, int embed);
  127. static void instance_free (struct instance *o);
  128. static void process_handler_event (NCDModuleProcess *process, int event)
  129. {
  130. struct instance *o = UPPER_OBJECT(process, struct instance, process);
  131. switch (event) {
  132. case NCDMODULEPROCESS_EVENT_UP: {
  133. ASSERT(o->state == STATE_WORKING)
  134. // signal up
  135. NCDModuleInst_Backend_Up(o->i);
  136. // set state up
  137. o->state = STATE_UP;
  138. } break;
  139. case NCDMODULEPROCESS_EVENT_DOWN: {
  140. ASSERT(o->state == STATE_UP)
  141. // signal down
  142. NCDModuleInst_Backend_Down(o->i);
  143. // set state waiting
  144. o->state = STATE_WAITING;
  145. } break;
  146. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  147. ASSERT(o->state == STATE_TERMINATING)
  148. // die finally
  149. instance_free(o);
  150. return;
  151. } break;
  152. default: ASSERT(0);
  153. }
  154. }
  155. static int process_func_getspecialobj_embed (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object)
  156. {
  157. struct instance *o = UPPER_OBJECT(process, struct instance, process);
  158. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  159. }
  160. static int process_func_getspecialobj_noembed (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object)
  161. {
  162. struct instance *o = UPPER_OBJECT(process, struct instance, process);
  163. if (name == NCD_STRING_CALLER) {
  164. *out_object = NCDObject_Build(-1, o, NCDObject_no_getvar, caller_obj_func_getobj);
  165. return 1;
  166. }
  167. return 0;
  168. }
  169. static int process_func_getspecialobj_with_caller_target (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object)
  170. {
  171. struct instance *o = UPPER_OBJECT(process, struct instance, process);
  172. if (name == NCD_STRING_CALLER) {
  173. *out_object = NCDObject_Build(-1, o, NCDObject_no_getvar, caller_obj_func_getobj_with_caller_target);
  174. return 1;
  175. }
  176. return 0;
  177. }
  178. static int caller_obj_func_getobj (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object)
  179. {
  180. struct instance *o = NCDObject_DataPtr(obj);
  181. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  182. }
  183. static int caller_obj_func_getobj_with_caller_target (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object)
  184. {
  185. struct instance_with_caller_target *o_ch = NCDObject_DataPtr(obj);
  186. ASSERT(o_ch->num_names > 0)
  187. NCD_string_id_t *names = CallNames_GetNames(o_ch);
  188. NCDObject object;
  189. if (!NCDModuleInst_Backend_GetObj(o_ch->base.i, names[0], &object)) {
  190. return 0;
  191. }
  192. NCDObject obj2;
  193. if (!NCDObject_ResolveObjExprCompact(&object, names + 1, o_ch->num_names - 1, &obj2)) {
  194. return 0;
  195. }
  196. if (name == NCD_STRING_EMPTY) {
  197. *out_object = obj2;
  198. return 1;
  199. }
  200. return NCDObject_GetObj(&obj2, name, out_object);
  201. }
  202. static void func_new_templ (void *vo, NCDModuleInst *i, NCDValRef template_name, NCDValRef args, int embed)
  203. {
  204. ASSERT(NCDVal_IsInvalid(template_name) || NCDVal_IsString(template_name))
  205. ASSERT(NCDVal_IsInvalid(args) || NCDVal_IsList(args))
  206. ASSERT(embed == !!embed)
  207. struct instance *o = vo;
  208. o->i = i;
  209. if (NCDVal_IsInvalid(template_name) || ncd_is_none(template_name)) {
  210. // signal up
  211. NCDModuleInst_Backend_Up(o->i);
  212. // set state none
  213. o->state = STATE_NONE;
  214. } else {
  215. // create process
  216. if (!NCDModuleProcess_InitValue(&o->process, o->i, template_name, args, process_handler_event)) {
  217. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  218. goto fail0;
  219. }
  220. // set special functions
  221. if (embed) {
  222. NCDModuleProcess_SetSpecialFuncs(&o->process, process_func_getspecialobj_embed);
  223. } else {
  224. NCDModuleProcess_SetSpecialFuncs(&o->process, process_func_getspecialobj_noembed);
  225. }
  226. // set state working
  227. o->state = STATE_WORKING;
  228. }
  229. return;
  230. fail0:
  231. NCDModuleInst_Backend_DeadError(i);
  232. }
  233. static void instance_free (struct instance *o)
  234. {
  235. // free process
  236. if (o->state != STATE_NONE) {
  237. NCDModuleProcess_Free(&o->process);
  238. }
  239. NCDModuleInst_Backend_Dead(o->i);
  240. }
  241. static void func_new_call (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  242. {
  243. NCDValRef template_arg;
  244. NCDValRef args_arg;
  245. if (!NCDVal_ListRead(params->args, 2, &template_arg, &args_arg)) {
  246. ModuleLog(i, BLOG_ERROR, "wrong arity");
  247. goto fail0;
  248. }
  249. if (!NCDVal_IsString(template_arg) || !NCDVal_IsList(args_arg)) {
  250. ModuleLog(i, BLOG_ERROR, "wrong type");
  251. goto fail0;
  252. }
  253. func_new_templ(vo, i, template_arg, args_arg, 0);
  254. return;
  255. fail0:
  256. NCDModuleInst_Backend_DeadError(i);
  257. }
  258. static void func_new_call_with_caller_target (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  259. {
  260. struct instance *o = vo;
  261. struct instance_with_caller_target *o_ct = vo;
  262. o->i = i;
  263. NCDValRef template_arg;
  264. NCDValRef args_arg;
  265. NCDValRef caller_target_arg;
  266. if (!NCDVal_ListRead(params->args, 3, &template_arg, &args_arg, &caller_target_arg)) {
  267. ModuleLog(i, BLOG_ERROR, "wrong arity");
  268. goto fail0;
  269. }
  270. if (!NCDVal_IsString(template_arg) || !NCDVal_IsList(args_arg) || !NCDVal_IsString(caller_target_arg)) {
  271. ModuleLog(i, BLOG_ERROR, "wrong type");
  272. goto fail0;
  273. }
  274. NCDValContString cts;
  275. if (!NCDVal_StringContinuize(caller_target_arg, &cts)) {
  276. ModuleLog(i, BLOG_ERROR, "NCDVal_StringContinuize failed");
  277. goto fail0;
  278. }
  279. int res = CallNames_InitNames(o_ct, i->params->iparams->string_index, cts.data, NCDVal_StringLength(caller_target_arg));
  280. NCDValContString_Free(&cts);
  281. if (!res) {
  282. ModuleLog(i, BLOG_ERROR, "CallerNames_InitNames failed");
  283. goto fail0;
  284. }
  285. if (ncd_is_none(template_arg)) {
  286. // signal up
  287. NCDModuleInst_Backend_Up(i);
  288. // set state none
  289. o->state = STATE_NONE;
  290. } else {
  291. // create process
  292. if (!NCDModuleProcess_InitValue(&o->process, i, template_arg, args_arg, process_handler_event)) {
  293. ModuleLog(i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  294. goto fail1;
  295. }
  296. // set special functions
  297. NCDModuleProcess_SetSpecialFuncs(&o->process, process_func_getspecialobj_with_caller_target);
  298. // set state working
  299. o->state = STATE_WORKING;
  300. }
  301. return;
  302. fail1:
  303. CallNames_FreeNames(o_ct);
  304. fail0:
  305. NCDModuleInst_Backend_DeadError(i);
  306. }
  307. static void func_new_embcall_multif (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  308. {
  309. NCDValRef args = params->args;
  310. NCDValRef template_value = NCDVal_NewInvalid();
  311. size_t count = NCDVal_ListCount(args);
  312. size_t j = 0;
  313. while (j < count) {
  314. NCDValRef arg = NCDVal_ListGet(args, j);
  315. if (j == count - 1) {
  316. if (!NCDVal_IsString(arg)) {
  317. ModuleLog(i, BLOG_ERROR, "bad arguments");
  318. goto fail0;
  319. }
  320. template_value = arg;
  321. break;
  322. }
  323. NCDValRef arg2 = NCDVal_ListGet(args, j + 1);
  324. int arg_val;
  325. if (!ncd_read_boolean(arg, &arg_val) || !NCDVal_IsString(arg2)) {
  326. ModuleLog(i, BLOG_ERROR, "bad arguments");
  327. goto fail0;
  328. }
  329. if (arg_val) {
  330. template_value = arg2;
  331. break;
  332. }
  333. j += 2;
  334. }
  335. func_new_templ(vo, i, template_value, NCDVal_NewInvalid(), 1);
  336. return;
  337. fail0:
  338. NCDModuleInst_Backend_DeadError(i);
  339. }
  340. static void func_die (void *vo)
  341. {
  342. struct instance *o = vo;
  343. ASSERT(o->state != STATE_TERMINATING)
  344. // if none, die now
  345. if (o->state == STATE_NONE) {
  346. instance_free(o);
  347. return;
  348. }
  349. // request process to terminate
  350. NCDModuleProcess_Terminate(&o->process);
  351. // set state terminating
  352. o->state = STATE_TERMINATING;
  353. }
  354. static void func_die_with_caller_target (void *vo)
  355. {
  356. struct instance_with_caller_target *o_ct = vo;
  357. CallNames_FreeNames(o_ct);
  358. func_die(vo);
  359. }
  360. static void func_clean (void *vo)
  361. {
  362. struct instance *o = vo;
  363. if (o->state != STATE_WAITING) {
  364. return;
  365. }
  366. // allow process to continue
  367. NCDModuleProcess_Continue(&o->process);
  368. // set state working
  369. o->state = STATE_WORKING;
  370. }
  371. static int func_getobj (void *vo, NCD_string_id_t name, NCDObject *out_object)
  372. {
  373. struct instance *o = vo;
  374. if (o->state == STATE_NONE) {
  375. return 0;
  376. }
  377. return NCDModuleProcess_GetObj(&o->process, name, out_object);
  378. }
  379. static struct NCDModule modules[] = {
  380. {
  381. .type = "call",
  382. .func_new2 = func_new_call,
  383. .func_die = func_die,
  384. .func_clean = func_clean,
  385. .func_getobj = func_getobj,
  386. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN|NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS,
  387. .alloc_size = sizeof(struct instance)
  388. }, {
  389. .type = "call_with_caller_target",
  390. .func_new2 = func_new_call_with_caller_target,
  391. .func_die = func_die_with_caller_target,
  392. .func_clean = func_clean,
  393. .func_getobj = func_getobj,
  394. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN|NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS,
  395. .alloc_size = sizeof(struct instance_with_caller_target)
  396. }, {
  397. .type = "embcall2_multif",
  398. .func_new2 = func_new_embcall_multif,
  399. .func_die = func_die,
  400. .func_clean = func_clean,
  401. .func_getobj = func_getobj,
  402. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN|NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS,
  403. .alloc_size = sizeof(struct instance)
  404. }, {
  405. .type = NULL
  406. }
  407. };
  408. const struct NCDModuleGroup ncdmodule_call2 = {
  409. .modules = modules
  410. };