value.c 47 KB

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