value.c 55 KB

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