NCDInterpreter.c 42 KB

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