value.c 57 KB

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