value.c 50 KB

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