value.c 55 KB

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