value.c 53 KB

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