value.c 54 KB

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