value.c 43 KB

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