NCDInterpreter.c 43 KB

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