NCDModule.h 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  1. /**
  2. * @file NCDModule.h
  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. #ifndef BADVPN_NCD_NCDMODULE_H
  30. #define BADVPN_NCD_NCDMODULE_H
  31. #include <stdarg.h>
  32. #include <misc/debug.h>
  33. #include <system/BReactor.h>
  34. #include <base/BLog.h>
  35. #include <ncd/NCDVal.h>
  36. #include <ncd/NCDObject.h>
  37. #include <ncd/NCDStringIndex.h>
  38. #ifndef BADVPN_NO_PROCESS
  39. #include <system/BProcess.h>
  40. #endif
  41. #ifndef BADVPN_NO_UDEV
  42. #include <udevmonitor/NCDUdevManager.h>
  43. #endif
  44. #ifndef BADVPN_NO_RANDOM
  45. #include <random/BRandom2.h>
  46. #endif
  47. #define NCDMODULE_EVENT_UP 1
  48. #define NCDMODULE_EVENT_DOWN 2
  49. #define NCDMODULE_EVENT_DOWNUP 3
  50. #define NCDMODULE_EVENT_DEAD 4
  51. #define NCDMODULE_EVENT_DEADERROR 5
  52. struct NCDModuleInst_s;
  53. struct NCDModuleProcess_s;
  54. struct NCDModuleGroup;
  55. struct NCDInterpModule;
  56. struct NCDInterpModuleGroup;
  57. struct NCDCall_interp_shared;
  58. struct NCDInterpFunction;
  59. /**
  60. * Function called to inform the interpeter of state changes of the
  61. * module instance.
  62. * Possible events are:
  63. *
  64. * - NCDMODULE_EVENT_UP: the instance came up.
  65. * The instance was in down state.
  66. * The instance enters up state.
  67. *
  68. * - NCDMODULE_EVENT_DOWN: the instance went down.
  69. * The instance was in up state.
  70. * The instance enters down state.
  71. *
  72. * After the instance goes down, the interpreter should eventually call
  73. * {@link NCDModuleInst_Clean} or {@link NCDModuleInst_Die}, unless
  74. * the module goes up again.
  75. *
  76. * - NCDMODULE_EVENT_DEAD: the module died. To determine if the module
  77. * died with error, read the is_error member of {@link NCDModuleInst}.
  78. * The instance enters dead state.
  79. *
  80. * This function is not being called in event context. The interpreter should
  81. * only update its internal state, and visibly react only via jobs that it pushes
  82. * from within this function. The only exception is that it may free the
  83. * instance from within the NCDMODULE_EVENT_DEAD event.
  84. *
  85. * @param inst the module instance
  86. * @param event event number
  87. */
  88. typedef void (*NCDModuleInst_func_event) (struct NCDModuleInst_s *inst, int event);
  89. /**
  90. * Function called when the module instance wants the interpreter to
  91. * resolve an object from the point of view of its statement.
  92. * The instance will not be in dead state.
  93. * This function must not have any side effects.
  94. *
  95. * @param inst the module instance
  96. * @param name name of the object as an {@link NCDStringIndex} identifier
  97. * @param out_object the object will be returned here
  98. * @return 1 on success, 0 on failure
  99. */
  100. typedef int (*NCDModuleInst_func_getobj) (struct NCDModuleInst_s *inst, NCD_string_id_t name, NCDObject *out_object);
  101. /**
  102. * Function called when the module instance wants the interpreter to
  103. * create a new process backend from a process template.
  104. * The instance will not be in dead state.
  105. *
  106. * On success, the interpreter must have called {@link NCDModuleProcess_Interp_SetHandlers}
  107. * from within this function, to allow communication with the controller of the process.
  108. * On success, the new process backend enters down state.
  109. *
  110. * This function is not being called in event context. The interpreter should
  111. * only update its internal state, and visibly react only via jobs that it pushes
  112. * from within this function.
  113. *
  114. * @param user value of 'user' member of {@link NCDModuleInst_iparams}
  115. * @param p handle for the new process backend
  116. * @param template_name name of the template to create the process from,
  117. * as an {@link NCDStringIndex} identifier
  118. * @return 1 on success, 0 on failure
  119. */
  120. typedef int (*NCDModuleInst_func_initprocess) (void *user, struct NCDModuleProcess_s *p, NCD_string_id_t template_name);
  121. /**
  122. * Function called when the module instance wants the interpreter to
  123. * initiate termination, as if it received an external terminatio request (signal).
  124. *
  125. * @param user value of 'user' member of {@link NCDModuleInst_iparams}
  126. * @param exit_code exit code to return the the operating system. This overrides any previously
  127. * set exit code, and will be overriden by a signal to the value 1.
  128. *
  129. */
  130. typedef void (*NCDModuleInst_func_interp_exit) (void *user, int exit_code);
  131. /**
  132. * Function called when the module instance wants the interpreter to
  133. * provide its extra command line arguments.
  134. *
  135. * @param user value of 'user' member of {@link NCDModuleInst_iparams}
  136. * @param mem value memory to use
  137. * @param out_value write value reference here on success
  138. * @return 1 if available, 0 if not available. If available, but out of memory, return 1
  139. * and an invalid value.
  140. */
  141. typedef int (*NCDModuleInst_func_interp_getargs) (void *user, NCDValMem *mem, NCDValRef *out_value);
  142. /**
  143. * Function called when the module instance wants the interpreter to
  144. * provide its retry time.
  145. *
  146. * @param user value of 'user' member of {@link NCDModuleInst_iparams}
  147. * @return retry time in milliseconds
  148. */
  149. typedef btime_t (*NCDModuleInst_func_interp_getretrytime) (void *user);
  150. /**
  151. * Function called when the module instance wants the interpreter to
  152. * load a new module group.
  153. *
  154. * @param user value of 'user' member of {@link NCDModuleInst_iparams}
  155. * @param group module group to load
  156. * @return 1 on success, 0 on failure
  157. */
  158. typedef int (*NCDModuleInst_func_interp_loadgroup) (void *user, const struct NCDModuleGroup *group);
  159. #define NCDMODULEPROCESS_EVENT_UP 1
  160. #define NCDMODULEPROCESS_EVENT_DOWN 2
  161. #define NCDMODULEPROCESS_EVENT_TERMINATED 3
  162. /**
  163. * Handler which reports process state changes from the interpreter.
  164. * Possible events are:
  165. *
  166. * - NCDMODULEPROCESS_EVENT_UP: the process went up.
  167. * The process was in down state.
  168. * The process enters up state.
  169. *
  170. * - NCDMODULEPROCESS_EVENT_DOWN: the process went down.
  171. * The process was in up state.
  172. * The process enters waiting state.
  173. *
  174. * NOTE: the process enters waiting state, NOT down state, and is paused.
  175. * To allow the process to continue, call {@link NCDModuleProcess_Continue}.
  176. *
  177. * - NCDMODULEPROCESS_EVENT_TERMINATED: the process terminated.
  178. * The process was in terminating state.
  179. * The process enters terminated state.
  180. *
  181. * @param user pointer to the process. Use {@link UPPER_OBJECT} to retrieve the pointer
  182. * to the containing struct.
  183. * @param event event number
  184. */
  185. typedef void (*NCDModuleProcess_handler_event) (struct NCDModuleProcess_s *process, int event);
  186. /**
  187. * Function called when the interpreter wants to resolve a special
  188. * object in the process.
  189. * This function must have no side effects.
  190. *
  191. * @param user pointer to the process. Use {@link UPPER_OBJECT} to retrieve the pointer
  192. * to the containing struct.
  193. * @param name name of the object as an {@link NCDStringIndex} identifier
  194. * @param out_object the object will be returned here
  195. * @return 1 on success, 0 on failure
  196. */
  197. typedef int (*NCDModuleProcess_func_getspecialobj) (struct NCDModuleProcess_s *process, NCD_string_id_t name, NCDObject *out_object);
  198. #define NCDMODULEPROCESS_INTERP_EVENT_CONTINUE 1
  199. #define NCDMODULEPROCESS_INTERP_EVENT_TERMINATE 2
  200. /**
  201. * Function called to report process backend requests to the interpreter.
  202. * Possible events are:
  203. *
  204. * - NCDMODULEPROCESS_INTERP_EVENT_CONTINUE: the process can continue.
  205. * The process backend was in waiting state.
  206. * The process backend enters down state.
  207. *
  208. * - NCDMODULEPROCESS_INTERP_EVENT_TERMINATE: the process should terminate.
  209. * The process backend was in down, up or waiting state.
  210. * The process backend enters terminating state.
  211. *
  212. * The interpreter should call {@link NCDModuleProcess_Interp_Terminated}
  213. * when the process terminates.
  214. *
  215. * This function is not being called in event context. The interpreter should
  216. * only update its internal state, and visibly react only via jobs that it pushes
  217. * from within this function.
  218. *
  219. * @param user as in {@link NCDModuleProcess_Interp_SetHandlers}
  220. * @param event event number
  221. */
  222. typedef void (*NCDModuleProcess_interp_func_event) (void *user, int event);
  223. /**
  224. * Function called to have the interpreter resolve an object within the process
  225. * of a process backend.
  226. * This function must not have any side effects.
  227. *
  228. * @param user as in {@link NCDModuleProcess_Interp_SetHandlers}
  229. * @param name name of the object as an {@link NCDStringIndex} identifier
  230. * @param out_object the object will be returned here
  231. * @return 1 on success, 0 in failure
  232. */
  233. typedef int (*NCDModuleProcess_interp_func_getobj) (void *user, NCD_string_id_t name, NCDObject *out_object);
  234. struct NCDModule;
  235. /**
  236. * Contains parameters to the module initialization function
  237. * ({@link NCDModule_func_new2}) that are passed indirectly.
  238. */
  239. struct NCDModuleInst_new_params {
  240. /**
  241. * A reference to the argument list for the module instance.
  242. * The reference remains valid as long as the backend instance
  243. * exists.
  244. */
  245. NCDValRef args;
  246. /**
  247. * If the module instance corresponds to a method-like statement,
  248. * this pointer identifies the object it is being invoked with.
  249. * If the object is a statement (i.e. a {@link NCDModuleInst}), then this
  250. * will be the NCDModuleInst pointer, and {@link NCDModuleInst_Backend_GetUser}
  251. * can be called on this to retrieve the pointer to preallocated memory for
  252. * the backend instance; *unless* {@link NCDModuleInst_Backend_PassMemToMethods}
  253. * was called for the object on which the method is being called, in which case
  254. * this will directly point to the preallocated memory.
  255. * On the other hand, if this is a method on an internal object built using
  256. * only {@link NCDObject_Build} or {@link NCDObject_BuildFull},
  257. * this pointer will be whatever was passed as the "data_ptr" argument, for the
  258. * first function, and as "method_user", for the latter function.
  259. */
  260. void *method_user;
  261. };
  262. /**
  263. * Contains parameters to {@link NCDModuleInst_Init} that are passed indirectly.
  264. * This itself only contains parameters related to communication between the
  265. * backend and the creator of the module instance; other parameters are passed
  266. * via the iparams member;
  267. */
  268. struct NCDModuleInst_params {
  269. /**
  270. * Callback to report state changes.
  271. */
  272. NCDModuleInst_func_event func_event;
  273. /**
  274. * Callback to resolve objects from the viewpoint of the instance.
  275. */
  276. NCDModuleInst_func_getobj func_getobj;
  277. /**
  278. * Log function which appends a log prefix with {@link BLog_Append}.
  279. */
  280. BLog_logfunc logfunc;
  281. /**
  282. * Pointer to an {@link NCDModuleInst_iparams} structure, which exposes
  283. * services provided by the interpreter.
  284. */
  285. const struct NCDModuleInst_iparams *iparams;
  286. };
  287. /**
  288. * Contains parameters to {@link NCDModuleInst_Init} that are passed indirectly.
  289. * This only contains parameters related to services provided by the interpreter.
  290. */
  291. struct NCDModuleInst_iparams {
  292. /**
  293. * Reactor we live in.
  294. */
  295. BReactor *reactor;
  296. #ifndef BADVPN_NO_PROCESS
  297. /**
  298. * Process manager.
  299. */
  300. BProcessManager *manager;
  301. #endif
  302. #ifndef BADVPN_NO_UDEV
  303. /**
  304. * Udev manager.
  305. */
  306. NCDUdevManager *umanager;
  307. #endif
  308. #ifndef BADVPN_NO_RANDOM
  309. /**
  310. * Random number generator.
  311. */
  312. BRandom2 *random2;
  313. #endif
  314. /**
  315. * String index which keeps a mapping between strings and string identifiers.
  316. */
  317. NCDStringIndex *string_index;
  318. /**
  319. * Pointer passed to the interpreter callbacks below, for state keeping.
  320. */
  321. void *user;
  322. /**
  323. * Callback to create a new template process.
  324. */
  325. NCDModuleInst_func_initprocess func_initprocess;
  326. /**
  327. * Callback to request interpreter termination.
  328. */
  329. NCDModuleInst_func_interp_exit func_interp_exit;
  330. /**
  331. * Callback to get extra command line arguments.
  332. */
  333. NCDModuleInst_func_interp_getargs func_interp_getargs;
  334. /**
  335. * Callback to get retry time.
  336. */
  337. NCDModuleInst_func_interp_getretrytime func_interp_getretrytime;
  338. /**
  339. * Callback to load a module group.
  340. */
  341. NCDModuleInst_func_interp_loadgroup func_loadgroup;
  342. };
  343. /**
  344. * Module instance.
  345. * The module instance is initialized by the interpreter by calling
  346. * {@link NCDModuleInst_Init}. It is implemented by a module backend
  347. * specified in a {@link NCDModule}.
  348. */
  349. typedef struct NCDModuleInst_s {
  350. const struct NCDInterpModule *m;
  351. const struct NCDModuleInst_params *params;
  352. void *mem; // not modified by NCDModuleInst (but passed to module)
  353. unsigned int state:3;
  354. unsigned int pass_mem_to_methods:1;
  355. unsigned int istate:3; // untouched by NCDModuleInst
  356. NCDPersistentObj pobj;
  357. DebugObject d_obj;
  358. } NCDModuleInst;
  359. /**
  360. * Weak NCDModuleInst reference.
  361. */
  362. typedef struct {
  363. NCDObjRef objref;
  364. DebugObject d_obj;
  365. } NCDModuleRef;
  366. /**
  367. * Process created from a process template on behalf of a module backend
  368. * instance, implemented by the interpreter.
  369. */
  370. typedef struct NCDModuleProcess_s {
  371. NCDValRef args;
  372. NCDModuleProcess_handler_event handler_event;
  373. NCDModuleProcess_func_getspecialobj func_getspecialobj;
  374. void *interp_user;
  375. NCDModuleProcess_interp_func_event interp_func_event;
  376. NCDModuleProcess_interp_func_getobj interp_func_getobj;
  377. #ifndef NDEBUG
  378. int state;
  379. #endif
  380. DebugObject d_obj;
  381. } NCDModuleProcess;
  382. /**
  383. * Initializes an instance of an NCD module.
  384. * The instance is initialized in down state.
  385. * WARNING: this directly calls the module backend; expect to be called back
  386. *
  387. * This and other non-Backend methods are the interpreter interface.
  388. * The Backend methods are the module backend interface and are documented
  389. * independently with their own logical states.
  390. *
  391. * NOTE: the instance structure \a n should have the member 'mem' initialized
  392. * to point to preallocated memory for the statement. This memory must be
  393. * at least m->prealloc_size big and must be properly aligned for any object.
  394. * The 'mem' pointer is never modified by NCDModuleInst, so that the interpreter
  395. * can use it as outside the lifetime of NCDModuleInst.
  396. *
  397. * @param n the instance
  398. * @param m pointer to the {@link NCDInterpModule} structure representing the module
  399. * to be instantiated
  400. * @param method_context a context pointer passed to the module backend, applicable to method-like
  401. * statements only. This should be set to the 'user' member of the
  402. * {@link NCDObject} which represents the base object for the method.
  403. * The caller must ensure that the NCDObject that was used is of the type
  404. * expected by the module being instanciated.
  405. * @param args arguments to the module. Must be a list value. Must be available and unchanged
  406. * as long as the instance exists.
  407. * @param user argument to callback functions
  408. * @param params more parameters, see {@link NCDModuleInst_params}
  409. */
  410. void NCDModuleInst_Init (NCDModuleInst *n, const struct NCDInterpModule *m, void *method_context, NCDValRef args, const struct NCDModuleInst_params *params);
  411. /**
  412. * Frees the instance.
  413. * The instance must be in dead state.
  414. *
  415. * @param n the instance
  416. */
  417. void NCDModuleInst_Free (NCDModuleInst *n);
  418. /**
  419. * Requests the instance to die.
  420. * The instance must be in down or up state.
  421. * The instance enters dying state.
  422. * WARNING: this directly calls the module backend; expect to be called back
  423. *
  424. * @param n the instance
  425. */
  426. void NCDModuleInst_Die (NCDModuleInst *n);
  427. /**
  428. * Attempts to destroy the instance immediately.
  429. * This function can be used to optimize destroying instances of modules which
  430. * don't specify any {@link NCDModule_func_die} handler. If immediate destruction
  431. * is not possible, this does nothing and returns 0; {@link NCDModuleInst_Die}
  432. * should be used to destroy the instance instead. If however immediate destruction
  433. * is possible, this destroys the module instance and returns 1; {@link NCDModuleInst_Free}
  434. * must not be called after that.
  435. * The instance must be in down or up state, as for {@link NCDModuleInst_Die}.
  436. *
  437. * @param n the instance
  438. * @return 1 if destruction was performed, 0 if not
  439. */
  440. int NCDModuleInst_TryFree (NCDModuleInst *n);
  441. /**
  442. * Informs the module that it is in a clean state to proceed.
  443. * The instance must be in down state.
  444. * WARNING: this directly calls the module backend; expect to be called back
  445. *
  446. * @param n the instance
  447. */
  448. void NCDModuleInst_Clean (NCDModuleInst *n);
  449. /**
  450. * Returns an {@link NCDObject} which can be used to resolve variables and objects
  451. * within this instance, as well as call its methods. The resulting object may only
  452. * be used immediately, and becomes invalid when the instance is freed.
  453. *
  454. * @param n the instance
  455. * @return an NCDObject for this instance
  456. */
  457. NCDObject NCDModuleInst_Object (NCDModuleInst *n);
  458. /**
  459. * If this is called, any methods called on this object will receive the preallocated
  460. * memory pointer as the object state pointer. This means that in the
  461. * {@link NCDModule_func_getvar2} function which is called when a method is created,
  462. * the preallocated memory should be accessed as params->method_user.
  463. * By default, however, params->method_user points to the NCDModuleInst of the base
  464. * object, and {@link NCDModuleInst_Backend_GetUser} is needed to retrieve the
  465. * preallocated memory pointer.
  466. */
  467. void NCDModuleInst_Backend_PassMemToMethods (NCDModuleInst *n);
  468. /**
  469. * Retuns the state pointer passed to handlers of a module backend instance;
  470. * see {@link NCDModule_func_new2}.
  471. *
  472. * @param n backend instance handle
  473. * @return argument passed to handlers
  474. */
  475. void * NCDModuleInst_Backend_GetUser (NCDModuleInst *n);
  476. /**
  477. * Puts the backend instance into up state.
  478. * The instance must be in down state.
  479. * The instance enters up state.
  480. *
  481. * @param n backend instance handle
  482. */
  483. void NCDModuleInst_Backend_Up (NCDModuleInst *n);
  484. /**
  485. * Puts the backend instance into down state.
  486. * The instance must be in up state.
  487. * The instance enters down state.
  488. *
  489. * @param n backend instance handle
  490. */
  491. void NCDModuleInst_Backend_Down (NCDModuleInst *n);
  492. /**
  493. * Puts the backend instance into down state, then immediatly back into the up state.
  494. * This effectively causes the interpreter to start backtracking to this statement.
  495. * The instance must be in up state, and remains in up state.
  496. *
  497. * @param n backend instance handle
  498. */
  499. void NCDModuleInst_Backend_DownUp (NCDModuleInst *n);
  500. /**
  501. * Destroys the backend instance.
  502. * The backend instance handle becomes invalid and must not be used from
  503. * the backend any longer.
  504. *
  505. * @param n backend instance handle
  506. */
  507. void NCDModuleInst_Backend_Dead (NCDModuleInst *n);
  508. /**
  509. * Like {@link NCDModuleInst_Backend_Dead}, but also reports an error condition
  510. * to the interpreter.
  511. */
  512. void NCDModuleInst_Backend_DeadError (NCDModuleInst *n);
  513. /**
  514. * Resolves an object for a backend instance, from the point of the instance's
  515. * statement in the containing process.
  516. *
  517. * @param n backend instance handle
  518. * @param name name of the object to resolve as an {@link NCDStringIndex} identifier
  519. * @param out_object the object will be returned here
  520. * @return 1 on success, 0 on failure
  521. */
  522. int NCDModuleInst_Backend_GetObj (NCDModuleInst *n, NCD_string_id_t name, NCDObject *out_object) WARN_UNUSED;
  523. /**
  524. * Logs a backend instance message.
  525. *
  526. * @param n backend instance handle
  527. * @param channel log channel
  528. * @param level loglevel
  529. * @param fmt format string as in printf, arguments follow
  530. */
  531. void NCDModuleInst_Backend_Log (NCDModuleInst *n, int channel, int level, const char *fmt, ...);
  532. /**
  533. * Like {@link NCDModuleInst_Backend_Log}, but the extra arguments are passed
  534. * as a va_list. This allows creation of logging wrappers.
  535. */
  536. void NCDModuleInst_Backend_LogVarArg (NCDModuleInst *n, int channel, int level, const char *fmt, va_list vl);
  537. /**
  538. * Returns a logging context. The context is valid until the backend dies.
  539. */
  540. BLogContext NCDModuleInst_Backend_LogContext (NCDModuleInst *n);
  541. /**
  542. * Initiates interpreter termination.
  543. *
  544. * @param n backend instance handle
  545. * @param exit_code exit code to return to the operating system. This overrides
  546. * any previously set exit code, and will be overriden by a
  547. * termination signal to the value 1.
  548. */
  549. void NCDModuleInst_Backend_InterpExit (NCDModuleInst *n, int exit_code);
  550. /**
  551. * Retrieves extra command line arguments passed to the interpreter.
  552. *
  553. * @param n backend instance handle
  554. * @param mem value memory to use
  555. * @param out_value the arguments will be written here on success as a list value
  556. * @return 1 if available, 0 if not available. If available, but out of memory, returns 1
  557. * and an invalid value.
  558. */
  559. int NCDModuleInst_Backend_InterpGetArgs (NCDModuleInst *n, NCDValMem *mem, NCDValRef *out_value);
  560. /**
  561. * Returns the retry time of the intepreter.
  562. *
  563. * @param n backend instance handle
  564. * @return retry time in milliseconds
  565. */
  566. btime_t NCDModuleInst_Backend_InterpGetRetryTime (NCDModuleInst *n);
  567. /**
  568. * Loads a module group into the interpreter.
  569. *
  570. * @param n backend instance handle
  571. * @param group module group to load
  572. * @return 1 on success, 0 on failure
  573. */
  574. int NCDModuleInst_Backend_InterpLoadGroup (NCDModuleInst *n, const struct NCDModuleGroup *group);
  575. /**
  576. * Initializes a weak reference to a module instance.
  577. * The instane must no have had NCDModuleInst_Backend_PassMemToMethods
  578. * called.
  579. */
  580. void NCDModuleRef_Init (NCDModuleRef *o, NCDModuleInst *inst);
  581. /**
  582. * Frees the reference.
  583. */
  584. void NCDModuleRef_Free (NCDModuleRef *o);
  585. /**
  586. * Dereferences the reference.
  587. * If the reference was broken, returns NULL.
  588. */
  589. NCDModuleInst * NCDModuleRef_Deref (NCDModuleRef *o);
  590. /**
  591. * Initializes a process in the interpreter from a process template.
  592. * This must be called on behalf of a module backend instance.
  593. * The process is initializes in down state.
  594. *
  595. * @param o the process
  596. * @param n backend instance whose interpreter will be providing the process
  597. * @param template_name name of the process template as an {@link NCDStringIndex} identifier
  598. * @param args arguments to the process. Must be an invalid value or a list value.
  599. * The value must be available and unchanged while the process exists.
  600. * @param handler_event handler which reports events about the process from the
  601. * interpreter
  602. * @return 1 on success, 0 on failure
  603. */
  604. int NCDModuleProcess_InitId (NCDModuleProcess *o, NCDModuleInst *n, NCD_string_id_t template_name, NCDValRef args, NCDModuleProcess_handler_event handler_event) WARN_UNUSED;
  605. /**
  606. * Wrapper around {@link NCDModuleProcess_InitId} which takes the template name as an
  607. * {@link NCDValRef}, which must point to a string value.
  608. */
  609. int NCDModuleProcess_InitValue (NCDModuleProcess *o, NCDModuleInst *n, NCDValRef template_name, NCDValRef args, NCDModuleProcess_handler_event handler_event) WARN_UNUSED;
  610. /**
  611. * Frees the process.
  612. * The process must be in terminated state.
  613. *
  614. * @param o the process
  615. */
  616. void NCDModuleProcess_Free (NCDModuleProcess *o);
  617. /**
  618. * Does nothing.
  619. * The process must be in terminated state.
  620. *
  621. * @param o the process
  622. */
  623. void NCDModuleProcess_AssertFree (NCDModuleProcess *o);
  624. /**
  625. * Sets callback functions for providing special objects within the process.
  626. *
  627. * @param o the process
  628. * @param func_getspecialobj function for resolving special objects, or NULL
  629. */
  630. void NCDModuleProcess_SetSpecialFuncs (NCDModuleProcess *o, NCDModuleProcess_func_getspecialobj func_getspecialobj);
  631. /**
  632. * Continues the process after the process went down.
  633. * The process must be in waiting state.
  634. * The process enters down state.
  635. *
  636. * @param o the process
  637. */
  638. void NCDModuleProcess_Continue (NCDModuleProcess *o);
  639. /**
  640. * Requests the process to terminate.
  641. * The process must be in down, up or waiting state.
  642. * The process enters terminating state.
  643. *
  644. * @param o the process
  645. */
  646. void NCDModuleProcess_Terminate (NCDModuleProcess *o);
  647. /**
  648. * Resolves an object within the process from the point
  649. * at the end of the process.
  650. * This function has no side effects.
  651. *
  652. * @param o the process
  653. * @param name name of the object to resolve as an {@link NCDStringIndex} identifier
  654. * @param out_object the object will be returned here
  655. * @return 1 on success, 0 on failure
  656. */
  657. int NCDModuleProcess_GetObj (NCDModuleProcess *o, NCD_string_id_t name, NCDObject *out_object) WARN_UNUSED;
  658. /**
  659. * Sets callback functions for the interpreter to implement the
  660. * process backend.
  661. * Must be called from within {@link NCDModuleInst_func_initprocess}
  662. * if success is to be reported there.
  663. *
  664. * @param o process backend handle, as in {@link NCDModuleInst_func_initprocess}
  665. * @param interp_user argument to callback functions
  666. * @param interp_func_event function for reporting continue/terminate requests
  667. * @param interp_func_getobj function for resolving objects within the process
  668. */
  669. void NCDModuleProcess_Interp_SetHandlers (NCDModuleProcess *o, void *interp_user,
  670. NCDModuleProcess_interp_func_event interp_func_event,
  671. NCDModuleProcess_interp_func_getobj interp_func_getobj);
  672. /**
  673. * Reports the process backend as up.
  674. * The process backend must be in down state.
  675. * The process backend enters up state.
  676. * WARNING: this directly calls the process creator; expect to be called back
  677. *
  678. * @param o process backend handle
  679. */
  680. void NCDModuleProcess_Interp_Up (NCDModuleProcess *o);
  681. /**
  682. * Reports the process backend as down.
  683. * The process backend must be in up state.
  684. * The process backend enters waiting state.
  685. * WARNING: this directly calls the process creator; expect to be called back
  686. *
  687. * NOTE: the backend enters waiting state, NOT down state. The interpreter should
  688. * pause the process until {@link NCDModuleProcess_interp_func_event} reports
  689. * NCDMODULEPROCESS_INTERP_EVENT_CONTINUE, unless termination is requested via
  690. * NCDMODULEPROCESS_INTERP_EVENT_TERMINATE.
  691. *
  692. * @param o process backend handle
  693. */
  694. void NCDModuleProcess_Interp_Down (NCDModuleProcess *o);
  695. /**
  696. * Reports termination of the process backend.
  697. * The process backend must be in terminating state.
  698. * The process backend handle becomes invalid and must not be used
  699. * by the interpreter any longer.
  700. * WARNING: this directly calls the process creator; expect to be called back
  701. *
  702. * @param o process backend handle
  703. */
  704. void NCDModuleProcess_Interp_Terminated (NCDModuleProcess *o);
  705. /**
  706. * Resolves a special process object for the process backend.
  707. *
  708. * @param o process backend handle
  709. * @param name name of the object as an {@link NCDStringIndex} identifier
  710. * @param out_object the object will be returned here
  711. * @return 1 on success, 0 on failure
  712. */
  713. int NCDModuleProcess_Interp_GetSpecialObj (NCDModuleProcess *o, NCD_string_id_t name, NCDObject *out_object) WARN_UNUSED;
  714. /**
  715. * Function called before any instance of any backend in a module
  716. * group is created;
  717. *
  718. * @param params structure containing global resources, such as
  719. * {@link BReactor}, {@link BProcessManager} and {@link NCDUdevManager}.
  720. * @return 1 on success, 0 on failure
  721. */
  722. typedef int (*NCDModule_func_globalinit) (struct NCDInterpModuleGroup *group, const struct NCDModuleInst_iparams *params);
  723. /**
  724. * Function called to clean up after {@link NCDModule_func_globalinit} and modules
  725. * in a module group.
  726. * There are no backend instances alive from this module group.
  727. */
  728. typedef void (*NCDModule_func_globalfree) (struct NCDInterpModuleGroup *group);
  729. /**
  730. * Handler called to create an new module backend instance.
  731. * The backend is initialized in down state.
  732. *
  733. * If the backend fails initialization, this function should report the backend
  734. * instance to have died with error by calling {@link NCDModuleInst_Backend_DeadError}.
  735. *
  736. * @param o if the module specifies a positive alloc_size value in the {@link NCDModule}
  737. * structure, this will point to the allocated memory that can be used by the
  738. * module instance while it exists. If the alloc_size is 0 (default), this may or
  739. * may not be NULL.
  740. * @param i module backend instance handler. The backend may only use this handle via
  741. * the Backend functions of {@link NCDModuleInst}.
  742. */
  743. typedef void (*NCDModule_func_new2) (void *o, NCDModuleInst *i, const struct NCDModuleInst_new_params *params);
  744. /**
  745. * Handler called to request termination of a backend instance.
  746. * The backend instance was in down or up state.
  747. * The backend instance enters dying state.
  748. *
  749. * @param o state pointer, as in {@link NCDModule_func_new2}
  750. */
  751. typedef void (*NCDModule_func_die) (void *o);
  752. /**
  753. * Function called to resolve a variable within a backend instance.
  754. * The backend instance is in up state, or in up or down state if can_resolve_when_down=1.
  755. * This function must not have any side effects.
  756. *
  757. * @param o state pointer, as in {@link NCDModule_func_new2}
  758. * @param name name of the variable to resolve
  759. * @param mem value memory to use
  760. * @param out on success, the backend should initialize the value here
  761. * @return 1 if exists, 0 if not exists. If exists, but out of memory, return 1
  762. * and an invalid value.
  763. */
  764. typedef int (*NCDModule_func_getvar) (void *o, const char *name, NCDValMem *mem, NCDValRef *out);
  765. /**
  766. * Function called to resolve a variable within a backend instance.
  767. * The backend instance is in up state, or in up or down state if can_resolve_when_down=1.
  768. * This function must not have any side effects.
  769. *
  770. * @param o state pointer, as in {@link NCDModule_func_new2}
  771. * @param name name of the variable to resolve as an {@link NCDStringIndex} identifier
  772. * @param mem value memory to use
  773. * @param out on success, the backend should initialize the value here
  774. * @return 1 if exists, 0 if not exists. If exists, but out of memory, return 1
  775. * and an invalid value.
  776. */
  777. typedef int (*NCDModule_func_getvar2) (void *o, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out);
  778. /**
  779. * Function called to resolve an object within a backend instance.
  780. * The backend instance is in up state, or in up or down state if can_resolve_when_down=1.
  781. * This function must not have any side effects.
  782. *
  783. * @param o state pointer, as in {@link NCDModule_func_new2}
  784. * @param name name of the object to resolve as an {@link NCDStringIndex} identifier
  785. * @param out_object the object will be returned here
  786. * @return 1 on success, 0 on failure
  787. */
  788. typedef int (*NCDModule_func_getobj) (void *o, NCD_string_id_t name, NCDObject *out_object);
  789. /**
  790. * Handler called when the module instance is in a clean state.
  791. * This means that all statements preceding it in the process are
  792. * up, this statement is down, and all following statements are
  793. * uninitialized. When a backend instance goes down, it is guaranteed,
  794. * as long as it stays down, that either this will be called or
  795. * termination will be requested with {@link NCDModule_func_die}.
  796. * The backend instance was in down state.
  797. *
  798. * @param o state pointer, as in {@link NCDModule_func_new2}
  799. */
  800. typedef void (*NCDModule_func_clean) (void *o);
  801. #define NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN (1 << 0)
  802. /**
  803. * Structure encapsulating the implementation of a module backend.
  804. */
  805. struct NCDModule {
  806. /**
  807. * If this implements a plain statement, the name of the statement.
  808. * If this implements a method, then "base_type::method_name".
  809. */
  810. const char *type;
  811. /**
  812. * The base type for methods operating on instances of this backend.
  813. * Any module with type of form "base_type::method_name" is considered
  814. * a method of instances of this backend.
  815. * If this is NULL, the base type will default to type.
  816. */
  817. const char *base_type;
  818. /**
  819. * Function called to create an new backend instance.
  820. */
  821. NCDModule_func_new2 func_new2;
  822. /**
  823. * Function called to request termination of a backend instance.
  824. * May be NULL, in which case the default is to call NCDModuleInst_Backend_Dead().
  825. */
  826. NCDModule_func_die func_die;
  827. /**
  828. * Function called to resolve a variable within the backend instance.
  829. * May be NULL.
  830. */
  831. NCDModule_func_getvar func_getvar;
  832. /**
  833. * Function called to resolve a variable within the backend instance.
  834. * May be NULL.
  835. */
  836. NCDModule_func_getvar2 func_getvar2;
  837. /**
  838. * Function called to resolve an object within the backend instance.
  839. * May be NULL.
  840. */
  841. NCDModule_func_getobj func_getobj;
  842. /**
  843. * Function called when the backend instance is in a clean state.
  844. * May be NULL.
  845. */
  846. NCDModule_func_clean func_clean;
  847. /**
  848. * Various flags.
  849. *
  850. * - NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN
  851. * Whether the interpreter is allowed to call func_getvar and func_getobj
  852. * even when the backend instance is in down state (as opposed to just
  853. * in up state.
  854. */
  855. int flags;
  856. /**
  857. * The amount of memory to preallocate for each module instance.
  858. * Preallocation can be used to avoid having to allocate memory from
  859. * module initialization. The memory can be accessed via the first
  860. * argument to {@link NCDModule_func_new2} and other calls.
  861. */
  862. int alloc_size;
  863. };
  864. /**
  865. * Structure encapsulating a group of module backend implementations,
  866. * with global init and free functions.
  867. */
  868. struct NCDModuleGroup {
  869. /**
  870. * Function called before any instance of any module backend in this
  871. * group is crated. May be NULL.
  872. */
  873. NCDModule_func_globalinit func_globalinit;
  874. /**
  875. * Function called to clean up after {@link NCDModule_func_globalinit}.
  876. * May be NULL.
  877. */
  878. NCDModule_func_globalfree func_globalfree;
  879. /**
  880. * Array of module backends. The array must be terminated with a
  881. * structure that has a NULL type member.
  882. */
  883. const struct NCDModule *modules;
  884. /**
  885. * A pointer to an array of strings which will be mapped to
  886. * {@link NCDStringIndex} string identifiers for the module to use.
  887. * The array must be terminated by NULL. The resulting string
  888. * identifiers will be available in the 'strings' member in
  889. * {@link NCDInterpModuleGroup}.
  890. */
  891. const char *const*strings;
  892. /**
  893. * A pointer to an array of global functions implemented by this module
  894. * group. The array must be terminated with a structure that has a NULL
  895. * func_name member. May be NULL.
  896. */
  897. struct NCDModuleFunction const *functions;
  898. };
  899. /**
  900. * Represents an {@link NCDModule} within an interpreter.
  901. * This structure is initialized by the interpreter when it loads a module group.
  902. */
  903. struct NCDInterpModule {
  904. /**
  905. * A copy of the original NCDModule structure,
  906. */
  907. struct NCDModule module;
  908. /**
  909. * The string identifier of this module's base_type (or type if base_type is
  910. * not specified) according to {@link NCDStringIndex}.
  911. */
  912. NCD_string_id_t base_type_id;
  913. /**
  914. * A pointer to the {@link NCDInterpModuleGroup} representing the group
  915. * this module belongs to.
  916. */
  917. struct NCDInterpModuleGroup *group;
  918. };
  919. /**
  920. * Represents an {@link NCDModuleGroup} within an interpreter.
  921. * This structure is initialized by the interpreter when it loads a module group.
  922. */
  923. struct NCDInterpModuleGroup {
  924. /**
  925. * A copy of the original NCDModuleGroup structure.
  926. */
  927. struct NCDModuleGroup group;
  928. /**
  929. * An array of string identifiers corresponding to the strings
  930. * in the 'strings' member of NCDModuleGroup. May be NULL if there
  931. * are no strings in the NCDModuleGroup.
  932. */
  933. NCD_string_id_t *strings;
  934. /**
  935. * Pointer which allows the module to keep private interpreter-wide state.
  936. * This can be freely modified by the module; the interpeter will not
  937. * read or write it.
  938. */
  939. void *group_state;
  940. };
  941. /**
  942. * An abstraction of function call evaluations, mutually decoupling the
  943. * interpreter and the function implementations.
  944. *
  945. * The function implementation is given an instance of this structure
  946. * in the evaluation callback (func_eval), and uses it to request
  947. * information and submit results. The function implementation does these
  948. * things by calling the various NCDCall functions with the NCDCall
  949. * instance. Note that function arguments are evaluated on demand from
  950. * the function implementation. This enables behavior such as
  951. * short-circuit evaluation of logical operators.
  952. *
  953. * The NCDCall struct has a value semantic - it can be copied
  954. * around freely by the function implementation during the
  955. * lifetime of the evaluation call.
  956. */
  957. typedef struct {
  958. struct NCDCall_interp_shared const *interp_shared;
  959. void *interp_user;
  960. struct NCDInterpFunction const *interp_function;
  961. size_t arg_count;
  962. NCDValMem *res_mem;
  963. NCDValRef *out_ref;
  964. } NCDCall;
  965. /**
  966. * Used by the intepreter to call a function.
  967. *
  968. * It calls the func_eval callback of the function implementation
  969. * with an appropriately initialized NCDCall value. This is the only
  970. * NCDCall related function used by the interpreter.
  971. *
  972. * As part of the call, callbacks to the interpreter (given in interp_shared)
  973. * may occur. All of these callbacks are passed interp_user as the first
  974. * argument. The callbacks are:
  975. * - logfunc (to log a message),
  976. * - func_eval_arg (to evaluate a particular function argument).
  977. *
  978. * @param interp_shared pointer to things expected to be the same for all
  979. * function calls by the interpreter. This includes interpreter callbacks.
  980. * @param interp_user pointer to be passed through to interpreter callbacks
  981. * @param interp_function the function to call. The evaluation function of
  982. * the function implementation that will be called is taken from here
  983. * (interp_function->function.func_eval), but this is also exposed to the
  984. * function implementation, so it should be initialized appropriately.
  985. * @param arg_count number of arguments passed to the function.
  986. * The function implementation is only permitted to attempt evaluation
  987. * of arguments with indices lesser than arg_count.
  988. * @param res_mem the (initialized) value memory object for the result to
  989. * be stored into. However this may also be used as storage for temporary
  990. * values computed as part of the call.
  991. * @param res_out on success, *res_out will be set to the result of the call
  992. * @return 1 on success, 0 on error
  993. */
  994. int NCDCall_DoIt (
  995. struct NCDCall_interp_shared const *interp_shared, void *interp_user,
  996. struct NCDInterpFunction const *interp_function,
  997. size_t arg_count, NCDValMem *res_mem, NCDValRef *res_out
  998. ) WARN_UNUSED;
  999. /**
  1000. * Returns a pointer to the NCDInterpFunction object for the function,
  1001. * initialized by the interpreter. This alows, among other things,
  1002. * determining which function is being called, and getting the module
  1003. * group's private data pointer.
  1004. */
  1005. struct NCDInterpFunction const * NCDCall_InterpFunction (NCDCall const *o);
  1006. /**
  1007. * Returns a pointer to an NCDModuleInst_iparams structure, exposing
  1008. * services provided by the interpreter.
  1009. */
  1010. struct NCDModuleInst_iparams const * NCDCall_Iparams (NCDCall const *o);
  1011. /**
  1012. * Returns the number of arguments for the function call.
  1013. */
  1014. size_t NCDCall_ArgCount (NCDCall const *o);
  1015. /**
  1016. * Attempts to evaluate a function argument.
  1017. *
  1018. * @param index the index of the argument to be evaluated. Must be
  1019. * in the range [0, ArgCount).
  1020. * @param mem the value memory object for the value to be stored into.
  1021. * However temporary value data may also be stored here.
  1022. * @return on success, the value reference to the argument value;
  1023. * on failure, an invalid value reference
  1024. */
  1025. NCDValRef NCDCall_EvalArg (NCDCall const *o, size_t index, NCDValMem *mem);
  1026. /**
  1027. * Returns a pointer to the value memory object that the result
  1028. * of the call should be stored into. The memory object may also
  1029. * be used to store temporary value data.
  1030. */
  1031. NCDValMem * NCDCall_ResMem (NCDCall const *o);
  1032. /**
  1033. * Provides result value of the evaluation to the interpreter.
  1034. * Note that this only stores the result without any immediate
  1035. * action.
  1036. *
  1037. * Passing an invalid value reference indicates failure of the
  1038. * call, though failure is also assumed if this function is
  1039. * not called at all during the call. When a non-invalid
  1040. * value reference is passed (indicating success), it must point
  1041. * to a value stored within the memory object returned by
  1042. * NCDCall_ResMem.
  1043. *
  1044. * @param ref the result value for the call, or an invalid
  1045. * value reference to indicate failure of the call
  1046. */
  1047. void NCDCall_SetResult (NCDCall const *o, NCDValRef ref);
  1048. /**
  1049. * Returns a log context that can be used to log messages
  1050. * associated with the call.
  1051. */
  1052. BLogContext NCDCall_LogContext (NCDCall const *o);
  1053. /**
  1054. * A structure initialized by the interpreter and passed
  1055. * to NCDCall_DoIt for function evaluations.
  1056. * It contains a pointer to things expected to be the same for all
  1057. * function calls by the interpreter, so it can be initialized once
  1058. * and not for every call.
  1059. */
  1060. struct NCDCall_interp_shared {
  1061. /**
  1062. * A callack for log messages originating from the function call.
  1063. * The first argument is the interp_user argument to NCDCall_DoIt.
  1064. */
  1065. BLog_logfunc logfunc;
  1066. /**
  1067. * A callback for evaluating function arguments.
  1068. *
  1069. * @param user interpreter private data (the interp_user argument
  1070. * to NCDCall_DoIt)
  1071. * @param index the index of the argument to be evaluated.
  1072. * This will be in the range [0, arg_count).
  1073. * @param mem the value memory object where the result of the
  1074. * evaluation should be stored. Temporary value data may also
  1075. * be stored here.
  1076. * @param out on success, *out should be set to the value reference
  1077. * to the result of the evaluation. An invalid value reference is
  1078. * permitted, in which case failure is assumed.
  1079. * @return 1 on success (but see above), 0 on failure
  1080. */
  1081. int (*func_eval_arg) (void *user, size_t index, NCDValMem *mem, NCDValRef *out);
  1082. /**
  1083. * A pointer to an NCDModuleInst_iparams structure, exposing
  1084. * services provided by the interpreter.
  1085. */
  1086. struct NCDModuleInst_iparams const *iparams;
  1087. };
  1088. /**
  1089. * This structure is initialized statically by a function
  1090. * implementation to describe the function and provide
  1091. * the resources required for its evaluation by the interpreter.
  1092. *
  1093. * These structures always appear in arrays, pointed to by
  1094. * the functions pointer in the NCDModuleGroup structure.
  1095. */
  1096. struct NCDModuleFunction {
  1097. /**
  1098. * The name of the function.
  1099. * NULL to terminate the array of functions.
  1100. */
  1101. char const *func_name;
  1102. /**
  1103. * Callback for evaluating the function.
  1104. */
  1105. void (*func_eval) (NCDCall call);
  1106. };
  1107. /**
  1108. * Represents an {@link NCDModuleFunction} within an interpreter.
  1109. * This structure is initialized by the interpreter when it loads a module group.
  1110. */
  1111. struct NCDInterpFunction {
  1112. /**
  1113. * A copy of the original NCDModuleFunction structure.
  1114. * We could just use a pointer to the original statically defined structure,
  1115. * but then we wouldn't be annoying the premature-optimization hipsters.
  1116. */
  1117. struct NCDModuleFunction function;
  1118. /**
  1119. * The string identifier of this functions's name. according to
  1120. * {@link NCDStringIndex}.
  1121. */
  1122. NCD_string_id_t func_name_id;
  1123. /**
  1124. * A pointer to the {@link NCDInterpModuleGroup} representing the group
  1125. * this function belongs to.
  1126. */
  1127. struct NCDInterpModuleGroup *group;
  1128. };
  1129. #endif