value.c 48 KB

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