value.c 62 KB

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