value.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  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::replace(where, what)
  38. * value value::replace_this(value)
  39. * value value::insert_undo(where, what)
  40. * value value::replace_undo(where, what)
  41. * value value::replace_this_undo(value)
  42. *
  43. * Description:
  44. * Value objects allow examining and manipulating values.
  45. * These value objects are actually references to internal value structures, which
  46. * may be shared between value objects.
  47. *
  48. * value(value) constructs a new value object from the given value.
  49. *
  50. * value::get(where) constructs a value object for the element at position 'where'
  51. * (for a list), or the value corresponding to key 'where' (for a map). It is an
  52. * error if the base value is not a list or a map, the index is out of bounds of
  53. * the list, or the key does not exist in the map.
  54. * The resulting value object is NOT a copy, and shares (part of) the same
  55. * underlying value structure as the base value object. Deleting it will remove
  56. * it from the list or map it is part of.
  57. *
  58. * value::try_get(where) is like get(), except that if any restriction on 'where'
  59. * is violated, no error is triggered; instead, the value object is constructed
  60. * as being deleted; this state is exposed via the 'exists' variable.
  61. * This can be used to check for the presence of a key in a map, and in case it
  62. * exists, allow access to the corresponding value without another get() statement.
  63. *
  64. * value::getpath(path) is like get(), except that it performs multiple
  65. * consecutive resolutions. Also, if the path is an empty list, it performs
  66. * no resulution at all.
  67. *
  68. * value::insert(where, what) constructs a value object by inserting into an
  69. * existing value object.
  70. * For lists, 'where' is the index of the element to insert before, or the length
  71. * of the list to append to it.
  72. * For maps, 'where' is the key to insert under. If the key already exists in the
  73. * map, its value is replaced; any references to the old value however remain valid.
  74. *
  75. * value::replace(where, what) is like insert(), exept that, when inserting into a
  76. * list, the value at the specified index is replaced with the new value (unless
  77. * the index is equal to the length of the list).
  78. *
  79. * insert_undo() and replace_undo() are versions of insert() and replace() which
  80. * attempt to revert the modifications when they deinitialize.
  81. * Specifically, they work like that:
  82. * - On initiialization, they take an internal reference to the value being replaced
  83. * (if any; note that insert_undo() into a list never replaces a value).
  84. * - On deinitialization, they remove the the inserted value from its parent (if there
  85. * is one), and insert the old replaced value (to which a reference was kept) in that
  86. * place (if any, and assuming it has not been deleted).
  87. * Note that if the inserted value changes parents in between init and deinit, the
  88. * result of undoing may be unexpected.
  89. *
  90. * Variables:
  91. * (empty) - the value stored in the value object
  92. * type - type of the value; "string", "list" or "map"
  93. * length - number of elements in the list or map, or the number of bytes in a
  94. * string
  95. * keys - a list of keys in the map (only if the value is a map)
  96. * exists - "true" or "false", reflecting whether the value object holds a value
  97. * (is not in deleted state)
  98. *
  99. * Synopsis:
  100. * value::remove(where)
  101. * value::delete()
  102. *
  103. * Description:
  104. * value::remove(where) removes from an existing value object.
  105. * For lists, 'where' is the index of the element to remove, and must be in range.
  106. * For maps, 'where' is the key to remove, and must be an existing key.
  107. * In any case, any references to the removed value remain valid.
  108. *
  109. * value::delete() deletes the underlying value data of this value object.
  110. * After delection, the value object enters a deleted state, which will cause any
  111. * operation on it to fail. Any other value objects which referred to the same value
  112. * or parts of it will too enter deleted state. If the value was an element
  113. * in a list or map, is is removed from it.
  114. *
  115. * Synopsis:
  116. * value value::substr(string start [, string length])
  117. *
  118. * Description:
  119. * Constructs a string value by extracting a part of a string.
  120. * 'start' specifies the index of the character (from zero) where the substring to
  121. * extract starts, and must be <= the length of the string.
  122. * 'length' specifies the maximum number of characters to extract, if given.
  123. * The newly constructed value is a copy of the extracted substring.
  124. * The value must be a string value.
  125. *
  126. * Synopsis:
  127. * value::reset(what)
  128. *
  129. * Description:
  130. * Effectively deconstructs and reconstructs the value object. More precisely,
  131. * it builds a new value structure from 'what', possibly invokes a scheduled undo
  132. * operation (as scheduled by insert_undo() and replace_undo()), sets up this
  133. * value object to reference the newly built value structure, without any scheduled
  134. * undo operation.
  135. */
  136. #include <stdlib.h>
  137. #include <string.h>
  138. #include <stddef.h>
  139. #include <limits.h>
  140. #include <inttypes.h>
  141. #include <misc/offset.h>
  142. #include <misc/debug.h>
  143. #include <misc/parse_number.h>
  144. #include <misc/balloc.h>
  145. #include <structure/LinkedList0.h>
  146. #include <structure/IndexedList.h>
  147. #include <structure/SAvl.h>
  148. #include <ncd/NCDModule.h>
  149. #include <generated/blog_channel_ncd_value.h>
  150. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  151. struct value;
  152. #include "value_maptree.h"
  153. #include <structure/SAvl_decl.h>
  154. struct valref {
  155. struct value *v;
  156. LinkedList0Node refs_list_node;
  157. };
  158. typedef void (*value_deinit_func) (void *deinit_data, NCDModuleInst *i);
  159. struct instance {
  160. NCDModuleInst *i;
  161. struct valref ref;
  162. value_deinit_func deinit_func;
  163. void *deinit_data;
  164. };
  165. struct value {
  166. LinkedList0 refs_list;
  167. struct value *parent;
  168. union {
  169. struct {
  170. IndexedListNode list_contents_il_node;
  171. } list_parent;
  172. struct {
  173. NCDValMem key_mem;
  174. NCDValRef key;
  175. MapTreeNode maptree_node;
  176. } map_parent;
  177. };
  178. int type;
  179. union {
  180. struct {
  181. uint8_t *string;
  182. size_t length;
  183. } string;
  184. struct {
  185. IndexedList list_contents_il;
  186. } list;
  187. struct {
  188. MapTree map_tree;
  189. } map;
  190. };
  191. };
  192. static const char * get_type_str (int type);
  193. static void value_cleanup (struct value *v);
  194. static void value_delete (struct value *v);
  195. static struct value * value_init_string (NCDModuleInst *i, const uint8_t *str, size_t len);
  196. static struct value * value_init_list (NCDModuleInst *i);
  197. static size_t value_list_len (struct value *v);
  198. static struct value * value_list_at (struct value *v, size_t index);
  199. static size_t value_list_indexof (struct value *v, struct value *ev);
  200. static int value_list_insert (NCDModuleInst *i, struct value *list, struct value *v, size_t index);
  201. static void value_list_remove (struct value *list, struct value *v);
  202. static struct value * value_init_map (NCDModuleInst *i);
  203. static size_t value_map_len (struct value *map);
  204. static struct value * value_map_at (struct value *map, size_t index);
  205. static struct value * value_map_find (struct value *map, NCDValRef key);
  206. static int value_map_insert (struct value *map, struct value *v, NCDValMem mem, NCDValSafeRef key, NCDModuleInst *i);
  207. static void value_map_remove (struct value *map, struct value *v);
  208. static void value_map_remove2 (struct value *map, struct value *v, NCDValMem *out_mem, NCDValSafeRef *out_key);
  209. static struct value * value_init_fromvalue (NCDModuleInst *i, NCDValRef value);
  210. static int value_to_value (NCDModuleInst *i, struct value *v, NCDValMem *mem, NCDValRef *out_value);
  211. static struct value * value_get (NCDModuleInst *i, struct value *v, NCDValRef where, int no_error);
  212. static struct value * value_get_path (NCDModuleInst *i, struct value *v, NCDValRef path);
  213. static struct value * value_insert (NCDModuleInst *i, struct value *v, NCDValRef where, NCDValRef what, int is_replace, struct value **out_oldv);
  214. static int value_remove (NCDModuleInst *i, struct value *v, NCDValRef where);
  215. static void valref_init (struct valref *r, struct value *v);
  216. static void valref_free (struct valref *r);
  217. static struct value * valref_val (struct valref *r);
  218. static void valref_break (struct valref *r);
  219. #include "value_maptree.h"
  220. #include <structure/SAvl_impl.h>
  221. static const char * get_type_str (int type)
  222. {
  223. switch (type) {
  224. case NCDVAL_STRING: return "string";
  225. case NCDVAL_LIST: return "list";
  226. case NCDVAL_MAP: return "map";
  227. }
  228. ASSERT(0)
  229. return NULL;
  230. }
  231. static void value_cleanup (struct value *v)
  232. {
  233. if (v->parent || !LinkedList0_IsEmpty(&v->refs_list)) {
  234. return;
  235. }
  236. switch (v->type) {
  237. case NCDVAL_STRING: {
  238. BFree(v->string.string);
  239. } break;
  240. case NCDVAL_LIST: {
  241. while (value_list_len(v) > 0) {
  242. struct value *ev = value_list_at(v, 0);
  243. value_list_remove(v, ev);
  244. value_cleanup(ev);
  245. }
  246. } break;
  247. case NCDVAL_MAP: {
  248. while (value_map_len(v) > 0) {
  249. struct value *ev = value_map_at(v, 0);
  250. value_map_remove(v, ev);
  251. value_cleanup(ev);
  252. }
  253. } break;
  254. default: ASSERT(0);
  255. }
  256. free(v);
  257. }
  258. static void value_delete (struct value *v)
  259. {
  260. if (v->parent) {
  261. switch (v->parent->type) {
  262. case NCDVAL_LIST: {
  263. value_list_remove(v->parent, v);
  264. } break;
  265. case NCDVAL_MAP: {
  266. value_map_remove(v->parent, v);
  267. } break;
  268. default: ASSERT(0);
  269. }
  270. }
  271. LinkedList0Node *ln;
  272. while (ln = LinkedList0_GetFirst(&v->refs_list)) {
  273. struct valref *r = UPPER_OBJECT(ln, struct valref, refs_list_node);
  274. ASSERT(r->v == v)
  275. valref_break(r);
  276. }
  277. switch (v->type) {
  278. case NCDVAL_STRING: {
  279. BFree(v->string.string);
  280. } break;
  281. case NCDVAL_LIST: {
  282. while (value_list_len(v) > 0) {
  283. struct value *ev = value_list_at(v, 0);
  284. value_delete(ev);
  285. }
  286. } break;
  287. case NCDVAL_MAP: {
  288. while (value_map_len(v) > 0) {
  289. struct value *ev = value_map_at(v, 0);
  290. value_delete(ev);
  291. }
  292. } break;
  293. default: ASSERT(0);
  294. }
  295. free(v);
  296. }
  297. static struct value * value_init_string (NCDModuleInst *i, const uint8_t *str, size_t len)
  298. {
  299. struct value *v = malloc(sizeof(*v));
  300. if (!v) {
  301. ModuleLog(i, BLOG_ERROR, "malloc failed");
  302. goto fail0;
  303. }
  304. LinkedList0_Init(&v->refs_list);
  305. v->parent = NULL;
  306. v->type = NCDVAL_STRING;
  307. if (!(v->string.string = BAlloc(len))) {
  308. ModuleLog(i, BLOG_ERROR, "BAlloc failed");
  309. goto fail1;
  310. }
  311. memcpy(v->string.string, str, len);
  312. v->string.length = len;
  313. return v;
  314. fail1:
  315. free(v);
  316. fail0:
  317. return NULL;
  318. }
  319. static struct value * value_init_list (NCDModuleInst *i)
  320. {
  321. struct value *v = malloc(sizeof(*v));
  322. if (!v) {
  323. ModuleLog(i, BLOG_ERROR, "malloc failed");
  324. return NULL;
  325. }
  326. LinkedList0_Init(&v->refs_list);
  327. v->parent = NULL;
  328. v->type = NCDVAL_LIST;
  329. IndexedList_Init(&v->list.list_contents_il);
  330. return v;
  331. }
  332. static size_t value_list_len (struct value *v)
  333. {
  334. ASSERT(v->type == NCDVAL_LIST)
  335. return IndexedList_Count(&v->list.list_contents_il);
  336. }
  337. static struct value * value_list_at (struct value *v, size_t index)
  338. {
  339. ASSERT(v->type == NCDVAL_LIST)
  340. ASSERT(index < value_list_len(v))
  341. IndexedListNode *iln = IndexedList_GetAt(&v->list.list_contents_il, index);
  342. ASSERT(iln)
  343. struct value *e = UPPER_OBJECT(iln, struct value, list_parent.list_contents_il_node);
  344. ASSERT(e->parent == v)
  345. return e;
  346. }
  347. static size_t value_list_indexof (struct value *v, struct value *ev)
  348. {
  349. ASSERT(v->type == NCDVAL_LIST)
  350. ASSERT(ev->parent == v)
  351. uint64_t index = IndexedList_IndexOf(&v->list.list_contents_il, &ev->list_parent.list_contents_il_node);
  352. ASSERT(index < value_list_len(v))
  353. return index;
  354. }
  355. static int value_list_insert (NCDModuleInst *i, struct value *list, struct value *v, size_t index)
  356. {
  357. ASSERT(list->type == NCDVAL_LIST)
  358. ASSERT(!v->parent)
  359. ASSERT(index <= value_list_len(list))
  360. if (value_list_len(list) == SIZE_MAX) {
  361. ModuleLog(i, BLOG_ERROR, "list has too many elements");
  362. return 0;
  363. }
  364. IndexedList_InsertAt(&list->list.list_contents_il, &v->list_parent.list_contents_il_node, index);
  365. v->parent = list;
  366. return 1;
  367. }
  368. static void value_list_remove (struct value *list, struct value *v)
  369. {
  370. ASSERT(list->type == NCDVAL_LIST)
  371. ASSERT(v->parent == list)
  372. IndexedList_Remove(&list->list.list_contents_il, &v->list_parent.list_contents_il_node);
  373. v->parent = NULL;
  374. }
  375. static struct value * value_init_map (NCDModuleInst *i)
  376. {
  377. struct value *v = malloc(sizeof(*v));
  378. if (!v) {
  379. ModuleLog(i, BLOG_ERROR, "malloc failed");
  380. return NULL;
  381. }
  382. LinkedList0_Init(&v->refs_list);
  383. v->parent = NULL;
  384. v->type = NCDVAL_MAP;
  385. MapTree_Init(&v->map.map_tree);
  386. return v;
  387. }
  388. static size_t value_map_len (struct value *map)
  389. {
  390. ASSERT(map->type == NCDVAL_MAP)
  391. return MapTree_Count(&map->map.map_tree, 0);
  392. }
  393. static struct value * value_map_at (struct value *map, size_t index)
  394. {
  395. ASSERT(map->type == NCDVAL_MAP)
  396. ASSERT(index < value_map_len(map))
  397. struct value *e = MapTree_GetAt(&map->map.map_tree, 0, index);
  398. ASSERT(e)
  399. ASSERT(e->parent == map)
  400. return e;
  401. }
  402. static struct value * value_map_find (struct value *map, NCDValRef key)
  403. {
  404. ASSERT(map->type == NCDVAL_MAP)
  405. ASSERT(NCDVal_Type(key))
  406. struct value *e = MapTree_LookupExact(&map->map.map_tree, 0, key);
  407. ASSERT(!e || e->parent == map)
  408. return e;
  409. }
  410. static int value_map_insert (struct value *map, struct value *v, NCDValMem mem, NCDValSafeRef key, NCDModuleInst *i)
  411. {
  412. ASSERT(map->type == NCDVAL_MAP)
  413. ASSERT(!v->parent)
  414. ASSERT((NCDVal_Type(NCDVal_FromSafe(&mem, key)), 1))
  415. ASSERT(!value_map_find(map, NCDVal_FromSafe(&mem, key)))
  416. if (value_map_len(map) == SIZE_MAX) {
  417. ModuleLog(i, BLOG_ERROR, "map has too many elements");
  418. return 0;
  419. }
  420. v->map_parent.key_mem = mem;
  421. v->map_parent.key = NCDVal_FromSafe(&v->map_parent.key_mem, key);
  422. int res = MapTree_Insert(&map->map.map_tree, 0, v, NULL);
  423. ASSERT_EXECUTE(res)
  424. v->parent = map;
  425. return 1;
  426. }
  427. static void value_map_remove (struct value *map, struct value *v)
  428. {
  429. ASSERT(map->type == NCDVAL_MAP)
  430. ASSERT(v->parent == map)
  431. MapTree_Remove(&map->map.map_tree, 0, v);
  432. NCDValMem_Free(&v->map_parent.key_mem);
  433. v->parent = NULL;
  434. }
  435. static void value_map_remove2 (struct value *map, struct value *v, NCDValMem *out_mem, NCDValSafeRef *out_key)
  436. {
  437. ASSERT(map->type == NCDVAL_MAP)
  438. ASSERT(v->parent == map)
  439. ASSERT(out_mem)
  440. ASSERT(out_key)
  441. MapTree_Remove(&map->map.map_tree, 0, v);
  442. *out_mem = v->map_parent.key_mem;
  443. *out_key = NCDVal_ToSafe(v->map_parent.key);
  444. v->parent = NULL;
  445. }
  446. static struct value * value_init_fromvalue (NCDModuleInst *i, NCDValRef value)
  447. {
  448. ASSERT((NCDVal_Type(value), 1))
  449. struct value *v;
  450. switch (NCDVal_Type(value)) {
  451. case NCDVAL_STRING: {
  452. if (!(v = value_init_string(i, (const uint8_t *)NCDVal_StringValue(value), NCDVal_StringLength(value)))) {
  453. goto fail0;
  454. }
  455. } break;
  456. case NCDVAL_LIST: {
  457. if (!(v = value_init_list(i))) {
  458. goto fail0;
  459. }
  460. size_t count = NCDVal_ListCount(value);
  461. for (size_t j = 0; j < count; j++) {
  462. struct value *ev = value_init_fromvalue(i, NCDVal_ListGet(value, j));
  463. if (!ev) {
  464. goto fail1;
  465. }
  466. if (!value_list_insert(i, v, ev, value_list_len(v))) {
  467. value_cleanup(ev);
  468. goto fail1;
  469. }
  470. }
  471. } break;
  472. case NCDVAL_MAP: {
  473. if (!(v = value_init_map(i))) {
  474. goto fail0;
  475. }
  476. for (NCDValMapElem e = NCDVal_MapFirst(value); !NCDVal_MapElemInvalid(e); e = NCDVal_MapNext(value, e)) {
  477. NCDValRef ekey = NCDVal_MapElemKey(value, e);
  478. NCDValRef eval = NCDVal_MapElemVal(value, e);
  479. NCDValMem key_mem;
  480. NCDValMem_Init(&key_mem);
  481. NCDValRef key = NCDVal_NewCopy(&key_mem, ekey);
  482. if (NCDVal_IsInvalid(key)) {
  483. BLog(BLOG_ERROR, "NCDVal_NewCopy failed");
  484. NCDValMem_Free(&key_mem);
  485. goto fail1;
  486. }
  487. struct value *ev = value_init_fromvalue(i, eval);
  488. if (!ev) {
  489. NCDValMem_Free(&key_mem);
  490. goto fail1;
  491. }
  492. if (!value_map_insert(v, ev, key_mem, NCDVal_ToSafe(key), i)) {
  493. NCDValMem_Free(&key_mem);
  494. value_cleanup(ev);
  495. goto fail1;
  496. }
  497. }
  498. } break;
  499. default:
  500. ASSERT(0);
  501. return NULL;
  502. }
  503. return v;
  504. fail1:
  505. value_cleanup(v);
  506. fail0:
  507. return NULL;
  508. }
  509. static int value_to_value (NCDModuleInst *i, struct value *v, NCDValMem *mem, NCDValRef *out_value)
  510. {
  511. ASSERT(mem)
  512. ASSERT(out_value)
  513. switch (v->type) {
  514. case NCDVAL_STRING: {
  515. *out_value = NCDVal_NewStringBin(mem, v->string.string, v->string.length);
  516. if (NCDVal_IsInvalid(*out_value)) {
  517. ModuleLog(i, BLOG_ERROR, "NCDVal_NewStringBin failed");
  518. goto fail;
  519. }
  520. } break;
  521. case NCDVAL_LIST: {
  522. *out_value = NCDVal_NewList(mem, value_list_len(v));
  523. if (NCDVal_IsInvalid(*out_value)) {
  524. ModuleLog(i, BLOG_ERROR, "NCDVal_NewList failed");
  525. goto fail;
  526. }
  527. for (size_t index = 0; index < value_list_len(v); index++) {
  528. NCDValRef eval;
  529. if (!value_to_value(i, value_list_at(v, index), mem, &eval)) {
  530. goto fail;
  531. }
  532. NCDVal_ListAppend(*out_value, eval);
  533. }
  534. } break;
  535. case NCDVAL_MAP: {
  536. *out_value = NCDVal_NewMap(mem, value_map_len(v));
  537. if (NCDVal_IsInvalid(*out_value)) {
  538. ModuleLog(i, BLOG_ERROR, "NCDVal_NewMap failed");
  539. goto fail;
  540. }
  541. for (size_t index = 0; index < value_map_len(v); index++) {
  542. struct value *ev = value_map_at(v, index);
  543. NCDValRef key = NCDVal_NewCopy(mem, ev->map_parent.key);
  544. if (NCDVal_IsInvalid(key)) {
  545. ModuleLog(i, BLOG_ERROR, "NCDVal_NewCopy failed");
  546. goto fail;
  547. }
  548. NCDValRef val;
  549. if (!value_to_value(i, ev, mem, &val)) {
  550. goto fail;
  551. }
  552. int res = NCDVal_MapInsert(*out_value, key, val);
  553. ASSERT_EXECUTE(res)
  554. }
  555. } break;
  556. default: ASSERT(0);
  557. }
  558. return 1;
  559. fail:
  560. return 0;
  561. }
  562. static struct value * value_get (NCDModuleInst *i, struct value *v, NCDValRef where, int no_error)
  563. {
  564. ASSERT((NCDVal_Type(where), 1))
  565. switch (v->type) {
  566. case NCDVAL_STRING: {
  567. if (!no_error) ModuleLog(i, BLOG_ERROR, "cannot resolve into a string");
  568. goto fail;
  569. } break;
  570. case NCDVAL_LIST: {
  571. uintmax_t index;
  572. if (!NCDVal_IsStringNoNulls(where) || !parse_unsigned_integer(NCDVal_StringValue(where), &index)) {
  573. if (!no_error) ModuleLog(i, BLOG_ERROR, "index is not a valid number (resolving into list)");
  574. goto fail;
  575. }
  576. if (index >= value_list_len(v)) {
  577. if (!no_error) ModuleLog(i, BLOG_ERROR, "index is out of bounds (resolving into list)");
  578. goto fail;
  579. }
  580. v = value_list_at(v, index);
  581. } break;
  582. case NCDVAL_MAP: {
  583. v = value_map_find(v, where);
  584. if (!v) {
  585. if (!no_error) ModuleLog(i, BLOG_ERROR, "key does not exist (resolving into map)");
  586. goto fail;
  587. }
  588. } break;
  589. default: ASSERT(0);
  590. }
  591. return v;
  592. fail:
  593. return NULL;
  594. }
  595. static struct value * value_get_path (NCDModuleInst *i, struct value *v, NCDValRef path)
  596. {
  597. ASSERT(NCDVal_IsList(path))
  598. size_t count = NCDVal_ListCount(path);
  599. for (size_t j = 0; j < count; j++) {
  600. if (!(v = value_get(i, v, NCDVal_ListGet(path, j), 0))) {
  601. goto fail;
  602. }
  603. }
  604. return v;
  605. fail:
  606. return NULL;
  607. }
  608. static struct value * value_insert (NCDModuleInst *i, struct value *v, NCDValRef where, NCDValRef what, int is_replace, struct value **out_oldv)
  609. {
  610. ASSERT(v)
  611. ASSERT((NCDVal_Type(where), 1))
  612. ASSERT((NCDVal_Type(what), 1))
  613. ASSERT(is_replace == !!is_replace)
  614. struct value *nv = value_init_fromvalue(i, what);
  615. if (!nv) {
  616. goto fail0;
  617. }
  618. struct value *oldv = NULL;
  619. switch (v->type) {
  620. case NCDVAL_STRING: {
  621. ModuleLog(i, BLOG_ERROR, "cannot insert into a string");
  622. goto fail1;
  623. } break;
  624. case NCDVAL_LIST: {
  625. uintmax_t index;
  626. if (!NCDVal_IsStringNoNulls(where) || !parse_unsigned_integer(NCDVal_StringValue(where), &index)) {
  627. ModuleLog(i, BLOG_ERROR, "index is not a valid number (inserting into list)");
  628. goto fail1;
  629. }
  630. if (index > value_list_len(v)) {
  631. ModuleLog(i, BLOG_ERROR, "index is out of bounds (inserting into list)");
  632. goto fail1;
  633. }
  634. if (is_replace && index < value_list_len(v)) {
  635. oldv = value_list_at(v, index);
  636. value_list_remove(v, oldv);
  637. int res = value_list_insert(i, v, nv, index);
  638. ASSERT_EXECUTE(res)
  639. } else {
  640. if (!value_list_insert(i, v, nv, index)) {
  641. goto fail1;
  642. }
  643. }
  644. } break;
  645. case NCDVAL_MAP: {
  646. oldv = value_map_find(v, where);
  647. if (!oldv && value_map_len(v) == SIZE_MAX) {
  648. ModuleLog(i, BLOG_ERROR, "map has too many elements");
  649. goto fail1;
  650. }
  651. NCDValMem key_mem;
  652. NCDValMem_Init(&key_mem);
  653. NCDValRef key = NCDVal_NewCopy(&key_mem, where);
  654. if (NCDVal_IsInvalid(key)) {
  655. ModuleLog(i, BLOG_ERROR, "NCDVal_NewCopy failed");
  656. NCDValMem_Free(&key_mem);
  657. goto fail1;
  658. }
  659. if (oldv) {
  660. value_map_remove(v, oldv);
  661. }
  662. int res = value_map_insert(v, nv, key_mem, NCDVal_ToSafe(key), i);
  663. ASSERT_EXECUTE(res)
  664. } break;
  665. default: ASSERT(0);
  666. }
  667. if (out_oldv) {
  668. *out_oldv = oldv;
  669. }
  670. else if (oldv) {
  671. value_cleanup(oldv);
  672. }
  673. return nv;
  674. fail1:
  675. value_cleanup(nv);
  676. fail0:
  677. return NULL;
  678. }
  679. static int value_remove (NCDModuleInst *i, struct value *v, NCDValRef where)
  680. {
  681. ASSERT(v)
  682. ASSERT((NCDVal_Type(where), 1))
  683. switch (v->type) {
  684. case NCDVAL_STRING: {
  685. ModuleLog(i, BLOG_ERROR, "cannot remove from a string");
  686. goto fail;
  687. } break;
  688. case NCDVAL_LIST: {
  689. uintmax_t index;
  690. if (!NCDVal_IsStringNoNulls(where) || !parse_unsigned_integer(NCDVal_StringValue(where), &index)) {
  691. ModuleLog(i, BLOG_ERROR, "index is not a valid number (removing from list)");
  692. goto fail;
  693. }
  694. if (index >= value_list_len(v)) {
  695. ModuleLog(i, BLOG_ERROR, "index is out of bounds (removing from list)");
  696. goto fail;
  697. }
  698. struct value *ov = value_list_at(v, index);
  699. value_list_remove(v, ov);
  700. value_cleanup(ov);
  701. } break;
  702. case NCDVAL_MAP: {
  703. struct value *ov = value_map_find(v, where);
  704. if (!ov) {
  705. ModuleLog(i, BLOG_ERROR, "key does not exist (removing from map)");
  706. goto fail;
  707. }
  708. value_map_remove(v, ov);
  709. value_cleanup(ov);
  710. } break;
  711. default: ASSERT(0);
  712. }
  713. return 1;
  714. fail:
  715. return 0;
  716. }
  717. static void valref_init (struct valref *r, struct value *v)
  718. {
  719. r->v = v;
  720. if (v) {
  721. LinkedList0_Prepend(&v->refs_list, &r->refs_list_node);
  722. }
  723. }
  724. static void valref_free (struct valref *r)
  725. {
  726. if (r->v) {
  727. LinkedList0_Remove(&r->v->refs_list, &r->refs_list_node);
  728. value_cleanup(r->v);
  729. }
  730. }
  731. static struct value * valref_val (struct valref *r)
  732. {
  733. return r->v;
  734. }
  735. static void valref_break (struct valref *r)
  736. {
  737. ASSERT(r->v)
  738. LinkedList0_Remove(&r->v->refs_list, &r->refs_list_node);
  739. r->v = NULL;
  740. }
  741. static void func_new_common (void *vo, NCDModuleInst *i, struct value *v, value_deinit_func deinit_func, void *deinit_data)
  742. {
  743. struct instance *o = vo;
  744. o->i = i;
  745. // init value references
  746. valref_init(&o->ref, v);
  747. // remember deinit
  748. o->deinit_func = deinit_func;
  749. o->deinit_data = deinit_data;
  750. NCDModuleInst_Backend_Up(i);
  751. return;
  752. }
  753. static void func_die (void *vo)
  754. {
  755. struct instance *o = vo;
  756. // deinit
  757. if (o->deinit_func) {
  758. o->deinit_func(o->deinit_data, o->i);
  759. }
  760. // free value reference
  761. valref_free(&o->ref);
  762. NCDModuleInst_Backend_Dead(o->i);
  763. }
  764. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  765. {
  766. struct instance *o = vo;
  767. struct value *v = valref_val(&o->ref);
  768. if (!strcmp(name, "exists")) {
  769. const char *str = v ? "true" : "false";
  770. *out = NCDVal_NewString(mem, str);
  771. if (NCDVal_IsInvalid(*out)) {
  772. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  773. }
  774. return 1;
  775. }
  776. if (strcmp(name, "type") && strcmp(name, "length") && strcmp(name, "keys") && strcmp(name, "")) {
  777. return 0;
  778. }
  779. if (!v) {
  780. ModuleLog(o->i, BLOG_ERROR, "value was deleted");
  781. return 0;
  782. }
  783. if (!strcmp(name, "type")) {
  784. *out = NCDVal_NewString(mem, get_type_str(v->type));
  785. if (NCDVal_IsInvalid(*out)) {
  786. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  787. }
  788. }
  789. else if (!strcmp(name, "length")) {
  790. size_t len;
  791. switch (v->type) {
  792. case NCDVAL_LIST:
  793. len = value_list_len(v);
  794. break;
  795. case NCDVAL_MAP:
  796. len = value_map_len(v);
  797. break;
  798. case NCDVAL_STRING:
  799. len = v->string.length;
  800. break;
  801. default:
  802. ASSERT(0);
  803. }
  804. char str[64];
  805. snprintf(str, sizeof(str), "%zu", len);
  806. *out = NCDVal_NewString(mem, str);
  807. if (NCDVal_IsInvalid(*out)) {
  808. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  809. }
  810. }
  811. else if (!strcmp(name, "keys")) {
  812. if (v->type != NCDVAL_MAP) {
  813. ModuleLog(o->i, BLOG_ERROR, "value is not a map (reading keys variable)");
  814. return 0;
  815. }
  816. *out = NCDVal_NewList(mem, value_map_len(v));
  817. if (NCDVal_IsInvalid(*out)) {
  818. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewList failed");
  819. goto fail;
  820. }
  821. for (size_t j = 0; j < value_map_len(v); j++) {
  822. struct value *ev = value_map_at(v, j);
  823. NCDValRef key = NCDVal_NewCopy(mem, ev->map_parent.key);
  824. if (NCDVal_IsInvalid(key)) {
  825. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
  826. goto fail;
  827. }
  828. NCDVal_ListAppend(*out, key);
  829. }
  830. }
  831. else if (!strcmp(name, "")) {
  832. if (!value_to_value(o->i, v, mem, out)) {
  833. return 0;
  834. }
  835. }
  836. else {
  837. ASSERT(0);
  838. }
  839. return 1;
  840. fail:
  841. *out = NCDVal_NewInvalid();
  842. return 1;
  843. }
  844. static void func_new_value (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  845. {
  846. NCDValRef value_arg;
  847. if (!NCDVal_ListRead(params->args, 1, &value_arg)) {
  848. ModuleLog(i, BLOG_ERROR, "wrong arity");
  849. goto fail0;
  850. }
  851. struct value *v = value_init_fromvalue(i, value_arg);
  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_get (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  862. {
  863. NCDValRef where_arg;
  864. if (!NCDVal_ListRead(params->args, 1, &where_arg)) {
  865. ModuleLog(i, BLOG_ERROR, "wrong arity");
  866. goto fail0;
  867. }
  868. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->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, 0);
  875. if (!v) {
  876. goto fail0;
  877. }
  878. func_new_common(vo, i, v, NULL, NULL);
  879. return;
  880. fail0:
  881. NCDModuleInst_Backend_SetError(i);
  882. NCDModuleInst_Backend_Dead(i);
  883. }
  884. static void func_new_try_get (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  885. {
  886. NCDValRef where_arg;
  887. if (!NCDVal_ListRead(params->args, 1, &where_arg)) {
  888. ModuleLog(i, BLOG_ERROR, "wrong arity");
  889. goto fail0;
  890. }
  891. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  892. struct value *mov = valref_val(&mo->ref);
  893. if (!mov) {
  894. ModuleLog(i, BLOG_ERROR, "value was deleted");
  895. goto fail0;
  896. }
  897. struct value *v = value_get(i, mov, where_arg, 1);
  898. func_new_common(vo, i, v, NULL, NULL);
  899. return;
  900. fail0:
  901. NCDModuleInst_Backend_SetError(i);
  902. NCDModuleInst_Backend_Dead(i);
  903. }
  904. static void func_new_getpath (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  905. {
  906. NCDValRef path_arg;
  907. if (!NCDVal_ListRead(params->args, 1, &path_arg)) {
  908. ModuleLog(i, BLOG_ERROR, "wrong arity");
  909. goto fail0;
  910. }
  911. if (!NCDVal_IsList(path_arg)) {
  912. ModuleLog(i, BLOG_ERROR, "wrong type");
  913. goto fail0;
  914. }
  915. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  916. struct value *mov = valref_val(&mo->ref);
  917. if (!mov) {
  918. ModuleLog(i, BLOG_ERROR, "value was deleted");
  919. goto fail0;
  920. }
  921. struct value *v = value_get_path(i, mov, path_arg);
  922. if (!v) {
  923. goto fail0;
  924. }
  925. func_new_common(vo, i, v, NULL, NULL);
  926. return;
  927. fail0:
  928. NCDModuleInst_Backend_SetError(i);
  929. NCDModuleInst_Backend_Dead(i);
  930. }
  931. static void func_new_insert_replace_common (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, int is_replace)
  932. {
  933. NCDValRef where_arg;
  934. NCDValRef what_arg;
  935. if (!NCDVal_ListRead(params->args, 2, &where_arg, &what_arg)) {
  936. ModuleLog(i, BLOG_ERROR, "wrong arity");
  937. goto fail0;
  938. }
  939. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  940. struct value *mov = valref_val(&mo->ref);
  941. if (!mov) {
  942. ModuleLog(i, BLOG_ERROR, "value was deleted");
  943. goto fail0;
  944. }
  945. struct value *v = value_insert(i, mov, where_arg, what_arg, is_replace, NULL);
  946. if (!v) {
  947. goto fail0;
  948. }
  949. func_new_common(vo, i, v, NULL, NULL);
  950. return;
  951. fail0:
  952. NCDModuleInst_Backend_SetError(i);
  953. NCDModuleInst_Backend_Dead(i);
  954. }
  955. static void func_new_insert (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  956. {
  957. func_new_insert_replace_common(vo, i, params, 0);
  958. }
  959. static void func_new_replace (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  960. {
  961. func_new_insert_replace_common(vo, i, params, 1);
  962. }
  963. struct insert_undo_deinit_data {
  964. struct valref val_ref;
  965. struct valref oldval_ref;
  966. };
  967. static void undo_deinit_func (struct insert_undo_deinit_data *data, NCDModuleInst *i)
  968. {
  969. struct value *val = valref_val(&data->val_ref);
  970. struct value *oldval = valref_val(&data->oldval_ref);
  971. if (val && val->parent && (!oldval || !oldval->parent)) {
  972. // get parent
  973. struct value *parent = val->parent;
  974. // remove this value from parent and restore saved one (or none)
  975. switch (parent->type) {
  976. case NCDVAL_LIST: {
  977. size_t index = value_list_indexof(parent, val);
  978. value_list_remove(parent, val);
  979. if (oldval) {
  980. int res = value_list_insert(i, parent, oldval, index);
  981. ASSERT_EXECUTE(res)
  982. }
  983. } break;
  984. case NCDVAL_MAP: {
  985. NCDValMem key_mem;
  986. NCDValSafeRef key;
  987. value_map_remove2(parent, val, &key_mem, &key);
  988. if (oldval) {
  989. int res = value_map_insert(parent, oldval, key_mem, key, i);
  990. ASSERT_EXECUTE(res)
  991. } else {
  992. NCDValMem_Free(&key_mem);
  993. }
  994. } break;
  995. default: ASSERT(0);
  996. }
  997. }
  998. valref_free(&data->oldval_ref);
  999. valref_free(&data->val_ref);
  1000. free(data);
  1001. }
  1002. static void func_new_insert_replace_undo_common (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, int is_replace)
  1003. {
  1004. NCDValRef where_arg;
  1005. NCDValRef what_arg;
  1006. if (!NCDVal_ListRead(params->args, 2, &where_arg, &what_arg)) {
  1007. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1008. goto fail0;
  1009. }
  1010. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1011. struct value *mov = valref_val(&mo->ref);
  1012. if (!mov) {
  1013. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1014. goto fail0;
  1015. }
  1016. struct insert_undo_deinit_data *data = malloc(sizeof(*data));
  1017. if (!data) {
  1018. ModuleLog(i, BLOG_ERROR, "malloc failed");
  1019. goto fail0;
  1020. }
  1021. struct value *oldv;
  1022. struct value *v = value_insert(i, mov, where_arg, what_arg, is_replace, &oldv);
  1023. if (!v) {
  1024. goto fail1;
  1025. }
  1026. valref_init(&data->val_ref, v);
  1027. valref_init(&data->oldval_ref, oldv);
  1028. func_new_common(vo, i, v, (value_deinit_func)undo_deinit_func, data);
  1029. return;
  1030. fail1:
  1031. free(data);
  1032. fail0:
  1033. NCDModuleInst_Backend_SetError(i);
  1034. NCDModuleInst_Backend_Dead(i);
  1035. }
  1036. static void func_new_insert_undo (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1037. {
  1038. func_new_insert_replace_undo_common(vo, i, params, 0);
  1039. }
  1040. static void func_new_replace_undo (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1041. {
  1042. func_new_insert_replace_undo_common(vo, i, params, 1);
  1043. }
  1044. static void func_new_replace_this (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1045. {
  1046. NCDValRef value_arg;
  1047. if (!NCDVal_ListRead(params->args, 1, &value_arg)) {
  1048. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1049. goto fail0;
  1050. }
  1051. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->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. struct value *v = value_init_fromvalue(i, value_arg);
  1058. if (!v) {
  1059. goto fail0;
  1060. }
  1061. if (mov->parent) {
  1062. struct value *parent = mov->parent;
  1063. switch (parent->type) {
  1064. case NCDVAL_LIST: {
  1065. size_t index = value_list_indexof(parent, mov);
  1066. value_list_remove(parent, mov);
  1067. int res = value_list_insert(i, parent, v, index);
  1068. ASSERT_EXECUTE(res)
  1069. } break;
  1070. case NCDVAL_MAP: {
  1071. NCDValMem key_mem;
  1072. NCDValSafeRef key;
  1073. value_map_remove2(parent, mov, &key_mem, &key);
  1074. int res = value_map_insert(parent, v, key_mem, key, i);
  1075. ASSERT_EXECUTE(res)
  1076. } break;
  1077. default: ASSERT(0);
  1078. }
  1079. value_cleanup(mov);
  1080. }
  1081. func_new_common(vo, i, v, NULL, NULL);
  1082. return;
  1083. fail0:
  1084. NCDModuleInst_Backend_SetError(i);
  1085. NCDModuleInst_Backend_Dead(i);
  1086. }
  1087. static void func_new_replace_this_undo (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1088. {
  1089. NCDValRef value_arg;
  1090. if (!NCDVal_ListRead(params->args, 1, &value_arg)) {
  1091. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1092. goto fail0;
  1093. }
  1094. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1095. struct value *mov = valref_val(&mo->ref);
  1096. if (!mov) {
  1097. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1098. goto fail0;
  1099. }
  1100. struct value *v = value_init_fromvalue(i, value_arg);
  1101. if (!v) {
  1102. goto fail0;
  1103. }
  1104. struct insert_undo_deinit_data *data = malloc(sizeof(*data));
  1105. if (!data) {
  1106. ModuleLog(i, BLOG_ERROR, "malloc failed");
  1107. goto fail1;
  1108. }
  1109. valref_init(&data->val_ref, v);
  1110. valref_init(&data->oldval_ref, mov);
  1111. if (mov->parent) {
  1112. struct value *parent = mov->parent;
  1113. switch (parent->type) {
  1114. case NCDVAL_LIST: {
  1115. size_t index = value_list_indexof(parent, mov);
  1116. value_list_remove(parent, mov);
  1117. int res = value_list_insert(i, parent, v, index);
  1118. ASSERT_EXECUTE(res)
  1119. } break;
  1120. case NCDVAL_MAP: {
  1121. NCDValMem key_mem;
  1122. NCDValSafeRef key;
  1123. value_map_remove2(parent, mov, &key_mem, &key);
  1124. int res = value_map_insert(parent, v, key_mem, key, i);
  1125. ASSERT_EXECUTE(res)
  1126. } break;
  1127. default: ASSERT(0);
  1128. }
  1129. }
  1130. func_new_common(vo, i, v, (value_deinit_func)undo_deinit_func, data);
  1131. return;
  1132. fail1:
  1133. value_cleanup(v);
  1134. fail0:
  1135. NCDModuleInst_Backend_SetError(i);
  1136. NCDModuleInst_Backend_Dead(i);
  1137. }
  1138. static void func_new_substr (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1139. {
  1140. NCDValRef start_arg;
  1141. NCDValRef length_arg = NCDVal_NewInvalid();
  1142. if (!NCDVal_ListRead(params->args, 1, &start_arg) &&
  1143. !NCDVal_ListRead(params->args, 2, &start_arg, &length_arg)) {
  1144. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1145. goto fail0;
  1146. }
  1147. if (!NCDVal_IsStringNoNulls(start_arg) || (!NCDVal_IsInvalid(length_arg) && !NCDVal_IsStringNoNulls(length_arg))) {
  1148. ModuleLog(i, BLOG_ERROR, "wrong type");
  1149. goto fail0;
  1150. }
  1151. uintmax_t start;
  1152. if (!parse_unsigned_integer(NCDVal_StringValue(start_arg), &start)) {
  1153. ModuleLog(i, BLOG_ERROR, "start is not a number");
  1154. goto fail0;
  1155. }
  1156. uintmax_t length = UINTMAX_MAX;
  1157. if (!NCDVal_IsInvalid(length_arg) && !parse_unsigned_integer(NCDVal_StringValue(length_arg), &length)) {
  1158. ModuleLog(i, BLOG_ERROR, "length is not a number");
  1159. goto fail0;
  1160. }
  1161. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1162. struct value *mov = valref_val(&mo->ref);
  1163. if (!mov) {
  1164. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1165. goto fail0;
  1166. }
  1167. if (mov->type != NCDVAL_STRING) {
  1168. ModuleLog(i, BLOG_ERROR, "value is not a string");
  1169. goto fail0;
  1170. }
  1171. if (start > mov->string.length) {
  1172. ModuleLog(i, BLOG_ERROR, "start is out of range");
  1173. goto fail0;
  1174. }
  1175. size_t remain = mov->string.length - start;
  1176. size_t amount = length < remain ? length : remain;
  1177. struct value *v = value_init_string(i, mov->string.string + start, amount);
  1178. if (!v) {
  1179. goto fail0;
  1180. }
  1181. func_new_common(vo, i, v, NULL, NULL);
  1182. return;
  1183. fail0:
  1184. NCDModuleInst_Backend_SetError(i);
  1185. NCDModuleInst_Backend_Dead(i);
  1186. }
  1187. static void remove_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1188. {
  1189. NCDValRef where_arg;
  1190. if (!NCDVal_ListRead(params->args, 1, &where_arg)) {
  1191. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1192. goto fail0;
  1193. }
  1194. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1195. struct value *mov = valref_val(&mo->ref);
  1196. if (!mov) {
  1197. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1198. goto fail0;
  1199. }
  1200. if (!value_remove(i, mov, where_arg)) {
  1201. goto fail0;
  1202. }
  1203. NCDModuleInst_Backend_Up(i);
  1204. return;
  1205. fail0:
  1206. NCDModuleInst_Backend_SetError(i);
  1207. NCDModuleInst_Backend_Dead(i);
  1208. }
  1209. static void delete_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1210. {
  1211. if (!NCDVal_ListRead(params->args, 0)) {
  1212. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1213. goto fail0;
  1214. }
  1215. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1216. struct value *mov = valref_val(&mo->ref);
  1217. if (!mov) {
  1218. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1219. goto fail0;
  1220. }
  1221. value_delete(mov);
  1222. NCDModuleInst_Backend_Up(i);
  1223. return;
  1224. fail0:
  1225. NCDModuleInst_Backend_SetError(i);
  1226. NCDModuleInst_Backend_Dead(i);
  1227. }
  1228. static void reset_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1229. {
  1230. NCDValRef what_arg;
  1231. if (!NCDVal_ListRead(params->args, 1, &what_arg)) {
  1232. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1233. goto fail0;
  1234. }
  1235. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1236. // build value from argument
  1237. struct value *newv = value_init_fromvalue(i, what_arg);
  1238. if (!newv) {
  1239. goto fail0;
  1240. }
  1241. // deinit
  1242. if (mo->deinit_func) {
  1243. mo->deinit_func(mo->deinit_data, i);
  1244. }
  1245. // free value reference
  1246. valref_free(&mo->ref);
  1247. // set up value reference
  1248. valref_init(&mo->ref, newv);
  1249. // set no deinit function
  1250. mo->deinit_func = NULL;
  1251. NCDModuleInst_Backend_Up(i);
  1252. return;
  1253. fail0:
  1254. NCDModuleInst_Backend_SetError(i);
  1255. NCDModuleInst_Backend_Dead(i);
  1256. }
  1257. static struct NCDModule modules[] = {
  1258. {
  1259. .type = "value",
  1260. .func_new2 = func_new_value,
  1261. .func_die = func_die,
  1262. .func_getvar = func_getvar,
  1263. .alloc_size = sizeof(struct instance)
  1264. }, {
  1265. .type = "value::get",
  1266. .base_type = "value",
  1267. .func_new2 = func_new_get,
  1268. .func_die = func_die,
  1269. .func_getvar = func_getvar,
  1270. .alloc_size = sizeof(struct instance)
  1271. }, {
  1272. .type = "value::try_get",
  1273. .base_type = "value",
  1274. .func_new2 = func_new_try_get,
  1275. .func_die = func_die,
  1276. .func_getvar = func_getvar,
  1277. .alloc_size = sizeof(struct instance)
  1278. }, {
  1279. .type = "value::getpath",
  1280. .base_type = "value",
  1281. .func_new2 = func_new_getpath,
  1282. .func_die = func_die,
  1283. .func_getvar = func_getvar,
  1284. .alloc_size = sizeof(struct instance)
  1285. }, {
  1286. .type = "value::insert",
  1287. .base_type = "value",
  1288. .func_new2 = func_new_insert,
  1289. .func_die = func_die,
  1290. .func_getvar = func_getvar,
  1291. .alloc_size = sizeof(struct instance)
  1292. }, {
  1293. .type = "value::replace",
  1294. .base_type = "value",
  1295. .func_new2 = func_new_replace,
  1296. .func_die = func_die,
  1297. .func_getvar = func_getvar,
  1298. .alloc_size = sizeof(struct instance)
  1299. }, {
  1300. .type = "value::replace_this",
  1301. .base_type = "value",
  1302. .func_new2 = func_new_replace_this,
  1303. .func_die = func_die,
  1304. .func_getvar = func_getvar,
  1305. .alloc_size = sizeof(struct instance)
  1306. }, {
  1307. .type = "value::insert_undo",
  1308. .base_type = "value",
  1309. .func_new2 = func_new_insert_undo,
  1310. .func_die = func_die,
  1311. .func_getvar = func_getvar,
  1312. .alloc_size = sizeof(struct instance)
  1313. }, {
  1314. .type = "value::replace_undo",
  1315. .base_type = "value",
  1316. .func_new2 = func_new_replace_undo,
  1317. .func_die = func_die,
  1318. .func_getvar = func_getvar,
  1319. .alloc_size = sizeof(struct instance)
  1320. }, {
  1321. .type = "value::replace_this_undo",
  1322. .base_type = "value",
  1323. .func_new2 = func_new_replace_this_undo,
  1324. .func_die = func_die,
  1325. .func_getvar = func_getvar,
  1326. .alloc_size = sizeof(struct instance)
  1327. }, {
  1328. .type = "value::remove",
  1329. .func_new2 = remove_func_new
  1330. }, {
  1331. .type = "value::delete",
  1332. .func_new2 = delete_func_new
  1333. }, {
  1334. .type = "value::reset",
  1335. .func_new2 = reset_func_new
  1336. }, {
  1337. .type = "value::substr",
  1338. .base_type = "value",
  1339. .func_new2 = func_new_substr,
  1340. .func_die = func_die,
  1341. .func_getvar = func_getvar,
  1342. .alloc_size = sizeof(struct instance)
  1343. }, {
  1344. .type = NULL
  1345. }
  1346. };
  1347. const struct NCDModuleGroup ncdmodule_value = {
  1348. .modules = modules
  1349. };