call2.c 20 KB

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