value.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  1. /**
  2. * @file value.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Synopsis:
  32. * value(value)
  33. * value value::get(where)
  34. * value value::try_get(where)
  35. * value value::getpath(list path)
  36. * value value::insert(where, what)
  37. * value value::insert_undo(where, what)
  38. *
  39. * Description:
  40. * Value objects allow examining and manipulating values.
  41. *
  42. * value(value) constructs a new value object from the given value.
  43. *
  44. * value::get(where) constructs a value object for the element at position 'where'
  45. * (for a list), or the value corresponding to key 'where' (for a map). It is an
  46. * error if the base value is not a list or a map, the index is out of bounds of
  47. * the list, or the key does not exist in the map.
  48. * The resulting value object is NOT a copy, and shares (part of) the same
  49. * underlying value structure as the base value object. Deleting it will remove
  50. * it from the list or map it is part of.
  51. *
  52. * value::try_get(where) is like get(), except that if any restriction on 'where'
  53. * is violated, no error is triggered; instead, the value object is constructed
  54. * as being deleted; this state is exposed via the 'exists' variable.
  55. * This can be used to check for the presence of a key in a map, and in case it
  56. * exists, allow access to the corresponding value without another get() statement.
  57. *
  58. * value::getpath(path) is like get(), except that it performs multiple
  59. * consecutive resolutions. Also, if the path is an empty list, it performs
  60. * no resulution at all.
  61. *
  62. * value::insert(where, what) constructs a value object by inserting into an
  63. * existing value object.
  64. * For lists, 'where' is the index of the element to insert before, or the length
  65. * of the list to append to it.
  66. * For maps, 'where' is the key to insert under. If the key already exists in the
  67. * map, its value is replaced; any references to the old value however remain valid.
  68. *
  69. * value::insert_undo(where, what) is like insert(), except that, on
  70. * deinitialization, it attempts to revert the value to the original state.
  71. * It does this by taking a reference to the old value at 'where' (if any) and
  72. * before inserting the new value 'what' to that location. On deinitialization,
  73. * it removes the value that it inserted from its parent and inserts the stored
  74. * referenced value in its place, assuming this is possible (the inserted value
  75. * has not been deleted and has a parent at deinitialization time).
  76. *
  77. * Variables:
  78. * (empty) - the value stored in the value object
  79. * type - type of the value; "string", "list" or "map"
  80. * length - number of elements in the list or map, or the number of bytes in a
  81. * string
  82. * keys - a list of keys in the map (only if the value is a map)
  83. * exists - "true" or "false", reflecting whether the value object holds a value
  84. * (is not in deleted state)
  85. *
  86. * Synopsis:
  87. * value::remove(where)
  88. * value::delete()
  89. *
  90. * Description:
  91. * value::remove(where) removes from an existing value object.
  92. * For lists, 'where' is the index of the element to remove, and must be in range.
  93. * For maps, 'where' is the key to remove, and must be an existing key.
  94. * In any case, any references to the removed value remain valid.
  95. *
  96. * value::delete() deletes the underlying value data of this value object.
  97. * After delection, the value object enters a deleted state, which will cause any
  98. * operation on it to fail. Any other value objects which referred to the same value
  99. * or parts of it will too enter deleted state. If the value was an element
  100. * in a list or map, is is removed from it.
  101. *
  102. * Synopsis:
  103. * value value::substr(string start [, string length])
  104. *
  105. * Description:
  106. * Constructs a string value by extracting a part of a string.
  107. * 'start' specifies the index of the character (from zero) where the substring to
  108. * extract starts, and must be <= the length of the string.
  109. * 'length' specifies the maximum number of characters to extract, if given.
  110. * The newly constructed value is a copy of the extracted substring.
  111. * The value must be a string value.
  112. */
  113. #include <stdlib.h>
  114. #include <string.h>
  115. #include <stddef.h>
  116. #include <limits.h>
  117. #include <inttypes.h>
  118. #include <misc/offset.h>
  119. #include <misc/debug.h>
  120. #include <misc/parse_number.h>
  121. #include <structure/LinkedList0.h>
  122. #include <structure/IndexedList.h>
  123. #include <structure/BCountAVL.h>
  124. #include <ncd/NCDModule.h>
  125. #include <generated/blog_channel_ncd_value.h>
  126. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  127. struct value;
  128. struct valref {
  129. struct value *v;
  130. LinkedList0Node refs_list_node;
  131. };
  132. typedef void (*value_deinit_func) (void *deinit_data, NCDModuleInst *i);
  133. struct instance {
  134. NCDModuleInst *i;
  135. struct valref ref;
  136. value_deinit_func deinit_func;
  137. void *deinit_data;
  138. };
  139. struct value {
  140. LinkedList0 refs_list;
  141. struct value *parent;
  142. union {
  143. struct {
  144. IndexedListNode list_contents_il_node;
  145. } list_parent;
  146. struct {
  147. NCDValMem key_mem;
  148. NCDValRef key;
  149. BCountAVLNode map_contents_tree_node;
  150. } map_parent;
  151. };
  152. int type;
  153. union {
  154. struct {
  155. uint8_t *string;
  156. size_t length;
  157. } string;
  158. struct {
  159. IndexedList list_contents_il;
  160. } list;
  161. struct {
  162. BCountAVL map_contents_tree;
  163. } map;
  164. };
  165. };
  166. static int ncdval_comparator (void *unused, void *vv1, void *vv2);
  167. static const char * get_type_str (int type);
  168. static void value_cleanup (struct value *v);
  169. static void value_delete (struct value *v);
  170. static struct value * value_init_string (NCDModuleInst *i, const uint8_t *str, size_t len);
  171. static struct value * value_init_list (NCDModuleInst *i);
  172. static size_t value_list_len (struct value *v);
  173. static struct value * value_list_at (struct value *v, size_t index);
  174. static size_t value_list_indexof (struct value *v, struct value *ev);
  175. static int value_list_insert (NCDModuleInst *i, struct value *list, struct value *v, size_t index);
  176. static void value_list_remove (struct value *list, struct value *v);
  177. static struct value * value_init_map (NCDModuleInst *i);
  178. static size_t value_map_len (struct value *map);
  179. static struct value * value_map_at (struct value *map, size_t index);
  180. static struct value * value_map_find (struct value *map, NCDValRef key);
  181. static int value_map_insert (struct value *map, struct value *v, NCDValMem mem, NCDValSafeRef key, NCDModuleInst *i);
  182. static void value_map_remove (struct value *map, struct value *v);
  183. static void value_map_remove2 (struct value *map, struct value *v, NCDValMem *out_mem, NCDValSafeRef *out_key);
  184. static struct value * value_init_fromvalue (NCDModuleInst *i, NCDValRef value);
  185. static int value_to_value (NCDModuleInst *i, struct value *v, NCDValMem *mem, NCDValRef *out_value);
  186. static struct value * value_get (NCDModuleInst *i, struct value *v, NCDValRef where, int no_error);
  187. static struct value * value_get_path (NCDModuleInst *i, struct value *v, NCDValRef path);
  188. static struct value * value_insert (NCDModuleInst *i, struct value *v, NCDValRef where, NCDValRef what, struct value **out_oldv);
  189. static int value_remove (NCDModuleInst *i, struct value *v, NCDValRef where);
  190. static void valref_init (struct valref *r, struct value *v);
  191. static void valref_free (struct valref *r);
  192. static struct value * valref_val (struct valref *r);
  193. static void valref_break (struct valref *r);
  194. static int ncdval_comparator (void *unused, void *vv1, void *vv2)
  195. {
  196. NCDValRef *v1 = vv1;
  197. NCDValRef *v2 = vv2;
  198. return NCDVal_Compare(*v1, *v2);
  199. }
  200. static const char * get_type_str (int type)
  201. {
  202. switch (type) {
  203. case NCDVAL_STRING: return "string";
  204. case NCDVAL_LIST: return "list";
  205. case NCDVAL_MAP: return "map";
  206. }
  207. ASSERT(0)
  208. return NULL;
  209. }
  210. static void value_cleanup (struct value *v)
  211. {
  212. if (v->parent || !LinkedList0_IsEmpty(&v->refs_list)) {
  213. return;
  214. }
  215. switch (v->type) {
  216. case NCDVAL_STRING: {
  217. free(v->string.string);
  218. } break;
  219. case NCDVAL_LIST: {
  220. while (value_list_len(v) > 0) {
  221. struct value *ev = value_list_at(v, 0);
  222. value_list_remove(v, ev);
  223. value_cleanup(ev);
  224. }
  225. } break;
  226. case NCDVAL_MAP: {
  227. while (value_map_len(v) > 0) {
  228. struct value *ev = value_map_at(v, 0);
  229. value_map_remove(v, ev);
  230. value_cleanup(ev);
  231. }
  232. } break;
  233. default: ASSERT(0);
  234. }
  235. free(v);
  236. }
  237. static void value_delete (struct value *v)
  238. {
  239. if (v->parent) {
  240. switch (v->parent->type) {
  241. case NCDVAL_LIST: {
  242. value_list_remove(v->parent, v);
  243. } break;
  244. case NCDVAL_MAP: {
  245. value_map_remove(v->parent, v);
  246. } break;
  247. default: ASSERT(0);
  248. }
  249. }
  250. LinkedList0Node *ln;
  251. while (ln = LinkedList0_GetFirst(&v->refs_list)) {
  252. struct valref *r = UPPER_OBJECT(ln, struct valref, refs_list_node);
  253. ASSERT(r->v == v)
  254. valref_break(r);
  255. }
  256. switch (v->type) {
  257. case NCDVAL_STRING: {
  258. free(v->string.string);
  259. } break;
  260. case NCDVAL_LIST: {
  261. while (value_list_len(v) > 0) {
  262. struct value *ev = value_list_at(v, 0);
  263. value_delete(ev);
  264. }
  265. } break;
  266. case NCDVAL_MAP: {
  267. while (value_map_len(v) > 0) {
  268. struct value *ev = value_map_at(v, 0);
  269. value_delete(ev);
  270. }
  271. } break;
  272. default: ASSERT(0);
  273. }
  274. free(v);
  275. }
  276. static struct value * value_init_string (NCDModuleInst *i, const uint8_t *str, size_t len)
  277. {
  278. struct value *v = malloc(sizeof(*v));
  279. if (!v) {
  280. ModuleLog(i, BLOG_ERROR, "malloc failed");
  281. goto fail0;
  282. }
  283. LinkedList0_Init(&v->refs_list);
  284. v->parent = NULL;
  285. v->type = NCDVAL_STRING;
  286. if (!(v->string.string = malloc(len))) {
  287. ModuleLog(i, BLOG_ERROR, "malloc failed");
  288. goto fail1;
  289. }
  290. memcpy(v->string.string, str, len);
  291. v->string.length = len;
  292. return v;
  293. fail1:
  294. free(v);
  295. fail0:
  296. return NULL;
  297. }
  298. static struct value * value_init_list (NCDModuleInst *i)
  299. {
  300. struct value *v = malloc(sizeof(*v));
  301. if (!v) {
  302. ModuleLog(i, BLOG_ERROR, "malloc failed");
  303. return NULL;
  304. }
  305. LinkedList0_Init(&v->refs_list);
  306. v->parent = NULL;
  307. v->type = NCDVAL_LIST;
  308. IndexedList_Init(&v->list.list_contents_il);
  309. return v;
  310. }
  311. static size_t value_list_len (struct value *v)
  312. {
  313. ASSERT(v->type == NCDVAL_LIST)
  314. return IndexedList_Count(&v->list.list_contents_il);
  315. }
  316. static struct value * value_list_at (struct value *v, size_t index)
  317. {
  318. ASSERT(v->type == NCDVAL_LIST)
  319. ASSERT(index < value_list_len(v))
  320. IndexedListNode *iln = IndexedList_GetAt(&v->list.list_contents_il, index);
  321. ASSERT(iln)
  322. struct value *e = UPPER_OBJECT(iln, struct value, list_parent.list_contents_il_node);
  323. ASSERT(e->parent == v)
  324. return e;
  325. }
  326. static size_t value_list_indexof (struct value *v, struct value *ev)
  327. {
  328. ASSERT(v->type == NCDVAL_LIST)
  329. ASSERT(ev->parent == v)
  330. uint64_t index = IndexedList_IndexOf(&v->list.list_contents_il, &ev->list_parent.list_contents_il_node);
  331. ASSERT(index < value_list_len(v))
  332. return index;
  333. }
  334. static int value_list_insert (NCDModuleInst *i, struct value *list, struct value *v, size_t index)
  335. {
  336. ASSERT(list->type == NCDVAL_LIST)
  337. ASSERT(!v->parent)
  338. ASSERT(index <= value_list_len(list))
  339. if (value_list_len(list) == SIZE_MAX) {
  340. ModuleLog(i, BLOG_ERROR, "list has too many elements");
  341. return 0;
  342. }
  343. IndexedList_InsertAt(&list->list.list_contents_il, &v->list_parent.list_contents_il_node, index);
  344. v->parent = list;
  345. return 1;
  346. }
  347. static void value_list_remove (struct value *list, struct value *v)
  348. {
  349. ASSERT(list->type == NCDVAL_LIST)
  350. ASSERT(v->parent == list)
  351. IndexedList_Remove(&list->list.list_contents_il, &v->list_parent.list_contents_il_node);
  352. v->parent = NULL;
  353. }
  354. static struct value * value_init_map (NCDModuleInst *i)
  355. {
  356. struct value *v = malloc(sizeof(*v));
  357. if (!v) {
  358. ModuleLog(i, BLOG_ERROR, "malloc failed");
  359. return NULL;
  360. }
  361. LinkedList0_Init(&v->refs_list);
  362. v->parent = NULL;
  363. v->type = NCDVAL_MAP;
  364. BCountAVL_Init(&v->map.map_contents_tree, OFFSET_DIFF(struct value, map_parent.key, map_parent.map_contents_tree_node), ncdval_comparator, NULL);
  365. return v;
  366. }
  367. static size_t value_map_len (struct value *map)
  368. {
  369. ASSERT(map->type == NCDVAL_MAP)
  370. return BCountAVL_Count(&map->map.map_contents_tree);
  371. }
  372. static struct value * value_map_at (struct value *map, size_t index)
  373. {
  374. ASSERT(map->type == NCDVAL_MAP)
  375. ASSERT(index < value_map_len(map))
  376. BCountAVLNode *tn = BCountAVL_GetAt(&map->map.map_contents_tree, index);
  377. ASSERT(tn)
  378. struct value *e = UPPER_OBJECT(tn, struct value, map_parent.map_contents_tree_node);
  379. ASSERT(e->parent == map)
  380. return e;
  381. }
  382. static struct value * value_map_find (struct value *map, NCDValRef key)
  383. {
  384. ASSERT(map->type == NCDVAL_MAP)
  385. ASSERT(NCDVal_Type(key))
  386. BCountAVLNode *tn = BCountAVL_LookupExact(&map->map.map_contents_tree, &key);
  387. if (!tn) {
  388. return NULL;
  389. }
  390. struct value *e = UPPER_OBJECT(tn, struct value, map_parent.map_contents_tree_node);
  391. ASSERT(e->parent == map)
  392. return e;
  393. }
  394. static int value_map_insert (struct value *map, struct value *v, NCDValMem mem, NCDValSafeRef key, NCDModuleInst *i)
  395. {
  396. ASSERT(map->type == NCDVAL_MAP)
  397. ASSERT(!v->parent)
  398. ASSERT((NCDVal_Type(NCDVal_FromSafe(&mem, key)), 1))
  399. ASSERT(!value_map_find(map, NCDVal_FromSafe(&mem, key)))
  400. if (value_map_len(map) == SIZE_MAX) {
  401. ModuleLog(i, BLOG_ERROR, "map has too many elements");
  402. return 0;
  403. }
  404. v->map_parent.key_mem = mem;
  405. v->map_parent.key = NCDVal_FromSafe(&v->map_parent.key_mem, key);
  406. int res = BCountAVL_Insert(&map->map.map_contents_tree, &v->map_parent.map_contents_tree_node, NULL);
  407. ASSERT(res)
  408. v->parent = map;
  409. return 1;
  410. }
  411. static void value_map_remove (struct value *map, struct value *v)
  412. {
  413. ASSERT(map->type == NCDVAL_MAP)
  414. ASSERT(v->parent == map)
  415. BCountAVL_Remove(&map->map.map_contents_tree, &v->map_parent.map_contents_tree_node);
  416. NCDValMem_Free(&v->map_parent.key_mem);
  417. v->parent = NULL;
  418. }
  419. static void value_map_remove2 (struct value *map, struct value *v, NCDValMem *out_mem, NCDValSafeRef *out_key)
  420. {
  421. ASSERT(map->type == NCDVAL_MAP)
  422. ASSERT(v->parent == map)
  423. ASSERT(out_mem)
  424. ASSERT(out_key)
  425. BCountAVL_Remove(&map->map.map_contents_tree, &v->map_parent.map_contents_tree_node);
  426. *out_mem = v->map_parent.key_mem;
  427. *out_key = NCDVal_ToSafe(v->map_parent.key);
  428. v->parent = NULL;
  429. }
  430. static struct value * value_init_fromvalue (NCDModuleInst *i, NCDValRef value)
  431. {
  432. ASSERT((NCDVal_Type(value), 1))
  433. struct value *v;
  434. switch (NCDVal_Type(value)) {
  435. case NCDVAL_STRING: {
  436. if (!(v = value_init_string(i, (const uint8_t *)NCDVal_StringValue(value), NCDVal_StringLength(value)))) {
  437. goto fail0;
  438. }
  439. } break;
  440. case NCDVAL_LIST: {
  441. if (!(v = value_init_list(i))) {
  442. goto fail0;
  443. }
  444. size_t count = NCDVal_ListCount(value);
  445. for (size_t j = 0; j < count; j++) {
  446. struct value *ev = value_init_fromvalue(i, NCDVal_ListGet(value, j));
  447. if (!ev) {
  448. goto fail1;
  449. }
  450. if (!value_list_insert(i, v, ev, value_list_len(v))) {
  451. value_cleanup(ev);
  452. goto fail1;
  453. }
  454. }
  455. } break;
  456. case NCDVAL_MAP: {
  457. if (!(v = value_init_map(i))) {
  458. goto fail0;
  459. }
  460. for (NCDValMapElem e = NCDVal_MapFirst(value); !NCDVal_MapElemInvalid(e); e = NCDVal_MapNext(value, e)) {
  461. NCDValRef ekey = NCDVal_MapElemKey(value, e);
  462. NCDValRef eval = NCDVal_MapElemVal(value, e);
  463. NCDValMem key_mem;
  464. NCDValMem_Init(&key_mem);
  465. NCDValRef key = NCDVal_NewCopy(&key_mem, ekey);
  466. if (NCDVal_IsInvalid(key)) {
  467. BLog(BLOG_ERROR, "NCDVal_NewCopy failed");
  468. NCDValMem_Free(&key_mem);
  469. goto fail1;
  470. }
  471. struct value *ev = value_init_fromvalue(i, eval);
  472. if (!ev) {
  473. NCDValMem_Free(&key_mem);
  474. goto fail1;
  475. }
  476. if (!value_map_insert(v, ev, key_mem, NCDVal_ToSafe(key), i)) {
  477. NCDValMem_Free(&key_mem);
  478. value_cleanup(ev);
  479. goto fail1;
  480. }
  481. }
  482. } break;
  483. default: ASSERT(0);
  484. }
  485. return v;
  486. fail1:
  487. value_cleanup(v);
  488. fail0:
  489. return NULL;
  490. }
  491. static int value_to_value (NCDModuleInst *i, struct value *v, NCDValMem *mem, NCDValRef *out_value)
  492. {
  493. ASSERT(mem)
  494. ASSERT(out_value)
  495. switch (v->type) {
  496. case NCDVAL_STRING: {
  497. *out_value = NCDVal_NewStringBin(mem, v->string.string, v->string.length);
  498. if (NCDVal_IsInvalid(*out_value)) {
  499. ModuleLog(i, BLOG_ERROR, "NCDVal_NewStringBin failed");
  500. goto fail;
  501. }
  502. } break;
  503. case NCDVAL_LIST: {
  504. *out_value = NCDVal_NewList(mem, value_list_len(v));
  505. if (NCDVal_IsInvalid(*out_value)) {
  506. ModuleLog(i, BLOG_ERROR, "NCDVal_NewList failed");
  507. goto fail;
  508. }
  509. for (size_t index = 0; index < value_list_len(v); index++) {
  510. NCDValRef eval;
  511. if (!value_to_value(i, value_list_at(v, index), mem, &eval)) {
  512. goto fail;
  513. }
  514. NCDVal_ListAppend(*out_value, eval);
  515. }
  516. } break;
  517. case NCDVAL_MAP: {
  518. *out_value = NCDVal_NewMap(mem, value_map_len(v));
  519. if (NCDVal_IsInvalid(*out_value)) {
  520. ModuleLog(i, BLOG_ERROR, "NCDVal_NewMap failed");
  521. goto fail;
  522. }
  523. for (size_t index = 0; index < value_map_len(v); index++) {
  524. struct value *ev = value_map_at(v, index);
  525. NCDValRef key = NCDVal_NewCopy(mem, ev->map_parent.key);
  526. if (NCDVal_IsInvalid(key)) {
  527. ModuleLog(i, BLOG_ERROR, "NCDVal_NewCopy failed");
  528. goto fail;
  529. }
  530. NCDValRef val;
  531. if (!value_to_value(i, ev, mem, &val)) {
  532. goto fail;
  533. }
  534. int res = NCDVal_MapInsert(*out_value, key, val);
  535. ASSERT(res)
  536. }
  537. } break;
  538. default: ASSERT(0);
  539. }
  540. return 1;
  541. fail:
  542. return 0;
  543. }
  544. static struct value * value_get (NCDModuleInst *i, struct value *v, NCDValRef where, int no_error)
  545. {
  546. ASSERT((NCDVal_Type(where), 1))
  547. switch (v->type) {
  548. case NCDVAL_STRING: {
  549. if (!no_error) ModuleLog(i, BLOG_ERROR, "cannot resolve into a string");
  550. goto fail;
  551. } break;
  552. case NCDVAL_LIST: {
  553. uintmax_t index;
  554. if (!NCDVal_IsStringNoNulls(where) || !parse_unsigned_integer(NCDVal_StringValue(where), &index)) {
  555. if (!no_error) ModuleLog(i, BLOG_ERROR, "index is not a valid number (resolving into list)");
  556. goto fail;
  557. }
  558. if (index >= value_list_len(v)) {
  559. if (!no_error) ModuleLog(i, BLOG_ERROR, "index is out of bounds (resolving into list)");
  560. goto fail;
  561. }
  562. v = value_list_at(v, index);
  563. } break;
  564. case NCDVAL_MAP: {
  565. v = value_map_find(v, where);
  566. if (!v) {
  567. if (!no_error) ModuleLog(i, BLOG_ERROR, "key does not exist (resolving into map)");
  568. goto fail;
  569. }
  570. } break;
  571. default: ASSERT(0);
  572. }
  573. return v;
  574. fail:
  575. return NULL;
  576. }
  577. static struct value * value_get_path (NCDModuleInst *i, struct value *v, NCDValRef path)
  578. {
  579. ASSERT(NCDVal_IsList(path))
  580. size_t count = NCDVal_ListCount(path);
  581. for (size_t j = 0; j < count; j++) {
  582. if (!(v = value_get(i, v, NCDVal_ListGet(path, j), 0))) {
  583. goto fail;
  584. }
  585. }
  586. return v;
  587. fail:
  588. return NULL;
  589. }
  590. static struct value * value_insert (NCDModuleInst *i, struct value *v, NCDValRef where, NCDValRef what, struct value **out_oldv)
  591. {
  592. ASSERT(v)
  593. ASSERT((NCDVal_Type(where), 1))
  594. ASSERT((NCDVal_Type(what), 1))
  595. struct value *nv = value_init_fromvalue(i, what);
  596. if (!nv) {
  597. goto fail0;
  598. }
  599. struct value *oldv = NULL;
  600. switch (v->type) {
  601. case NCDVAL_STRING: {
  602. ModuleLog(i, BLOG_ERROR, "cannot insert into a string");
  603. goto fail1;
  604. } break;
  605. case NCDVAL_LIST: {
  606. uintmax_t index;
  607. if (!NCDVal_IsStringNoNulls(where) || !parse_unsigned_integer(NCDVal_StringValue(where), &index)) {
  608. ModuleLog(i, BLOG_ERROR, "index is not a valid number (inserting into list)");
  609. goto fail1;
  610. }
  611. if (index > value_list_len(v)) {
  612. ModuleLog(i, BLOG_ERROR, "index is out of bounds (inserting into list)");
  613. goto fail1;
  614. }
  615. if (!value_list_insert(i, v, nv, index)) {
  616. goto fail1;
  617. }
  618. } break;
  619. case NCDVAL_MAP: {
  620. oldv = value_map_find(v, where);
  621. if (!oldv && value_map_len(v) == SIZE_MAX) {
  622. ModuleLog(i, BLOG_ERROR, "map has too many elements");
  623. goto fail1;
  624. }
  625. NCDValMem key_mem;
  626. NCDValMem_Init(&key_mem);
  627. NCDValRef key = NCDVal_NewCopy(&key_mem, where);
  628. if (NCDVal_IsInvalid(key)) {
  629. ModuleLog(i, BLOG_ERROR, "NCDVal_NewCopy failed");
  630. NCDValMem_Free(&key_mem);
  631. goto fail1;
  632. }
  633. if (oldv) {
  634. value_map_remove(v, oldv);
  635. }
  636. int res = value_map_insert(v, nv, key_mem, NCDVal_ToSafe(key), i);
  637. ASSERT(res)
  638. } break;
  639. default: ASSERT(0);
  640. }
  641. if (out_oldv) {
  642. *out_oldv = oldv;
  643. }
  644. else if (oldv) {
  645. value_cleanup(oldv);
  646. }
  647. return nv;
  648. fail1:
  649. value_cleanup(nv);
  650. fail0:
  651. return NULL;
  652. }
  653. static int value_remove (NCDModuleInst *i, struct value *v, NCDValRef where)
  654. {
  655. ASSERT(v)
  656. ASSERT((NCDVal_Type(where), 1))
  657. switch (v->type) {
  658. case NCDVAL_STRING: {
  659. ModuleLog(i, BLOG_ERROR, "cannot remove from a string");
  660. goto fail;
  661. } break;
  662. case NCDVAL_LIST: {
  663. uintmax_t index;
  664. if (!NCDVal_IsStringNoNulls(where) || !parse_unsigned_integer(NCDVal_StringValue(where), &index)) {
  665. ModuleLog(i, BLOG_ERROR, "index is not a valid number (removing from list)");
  666. goto fail;
  667. }
  668. if (index >= value_list_len(v)) {
  669. ModuleLog(i, BLOG_ERROR, "index is out of bounds (removing from list)");
  670. goto fail;
  671. }
  672. struct value *ov = value_list_at(v, index);
  673. value_list_remove(v, ov);
  674. value_cleanup(ov);
  675. } break;
  676. case NCDVAL_MAP: {
  677. struct value *ov = value_map_find(v, where);
  678. if (!ov) {
  679. ModuleLog(i, BLOG_ERROR, "key does not exist (removing from map)");
  680. goto fail;
  681. }
  682. value_map_remove(v, ov);
  683. value_cleanup(ov);
  684. } break;
  685. default: ASSERT(0);
  686. }
  687. return 1;
  688. fail:
  689. return 0;
  690. }
  691. static void valref_init (struct valref *r, struct value *v)
  692. {
  693. r->v = v;
  694. if (v) {
  695. LinkedList0_Prepend(&v->refs_list, &r->refs_list_node);
  696. }
  697. }
  698. static void valref_free (struct valref *r)
  699. {
  700. if (r->v) {
  701. LinkedList0_Remove(&r->v->refs_list, &r->refs_list_node);
  702. value_cleanup(r->v);
  703. }
  704. }
  705. static struct value * valref_val (struct valref *r)
  706. {
  707. return r->v;
  708. }
  709. static void valref_break (struct valref *r)
  710. {
  711. ASSERT(r->v)
  712. LinkedList0_Remove(&r->v->refs_list, &r->refs_list_node);
  713. r->v = NULL;
  714. }
  715. static void func_new_common (void *vo, NCDModuleInst *i, struct value *v, value_deinit_func deinit_func, void *deinit_data)
  716. {
  717. struct instance *o = vo;
  718. o->i = i;
  719. // init value references
  720. valref_init(&o->ref, v);
  721. // remember deinit
  722. o->deinit_func = deinit_func;
  723. o->deinit_data = deinit_data;
  724. NCDModuleInst_Backend_Up(i);
  725. return;
  726. }
  727. static void func_die (void *vo)
  728. {
  729. struct instance *o = vo;
  730. // deinit
  731. if (o->deinit_func) {
  732. o->deinit_func(o->deinit_data, o->i);
  733. }
  734. // free value reference
  735. valref_free(&o->ref);
  736. NCDModuleInst_Backend_Dead(o->i);
  737. }
  738. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  739. {
  740. struct instance *o = vo;
  741. struct value *v = valref_val(&o->ref);
  742. if (!strcmp(name, "exists")) {
  743. const char *str = v ? "true" : "false";
  744. *out = NCDVal_NewString(mem, str);
  745. if (NCDVal_IsInvalid(*out)) {
  746. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  747. }
  748. return 1;
  749. }
  750. if (strcmp(name, "type") && strcmp(name, "length") && strcmp(name, "keys") && strcmp(name, "")) {
  751. return 0;
  752. }
  753. if (!v) {
  754. ModuleLog(o->i, BLOG_ERROR, "value was deleted");
  755. return 0;
  756. }
  757. if (!strcmp(name, "type")) {
  758. *out = NCDVal_NewString(mem, get_type_str(v->type));
  759. if (NCDVal_IsInvalid(*out)) {
  760. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  761. }
  762. }
  763. else if (!strcmp(name, "length")) {
  764. size_t len;
  765. switch (v->type) {
  766. case NCDVAL_LIST:
  767. len = value_list_len(v);
  768. break;
  769. case NCDVAL_MAP:
  770. len = value_map_len(v);
  771. break;
  772. case NCDVAL_STRING:
  773. len = v->string.length;
  774. break;
  775. default:
  776. ASSERT(0);
  777. }
  778. char str[64];
  779. snprintf(str, sizeof(str), "%zu", len);
  780. *out = NCDVal_NewString(mem, str);
  781. if (NCDVal_IsInvalid(*out)) {
  782. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  783. }
  784. }
  785. else if (!strcmp(name, "keys")) {
  786. if (v->type != NCDVAL_MAP) {
  787. ModuleLog(o->i, BLOG_ERROR, "value is not a map (reading keys variable)");
  788. return 0;
  789. }
  790. *out = NCDVal_NewList(mem, value_map_len(v));
  791. if (NCDVal_IsInvalid(*out)) {
  792. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewList failed");
  793. goto fail;
  794. }
  795. for (size_t j = 0; j < value_map_len(v); j++) {
  796. struct value *ev = value_map_at(v, j);
  797. NCDValRef key = NCDVal_NewCopy(mem, ev->map_parent.key);
  798. if (NCDVal_IsInvalid(key)) {
  799. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
  800. goto fail;
  801. }
  802. NCDVal_ListAppend(*out, key);
  803. }
  804. }
  805. else if (!strcmp(name, "")) {
  806. if (!value_to_value(o->i, v, mem, out)) {
  807. return 0;
  808. }
  809. }
  810. else {
  811. ASSERT(0);
  812. }
  813. return 1;
  814. fail:
  815. *out = NCDVal_NewInvalid();
  816. return 1;
  817. }
  818. static void func_new_value (void *vo, NCDModuleInst *i)
  819. {
  820. NCDValRef value_arg;
  821. if (!NCDVal_ListRead(i->args, 1, &value_arg)) {
  822. ModuleLog(i, BLOG_ERROR, "wrong arity");
  823. goto fail0;
  824. }
  825. struct value *v = value_init_fromvalue(i, value_arg);
  826. if (!v) {
  827. goto fail0;
  828. }
  829. func_new_common(vo, i, v, NULL, NULL);
  830. return;
  831. fail0:
  832. NCDModuleInst_Backend_SetError(i);
  833. NCDModuleInst_Backend_Dead(i);
  834. }
  835. static void func_new_get (void *vo, NCDModuleInst *i)
  836. {
  837. NCDValRef where_arg;
  838. if (!NCDVal_ListRead(i->args, 1, &where_arg)) {
  839. ModuleLog(i, BLOG_ERROR, "wrong arity");
  840. goto fail0;
  841. }
  842. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  843. struct value *mov = valref_val(&mo->ref);
  844. if (!mov) {
  845. ModuleLog(i, BLOG_ERROR, "value was deleted");
  846. goto fail0;
  847. }
  848. struct value *v = value_get(i, mov, where_arg, 0);
  849. if (!v) {
  850. goto fail0;
  851. }
  852. func_new_common(vo, i, v, NULL, NULL);
  853. return;
  854. fail0:
  855. NCDModuleInst_Backend_SetError(i);
  856. NCDModuleInst_Backend_Dead(i);
  857. }
  858. static void func_new_try_get (void *vo, NCDModuleInst *i)
  859. {
  860. NCDValRef where_arg;
  861. if (!NCDVal_ListRead(i->args, 1, &where_arg)) {
  862. ModuleLog(i, BLOG_ERROR, "wrong arity");
  863. goto fail0;
  864. }
  865. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  866. struct value *mov = valref_val(&mo->ref);
  867. if (!mov) {
  868. ModuleLog(i, BLOG_ERROR, "value was deleted");
  869. goto fail0;
  870. }
  871. struct value *v = value_get(i, mov, where_arg, 1);
  872. func_new_common(vo, i, v, NULL, NULL);
  873. return;
  874. fail0:
  875. NCDModuleInst_Backend_SetError(i);
  876. NCDModuleInst_Backend_Dead(i);
  877. }
  878. static void func_new_getpath (void *vo, NCDModuleInst *i)
  879. {
  880. NCDValRef path_arg;
  881. if (!NCDVal_ListRead(i->args, 1, &path_arg)) {
  882. ModuleLog(i, BLOG_ERROR, "wrong arity");
  883. goto fail0;
  884. }
  885. if (!NCDVal_IsList(path_arg)) {
  886. ModuleLog(i, BLOG_ERROR, "wrong type");
  887. goto fail0;
  888. }
  889. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  890. struct value *mov = valref_val(&mo->ref);
  891. if (!mov) {
  892. ModuleLog(i, BLOG_ERROR, "value was deleted");
  893. goto fail0;
  894. }
  895. struct value *v = value_get_path(i, mov, path_arg);
  896. if (!v) {
  897. goto fail0;
  898. }
  899. func_new_common(vo, i, v, NULL, NULL);
  900. return;
  901. fail0:
  902. NCDModuleInst_Backend_SetError(i);
  903. NCDModuleInst_Backend_Dead(i);
  904. }
  905. static void func_new_insert (void *vo, NCDModuleInst *i)
  906. {
  907. NCDValRef where_arg;
  908. NCDValRef what_arg;
  909. if (!NCDVal_ListRead(i->args, 2, &where_arg, &what_arg)) {
  910. ModuleLog(i, BLOG_ERROR, "wrong arity");
  911. goto fail0;
  912. }
  913. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  914. struct value *mov = valref_val(&mo->ref);
  915. if (!mov) {
  916. ModuleLog(i, BLOG_ERROR, "value was deleted");
  917. goto fail0;
  918. }
  919. struct value *v = value_insert(i, mov, where_arg, what_arg, NULL);
  920. if (!v) {
  921. goto fail0;
  922. }
  923. func_new_common(vo, i, v, NULL, NULL);
  924. return;
  925. fail0:
  926. NCDModuleInst_Backend_SetError(i);
  927. NCDModuleInst_Backend_Dead(i);
  928. }
  929. struct insert_undo_deinit_data {
  930. struct valref val_ref;
  931. struct valref oldval_ref;
  932. };
  933. static void insert_undo_deinit_func (struct insert_undo_deinit_data *data, NCDModuleInst *i)
  934. {
  935. struct value *val = valref_val(&data->val_ref);
  936. struct value *oldval = valref_val(&data->oldval_ref);
  937. if (val && val->parent && (!oldval || !oldval->parent)) {
  938. // get parent
  939. struct value *parent = val->parent;
  940. // remove this value from parent and restore saved one (or none)
  941. switch (parent->type) {
  942. case NCDVAL_LIST: {
  943. size_t index = value_list_indexof(parent, val);
  944. value_list_remove(parent, val);
  945. if (oldval) {
  946. int res = value_list_insert(i, parent, oldval, index);
  947. ASSERT(res)
  948. }
  949. } break;
  950. case NCDVAL_MAP: {
  951. NCDValMem key_mem;
  952. NCDValSafeRef key;
  953. value_map_remove2(parent, val, &key_mem, &key);
  954. if (oldval) {
  955. int res = value_map_insert(parent, oldval, key_mem, key, i);
  956. ASSERT(res)
  957. } else {
  958. NCDValMem_Free(&key_mem);
  959. }
  960. } break;
  961. default: ASSERT(0);
  962. }
  963. }
  964. valref_free(&data->oldval_ref);
  965. valref_free(&data->val_ref);
  966. free(data);
  967. }
  968. static void func_new_insert_undo (void *vo, NCDModuleInst *i)
  969. {
  970. NCDValRef where_arg;
  971. NCDValRef what_arg;
  972. if (!NCDVal_ListRead(i->args, 2, &where_arg, &what_arg)) {
  973. ModuleLog(i, BLOG_ERROR, "wrong arity");
  974. goto fail0;
  975. }
  976. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  977. struct value *mov = valref_val(&mo->ref);
  978. if (!mov) {
  979. ModuleLog(i, BLOG_ERROR, "value was deleted");
  980. goto fail0;
  981. }
  982. struct insert_undo_deinit_data *data = malloc(sizeof(*data));
  983. if (!data) {
  984. ModuleLog(i, BLOG_ERROR, "malloc failed");
  985. goto fail0;
  986. }
  987. struct value *oldv;
  988. struct value *v = value_insert(i, mov, where_arg, what_arg, &oldv);
  989. if (!v) {
  990. goto fail1;
  991. }
  992. valref_init(&data->val_ref, v);
  993. valref_init(&data->oldval_ref, oldv);
  994. func_new_common(vo, i, v, (value_deinit_func)insert_undo_deinit_func, data);
  995. return;
  996. fail1:
  997. free(data);
  998. fail0:
  999. NCDModuleInst_Backend_SetError(i);
  1000. NCDModuleInst_Backend_Dead(i);
  1001. }
  1002. static void func_new_substr (void *vo, NCDModuleInst *i)
  1003. {
  1004. NCDValRef start_arg;
  1005. NCDValRef length_arg = NCDVal_NewInvalid();
  1006. if (!NCDVal_ListRead(i->args, 1, &start_arg) &&
  1007. !NCDVal_ListRead(i->args, 2, &start_arg, &length_arg)) {
  1008. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1009. goto fail0;
  1010. }
  1011. if (!NCDVal_IsStringNoNulls(start_arg) || (!NCDVal_IsInvalid(length_arg) && !NCDVal_IsStringNoNulls(length_arg))) {
  1012. ModuleLog(i, BLOG_ERROR, "wrong type");
  1013. goto fail0;
  1014. }
  1015. uintmax_t start;
  1016. if (!parse_unsigned_integer(NCDVal_StringValue(start_arg), &start)) {
  1017. ModuleLog(i, BLOG_ERROR, "start is not a number");
  1018. goto fail0;
  1019. }
  1020. uintmax_t length = UINTMAX_MAX;
  1021. if (!NCDVal_IsInvalid(length_arg) && !parse_unsigned_integer(NCDVal_StringValue(length_arg), &length)) {
  1022. ModuleLog(i, BLOG_ERROR, "length is not a number");
  1023. goto fail0;
  1024. }
  1025. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  1026. struct value *mov = valref_val(&mo->ref);
  1027. if (!mov) {
  1028. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1029. goto fail0;
  1030. }
  1031. if (mov->type != NCDVAL_STRING) {
  1032. ModuleLog(i, BLOG_ERROR, "value is not a string");
  1033. goto fail0;
  1034. }
  1035. if (start > mov->string.length) {
  1036. ModuleLog(i, BLOG_ERROR, "start is out of range");
  1037. goto fail0;
  1038. }
  1039. size_t remain = mov->string.length - start;
  1040. size_t amount = length < remain ? length : remain;
  1041. struct value *v = value_init_string(i, mov->string.string + start, amount);
  1042. if (!v) {
  1043. goto fail0;
  1044. }
  1045. func_new_common(vo, i, v, NULL, NULL);
  1046. return;
  1047. fail0:
  1048. NCDModuleInst_Backend_SetError(i);
  1049. NCDModuleInst_Backend_Dead(i);
  1050. }
  1051. static void remove_func_new (NCDModuleInst *i)
  1052. {
  1053. NCDValRef where_arg;
  1054. if (!NCDVal_ListRead(i->args, 1, &where_arg)) {
  1055. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1056. goto fail0;
  1057. }
  1058. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  1059. struct value *mov = valref_val(&mo->ref);
  1060. if (!mov) {
  1061. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1062. goto fail0;
  1063. }
  1064. if (!value_remove(i, mov, where_arg)) {
  1065. goto fail0;
  1066. }
  1067. NCDModuleInst_Backend_Up(i);
  1068. return;
  1069. fail0:
  1070. NCDModuleInst_Backend_SetError(i);
  1071. NCDModuleInst_Backend_Dead(i);
  1072. }
  1073. static void delete_func_new (NCDModuleInst *i)
  1074. {
  1075. if (!NCDVal_ListRead(i->args, 0)) {
  1076. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1077. goto fail0;
  1078. }
  1079. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  1080. struct value *mov = valref_val(&mo->ref);
  1081. if (!mov) {
  1082. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1083. goto fail0;
  1084. }
  1085. value_delete(mov);
  1086. NCDModuleInst_Backend_Up(i);
  1087. return;
  1088. fail0:
  1089. NCDModuleInst_Backend_SetError(i);
  1090. NCDModuleInst_Backend_Dead(i);
  1091. }
  1092. static const struct NCDModule modules[] = {
  1093. {
  1094. .type = "value",
  1095. .func_new2 = func_new_value,
  1096. .func_die = func_die,
  1097. .func_getvar = func_getvar,
  1098. .alloc_size = sizeof(struct instance)
  1099. }, {
  1100. .type = "value::get",
  1101. .base_type = "value",
  1102. .func_new2 = func_new_get,
  1103. .func_die = func_die,
  1104. .func_getvar = func_getvar,
  1105. .alloc_size = sizeof(struct instance)
  1106. }, {
  1107. .type = "value::try_get",
  1108. .base_type = "value",
  1109. .func_new2 = func_new_try_get,
  1110. .func_die = func_die,
  1111. .func_getvar = func_getvar,
  1112. .alloc_size = sizeof(struct instance)
  1113. }, {
  1114. .type = "value::getpath",
  1115. .base_type = "value",
  1116. .func_new2 = func_new_getpath,
  1117. .func_die = func_die,
  1118. .func_getvar = func_getvar,
  1119. .alloc_size = sizeof(struct instance)
  1120. }, {
  1121. .type = "value::insert",
  1122. .base_type = "value",
  1123. .func_new2 = func_new_insert,
  1124. .func_die = func_die,
  1125. .func_getvar = func_getvar,
  1126. .alloc_size = sizeof(struct instance)
  1127. }, {
  1128. .type = "value::insert_undo",
  1129. .base_type = "value",
  1130. .func_new2 = func_new_insert_undo,
  1131. .func_die = func_die,
  1132. .func_getvar = func_getvar,
  1133. .alloc_size = sizeof(struct instance)
  1134. }, {
  1135. .type = "value::remove",
  1136. .func_new = remove_func_new
  1137. }, {
  1138. .type = "value::delete",
  1139. .func_new = delete_func_new
  1140. }, {
  1141. .type = "value::substr",
  1142. .base_type = "value",
  1143. .func_new2 = func_new_substr,
  1144. .func_die = func_die,
  1145. .func_getvar = func_getvar,
  1146. .alloc_size = sizeof(struct instance)
  1147. }, {
  1148. .type = NULL
  1149. }
  1150. };
  1151. const struct NCDModuleGroup ncdmodule_value = {
  1152. .modules = modules
  1153. };