value.c 37 KB

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