NCDInterpreter.c 43 KB

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