NCDInterpreter.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  1. /**
  2. * @file NCDInterpreter.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. #include <stdint.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include <limits.h>
  33. #include <stdarg.h>
  34. #include <misc/offset.h>
  35. #include <misc/balloc.h>
  36. #include <misc/expstring.h>
  37. #include <base/BLog.h>
  38. #include <ncd/NCDSugar.h>
  39. #include <ncd/modules/modules.h>
  40. #include "NCDInterpreter.h"
  41. #include <generated/blog_channel_ncd.h>
  42. #define SSTATE_CHILD 0
  43. #define SSTATE_ADULT 1
  44. #define SSTATE_DYING 2
  45. #define SSTATE_FORGOTTEN 3
  46. #define PSTATE_WORKING 0
  47. #define PSTATE_UP 1
  48. #define PSTATE_WAITING 2
  49. #define PSTATE_TERMINATING 3
  50. struct statement {
  51. NCDModuleInst inst;
  52. NCDValMem args_mem;
  53. int mem_size;
  54. int i;
  55. };
  56. struct process {
  57. NCDInterpreter *interp;
  58. BReactor *reactor;
  59. NCDInterpProcess *iprocess;
  60. NCDModuleProcess *module_process;
  61. BSmallTimer wait_timer;
  62. BSmallPending work_job;
  63. LinkedList1Node list_node; // node in processes
  64. int ap;
  65. int fp;
  66. int num_statements;
  67. unsigned int error:1;
  68. unsigned int have_alloc:1;
  69. #ifndef NDEBUG
  70. int state;
  71. #endif
  72. struct statement statements[];
  73. };
  74. static void start_terminate (NCDInterpreter *interp, int exit_code);
  75. static char * implode_id_strings (NCDInterpreter *interp, const NCD_string_id_t *names, size_t num_names, char del);
  76. static void clear_process_cache (NCDInterpreter *interp);
  77. static struct process * process_allocate (NCDInterpreter *interp, NCDInterpProcess *iprocess);
  78. static void process_release (struct process *p, int no_push);
  79. static void process_assert_statements_cleared (struct process *p);
  80. static int process_new (NCDInterpreter *interp, NCDInterpProcess *iprocess, NCDModuleProcess *module_process);
  81. static void process_free (struct process *p, NCDModuleProcess **out_mp);
  82. static void process_set_state (struct process *p, int state);
  83. static void process_start_terminating (struct process *p);
  84. static int process_have_child (struct process *p);
  85. static void process_assert_pointers (struct process *p);
  86. static void process_logfunc (struct process *p);
  87. static void process_log (struct process *p, int level, const char *fmt, ...);
  88. static void process_work_job_handler_working (struct process *p);
  89. static void process_work_job_handler_up (struct process *p);
  90. static void process_work_job_handler_waiting (struct process *p);
  91. static void process_work_job_handler_terminating (struct process *p);
  92. static int eval_func_eval_var (void *user, NCD_string_id_t const *varnames, size_t num_names, NCDValMem *mem, NCDValRef *out);
  93. static int eval_func_eval_call (void *user, NCD_string_id_t func_name_id, NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out);
  94. static void process_advance (struct process *p);
  95. static void process_wait_timer_handler (BSmallTimer *timer);
  96. static int process_find_object (struct process *p, int pos, NCD_string_id_t name, NCDObject *out_object);
  97. static int process_resolve_object_expr (struct process *p, int pos, const NCD_string_id_t *names, size_t num_names, NCDObject *out_object);
  98. static int process_resolve_variable_expr (struct process *p, int pos, const NCD_string_id_t *names, size_t num_names, NCDValMem *mem, NCDValRef *out_value);
  99. static void statement_logfunc (struct statement *ps);
  100. static void statement_log (struct statement *ps, int level, const char *fmt, ...);
  101. static struct process * statement_process (struct statement *ps);
  102. static int statement_mem_is_allocated (struct statement *ps);
  103. static int statement_mem_size (struct statement *ps);
  104. static int statement_allocate_memory (struct statement *ps, int alloc_size);
  105. static void statement_instance_func_event (NCDModuleInst *inst, int event);
  106. static int statement_instance_func_getobj (NCDModuleInst *inst, NCD_string_id_t objname, NCDObject *out_object);
  107. static int statement_instance_func_initprocess (void *vinterp, NCDModuleProcess *mp, NCD_string_id_t template_name);
  108. static void statement_instance_logfunc (NCDModuleInst *inst);
  109. static void statement_instance_func_interp_exit (void *vinterp, int exit_code);
  110. static int statement_instance_func_interp_getargs (void *vinterp, NCDValMem *mem, NCDValRef *out_value);
  111. static btime_t statement_instance_func_interp_getretrytime (void *vinterp);
  112. static int statement_instance_func_interp_loadgroup (void *vinterp, const struct NCDModuleGroup *group);
  113. static void process_moduleprocess_func_event (struct process *p, int event);
  114. static int process_moduleprocess_func_getobj (struct process *p, NCD_string_id_t name, NCDObject *out_object);
  115. #define STATEMENT_LOG(ps, channel, ...) if (BLog_WouldLog(BLOG_CURRENT_CHANNEL, channel)) statement_log(ps, channel, __VA_ARGS__)
  116. int NCDInterpreter_Init (NCDInterpreter *o, NCDProgram program, struct NCDInterpreter_params params)
  117. {
  118. ASSERT(!NCDProgram_ContainsElemType(&program, NCDPROGRAMELEM_INCLUDE));
  119. ASSERT(!NCDProgram_ContainsElemType(&program, NCDPROGRAMELEM_INCLUDE_GUARD));
  120. ASSERT(params.handler_finished);
  121. ASSERT(params.num_extra_args >= 0);
  122. ASSERT(params.reactor);
  123. #ifndef BADVPN_NO_PROCESS
  124. ASSERT(params.manager);
  125. #endif
  126. #ifndef BADVPN_NO_UDEV
  127. ASSERT(params.umanager);
  128. #endif
  129. #ifndef BADVPN_NO_RANDOM
  130. ASSERT(params.random2);
  131. #endif
  132. // set params
  133. o->params = params;
  134. // set not terminating
  135. o->terminating = 0;
  136. // set program
  137. o->program = program;
  138. // init string index
  139. if (!NCDStringIndex_Init(&o->string_index)) {
  140. BLog(BLOG_ERROR, "NCDStringIndex_Init failed");
  141. goto fail0;
  142. }
  143. // init module index
  144. if (!NCDModuleIndex_Init(&o->mindex, &o->string_index)) {
  145. BLog(BLOG_ERROR, "NCDModuleIndex_Init failed");
  146. goto fail2;
  147. }
  148. // init pointers to global resources in out struct NCDModuleInst_iparams.
  149. // Don't initialize any callback at this point as these must not be called
  150. // from globalinit functions of modules.
  151. o->module_iparams.reactor = params.reactor;
  152. #ifndef BADVPN_NO_PROCESS
  153. o->module_iparams.manager = params.manager;
  154. #endif
  155. #ifndef BADVPN_NO_UDEV
  156. o->module_iparams.umanager = params.umanager;
  157. #endif
  158. #ifndef BADVPN_NO_RANDOM
  159. o->module_iparams.random2 = params.random2;
  160. #endif
  161. o->module_iparams.string_index = &o->string_index;
  162. // add module groups to index and allocate string id's for base_type's
  163. for (const struct NCDModuleGroup **g = ncd_modules; *g; g++) {
  164. if (!NCDModuleIndex_AddGroup(&o->mindex, *g, &o->module_iparams, &o->string_index)) {
  165. BLog(BLOG_ERROR, "NCDModuleIndex_AddGroup failed");
  166. goto fail3;
  167. }
  168. }
  169. // desugar
  170. if (!NCDSugar_Desugar(&o->program)) {
  171. BLog(BLOG_ERROR, "NCDSugar_Desugar failed");
  172. goto fail3;
  173. }
  174. // init expression evaluator
  175. if (!NCDEvaluator_Init(&o->evaluator, &o->string_index)) {
  176. BLog(BLOG_ERROR, "NCDEvaluator_Init failed");
  177. goto fail3;
  178. }
  179. // init interp program
  180. if (!NCDInterpProg_Init(&o->iprogram, &o->program, &o->string_index, &o->evaluator, &o->mindex)) {
  181. BLog(BLOG_ERROR, "NCDInterpProg_Init failed");
  182. goto fail5;
  183. }
  184. // init the rest of the module parameters structures
  185. o->module_params.func_event = statement_instance_func_event;
  186. o->module_params.func_getobj = statement_instance_func_getobj;
  187. o->module_params.logfunc = (BLog_logfunc)statement_instance_logfunc;
  188. o->module_params.iparams = &o->module_iparams;
  189. o->module_iparams.user = o;
  190. o->module_iparams.func_initprocess = statement_instance_func_initprocess;
  191. o->module_iparams.func_interp_exit = statement_instance_func_interp_exit;
  192. o->module_iparams.func_interp_getargs = statement_instance_func_interp_getargs;
  193. o->module_iparams.func_interp_getretrytime = statement_instance_func_interp_getretrytime;
  194. o->module_iparams.func_loadgroup = statement_instance_func_interp_loadgroup;
  195. // init processes list
  196. LinkedList1_Init(&o->processes);
  197. // init processes
  198. for (NCDProgramElem *elem = NCDProgram_FirstElem(&o->program); elem; elem = NCDProgram_NextElem(&o->program, elem)) {
  199. ASSERT(NCDProgramElem_Type(elem) == NCDPROGRAMELEM_PROCESS)
  200. NCDProcess *p = NCDProgramElem_Process(elem);
  201. if (NCDProcess_IsTemplate(p)) {
  202. continue;
  203. }
  204. // get string id for process name
  205. NCD_string_id_t name_id = NCDStringIndex_Lookup(&o->string_index, NCDProcess_Name(p));
  206. ASSERT(name_id >= 0)
  207. // find iprocess
  208. NCDInterpProcess *iprocess = NCDInterpProg_FindProcess(&o->iprogram, name_id);
  209. ASSERT(iprocess)
  210. if (!process_new(o, iprocess, NULL)) {
  211. BLog(BLOG_ERROR, "failed to initialize process, exiting");
  212. goto fail7;
  213. }
  214. }
  215. DebugObject_Init(&o->d_obj);
  216. return 1;
  217. fail7:;
  218. // free processes
  219. LinkedList1Node *ln;
  220. while (ln = LinkedList1_GetFirst(&o->processes)) {
  221. struct process *p = UPPER_OBJECT(ln, struct process, list_node);
  222. BSmallPending_Unset(&p->work_job, BReactor_PendingGroup(o->params.reactor));
  223. NCDModuleProcess *mp;
  224. process_free(p, &mp);
  225. ASSERT(!mp)
  226. }
  227. // clear process cache (process_free() above may push to cache)
  228. clear_process_cache(o);
  229. // free interp program
  230. NCDInterpProg_Free(&o->iprogram);
  231. fail5:
  232. // free placeholder database
  233. NCDEvaluator_Free(&o->evaluator);
  234. fail3:
  235. // free module index
  236. NCDModuleIndex_Free(&o->mindex);
  237. fail2:
  238. // free string index
  239. NCDStringIndex_Free(&o->string_index);
  240. fail0:
  241. // free program AST
  242. NCDProgram_Free(&o->program);
  243. return 0;
  244. }
  245. void NCDInterpreter_Free (NCDInterpreter *o)
  246. {
  247. DebugObject_Free(&o->d_obj);
  248. // any process that exists must be completely uninitialized
  249. // free processes
  250. LinkedList1Node *ln;
  251. while (ln = LinkedList1_GetFirst(&o->processes)) {
  252. struct process *p = UPPER_OBJECT(ln, struct process, list_node);
  253. BSmallPending_Unset(&p->work_job, BReactor_PendingGroup(o->params.reactor));
  254. NCDModuleProcess *mp;
  255. process_free(p, &mp);
  256. ASSERT(!mp)
  257. }
  258. // clear process cache
  259. clear_process_cache(o);
  260. // free interp program
  261. NCDInterpProg_Free(&o->iprogram);
  262. // free placeholder database
  263. NCDEvaluator_Free(&o->evaluator);
  264. // free module index
  265. NCDModuleIndex_Free(&o->mindex);
  266. // free string index
  267. NCDStringIndex_Free(&o->string_index);
  268. // free program AST
  269. NCDProgram_Free(&o->program);
  270. }
  271. void NCDInterpreter_RequestShutdown (NCDInterpreter *o, int exit_code)
  272. {
  273. DebugObject_Access(&o->d_obj);
  274. start_terminate(o, exit_code);
  275. }
  276. void start_terminate (NCDInterpreter *interp, int exit_code)
  277. {
  278. // remember exit code
  279. interp->main_exit_code = exit_code;
  280. // if we're already terminating, there's nothing to do
  281. if (interp->terminating) {
  282. return;
  283. }
  284. // set the terminating flag
  285. interp->terminating = 1;
  286. // if there are no processes, we're done
  287. if (LinkedList1_IsEmpty(&interp->processes)) {
  288. interp->params.handler_finished(interp->params.user, interp->main_exit_code);
  289. return;
  290. }
  291. // start terminating non-template processes
  292. for (LinkedList1Node *ln = LinkedList1_GetFirst(&interp->processes); ln; ln = LinkedList1Node_Next(ln)) {
  293. struct process *p = UPPER_OBJECT(ln, struct process, list_node);
  294. if (p->module_process) {
  295. continue;
  296. }
  297. process_start_terminating(p);
  298. }
  299. }
  300. char * implode_id_strings (NCDInterpreter *interp, const NCD_string_id_t *names, size_t num_names, char del)
  301. {
  302. ExpString str;
  303. if (!ExpString_Init(&str)) {
  304. goto fail0;
  305. }
  306. int is_first = 1;
  307. while (num_names > 0) {
  308. if (!is_first && !ExpString_AppendChar(&str, del)) {
  309. goto fail1;
  310. }
  311. const char *name_str = NCDStringIndex_Value(&interp->string_index, *names);
  312. if (!ExpString_Append(&str, name_str)) {
  313. goto fail1;
  314. }
  315. names++;
  316. num_names--;
  317. is_first = 0;
  318. }
  319. return ExpString_Get(&str);
  320. fail1:
  321. ExpString_Free(&str);
  322. fail0:
  323. return NULL;
  324. }
  325. void clear_process_cache (NCDInterpreter *interp)
  326. {
  327. for (NCDProgramElem *elem = NCDProgram_FirstElem(&interp->program); elem; elem = NCDProgram_NextElem(&interp->program, elem)) {
  328. ASSERT(NCDProgramElem_Type(elem) == NCDPROGRAMELEM_PROCESS)
  329. NCDProcess *ast_p = NCDProgramElem_Process(elem);
  330. NCD_string_id_t name_id = NCDStringIndex_Lookup(&interp->string_index, NCDProcess_Name(ast_p));
  331. NCDInterpProcess *iprocess = NCDInterpProg_FindProcess(&interp->iprogram, name_id);
  332. struct process *p;
  333. while (p = NCDInterpProcess_CachePull(iprocess)) {
  334. process_release(p, 1);
  335. }
  336. }
  337. }
  338. struct process * process_allocate (NCDInterpreter *interp, NCDInterpProcess *iprocess)
  339. {
  340. ASSERT(iprocess)
  341. // try to pull from cache
  342. struct process *p = NCDInterpProcess_CachePull(iprocess);
  343. if (p) {
  344. goto allocated;
  345. }
  346. // get number of statements
  347. int num_statements = NCDInterpProcess_NumStatements(iprocess);
  348. // get size of preallocated memory
  349. int mem_size = NCDInterpProcess_PreallocSize(iprocess);
  350. if (mem_size < 0) {
  351. goto fail0;
  352. }
  353. // start with size of process structure
  354. size_t alloc_size = sizeof(struct process);
  355. // add size of statements array
  356. if (num_statements > SIZE_MAX / sizeof(struct statement)) {
  357. goto fail0;
  358. }
  359. if (!BSizeAdd(&alloc_size, num_statements * sizeof(struct statement))) {
  360. goto fail0;
  361. }
  362. // align for preallocated memory
  363. if (!BSizeAlign(&alloc_size, BMAX_ALIGN)) {
  364. goto fail0;
  365. }
  366. size_t mem_off = alloc_size;
  367. // add size of preallocated memory
  368. if (mem_size > SIZE_MAX || !BSizeAdd(&alloc_size, mem_size)) {
  369. goto fail0;
  370. }
  371. // allocate memory
  372. p = BAlloc(alloc_size);
  373. if (!p) {
  374. goto fail0;
  375. }
  376. // set variables
  377. p->interp = interp;
  378. p->reactor = interp->params.reactor;
  379. p->iprocess = iprocess;
  380. p->ap = 0;
  381. p->fp = 0;
  382. p->num_statements = num_statements;
  383. p->error = 0;
  384. p->have_alloc = 0;
  385. // init statements
  386. char *mem = (char *)p + mem_off;
  387. for (int i = 0; i < num_statements; i++) {
  388. struct statement *ps = &p->statements[i];
  389. ps->i = i;
  390. ps->inst.istate = SSTATE_FORGOTTEN;
  391. ps->mem_size = NCDInterpProcess_StatementPreallocSize(iprocess, i);
  392. ps->inst.mem = mem + NCDInterpProcess_StatementPreallocOffset(iprocess, i);
  393. }
  394. // init timer
  395. BSmallTimer_Init(&p->wait_timer, process_wait_timer_handler);
  396. // init work job
  397. BSmallPending_Init(&p->work_job, BReactor_PendingGroup(p->reactor), NULL, NULL);
  398. allocated:
  399. ASSERT(p->interp == interp)
  400. ASSERT(p->reactor == interp->params.reactor)
  401. ASSERT(p->iprocess == iprocess)
  402. ASSERT(p->ap == 0)
  403. ASSERT(p->fp == 0)
  404. ASSERT(p->num_statements == NCDInterpProcess_NumStatements(iprocess))
  405. ASSERT(p->error == 0)
  406. process_assert_statements_cleared(p);
  407. ASSERT(!BSmallPending_IsSet(&p->work_job))
  408. ASSERT(!BSmallTimer_IsRunning(&p->wait_timer))
  409. return p;
  410. fail0:
  411. BLog(BLOG_ERROR, "failed to allocate memory for process %s", NCDInterpProcess_Name(iprocess));
  412. return NULL;
  413. }
  414. void process_release (struct process *p, int no_push)
  415. {
  416. ASSERT(p->ap == 0)
  417. ASSERT(p->fp == 0)
  418. ASSERT(p->error == 0)
  419. process_assert_statements_cleared(p);
  420. ASSERT(!BSmallPending_IsSet(&p->work_job))
  421. ASSERT(!BSmallTimer_IsRunning(&p->wait_timer))
  422. // try to push to cache
  423. if (!no_push && !p->have_alloc) {
  424. if (NCDInterpProcess_CachePush(p->iprocess, p)) {
  425. return;
  426. }
  427. }
  428. // free work job
  429. BSmallPending_Free(&p->work_job, BReactor_PendingGroup(p->reactor));
  430. // free statement memory
  431. if (p->have_alloc) {
  432. for (int i = 0; i < p->num_statements; i++) {
  433. struct statement *ps = &p->statements[i];
  434. if (statement_mem_is_allocated(ps)) {
  435. free(ps->inst.mem);
  436. }
  437. }
  438. }
  439. // free strucure
  440. BFree(p);
  441. }
  442. void process_assert_statements_cleared (struct process *p)
  443. {
  444. #ifndef NDEBUG
  445. for (int i = 0; i < p->num_statements; i++) {
  446. ASSERT(p->statements[i].i == i)
  447. ASSERT(p->statements[i].inst.istate == SSTATE_FORGOTTEN)
  448. }
  449. #endif
  450. }
  451. int process_new (NCDInterpreter *interp, NCDInterpProcess *iprocess, NCDModuleProcess *module_process)
  452. {
  453. ASSERT(iprocess)
  454. // allocate prepared process struct
  455. struct process *p = process_allocate(interp, iprocess);
  456. if (!p) {
  457. return 0;
  458. }
  459. // set module process pointer
  460. p->module_process = module_process;
  461. // set module process handlers
  462. if (p->module_process) {
  463. NCDModuleProcess_Interp_SetHandlers(p->module_process, p,
  464. (NCDModuleProcess_interp_func_event)process_moduleprocess_func_event,
  465. (NCDModuleProcess_interp_func_getobj)process_moduleprocess_func_getobj);
  466. }
  467. // set state
  468. process_set_state(p, PSTATE_WORKING);
  469. BSmallPending_SetHandler(&p->work_job, (BSmallPending_handler)process_work_job_handler_working, p);
  470. // insert to processes list
  471. LinkedList1_Append(&interp->processes, &p->list_node);
  472. // schedule work
  473. BSmallPending_Set(&p->work_job, BReactor_PendingGroup(p->reactor));
  474. return 1;
  475. }
  476. void process_set_state (struct process *p, int state)
  477. {
  478. #ifndef NDEBUG
  479. p->state = state;
  480. #endif
  481. }
  482. void process_free (struct process *p, NCDModuleProcess **out_mp)
  483. {
  484. ASSERT(p->ap == 0)
  485. ASSERT(p->fp == 0)
  486. ASSERT(out_mp)
  487. ASSERT(!BSmallPending_IsSet(&p->work_job))
  488. // give module process to caller so it can inform the process creator that the process has terminated
  489. *out_mp = p->module_process;
  490. // remove from processes list
  491. LinkedList1_Remove(&p->interp->processes, &p->list_node);
  492. // free timer
  493. BReactor_RemoveSmallTimer(p->reactor, &p->wait_timer);
  494. // clear error
  495. p->error = 0;
  496. process_release(p, 0);
  497. }
  498. void process_start_terminating (struct process *p)
  499. {
  500. // set state terminating
  501. process_set_state(p, PSTATE_TERMINATING);
  502. BSmallPending_SetHandler(&p->work_job, (BSmallPending_handler)process_work_job_handler_terminating, p);
  503. // schedule work
  504. BSmallPending_Set(&p->work_job, BReactor_PendingGroup(p->reactor));
  505. }
  506. int process_have_child (struct process *p)
  507. {
  508. return (p->ap > 0 && p->statements[p->ap - 1].inst.istate == SSTATE_CHILD);
  509. }
  510. void process_assert_pointers (struct process *p)
  511. {
  512. ASSERT(p->ap <= p->num_statements)
  513. ASSERT(p->fp >= p->ap)
  514. ASSERT(p->fp <= p->num_statements)
  515. #ifndef NDEBUG
  516. // check AP
  517. for (int i = 0; i < p->ap; i++) {
  518. if (i == p->ap - 1) {
  519. ASSERT(p->statements[i].inst.istate == SSTATE_ADULT || p->statements[i].inst.istate == SSTATE_CHILD)
  520. } else {
  521. ASSERT(p->statements[i].inst.istate == SSTATE_ADULT)
  522. }
  523. }
  524. // check FP
  525. int fp = p->num_statements;
  526. while (fp > 0 && p->statements[fp - 1].inst.istate == SSTATE_FORGOTTEN) {
  527. fp--;
  528. }
  529. ASSERT(p->fp == fp)
  530. #endif
  531. }
  532. void process_logfunc (struct process *p)
  533. {
  534. BLog_Append("process %s: ", NCDInterpProcess_Name(p->iprocess));
  535. }
  536. void process_log (struct process *p, int level, const char *fmt, ...)
  537. {
  538. va_list vl;
  539. va_start(vl, fmt);
  540. BLog_LogViaFuncVarArg((BLog_logfunc)process_logfunc, p, BLOG_CURRENT_CHANNEL, level, fmt, vl);
  541. va_end(vl);
  542. }
  543. void process_work_job_handler_working (struct process *p)
  544. {
  545. process_assert_pointers(p);
  546. ASSERT(p->state == PSTATE_WORKING)
  547. // cleaning up?
  548. if (p->ap < p->fp) {
  549. // order the last living statement to die, if needed
  550. struct statement *ps = &p->statements[p->fp - 1];
  551. if (ps->inst.istate == SSTATE_DYING) {
  552. return;
  553. }
  554. STATEMENT_LOG(ps, BLOG_INFO, "killing");
  555. // set statement state DYING
  556. ps->inst.istate = SSTATE_DYING;
  557. // order it to die
  558. NCDModuleInst_Die(&ps->inst);
  559. return;
  560. }
  561. // clean?
  562. if (process_have_child(p)) {
  563. ASSERT(p->ap > 0)
  564. ASSERT(p->ap <= p->num_statements)
  565. struct statement *ps = &p->statements[p->ap - 1];
  566. ASSERT(ps->inst.istate == SSTATE_CHILD)
  567. STATEMENT_LOG(ps, BLOG_INFO, "clean");
  568. // report clean
  569. NCDModuleInst_Clean(&ps->inst);
  570. return;
  571. }
  572. // finished?
  573. if (p->ap == p->num_statements) {
  574. process_log(p, BLOG_INFO, "victory");
  575. // set state up
  576. process_set_state(p, PSTATE_UP);
  577. BSmallPending_SetHandler(&p->work_job, (BSmallPending_handler)process_work_job_handler_up, p);
  578. // set module process up
  579. if (p->module_process) {
  580. NCDModuleProcess_Interp_Up(p->module_process);
  581. }
  582. return;
  583. }
  584. // advancing?
  585. struct statement *ps = &p->statements[p->ap];
  586. ASSERT(ps->inst.istate == SSTATE_FORGOTTEN)
  587. if (p->error) {
  588. STATEMENT_LOG(ps, BLOG_INFO, "waiting after error");
  589. // clear error
  590. p->error = 0;
  591. // set wait timer
  592. BReactor_SetSmallTimer(p->reactor, &p->wait_timer, BTIMER_SET_RELATIVE, p->interp->params.retry_time);
  593. } else {
  594. // advance
  595. process_advance(p);
  596. }
  597. }
  598. void process_work_job_handler_up (struct process *p)
  599. {
  600. process_assert_pointers(p);
  601. ASSERT(p->state == PSTATE_UP)
  602. ASSERT(p->ap < p->num_statements || process_have_child(p))
  603. // if we have module process, wait for its permission to continue
  604. if (p->module_process) {
  605. // set state waiting
  606. process_set_state(p, PSTATE_WAITING);
  607. BSmallPending_SetHandler(&p->work_job, (BSmallPending_handler)process_work_job_handler_waiting, p);
  608. // set module process down
  609. NCDModuleProcess_Interp_Down(p->module_process);
  610. return;
  611. }
  612. // set state working
  613. process_set_state(p, PSTATE_WORKING);
  614. BSmallPending_SetHandler(&p->work_job, (BSmallPending_handler)process_work_job_handler_working, p);
  615. // delegate the rest to the working handler
  616. process_work_job_handler_working(p);
  617. }
  618. void process_work_job_handler_waiting (struct process *p)
  619. {
  620. process_assert_pointers(p);
  621. ASSERT(p->state == PSTATE_WAITING)
  622. // do absolutely nothing. Having this no-op handler avoids a branch
  623. // in statement_instance_func_event().
  624. }
  625. void process_work_job_handler_terminating (struct process *p)
  626. {
  627. process_assert_pointers(p);
  628. ASSERT(p->state == PSTATE_TERMINATING)
  629. again:
  630. if (p->fp == 0) {
  631. NCDInterpreter *interp = p->interp;
  632. // free process
  633. NCDModuleProcess *mp;
  634. process_free(p, &mp);
  635. // if program is terminating amd there are no more processes, exit program
  636. if (interp->terminating && LinkedList1_IsEmpty(&interp->processes)) {
  637. ASSERT(!mp)
  638. interp->params.handler_finished(interp->params.user, interp->main_exit_code);
  639. return;
  640. }
  641. // inform the process creator that the process has terminated
  642. if (mp) {
  643. NCDModuleProcess_Interp_Terminated(mp);
  644. return;
  645. }
  646. return;
  647. }
  648. // order the last living statement to die, if needed
  649. struct statement *ps = &p->statements[p->fp - 1];
  650. ASSERT(ps->inst.istate != SSTATE_FORGOTTEN)
  651. if (ps->inst.istate == SSTATE_DYING) {
  652. return;
  653. }
  654. STATEMENT_LOG(ps, BLOG_INFO, "killing");
  655. // update AP
  656. if (p->ap > ps->i) {
  657. p->ap = ps->i;
  658. }
  659. // optimize for statements which can be destroyed immediately
  660. if (NCDModuleInst_TryFree(&ps->inst)) {
  661. STATEMENT_LOG(ps, BLOG_INFO, "died");
  662. // free arguments memory
  663. NCDValMem_Free(&ps->args_mem);
  664. // set statement state FORGOTTEN
  665. ps->inst.istate = SSTATE_FORGOTTEN;
  666. // update FP
  667. while (p->fp > 0 && p->statements[p->fp - 1].inst.istate == SSTATE_FORGOTTEN) {
  668. p->fp--;
  669. }
  670. goto again;
  671. }
  672. // set statement state DYING
  673. ps->inst.istate = SSTATE_DYING;
  674. // order it to die
  675. NCDModuleInst_Die(&ps->inst);
  676. return;
  677. }
  678. int eval_func_eval_var (void *user, NCD_string_id_t const *varnames, size_t num_names, NCDValMem *mem, NCDValRef *out)
  679. {
  680. struct process *p = user;
  681. ASSERT(varnames)
  682. ASSERT(num_names > 0)
  683. ASSERT(mem)
  684. ASSERT(out)
  685. return process_resolve_variable_expr(p, p->ap, varnames, num_names, mem, out);
  686. }
  687. static int eval_func_eval_call (void *user, NCD_string_id_t func_name_id, NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out)
  688. {
  689. struct process *p = user;
  690. struct statement *ps = &p->statements[p->ap];
  691. STATEMENT_LOG(ps, BLOG_ERROR, "unknown function");
  692. return 0;
  693. }
  694. void process_advance (struct process *p)
  695. {
  696. process_assert_pointers(p);
  697. ASSERT(p->ap == p->fp)
  698. ASSERT(!process_have_child(p))
  699. ASSERT(p->ap < p->num_statements)
  700. ASSERT(!p->error)
  701. ASSERT(!BSmallPending_IsSet(&p->work_job))
  702. ASSERT(p->state == PSTATE_WORKING)
  703. struct statement *ps = &p->statements[p->ap];
  704. ASSERT(ps->inst.istate == SSTATE_FORGOTTEN)
  705. STATEMENT_LOG(ps, BLOG_INFO, "initializing");
  706. // need to determine the module and object to use it on (if it's a method)
  707. const struct NCDInterpModule *module;
  708. void *method_context = NULL;
  709. // get object names, e.g. "my.cat" in "my.cat->meow();"
  710. // (or NULL if this is not a method statement)
  711. const NCD_string_id_t *objnames;
  712. size_t num_objnames;
  713. NCDInterpProcess_StatementObjNames(p->iprocess, p->ap, &objnames, &num_objnames);
  714. if (!objnames) {
  715. // not a method; module is already known by NCDInterpProcess
  716. module = NCDInterpProcess_StatementGetSimpleModule(p->iprocess, p->ap, &p->interp->string_index, &p->interp->mindex);
  717. if (!module) {
  718. const char *cmdname_str = NCDInterpProcess_StatementCmdName(p->iprocess, p->ap, &p->interp->string_index);
  719. STATEMENT_LOG(ps, BLOG_ERROR, "unknown simple statement: %s", cmdname_str);
  720. goto fail0;
  721. }
  722. } else {
  723. // get object
  724. NCDObject object;
  725. if (!process_resolve_object_expr(p, p->ap, objnames, num_objnames, &object)) {
  726. goto fail0;
  727. }
  728. // get object type
  729. NCD_string_id_t object_type = NCDObject_Type(&object);
  730. if (object_type < 0) {
  731. STATEMENT_LOG(ps, BLOG_ERROR, "cannot call method on object with no type");
  732. goto fail0;
  733. }
  734. // get method context
  735. method_context = NCDObject_MethodUser(&object);
  736. // find module based on type of object
  737. module = NCDInterpProcess_StatementGetMethodModule(p->iprocess, p->ap, object_type, &p->interp->mindex);
  738. if (!module) {
  739. const char *type_str = NCDStringIndex_Value(&p->interp->string_index, object_type);
  740. const char *cmdname_str = NCDInterpProcess_StatementCmdName(p->iprocess, p->ap, &p->interp->string_index);
  741. STATEMENT_LOG(ps, BLOG_ERROR, "unknown method statement: %s::%s", type_str, cmdname_str);
  742. goto fail0;
  743. }
  744. }
  745. // get evaluator expression for the arguments
  746. NCDEvaluatorExpr *expr = NCDInterpProcess_GetStatementArgsExpr(p->iprocess, ps->i);
  747. // evaluate arguments
  748. NCDValRef args;
  749. NCDEvaluator_EvalFuncs funcs = {p, eval_func_eval_var, eval_func_eval_call};
  750. if (!NCDEvaluatorExpr_Eval(expr, &p->interp->evaluator, &funcs, &ps->args_mem, &args)) {
  751. STATEMENT_LOG(ps, BLOG_ERROR, "failed to evaluate arguments");
  752. goto fail0;
  753. }
  754. // convert non-continuous strings unless the module can handle them
  755. if (!(module->module.flags & NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS)) {
  756. if (!NCDValMem_ConvertNonContinuousStrings(&ps->args_mem, &args)) {
  757. STATEMENT_LOG(ps, BLOG_ERROR, "NCDValMem_ConvertNonContinuousStrings failed");
  758. goto fail1;
  759. }
  760. }
  761. // allocate memory
  762. if (!statement_allocate_memory(ps, module->module.alloc_size)) {
  763. STATEMENT_LOG(ps, BLOG_ERROR, "failed to allocate memory");
  764. goto fail1;
  765. }
  766. // set statement state CHILD
  767. ps->inst.istate = SSTATE_CHILD;
  768. // increment AP
  769. p->ap++;
  770. // increment FP
  771. p->fp++;
  772. process_assert_pointers(p);
  773. // initialize module instance
  774. NCDModuleInst_Init(&ps->inst, module, method_context, args, &p->interp->module_params);
  775. return;
  776. fail1:
  777. NCDValMem_Free(&ps->args_mem);
  778. fail0:
  779. // set error
  780. p->error = 1;
  781. // schedule work to start the timer
  782. BSmallPending_Set(&p->work_job, BReactor_PendingGroup(p->reactor));
  783. }
  784. void process_wait_timer_handler (BSmallTimer *timer)
  785. {
  786. struct process *p = UPPER_OBJECT(timer, struct process, wait_timer);
  787. process_assert_pointers(p);
  788. ASSERT(!BSmallPending_IsSet(&p->work_job))
  789. // check if something happened that means we no longer need to retry
  790. if (p->ap != p->fp || process_have_child(p) || p->ap == p->num_statements) {
  791. return;
  792. }
  793. process_log(p, BLOG_INFO, "retrying");
  794. // advance. Note: the asserts for this are indeed satisfied, though this
  795. // it not trivial to prove.
  796. process_advance(p);
  797. }
  798. int process_find_object (struct process *p, int pos, NCD_string_id_t name, NCDObject *out_object)
  799. {
  800. ASSERT(pos >= 0)
  801. ASSERT(pos <= p->num_statements)
  802. ASSERT(out_object)
  803. int i = NCDInterpProcess_FindStatement(p->iprocess, pos, name);
  804. if (i >= 0) {
  805. struct statement *ps = &p->statements[i];
  806. ASSERT(i < p->num_statements)
  807. if (ps->inst.istate == SSTATE_FORGOTTEN) {
  808. process_log(p, BLOG_ERROR, "statement (%d) is uninitialized", i);
  809. return 0;
  810. }
  811. *out_object = NCDModuleInst_Object(&ps->inst);
  812. return 1;
  813. }
  814. if (p->module_process && NCDModuleProcess_Interp_GetSpecialObj(p->module_process, name, out_object)) {
  815. return 1;
  816. }
  817. return 0;
  818. }
  819. int process_resolve_object_expr (struct process *p, int pos, const NCD_string_id_t *names, size_t num_names, NCDObject *out_object)
  820. {
  821. ASSERT(pos >= 0)
  822. ASSERT(pos <= p->num_statements)
  823. ASSERT(names)
  824. ASSERT(num_names > 0)
  825. ASSERT(out_object)
  826. NCDObject object;
  827. if (!process_find_object(p, pos, names[0], &object)) {
  828. goto fail;
  829. }
  830. if (!NCDObject_ResolveObjExprCompact(&object, names + 1, num_names - 1, out_object)) {
  831. goto fail;
  832. }
  833. return 1;
  834. fail:;
  835. char *name = implode_id_strings(p->interp, names, num_names, '.');
  836. process_log(p, BLOG_ERROR, "failed to resolve object (%s) from position %zu", (name ? name : ""), pos);
  837. free(name);
  838. return 0;
  839. }
  840. int process_resolve_variable_expr (struct process *p, int pos, const NCD_string_id_t *names, size_t num_names, NCDValMem *mem, NCDValRef *out_value)
  841. {
  842. ASSERT(pos >= 0)
  843. ASSERT(pos <= p->num_statements)
  844. ASSERT(names)
  845. ASSERT(num_names > 0)
  846. ASSERT(mem)
  847. ASSERT(out_value)
  848. NCDObject object;
  849. if (!process_find_object(p, pos, names[0], &object)) {
  850. goto fail;
  851. }
  852. if (!NCDObject_ResolveVarExprCompact(&object, names + 1, num_names - 1, mem, out_value)) {
  853. goto fail;
  854. }
  855. return 1;
  856. fail:;
  857. char *name = implode_id_strings(p->interp, names, num_names, '.');
  858. process_log(p, BLOG_ERROR, "failed to resolve variable (%s) from position %zu", (name ? name : ""), pos);
  859. free(name);
  860. return 0;
  861. }
  862. void statement_logfunc (struct statement *ps)
  863. {
  864. process_logfunc(statement_process(ps));
  865. BLog_Append("statement %zu: ", ps->i);
  866. }
  867. void statement_log (struct statement *ps, int level, const char *fmt, ...)
  868. {
  869. va_list vl;
  870. va_start(vl, fmt);
  871. BLog_LogViaFuncVarArg((BLog_logfunc)statement_logfunc, ps, BLOG_CURRENT_CHANNEL, level, fmt, vl);
  872. va_end(vl);
  873. }
  874. struct process * statement_process (struct statement *ps)
  875. {
  876. return UPPER_OBJECT(ps - ps->i, struct process, statements);
  877. }
  878. int statement_mem_is_allocated (struct statement *ps)
  879. {
  880. return (ps->mem_size < 0);
  881. }
  882. int statement_mem_size (struct statement *ps)
  883. {
  884. return (ps->mem_size >= 0 ? ps->mem_size : -ps->mem_size);
  885. }
  886. int statement_allocate_memory (struct statement *ps, int alloc_size)
  887. {
  888. ASSERT(alloc_size >= 0)
  889. if (alloc_size > statement_mem_size(ps)) {
  890. // allocate new memory
  891. char *new_mem = malloc(alloc_size);
  892. if (!new_mem) {
  893. STATEMENT_LOG(ps, BLOG_ERROR, "malloc failed");
  894. return 0;
  895. }
  896. // release old memory unless it was preallocated
  897. if (statement_mem_is_allocated(ps)) {
  898. free(ps->inst.mem);
  899. }
  900. struct process *p = statement_process(ps);
  901. // register memory in statement
  902. ps->inst.mem = new_mem;
  903. ps->mem_size = -alloc_size;
  904. // set the alloc flag in the process to make sure process_free()
  905. // releases the allocated memory
  906. p->have_alloc = 1;
  907. // register alloc size for future preallocations
  908. NCDInterpProcess_StatementBumpAllocSize(p->iprocess, ps->i, alloc_size);
  909. }
  910. return 1;
  911. }
  912. void statement_instance_func_event (NCDModuleInst *inst, int event)
  913. {
  914. struct statement *ps = UPPER_OBJECT(inst, struct statement, inst);
  915. ASSERT(ps->inst.istate == SSTATE_CHILD || ps->inst.istate == SSTATE_ADULT || ps->inst.istate == SSTATE_DYING)
  916. struct process *p = statement_process(ps);
  917. process_assert_pointers(p);
  918. // schedule work
  919. BSmallPending_Set(&p->work_job, BReactor_PendingGroup(p->reactor));
  920. switch (event) {
  921. case NCDMODULE_EVENT_UP: {
  922. ASSERT(ps->inst.istate == SSTATE_CHILD)
  923. STATEMENT_LOG(ps, BLOG_INFO, "up");
  924. // set state ADULT
  925. ps->inst.istate = SSTATE_ADULT;
  926. } break;
  927. case NCDMODULE_EVENT_DOWN: {
  928. ASSERT(ps->inst.istate == SSTATE_ADULT)
  929. STATEMENT_LOG(ps, BLOG_INFO, "down");
  930. // set state CHILD
  931. ps->inst.istate = SSTATE_CHILD;
  932. // clear error
  933. if (ps->i < p->ap) {
  934. p->error = 0;
  935. }
  936. // update AP
  937. if (p->ap > ps->i + 1) {
  938. p->ap = ps->i + 1;
  939. }
  940. } break;
  941. case NCDMODULE_EVENT_DOWNUP: {
  942. ASSERT(ps->inst.istate == SSTATE_ADULT)
  943. STATEMENT_LOG(ps, BLOG_INFO, "down");
  944. STATEMENT_LOG(ps, BLOG_INFO, "up");
  945. // clear error
  946. if (ps->i < p->ap) {
  947. p->error = 0;
  948. }
  949. // update AP
  950. if (p->ap > ps->i + 1) {
  951. p->ap = ps->i + 1;
  952. }
  953. } break;
  954. case NCDMODULE_EVENT_DEAD: {
  955. STATEMENT_LOG(ps, BLOG_INFO, "died");
  956. // free instance
  957. NCDModuleInst_Free(&ps->inst);
  958. // free arguments memory
  959. NCDValMem_Free(&ps->args_mem);
  960. // set state FORGOTTEN
  961. ps->inst.istate = SSTATE_FORGOTTEN;
  962. // update AP
  963. if (p->ap > ps->i) {
  964. p->ap = ps->i;
  965. }
  966. // update FP
  967. while (p->fp > 0 && p->statements[p->fp - 1].inst.istate == SSTATE_FORGOTTEN) {
  968. p->fp--;
  969. }
  970. } break;
  971. case NCDMODULE_EVENT_DEADERROR: {
  972. STATEMENT_LOG(ps, BLOG_ERROR, "died with error");
  973. // free instance
  974. NCDModuleInst_Free(&ps->inst);
  975. // free arguments memory
  976. NCDValMem_Free(&ps->args_mem);
  977. // set state FORGOTTEN
  978. ps->inst.istate = SSTATE_FORGOTTEN;
  979. // set error
  980. if (ps->i < p->ap) {
  981. p->error = 1;
  982. }
  983. // update AP
  984. if (p->ap > ps->i) {
  985. p->ap = ps->i;
  986. }
  987. // update FP
  988. while (p->fp > 0 && p->statements[p->fp - 1].inst.istate == SSTATE_FORGOTTEN) {
  989. p->fp--;
  990. }
  991. } break;
  992. }
  993. }
  994. int statement_instance_func_getobj (NCDModuleInst *inst, NCD_string_id_t objname, NCDObject *out_object)
  995. {
  996. struct statement *ps = UPPER_OBJECT(inst, struct statement, inst);
  997. ASSERT(ps->inst.istate != SSTATE_FORGOTTEN)
  998. return process_find_object(statement_process(ps), ps->i, objname, out_object);
  999. }
  1000. int statement_instance_func_initprocess (void *vinterp, NCDModuleProcess* mp, NCD_string_id_t template_name)
  1001. {
  1002. NCDInterpreter *interp = vinterp;
  1003. // find process
  1004. NCDInterpProcess *iprocess = NCDInterpProg_FindProcess(&interp->iprogram, template_name);
  1005. if (!iprocess) {
  1006. const char *str = NCDStringIndex_Value(&interp->string_index, template_name);
  1007. BLog(BLOG_ERROR, "no template named %s", str);
  1008. return 0;
  1009. }
  1010. // make sure it's a template
  1011. if (!NCDInterpProcess_IsTemplate(iprocess)) {
  1012. const char *str = NCDStringIndex_Value(&interp->string_index, template_name);
  1013. BLog(BLOG_ERROR, "need template to create a process, but %s is a process", str);
  1014. return 0;
  1015. }
  1016. // create process
  1017. if (!process_new(interp, iprocess, mp)) {
  1018. const char *str = NCDStringIndex_Value(&interp->string_index, template_name);
  1019. BLog(BLOG_ERROR, "failed to create process from template %s", str);
  1020. return 0;
  1021. }
  1022. if (BLog_WouldLog(BLOG_INFO, BLOG_CURRENT_CHANNEL)) {
  1023. const char *str = NCDStringIndex_Value(&interp->string_index, template_name);
  1024. BLog(BLOG_INFO, "created process from template %s", str);
  1025. }
  1026. return 1;
  1027. }
  1028. void statement_instance_logfunc (NCDModuleInst *inst)
  1029. {
  1030. struct statement *ps = UPPER_OBJECT(inst, struct statement, inst);
  1031. ASSERT(ps->inst.istate != SSTATE_FORGOTTEN)
  1032. statement_logfunc(ps);
  1033. BLog_Append("module: ");
  1034. }
  1035. void statement_instance_func_interp_exit (void *vinterp, int exit_code)
  1036. {
  1037. NCDInterpreter *interp = vinterp;
  1038. start_terminate(interp, exit_code);
  1039. }
  1040. int statement_instance_func_interp_getargs (void *vinterp, NCDValMem *mem, NCDValRef *out_value)
  1041. {
  1042. NCDInterpreter *interp = vinterp;
  1043. *out_value = NCDVal_NewList(mem, interp->params.num_extra_args);
  1044. if (NCDVal_IsInvalid(*out_value)) {
  1045. BLog(BLOG_ERROR, "NCDVal_NewList failed");
  1046. goto fail;
  1047. }
  1048. for (int i = 0; i < interp->params.num_extra_args; i++) {
  1049. NCDValRef arg = NCDVal_NewString(mem, interp->params.extra_args[i]);
  1050. if (NCDVal_IsInvalid(arg)) {
  1051. BLog(BLOG_ERROR, "NCDVal_NewString failed");
  1052. goto fail;
  1053. }
  1054. if (!NCDVal_ListAppend(*out_value, arg)) {
  1055. BLog(BLOG_ERROR, "depth limit exceeded");
  1056. goto fail;
  1057. }
  1058. }
  1059. return 1;
  1060. fail:
  1061. *out_value = NCDVal_NewInvalid();
  1062. return 1;
  1063. }
  1064. btime_t statement_instance_func_interp_getretrytime (void *vinterp)
  1065. {
  1066. NCDInterpreter *interp = vinterp;
  1067. return interp->params.retry_time;
  1068. }
  1069. int statement_instance_func_interp_loadgroup (void *vinterp, const struct NCDModuleGroup *group)
  1070. {
  1071. NCDInterpreter *interp = vinterp;
  1072. if (!NCDModuleIndex_AddGroup(&interp->mindex, group, &interp->module_iparams, &interp->string_index)) {
  1073. BLog(BLOG_ERROR, "NCDModuleIndex_AddGroup failed");
  1074. return 0;
  1075. }
  1076. return 1;
  1077. }
  1078. void process_moduleprocess_func_event (struct process *p, int event)
  1079. {
  1080. ASSERT(p->module_process)
  1081. switch (event) {
  1082. case NCDMODULEPROCESS_INTERP_EVENT_CONTINUE: {
  1083. ASSERT(p->state == PSTATE_WAITING)
  1084. // set state working
  1085. process_set_state(p, PSTATE_WORKING);
  1086. BSmallPending_SetHandler(&p->work_job, (BSmallPending_handler)process_work_job_handler_working, p);
  1087. // schedule work
  1088. BSmallPending_Set(&p->work_job, BReactor_PendingGroup(p->reactor));
  1089. } break;
  1090. case NCDMODULEPROCESS_INTERP_EVENT_TERMINATE: {
  1091. ASSERT(p->state != PSTATE_TERMINATING)
  1092. process_log(p, BLOG_INFO, "process termination requested");
  1093. // start terminating
  1094. process_start_terminating(p);
  1095. } break;
  1096. default: ASSERT(0);
  1097. }
  1098. }
  1099. int process_moduleprocess_func_getobj (struct process *p, NCD_string_id_t name, NCDObject *out_object)
  1100. {
  1101. ASSERT(p->module_process)
  1102. return process_find_object(p, p->num_statements, name, out_object);
  1103. }