value.c 41 KB

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