value.c 40 KB

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