NCDInterpreter.c 43 KB

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