NCDInterpreter.c 43 KB

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