call2.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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/NCDModule.h>
  95. #include <ncd/static_strings.h>
  96. #include <ncd/extra/value_utils.h>
  97. #include <generated/blog_channel_ncd_call2.h>
  98. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  99. #define STATE_WORKING 1
  100. #define STATE_UP 2
  101. #define STATE_WAITING 3
  102. #define STATE_TERMINATING 4
  103. #define STATE_NONE 5
  104. #define NUM_STATIC_NAMES 4
  105. struct instance {
  106. NCDModuleInst *i;
  107. NCDModuleProcess process;
  108. int state;
  109. };
  110. struct instance_with_caller_target {
  111. struct instance base;
  112. NCD_string_id_t *dynamic_names;
  113. size_t num_names;
  114. NCD_string_id_t static_names[NUM_STATIC_NAMES];
  115. };
  116. #define NAMES_PARAM_NAME CallNames
  117. #define NAMES_PARAM_TYPE struct instance_with_caller_target
  118. #define NAMES_PARAM_MEMBER_DYNAMIC_NAMES dynamic_names
  119. #define NAMES_PARAM_MEMBER_STATIC_NAMES static_names
  120. #define NAMES_PARAM_MEMBER_NUM_NAMES num_names
  121. #define NAMES_PARAM_NUM_STATIC_NAMES NUM_STATIC_NAMES
  122. #include <ncd/extra/make_fast_names.h>
  123. static void process_handler_event (NCDModuleProcess *process, int event);
  124. static int process_func_getspecialobj_embed (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object);
  125. static int process_func_getspecialobj_noembed (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object);
  126. static int process_func_getspecialobj_with_caller_target (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object);
  127. static int caller_obj_func_getobj (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object);
  128. static int caller_obj_func_getobj_with_caller_target (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object);
  129. static void func_new_templ (void *vo, NCDModuleInst *i, NCDValRef template_name, NCDValRef args, int embed);
  130. static void instance_free (struct instance *o);
  131. static void process_handler_event (NCDModuleProcess *process, int event)
  132. {
  133. struct instance *o = UPPER_OBJECT(process, struct instance, process);
  134. switch (event) {
  135. case NCDMODULEPROCESS_EVENT_UP: {
  136. ASSERT(o->state == STATE_WORKING)
  137. // signal up
  138. NCDModuleInst_Backend_Up(o->i);
  139. // set state up
  140. o->state = STATE_UP;
  141. } break;
  142. case NCDMODULEPROCESS_EVENT_DOWN: {
  143. ASSERT(o->state == STATE_UP)
  144. // signal down
  145. NCDModuleInst_Backend_Down(o->i);
  146. // set state waiting
  147. o->state = STATE_WAITING;
  148. } break;
  149. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  150. ASSERT(o->state == STATE_TERMINATING)
  151. // die finally
  152. instance_free(o);
  153. return;
  154. } break;
  155. default: ASSERT(0);
  156. }
  157. }
  158. static int process_func_getspecialobj_embed (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object)
  159. {
  160. struct instance *o = UPPER_OBJECT(process, struct instance, process);
  161. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  162. }
  163. static int process_func_getspecialobj_noembed (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object)
  164. {
  165. struct instance *o = UPPER_OBJECT(process, struct instance, process);
  166. if (name == NCD_STRING_CALLER) {
  167. *out_object = NCDObject_Build(-1, o, NCDObject_no_getvar, caller_obj_func_getobj);
  168. return 1;
  169. }
  170. return 0;
  171. }
  172. static int process_func_getspecialobj_with_caller_target (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object)
  173. {
  174. struct instance *o = UPPER_OBJECT(process, struct instance, process);
  175. if (name == NCD_STRING_CALLER) {
  176. *out_object = NCDObject_Build(-1, o, NCDObject_no_getvar, caller_obj_func_getobj_with_caller_target);
  177. return 1;
  178. }
  179. return 0;
  180. }
  181. static int caller_obj_func_getobj (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object)
  182. {
  183. struct instance *o = NCDObject_DataPtr(obj);
  184. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  185. }
  186. static int caller_obj_func_getobj_with_caller_target (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object)
  187. {
  188. struct instance_with_caller_target *o_ch = NCDObject_DataPtr(obj);
  189. ASSERT(o_ch->num_names > 0)
  190. NCD_string_id_t *names = CallNames_GetNames(o_ch);
  191. NCDObject object;
  192. if (!NCDModuleInst_Backend_GetObj(o_ch->base.i, names[0], &object)) {
  193. return 0;
  194. }
  195. NCDObject obj2;
  196. if (!NCDObject_ResolveObjExprCompact(&object, names + 1, o_ch->num_names - 1, &obj2)) {
  197. return 0;
  198. }
  199. if (name == NCD_STRING_EMPTY) {
  200. *out_object = obj2;
  201. return 1;
  202. }
  203. return NCDObject_GetObj(&obj2, name, out_object);
  204. }
  205. static void func_new_templ (void *vo, NCDModuleInst *i, NCDValRef template_name, NCDValRef args, int embed)
  206. {
  207. ASSERT(NCDVal_IsInvalid(template_name) || NCDVal_IsString(template_name))
  208. ASSERT(NCDVal_IsInvalid(args) || NCDVal_IsList(args))
  209. ASSERT(embed == !!embed)
  210. struct instance *o = vo;
  211. o->i = i;
  212. if (NCDVal_IsInvalid(template_name) || ncd_is_none(template_name)) {
  213. // signal up
  214. NCDModuleInst_Backend_Up(o->i);
  215. // set state none
  216. o->state = STATE_NONE;
  217. } else {
  218. // create process
  219. if (!NCDModuleProcess_InitValue(&o->process, o->i, template_name, args, process_handler_event)) {
  220. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  221. goto fail0;
  222. }
  223. // set special functions
  224. if (embed) {
  225. NCDModuleProcess_SetSpecialFuncs(&o->process, process_func_getspecialobj_embed);
  226. } else {
  227. NCDModuleProcess_SetSpecialFuncs(&o->process, process_func_getspecialobj_noembed);
  228. }
  229. // set state working
  230. o->state = STATE_WORKING;
  231. }
  232. return;
  233. fail0:
  234. NCDModuleInst_Backend_DeadError(i);
  235. }
  236. static void instance_free (struct instance *o)
  237. {
  238. // free process
  239. if (o->state != STATE_NONE) {
  240. NCDModuleProcess_Free(&o->process);
  241. }
  242. NCDModuleInst_Backend_Dead(o->i);
  243. }
  244. static void func_new_call (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  245. {
  246. NCDValRef template_arg;
  247. NCDValRef args_arg;
  248. if (!NCDVal_ListRead(params->args, 2, &template_arg, &args_arg)) {
  249. ModuleLog(i, BLOG_ERROR, "wrong arity");
  250. goto fail0;
  251. }
  252. if (!NCDVal_IsString(template_arg) || !NCDVal_IsList(args_arg)) {
  253. ModuleLog(i, BLOG_ERROR, "wrong type");
  254. goto fail0;
  255. }
  256. func_new_templ(vo, i, template_arg, args_arg, 0);
  257. return;
  258. fail0:
  259. NCDModuleInst_Backend_DeadError(i);
  260. }
  261. static void func_new_call_with_caller_target (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  262. {
  263. struct instance *o = vo;
  264. struct instance_with_caller_target *o_ct = vo;
  265. o->i = i;
  266. NCDValRef template_arg;
  267. NCDValRef args_arg;
  268. NCDValRef caller_target_arg;
  269. if (!NCDVal_ListRead(params->args, 3, &template_arg, &args_arg, &caller_target_arg)) {
  270. ModuleLog(i, BLOG_ERROR, "wrong arity");
  271. goto fail0;
  272. }
  273. if (!NCDVal_IsString(template_arg) || !NCDVal_IsList(args_arg) || !NCDVal_IsString(caller_target_arg)) {
  274. ModuleLog(i, BLOG_ERROR, "wrong type");
  275. goto fail0;
  276. }
  277. if (!CallNames_InitNames(o_ct, i->params->iparams->string_index, NCDVal_StringData(caller_target_arg), NCDVal_StringLength(caller_target_arg))) {
  278. ModuleLog(i, BLOG_ERROR, "CallerNames_InitNames failed");
  279. goto fail0;
  280. }
  281. if (ncd_is_none(template_arg)) {
  282. // signal up
  283. NCDModuleInst_Backend_Up(i);
  284. // set state none
  285. o->state = STATE_NONE;
  286. } else {
  287. // create process
  288. if (!NCDModuleProcess_InitValue(&o->process, i, template_arg, args_arg, process_handler_event)) {
  289. ModuleLog(i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  290. goto fail1;
  291. }
  292. // set special functions
  293. NCDModuleProcess_SetSpecialFuncs(&o->process, process_func_getspecialobj_with_caller_target);
  294. // set state working
  295. o->state = STATE_WORKING;
  296. }
  297. return;
  298. fail1:
  299. CallNames_FreeNames(o_ct);
  300. fail0:
  301. NCDModuleInst_Backend_DeadError(i);
  302. }
  303. static void func_new_embcall_multif (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  304. {
  305. NCDValRef args = params->args;
  306. NCDValRef template_value = NCDVal_NewInvalid();
  307. size_t count = NCDVal_ListCount(args);
  308. size_t j = 0;
  309. while (j < count) {
  310. NCDValRef arg = NCDVal_ListGet(args, j);
  311. if (j == count - 1) {
  312. if (!NCDVal_IsString(arg)) {
  313. ModuleLog(i, BLOG_ERROR, "bad arguments");
  314. goto fail0;
  315. }
  316. template_value = arg;
  317. break;
  318. }
  319. NCDValRef arg2 = NCDVal_ListGet(args, j + 1);
  320. if (!NCDVal_IsString(arg) || !NCDVal_IsString(arg2)) {
  321. ModuleLog(i, BLOG_ERROR, "bad arguments");
  322. goto fail0;
  323. }
  324. if (ncd_read_boolean(arg)) {
  325. template_value = arg2;
  326. break;
  327. }
  328. j += 2;
  329. }
  330. func_new_templ(vo, i, template_value, NCDVal_NewInvalid(), 1);
  331. return;
  332. fail0:
  333. NCDModuleInst_Backend_DeadError(i);
  334. }
  335. static void func_die (void *vo)
  336. {
  337. struct instance *o = vo;
  338. ASSERT(o->state != STATE_TERMINATING)
  339. // if none, die now
  340. if (o->state == STATE_NONE) {
  341. instance_free(o);
  342. return;
  343. }
  344. // request process to terminate
  345. NCDModuleProcess_Terminate(&o->process);
  346. // set state terminating
  347. o->state = STATE_TERMINATING;
  348. }
  349. static void func_die_with_caller_target (void *vo)
  350. {
  351. struct instance_with_caller_target *o_ct = vo;
  352. CallNames_FreeNames(o_ct);
  353. func_die(vo);
  354. }
  355. static void func_clean (void *vo)
  356. {
  357. struct instance *o = vo;
  358. if (o->state != STATE_WAITING) {
  359. return;
  360. }
  361. // allow process to continue
  362. NCDModuleProcess_Continue(&o->process);
  363. // set state working
  364. o->state = STATE_WORKING;
  365. }
  366. static int func_getobj (void *vo, NCD_string_id_t name, NCDObject *out_object)
  367. {
  368. struct instance *o = vo;
  369. if (o->state == STATE_NONE) {
  370. return 0;
  371. }
  372. return NCDModuleProcess_GetObj(&o->process, name, out_object);
  373. }
  374. static struct NCDModule modules[] = {
  375. {
  376. .type = "call",
  377. .func_new2 = func_new_call,
  378. .func_die = func_die,
  379. .func_clean = func_clean,
  380. .func_getobj = func_getobj,
  381. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  382. .alloc_size = sizeof(struct instance)
  383. }, {
  384. .type = "call_with_caller_target",
  385. .func_new2 = func_new_call_with_caller_target,
  386. .func_die = func_die_with_caller_target,
  387. .func_clean = func_clean,
  388. .func_getobj = func_getobj,
  389. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  390. .alloc_size = sizeof(struct instance_with_caller_target)
  391. }, {
  392. .type = "embcall2_multif",
  393. .func_new2 = func_new_embcall_multif,
  394. .func_die = func_die,
  395. .func_clean = func_clean,
  396. .func_getobj = func_getobj,
  397. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  398. .alloc_size = sizeof(struct instance)
  399. }, {
  400. .type = NULL
  401. }
  402. };
  403. const struct NCDModuleGroup ncdmodule_call2 = {
  404. .modules = modules
  405. };