value.c 40 KB

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