value.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839
  1. /**
  2. * @file value.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Synopsis:
  32. * value(value)
  33. * value value::get(where)
  34. * value value::try_get(where)
  35. * value value::getpath(list path)
  36. * value value::insert(where, what)
  37. * value value::insert(what)
  38. * value value::replace(where, what)
  39. * value value::replace_this(value)
  40. * value value::insert_undo(where, what)
  41. * value value::insert_undo(what)
  42. * value value::replace_undo(where, what)
  43. * value value::replace_this_undo(value)
  44. *
  45. * Description:
  46. * Value objects allow examining and manipulating values.
  47. * These value objects are actually references to internal value structures, which
  48. * may be shared between value objects.
  49. *
  50. * value(value) constructs a new value object from the given value.
  51. *
  52. * value::get(where) constructs a value object for the element at position 'where'
  53. * (for a list), or the value corresponding to key 'where' (for a map). It is an
  54. * error if the base value is not a list or a map, the index is out of bounds of
  55. * the list, or the key does not exist in the map.
  56. * The resulting value object is NOT a copy, and shares (part of) the same
  57. * underlying value structure as the base value object. Deleting it will remove
  58. * it from the list or map it is part of.
  59. *
  60. * value::try_get(where) is like get(), except that if any restriction on 'where'
  61. * is violated, no error is triggered; instead, the value object is constructed
  62. * as being deleted; this state is exposed via the 'exists' variable.
  63. * This can be used to check for the presence of a key in a map, and in case it
  64. * exists, allow access to the corresponding value without another get() statement.
  65. *
  66. * value::getpath(path) is like get(), except that it performs multiple
  67. * consecutive resolutions. Also, if the path is an empty list, it performs
  68. * no resulution at all.
  69. *
  70. * value::insert(where, what) constructs a value object by inserting into an
  71. * existing value object.
  72. * For lists, 'where' is the index of the element to insert before, or the length
  73. * of the list to append to it.
  74. * For maps, 'where' is the key to insert under. If the key already exists in the
  75. * map, its value is replaced; any references to the old value however remain valid.
  76. *
  77. * value::insert(what) constructs a value object by appending to a list. An error
  78. * is triggered if the base value is not a list. Assuming 'list' is a list value,
  79. * list->insert(X) is equivalent to list->insert(list.length, X).
  80. *
  81. * value::replace(where, what) is like insert(), exept that, when inserting into a
  82. * list, the value at the specified index is replaced with the new value (unless
  83. * the index is equal to the length of the list).
  84. *
  85. * insert_undo() and replace_undo() are versions of insert() and replace() which
  86. * attempt to revert the modifications when they deinitialize.
  87. * Specifically, they work like that:
  88. * - On initiialization, they take an internal reference to the value being replaced
  89. * (if any; note that insert_undo() into a list never replaces a value).
  90. * - On deinitialization, they remove the the inserted value from its parent (if there
  91. * is one), and insert the old replaced value (to which a reference was kept) in that
  92. * place (if any, and assuming it has not been deleted).
  93. * Note that if the inserted value changes parents in between init and deinit, the
  94. * result of undoing may be unexpected.
  95. *
  96. * Variables:
  97. * (empty) - the value stored in the value object
  98. * type - type of the value; "string", "list" or "map"
  99. * length - number of elements in the list or map, or the number of bytes in a
  100. * string
  101. * keys - a list of keys in the map (only if the value is a map)
  102. * exists - "true" or "false", reflecting whether the value object holds a value
  103. * (is not in deleted state)
  104. *
  105. * Synopsis:
  106. * value::remove(where)
  107. * value::delete()
  108. *
  109. * Description:
  110. * value::remove(where) removes from an existing value object.
  111. * For lists, 'where' is the index of the element to remove, and must be in range.
  112. * For maps, 'where' is the key to remove, and must be an existing key.
  113. * In any case, any references to the removed value remain valid.
  114. *
  115. * value::delete() deletes the underlying value data of this value object.
  116. * After delection, the value object enters a deleted state, which will cause any
  117. * operation on it to fail. Any other value objects which referred to the same value
  118. * or parts of it will too enter deleted state. If the value was an element
  119. * in a list or map, is is removed from it.
  120. *
  121. * Synopsis:
  122. * value value::substr(string start [, string length])
  123. *
  124. * Description:
  125. * Constructs a string value by extracting a part of a string.
  126. * 'start' specifies the index of the character (from zero) where the substring to
  127. * extract starts, and must be <= the length of the string.
  128. * 'length' specifies the maximum number of characters to extract, if given.
  129. * The newly constructed value is a copy of the extracted substring.
  130. * The value must be a string value.
  131. *
  132. * Synopsis:
  133. * value::reset(what)
  134. *
  135. * Description:
  136. * Effectively deconstructs and reconstructs the value object. More precisely,
  137. * it builds a new value structure from 'what', possibly invokes a scheduled undo
  138. * operation (as scheduled by insert_undo() and replace_undo()), sets up this
  139. * value object to reference the newly built value structure, without any scheduled
  140. * undo operation.
  141. *
  142. * Synopsis:
  143. * value::append(string data)
  144. *
  145. * Description:
  146. * Appends the given data to a string value. The value this is called on must
  147. * be a string value.
  148. */
  149. #include <stdlib.h>
  150. #include <string.h>
  151. #include <stddef.h>
  152. #include <limits.h>
  153. #include <inttypes.h>
  154. #include <misc/offset.h>
  155. #include <misc/debug.h>
  156. #include <misc/balloc.h>
  157. #include <structure/LinkedList0.h>
  158. #include <structure/IndexedList.h>
  159. #include <structure/SAvl.h>
  160. #include <ncd/NCDModule.h>
  161. #include <ncd/NCDStringIndex.h>
  162. #include <ncd/static_strings.h>
  163. #include <ncd/extra/value_utils.h>
  164. #include <generated/blog_channel_ncd_value.h>
  165. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  166. #define IDSTRING_TYPE (NCDVAL_STRING | (1 << 3))
  167. struct value;
  168. #include "value_maptree.h"
  169. #include <structure/SAvl_decl.h>
  170. struct valref {
  171. struct value *v;
  172. LinkedList0Node refs_list_node;
  173. };
  174. typedef void (*value_deinit_func) (void *deinit_data, NCDModuleInst *i);
  175. struct instance {
  176. NCDModuleInst *i;
  177. struct valref ref;
  178. value_deinit_func deinit_func;
  179. void *deinit_data;
  180. };
  181. struct value {
  182. LinkedList0 refs_list;
  183. struct value *parent;
  184. union {
  185. struct {
  186. IndexedListNode list_contents_il_node;
  187. } list_parent;
  188. struct {
  189. NCDValMem key_mem;
  190. NCDValRef key;
  191. MapTreeNode maptree_node;
  192. } map_parent;
  193. };
  194. int type;
  195. union {
  196. struct {
  197. char *string;
  198. size_t length;
  199. } string;
  200. struct {
  201. NCD_string_id_t id;
  202. NCDStringIndex *string_index;
  203. } idstring;
  204. struct {
  205. IndexedList list_contents_il;
  206. } list;
  207. struct {
  208. MapTree map_tree;
  209. } map;
  210. };
  211. };
  212. static const char * get_type_str (int type);
  213. static void value_cleanup (struct value *v);
  214. static void value_delete (struct value *v);
  215. static struct value * value_init_string (NCDModuleInst *i, const char *str, size_t len);
  216. static struct value * value_init_idstring (NCDModuleInst *i, NCD_string_id_t id, NCDStringIndex *string_index);
  217. static struct value * value_init_list (NCDModuleInst *i);
  218. static size_t value_list_len (struct value *v);
  219. static struct value * value_list_at (struct value *v, size_t index);
  220. static size_t value_list_indexof (struct value *v, struct value *ev);
  221. static int value_list_insert (NCDModuleInst *i, struct value *list, struct value *v, size_t index);
  222. static void value_list_remove (struct value *list, struct value *v);
  223. static struct value * value_init_map (NCDModuleInst *i);
  224. static size_t value_map_len (struct value *map);
  225. static struct value * value_map_at (struct value *map, size_t index);
  226. static struct value * value_map_find (struct value *map, NCDValRef key);
  227. static int value_map_insert (struct value *map, struct value *v, NCDValMem mem, NCDValSafeRef key, NCDModuleInst *i);
  228. static void value_map_remove (struct value *map, struct value *v);
  229. static void value_map_remove2 (struct value *map, struct value *v, NCDValMem *out_mem, NCDValSafeRef *out_key);
  230. static struct value * value_init_fromvalue (NCDModuleInst *i, NCDValRef value);
  231. static int value_to_value (NCDModuleInst *i, struct value *v, NCDValMem *mem, NCDValRef *out_value);
  232. static struct value * value_get (NCDModuleInst *i, struct value *v, NCDValRef where, int no_error);
  233. static struct value * value_get_path (NCDModuleInst *i, struct value *v, NCDValRef path);
  234. static struct value * value_insert (NCDModuleInst *i, struct value *v, NCDValRef where, NCDValRef what, int is_replace, struct value **out_oldv);
  235. static struct value * value_insert_simple (NCDModuleInst *i, struct value *v, NCDValRef what);
  236. static int value_remove (NCDModuleInst *i, struct value *v, NCDValRef where);
  237. static int value_append (NCDModuleInst *i, struct value *v, NCDValRef data);
  238. static void valref_init (struct valref *r, struct value *v);
  239. static void valref_free (struct valref *r);
  240. static struct value * valref_val (struct valref *r);
  241. static void valref_break (struct valref *r);
  242. enum {STRING_EXISTS, STRING_TYPE, STRING_LENGTH, STRING_KEYS};
  243. static struct NCD_string_request strings[] = {
  244. {"exists"}, {"type"}, {"length"}, {"keys"}, {NULL}
  245. };
  246. #include "value_maptree.h"
  247. #include <structure/SAvl_impl.h>
  248. static const char * get_type_str (int type)
  249. {
  250. switch (type) {
  251. case NCDVAL_STRING:
  252. case IDSTRING_TYPE: return "string";
  253. case NCDVAL_LIST: return "list";
  254. case NCDVAL_MAP: return "map";
  255. }
  256. ASSERT(0)
  257. return NULL;
  258. }
  259. static void value_cleanup (struct value *v)
  260. {
  261. if (v->parent || !LinkedList0_IsEmpty(&v->refs_list)) {
  262. return;
  263. }
  264. switch (v->type) {
  265. case NCDVAL_STRING: {
  266. BFree(v->string.string);
  267. } break;
  268. case IDSTRING_TYPE: {
  269. } break;
  270. case NCDVAL_LIST: {
  271. while (value_list_len(v) > 0) {
  272. struct value *ev = value_list_at(v, 0);
  273. value_list_remove(v, ev);
  274. value_cleanup(ev);
  275. }
  276. } break;
  277. case NCDVAL_MAP: {
  278. while (value_map_len(v) > 0) {
  279. struct value *ev = value_map_at(v, 0);
  280. value_map_remove(v, ev);
  281. value_cleanup(ev);
  282. }
  283. } break;
  284. default: ASSERT(0);
  285. }
  286. free(v);
  287. }
  288. static void value_delete (struct value *v)
  289. {
  290. if (v->parent) {
  291. switch (v->parent->type) {
  292. case NCDVAL_LIST: {
  293. value_list_remove(v->parent, v);
  294. } break;
  295. case NCDVAL_MAP: {
  296. value_map_remove(v->parent, v);
  297. } break;
  298. default: ASSERT(0);
  299. }
  300. }
  301. LinkedList0Node *ln;
  302. while (ln = LinkedList0_GetFirst(&v->refs_list)) {
  303. struct valref *r = UPPER_OBJECT(ln, struct valref, refs_list_node);
  304. ASSERT(r->v == v)
  305. valref_break(r);
  306. }
  307. switch (v->type) {
  308. case NCDVAL_STRING: {
  309. BFree(v->string.string);
  310. } break;
  311. case IDSTRING_TYPE: {
  312. } break;
  313. case NCDVAL_LIST: {
  314. while (value_list_len(v) > 0) {
  315. struct value *ev = value_list_at(v, 0);
  316. value_delete(ev);
  317. }
  318. } break;
  319. case NCDVAL_MAP: {
  320. while (value_map_len(v) > 0) {
  321. struct value *ev = value_map_at(v, 0);
  322. value_delete(ev);
  323. }
  324. } break;
  325. default: ASSERT(0);
  326. }
  327. free(v);
  328. }
  329. static struct value * value_init_string (NCDModuleInst *i, const char *str, size_t len)
  330. {
  331. struct value *v = malloc(sizeof(*v));
  332. if (!v) {
  333. ModuleLog(i, BLOG_ERROR, "malloc failed");
  334. goto fail0;
  335. }
  336. LinkedList0_Init(&v->refs_list);
  337. v->parent = NULL;
  338. v->type = NCDVAL_STRING;
  339. if (!(v->string.string = BAlloc(len))) {
  340. ModuleLog(i, BLOG_ERROR, "BAlloc failed");
  341. goto fail1;
  342. }
  343. memcpy(v->string.string, str, len);
  344. v->string.length = len;
  345. return v;
  346. fail1:
  347. free(v);
  348. fail0:
  349. return NULL;
  350. }
  351. static struct value * value_init_idstring (NCDModuleInst *i, NCD_string_id_t id, NCDStringIndex *string_index)
  352. {
  353. ASSERT(string_index == i->params->iparams->string_index)
  354. struct value *v = malloc(sizeof(*v));
  355. if (!v) {
  356. ModuleLog(i, BLOG_ERROR, "malloc failed");
  357. goto fail0;
  358. }
  359. LinkedList0_Init(&v->refs_list);
  360. v->parent = NULL;
  361. v->type = IDSTRING_TYPE;
  362. v->idstring.id = id;
  363. v->idstring.string_index = string_index;
  364. return v;
  365. fail0:
  366. return NULL;
  367. }
  368. static struct value * value_init_list (NCDModuleInst *i)
  369. {
  370. struct value *v = malloc(sizeof(*v));
  371. if (!v) {
  372. ModuleLog(i, BLOG_ERROR, "malloc failed");
  373. return NULL;
  374. }
  375. LinkedList0_Init(&v->refs_list);
  376. v->parent = NULL;
  377. v->type = NCDVAL_LIST;
  378. IndexedList_Init(&v->list.list_contents_il);
  379. return v;
  380. }
  381. static size_t value_list_len (struct value *v)
  382. {
  383. ASSERT(v->type == NCDVAL_LIST)
  384. return IndexedList_Count(&v->list.list_contents_il);
  385. }
  386. static struct value * value_list_at (struct value *v, size_t index)
  387. {
  388. ASSERT(v->type == NCDVAL_LIST)
  389. ASSERT(index < value_list_len(v))
  390. IndexedListNode *iln = IndexedList_GetAt(&v->list.list_contents_il, index);
  391. ASSERT(iln)
  392. struct value *e = UPPER_OBJECT(iln, struct value, list_parent.list_contents_il_node);
  393. ASSERT(e->parent == v)
  394. return e;
  395. }
  396. static size_t value_list_indexof (struct value *v, struct value *ev)
  397. {
  398. ASSERT(v->type == NCDVAL_LIST)
  399. ASSERT(ev->parent == v)
  400. uint64_t index = IndexedList_IndexOf(&v->list.list_contents_il, &ev->list_parent.list_contents_il_node);
  401. ASSERT(index < value_list_len(v))
  402. return index;
  403. }
  404. static int value_list_insert (NCDModuleInst *i, struct value *list, struct value *v, size_t index)
  405. {
  406. ASSERT(list->type == NCDVAL_LIST)
  407. ASSERT(!v->parent)
  408. ASSERT(index <= value_list_len(list))
  409. if (value_list_len(list) == SIZE_MAX) {
  410. ModuleLog(i, BLOG_ERROR, "list has too many elements");
  411. return 0;
  412. }
  413. IndexedList_InsertAt(&list->list.list_contents_il, &v->list_parent.list_contents_il_node, index);
  414. v->parent = list;
  415. return 1;
  416. }
  417. static void value_list_remove (struct value *list, struct value *v)
  418. {
  419. ASSERT(list->type == NCDVAL_LIST)
  420. ASSERT(v->parent == list)
  421. IndexedList_Remove(&list->list.list_contents_il, &v->list_parent.list_contents_il_node);
  422. v->parent = NULL;
  423. }
  424. static struct value * value_init_map (NCDModuleInst *i)
  425. {
  426. struct value *v = malloc(sizeof(*v));
  427. if (!v) {
  428. ModuleLog(i, BLOG_ERROR, "malloc failed");
  429. return NULL;
  430. }
  431. LinkedList0_Init(&v->refs_list);
  432. v->parent = NULL;
  433. v->type = NCDVAL_MAP;
  434. MapTree_Init(&v->map.map_tree);
  435. return v;
  436. }
  437. static size_t value_map_len (struct value *map)
  438. {
  439. ASSERT(map->type == NCDVAL_MAP)
  440. return MapTree_Count(&map->map.map_tree, 0);
  441. }
  442. static struct value * value_map_at (struct value *map, size_t index)
  443. {
  444. ASSERT(map->type == NCDVAL_MAP)
  445. ASSERT(index < value_map_len(map))
  446. struct value *e = MapTree_GetAt(&map->map.map_tree, 0, index);
  447. ASSERT(e)
  448. ASSERT(e->parent == map)
  449. return e;
  450. }
  451. static struct value * value_map_find (struct value *map, NCDValRef key)
  452. {
  453. ASSERT(map->type == NCDVAL_MAP)
  454. ASSERT(NCDVal_Type(key))
  455. struct value *e = MapTree_LookupExact(&map->map.map_tree, 0, key);
  456. ASSERT(!e || e->parent == map)
  457. return e;
  458. }
  459. static int value_map_insert (struct value *map, struct value *v, NCDValMem mem, NCDValSafeRef key, NCDModuleInst *i)
  460. {
  461. ASSERT(map->type == NCDVAL_MAP)
  462. ASSERT(!v->parent)
  463. ASSERT((NCDVal_Type(NCDVal_FromSafe(&mem, key)), 1))
  464. ASSERT(!value_map_find(map, NCDVal_FromSafe(&mem, key)))
  465. if (value_map_len(map) == SIZE_MAX) {
  466. ModuleLog(i, BLOG_ERROR, "map has too many elements");
  467. return 0;
  468. }
  469. v->map_parent.key_mem = mem;
  470. v->map_parent.key = NCDVal_FromSafe(&v->map_parent.key_mem, key);
  471. int res = MapTree_Insert(&map->map.map_tree, 0, v, NULL);
  472. ASSERT_EXECUTE(res)
  473. v->parent = map;
  474. return 1;
  475. }
  476. static void value_map_remove (struct value *map, struct value *v)
  477. {
  478. ASSERT(map->type == NCDVAL_MAP)
  479. ASSERT(v->parent == map)
  480. MapTree_Remove(&map->map.map_tree, 0, v);
  481. NCDValMem_Free(&v->map_parent.key_mem);
  482. v->parent = NULL;
  483. }
  484. static void value_map_remove2 (struct value *map, struct value *v, NCDValMem *out_mem, NCDValSafeRef *out_key)
  485. {
  486. ASSERT(map->type == NCDVAL_MAP)
  487. ASSERT(v->parent == map)
  488. ASSERT(out_mem)
  489. ASSERT(out_key)
  490. MapTree_Remove(&map->map.map_tree, 0, v);
  491. *out_mem = v->map_parent.key_mem;
  492. *out_key = NCDVal_ToSafe(v->map_parent.key);
  493. v->parent = NULL;
  494. }
  495. static struct value * value_init_fromvalue (NCDModuleInst *i, NCDValRef value)
  496. {
  497. ASSERT((NCDVal_Type(value), 1))
  498. struct value *v;
  499. switch (NCDVal_Type(value)) {
  500. case NCDVAL_STRING: {
  501. if (NCDVal_IsIdString(value)) {
  502. v = value_init_idstring(i, NCDVal_IdStringId(value), NCDVal_IdStringStringIndex(value));
  503. } else {
  504. v = value_init_string(i, NCDVal_StringData(value), NCDVal_StringLength(value));
  505. }
  506. if (!v) {
  507. goto fail0;
  508. }
  509. } break;
  510. case NCDVAL_LIST: {
  511. if (!(v = value_init_list(i))) {
  512. goto fail0;
  513. }
  514. size_t count = NCDVal_ListCount(value);
  515. for (size_t j = 0; j < count; j++) {
  516. struct value *ev = value_init_fromvalue(i, NCDVal_ListGet(value, j));
  517. if (!ev) {
  518. goto fail1;
  519. }
  520. if (!value_list_insert(i, v, ev, value_list_len(v))) {
  521. value_cleanup(ev);
  522. goto fail1;
  523. }
  524. }
  525. } break;
  526. case NCDVAL_MAP: {
  527. if (!(v = value_init_map(i))) {
  528. goto fail0;
  529. }
  530. for (NCDValMapElem e = NCDVal_MapFirst(value); !NCDVal_MapElemInvalid(e); e = NCDVal_MapNext(value, e)) {
  531. NCDValRef ekey = NCDVal_MapElemKey(value, e);
  532. NCDValRef eval = NCDVal_MapElemVal(value, e);
  533. NCDValMem key_mem;
  534. NCDValMem_Init(&key_mem);
  535. NCDValRef key = NCDVal_NewCopy(&key_mem, ekey);
  536. if (NCDVal_IsInvalid(key)) {
  537. NCDValMem_Free(&key_mem);
  538. goto fail1;
  539. }
  540. struct value *ev = value_init_fromvalue(i, eval);
  541. if (!ev) {
  542. NCDValMem_Free(&key_mem);
  543. goto fail1;
  544. }
  545. if (!value_map_insert(v, ev, key_mem, NCDVal_ToSafe(key), i)) {
  546. NCDValMem_Free(&key_mem);
  547. value_cleanup(ev);
  548. goto fail1;
  549. }
  550. }
  551. } break;
  552. default:
  553. ASSERT(0);
  554. return NULL;
  555. }
  556. return v;
  557. fail1:
  558. value_cleanup(v);
  559. fail0:
  560. return NULL;
  561. }
  562. static int value_to_value (NCDModuleInst *i, struct value *v, NCDValMem *mem, NCDValRef *out_value)
  563. {
  564. ASSERT(mem)
  565. ASSERT(out_value)
  566. switch (v->type) {
  567. case NCDVAL_STRING: {
  568. *out_value = NCDVal_NewStringBin(mem, (const uint8_t *)v->string.string, v->string.length);
  569. if (NCDVal_IsInvalid(*out_value)) {
  570. goto fail;
  571. }
  572. } break;
  573. case IDSTRING_TYPE: {
  574. *out_value = NCDVal_NewIdString(mem, v->idstring.id, v->idstring.string_index);
  575. if (NCDVal_IsInvalid(*out_value)) {
  576. goto fail;
  577. }
  578. } break;
  579. case NCDVAL_LIST: {
  580. *out_value = NCDVal_NewList(mem, value_list_len(v));
  581. if (NCDVal_IsInvalid(*out_value)) {
  582. goto fail;
  583. }
  584. for (size_t index = 0; index < value_list_len(v); index++) {
  585. NCDValRef eval;
  586. if (!value_to_value(i, value_list_at(v, index), mem, &eval)) {
  587. goto fail;
  588. }
  589. if (!NCDVal_ListAppend(*out_value, eval)) {
  590. goto fail;
  591. }
  592. }
  593. } break;
  594. case NCDVAL_MAP: {
  595. *out_value = NCDVal_NewMap(mem, value_map_len(v));
  596. if (NCDVal_IsInvalid(*out_value)) {
  597. goto fail;
  598. }
  599. for (size_t index = 0; index < value_map_len(v); index++) {
  600. struct value *ev = value_map_at(v, index);
  601. NCDValRef key = NCDVal_NewCopy(mem, ev->map_parent.key);
  602. if (NCDVal_IsInvalid(key)) {
  603. goto fail;
  604. }
  605. NCDValRef val;
  606. if (!value_to_value(i, ev, mem, &val)) {
  607. goto fail;
  608. }
  609. int inserted;
  610. if (!NCDVal_MapInsert(*out_value, key, val, &inserted)) {
  611. goto fail;
  612. }
  613. ASSERT_EXECUTE(inserted)
  614. }
  615. } break;
  616. default: ASSERT(0);
  617. }
  618. return 1;
  619. fail:
  620. return 0;
  621. }
  622. static struct value * value_get (NCDModuleInst *i, struct value *v, NCDValRef where, int no_error)
  623. {
  624. ASSERT((NCDVal_Type(where), 1))
  625. switch (v->type) {
  626. case NCDVAL_STRING:
  627. case IDSTRING_TYPE: {
  628. if (!no_error) ModuleLog(i, BLOG_ERROR, "cannot resolve into a string");
  629. goto fail;
  630. } break;
  631. case NCDVAL_LIST: {
  632. uintmax_t index;
  633. if (!NCDVal_IsString(where) || !ncd_read_uintmax(where, &index)) {
  634. if (!no_error) ModuleLog(i, BLOG_ERROR, "index is not a valid number (resolving into list)");
  635. goto fail;
  636. }
  637. if (index >= value_list_len(v)) {
  638. if (!no_error) ModuleLog(i, BLOG_ERROR, "index is out of bounds (resolving into list)");
  639. goto fail;
  640. }
  641. v = value_list_at(v, index);
  642. } break;
  643. case NCDVAL_MAP: {
  644. v = value_map_find(v, where);
  645. if (!v) {
  646. if (!no_error) ModuleLog(i, BLOG_ERROR, "key does not exist (resolving into map)");
  647. goto fail;
  648. }
  649. } break;
  650. default: ASSERT(0);
  651. }
  652. return v;
  653. fail:
  654. return NULL;
  655. }
  656. static struct value * value_get_path (NCDModuleInst *i, struct value *v, NCDValRef path)
  657. {
  658. ASSERT(NCDVal_IsList(path))
  659. size_t count = NCDVal_ListCount(path);
  660. for (size_t j = 0; j < count; j++) {
  661. if (!(v = value_get(i, v, NCDVal_ListGet(path, j), 0))) {
  662. goto fail;
  663. }
  664. }
  665. return v;
  666. fail:
  667. return NULL;
  668. }
  669. static struct value * value_insert (NCDModuleInst *i, struct value *v, NCDValRef where, NCDValRef what, int is_replace, struct value **out_oldv)
  670. {
  671. ASSERT(v)
  672. ASSERT((NCDVal_Type(where), 1))
  673. ASSERT((NCDVal_Type(what), 1))
  674. ASSERT(is_replace == !!is_replace)
  675. struct value *nv = value_init_fromvalue(i, what);
  676. if (!nv) {
  677. goto fail0;
  678. }
  679. struct value *oldv = NULL;
  680. switch (v->type) {
  681. case NCDVAL_STRING:
  682. case IDSTRING_TYPE: {
  683. ModuleLog(i, BLOG_ERROR, "cannot insert into a string");
  684. goto fail1;
  685. } break;
  686. case NCDVAL_LIST: {
  687. uintmax_t index;
  688. if (!NCDVal_IsString(where) || !ncd_read_uintmax(where, &index)) {
  689. ModuleLog(i, BLOG_ERROR, "index is not a valid number (inserting into list)");
  690. goto fail1;
  691. }
  692. if (index > value_list_len(v)) {
  693. ModuleLog(i, BLOG_ERROR, "index is out of bounds (inserting into list)");
  694. goto fail1;
  695. }
  696. if (is_replace && index < value_list_len(v)) {
  697. oldv = value_list_at(v, index);
  698. value_list_remove(v, oldv);
  699. int res = value_list_insert(i, v, nv, index);
  700. ASSERT_EXECUTE(res)
  701. } else {
  702. if (!value_list_insert(i, v, nv, index)) {
  703. goto fail1;
  704. }
  705. }
  706. } break;
  707. case NCDVAL_MAP: {
  708. oldv = value_map_find(v, where);
  709. if (!oldv && value_map_len(v) == SIZE_MAX) {
  710. ModuleLog(i, BLOG_ERROR, "map has too many elements");
  711. goto fail1;
  712. }
  713. NCDValMem key_mem;
  714. NCDValMem_Init(&key_mem);
  715. NCDValRef key = NCDVal_NewCopy(&key_mem, where);
  716. if (NCDVal_IsInvalid(key)) {
  717. NCDValMem_Free(&key_mem);
  718. goto fail1;
  719. }
  720. if (oldv) {
  721. value_map_remove(v, oldv);
  722. }
  723. int res = value_map_insert(v, nv, key_mem, NCDVal_ToSafe(key), i);
  724. ASSERT_EXECUTE(res)
  725. } break;
  726. default: ASSERT(0);
  727. }
  728. if (out_oldv) {
  729. *out_oldv = oldv;
  730. }
  731. else if (oldv) {
  732. value_cleanup(oldv);
  733. }
  734. return nv;
  735. fail1:
  736. value_cleanup(nv);
  737. fail0:
  738. return NULL;
  739. }
  740. static struct value * value_insert_simple (NCDModuleInst *i, struct value *v, NCDValRef what)
  741. {
  742. ASSERT(v)
  743. ASSERT((NCDVal_Type(what), 1))
  744. struct value *nv = value_init_fromvalue(i, what);
  745. if (!nv) {
  746. goto fail0;
  747. }
  748. switch (v->type) {
  749. case NCDVAL_LIST: {
  750. if (!value_list_insert(i, v, nv, value_list_len(v))) {
  751. goto fail1;
  752. }
  753. } break;
  754. default:
  755. ModuleLog(i, BLOG_ERROR, "one-argument insert is only defined for lists");
  756. return NULL;
  757. }
  758. return nv;
  759. fail1:
  760. value_cleanup(nv);
  761. fail0:
  762. return NULL;
  763. }
  764. static int value_remove (NCDModuleInst *i, struct value *v, NCDValRef where)
  765. {
  766. ASSERT(v)
  767. ASSERT((NCDVal_Type(where), 1))
  768. switch (v->type) {
  769. case NCDVAL_STRING:
  770. case IDSTRING_TYPE: {
  771. ModuleLog(i, BLOG_ERROR, "cannot remove from a string");
  772. goto fail;
  773. } break;
  774. case NCDVAL_LIST: {
  775. uintmax_t index;
  776. if (!NCDVal_IsString(where) || !ncd_read_uintmax(where, &index)) {
  777. ModuleLog(i, BLOG_ERROR, "index is not a valid number (removing from list)");
  778. goto fail;
  779. }
  780. if (index >= value_list_len(v)) {
  781. ModuleLog(i, BLOG_ERROR, "index is out of bounds (removing from list)");
  782. goto fail;
  783. }
  784. struct value *ov = value_list_at(v, index);
  785. value_list_remove(v, ov);
  786. value_cleanup(ov);
  787. } break;
  788. case NCDVAL_MAP: {
  789. struct value *ov = value_map_find(v, where);
  790. if (!ov) {
  791. ModuleLog(i, BLOG_ERROR, "key does not exist (removing from map)");
  792. goto fail;
  793. }
  794. value_map_remove(v, ov);
  795. value_cleanup(ov);
  796. } break;
  797. default: ASSERT(0);
  798. }
  799. return 1;
  800. fail:
  801. return 0;
  802. }
  803. static int value_append (NCDModuleInst *i, struct value *v, NCDValRef data)
  804. {
  805. ASSERT(v)
  806. ASSERT((NCDVal_Type(data), 1))
  807. switch (v->type) {
  808. case NCDVAL_STRING: {
  809. if (!NCDVal_IsString(data)) {
  810. ModuleLog(i, BLOG_ERROR, "cannot append non-string to string");
  811. return 0;
  812. }
  813. const char *append_string = NCDVal_StringData(data);
  814. size_t append_length = NCDVal_StringLength(data);
  815. if (append_length > SIZE_MAX - v->string.length) {
  816. ModuleLog(i, BLOG_ERROR, "too much data to append");
  817. return 0;
  818. }
  819. size_t new_length = v->string.length + append_length;
  820. char *new_string = BRealloc(v->string.string, new_length);
  821. if (!new_string) {
  822. ModuleLog(i, BLOG_ERROR, "BRealloc failed");
  823. return 0;
  824. }
  825. memcpy(new_string + v->string.length, append_string, append_length);
  826. v->string.string = new_string;
  827. v->string.length = new_length;
  828. } break;
  829. case IDSTRING_TYPE: {
  830. if (!NCDVal_IsString(data)) {
  831. ModuleLog(i, BLOG_ERROR, "cannot append non-string to string");
  832. return 0;
  833. }
  834. const char *append_string = NCDVal_StringData(data);
  835. size_t append_length = NCDVal_StringLength(data);
  836. const char *string = NCDStringIndex_Value(v->idstring.string_index, v->idstring.id);
  837. size_t length = NCDStringIndex_Length(v->idstring.string_index, v->idstring.id);
  838. if (append_length > SIZE_MAX - length) {
  839. ModuleLog(i, BLOG_ERROR, "too much data to append");
  840. return 0;
  841. }
  842. size_t new_length = length + append_length;
  843. char *new_string = BAlloc(new_length);
  844. if (!new_string) {
  845. ModuleLog(i, BLOG_ERROR, "BAlloc failed");
  846. return 0;
  847. }
  848. memcpy(new_string, string, length);
  849. memcpy(new_string + length, append_string, append_length);
  850. v->type = NCDVAL_STRING;
  851. v->string.string = new_string;
  852. v->string.length = new_length;
  853. } break;
  854. default:
  855. ModuleLog(i, BLOG_ERROR, "cannot append to non-string");
  856. return 0;
  857. }
  858. return 1;
  859. }
  860. static void valref_init (struct valref *r, struct value *v)
  861. {
  862. r->v = v;
  863. if (v) {
  864. LinkedList0_Prepend(&v->refs_list, &r->refs_list_node);
  865. }
  866. }
  867. static void valref_free (struct valref *r)
  868. {
  869. if (r->v) {
  870. LinkedList0_Remove(&r->v->refs_list, &r->refs_list_node);
  871. value_cleanup(r->v);
  872. }
  873. }
  874. static struct value * valref_val (struct valref *r)
  875. {
  876. return r->v;
  877. }
  878. static void valref_break (struct valref *r)
  879. {
  880. ASSERT(r->v)
  881. LinkedList0_Remove(&r->v->refs_list, &r->refs_list_node);
  882. r->v = NULL;
  883. }
  884. static void func_new_common (void *vo, NCDModuleInst *i, struct value *v, value_deinit_func deinit_func, void *deinit_data)
  885. {
  886. struct instance *o = vo;
  887. o->i = i;
  888. // init value references
  889. valref_init(&o->ref, v);
  890. // remember deinit
  891. o->deinit_func = deinit_func;
  892. o->deinit_data = deinit_data;
  893. NCDModuleInst_Backend_Up(i);
  894. return;
  895. }
  896. static void func_die (void *vo)
  897. {
  898. struct instance *o = vo;
  899. // deinit
  900. if (o->deinit_func) {
  901. o->deinit_func(o->deinit_data, o->i);
  902. }
  903. // free value reference
  904. valref_free(&o->ref);
  905. NCDModuleInst_Backend_Dead(o->i);
  906. }
  907. static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  908. {
  909. struct instance *o = vo;
  910. struct value *v = valref_val(&o->ref);
  911. if (name == strings[STRING_EXISTS].id) {
  912. *out = ncd_make_boolean(mem, !!v, o->i->params->iparams->string_index);
  913. return 1;
  914. }
  915. if (name != strings[STRING_TYPE].id && name != strings[STRING_LENGTH].id &&
  916. name != strings[STRING_KEYS].id && name != NCD_STRING_EMPTY) {
  917. return 0;
  918. }
  919. if (!v) {
  920. ModuleLog(o->i, BLOG_ERROR, "value was deleted");
  921. return 0;
  922. }
  923. if (name == strings[STRING_TYPE].id) {
  924. *out = NCDVal_NewString(mem, get_type_str(v->type));
  925. }
  926. else if (name == strings[STRING_LENGTH].id) {
  927. size_t len = 0; // to remove warning
  928. switch (v->type) {
  929. case NCDVAL_LIST:
  930. len = value_list_len(v);
  931. break;
  932. case NCDVAL_MAP:
  933. len = value_map_len(v);
  934. break;
  935. case NCDVAL_STRING:
  936. len = v->string.length;
  937. break;
  938. case IDSTRING_TYPE:
  939. len = NCDStringIndex_Length(v->idstring.string_index, v->idstring.id);
  940. break;
  941. default:
  942. ASSERT(0);
  943. }
  944. *out = ncd_make_uintmax(mem, len);
  945. }
  946. else if (name == strings[STRING_KEYS].id) {
  947. if (v->type != NCDVAL_MAP) {
  948. ModuleLog(o->i, BLOG_ERROR, "value is not a map (reading keys variable)");
  949. return 0;
  950. }
  951. *out = NCDVal_NewList(mem, value_map_len(v));
  952. if (NCDVal_IsInvalid(*out)) {
  953. goto fail;
  954. }
  955. for (size_t j = 0; j < value_map_len(v); j++) {
  956. struct value *ev = value_map_at(v, j);
  957. NCDValRef key = NCDVal_NewCopy(mem, ev->map_parent.key);
  958. if (NCDVal_IsInvalid(key)) {
  959. goto fail;
  960. }
  961. if (!NCDVal_ListAppend(*out, key)) {
  962. goto fail;
  963. }
  964. }
  965. }
  966. else if (name == NCD_STRING_EMPTY) {
  967. if (!value_to_value(o->i, v, mem, out)) {
  968. return 0;
  969. }
  970. }
  971. else {
  972. ASSERT(0);
  973. }
  974. return 1;
  975. fail:
  976. *out = NCDVal_NewInvalid();
  977. return 1;
  978. }
  979. static void func_new_value (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  980. {
  981. NCDValRef value_arg;
  982. if (!NCDVal_ListRead(params->args, 1, &value_arg)) {
  983. ModuleLog(i, BLOG_ERROR, "wrong arity");
  984. goto fail0;
  985. }
  986. struct value *v = value_init_fromvalue(i, value_arg);
  987. if (!v) {
  988. goto fail0;
  989. }
  990. func_new_common(vo, i, v, NULL, NULL);
  991. return;
  992. fail0:
  993. NCDModuleInst_Backend_DeadError(i);
  994. }
  995. static void func_new_get (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  996. {
  997. NCDValRef where_arg;
  998. if (!NCDVal_ListRead(params->args, 1, &where_arg)) {
  999. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1000. goto fail0;
  1001. }
  1002. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1003. struct value *mov = valref_val(&mo->ref);
  1004. if (!mov) {
  1005. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1006. goto fail0;
  1007. }
  1008. struct value *v = value_get(i, mov, where_arg, 0);
  1009. if (!v) {
  1010. goto fail0;
  1011. }
  1012. func_new_common(vo, i, v, NULL, NULL);
  1013. return;
  1014. fail0:
  1015. NCDModuleInst_Backend_DeadError(i);
  1016. }
  1017. static void func_new_try_get (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1018. {
  1019. NCDValRef where_arg;
  1020. if (!NCDVal_ListRead(params->args, 1, &where_arg)) {
  1021. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1022. goto fail0;
  1023. }
  1024. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1025. struct value *mov = valref_val(&mo->ref);
  1026. if (!mov) {
  1027. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1028. goto fail0;
  1029. }
  1030. struct value *v = value_get(i, mov, where_arg, 1);
  1031. func_new_common(vo, i, v, NULL, NULL);
  1032. return;
  1033. fail0:
  1034. NCDModuleInst_Backend_DeadError(i);
  1035. }
  1036. static void func_new_getpath (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1037. {
  1038. NCDValRef path_arg;
  1039. if (!NCDVal_ListRead(params->args, 1, &path_arg)) {
  1040. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1041. goto fail0;
  1042. }
  1043. if (!NCDVal_IsList(path_arg)) {
  1044. ModuleLog(i, BLOG_ERROR, "wrong type");
  1045. goto fail0;
  1046. }
  1047. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1048. struct value *mov = valref_val(&mo->ref);
  1049. if (!mov) {
  1050. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1051. goto fail0;
  1052. }
  1053. struct value *v = value_get_path(i, mov, path_arg);
  1054. if (!v) {
  1055. goto fail0;
  1056. }
  1057. func_new_common(vo, i, v, NULL, NULL);
  1058. return;
  1059. fail0:
  1060. NCDModuleInst_Backend_DeadError(i);
  1061. }
  1062. static void func_new_insert_replace_common (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, int is_replace)
  1063. {
  1064. NCDValRef where_arg = NCDVal_NewInvalid();
  1065. NCDValRef what_arg;
  1066. if (!(!is_replace && NCDVal_ListRead(params->args, 1, &what_arg)) &&
  1067. !NCDVal_ListRead(params->args, 2, &where_arg, &what_arg)) {
  1068. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1069. goto fail0;
  1070. }
  1071. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1072. struct value *mov = valref_val(&mo->ref);
  1073. if (!mov) {
  1074. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1075. goto fail0;
  1076. }
  1077. struct value *v;
  1078. if (NCDVal_IsInvalid(where_arg)) {
  1079. v = value_insert_simple(i, mov, what_arg);
  1080. } else {
  1081. v = value_insert(i, mov, where_arg, what_arg, is_replace, NULL);
  1082. }
  1083. if (!v) {
  1084. goto fail0;
  1085. }
  1086. func_new_common(vo, i, v, NULL, NULL);
  1087. return;
  1088. fail0:
  1089. NCDModuleInst_Backend_DeadError(i);
  1090. }
  1091. static void func_new_insert (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1092. {
  1093. func_new_insert_replace_common(vo, i, params, 0);
  1094. }
  1095. static void func_new_replace (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1096. {
  1097. func_new_insert_replace_common(vo, i, params, 1);
  1098. }
  1099. struct insert_undo_deinit_data {
  1100. struct valref val_ref;
  1101. struct valref oldval_ref;
  1102. };
  1103. static void undo_deinit_func (struct insert_undo_deinit_data *data, NCDModuleInst *i)
  1104. {
  1105. struct value *val = valref_val(&data->val_ref);
  1106. struct value *oldval = valref_val(&data->oldval_ref);
  1107. if (val && val->parent && (!oldval || !oldval->parent)) {
  1108. // get parent
  1109. struct value *parent = val->parent;
  1110. // remove this value from parent and restore saved one (or none)
  1111. switch (parent->type) {
  1112. case NCDVAL_LIST: {
  1113. size_t index = value_list_indexof(parent, val);
  1114. value_list_remove(parent, val);
  1115. if (oldval) {
  1116. int res = value_list_insert(i, parent, oldval, index);
  1117. ASSERT_EXECUTE(res)
  1118. }
  1119. } break;
  1120. case NCDVAL_MAP: {
  1121. NCDValMem key_mem;
  1122. NCDValSafeRef key;
  1123. value_map_remove2(parent, val, &key_mem, &key);
  1124. if (oldval) {
  1125. int res = value_map_insert(parent, oldval, key_mem, key, i);
  1126. ASSERT_EXECUTE(res)
  1127. } else {
  1128. NCDValMem_Free(&key_mem);
  1129. }
  1130. } break;
  1131. default: ASSERT(0);
  1132. }
  1133. }
  1134. valref_free(&data->oldval_ref);
  1135. valref_free(&data->val_ref);
  1136. free(data);
  1137. }
  1138. static void func_new_insert_replace_undo_common (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, int is_replace)
  1139. {
  1140. NCDValRef where_arg = NCDVal_NewInvalid();
  1141. NCDValRef what_arg;
  1142. if (!(!is_replace && NCDVal_ListRead(params->args, 1, &what_arg)) &&
  1143. !NCDVal_ListRead(params->args, 2, &where_arg, &what_arg)) {
  1144. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1145. goto fail0;
  1146. }
  1147. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1148. struct value *mov = valref_val(&mo->ref);
  1149. if (!mov) {
  1150. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1151. goto fail0;
  1152. }
  1153. struct insert_undo_deinit_data *data = malloc(sizeof(*data));
  1154. if (!data) {
  1155. ModuleLog(i, BLOG_ERROR, "malloc failed");
  1156. goto fail0;
  1157. }
  1158. struct value *oldv;
  1159. struct value *v;
  1160. if (NCDVal_IsInvalid(where_arg)) {
  1161. oldv = NULL;
  1162. v = value_insert_simple(i, mov, what_arg);
  1163. } else {
  1164. v = value_insert(i, mov, where_arg, what_arg, is_replace, &oldv);
  1165. }
  1166. if (!v) {
  1167. goto fail1;
  1168. }
  1169. valref_init(&data->val_ref, v);
  1170. valref_init(&data->oldval_ref, oldv);
  1171. func_new_common(vo, i, v, (value_deinit_func)undo_deinit_func, data);
  1172. return;
  1173. fail1:
  1174. free(data);
  1175. fail0:
  1176. NCDModuleInst_Backend_DeadError(i);
  1177. }
  1178. static void func_new_insert_undo (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1179. {
  1180. func_new_insert_replace_undo_common(vo, i, params, 0);
  1181. }
  1182. static void func_new_replace_undo (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1183. {
  1184. func_new_insert_replace_undo_common(vo, i, params, 1);
  1185. }
  1186. static void func_new_replace_this (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1187. {
  1188. NCDValRef value_arg;
  1189. if (!NCDVal_ListRead(params->args, 1, &value_arg)) {
  1190. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1191. goto fail0;
  1192. }
  1193. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1194. struct value *mov = valref_val(&mo->ref);
  1195. if (!mov) {
  1196. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1197. goto fail0;
  1198. }
  1199. struct value *v = value_init_fromvalue(i, value_arg);
  1200. if (!v) {
  1201. goto fail0;
  1202. }
  1203. if (mov->parent) {
  1204. struct value *parent = mov->parent;
  1205. switch (parent->type) {
  1206. case NCDVAL_LIST: {
  1207. size_t index = value_list_indexof(parent, mov);
  1208. value_list_remove(parent, mov);
  1209. int res = value_list_insert(i, parent, v, index);
  1210. ASSERT_EXECUTE(res)
  1211. } break;
  1212. case NCDVAL_MAP: {
  1213. NCDValMem key_mem;
  1214. NCDValSafeRef key;
  1215. value_map_remove2(parent, mov, &key_mem, &key);
  1216. int res = value_map_insert(parent, v, key_mem, key, i);
  1217. ASSERT_EXECUTE(res)
  1218. } break;
  1219. default: ASSERT(0);
  1220. }
  1221. value_cleanup(mov);
  1222. }
  1223. func_new_common(vo, i, v, NULL, NULL);
  1224. return;
  1225. fail0:
  1226. NCDModuleInst_Backend_DeadError(i);
  1227. }
  1228. static void func_new_replace_this_undo (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1229. {
  1230. NCDValRef value_arg;
  1231. if (!NCDVal_ListRead(params->args, 1, &value_arg)) {
  1232. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1233. goto fail0;
  1234. }
  1235. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1236. struct value *mov = valref_val(&mo->ref);
  1237. if (!mov) {
  1238. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1239. goto fail0;
  1240. }
  1241. struct value *v = value_init_fromvalue(i, value_arg);
  1242. if (!v) {
  1243. goto fail0;
  1244. }
  1245. struct insert_undo_deinit_data *data = malloc(sizeof(*data));
  1246. if (!data) {
  1247. ModuleLog(i, BLOG_ERROR, "malloc failed");
  1248. goto fail1;
  1249. }
  1250. valref_init(&data->val_ref, v);
  1251. valref_init(&data->oldval_ref, mov);
  1252. if (mov->parent) {
  1253. struct value *parent = mov->parent;
  1254. switch (parent->type) {
  1255. case NCDVAL_LIST: {
  1256. size_t index = value_list_indexof(parent, mov);
  1257. value_list_remove(parent, mov);
  1258. int res = value_list_insert(i, parent, v, index);
  1259. ASSERT_EXECUTE(res)
  1260. } break;
  1261. case NCDVAL_MAP: {
  1262. NCDValMem key_mem;
  1263. NCDValSafeRef key;
  1264. value_map_remove2(parent, mov, &key_mem, &key);
  1265. int res = value_map_insert(parent, v, key_mem, key, i);
  1266. ASSERT_EXECUTE(res)
  1267. } break;
  1268. default: ASSERT(0);
  1269. }
  1270. }
  1271. func_new_common(vo, i, v, (value_deinit_func)undo_deinit_func, data);
  1272. return;
  1273. fail1:
  1274. value_cleanup(v);
  1275. fail0:
  1276. NCDModuleInst_Backend_DeadError(i);
  1277. }
  1278. static void func_new_substr (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1279. {
  1280. NCDValRef start_arg;
  1281. NCDValRef length_arg = NCDVal_NewInvalid();
  1282. if (!NCDVal_ListRead(params->args, 1, &start_arg) &&
  1283. !NCDVal_ListRead(params->args, 2, &start_arg, &length_arg)) {
  1284. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1285. goto fail0;
  1286. }
  1287. if (!NCDVal_IsString(start_arg) || (!NCDVal_IsInvalid(length_arg) && !NCDVal_IsString(length_arg))) {
  1288. ModuleLog(i, BLOG_ERROR, "wrong type");
  1289. goto fail0;
  1290. }
  1291. uintmax_t start;
  1292. if (!ncd_read_uintmax(start_arg, &start)) {
  1293. ModuleLog(i, BLOG_ERROR, "start is not a number");
  1294. goto fail0;
  1295. }
  1296. uintmax_t length = UINTMAX_MAX;
  1297. if (!NCDVal_IsInvalid(length_arg) && !ncd_read_uintmax(length_arg, &length)) {
  1298. ModuleLog(i, BLOG_ERROR, "length is not a number");
  1299. goto fail0;
  1300. }
  1301. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1302. struct value *mov = valref_val(&mo->ref);
  1303. if (!mov) {
  1304. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1305. goto fail0;
  1306. }
  1307. if (mov->type != NCDVAL_STRING && mov->type != IDSTRING_TYPE) {
  1308. ModuleLog(i, BLOG_ERROR, "value is not a string");
  1309. goto fail0;
  1310. }
  1311. const char *string_data;
  1312. size_t string_len;
  1313. if (mov->type == IDSTRING_TYPE) {
  1314. string_data = NCDStringIndex_Value(mov->idstring.string_index, mov->idstring.id);
  1315. string_len = NCDStringIndex_Length(mov->idstring.string_index, mov->idstring.id);
  1316. } else {
  1317. string_data = mov->string.string;
  1318. string_len = mov->string.length;
  1319. }
  1320. if (start > string_len) {
  1321. ModuleLog(i, BLOG_ERROR, "start is out of range");
  1322. goto fail0;
  1323. }
  1324. size_t remain = string_len - start;
  1325. size_t amount = length < remain ? length : remain;
  1326. struct value *v = value_init_string(i, string_data + start, amount);
  1327. if (!v) {
  1328. goto fail0;
  1329. }
  1330. func_new_common(vo, i, v, NULL, NULL);
  1331. return;
  1332. fail0:
  1333. NCDModuleInst_Backend_DeadError(i);
  1334. }
  1335. static void remove_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1336. {
  1337. NCDValRef where_arg;
  1338. if (!NCDVal_ListRead(params->args, 1, &where_arg)) {
  1339. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1340. goto fail0;
  1341. }
  1342. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1343. struct value *mov = valref_val(&mo->ref);
  1344. if (!mov) {
  1345. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1346. goto fail0;
  1347. }
  1348. if (!value_remove(i, mov, where_arg)) {
  1349. goto fail0;
  1350. }
  1351. NCDModuleInst_Backend_Up(i);
  1352. return;
  1353. fail0:
  1354. NCDModuleInst_Backend_DeadError(i);
  1355. }
  1356. static void delete_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1357. {
  1358. if (!NCDVal_ListRead(params->args, 0)) {
  1359. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1360. goto fail0;
  1361. }
  1362. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1363. struct value *mov = valref_val(&mo->ref);
  1364. if (!mov) {
  1365. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1366. goto fail0;
  1367. }
  1368. value_delete(mov);
  1369. NCDModuleInst_Backend_Up(i);
  1370. return;
  1371. fail0:
  1372. NCDModuleInst_Backend_DeadError(i);
  1373. }
  1374. static void reset_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1375. {
  1376. NCDValRef what_arg;
  1377. if (!NCDVal_ListRead(params->args, 1, &what_arg)) {
  1378. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1379. goto fail0;
  1380. }
  1381. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1382. // build value from argument
  1383. struct value *newv = value_init_fromvalue(i, what_arg);
  1384. if (!newv) {
  1385. goto fail0;
  1386. }
  1387. // deinit
  1388. if (mo->deinit_func) {
  1389. mo->deinit_func(mo->deinit_data, i);
  1390. }
  1391. // free value reference
  1392. valref_free(&mo->ref);
  1393. // set up value reference
  1394. valref_init(&mo->ref, newv);
  1395. // set no deinit function
  1396. mo->deinit_func = NULL;
  1397. NCDModuleInst_Backend_Up(i);
  1398. return;
  1399. fail0:
  1400. NCDModuleInst_Backend_DeadError(i);
  1401. }
  1402. static void append_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  1403. {
  1404. NCDValRef data_arg;
  1405. if (!NCDVal_ListRead(params->args, 1, &data_arg)) {
  1406. ModuleLog(i, BLOG_ERROR, "wrong arity");
  1407. goto fail0;
  1408. }
  1409. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  1410. struct value *mov = valref_val(&mo->ref);
  1411. if (!mov) {
  1412. ModuleLog(i, BLOG_ERROR, "value was deleted");
  1413. goto fail0;
  1414. }
  1415. if (!value_append(i, mov, data_arg)) {
  1416. goto fail0;
  1417. }
  1418. NCDModuleInst_Backend_Up(i);
  1419. return;
  1420. fail0:
  1421. NCDModuleInst_Backend_DeadError(i);
  1422. }
  1423. static struct NCDModule modules[] = {
  1424. {
  1425. .type = "value",
  1426. .func_new2 = func_new_value,
  1427. .func_die = func_die,
  1428. .func_getvar2 = func_getvar2,
  1429. .alloc_size = sizeof(struct instance)
  1430. }, {
  1431. .type = "value::get",
  1432. .base_type = "value",
  1433. .func_new2 = func_new_get,
  1434. .func_die = func_die,
  1435. .func_getvar2 = func_getvar2,
  1436. .alloc_size = sizeof(struct instance)
  1437. }, {
  1438. .type = "value::try_get",
  1439. .base_type = "value",
  1440. .func_new2 = func_new_try_get,
  1441. .func_die = func_die,
  1442. .func_getvar2 = func_getvar2,
  1443. .alloc_size = sizeof(struct instance)
  1444. }, {
  1445. .type = "value::getpath",
  1446. .base_type = "value",
  1447. .func_new2 = func_new_getpath,
  1448. .func_die = func_die,
  1449. .func_getvar2 = func_getvar2,
  1450. .alloc_size = sizeof(struct instance)
  1451. }, {
  1452. .type = "value::insert",
  1453. .base_type = "value",
  1454. .func_new2 = func_new_insert,
  1455. .func_die = func_die,
  1456. .func_getvar2 = func_getvar2,
  1457. .alloc_size = sizeof(struct instance)
  1458. }, {
  1459. .type = "value::replace",
  1460. .base_type = "value",
  1461. .func_new2 = func_new_replace,
  1462. .func_die = func_die,
  1463. .func_getvar2 = func_getvar2,
  1464. .alloc_size = sizeof(struct instance)
  1465. }, {
  1466. .type = "value::replace_this",
  1467. .base_type = "value",
  1468. .func_new2 = func_new_replace_this,
  1469. .func_die = func_die,
  1470. .func_getvar2 = func_getvar2,
  1471. .alloc_size = sizeof(struct instance)
  1472. }, {
  1473. .type = "value::insert_undo",
  1474. .base_type = "value",
  1475. .func_new2 = func_new_insert_undo,
  1476. .func_die = func_die,
  1477. .func_getvar2 = func_getvar2,
  1478. .alloc_size = sizeof(struct instance)
  1479. }, {
  1480. .type = "value::replace_undo",
  1481. .base_type = "value",
  1482. .func_new2 = func_new_replace_undo,
  1483. .func_die = func_die,
  1484. .func_getvar2 = func_getvar2,
  1485. .alloc_size = sizeof(struct instance)
  1486. }, {
  1487. .type = "value::replace_this_undo",
  1488. .base_type = "value",
  1489. .func_new2 = func_new_replace_this_undo,
  1490. .func_die = func_die,
  1491. .func_getvar2 = func_getvar2,
  1492. .alloc_size = sizeof(struct instance)
  1493. }, {
  1494. .type = "value::remove",
  1495. .func_new2 = remove_func_new
  1496. }, {
  1497. .type = "value::delete",
  1498. .func_new2 = delete_func_new
  1499. }, {
  1500. .type = "value::reset",
  1501. .func_new2 = reset_func_new
  1502. }, {
  1503. .type = "value::substr",
  1504. .base_type = "value",
  1505. .func_new2 = func_new_substr,
  1506. .func_die = func_die,
  1507. .func_getvar2 = func_getvar2,
  1508. .alloc_size = sizeof(struct instance)
  1509. }, {
  1510. .type = "value::append",
  1511. .func_new2 = append_func_new
  1512. }, {
  1513. .type = NULL
  1514. }
  1515. };
  1516. const struct NCDModuleGroup ncdmodule_value = {
  1517. .modules = modules,
  1518. .strings = strings
  1519. };