value.c 54 KB

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