NCDModule.h 44 KB

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