value.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  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. #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. case NCDVALUE_STRING:
  772. len = v->string.length;
  773. break;
  774. default:
  775. ASSERT(0);
  776. }
  777. char str[64];
  778. snprintf(str, sizeof(str), "%zu", len);
  779. if (!NCDValue_InitString(out_value, str)) {
  780. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  781. return 0;
  782. }
  783. }
  784. else if (!strcmp(name, "keys")) {
  785. if (v->type != NCDVALUE_MAP) {
  786. ModuleLog(o->i, BLOG_ERROR, "value is not a map (reading keys variable)");
  787. return 0;
  788. }
  789. NCDValue_InitList(out_value);
  790. for (size_t i = 0; i < value_map_len(v); i++) {
  791. struct value *ev = value_map_at(v, i);
  792. NCDValue key;
  793. if (!NCDValue_InitCopy(&key, &ev->map_parent.key)) {
  794. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  795. goto map_fail1;
  796. }
  797. if (!NCDValue_ListAppend(out_value, key)) {
  798. ModuleLog(o->i, BLOG_ERROR, "NCDValue_ListAppend failed");
  799. NCDValue_Free(&key);
  800. goto map_fail1;
  801. }
  802. }
  803. return 1;
  804. map_fail1:
  805. NCDValue_Free(out_value);
  806. return 0;
  807. }
  808. else if (!strcmp(name, "")) {
  809. if (!value_to_value(o->i, v, out_value)) {
  810. return 0;
  811. }
  812. }
  813. else {
  814. ASSERT(0);
  815. }
  816. return 1;
  817. }
  818. static void func_new_value (NCDModuleInst *i)
  819. {
  820. NCDValue *value_arg;
  821. if (!NCDValue_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(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 (NCDModuleInst *i)
  836. {
  837. NCDValue *where_arg;
  838. if (!NCDValue_ListRead(i->args, 1, &where_arg)) {
  839. ModuleLog(i, BLOG_ERROR, "wrong arity");
  840. goto fail0;
  841. }
  842. struct instance *mo = ((NCDModuleInst *)i->method_user)->inst_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(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 (NCDModuleInst *i)
  859. {
  860. NCDValue *where_arg;
  861. if (!NCDValue_ListRead(i->args, 1, &where_arg)) {
  862. ModuleLog(i, BLOG_ERROR, "wrong arity");
  863. goto fail0;
  864. }
  865. struct instance *mo = ((NCDModuleInst *)i->method_user)->inst_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(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 (NCDModuleInst *i)
  879. {
  880. NCDValue *path_arg;
  881. if (!NCDValue_ListRead(i->args, 1, &path_arg)) {
  882. ModuleLog(i, BLOG_ERROR, "wrong arity");
  883. goto fail0;
  884. }
  885. if (NCDValue_Type(path_arg) != NCDVALUE_LIST) {
  886. ModuleLog(i, BLOG_ERROR, "wrong type");
  887. goto fail0;
  888. }
  889. struct instance *mo = ((NCDModuleInst *)i->method_user)->inst_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(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 (NCDModuleInst *i)
  906. {
  907. NCDValue *where_arg;
  908. NCDValue *what_arg;
  909. if (!NCDValue_ListRead(i->args, 2, &where_arg, &what_arg)) {
  910. ModuleLog(i, BLOG_ERROR, "wrong arity");
  911. goto fail0;
  912. }
  913. struct instance *mo = ((NCDModuleInst *)i->method_user)->inst_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(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 NCDVALUE_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 NCDVALUE_MAP: {
  951. NCDValue key;
  952. value_map_remove2(parent, val, &key);
  953. if (oldval) {
  954. int res = value_map_insert(parent, oldval, key, i);
  955. ASSERT(res)
  956. } else {
  957. NCDValue_Free(&key);
  958. }
  959. } break;
  960. default: ASSERT(0);
  961. }
  962. }
  963. valref_free(&data->oldval_ref);
  964. valref_free(&data->val_ref);
  965. free(data);
  966. }
  967. static void func_new_insert_undo (NCDModuleInst *i)
  968. {
  969. NCDValue *where_arg;
  970. NCDValue *what_arg;
  971. if (!NCDValue_ListRead(i->args, 2, &where_arg, &what_arg)) {
  972. ModuleLog(i, BLOG_ERROR, "wrong arity");
  973. goto fail0;
  974. }
  975. struct instance *mo = ((NCDModuleInst *)i->method_user)->inst_user;
  976. struct value *mov = valref_val(&mo->ref);
  977. if (!mov) {
  978. ModuleLog(i, BLOG_ERROR, "value was deleted");
  979. goto fail0;
  980. }
  981. struct insert_undo_deinit_data *data = malloc(sizeof(*data));
  982. if (!data) {
  983. ModuleLog(i, BLOG_ERROR, "malloc failed");
  984. goto fail0;
  985. }
  986. struct value *oldv;
  987. struct value *v = value_insert(i, mov, where_arg, what_arg, &oldv);
  988. if (!v) {
  989. goto fail1;
  990. }
  991. valref_init(&data->val_ref, v);
  992. valref_init(&data->oldval_ref, oldv);
  993. func_new_common(i, v, (value_deinit_func)insert_undo_deinit_func, data);
  994. return;
  995. fail1:
  996. free(data);
  997. fail0:
  998. NCDModuleInst_Backend_SetError(i);
  999. NCDModuleInst_Backend_Dead(i);
  1000. }
  1001. static void remove_func_new (NCDModuleInst *i)
  1002. {
  1003. NCDValue *where_arg;
  1004. if (!NCDValue_ListRead(i->args, 1, &where_arg)) {
  1005. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1006. goto fail0;
  1007. }
  1008. struct instance *mo = ((NCDModuleInst *)i->method_user)->inst_user;
  1009. struct value *mov = valref_val(&mo->ref);
  1010. if (!mov) {
  1011. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1012. goto fail0;
  1013. }
  1014. if (!value_remove(i, mov, where_arg)) {
  1015. goto fail0;
  1016. }
  1017. NCDModuleInst_Backend_Up(i);
  1018. return;
  1019. fail0:
  1020. NCDModuleInst_Backend_SetError(i);
  1021. NCDModuleInst_Backend_Dead(i);
  1022. }
  1023. static void delete_func_new (NCDModuleInst *i)
  1024. {
  1025. if (!NCDValue_ListRead(i->args, 0)) {
  1026. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1027. goto fail0;
  1028. }
  1029. struct instance *mo = ((NCDModuleInst *)i->method_user)->inst_user;
  1030. struct value *mov = valref_val(&mo->ref);
  1031. if (!mov) {
  1032. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1033. goto fail0;
  1034. }
  1035. value_delete(mov);
  1036. NCDModuleInst_Backend_Up(i);
  1037. return;
  1038. fail0:
  1039. NCDModuleInst_Backend_SetError(i);
  1040. NCDModuleInst_Backend_Dead(i);
  1041. }
  1042. static const struct NCDModule modules[] = {
  1043. {
  1044. .type = "value",
  1045. .func_new = func_new_value,
  1046. .func_die = func_die,
  1047. .func_getvar = func_getvar
  1048. }, {
  1049. .type = "value::get",
  1050. .base_type = "value",
  1051. .func_new = func_new_get,
  1052. .func_die = func_die,
  1053. .func_getvar = func_getvar
  1054. }, {
  1055. .type = "value::try_get",
  1056. .base_type = "value",
  1057. .func_new = func_new_try_get,
  1058. .func_die = func_die,
  1059. .func_getvar = func_getvar
  1060. }, {
  1061. .type = "value::getpath",
  1062. .base_type = "value",
  1063. .func_new = func_new_getpath,
  1064. .func_die = func_die,
  1065. .func_getvar = func_getvar
  1066. }, {
  1067. .type = "value::insert",
  1068. .base_type = "value",
  1069. .func_new = func_new_insert,
  1070. .func_die = func_die,
  1071. .func_getvar = func_getvar
  1072. }, {
  1073. .type = "value::insert_undo",
  1074. .base_type = "value",
  1075. .func_new = func_new_insert_undo,
  1076. .func_die = func_die,
  1077. .func_getvar = func_getvar
  1078. }, {
  1079. .type = "value::remove",
  1080. .func_new = remove_func_new
  1081. }, {
  1082. .type = "value::delete",
  1083. .func_new = delete_func_new
  1084. }, {
  1085. .type = NULL
  1086. }
  1087. };
  1088. const struct NCDModuleGroup ncdmodule_value = {
  1089. .modules = modules
  1090. };