NCDInterpreter.c 43 KB

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