value.c 57 KB

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