value.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  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 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. BLog(BLOG_ERROR, "NCDVal_NewCopy failed");
  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. ModuleLog(i, BLOG_ERROR, "NCDVal_NewStringBin failed");
  572. goto fail;
  573. }
  574. } break;
  575. case IDSTRING_TYPE: {
  576. *out_value = NCDVal_NewIdString(mem, v->idstring.id, v->idstring.string_index);
  577. if (NCDVal_IsInvalid(*out_value)) {
  578. ModuleLog(i, BLOG_ERROR, "NCDVal_NewIdString failed");
  579. goto fail;
  580. }
  581. } break;
  582. case NCDVAL_LIST: {
  583. *out_value = NCDVal_NewList(mem, value_list_len(v));
  584. if (NCDVal_IsInvalid(*out_value)) {
  585. ModuleLog(i, BLOG_ERROR, "NCDVal_NewList failed");
  586. goto fail;
  587. }
  588. for (size_t index = 0; index < value_list_len(v); index++) {
  589. NCDValRef eval;
  590. if (!value_to_value(i, value_list_at(v, index), mem, &eval)) {
  591. goto fail;
  592. }
  593. NCDVal_ListAppend(*out_value, eval);
  594. }
  595. } break;
  596. case NCDVAL_MAP: {
  597. *out_value = NCDVal_NewMap(mem, value_map_len(v));
  598. if (NCDVal_IsInvalid(*out_value)) {
  599. ModuleLog(i, BLOG_ERROR, "NCDVal_NewMap failed");
  600. goto fail;
  601. }
  602. for (size_t index = 0; index < value_map_len(v); index++) {
  603. struct value *ev = value_map_at(v, index);
  604. NCDValRef key = NCDVal_NewCopy(mem, ev->map_parent.key);
  605. if (NCDVal_IsInvalid(key)) {
  606. ModuleLog(i, BLOG_ERROR, "NCDVal_NewCopy failed");
  607. goto fail;
  608. }
  609. NCDValRef val;
  610. if (!value_to_value(i, ev, mem, &val)) {
  611. goto fail;
  612. }
  613. int res = NCDVal_MapInsert(*out_value, key, val);
  614. ASSERT_EXECUTE(res)
  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. ModuleLog(i, BLOG_ERROR, "NCDVal_NewCopy failed");
  719. NCDValMem_Free(&key_mem);
  720. goto fail1;
  721. }
  722. if (oldv) {
  723. value_map_remove(v, oldv);
  724. }
  725. int res = value_map_insert(v, nv, key_mem, NCDVal_ToSafe(key), i);
  726. ASSERT_EXECUTE(res)
  727. } break;
  728. default: ASSERT(0);
  729. }
  730. if (out_oldv) {
  731. *out_oldv = oldv;
  732. }
  733. else if (oldv) {
  734. value_cleanup(oldv);
  735. }
  736. return nv;
  737. fail1:
  738. value_cleanup(nv);
  739. fail0:
  740. return NULL;
  741. }
  742. static struct value * value_insert_simple (NCDModuleInst *i, struct value *v, NCDValRef what)
  743. {
  744. ASSERT(v)
  745. ASSERT((NCDVal_Type(what), 1))
  746. struct value *nv = value_init_fromvalue(i, what);
  747. if (!nv) {
  748. goto fail0;
  749. }
  750. switch (v->type) {
  751. case NCDVAL_LIST: {
  752. if (!value_list_insert(i, v, nv, value_list_len(v))) {
  753. goto fail1;
  754. }
  755. } break;
  756. default:
  757. ModuleLog(i, BLOG_ERROR, "one-argument insert is only defined for lists");
  758. return NULL;
  759. }
  760. return nv;
  761. fail1:
  762. value_cleanup(nv);
  763. fail0:
  764. return NULL;
  765. }
  766. static int value_remove (NCDModuleInst *i, struct value *v, NCDValRef where)
  767. {
  768. ASSERT(v)
  769. ASSERT((NCDVal_Type(where), 1))
  770. switch (v->type) {
  771. case NCDVAL_STRING:
  772. case IDSTRING_TYPE: {
  773. ModuleLog(i, BLOG_ERROR, "cannot remove from a string");
  774. goto fail;
  775. } break;
  776. case NCDVAL_LIST: {
  777. uintmax_t index;
  778. if (!NCDVal_IsString(where) || !ncd_read_uintmax(where, &index)) {
  779. ModuleLog(i, BLOG_ERROR, "index is not a valid number (removing from list)");
  780. goto fail;
  781. }
  782. if (index >= value_list_len(v)) {
  783. ModuleLog(i, BLOG_ERROR, "index is out of bounds (removing from list)");
  784. goto fail;
  785. }
  786. struct value *ov = value_list_at(v, index);
  787. value_list_remove(v, ov);
  788. value_cleanup(ov);
  789. } break;
  790. case NCDVAL_MAP: {
  791. struct value *ov = value_map_find(v, where);
  792. if (!ov) {
  793. ModuleLog(i, BLOG_ERROR, "key does not exist (removing from map)");
  794. goto fail;
  795. }
  796. value_map_remove(v, ov);
  797. value_cleanup(ov);
  798. } break;
  799. default: ASSERT(0);
  800. }
  801. return 1;
  802. fail:
  803. return 0;
  804. }
  805. static int value_append (NCDModuleInst *i, struct value *v, NCDValRef data)
  806. {
  807. ASSERT(v)
  808. ASSERT((NCDVal_Type(data), 1))
  809. switch (v->type) {
  810. case NCDVAL_STRING: {
  811. if (!NCDVal_IsString(data)) {
  812. ModuleLog(i, BLOG_ERROR, "cannot append non-string to string");
  813. return 0;
  814. }
  815. const char *append_string = NCDVal_StringData(data);
  816. size_t append_length = NCDVal_StringLength(data);
  817. if (append_length > SIZE_MAX - v->string.length) {
  818. ModuleLog(i, BLOG_ERROR, "too much data to append");
  819. return 0;
  820. }
  821. size_t new_length = v->string.length + append_length;
  822. char *new_string = BRealloc(v->string.string, new_length);
  823. if (!new_string) {
  824. ModuleLog(i, BLOG_ERROR, "BRealloc failed");
  825. return 0;
  826. }
  827. memcpy(new_string + v->string.length, append_string, append_length);
  828. v->string.string = new_string;
  829. v->string.length = new_length;
  830. } break;
  831. case IDSTRING_TYPE: {
  832. if (!NCDVal_IsString(data)) {
  833. ModuleLog(i, BLOG_ERROR, "cannot append non-string to string");
  834. return 0;
  835. }
  836. const char *append_string = NCDVal_StringData(data);
  837. size_t append_length = NCDVal_StringLength(data);
  838. const char *string = NCDStringIndex_Value(v->idstring.string_index, v->idstring.id);
  839. size_t length = strlen(string);
  840. if (append_length > SIZE_MAX - length) {
  841. ModuleLog(i, BLOG_ERROR, "too much data to append");
  842. return 0;
  843. }
  844. size_t new_length = length + append_length;
  845. char *new_string = BAlloc(new_length);
  846. if (!new_string) {
  847. ModuleLog(i, BLOG_ERROR, "BAlloc failed");
  848. return 0;
  849. }
  850. memcpy(new_string, string, length);
  851. memcpy(new_string + length, append_string, append_length);
  852. v->type = NCDVAL_STRING;
  853. v->string.string = new_string;
  854. v->string.length = new_length;
  855. } break;
  856. default:
  857. ModuleLog(i, BLOG_ERROR, "cannot append to non-string");
  858. return 0;
  859. }
  860. return 1;
  861. }
  862. static void valref_init (struct valref *r, struct value *v)
  863. {
  864. r->v = v;
  865. if (v) {
  866. LinkedList0_Prepend(&v->refs_list, &r->refs_list_node);
  867. }
  868. }
  869. static void valref_free (struct valref *r)
  870. {
  871. if (r->v) {
  872. LinkedList0_Remove(&r->v->refs_list, &r->refs_list_node);
  873. value_cleanup(r->v);
  874. }
  875. }
  876. static struct value * valref_val (struct valref *r)
  877. {
  878. return r->v;
  879. }
  880. static void valref_break (struct valref *r)
  881. {
  882. ASSERT(r->v)
  883. LinkedList0_Remove(&r->v->refs_list, &r->refs_list_node);
  884. r->v = NULL;
  885. }
  886. static void func_new_common (void *vo, NCDModuleInst *i, struct value *v, value_deinit_func deinit_func, void *deinit_data)
  887. {
  888. struct instance *o = vo;
  889. o->i = i;
  890. // init value references
  891. valref_init(&o->ref, v);
  892. // remember deinit
  893. o->deinit_func = deinit_func;
  894. o->deinit_data = deinit_data;
  895. NCDModuleInst_Backend_Up(i);
  896. return;
  897. }
  898. static void func_die (void *vo)
  899. {
  900. struct instance *o = vo;
  901. // deinit
  902. if (o->deinit_func) {
  903. o->deinit_func(o->deinit_data, o->i);
  904. }
  905. // free value reference
  906. valref_free(&o->ref);
  907. NCDModuleInst_Backend_Dead(o->i);
  908. }
  909. static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  910. {
  911. struct instance *o = vo;
  912. struct value *v = valref_val(&o->ref);
  913. if (name == strings[STRING_EXISTS].id) {
  914. const char *str = v ? "true" : "false";
  915. *out = NCDVal_NewString(mem, str);
  916. if (NCDVal_IsInvalid(*out)) {
  917. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  918. }
  919. return 1;
  920. }
  921. if (name != strings[STRING_TYPE].id && name != strings[STRING_LENGTH].id &&
  922. name != strings[STRING_KEYS].id && name != NCD_STRING_EMPTY) {
  923. return 0;
  924. }
  925. if (!v) {
  926. ModuleLog(o->i, BLOG_ERROR, "value was deleted");
  927. return 0;
  928. }
  929. if (name == strings[STRING_TYPE].id) {
  930. *out = NCDVal_NewString(mem, get_type_str(v->type));
  931. if (NCDVal_IsInvalid(*out)) {
  932. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  933. }
  934. }
  935. else if (name == strings[STRING_LENGTH].id) {
  936. size_t len = 0; // to remove warning
  937. switch (v->type) {
  938. case NCDVAL_LIST:
  939. len = value_list_len(v);
  940. break;
  941. case NCDVAL_MAP:
  942. len = value_map_len(v);
  943. break;
  944. case NCDVAL_STRING:
  945. len = v->string.length;
  946. break;
  947. case IDSTRING_TYPE:
  948. len = strlen(NCDStringIndex_Value(v->idstring.string_index, v->idstring.id));
  949. break;
  950. default:
  951. ASSERT(0);
  952. }
  953. *out = ncd_make_uintmax(mem, len);
  954. if (NCDVal_IsInvalid(*out)) {
  955. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  956. }
  957. }
  958. else if (name == strings[STRING_KEYS].id) {
  959. if (v->type != NCDVAL_MAP) {
  960. ModuleLog(o->i, BLOG_ERROR, "value is not a map (reading keys variable)");
  961. return 0;
  962. }
  963. *out = NCDVal_NewList(mem, value_map_len(v));
  964. if (NCDVal_IsInvalid(*out)) {
  965. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewList failed");
  966. goto fail;
  967. }
  968. for (size_t j = 0; j < value_map_len(v); j++) {
  969. struct value *ev = value_map_at(v, j);
  970. NCDValRef key = NCDVal_NewCopy(mem, ev->map_parent.key);
  971. if (NCDVal_IsInvalid(key)) {
  972. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
  973. goto fail;
  974. }
  975. NCDVal_ListAppend(*out, key);
  976. }
  977. }
  978. else if (name == NCD_STRING_EMPTY) {
  979. if (!value_to_value(o->i, v, mem, out)) {
  980. return 0;
  981. }
  982. }
  983. else {
  984. ASSERT(0);
  985. }
  986. return 1;
  987. fail:
  988. *out = NCDVal_NewInvalid();
  989. return 1;
  990. }
  991. static void func_new_value (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  992. {
  993. NCDValRef value_arg;
  994. if (!NCDVal_ListRead(params->args, 1, &value_arg)) {
  995. ModuleLog(i, BLOG_ERROR, "wrong arity");
  996. goto fail0;
  997. }
  998. struct value *v = value_init_fromvalue(i, value_arg);
  999. if (!v) {
  1000. goto fail0;
  1001. }
  1002. func_new_common(vo, i, v, NULL, NULL);
  1003. return;
  1004. fail0:
  1005. NCDModuleInst_Backend_SetError(i);
  1006. NCDModuleInst_Backend_Dead(i);
  1007. }
  1008. static void func_new_get (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1009. {
  1010. NCDValRef where_arg;
  1011. if (!NCDVal_ListRead(params->args, 1, &where_arg)) {
  1012. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1013. goto fail0;
  1014. }
  1015. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1016. struct value *mov = valref_val(&mo->ref);
  1017. if (!mov) {
  1018. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1019. goto fail0;
  1020. }
  1021. struct value *v = value_get(i, mov, where_arg, 0);
  1022. if (!v) {
  1023. goto fail0;
  1024. }
  1025. func_new_common(vo, i, v, NULL, NULL);
  1026. return;
  1027. fail0:
  1028. NCDModuleInst_Backend_SetError(i);
  1029. NCDModuleInst_Backend_Dead(i);
  1030. }
  1031. static void func_new_try_get (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1032. {
  1033. NCDValRef where_arg;
  1034. if (!NCDVal_ListRead(params->args, 1, &where_arg)) {
  1035. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1036. goto fail0;
  1037. }
  1038. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1039. struct value *mov = valref_val(&mo->ref);
  1040. if (!mov) {
  1041. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1042. goto fail0;
  1043. }
  1044. struct value *v = value_get(i, mov, where_arg, 1);
  1045. func_new_common(vo, i, v, NULL, NULL);
  1046. return;
  1047. fail0:
  1048. NCDModuleInst_Backend_SetError(i);
  1049. NCDModuleInst_Backend_Dead(i);
  1050. }
  1051. static void func_new_getpath (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1052. {
  1053. NCDValRef path_arg;
  1054. if (!NCDVal_ListRead(params->args, 1, &path_arg)) {
  1055. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1056. goto fail0;
  1057. }
  1058. if (!NCDVal_IsList(path_arg)) {
  1059. ModuleLog(i, BLOG_ERROR, "wrong type");
  1060. goto fail0;
  1061. }
  1062. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1063. struct value *mov = valref_val(&mo->ref);
  1064. if (!mov) {
  1065. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1066. goto fail0;
  1067. }
  1068. struct value *v = value_get_path(i, mov, path_arg);
  1069. if (!v) {
  1070. goto fail0;
  1071. }
  1072. func_new_common(vo, i, v, NULL, NULL);
  1073. return;
  1074. fail0:
  1075. NCDModuleInst_Backend_SetError(i);
  1076. NCDModuleInst_Backend_Dead(i);
  1077. }
  1078. static void func_new_insert_replace_common (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, int is_replace)
  1079. {
  1080. NCDValRef where_arg = NCDVal_NewInvalid();
  1081. NCDValRef what_arg;
  1082. if (!(!is_replace && NCDVal_ListRead(params->args, 1, &what_arg)) &&
  1083. !NCDVal_ListRead(params->args, 2, &where_arg, &what_arg)) {
  1084. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1085. goto fail0;
  1086. }
  1087. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1088. struct value *mov = valref_val(&mo->ref);
  1089. if (!mov) {
  1090. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1091. goto fail0;
  1092. }
  1093. struct value *v;
  1094. if (NCDVal_IsInvalid(where_arg)) {
  1095. v = value_insert_simple(i, mov, what_arg);
  1096. } else {
  1097. v = value_insert(i, mov, where_arg, what_arg, is_replace, NULL);
  1098. }
  1099. if (!v) {
  1100. goto fail0;
  1101. }
  1102. func_new_common(vo, i, v, NULL, NULL);
  1103. return;
  1104. fail0:
  1105. NCDModuleInst_Backend_SetError(i);
  1106. NCDModuleInst_Backend_Dead(i);
  1107. }
  1108. static void func_new_insert (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1109. {
  1110. func_new_insert_replace_common(vo, i, params, 0);
  1111. }
  1112. static void func_new_replace (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1113. {
  1114. func_new_insert_replace_common(vo, i, params, 1);
  1115. }
  1116. struct insert_undo_deinit_data {
  1117. struct valref val_ref;
  1118. struct valref oldval_ref;
  1119. };
  1120. static void undo_deinit_func (struct insert_undo_deinit_data *data, NCDModuleInst *i)
  1121. {
  1122. struct value *val = valref_val(&data->val_ref);
  1123. struct value *oldval = valref_val(&data->oldval_ref);
  1124. if (val && val->parent && (!oldval || !oldval->parent)) {
  1125. // get parent
  1126. struct value *parent = val->parent;
  1127. // remove this value from parent and restore saved one (or none)
  1128. switch (parent->type) {
  1129. case NCDVAL_LIST: {
  1130. size_t index = value_list_indexof(parent, val);
  1131. value_list_remove(parent, val);
  1132. if (oldval) {
  1133. int res = value_list_insert(i, parent, oldval, index);
  1134. ASSERT_EXECUTE(res)
  1135. }
  1136. } break;
  1137. case NCDVAL_MAP: {
  1138. NCDValMem key_mem;
  1139. NCDValSafeRef key;
  1140. value_map_remove2(parent, val, &key_mem, &key);
  1141. if (oldval) {
  1142. int res = value_map_insert(parent, oldval, key_mem, key, i);
  1143. ASSERT_EXECUTE(res)
  1144. } else {
  1145. NCDValMem_Free(&key_mem);
  1146. }
  1147. } break;
  1148. default: ASSERT(0);
  1149. }
  1150. }
  1151. valref_free(&data->oldval_ref);
  1152. valref_free(&data->val_ref);
  1153. free(data);
  1154. }
  1155. static void func_new_insert_replace_undo_common (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, int is_replace)
  1156. {
  1157. NCDValRef where_arg = NCDVal_NewInvalid();
  1158. NCDValRef what_arg;
  1159. if (!(!is_replace && NCDVal_ListRead(params->args, 1, &what_arg)) &&
  1160. !NCDVal_ListRead(params->args, 2, &where_arg, &what_arg)) {
  1161. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1162. goto fail0;
  1163. }
  1164. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1165. struct value *mov = valref_val(&mo->ref);
  1166. if (!mov) {
  1167. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1168. goto fail0;
  1169. }
  1170. struct insert_undo_deinit_data *data = malloc(sizeof(*data));
  1171. if (!data) {
  1172. ModuleLog(i, BLOG_ERROR, "malloc failed");
  1173. goto fail0;
  1174. }
  1175. struct value *oldv;
  1176. struct value *v;
  1177. if (NCDVal_IsInvalid(where_arg)) {
  1178. oldv = NULL;
  1179. v = value_insert_simple(i, mov, what_arg);
  1180. } else {
  1181. v = value_insert(i, mov, where_arg, what_arg, is_replace, &oldv);
  1182. }
  1183. if (!v) {
  1184. goto fail1;
  1185. }
  1186. valref_init(&data->val_ref, v);
  1187. valref_init(&data->oldval_ref, oldv);
  1188. func_new_common(vo, i, v, (value_deinit_func)undo_deinit_func, data);
  1189. return;
  1190. fail1:
  1191. free(data);
  1192. fail0:
  1193. NCDModuleInst_Backend_SetError(i);
  1194. NCDModuleInst_Backend_Dead(i);
  1195. }
  1196. static void func_new_insert_undo (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1197. {
  1198. func_new_insert_replace_undo_common(vo, i, params, 0);
  1199. }
  1200. static void func_new_replace_undo (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1201. {
  1202. func_new_insert_replace_undo_common(vo, i, params, 1);
  1203. }
  1204. static void func_new_replace_this (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1205. {
  1206. NCDValRef value_arg;
  1207. if (!NCDVal_ListRead(params->args, 1, &value_arg)) {
  1208. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1209. goto fail0;
  1210. }
  1211. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1212. struct value *mov = valref_val(&mo->ref);
  1213. if (!mov) {
  1214. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1215. goto fail0;
  1216. }
  1217. struct value *v = value_init_fromvalue(i, value_arg);
  1218. if (!v) {
  1219. goto fail0;
  1220. }
  1221. if (mov->parent) {
  1222. struct value *parent = mov->parent;
  1223. switch (parent->type) {
  1224. case NCDVAL_LIST: {
  1225. size_t index = value_list_indexof(parent, mov);
  1226. value_list_remove(parent, mov);
  1227. int res = value_list_insert(i, parent, v, index);
  1228. ASSERT_EXECUTE(res)
  1229. } break;
  1230. case NCDVAL_MAP: {
  1231. NCDValMem key_mem;
  1232. NCDValSafeRef key;
  1233. value_map_remove2(parent, mov, &key_mem, &key);
  1234. int res = value_map_insert(parent, v, key_mem, key, i);
  1235. ASSERT_EXECUTE(res)
  1236. } break;
  1237. default: ASSERT(0);
  1238. }
  1239. value_cleanup(mov);
  1240. }
  1241. func_new_common(vo, i, v, NULL, NULL);
  1242. return;
  1243. fail0:
  1244. NCDModuleInst_Backend_SetError(i);
  1245. NCDModuleInst_Backend_Dead(i);
  1246. }
  1247. static void func_new_replace_this_undo (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1248. {
  1249. NCDValRef value_arg;
  1250. if (!NCDVal_ListRead(params->args, 1, &value_arg)) {
  1251. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1252. goto fail0;
  1253. }
  1254. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1255. struct value *mov = valref_val(&mo->ref);
  1256. if (!mov) {
  1257. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1258. goto fail0;
  1259. }
  1260. struct value *v = value_init_fromvalue(i, value_arg);
  1261. if (!v) {
  1262. goto fail0;
  1263. }
  1264. struct insert_undo_deinit_data *data = malloc(sizeof(*data));
  1265. if (!data) {
  1266. ModuleLog(i, BLOG_ERROR, "malloc failed");
  1267. goto fail1;
  1268. }
  1269. valref_init(&data->val_ref, v);
  1270. valref_init(&data->oldval_ref, mov);
  1271. if (mov->parent) {
  1272. struct value *parent = mov->parent;
  1273. switch (parent->type) {
  1274. case NCDVAL_LIST: {
  1275. size_t index = value_list_indexof(parent, mov);
  1276. value_list_remove(parent, mov);
  1277. int res = value_list_insert(i, parent, v, index);
  1278. ASSERT_EXECUTE(res)
  1279. } break;
  1280. case NCDVAL_MAP: {
  1281. NCDValMem key_mem;
  1282. NCDValSafeRef key;
  1283. value_map_remove2(parent, mov, &key_mem, &key);
  1284. int res = value_map_insert(parent, v, key_mem, key, i);
  1285. ASSERT_EXECUTE(res)
  1286. } break;
  1287. default: ASSERT(0);
  1288. }
  1289. }
  1290. func_new_common(vo, i, v, (value_deinit_func)undo_deinit_func, data);
  1291. return;
  1292. fail1:
  1293. value_cleanup(v);
  1294. fail0:
  1295. NCDModuleInst_Backend_SetError(i);
  1296. NCDModuleInst_Backend_Dead(i);
  1297. }
  1298. static void func_new_substr (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1299. {
  1300. NCDValRef start_arg;
  1301. NCDValRef length_arg = NCDVal_NewInvalid();
  1302. if (!NCDVal_ListRead(params->args, 1, &start_arg) &&
  1303. !NCDVal_ListRead(params->args, 2, &start_arg, &length_arg)) {
  1304. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1305. goto fail0;
  1306. }
  1307. if (!NCDVal_IsString(start_arg) || (!NCDVal_IsInvalid(length_arg) && !NCDVal_IsString(length_arg))) {
  1308. ModuleLog(i, BLOG_ERROR, "wrong type");
  1309. goto fail0;
  1310. }
  1311. uintmax_t start;
  1312. if (!ncd_read_uintmax(start_arg, &start)) {
  1313. ModuleLog(i, BLOG_ERROR, "start is not a number");
  1314. goto fail0;
  1315. }
  1316. uintmax_t length = UINTMAX_MAX;
  1317. if (!NCDVal_IsInvalid(length_arg) && !ncd_read_uintmax(length_arg, &length)) {
  1318. ModuleLog(i, BLOG_ERROR, "length is not a number");
  1319. goto fail0;
  1320. }
  1321. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1322. struct value *mov = valref_val(&mo->ref);
  1323. if (!mov) {
  1324. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1325. goto fail0;
  1326. }
  1327. if (mov->type != NCDVAL_STRING && mov->type != IDSTRING_TYPE) {
  1328. ModuleLog(i, BLOG_ERROR, "value is not a string");
  1329. goto fail0;
  1330. }
  1331. const char *string_data;
  1332. size_t string_len;
  1333. if (mov->type == IDSTRING_TYPE) {
  1334. string_data = NCDStringIndex_Value(mov->idstring.string_index, mov->idstring.id);
  1335. string_len = strlen(string_data);
  1336. } else {
  1337. string_data = mov->string.string;
  1338. string_len = mov->string.length;
  1339. }
  1340. if (start > string_len) {
  1341. ModuleLog(i, BLOG_ERROR, "start is out of range");
  1342. goto fail0;
  1343. }
  1344. size_t remain = string_len - start;
  1345. size_t amount = length < remain ? length : remain;
  1346. struct value *v = value_init_string(i, string_data + start, amount);
  1347. if (!v) {
  1348. goto fail0;
  1349. }
  1350. func_new_common(vo, i, v, NULL, NULL);
  1351. return;
  1352. fail0:
  1353. NCDModuleInst_Backend_SetError(i);
  1354. NCDModuleInst_Backend_Dead(i);
  1355. }
  1356. static void remove_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1357. {
  1358. NCDValRef where_arg;
  1359. if (!NCDVal_ListRead(params->args, 1, &where_arg)) {
  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. if (!value_remove(i, mov, where_arg)) {
  1370. goto fail0;
  1371. }
  1372. NCDModuleInst_Backend_Up(i);
  1373. return;
  1374. fail0:
  1375. NCDModuleInst_Backend_SetError(i);
  1376. NCDModuleInst_Backend_Dead(i);
  1377. }
  1378. static void delete_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1379. {
  1380. if (!NCDVal_ListRead(params->args, 0)) {
  1381. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1382. goto fail0;
  1383. }
  1384. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1385. struct value *mov = valref_val(&mo->ref);
  1386. if (!mov) {
  1387. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1388. goto fail0;
  1389. }
  1390. value_delete(mov);
  1391. NCDModuleInst_Backend_Up(i);
  1392. return;
  1393. fail0:
  1394. NCDModuleInst_Backend_SetError(i);
  1395. NCDModuleInst_Backend_Dead(i);
  1396. }
  1397. static void reset_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1398. {
  1399. NCDValRef what_arg;
  1400. if (!NCDVal_ListRead(params->args, 1, &what_arg)) {
  1401. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1402. goto fail0;
  1403. }
  1404. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1405. // build value from argument
  1406. struct value *newv = value_init_fromvalue(i, what_arg);
  1407. if (!newv) {
  1408. goto fail0;
  1409. }
  1410. // deinit
  1411. if (mo->deinit_func) {
  1412. mo->deinit_func(mo->deinit_data, i);
  1413. }
  1414. // free value reference
  1415. valref_free(&mo->ref);
  1416. // set up value reference
  1417. valref_init(&mo->ref, newv);
  1418. // set no deinit function
  1419. mo->deinit_func = NULL;
  1420. NCDModuleInst_Backend_Up(i);
  1421. return;
  1422. fail0:
  1423. NCDModuleInst_Backend_SetError(i);
  1424. NCDModuleInst_Backend_Dead(i);
  1425. }
  1426. static void append_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1427. {
  1428. NCDValRef data_arg;
  1429. if (!NCDVal_ListRead(params->args, 1, &data_arg)) {
  1430. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1431. goto fail0;
  1432. }
  1433. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1434. struct value *mov = valref_val(&mo->ref);
  1435. if (!mov) {
  1436. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1437. goto fail0;
  1438. }
  1439. if (!value_append(i, mov, data_arg)) {
  1440. goto fail0;
  1441. }
  1442. NCDModuleInst_Backend_Up(i);
  1443. return;
  1444. fail0:
  1445. NCDModuleInst_Backend_SetError(i);
  1446. NCDModuleInst_Backend_Dead(i);
  1447. }
  1448. static struct NCDModule modules[] = {
  1449. {
  1450. .type = "value",
  1451. .func_new2 = func_new_value,
  1452. .func_die = func_die,
  1453. .func_getvar2 = func_getvar2,
  1454. .alloc_size = sizeof(struct instance)
  1455. }, {
  1456. .type = "value::get",
  1457. .base_type = "value",
  1458. .func_new2 = func_new_get,
  1459. .func_die = func_die,
  1460. .func_getvar2 = func_getvar2,
  1461. .alloc_size = sizeof(struct instance)
  1462. }, {
  1463. .type = "value::try_get",
  1464. .base_type = "value",
  1465. .func_new2 = func_new_try_get,
  1466. .func_die = func_die,
  1467. .func_getvar2 = func_getvar2,
  1468. .alloc_size = sizeof(struct instance)
  1469. }, {
  1470. .type = "value::getpath",
  1471. .base_type = "value",
  1472. .func_new2 = func_new_getpath,
  1473. .func_die = func_die,
  1474. .func_getvar2 = func_getvar2,
  1475. .alloc_size = sizeof(struct instance)
  1476. }, {
  1477. .type = "value::insert",
  1478. .base_type = "value",
  1479. .func_new2 = func_new_insert,
  1480. .func_die = func_die,
  1481. .func_getvar2 = func_getvar2,
  1482. .alloc_size = sizeof(struct instance)
  1483. }, {
  1484. .type = "value::replace",
  1485. .base_type = "value",
  1486. .func_new2 = func_new_replace,
  1487. .func_die = func_die,
  1488. .func_getvar2 = func_getvar2,
  1489. .alloc_size = sizeof(struct instance)
  1490. }, {
  1491. .type = "value::replace_this",
  1492. .base_type = "value",
  1493. .func_new2 = func_new_replace_this,
  1494. .func_die = func_die,
  1495. .func_getvar2 = func_getvar2,
  1496. .alloc_size = sizeof(struct instance)
  1497. }, {
  1498. .type = "value::insert_undo",
  1499. .base_type = "value",
  1500. .func_new2 = func_new_insert_undo,
  1501. .func_die = func_die,
  1502. .func_getvar2 = func_getvar2,
  1503. .alloc_size = sizeof(struct instance)
  1504. }, {
  1505. .type = "value::replace_undo",
  1506. .base_type = "value",
  1507. .func_new2 = func_new_replace_undo,
  1508. .func_die = func_die,
  1509. .func_getvar2 = func_getvar2,
  1510. .alloc_size = sizeof(struct instance)
  1511. }, {
  1512. .type = "value::replace_this_undo",
  1513. .base_type = "value",
  1514. .func_new2 = func_new_replace_this_undo,
  1515. .func_die = func_die,
  1516. .func_getvar2 = func_getvar2,
  1517. .alloc_size = sizeof(struct instance)
  1518. }, {
  1519. .type = "value::remove",
  1520. .func_new2 = remove_func_new
  1521. }, {
  1522. .type = "value::delete",
  1523. .func_new2 = delete_func_new
  1524. }, {
  1525. .type = "value::reset",
  1526. .func_new2 = reset_func_new
  1527. }, {
  1528. .type = "value::substr",
  1529. .base_type = "value",
  1530. .func_new2 = func_new_substr,
  1531. .func_die = func_die,
  1532. .func_getvar2 = func_getvar2,
  1533. .alloc_size = sizeof(struct instance)
  1534. }, {
  1535. .type = "value::append",
  1536. .func_new2 = append_func_new
  1537. }, {
  1538. .type = NULL
  1539. }
  1540. };
  1541. const struct NCDModuleGroup ncdmodule_value = {
  1542. .modules = modules,
  1543. .strings = strings
  1544. };