value.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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(what)
  34. * value value::getpath(list path)
  35. *
  36. * Description:
  37. * Value objects allow examining and manipulating values.
  38. *
  39. * value(value) constructs a new value object from the given value.
  40. *
  41. * value::get(what) constructs a value object for the element at position 'what'
  42. * (for a list), or the value corresponding to key 'what' (for a map). It is an
  43. * error if the base value is not a list or a map, the index is out of bounds of
  44. * the list, or the key does not exist in the map.
  45. * The resulting value object is NOT a copy, and shares (part of) the same
  46. * underlying value structure as the base value object. Deleting it will remove
  47. * it from the list or map it is part of.
  48. *
  49. * value::getpath(path) is like get(), except that it performs multiple
  50. * consecutive resolutions. Also, if the path is an empty list, it performs
  51. * no resulution at all.
  52. *
  53. * Variables:
  54. * (empty) - the value stored in the value object
  55. * type - type of the value; "string", "list" or "map"
  56. * length - number of elements in the list or map (only if the value if a list
  57. * or a map)
  58. * keys - a list of keys in the map (only if the value is a map)
  59. *
  60. * Synopsis:
  61. * value::delete()
  62. *
  63. * Description:
  64. * Deletes the underlying value data of this value object. After delection,
  65. * the value object enters a deleted state, which will cause any operation
  66. * on it to fail. Any other value objects which referred to the same value
  67. * or parts of it will too enter deleted state. If the value was an element
  68. * in a list or map, is is removed from it.
  69. */
  70. #include <stdlib.h>
  71. #include <string.h>
  72. #include <stddef.h>
  73. #include <limits.h>
  74. #include <misc/offset.h>
  75. #include <misc/debug.h>
  76. #include <misc/parse_number.h>
  77. #include <structure/LinkedList0.h>
  78. #include <structure/IndexedList.h>
  79. #include <structure/BCountAVL.h>
  80. #include <ncd/NCDModule.h>
  81. #include <generated/blog_channel_ncd_value.h>
  82. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  83. struct value;
  84. struct instance {
  85. NCDModuleInst *i;
  86. struct value *v;
  87. LinkedList0Node refs_list_node;
  88. };
  89. struct value {
  90. LinkedList0 refs_list;
  91. struct value *parent;
  92. union {
  93. struct {
  94. IndexedListNode list_contents_il_node;
  95. } list_parent;
  96. struct {
  97. NCDValue key;
  98. BCountAVLNode map_contents_tree_node;
  99. } map_parent;
  100. };
  101. int type;
  102. union {
  103. struct {
  104. char *string;
  105. } string;
  106. struct {
  107. IndexedList list_contents_il;
  108. } list;
  109. struct {
  110. BCountAVL map_contents_tree;
  111. } map;
  112. };
  113. };
  114. static int ncdvalue_comparator (void *unused, void *vv1, void *vv2);
  115. static const char * get_type_str (int type);
  116. static void value_cleanup (struct value *v);
  117. static void value_delete (struct value *v);
  118. static struct value * value_init_string (NCDModuleInst *i, const char *str);
  119. static struct value * value_init_list (NCDModuleInst *i);
  120. static size_t value_list_len (struct value *v);
  121. static struct value * value_list_at (struct value *v, size_t index);
  122. static int value_list_insert (NCDModuleInst *i, struct value *list, struct value *v, size_t index);
  123. static void value_list_remove (struct value *list, struct value *v);
  124. static struct value * value_init_map (NCDModuleInst *i);
  125. static size_t value_map_len (struct value *map);
  126. static struct value * value_map_at (struct value *map, size_t index);
  127. static struct value * value_map_find (struct value *map, NCDValue *key);
  128. static int value_map_insert (struct value *map, struct value *v, NCDValue key, NCDModuleInst *i);
  129. static void value_map_remove (struct value *map, struct value *v);
  130. static struct value * value_init_fromvalue (NCDModuleInst *i, NCDValue *value);
  131. static int value_to_value (NCDModuleInst *i, struct value *v, NCDValue *out_value);
  132. static struct value * value_get (NCDModuleInst *i, struct value *v, NCDValue *what);
  133. static struct value * value_get_path (NCDModuleInst *i, struct value *v, NCDValue *path);
  134. static int ncdvalue_comparator (void *unused, void *vv1, void *vv2)
  135. {
  136. NCDValue *v1 = vv1;
  137. NCDValue *v2 = vv2;
  138. return NCDValue_Compare(v1, v2);
  139. }
  140. static const char * get_type_str (int type)
  141. {
  142. switch (type) {
  143. case NCDVALUE_STRING: return "string";
  144. case NCDVALUE_LIST: return "list";
  145. case NCDVALUE_MAP: return "map";
  146. }
  147. ASSERT(0)
  148. return NULL;
  149. }
  150. static void value_cleanup (struct value *v)
  151. {
  152. if (v->parent || !LinkedList0_IsEmpty(&v->refs_list)) {
  153. return;
  154. }
  155. switch (v->type) {
  156. case NCDVALUE_STRING: {
  157. free(v->string.string);
  158. } break;
  159. case NCDVALUE_LIST: {
  160. while (value_list_len(v) > 0) {
  161. struct value *ev = value_list_at(v, 0);
  162. value_list_remove(v, ev);
  163. value_cleanup(ev);
  164. }
  165. } break;
  166. case NCDVALUE_MAP: {
  167. while (value_map_len(v) > 0) {
  168. struct value *ev = value_map_at(v, 0);
  169. value_map_remove(v, ev);
  170. value_cleanup(ev);
  171. }
  172. } break;
  173. default: ASSERT(0);
  174. }
  175. free(v);
  176. }
  177. static void value_delete (struct value *v)
  178. {
  179. if (v->parent) {
  180. switch (v->parent->type) {
  181. case NCDVALUE_LIST: {
  182. value_list_remove(v->parent, v);
  183. } break;
  184. case NCDVALUE_MAP: {
  185. value_map_remove(v->parent, v);
  186. } break;
  187. default: ASSERT(0);
  188. }
  189. }
  190. LinkedList0Node *ln;
  191. while (ln = LinkedList0_GetFirst(&v->refs_list)) {
  192. struct instance *inst = UPPER_OBJECT(ln, struct instance, refs_list_node);
  193. ASSERT(inst->v == v)
  194. LinkedList0_Remove(&v->refs_list, &inst->refs_list_node);
  195. inst->v = NULL;
  196. }
  197. switch (v->type) {
  198. case NCDVALUE_STRING: {
  199. free(v->string.string);
  200. } break;
  201. case NCDVALUE_LIST: {
  202. while (value_list_len(v) > 0) {
  203. struct value *ev = value_list_at(v, 0);
  204. value_delete(ev);
  205. }
  206. } break;
  207. case NCDVALUE_MAP: {
  208. while (value_map_len(v) > 0) {
  209. struct value *ev = value_map_at(v, 0);
  210. value_delete(ev);
  211. }
  212. } break;
  213. default: ASSERT(0);
  214. }
  215. free(v);
  216. }
  217. static struct value * value_init_string (NCDModuleInst *i, const char *str)
  218. {
  219. struct value *v = malloc(sizeof(*v));
  220. if (!v) {
  221. ModuleLog(i, BLOG_ERROR, "malloc failed");
  222. goto fail0;
  223. }
  224. LinkedList0_Init(&v->refs_list);
  225. v->parent = NULL;
  226. v->type = NCDVALUE_STRING;
  227. if (!(v->string.string = strdup(str))) {
  228. ModuleLog(i, BLOG_ERROR, "strdup failed");
  229. goto fail1;
  230. }
  231. return v;
  232. fail1:
  233. free(v);
  234. fail0:
  235. return NULL;
  236. }
  237. static struct value * value_init_list (NCDModuleInst *i)
  238. {
  239. struct value *v = malloc(sizeof(*v));
  240. if (!v) {
  241. ModuleLog(i, BLOG_ERROR, "malloc failed");
  242. return NULL;
  243. }
  244. LinkedList0_Init(&v->refs_list);
  245. v->parent = NULL;
  246. v->type = NCDVALUE_LIST;
  247. IndexedList_Init(&v->list.list_contents_il);
  248. return v;
  249. }
  250. static size_t value_list_len (struct value *v)
  251. {
  252. ASSERT(v->type == NCDVALUE_LIST)
  253. return IndexedList_Count(&v->list.list_contents_il);
  254. }
  255. static struct value * value_list_at (struct value *v, size_t index)
  256. {
  257. ASSERT(v->type == NCDVALUE_LIST)
  258. ASSERT(index < value_list_len(v))
  259. IndexedListNode *iln = IndexedList_GetAt(&v->list.list_contents_il, index);
  260. ASSERT(iln)
  261. struct value *e = UPPER_OBJECT(iln, struct value, list_parent.list_contents_il_node);
  262. ASSERT(e->parent == v)
  263. return e;
  264. }
  265. static int value_list_insert (NCDModuleInst *i, struct value *list, struct value *v, size_t index)
  266. {
  267. ASSERT(list->type == NCDVALUE_LIST)
  268. ASSERT(!v->parent)
  269. ASSERT(index <= value_list_len(list))
  270. if (value_list_len(list) == SIZE_MAX) {
  271. ModuleLog(i, BLOG_ERROR, "list has too many elements");
  272. return 0;
  273. }
  274. IndexedList_InsertAt(&list->list.list_contents_il, &v->list_parent.list_contents_il_node, index);
  275. v->parent = list;
  276. return 1;
  277. }
  278. static void value_list_remove (struct value *list, struct value *v)
  279. {
  280. ASSERT(list->type == NCDVALUE_LIST)
  281. ASSERT(v->parent == list)
  282. IndexedList_Remove(&list->list.list_contents_il, &v->list_parent.list_contents_il_node);
  283. v->parent = NULL;
  284. }
  285. static struct value * value_init_map (NCDModuleInst *i)
  286. {
  287. struct value *v = malloc(sizeof(*v));
  288. if (!v) {
  289. ModuleLog(i, BLOG_ERROR, "malloc failed");
  290. return NULL;
  291. }
  292. LinkedList0_Init(&v->refs_list);
  293. v->parent = NULL;
  294. v->type = NCDVALUE_MAP;
  295. BCountAVL_Init(&v->map.map_contents_tree, OFFSET_DIFF(struct value, map_parent.key, map_parent.map_contents_tree_node), ncdvalue_comparator, NULL);
  296. return v;
  297. }
  298. static size_t value_map_len (struct value *map)
  299. {
  300. ASSERT(map->type == NCDVALUE_MAP)
  301. return BCountAVL_Count(&map->map.map_contents_tree);
  302. }
  303. static struct value * value_map_at (struct value *map, size_t index)
  304. {
  305. ASSERT(map->type == NCDVALUE_MAP)
  306. ASSERT(index < value_map_len(map))
  307. BCountAVLNode *tn = BCountAVL_GetAt(&map->map.map_contents_tree, index);
  308. ASSERT(tn)
  309. struct value *e = UPPER_OBJECT(tn, struct value, map_parent.map_contents_tree_node);
  310. ASSERT(e->parent == map)
  311. return e;
  312. }
  313. static struct value * value_map_find (struct value *map, NCDValue *key)
  314. {
  315. ASSERT(map->type == NCDVALUE_MAP)
  316. ASSERT(key)
  317. BCountAVLNode *tn = BCountAVL_LookupExact(&map->map.map_contents_tree, key);
  318. if (!tn) {
  319. return NULL;
  320. }
  321. struct value *e = UPPER_OBJECT(tn, struct value, map_parent.map_contents_tree_node);
  322. ASSERT(e->parent == map)
  323. return e;
  324. }
  325. static int value_map_insert (struct value *map, struct value *v, NCDValue key, NCDModuleInst *i)
  326. {
  327. ASSERT(map->type == NCDVALUE_MAP)
  328. ASSERT(!v->parent)
  329. ASSERT(!value_map_find(map, &key))
  330. if (value_map_len(map) == SIZE_MAX) {
  331. ModuleLog(i, BLOG_ERROR, "map has too many elements");
  332. return 0;
  333. }
  334. v->map_parent.key = key;
  335. int res = BCountAVL_Insert(&map->map.map_contents_tree, &v->map_parent.map_contents_tree_node, NULL);
  336. ASSERT(res)
  337. v->parent = map;
  338. return 1;
  339. }
  340. static void value_map_remove (struct value *map, struct value *v)
  341. {
  342. ASSERT(map->type == NCDVALUE_MAP)
  343. ASSERT(v->parent == map)
  344. BCountAVL_Remove(&map->map.map_contents_tree, &v->map_parent.map_contents_tree_node);
  345. NCDValue_Free(&v->map_parent.key);
  346. v->parent = NULL;
  347. }
  348. static struct value * value_init_fromvalue (NCDModuleInst *i, NCDValue *value)
  349. {
  350. struct value *v;
  351. switch (NCDValue_Type(value)) {
  352. case NCDVALUE_STRING: {
  353. if (!(v = value_init_string(i, NCDValue_StringValue(value)))) {
  354. goto fail0;
  355. }
  356. } break;
  357. case NCDVALUE_LIST: {
  358. if (!(v = value_init_list(i))) {
  359. goto fail0;
  360. }
  361. for (NCDValue *eval = NCDValue_ListFirst(value); eval; eval = NCDValue_ListNext(value, eval)) {
  362. struct value *ev = value_init_fromvalue(i, eval);
  363. if (!ev) {
  364. goto fail1;
  365. }
  366. if (!value_list_insert(i, v, ev, value_list_len(v))) {
  367. value_cleanup(ev);
  368. goto fail1;
  369. }
  370. }
  371. } break;
  372. case NCDVALUE_MAP: {
  373. if (!(v = value_init_map(i))) {
  374. goto fail0;
  375. }
  376. for (NCDValue *ekey = NCDValue_MapFirstKey(value); ekey; ekey = NCDValue_MapNextKey(value, ekey)) {
  377. NCDValue *eval = NCDValue_MapKeyValue(value, ekey);
  378. NCDValue key;
  379. if (!NCDValue_InitCopy(&key, ekey)) {
  380. BLog(BLOG_ERROR, "NCDValue_InitCopy failed");
  381. goto fail1;
  382. }
  383. struct value *ev = value_init_fromvalue(i, eval);
  384. if (!ev) {
  385. NCDValue_Free(&key);
  386. goto fail1;
  387. }
  388. if (!value_map_insert(v, ev, key, i)) {
  389. NCDValue_Free(&key);
  390. value_cleanup(ev);
  391. goto fail1;
  392. }
  393. }
  394. } break;
  395. default: ASSERT(0);
  396. }
  397. return v;
  398. fail1:
  399. value_cleanup(v);
  400. fail0:
  401. return NULL;
  402. }
  403. static int value_to_value (NCDModuleInst *i, struct value *v, NCDValue *out_value)
  404. {
  405. switch (v->type) {
  406. case NCDVALUE_STRING: {
  407. if (!(NCDValue_InitString(out_value, v->string.string))) {
  408. ModuleLog(i, BLOG_ERROR, "NCDValue_InitString failed");
  409. goto fail0;
  410. }
  411. } break;
  412. case NCDVALUE_LIST: {
  413. NCDValue_InitList(out_value);
  414. for (size_t index = 0; index < value_list_len(v); index++) {
  415. NCDValue eval;
  416. if (!value_to_value(i, value_list_at(v, index), &eval)) {
  417. goto fail1;
  418. }
  419. if (!NCDValue_ListAppend(out_value, eval)) {
  420. ModuleLog(i, BLOG_ERROR, "NCDValue_ListAppend failed");
  421. NCDValue_Free(&eval);
  422. goto fail1;
  423. }
  424. }
  425. } break;
  426. case NCDVALUE_MAP: {
  427. NCDValue_InitMap(out_value);
  428. for (size_t index = 0; index < value_map_len(v); index++) {
  429. struct value *ev = value_map_at(v, index);
  430. NCDValue key;
  431. NCDValue val;
  432. if (!NCDValue_InitCopy(&key, &ev->map_parent.key)) {
  433. ModuleLog(i, BLOG_ERROR, "NCDValue_InitCopy failed");
  434. goto fail1;
  435. }
  436. if (!value_to_value(i, ev, &val)) {
  437. NCDValue_Free(&key);
  438. goto fail1;
  439. }
  440. if (!NCDValue_MapInsert(out_value, key, val)) {
  441. ModuleLog(i, BLOG_ERROR, "NCDValue_MapInsert failed");
  442. NCDValue_Free(&key);
  443. NCDValue_Free(&val);
  444. goto fail1;
  445. }
  446. }
  447. } break;
  448. default: ASSERT(0);
  449. }
  450. return 1;
  451. fail1:
  452. NCDValue_Free(out_value);
  453. fail0:
  454. return 0;
  455. }
  456. static struct value * value_get (NCDModuleInst *i, struct value *v, NCDValue *what)
  457. {
  458. switch (v->type) {
  459. case NCDVALUE_STRING: {
  460. ModuleLog(i, BLOG_ERROR, "cannot resolve into a string");
  461. goto fail;
  462. } break;
  463. case NCDVALUE_LIST: {
  464. if (NCDValue_Type(what) != NCDVALUE_STRING) {
  465. ModuleLog(i, BLOG_ERROR, "index is not a string (resolving into list)");
  466. goto fail;
  467. }
  468. uintmax_t index;
  469. if (!parse_unsigned_integer(NCDValue_StringValue(what), &index)) {
  470. ModuleLog(i, BLOG_ERROR, "index is not a valid number (resolving into list)");
  471. goto fail;
  472. }
  473. if (index >= value_list_len(v)) {
  474. ModuleLog(i, BLOG_ERROR, "index is out of bounds (resolving into list)");
  475. goto fail;
  476. }
  477. v = value_list_at(v, index);
  478. } break;
  479. case NCDVALUE_MAP: {
  480. v = value_map_find(v, what);
  481. if (!v) {
  482. ModuleLog(i, BLOG_ERROR, "key does not exist (resolving into map)");
  483. goto fail;
  484. }
  485. } break;
  486. default: ASSERT(0);
  487. }
  488. return v;
  489. fail:
  490. return NULL;
  491. }
  492. static struct value * value_get_path (NCDModuleInst *i, struct value *v, NCDValue *path)
  493. {
  494. ASSERT(NCDValue_Type(path) == NCDVALUE_LIST)
  495. for (NCDValue *ev = NCDValue_ListFirst(path); ev; ev = NCDValue_ListNext(path, ev)) {
  496. if (!(v = value_get(i, v, ev))) {
  497. goto fail;
  498. }
  499. }
  500. return v;
  501. fail:
  502. return NULL;
  503. }
  504. static void func_new_common (NCDModuleInst *i, struct value *v)
  505. {
  506. ASSERT(v)
  507. // allocate instance
  508. struct instance *o = malloc(sizeof(*o));
  509. if (!o) {
  510. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  511. goto fail0;
  512. }
  513. o->i = i;
  514. NCDModuleInst_Backend_SetUser(i, o);
  515. // set value
  516. o->v = v;
  517. // add reference
  518. LinkedList0_Prepend(&o->v->refs_list, &o->refs_list_node);
  519. NCDModuleInst_Backend_Up(i);
  520. return;
  521. fail1:
  522. free(o);
  523. fail0:
  524. value_cleanup(v);
  525. NCDModuleInst_Backend_SetError(i);
  526. NCDModuleInst_Backend_Dead(i);
  527. }
  528. static void func_die (void *vo)
  529. {
  530. struct instance *o = vo;
  531. NCDModuleInst *i = o->i;
  532. if (o->v) {
  533. // remove reference
  534. LinkedList0_Remove(&o->v->refs_list, &o->refs_list_node);
  535. // cleanup after removing reference
  536. value_cleanup(o->v);
  537. }
  538. // free instance
  539. free(o);
  540. NCDModuleInst_Backend_Dead(i);
  541. }
  542. static int func_getvar (void *vo, const char *name, NCDValue *out_value)
  543. {
  544. struct instance *o = vo;
  545. if (strcmp(name, "type") && strcmp(name, "length") && strcmp(name, "keys") && strcmp(name, "")) {
  546. return 0;
  547. }
  548. if (!o->v) {
  549. ModuleLog(o->i, BLOG_ERROR, "value was deleted");
  550. return 0;
  551. }
  552. if (!strcmp(name, "type")) {
  553. if (!NCDValue_InitString(out_value, get_type_str(o->v->type))) {
  554. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  555. return 0;
  556. }
  557. }
  558. else if (!strcmp(name, "length")) {
  559. size_t len;
  560. switch (o->v->type) {
  561. case NCDVALUE_LIST:
  562. len = value_list_len(o->v);
  563. break;
  564. case NCDVALUE_MAP:
  565. len = value_map_len(o->v);
  566. break;
  567. default:
  568. ModuleLog(o->i, BLOG_ERROR, "value is not a list or map");
  569. return 0;
  570. }
  571. char str[64];
  572. snprintf(str, sizeof(str), "%zu", len);
  573. if (!NCDValue_InitString(out_value, str)) {
  574. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  575. return 0;
  576. }
  577. }
  578. else if (!strcmp(name, "keys")) {
  579. if (o->v->type != NCDVALUE_MAP) {
  580. ModuleLog(o->i, BLOG_ERROR, "value is not a map (reading keys variable)");
  581. return 0;
  582. }
  583. NCDValue_InitList(out_value);
  584. for (size_t i = 0; i < value_map_len(o->v); i++) {
  585. struct value *ev = value_map_at(o->v, i);
  586. NCDValue key;
  587. if (!NCDValue_InitCopy(&key, &ev->map_parent.key)) {
  588. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  589. goto map_fail1;
  590. }
  591. if (!NCDValue_ListAppend(out_value, key)) {
  592. ModuleLog(o->i, BLOG_ERROR, "NCDValue_ListAppend failed");
  593. NCDValue_Free(&key);
  594. goto map_fail1;
  595. }
  596. }
  597. return 1;
  598. map_fail1:
  599. NCDValue_Free(out_value);
  600. return 0;
  601. }
  602. else if (!strcmp(name, "")) {
  603. if (!value_to_value(o->i, o->v, out_value)) {
  604. return 0;
  605. }
  606. }
  607. else {
  608. ASSERT(0);
  609. }
  610. return 1;
  611. }
  612. static void func_new_value (NCDModuleInst *i)
  613. {
  614. NCDValue *value_arg;
  615. if (!NCDValue_ListRead(i->args, 1, &value_arg)) {
  616. ModuleLog(i, BLOG_ERROR, "wrong arity");
  617. goto fail0;
  618. }
  619. struct value *v = value_init_fromvalue(i, value_arg);
  620. if (!v) {
  621. goto fail0;
  622. }
  623. func_new_common(i, v);
  624. return;
  625. fail0:
  626. NCDModuleInst_Backend_SetError(i);
  627. NCDModuleInst_Backend_Dead(i);
  628. }
  629. static void func_new_get (NCDModuleInst *i)
  630. {
  631. NCDValue *what_arg;
  632. if (!NCDValue_ListRead(i->args, 1, &what_arg)) {
  633. ModuleLog(i, BLOG_ERROR, "wrong arity");
  634. goto fail0;
  635. }
  636. struct instance *mo = ((NCDModuleInst *)i->method_user)->inst_user;
  637. if (!mo->v) {
  638. ModuleLog(i, BLOG_ERROR, "value was deleted");
  639. goto fail0;
  640. }
  641. struct value *v = value_get(i, mo->v, what_arg);
  642. if (!v) {
  643. goto fail0;
  644. }
  645. func_new_common(i, v);
  646. return;
  647. fail0:
  648. NCDModuleInst_Backend_SetError(i);
  649. NCDModuleInst_Backend_Dead(i);
  650. }
  651. static void func_new_getpath (NCDModuleInst *i)
  652. {
  653. NCDValue *path_arg;
  654. if (!NCDValue_ListRead(i->args, 1, &path_arg)) {
  655. ModuleLog(i, BLOG_ERROR, "wrong arity");
  656. goto fail0;
  657. }
  658. if (NCDValue_Type(path_arg) != NCDVALUE_LIST) {
  659. ModuleLog(i, BLOG_ERROR, "wrong type");
  660. goto fail0;
  661. }
  662. struct instance *mo = ((NCDModuleInst *)i->method_user)->inst_user;
  663. if (!mo->v) {
  664. ModuleLog(i, BLOG_ERROR, "value was deleted");
  665. goto fail0;
  666. }
  667. struct value *v = value_get_path(i, mo->v, path_arg);
  668. if (!v) {
  669. goto fail0;
  670. }
  671. func_new_common(i, v);
  672. return;
  673. fail0:
  674. NCDModuleInst_Backend_SetError(i);
  675. NCDModuleInst_Backend_Dead(i);
  676. }
  677. static void delete_func_new (NCDModuleInst *i)
  678. {
  679. if (!NCDValue_ListRead(i->args, 0)) {
  680. ModuleLog(i, BLOG_ERROR, "wrong arity");
  681. goto fail0;
  682. }
  683. struct instance *mo = ((NCDModuleInst *)i->method_user)->inst_user;
  684. if (!mo->v) {
  685. ModuleLog(i, BLOG_ERROR, "value was deleted");
  686. goto fail0;
  687. }
  688. value_delete(mo->v);
  689. NCDModuleInst_Backend_Up(i);
  690. return;
  691. fail0:
  692. NCDModuleInst_Backend_SetError(i);
  693. NCDModuleInst_Backend_Dead(i);
  694. }
  695. static const struct NCDModule modules[] = {
  696. {
  697. .type = "value",
  698. .func_new = func_new_value,
  699. .func_die = func_die,
  700. .func_getvar = func_getvar
  701. }, {
  702. .type = "value::get",
  703. .base_type = "value",
  704. .func_new = func_new_get,
  705. .func_die = func_die,
  706. .func_getvar = func_getvar
  707. }, {
  708. .type = "value::getpath",
  709. .base_type = "value",
  710. .func_new = func_new_getpath,
  711. .func_die = func_die,
  712. .func_getvar = func_getvar
  713. }, {
  714. .type = "value::delete",
  715. .func_new = delete_func_new
  716. }, {
  717. .type = NULL
  718. }
  719. };
  720. const struct NCDModuleGroup ncdmodule_value = {
  721. .modules = modules
  722. };