value.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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 (only if the value if a list
  81. * or a map)
  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. #include <stdlib.h>
  103. #include <string.h>
  104. #include <stddef.h>
  105. #include <limits.h>
  106. #include <stdint.h>
  107. #include <misc/offset.h>
  108. #include <misc/debug.h>
  109. #include <misc/parse_number.h>
  110. #include <structure/LinkedList0.h>
  111. #include <structure/IndexedList.h>
  112. #include <structure/BCountAVL.h>
  113. #include <ncd/NCDModule.h>
  114. #include <generated/blog_channel_ncd_value.h>
  115. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  116. struct value;
  117. struct valref {
  118. struct value *v;
  119. LinkedList0Node refs_list_node;
  120. };
  121. typedef void (*value_deinit_func) (void *deinit_data, NCDModuleInst *i);
  122. struct instance {
  123. NCDModuleInst *i;
  124. struct valref ref;
  125. value_deinit_func deinit_func;
  126. void *deinit_data;
  127. };
  128. struct value {
  129. LinkedList0 refs_list;
  130. struct value *parent;
  131. union {
  132. struct {
  133. IndexedListNode list_contents_il_node;
  134. } list_parent;
  135. struct {
  136. NCDValue key;
  137. BCountAVLNode map_contents_tree_node;
  138. } map_parent;
  139. };
  140. int type;
  141. union {
  142. struct {
  143. uint8_t *string;
  144. size_t length;
  145. } string;
  146. struct {
  147. IndexedList list_contents_il;
  148. } list;
  149. struct {
  150. BCountAVL map_contents_tree;
  151. } map;
  152. };
  153. };
  154. static int ncdvalue_comparator (void *unused, void *vv1, void *vv2);
  155. static const char * get_type_str (int type);
  156. static void value_cleanup (struct value *v);
  157. static void value_delete (struct value *v);
  158. static struct value * value_init_string (NCDModuleInst *i, const uint8_t *str, size_t len);
  159. static struct value * value_init_list (NCDModuleInst *i);
  160. static size_t value_list_len (struct value *v);
  161. static struct value * value_list_at (struct value *v, size_t index);
  162. static size_t value_list_indexof (struct value *v, struct value *ev);
  163. static int value_list_insert (NCDModuleInst *i, struct value *list, struct value *v, size_t index);
  164. static void value_list_remove (struct value *list, struct value *v);
  165. static struct value * value_init_map (NCDModuleInst *i);
  166. static size_t value_map_len (struct value *map);
  167. static struct value * value_map_at (struct value *map, size_t index);
  168. static struct value * value_map_find (struct value *map, NCDValue *key);
  169. static int value_map_insert (struct value *map, struct value *v, NCDValue key, NCDModuleInst *i);
  170. static void value_map_remove (struct value *map, struct value *v);
  171. static void value_map_remove2 (struct value *map, struct value *v, NCDValue *out_key);
  172. static struct value * value_init_fromvalue (NCDModuleInst *i, NCDValue *value);
  173. static int value_to_value (NCDModuleInst *i, struct value *v, NCDValue *out_value);
  174. static struct value * value_get (NCDModuleInst *i, struct value *v, NCDValue *where, int no_error);
  175. static struct value * value_get_path (NCDModuleInst *i, struct value *v, NCDValue *path);
  176. static struct value * value_insert (NCDModuleInst *i, struct value *v, NCDValue *where, NCDValue *what, struct value **out_oldv);
  177. static int value_remove (NCDModuleInst *i, struct value *v, NCDValue *where);
  178. static void valref_init (struct valref *r, struct value *v);
  179. static void valref_free (struct valref *r);
  180. static struct value * valref_val (struct valref *r);
  181. static void valref_break (struct valref *r);
  182. static int ncdvalue_comparator (void *unused, void *vv1, void *vv2)
  183. {
  184. NCDValue *v1 = vv1;
  185. NCDValue *v2 = vv2;
  186. return NCDValue_Compare(v1, v2);
  187. }
  188. static const char * get_type_str (int type)
  189. {
  190. switch (type) {
  191. case NCDVALUE_STRING: return "string";
  192. case NCDVALUE_LIST: return "list";
  193. case NCDVALUE_MAP: return "map";
  194. }
  195. ASSERT(0)
  196. return NULL;
  197. }
  198. static void value_cleanup (struct value *v)
  199. {
  200. if (v->parent || !LinkedList0_IsEmpty(&v->refs_list)) {
  201. return;
  202. }
  203. switch (v->type) {
  204. case NCDVALUE_STRING: {
  205. free(v->string.string);
  206. } break;
  207. case NCDVALUE_LIST: {
  208. while (value_list_len(v) > 0) {
  209. struct value *ev = value_list_at(v, 0);
  210. value_list_remove(v, ev);
  211. value_cleanup(ev);
  212. }
  213. } break;
  214. case NCDVALUE_MAP: {
  215. while (value_map_len(v) > 0) {
  216. struct value *ev = value_map_at(v, 0);
  217. value_map_remove(v, ev);
  218. value_cleanup(ev);
  219. }
  220. } break;
  221. default: ASSERT(0);
  222. }
  223. free(v);
  224. }
  225. static void value_delete (struct value *v)
  226. {
  227. if (v->parent) {
  228. switch (v->parent->type) {
  229. case NCDVALUE_LIST: {
  230. value_list_remove(v->parent, v);
  231. } break;
  232. case NCDVALUE_MAP: {
  233. value_map_remove(v->parent, v);
  234. } break;
  235. default: ASSERT(0);
  236. }
  237. }
  238. LinkedList0Node *ln;
  239. while (ln = LinkedList0_GetFirst(&v->refs_list)) {
  240. struct valref *r = UPPER_OBJECT(ln, struct valref, refs_list_node);
  241. ASSERT(r->v == v)
  242. valref_break(r);
  243. }
  244. switch (v->type) {
  245. case NCDVALUE_STRING: {
  246. free(v->string.string);
  247. } break;
  248. case NCDVALUE_LIST: {
  249. while (value_list_len(v) > 0) {
  250. struct value *ev = value_list_at(v, 0);
  251. value_delete(ev);
  252. }
  253. } break;
  254. case NCDVALUE_MAP: {
  255. while (value_map_len(v) > 0) {
  256. struct value *ev = value_map_at(v, 0);
  257. value_delete(ev);
  258. }
  259. } break;
  260. default: ASSERT(0);
  261. }
  262. free(v);
  263. }
  264. static struct value * value_init_string (NCDModuleInst *i, const uint8_t *str, size_t len)
  265. {
  266. struct value *v = malloc(sizeof(*v));
  267. if (!v) {
  268. ModuleLog(i, BLOG_ERROR, "malloc failed");
  269. goto fail0;
  270. }
  271. LinkedList0_Init(&v->refs_list);
  272. v->parent = NULL;
  273. v->type = NCDVALUE_STRING;
  274. if (!(v->string.string = malloc(len))) {
  275. ModuleLog(i, BLOG_ERROR, "malloc failed");
  276. goto fail1;
  277. }
  278. memcpy(v->string.string, str, len);
  279. v->string.length = len;
  280. return v;
  281. fail1:
  282. free(v);
  283. fail0:
  284. return NULL;
  285. }
  286. static struct value * value_init_list (NCDModuleInst *i)
  287. {
  288. struct value *v = malloc(sizeof(*v));
  289. if (!v) {
  290. ModuleLog(i, BLOG_ERROR, "malloc failed");
  291. return NULL;
  292. }
  293. LinkedList0_Init(&v->refs_list);
  294. v->parent = NULL;
  295. v->type = NCDVALUE_LIST;
  296. IndexedList_Init(&v->list.list_contents_il);
  297. return v;
  298. }
  299. static size_t value_list_len (struct value *v)
  300. {
  301. ASSERT(v->type == NCDVALUE_LIST)
  302. return IndexedList_Count(&v->list.list_contents_il);
  303. }
  304. static struct value * value_list_at (struct value *v, size_t index)
  305. {
  306. ASSERT(v->type == NCDVALUE_LIST)
  307. ASSERT(index < value_list_len(v))
  308. IndexedListNode *iln = IndexedList_GetAt(&v->list.list_contents_il, index);
  309. ASSERT(iln)
  310. struct value *e = UPPER_OBJECT(iln, struct value, list_parent.list_contents_il_node);
  311. ASSERT(e->parent == v)
  312. return e;
  313. }
  314. static size_t value_list_indexof (struct value *v, struct value *ev)
  315. {
  316. ASSERT(v->type == NCDVALUE_LIST)
  317. ASSERT(ev->parent == v)
  318. uint64_t index = IndexedList_IndexOf(&v->list.list_contents_il, &ev->list_parent.list_contents_il_node);
  319. ASSERT(index < value_list_len(v))
  320. return index;
  321. }
  322. static int value_list_insert (NCDModuleInst *i, struct value *list, struct value *v, size_t index)
  323. {
  324. ASSERT(list->type == NCDVALUE_LIST)
  325. ASSERT(!v->parent)
  326. ASSERT(index <= value_list_len(list))
  327. if (value_list_len(list) == SIZE_MAX) {
  328. ModuleLog(i, BLOG_ERROR, "list has too many elements");
  329. return 0;
  330. }
  331. IndexedList_InsertAt(&list->list.list_contents_il, &v->list_parent.list_contents_il_node, index);
  332. v->parent = list;
  333. return 1;
  334. }
  335. static void value_list_remove (struct value *list, struct value *v)
  336. {
  337. ASSERT(list->type == NCDVALUE_LIST)
  338. ASSERT(v->parent == list)
  339. IndexedList_Remove(&list->list.list_contents_il, &v->list_parent.list_contents_il_node);
  340. v->parent = NULL;
  341. }
  342. static struct value * value_init_map (NCDModuleInst *i)
  343. {
  344. struct value *v = malloc(sizeof(*v));
  345. if (!v) {
  346. ModuleLog(i, BLOG_ERROR, "malloc failed");
  347. return NULL;
  348. }
  349. LinkedList0_Init(&v->refs_list);
  350. v->parent = NULL;
  351. v->type = NCDVALUE_MAP;
  352. BCountAVL_Init(&v->map.map_contents_tree, OFFSET_DIFF(struct value, map_parent.key, map_parent.map_contents_tree_node), ncdvalue_comparator, NULL);
  353. return v;
  354. }
  355. static size_t value_map_len (struct value *map)
  356. {
  357. ASSERT(map->type == NCDVALUE_MAP)
  358. return BCountAVL_Count(&map->map.map_contents_tree);
  359. }
  360. static struct value * value_map_at (struct value *map, size_t index)
  361. {
  362. ASSERT(map->type == NCDVALUE_MAP)
  363. ASSERT(index < value_map_len(map))
  364. BCountAVLNode *tn = BCountAVL_GetAt(&map->map.map_contents_tree, index);
  365. ASSERT(tn)
  366. struct value *e = UPPER_OBJECT(tn, struct value, map_parent.map_contents_tree_node);
  367. ASSERT(e->parent == map)
  368. return e;
  369. }
  370. static struct value * value_map_find (struct value *map, NCDValue *key)
  371. {
  372. ASSERT(map->type == NCDVALUE_MAP)
  373. ASSERT(key)
  374. BCountAVLNode *tn = BCountAVL_LookupExact(&map->map.map_contents_tree, key);
  375. if (!tn) {
  376. return NULL;
  377. }
  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 int value_map_insert (struct value *map, struct value *v, NCDValue key, NCDModuleInst *i)
  383. {
  384. ASSERT(map->type == NCDVALUE_MAP)
  385. ASSERT(!v->parent)
  386. ASSERT(!value_map_find(map, &key))
  387. if (value_map_len(map) == SIZE_MAX) {
  388. ModuleLog(i, BLOG_ERROR, "map has too many elements");
  389. return 0;
  390. }
  391. v->map_parent.key = key;
  392. int res = BCountAVL_Insert(&map->map.map_contents_tree, &v->map_parent.map_contents_tree_node, NULL);
  393. ASSERT(res)
  394. v->parent = map;
  395. return 1;
  396. }
  397. static void value_map_remove (struct value *map, struct value *v)
  398. {
  399. ASSERT(map->type == NCDVALUE_MAP)
  400. ASSERT(v->parent == map)
  401. BCountAVL_Remove(&map->map.map_contents_tree, &v->map_parent.map_contents_tree_node);
  402. NCDValue_Free(&v->map_parent.key);
  403. v->parent = NULL;
  404. }
  405. static void value_map_remove2 (struct value *map, struct value *v, NCDValue *out_key)
  406. {
  407. ASSERT(map->type == NCDVALUE_MAP)
  408. ASSERT(v->parent == map)
  409. ASSERT(out_key)
  410. BCountAVL_Remove(&map->map.map_contents_tree, &v->map_parent.map_contents_tree_node);
  411. *out_key = v->map_parent.key;
  412. v->parent = NULL;
  413. }
  414. static struct value * value_init_fromvalue (NCDModuleInst *i, NCDValue *value)
  415. {
  416. struct value *v;
  417. switch (NCDValue_Type(value)) {
  418. case NCDVALUE_STRING: {
  419. if (!(v = value_init_string(i, (const uint8_t *)NCDValue_StringValue(value), NCDValue_StringLength(value)))) {
  420. goto fail0;
  421. }
  422. } break;
  423. case NCDVALUE_LIST: {
  424. if (!(v = value_init_list(i))) {
  425. goto fail0;
  426. }
  427. for (NCDValue *eval = NCDValue_ListFirst(value); eval; eval = NCDValue_ListNext(value, eval)) {
  428. struct value *ev = value_init_fromvalue(i, eval);
  429. if (!ev) {
  430. goto fail1;
  431. }
  432. if (!value_list_insert(i, v, ev, value_list_len(v))) {
  433. value_cleanup(ev);
  434. goto fail1;
  435. }
  436. }
  437. } break;
  438. case NCDVALUE_MAP: {
  439. if (!(v = value_init_map(i))) {
  440. goto fail0;
  441. }
  442. for (NCDValue *ekey = NCDValue_MapFirstKey(value); ekey; ekey = NCDValue_MapNextKey(value, ekey)) {
  443. NCDValue *eval = NCDValue_MapKeyValue(value, ekey);
  444. NCDValue key;
  445. if (!NCDValue_InitCopy(&key, ekey)) {
  446. BLog(BLOG_ERROR, "NCDValue_InitCopy failed");
  447. goto fail1;
  448. }
  449. struct value *ev = value_init_fromvalue(i, eval);
  450. if (!ev) {
  451. NCDValue_Free(&key);
  452. goto fail1;
  453. }
  454. if (!value_map_insert(v, ev, key, i)) {
  455. NCDValue_Free(&key);
  456. value_cleanup(ev);
  457. goto fail1;
  458. }
  459. }
  460. } break;
  461. default: ASSERT(0);
  462. }
  463. return v;
  464. fail1:
  465. value_cleanup(v);
  466. fail0:
  467. return NULL;
  468. }
  469. static int value_to_value (NCDModuleInst *i, struct value *v, NCDValue *out_value)
  470. {
  471. switch (v->type) {
  472. case NCDVALUE_STRING: {
  473. if (!(NCDValue_InitStringBin(out_value, v->string.string, v->string.length))) {
  474. ModuleLog(i, BLOG_ERROR, "NCDValue_InitStringBin failed");
  475. goto fail0;
  476. }
  477. } break;
  478. case NCDVALUE_LIST: {
  479. NCDValue_InitList(out_value);
  480. for (size_t index = 0; index < value_list_len(v); index++) {
  481. NCDValue eval;
  482. if (!value_to_value(i, value_list_at(v, index), &eval)) {
  483. goto fail1;
  484. }
  485. if (!NCDValue_ListAppend(out_value, eval)) {
  486. ModuleLog(i, BLOG_ERROR, "NCDValue_ListAppend failed");
  487. NCDValue_Free(&eval);
  488. goto fail1;
  489. }
  490. }
  491. } break;
  492. case NCDVALUE_MAP: {
  493. NCDValue_InitMap(out_value);
  494. for (size_t index = 0; index < value_map_len(v); index++) {
  495. struct value *ev = value_map_at(v, index);
  496. NCDValue key;
  497. NCDValue val;
  498. if (!NCDValue_InitCopy(&key, &ev->map_parent.key)) {
  499. ModuleLog(i, BLOG_ERROR, "NCDValue_InitCopy failed");
  500. goto fail1;
  501. }
  502. if (!value_to_value(i, ev, &val)) {
  503. NCDValue_Free(&key);
  504. goto fail1;
  505. }
  506. if (!NCDValue_MapInsert(out_value, key, val)) {
  507. ModuleLog(i, BLOG_ERROR, "NCDValue_MapInsert failed");
  508. NCDValue_Free(&key);
  509. NCDValue_Free(&val);
  510. goto fail1;
  511. }
  512. }
  513. } break;
  514. default: ASSERT(0);
  515. }
  516. return 1;
  517. fail1:
  518. NCDValue_Free(out_value);
  519. fail0:
  520. return 0;
  521. }
  522. static struct value * value_get (NCDModuleInst *i, struct value *v, NCDValue *where, int no_error)
  523. {
  524. switch (v->type) {
  525. case NCDVALUE_STRING: {
  526. if (!no_error) ModuleLog(i, BLOG_ERROR, "cannot resolve into a string");
  527. goto fail;
  528. } break;
  529. case NCDVALUE_LIST: {
  530. if (NCDValue_Type(where) != NCDVALUE_STRING) {
  531. if (!no_error) ModuleLog(i, BLOG_ERROR, "index is not a string (resolving into list)");
  532. goto fail;
  533. }
  534. uintmax_t index;
  535. if (NCDValue_StringHasNulls(where) || !parse_unsigned_integer(NCDValue_StringValue(where), &index)) {
  536. if (!no_error) ModuleLog(i, BLOG_ERROR, "index is not a valid number (resolving into list)");
  537. goto fail;
  538. }
  539. if (index >= value_list_len(v)) {
  540. if (!no_error) ModuleLog(i, BLOG_ERROR, "index is out of bounds (resolving into list)");
  541. goto fail;
  542. }
  543. v = value_list_at(v, index);
  544. } break;
  545. case NCDVALUE_MAP: {
  546. v = value_map_find(v, where);
  547. if (!v) {
  548. if (!no_error) ModuleLog(i, BLOG_ERROR, "key does not exist (resolving into map)");
  549. goto fail;
  550. }
  551. } break;
  552. default: ASSERT(0);
  553. }
  554. return v;
  555. fail:
  556. return NULL;
  557. }
  558. static struct value * value_get_path (NCDModuleInst *i, struct value *v, NCDValue *path)
  559. {
  560. ASSERT(NCDValue_Type(path) == NCDVALUE_LIST)
  561. for (NCDValue *ev = NCDValue_ListFirst(path); ev; ev = NCDValue_ListNext(path, ev)) {
  562. if (!(v = value_get(i, v, ev, 0))) {
  563. goto fail;
  564. }
  565. }
  566. return v;
  567. fail:
  568. return NULL;
  569. }
  570. static struct value * value_insert (NCDModuleInst *i, struct value *v, NCDValue *where, NCDValue *what, struct value **out_oldv)
  571. {
  572. ASSERT(v)
  573. NCDValue_Type(where);
  574. NCDValue_Type(what);
  575. struct value *nv = value_init_fromvalue(i, what);
  576. if (!nv) {
  577. goto fail0;
  578. }
  579. struct value *oldv = NULL;
  580. switch (v->type) {
  581. case NCDVALUE_STRING: {
  582. ModuleLog(i, BLOG_ERROR, "cannot insert into a string");
  583. goto fail1;
  584. } break;
  585. case NCDVALUE_LIST: {
  586. if (NCDValue_Type(where) != NCDVALUE_STRING) {
  587. ModuleLog(i, BLOG_ERROR, "index is not a string (inserting into list)");
  588. goto fail1;
  589. }
  590. uintmax_t index;
  591. if (NCDValue_StringHasNulls(where) || !parse_unsigned_integer(NCDValue_StringValue(where), &index)) {
  592. ModuleLog(i, BLOG_ERROR, "index is not a valid number (inserting into list)");
  593. goto fail1;
  594. }
  595. if (index > value_list_len(v)) {
  596. ModuleLog(i, BLOG_ERROR, "index is out of bounds (inserting into list)");
  597. goto fail1;
  598. }
  599. if (!value_list_insert(i, v, nv, index)) {
  600. goto fail1;
  601. }
  602. } break;
  603. case NCDVALUE_MAP: {
  604. oldv = value_map_find(v, where);
  605. if (!oldv && value_map_len(v) == SIZE_MAX) {
  606. ModuleLog(i, BLOG_ERROR, "map has too many elements");
  607. goto fail1;
  608. }
  609. NCDValue key;
  610. if (!NCDValue_InitCopy(&key, where)) {
  611. ModuleLog(i, BLOG_ERROR, "NCDValue_InitCopy failed");
  612. goto fail1;
  613. }
  614. if (oldv) {
  615. value_map_remove(v, oldv);
  616. }
  617. int res = value_map_insert(v, nv, key, i);
  618. ASSERT(res)
  619. } break;
  620. default: ASSERT(0);
  621. }
  622. if (out_oldv) {
  623. *out_oldv = oldv;
  624. }
  625. else if (oldv) {
  626. value_cleanup(oldv);
  627. }
  628. return nv;
  629. fail1:
  630. value_cleanup(nv);
  631. fail0:
  632. return NULL;
  633. }
  634. static int value_remove (NCDModuleInst *i, struct value *v, NCDValue *where)
  635. {
  636. switch (v->type) {
  637. case NCDVALUE_STRING: {
  638. ModuleLog(i, BLOG_ERROR, "cannot remove from a string");
  639. goto fail;
  640. } break;
  641. case NCDVALUE_LIST: {
  642. if (NCDValue_Type(where) != NCDVALUE_STRING) {
  643. ModuleLog(i, BLOG_ERROR, "index is not a string (removing from list)");
  644. goto fail;
  645. }
  646. uintmax_t index;
  647. if (NCDValue_StringHasNulls(where) || !parse_unsigned_integer(NCDValue_StringValue(where), &index)) {
  648. ModuleLog(i, BLOG_ERROR, "index is not a valid number (removing from list)");
  649. goto fail;
  650. }
  651. if (index >= value_list_len(v)) {
  652. ModuleLog(i, BLOG_ERROR, "index is out of bounds (removing from list)");
  653. goto fail;
  654. }
  655. struct value *ov = value_list_at(v, index);
  656. value_list_remove(v, ov);
  657. value_cleanup(ov);
  658. } break;
  659. case NCDVALUE_MAP: {
  660. struct value *ov = value_map_find(v, where);
  661. if (!ov) {
  662. ModuleLog(i, BLOG_ERROR, "key does not exist (removing from map)");
  663. goto fail;
  664. }
  665. value_map_remove(v, ov);
  666. value_cleanup(ov);
  667. } break;
  668. default: ASSERT(0);
  669. }
  670. return 1;
  671. fail:
  672. return 0;
  673. }
  674. static void valref_init (struct valref *r, struct value *v)
  675. {
  676. r->v = v;
  677. if (v) {
  678. LinkedList0_Prepend(&v->refs_list, &r->refs_list_node);
  679. }
  680. }
  681. static void valref_free (struct valref *r)
  682. {
  683. if (r->v) {
  684. LinkedList0_Remove(&r->v->refs_list, &r->refs_list_node);
  685. value_cleanup(r->v);
  686. }
  687. }
  688. static struct value * valref_val (struct valref *r)
  689. {
  690. return r->v;
  691. }
  692. static void valref_break (struct valref *r)
  693. {
  694. ASSERT(r->v)
  695. LinkedList0_Remove(&r->v->refs_list, &r->refs_list_node);
  696. r->v = NULL;
  697. }
  698. static void func_new_common (NCDModuleInst *i, struct value *v, value_deinit_func deinit_func, void *deinit_data)
  699. {
  700. // allocate instance
  701. struct instance *o = malloc(sizeof(*o));
  702. if (!o) {
  703. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  704. goto fail0;
  705. }
  706. o->i = i;
  707. NCDModuleInst_Backend_SetUser(i, o);
  708. // init value references
  709. valref_init(&o->ref, v);
  710. // remember deinit
  711. o->deinit_func = deinit_func;
  712. o->deinit_data = deinit_data;
  713. NCDModuleInst_Backend_Up(i);
  714. return;
  715. fail0:
  716. value_cleanup(v);
  717. if (deinit_func) {
  718. deinit_func(deinit_data, i);
  719. }
  720. NCDModuleInst_Backend_SetError(i);
  721. NCDModuleInst_Backend_Dead(i);
  722. }
  723. static void func_die (void *vo)
  724. {
  725. struct instance *o = vo;
  726. NCDModuleInst *i = o->i;
  727. // deinit
  728. if (o->deinit_func) {
  729. o->deinit_func(o->deinit_data, i);
  730. }
  731. // free value reference
  732. valref_free(&o->ref);
  733. // free instance
  734. free(o);
  735. NCDModuleInst_Backend_Dead(i);
  736. }
  737. static int func_getvar (void *vo, const char *name, NCDValue *out_value)
  738. {
  739. struct instance *o = vo;
  740. struct value *v = valref_val(&o->ref);
  741. if (!strcmp(name, "exists")) {
  742. const char *str = v ? "true" : "false";
  743. if (!NCDValue_InitString(out_value, str)) {
  744. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  745. return 0;
  746. }
  747. return 1;
  748. }
  749. if (strcmp(name, "type") && strcmp(name, "length") && strcmp(name, "keys") && strcmp(name, "")) {
  750. return 0;
  751. }
  752. if (!v) {
  753. ModuleLog(o->i, BLOG_ERROR, "value was deleted");
  754. return 0;
  755. }
  756. if (!strcmp(name, "type")) {
  757. if (!NCDValue_InitString(out_value, get_type_str(v->type))) {
  758. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  759. return 0;
  760. }
  761. }
  762. else if (!strcmp(name, "length")) {
  763. size_t len;
  764. switch (v->type) {
  765. case NCDVALUE_LIST:
  766. len = value_list_len(v);
  767. break;
  768. case NCDVALUE_MAP:
  769. len = value_map_len(v);
  770. break;
  771. default:
  772. ModuleLog(o->i, BLOG_ERROR, "value is not a list or map");
  773. return 0;
  774. }
  775. char str[64];
  776. snprintf(str, sizeof(str), "%zu", len);
  777. if (!NCDValue_InitString(out_value, str)) {
  778. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  779. return 0;
  780. }
  781. }
  782. else if (!strcmp(name, "keys")) {
  783. if (v->type != NCDVALUE_MAP) {
  784. ModuleLog(o->i, BLOG_ERROR, "value is not a map (reading keys variable)");
  785. return 0;
  786. }
  787. NCDValue_InitList(out_value);
  788. for (size_t i = 0; i < value_map_len(v); i++) {
  789. struct value *ev = value_map_at(v, i);
  790. NCDValue key;
  791. if (!NCDValue_InitCopy(&key, &ev->map_parent.key)) {
  792. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  793. goto map_fail1;
  794. }
  795. if (!NCDValue_ListAppend(out_value, key)) {
  796. ModuleLog(o->i, BLOG_ERROR, "NCDValue_ListAppend failed");
  797. NCDValue_Free(&key);
  798. goto map_fail1;
  799. }
  800. }
  801. return 1;
  802. map_fail1:
  803. NCDValue_Free(out_value);
  804. return 0;
  805. }
  806. else if (!strcmp(name, "")) {
  807. if (!value_to_value(o->i, v, out_value)) {
  808. return 0;
  809. }
  810. }
  811. else {
  812. ASSERT(0);
  813. }
  814. return 1;
  815. }
  816. static void func_new_value (NCDModuleInst *i)
  817. {
  818. NCDValue *value_arg;
  819. if (!NCDValue_ListRead(i->args, 1, &value_arg)) {
  820. ModuleLog(i, BLOG_ERROR, "wrong arity");
  821. goto fail0;
  822. }
  823. struct value *v = value_init_fromvalue(i, value_arg);
  824. if (!v) {
  825. goto fail0;
  826. }
  827. func_new_common(i, v, NULL, NULL);
  828. return;
  829. fail0:
  830. NCDModuleInst_Backend_SetError(i);
  831. NCDModuleInst_Backend_Dead(i);
  832. }
  833. static void func_new_get (NCDModuleInst *i)
  834. {
  835. NCDValue *where_arg;
  836. if (!NCDValue_ListRead(i->args, 1, &where_arg)) {
  837. ModuleLog(i, BLOG_ERROR, "wrong arity");
  838. goto fail0;
  839. }
  840. struct instance *mo = ((NCDModuleInst *)i->method_user)->inst_user;
  841. struct value *mov = valref_val(&mo->ref);
  842. if (!mov) {
  843. ModuleLog(i, BLOG_ERROR, "value was deleted");
  844. goto fail0;
  845. }
  846. struct value *v = value_get(i, mov, where_arg, 0);
  847. if (!v) {
  848. goto fail0;
  849. }
  850. func_new_common(i, v, NULL, NULL);
  851. return;
  852. fail0:
  853. NCDModuleInst_Backend_SetError(i);
  854. NCDModuleInst_Backend_Dead(i);
  855. }
  856. static void func_new_try_get (NCDModuleInst *i)
  857. {
  858. NCDValue *where_arg;
  859. if (!NCDValue_ListRead(i->args, 1, &where_arg)) {
  860. ModuleLog(i, BLOG_ERROR, "wrong arity");
  861. goto fail0;
  862. }
  863. struct instance *mo = ((NCDModuleInst *)i->method_user)->inst_user;
  864. struct value *mov = valref_val(&mo->ref);
  865. if (!mov) {
  866. ModuleLog(i, BLOG_ERROR, "value was deleted");
  867. goto fail0;
  868. }
  869. struct value *v = value_get(i, mov, where_arg, 1);
  870. func_new_common(i, v, NULL, NULL);
  871. return;
  872. fail0:
  873. NCDModuleInst_Backend_SetError(i);
  874. NCDModuleInst_Backend_Dead(i);
  875. }
  876. static void func_new_getpath (NCDModuleInst *i)
  877. {
  878. NCDValue *path_arg;
  879. if (!NCDValue_ListRead(i->args, 1, &path_arg)) {
  880. ModuleLog(i, BLOG_ERROR, "wrong arity");
  881. goto fail0;
  882. }
  883. if (NCDValue_Type(path_arg) != NCDVALUE_LIST) {
  884. ModuleLog(i, BLOG_ERROR, "wrong type");
  885. goto fail0;
  886. }
  887. struct instance *mo = ((NCDModuleInst *)i->method_user)->inst_user;
  888. struct value *mov = valref_val(&mo->ref);
  889. if (!mov) {
  890. ModuleLog(i, BLOG_ERROR, "value was deleted");
  891. goto fail0;
  892. }
  893. struct value *v = value_get_path(i, mov, path_arg);
  894. if (!v) {
  895. goto fail0;
  896. }
  897. func_new_common(i, v, NULL, NULL);
  898. return;
  899. fail0:
  900. NCDModuleInst_Backend_SetError(i);
  901. NCDModuleInst_Backend_Dead(i);
  902. }
  903. static void func_new_insert (NCDModuleInst *i)
  904. {
  905. NCDValue *where_arg;
  906. NCDValue *what_arg;
  907. if (!NCDValue_ListRead(i->args, 2, &where_arg, &what_arg)) {
  908. ModuleLog(i, BLOG_ERROR, "wrong arity");
  909. goto fail0;
  910. }
  911. struct instance *mo = ((NCDModuleInst *)i->method_user)->inst_user;
  912. struct value *mov = valref_val(&mo->ref);
  913. if (!mov) {
  914. ModuleLog(i, BLOG_ERROR, "value was deleted");
  915. goto fail0;
  916. }
  917. struct value *v = value_insert(i, mov, where_arg, what_arg, NULL);
  918. if (!v) {
  919. goto fail0;
  920. }
  921. func_new_common(i, v, NULL, NULL);
  922. return;
  923. fail0:
  924. NCDModuleInst_Backend_SetError(i);
  925. NCDModuleInst_Backend_Dead(i);
  926. }
  927. struct insert_undo_deinit_data {
  928. struct valref val_ref;
  929. struct valref oldval_ref;
  930. };
  931. static void insert_undo_deinit_func (struct insert_undo_deinit_data *data, NCDModuleInst *i)
  932. {
  933. struct value *val = valref_val(&data->val_ref);
  934. struct value *oldval = valref_val(&data->oldval_ref);
  935. if (val && val->parent && (!oldval || !oldval->parent)) {
  936. // get parent
  937. struct value *parent = val->parent;
  938. // remove this value from parent and restore saved one (or none)
  939. switch (parent->type) {
  940. case NCDVALUE_LIST: {
  941. size_t index = value_list_indexof(parent, val);
  942. value_list_remove(parent, val);
  943. if (oldval) {
  944. int res = value_list_insert(i, parent, oldval, index);
  945. ASSERT(res)
  946. }
  947. } break;
  948. case NCDVALUE_MAP: {
  949. NCDValue key;
  950. value_map_remove2(parent, val, &key);
  951. if (oldval) {
  952. int res = value_map_insert(parent, oldval, key, i);
  953. ASSERT(res)
  954. } else {
  955. NCDValue_Free(&key);
  956. }
  957. } break;
  958. default: ASSERT(0);
  959. }
  960. }
  961. valref_free(&data->oldval_ref);
  962. valref_free(&data->val_ref);
  963. free(data);
  964. }
  965. static void func_new_insert_undo (NCDModuleInst *i)
  966. {
  967. NCDValue *where_arg;
  968. NCDValue *what_arg;
  969. if (!NCDValue_ListRead(i->args, 2, &where_arg, &what_arg)) {
  970. ModuleLog(i, BLOG_ERROR, "wrong arity");
  971. goto fail0;
  972. }
  973. struct instance *mo = ((NCDModuleInst *)i->method_user)->inst_user;
  974. struct value *mov = valref_val(&mo->ref);
  975. if (!mov) {
  976. ModuleLog(i, BLOG_ERROR, "value was deleted");
  977. goto fail0;
  978. }
  979. struct insert_undo_deinit_data *data = malloc(sizeof(*data));
  980. if (!data) {
  981. ModuleLog(i, BLOG_ERROR, "malloc failed");
  982. goto fail0;
  983. }
  984. struct value *oldv;
  985. struct value *v = value_insert(i, mov, where_arg, what_arg, &oldv);
  986. if (!v) {
  987. goto fail1;
  988. }
  989. valref_init(&data->val_ref, v);
  990. valref_init(&data->oldval_ref, oldv);
  991. func_new_common(i, v, (value_deinit_func)insert_undo_deinit_func, data);
  992. return;
  993. fail1:
  994. free(data);
  995. fail0:
  996. NCDModuleInst_Backend_SetError(i);
  997. NCDModuleInst_Backend_Dead(i);
  998. }
  999. static void remove_func_new (NCDModuleInst *i)
  1000. {
  1001. NCDValue *where_arg;
  1002. if (!NCDValue_ListRead(i->args, 1, &where_arg)) {
  1003. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1004. goto fail0;
  1005. }
  1006. struct instance *mo = ((NCDModuleInst *)i->method_user)->inst_user;
  1007. struct value *mov = valref_val(&mo->ref);
  1008. if (!mov) {
  1009. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1010. goto fail0;
  1011. }
  1012. if (!value_remove(i, mov, where_arg)) {
  1013. goto fail0;
  1014. }
  1015. NCDModuleInst_Backend_Up(i);
  1016. return;
  1017. fail0:
  1018. NCDModuleInst_Backend_SetError(i);
  1019. NCDModuleInst_Backend_Dead(i);
  1020. }
  1021. static void delete_func_new (NCDModuleInst *i)
  1022. {
  1023. if (!NCDValue_ListRead(i->args, 0)) {
  1024. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1025. goto fail0;
  1026. }
  1027. struct instance *mo = ((NCDModuleInst *)i->method_user)->inst_user;
  1028. struct value *mov = valref_val(&mo->ref);
  1029. if (!mov) {
  1030. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1031. goto fail0;
  1032. }
  1033. value_delete(mov);
  1034. NCDModuleInst_Backend_Up(i);
  1035. return;
  1036. fail0:
  1037. NCDModuleInst_Backend_SetError(i);
  1038. NCDModuleInst_Backend_Dead(i);
  1039. }
  1040. static const struct NCDModule modules[] = {
  1041. {
  1042. .type = "value",
  1043. .func_new = func_new_value,
  1044. .func_die = func_die,
  1045. .func_getvar = func_getvar
  1046. }, {
  1047. .type = "value::get",
  1048. .base_type = "value",
  1049. .func_new = func_new_get,
  1050. .func_die = func_die,
  1051. .func_getvar = func_getvar
  1052. }, {
  1053. .type = "value::try_get",
  1054. .base_type = "value",
  1055. .func_new = func_new_try_get,
  1056. .func_die = func_die,
  1057. .func_getvar = func_getvar
  1058. }, {
  1059. .type = "value::getpath",
  1060. .base_type = "value",
  1061. .func_new = func_new_getpath,
  1062. .func_die = func_die,
  1063. .func_getvar = func_getvar
  1064. }, {
  1065. .type = "value::insert",
  1066. .base_type = "value",
  1067. .func_new = func_new_insert,
  1068. .func_die = func_die,
  1069. .func_getvar = func_getvar
  1070. }, {
  1071. .type = "value::insert_undo",
  1072. .base_type = "value",
  1073. .func_new = func_new_insert_undo,
  1074. .func_die = func_die,
  1075. .func_getvar = func_getvar
  1076. }, {
  1077. .type = "value::remove",
  1078. .func_new = remove_func_new
  1079. }, {
  1080. .type = "value::delete",
  1081. .func_new = delete_func_new
  1082. }, {
  1083. .type = NULL
  1084. }
  1085. };
  1086. const struct NCDModuleGroup ncdmodule_value = {
  1087. .modules = modules
  1088. };