value.c 40 KB

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