value.c 37 KB

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