call2.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. * embcall(string template)
  84. *
  85. * Description:
  86. * Like call, but makes its own scope directly available in the called
  87. * template process, instead of via _caller. Also, doesn not provide any
  88. * arguments to the template process.
  89. *
  90. *
  91. * Synopsis:
  92. * inline_code(string template)
  93. * inline_code::call(list args)
  94. *
  95. * Description:
  96. * The inline_code() acts as a proxy object for calling the specified template.
  97. * The inline_code::call calls the template of the corresponding inline_code
  98. * instance, in a manner similar to a simple "call". The called template will
  99. * have access to the following resources:
  100. * - The arguments will be available in the usual fashion.
  101. * - The scope of the inline_code::call instance will be available as "_caller".
  102. * - The scope of the inline_code instance will be directly available.
  103. * - The scope of the inline_code instance will also be available as "_scope".
  104. * This is useful to access shadowed names, e.g. "_caller" and the arguments
  105. * stuff (_argN, _args).
  106. */
  107. #include <stdlib.h>
  108. #include <string.h>
  109. #include <misc/debug.h>
  110. #include <misc/offset.h>
  111. #include <structure/LinkedList0.h>
  112. #include <ncd/extra/NCDFastNames.h>
  113. #include <ncd/module_common.h>
  114. #include <generated/blog_channel_ncd_call2.h>
  115. #define STATE_WORKING 1
  116. #define STATE_UP 2
  117. #define STATE_WAITING 3
  118. #define STATE_TERMINATING 4
  119. #define STATE_NONE 5
  120. struct instance;
  121. typedef void (*call_extra_free_cb) (struct instance *o);
  122. struct instance {
  123. NCDModuleInst *i;
  124. call_extra_free_cb extra_free_cb;
  125. NCDModuleProcess process;
  126. int state;
  127. };
  128. struct instance_with_caller_target {
  129. struct instance base;
  130. NCDFastNames names;
  131. };
  132. struct inline_code {
  133. NCDModuleInst *i;
  134. NCDValRef template_name;
  135. LinkedList0 calls_list;
  136. };
  137. struct inline_code_call {
  138. struct instance base;
  139. struct inline_code *ic;
  140. LinkedList0Node ic_node;
  141. };
  142. static void process_handler_event (NCDModuleProcess *process, int event);
  143. static int process_func_getspecialobj_embed (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object);
  144. static int process_func_getspecialobj_noembed (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object);
  145. static int process_func_getspecialobj_with_caller_target (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object);
  146. static int caller_obj_func_getobj (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object);
  147. static int caller_obj_func_getobj_with_caller_target (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object);
  148. 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);
  149. static void instance_free (struct instance *o);
  150. static void call_with_caller_target_extra_free (struct instance *bo);
  151. static void inline_code_extra_free (struct instance *bo);
  152. static int inline_code_call_process_getspecialobj (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object);
  153. static int inline_code_scope_obj_getobj (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object);
  154. static void process_handler_event (NCDModuleProcess *process, int event)
  155. {
  156. struct instance *o = UPPER_OBJECT(process, struct instance, process);
  157. switch (event) {
  158. case NCDMODULEPROCESS_EVENT_UP: {
  159. ASSERT(o->state == STATE_WORKING)
  160. // signal up
  161. NCDModuleInst_Backend_Up(o->i);
  162. // set state up
  163. o->state = STATE_UP;
  164. } break;
  165. case NCDMODULEPROCESS_EVENT_DOWN: {
  166. ASSERT(o->state == STATE_UP)
  167. // signal down
  168. NCDModuleInst_Backend_Down(o->i);
  169. // set state waiting
  170. o->state = STATE_WAITING;
  171. } break;
  172. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  173. ASSERT(o->state == STATE_TERMINATING)
  174. // die finally
  175. instance_free(o);
  176. return;
  177. } break;
  178. default: ASSERT(0);
  179. }
  180. }
  181. static int process_func_getspecialobj_embed (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object)
  182. {
  183. struct instance *o = UPPER_OBJECT(process, struct instance, process);
  184. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  185. }
  186. static int process_func_getspecialobj_noembed (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object)
  187. {
  188. struct instance *o = UPPER_OBJECT(process, struct instance, process);
  189. if (name == NCD_STRING_CALLER) {
  190. *out_object = NCDObject_Build(-1, o, NCDObject_no_getvar, caller_obj_func_getobj);
  191. return 1;
  192. }
  193. return 0;
  194. }
  195. static int process_func_getspecialobj_with_caller_target (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object)
  196. {
  197. struct instance *o = UPPER_OBJECT(process, struct instance, process);
  198. if (name == NCD_STRING_CALLER) {
  199. *out_object = NCDObject_Build(-1, o, NCDObject_no_getvar, caller_obj_func_getobj_with_caller_target);
  200. return 1;
  201. }
  202. return 0;
  203. }
  204. static int caller_obj_func_getobj (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object)
  205. {
  206. struct instance *o = NCDObject_DataPtr(obj);
  207. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  208. }
  209. static int caller_obj_func_getobj_with_caller_target (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object)
  210. {
  211. struct instance_with_caller_target *o_ch = NCDObject_DataPtr(obj);
  212. NCD_string_id_t *names = NCDFastNames_GetNames(&o_ch->names);
  213. NCDObject object;
  214. if (!NCDModuleInst_Backend_GetObj(o_ch->base.i, names[0], &object)) {
  215. return 0;
  216. }
  217. NCDObject obj2;
  218. if (!NCDObject_ResolveObjExprCompact(&object, names + 1, NCDFastNames_GetNumNames(&o_ch->names) - 1, &obj2)) {
  219. return 0;
  220. }
  221. if (name == NCD_STRING_EMPTY) {
  222. *out_object = obj2;
  223. return 1;
  224. }
  225. return NCDObject_GetObj(&obj2, name, out_object);
  226. }
  227. 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)
  228. {
  229. ASSERT(NCDVal_IsInvalid(template_name) || NCDVal_IsString(template_name))
  230. ASSERT(NCDVal_IsInvalid(args) || NCDVal_IsList(args))
  231. ASSERT(func_getspecialobj)
  232. struct instance *o = vo;
  233. o->i = i;
  234. o->extra_free_cb = extra_free_cb;
  235. if (NCDVal_IsInvalid(template_name) || ncd_is_none(template_name)) {
  236. // signal up
  237. NCDModuleInst_Backend_Up(o->i);
  238. // set state none
  239. o->state = STATE_NONE;
  240. } else {
  241. // create process
  242. if (!NCDModuleProcess_InitValue(&o->process, o->i, template_name, args, process_handler_event)) {
  243. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  244. goto fail1;
  245. }
  246. // set special functions
  247. NCDModuleProcess_SetSpecialFuncs(&o->process, func_getspecialobj);
  248. // set state working
  249. o->state = STATE_WORKING;
  250. }
  251. return;
  252. fail1:
  253. if (o->extra_free_cb) {
  254. o->extra_free_cb(o);
  255. }
  256. NCDModuleInst_Backend_DeadError(i);
  257. }
  258. static void instance_free (struct instance *o)
  259. {
  260. if (o->extra_free_cb) {
  261. o->extra_free_cb(o);
  262. }
  263. // free process
  264. if (o->state != STATE_NONE) {
  265. NCDModuleProcess_Free(&o->process);
  266. }
  267. NCDModuleInst_Backend_Dead(o->i);
  268. }
  269. static void func_new_call (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  270. {
  271. NCDValRef template_arg;
  272. NCDValRef args_arg;
  273. if (!NCDVal_ListRead(params->args, 2, &template_arg, &args_arg)) {
  274. ModuleLog(i, BLOG_ERROR, "wrong arity");
  275. goto fail0;
  276. }
  277. if (!NCDVal_IsString(template_arg) || !NCDVal_IsList(args_arg)) {
  278. ModuleLog(i, BLOG_ERROR, "wrong type");
  279. goto fail0;
  280. }
  281. func_new_templ(vo, i, template_arg, args_arg, process_func_getspecialobj_noembed, NULL);
  282. return;
  283. fail0:
  284. NCDModuleInst_Backend_DeadError(i);
  285. }
  286. static void func_new_call_with_caller_target (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  287. {
  288. NCDValRef template_arg;
  289. NCDValRef args_arg;
  290. NCDValRef caller_target_arg;
  291. if (!NCDVal_ListRead(params->args, 3, &template_arg, &args_arg, &caller_target_arg)) {
  292. ModuleLog(i, BLOG_ERROR, "wrong arity");
  293. goto fail0;
  294. }
  295. if (!NCDVal_IsString(template_arg) || !NCDVal_IsList(args_arg) || !NCDVal_IsString(caller_target_arg)) {
  296. ModuleLog(i, BLOG_ERROR, "wrong type");
  297. goto fail0;
  298. }
  299. struct instance_with_caller_target *o = vo;
  300. int res = NCDFastNames_Init(&o->names, i->params->iparams->string_index, NCDVal_StringMemRef(caller_target_arg));
  301. if (!res) {
  302. ModuleLog(i, BLOG_ERROR, "NCDFastNames_Init failed");
  303. goto fail0;
  304. }
  305. func_new_templ(vo, i, template_arg, args_arg, process_func_getspecialobj_with_caller_target, call_with_caller_target_extra_free);
  306. return;
  307. fail0:
  308. NCDModuleInst_Backend_DeadError(i);
  309. }
  310. static void call_with_caller_target_extra_free (struct instance *bo)
  311. {
  312. struct instance_with_caller_target *o = (void *)bo;
  313. NCDFastNames_Free(&o->names);
  314. }
  315. static void func_new_embcall (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  316. {
  317. NCDValRef template_arg;
  318. if (!NCDVal_ListRead(params->args, 1, &template_arg)) {
  319. ModuleLog(i, BLOG_ERROR, "wrong arity");
  320. goto fail0;
  321. }
  322. if (!NCDVal_IsString(template_arg)) {
  323. ModuleLog(i, BLOG_ERROR, "wrong type");
  324. goto fail0;
  325. }
  326. func_new_templ(vo, i, template_arg, NCDVal_NewInvalid(), process_func_getspecialobj_embed, NULL);
  327. return;
  328. fail0:
  329. NCDModuleInst_Backend_DeadError(i);
  330. }
  331. static void func_die (void *vo)
  332. {
  333. struct instance *o = vo;
  334. ASSERT(o->state != STATE_TERMINATING)
  335. // if none, die now
  336. if (o->state == STATE_NONE) {
  337. instance_free(o);
  338. return;
  339. }
  340. // request process to terminate
  341. NCDModuleProcess_Terminate(&o->process);
  342. // set state terminating
  343. o->state = STATE_TERMINATING;
  344. }
  345. static void func_clean (void *vo)
  346. {
  347. struct instance *o = vo;
  348. if (o->state != STATE_WAITING) {
  349. return;
  350. }
  351. // allow process to continue
  352. NCDModuleProcess_Continue(&o->process);
  353. // set state working
  354. o->state = STATE_WORKING;
  355. }
  356. static int func_getobj (void *vo, NCD_string_id_t name, NCDObject *out_object)
  357. {
  358. struct instance *o = vo;
  359. if (o->state == STATE_NONE) {
  360. return 0;
  361. }
  362. return NCDModuleProcess_GetObj(&o->process, name, out_object);
  363. }
  364. static void inline_code_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  365. {
  366. NCDValRef template_arg;
  367. if (!NCDVal_ListRead(params->args, 1, &template_arg)) {
  368. ModuleLog(i, BLOG_ERROR, "wrong arity");
  369. goto fail0;
  370. }
  371. if (!NCDVal_IsString(template_arg)) {
  372. ModuleLog(i, BLOG_ERROR, "wrong type");
  373. goto fail0;
  374. }
  375. struct inline_code *o = vo;
  376. o->i = i;
  377. o->template_name = template_arg;
  378. LinkedList0_Init(&o->calls_list);
  379. NCDModuleInst_Backend_Up(i);
  380. return;
  381. fail0:
  382. NCDModuleInst_Backend_DeadError(i);
  383. }
  384. static void inline_code_die (void *vo)
  385. {
  386. struct inline_code *o = vo;
  387. for (LinkedList0Node *ln = LinkedList0_GetFirst(&o->calls_list); ln; ln = LinkedList0Node_Next(ln)) {
  388. struct inline_code_call *call = UPPER_OBJECT(ln, struct inline_code_call, ic_node);
  389. ASSERT(call->ic == o)
  390. call->ic = NULL;
  391. }
  392. NCDModuleInst_Backend_Dead(o->i);
  393. }
  394. static void inline_code_call_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  395. {
  396. struct inline_code_call *o = vo;
  397. o->ic = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  398. LinkedList0_Prepend(&o->ic->calls_list, &o->ic_node);
  399. func_new_templ(vo, i, o->ic->template_name, params->args, inline_code_call_process_getspecialobj, inline_code_extra_free);
  400. }
  401. static void inline_code_extra_free (struct instance *bo)
  402. {
  403. struct inline_code_call *o = (void *)bo;
  404. if (o->ic) {
  405. LinkedList0_Remove(&o->ic->calls_list, &o->ic_node);
  406. }
  407. }
  408. static int inline_code_call_process_getspecialobj (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object)
  409. {
  410. struct inline_code_call *o = UPPER_OBJECT(process, struct inline_code_call, base.process);
  411. if (name == NCD_STRING_CALLER) {
  412. *out_object = NCDObject_Build(-1, o, NCDObject_no_getvar, caller_obj_func_getobj);
  413. return 1;
  414. }
  415. if (!o->ic) {
  416. ModuleLog(o->base.i, BLOG_ERROR, "inline_code object is gone");
  417. return 0;
  418. }
  419. if (name == NCD_STRING_SCOPE) {
  420. *out_object = NCDObject_Build(-1, o->ic, NCDObject_no_getvar, inline_code_scope_obj_getobj);
  421. return 1;
  422. }
  423. return NCDModuleInst_Backend_GetObj(o->ic->i, name, out_object);
  424. }
  425. static int inline_code_scope_obj_getobj (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object)
  426. {
  427. struct inline_code *ic = NCDObject_DataPtr(obj);
  428. return NCDModuleInst_Backend_GetObj(ic->i, name, out_object);
  429. }
  430. static struct NCDModule modules[] = {
  431. {
  432. .type = "call",
  433. .func_new2 = func_new_call,
  434. .func_die = func_die,
  435. .func_clean = func_clean,
  436. .func_getobj = func_getobj,
  437. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  438. .alloc_size = sizeof(struct instance)
  439. }, {
  440. .type = "call_with_caller_target",
  441. .func_new2 = func_new_call_with_caller_target,
  442. .func_die = func_die,
  443. .func_clean = func_clean,
  444. .func_getobj = func_getobj,
  445. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  446. .alloc_size = sizeof(struct instance_with_caller_target)
  447. }, {
  448. .type = "embcall",
  449. .func_new2 = func_new_embcall,
  450. .func_die = func_die,
  451. .func_clean = func_clean,
  452. .func_getobj = func_getobj,
  453. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  454. .alloc_size = sizeof(struct instance)
  455. }, {
  456. .type = "inline_code",
  457. .func_new2 = inline_code_new,
  458. .func_die = inline_code_die,
  459. .alloc_size = sizeof(struct inline_code)
  460. }, {
  461. .type = "inline_code::call",
  462. .func_new2 = inline_code_call_new,
  463. .func_die = func_die,
  464. .func_clean = func_clean,
  465. .func_getobj = func_getobj,
  466. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  467. .alloc_size = sizeof(struct inline_code_call)
  468. }, {
  469. .type = NULL
  470. }
  471. };
  472. const struct NCDModuleGroup ncdmodule_call2 = {
  473. .modules = modules
  474. };