value.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098
  1. /**
  2. * @file value.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Synopsis:
  32. * value(value)
  33. * value value::get(where)
  34. * value value::try_get(where)
  35. * value value::getpath(list path)
  36. * value value::insert(where, what)
  37. * value value::insert(what)
  38. * value value::replace(where, what)
  39. * value value::replace_this(value)
  40. * value value::insert_undo(where, what)
  41. * value value::insert_undo(what)
  42. * value value::replace_undo(where, what)
  43. * value value::replace_this_undo(value)
  44. *
  45. * Description:
  46. * Value objects allow examining and manipulating values.
  47. * These value objects are actually references to internal value structures, which
  48. * may be shared between value objects.
  49. *
  50. * value(value) constructs a new value object from the given value.
  51. *
  52. * value::get(where) constructs a value object for the element at position 'where'
  53. * (for a list), or the value corresponding to key 'where' (for a map). It is an
  54. * error if the base value is not a list or a map, the index is out of bounds of
  55. * the list, or the key does not exist in the map.
  56. * The resulting value object is NOT a copy, and shares (part of) the same
  57. * underlying value structure as the base value object. Deleting it will remove
  58. * it from the list or map it is part of.
  59. *
  60. * value::try_get(where) is like get(), except that if any restriction on 'where'
  61. * is violated, no error is triggered; instead, the value object is constructed
  62. * as being deleted; this state is exposed via the 'exists' variable.
  63. * This can be used to check for the presence of a key in a map, and in case it
  64. * exists, allow access to the corresponding value without another get() statement.
  65. *
  66. * value::getpath(path) is like get(), except that it performs multiple
  67. * consecutive resolutions. Also, if the path is an empty list, it performs
  68. * no resulution at all.
  69. *
  70. * value::insert(where, what) constructs a value object by inserting into an
  71. * existing value object.
  72. * For lists, 'where' is the index of the element to insert before, or the length
  73. * of the list to append to it.
  74. * For maps, 'where' is the key to insert under. If the key already exists in the
  75. * map, its value is replaced; any references to the old value however remain valid.
  76. *
  77. * value::insert(what) constructs a value object by appending to a list. An error
  78. * is triggered if the base value is not a list. Assuming 'list' is a list value,
  79. * list->insert(X) is equivalent to list->insert(list.length, X).
  80. *
  81. * value::replace(where, what) is like insert(), exept that, when inserting into a
  82. * list, the value at the specified index is replaced with the new value (unless
  83. * the index is equal to the length of the list).
  84. *
  85. * insert_undo() and replace_undo() are versions of insert() and replace() which
  86. * attempt to revert the modifications when they deinitialize.
  87. * Specifically, they work like that:
  88. * - On initiialization, they take an internal reference to the value being replaced
  89. * (if any; note that insert_undo() into a list never replaces a value).
  90. * - On deinitialization, they remove the the inserted value from its parent (if there
  91. * is one), and insert the old replaced value (to which a reference was kept) in that
  92. * place (if any, and assuming it has not been deleted).
  93. * Note that if the inserted value changes parents in between init and deinit, the
  94. * result of undoing may be unexpected.
  95. *
  96. * Variables:
  97. * (empty) - the value stored in the value object
  98. * type - type of the value; "string", "list" or "map"
  99. * length - number of elements in the list or map, or the number of bytes in a
  100. * string
  101. * keys - a list of keys in the map (only if the value is a map)
  102. * exists - "true" or "false", reflecting whether the value object holds a value
  103. * (is not in deleted state)
  104. *
  105. * Synopsis:
  106. * value::remove(where)
  107. * value::delete()
  108. *
  109. * Description:
  110. * value::remove(where) removes from an existing value object.
  111. * For lists, 'where' is the index of the element to remove, and must be in range.
  112. * For maps, 'where' is the key to remove, and must be an existing key.
  113. * In any case, any references to the removed value remain valid.
  114. *
  115. * value::delete() deletes the underlying value data of this value object.
  116. * After delection, the value object enters a deleted state, which will cause any
  117. * operation on it to fail. Any other value objects which referred to the same value
  118. * or parts of it will too enter deleted state. If the value was an element
  119. * in a list or map, is is removed from it.
  120. *
  121. * Synopsis:
  122. * value value::substr(string start [, string length])
  123. *
  124. * Description:
  125. * Constructs a string value by extracting a part of a string.
  126. * 'start' specifies the index of the character (from zero) where the substring to
  127. * extract starts, and must be <= the length of the string.
  128. * 'length' specifies the maximum number of characters to extract, if given.
  129. * The newly constructed value is a copy of the extracted substring.
  130. * The value must be a string value.
  131. *
  132. * Synopsis:
  133. * value::reset(what)
  134. *
  135. * Description:
  136. * Effectively deconstructs and reconstructs the value object. More precisely,
  137. * it builds a new value structure from 'what', possibly invokes a scheduled undo
  138. * operation (as scheduled by insert_undo() and replace_undo()), sets up this
  139. * value object to reference the newly built value structure, without any scheduled
  140. * undo operation.
  141. *
  142. * Synopsis:
  143. * value::append(append_val)
  144. *
  145. * Description:
  146. * Only defined when the existing value object is a string or list. If it is a string,
  147. * appends the string 'append_val' to this string value; 'append_val' must be a string.
  148. * If is is a list, inserts 'append_val' to the end of this list value. Unlike insert(),
  149. * the resulting append() object is not itself a value object (which in case of insert()
  150. * serves as a reference to the new value).
  151. */
  152. #include <stdlib.h>
  153. #include <string.h>
  154. #include <stddef.h>
  155. #include <limits.h>
  156. #include <inttypes.h>
  157. #include <misc/offset.h>
  158. #include <misc/debug.h>
  159. #include <misc/balloc.h>
  160. #include <structure/LinkedList0.h>
  161. #include <structure/IndexedList.h>
  162. #include <structure/SAvl.h>
  163. #include <ncd/NCDStringIndex.h>
  164. #include <ncd/module_common.h>
  165. #include <generated/blog_channel_ncd_value.h>
  166. #define STOREDSTRING_TYPE (NCDVAL_STRING | (0 << 3))
  167. #define IDSTRING_TYPE (NCDVAL_STRING | (1 << 3))
  168. #define EXTERNALSTRING_TYPE (NCDVAL_STRING | (2 << 3))
  169. #define COMPOSEDSTRING_TYPE (NCDVAL_STRING | (3 << 3))
  170. struct value;
  171. #include "value_maptree.h"
  172. #include <structure/SAvl_decl.h>
  173. struct valref {
  174. struct value *v;
  175. LinkedList0Node refs_list_node;
  176. };
  177. typedef void (*value_deinit_func) (void *deinit_data, NCDModuleInst *i);
  178. struct instance {
  179. NCDModuleInst *i;
  180. struct valref ref;
  181. value_deinit_func deinit_func;
  182. void *deinit_data;
  183. };
  184. struct value {
  185. LinkedList0 refs_list;
  186. struct value *parent;
  187. union {
  188. struct {
  189. IndexedListNode list_contents_il_node;
  190. } list_parent;
  191. struct {
  192. NCDValMem key_mem;
  193. NCDValRef key;
  194. MapTreeNode maptree_node;
  195. } map_parent;
  196. };
  197. int type;
  198. union {
  199. struct {
  200. char *data;
  201. size_t length;
  202. } storedstring;
  203. struct {
  204. NCD_string_id_t id;
  205. NCDStringIndex *string_index;
  206. } idstring;
  207. struct {
  208. const char *data;
  209. size_t length;
  210. BRefTarget *ref_target;
  211. } externalstring;
  212. struct {
  213. NCDValComposedStringResource resource;
  214. size_t offset;
  215. size_t length;
  216. } composedstring;
  217. struct {
  218. IndexedList list_contents_il;
  219. } list;
  220. struct {
  221. MapTree map_tree;
  222. } map;
  223. };
  224. };
  225. static const char * get_type_str (int type);
  226. static void value_cleanup (struct value *v);
  227. static void value_delete (struct value *v);
  228. static struct value * value_init_storedstring (NCDModuleInst *i, const char *str, size_t len);
  229. static struct value * value_init_idstring (NCDModuleInst *i, NCD_string_id_t id, NCDStringIndex *string_index);
  230. static struct value * value_init_externalstring (NCDModuleInst *i, const char *data, size_t length, BRefTarget *ref_target);
  231. static struct value * value_init_composedstring (NCDModuleInst *i, NCDValComposedStringResource resource, size_t offset, size_t length);
  232. static int value_is_string (struct value *v);
  233. static size_t value_string_length (struct value *v);
  234. static void value_string_copy_out (struct value *v, char *dest);
  235. static void value_string_set_allocd (struct value *v, char *data, size_t length);
  236. static struct value * value_init_list (NCDModuleInst *i);
  237. static size_t value_list_len (struct value *v);
  238. static struct value * value_list_at (struct value *v, size_t index);
  239. static size_t value_list_indexof (struct value *v, struct value *ev);
  240. static int value_list_insert (NCDModuleInst *i, struct value *list, struct value *v, size_t index);
  241. static void value_list_remove (struct value *list, struct value *v);
  242. static struct value * value_init_map (NCDModuleInst *i);
  243. static size_t value_map_len (struct value *map);
  244. static struct value * value_map_at (struct value *map, size_t index);
  245. static struct value * value_map_find (struct value *map, NCDValRef key);
  246. static int value_map_insert (struct value *map, struct value *v, NCDValMem mem, NCDValSafeRef key, NCDModuleInst *i);
  247. static void value_map_remove (struct value *map, struct value *v);
  248. static void value_map_remove2 (struct value *map, struct value *v, NCDValMem *out_mem, NCDValSafeRef *out_key);
  249. static struct value * value_init_fromvalue (NCDModuleInst *i, NCDValRef value);
  250. static int value_to_value (NCDModuleInst *i, struct value *v, NCDValMem *mem, NCDValRef *out_value);
  251. static struct value * value_get (NCDModuleInst *i, struct value *v, NCDValRef where, int no_error);
  252. static struct value * value_get_path (NCDModuleInst *i, struct value *v, NCDValRef path);
  253. static struct value * value_insert (NCDModuleInst *i, struct value *v, NCDValRef where, NCDValRef what, int is_replace, struct value **out_oldv);
  254. static struct value * value_insert_simple (NCDModuleInst *i, struct value *v, NCDValRef what);
  255. static int value_remove (NCDModuleInst *i, struct value *v, NCDValRef where);
  256. static int value_append (NCDModuleInst *i, struct value *v, NCDValRef data);
  257. static void valref_init (struct valref *r, struct value *v);
  258. static void valref_free (struct valref *r);
  259. static struct value * valref_val (struct valref *r);
  260. static void valref_break (struct valref *r);
  261. enum {STRING_EXISTS, STRING_KEYS};
  262. static const char *strings[] = {
  263. "exists", "keys", NULL
  264. };
  265. #include "value_maptree.h"
  266. #include <structure/SAvl_impl.h>
  267. static const char * get_type_str (int type)
  268. {
  269. switch (type) {
  270. case STOREDSTRING_TYPE:
  271. case IDSTRING_TYPE:
  272. case EXTERNALSTRING_TYPE:
  273. case COMPOSEDSTRING_TYPE:
  274. return "string";
  275. case NCDVAL_LIST:
  276. return "list";
  277. case NCDVAL_MAP:
  278. return "map";
  279. }
  280. ASSERT(0)
  281. return NULL;
  282. }
  283. static void value_cleanup (struct value *v)
  284. {
  285. if (v->parent || !LinkedList0_IsEmpty(&v->refs_list)) {
  286. return;
  287. }
  288. switch (v->type) {
  289. case STOREDSTRING_TYPE: {
  290. BFree(v->storedstring.data);
  291. } break;
  292. case IDSTRING_TYPE: {
  293. } break;
  294. case EXTERNALSTRING_TYPE: {
  295. if (v->externalstring.ref_target) {
  296. BRefTarget_Deref(v->externalstring.ref_target);
  297. }
  298. } break;
  299. case COMPOSEDSTRING_TYPE: {
  300. if (v->composedstring.resource.ref_target) {
  301. BRefTarget_Deref(v->composedstring.resource.ref_target);
  302. }
  303. } break;
  304. case NCDVAL_LIST: {
  305. while (value_list_len(v) > 0) {
  306. struct value *ev = value_list_at(v, 0);
  307. value_list_remove(v, ev);
  308. value_cleanup(ev);
  309. }
  310. } break;
  311. case NCDVAL_MAP: {
  312. while (value_map_len(v) > 0) {
  313. struct value *ev = value_map_at(v, 0);
  314. value_map_remove(v, ev);
  315. value_cleanup(ev);
  316. }
  317. } break;
  318. default: ASSERT(0);
  319. }
  320. free(v);
  321. }
  322. static void value_delete (struct value *v)
  323. {
  324. if (v->parent) {
  325. switch (v->parent->type) {
  326. case NCDVAL_LIST: {
  327. value_list_remove(v->parent, v);
  328. } break;
  329. case NCDVAL_MAP: {
  330. value_map_remove(v->parent, v);
  331. } break;
  332. default: ASSERT(0);
  333. }
  334. }
  335. LinkedList0Node *ln;
  336. while (ln = LinkedList0_GetFirst(&v->refs_list)) {
  337. struct valref *r = UPPER_OBJECT(ln, struct valref, refs_list_node);
  338. ASSERT(r->v == v)
  339. valref_break(r);
  340. }
  341. switch (v->type) {
  342. case STOREDSTRING_TYPE: {
  343. BFree(v->storedstring.data);
  344. } break;
  345. case IDSTRING_TYPE: {
  346. } break;
  347. case EXTERNALSTRING_TYPE: {
  348. if (v->externalstring.ref_target) {
  349. BRefTarget_Deref(v->externalstring.ref_target);
  350. }
  351. } break;
  352. case COMPOSEDSTRING_TYPE: {
  353. if (v->composedstring.resource.ref_target) {
  354. BRefTarget_Deref(v->composedstring.resource.ref_target);
  355. }
  356. } break;
  357. case NCDVAL_LIST: {
  358. while (value_list_len(v) > 0) {
  359. struct value *ev = value_list_at(v, 0);
  360. value_delete(ev);
  361. }
  362. } break;
  363. case NCDVAL_MAP: {
  364. while (value_map_len(v) > 0) {
  365. struct value *ev = value_map_at(v, 0);
  366. value_delete(ev);
  367. }
  368. } break;
  369. default: ASSERT(0);
  370. }
  371. free(v);
  372. }
  373. static struct value * value_init_storedstring (NCDModuleInst *i, const char *str, size_t len)
  374. {
  375. struct value *v = malloc(sizeof(*v));
  376. if (!v) {
  377. ModuleLog(i, BLOG_ERROR, "malloc failed");
  378. goto fail0;
  379. }
  380. LinkedList0_Init(&v->refs_list);
  381. v->parent = NULL;
  382. v->type = STOREDSTRING_TYPE;
  383. if (!(v->storedstring.data = BAlloc(len))) {
  384. ModuleLog(i, BLOG_ERROR, "BAlloc failed");
  385. goto fail1;
  386. }
  387. if (str) {
  388. memcpy(v->storedstring.data, str, len);
  389. }
  390. v->storedstring.length = len;
  391. return v;
  392. fail1:
  393. free(v);
  394. fail0:
  395. return NULL;
  396. }
  397. static struct value * value_init_idstring (NCDModuleInst *i, NCD_string_id_t id, NCDStringIndex *string_index)
  398. {
  399. ASSERT(string_index == i->params->iparams->string_index)
  400. struct value *v = malloc(sizeof(*v));
  401. if (!v) {
  402. ModuleLog(i, BLOG_ERROR, "malloc failed");
  403. goto fail0;
  404. }
  405. LinkedList0_Init(&v->refs_list);
  406. v->parent = NULL;
  407. v->type = IDSTRING_TYPE;
  408. v->idstring.id = id;
  409. v->idstring.string_index = string_index;
  410. return v;
  411. fail0:
  412. return NULL;
  413. }
  414. static struct value * value_init_externalstring (NCDModuleInst *i, const char *data, size_t length, BRefTarget *ref_target)
  415. {
  416. struct value *v = malloc(sizeof(*v));
  417. if (!v) {
  418. ModuleLog(i, BLOG_ERROR, "malloc failed");
  419. goto fail0;
  420. }
  421. if (ref_target) {
  422. if (!BRefTarget_Ref(ref_target)) {
  423. ModuleLog(i, BLOG_ERROR, "BRefTarget_Ref failed");
  424. goto fail1;
  425. }
  426. }
  427. LinkedList0_Init(&v->refs_list);
  428. v->parent = NULL;
  429. v->type = EXTERNALSTRING_TYPE;
  430. v->externalstring.data = data;
  431. v->externalstring.length = length;
  432. v->externalstring.ref_target = ref_target;
  433. return v;
  434. fail1:
  435. free(v);
  436. fail0:
  437. return NULL;
  438. }
  439. static struct value * value_init_composedstring (NCDModuleInst *i, NCDValComposedStringResource resource, size_t offset, size_t length)
  440. {
  441. struct value *v = malloc(sizeof(*v));
  442. if (!v) {
  443. ModuleLog(i, BLOG_ERROR, "malloc failed");
  444. goto fail0;
  445. }
  446. if (resource.ref_target) {
  447. if (!BRefTarget_Ref(resource.ref_target)) {
  448. ModuleLog(i, BLOG_ERROR, "BRefTarget_Ref failed");
  449. goto fail1;
  450. }
  451. }
  452. LinkedList0_Init(&v->refs_list);
  453. v->parent = NULL;
  454. v->type = COMPOSEDSTRING_TYPE;
  455. v->composedstring.resource = resource;
  456. v->composedstring.offset = offset;
  457. v->composedstring.length = length;
  458. return v;
  459. fail1:
  460. free(v);
  461. fail0:
  462. return NULL;
  463. }
  464. static int value_is_string (struct value *v)
  465. {
  466. switch (v->type) {
  467. case STOREDSTRING_TYPE:
  468. case IDSTRING_TYPE:
  469. case EXTERNALSTRING_TYPE:
  470. case COMPOSEDSTRING_TYPE:
  471. return 1;
  472. default:
  473. return 0;
  474. }
  475. }
  476. static size_t value_string_length (struct value *v)
  477. {
  478. ASSERT(value_is_string(v))
  479. switch (v->type) {
  480. case STOREDSTRING_TYPE:
  481. return v->storedstring.length;
  482. case IDSTRING_TYPE:
  483. return NCDStringIndex_Length(v->idstring.string_index, v->idstring.id);
  484. case EXTERNALSTRING_TYPE:
  485. return v->externalstring.length;
  486. case COMPOSEDSTRING_TYPE:
  487. return v->composedstring.length;
  488. default:
  489. ASSERT(0);
  490. return 0;
  491. }
  492. }
  493. static void value_string_copy_out (struct value *v, char *dest)
  494. {
  495. ASSERT(value_is_string(v))
  496. switch (v->type) {
  497. case STOREDSTRING_TYPE:
  498. memcpy(dest, v->storedstring.data, v->storedstring.length);
  499. break;
  500. case IDSTRING_TYPE:
  501. memcpy(dest, NCDStringIndex_Value(v->idstring.string_index, v->idstring.id), NCDStringIndex_Length(v->idstring.string_index, v->idstring.id));
  502. break;
  503. case EXTERNALSTRING_TYPE:
  504. memcpy(dest, v->externalstring.data, v->externalstring.length);
  505. break;
  506. case COMPOSEDSTRING_TYPE: {
  507. b_cstring cstr = NCDValComposedStringResource_Cstring(v->composedstring.resource, v->composedstring.offset, v->composedstring.length);
  508. b_cstring_copy_to_buf(cstr, 0, cstr.length, dest);
  509. } break;
  510. default:
  511. ASSERT(0);
  512. }
  513. }
  514. static void value_string_set_allocd (struct value *v, char *data, size_t length)
  515. {
  516. ASSERT(value_is_string(v))
  517. ASSERT(data)
  518. switch (v->type) {
  519. case STOREDSTRING_TYPE: {
  520. BFree(v->storedstring.data);
  521. } break;
  522. case IDSTRING_TYPE: {
  523. } break;
  524. case EXTERNALSTRING_TYPE: {
  525. if (v->externalstring.ref_target) {
  526. BRefTarget_Deref(v->externalstring.ref_target);
  527. }
  528. } break;
  529. case COMPOSEDSTRING_TYPE: {
  530. if (v->composedstring.resource.ref_target) {
  531. BRefTarget_Deref(v->composedstring.resource.ref_target);
  532. }
  533. } break;
  534. default:
  535. ASSERT(0);
  536. }
  537. v->type = STOREDSTRING_TYPE;
  538. v->storedstring.data = data;
  539. v->storedstring.length = length;
  540. }
  541. static struct value * value_init_list (NCDModuleInst *i)
  542. {
  543. struct value *v = malloc(sizeof(*v));
  544. if (!v) {
  545. ModuleLog(i, BLOG_ERROR, "malloc failed");
  546. return NULL;
  547. }
  548. LinkedList0_Init(&v->refs_list);
  549. v->parent = NULL;
  550. v->type = NCDVAL_LIST;
  551. IndexedList_Init(&v->list.list_contents_il);
  552. return v;
  553. }
  554. static size_t value_list_len (struct value *v)
  555. {
  556. ASSERT(v->type == NCDVAL_LIST)
  557. return IndexedList_Count(&v->list.list_contents_il);
  558. }
  559. static struct value * value_list_at (struct value *v, size_t index)
  560. {
  561. ASSERT(v->type == NCDVAL_LIST)
  562. ASSERT(index < value_list_len(v))
  563. IndexedListNode *iln = IndexedList_GetAt(&v->list.list_contents_il, index);
  564. ASSERT(iln)
  565. struct value *e = UPPER_OBJECT(iln, struct value, list_parent.list_contents_il_node);
  566. ASSERT(e->parent == v)
  567. return e;
  568. }
  569. static size_t value_list_indexof (struct value *v, struct value *ev)
  570. {
  571. ASSERT(v->type == NCDVAL_LIST)
  572. ASSERT(ev->parent == v)
  573. uint64_t index = IndexedList_IndexOf(&v->list.list_contents_il, &ev->list_parent.list_contents_il_node);
  574. ASSERT(index < value_list_len(v))
  575. return index;
  576. }
  577. static int value_list_insert (NCDModuleInst *i, struct value *list, struct value *v, size_t index)
  578. {
  579. ASSERT(list->type == NCDVAL_LIST)
  580. ASSERT(!v->parent)
  581. ASSERT(index <= value_list_len(list))
  582. if (value_list_len(list) == SIZE_MAX) {
  583. ModuleLog(i, BLOG_ERROR, "list has too many elements");
  584. return 0;
  585. }
  586. IndexedList_InsertAt(&list->list.list_contents_il, &v->list_parent.list_contents_il_node, index);
  587. v->parent = list;
  588. return 1;
  589. }
  590. static void value_list_remove (struct value *list, struct value *v)
  591. {
  592. ASSERT(list->type == NCDVAL_LIST)
  593. ASSERT(v->parent == list)
  594. IndexedList_Remove(&list->list.list_contents_il, &v->list_parent.list_contents_il_node);
  595. v->parent = NULL;
  596. }
  597. static struct value * value_init_map (NCDModuleInst *i)
  598. {
  599. struct value *v = malloc(sizeof(*v));
  600. if (!v) {
  601. ModuleLog(i, BLOG_ERROR, "malloc failed");
  602. return NULL;
  603. }
  604. LinkedList0_Init(&v->refs_list);
  605. v->parent = NULL;
  606. v->type = NCDVAL_MAP;
  607. MapTree_Init(&v->map.map_tree);
  608. return v;
  609. }
  610. static size_t value_map_len (struct value *map)
  611. {
  612. ASSERT(map->type == NCDVAL_MAP)
  613. return MapTree_Count(&map->map.map_tree, 0);
  614. }
  615. static struct value * value_map_at (struct value *map, size_t index)
  616. {
  617. ASSERT(map->type == NCDVAL_MAP)
  618. ASSERT(index < value_map_len(map))
  619. struct value *e = MapTree_GetAt(&map->map.map_tree, 0, index);
  620. ASSERT(e)
  621. ASSERT(e->parent == map)
  622. return e;
  623. }
  624. static struct value * value_map_find (struct value *map, NCDValRef key)
  625. {
  626. ASSERT(map->type == NCDVAL_MAP)
  627. ASSERT(NCDVal_Type(key))
  628. struct value *e = MapTree_LookupExact(&map->map.map_tree, 0, key);
  629. ASSERT(!e || e->parent == map)
  630. return e;
  631. }
  632. static int value_map_insert (struct value *map, struct value *v, NCDValMem mem, NCDValSafeRef key, NCDModuleInst *i)
  633. {
  634. ASSERT(map->type == NCDVAL_MAP)
  635. ASSERT(!v->parent)
  636. ASSERT((NCDVal_Type(NCDVal_FromSafe(&mem, key)), 1))
  637. ASSERT(!value_map_find(map, NCDVal_FromSafe(&mem, key)))
  638. if (value_map_len(map) == SIZE_MAX) {
  639. ModuleLog(i, BLOG_ERROR, "map has too many elements");
  640. return 0;
  641. }
  642. v->map_parent.key_mem = mem;
  643. v->map_parent.key = NCDVal_FromSafe(&v->map_parent.key_mem, key);
  644. int res = MapTree_Insert(&map->map.map_tree, 0, v, NULL);
  645. ASSERT_EXECUTE(res)
  646. v->parent = map;
  647. return 1;
  648. }
  649. static void value_map_remove (struct value *map, struct value *v)
  650. {
  651. ASSERT(map->type == NCDVAL_MAP)
  652. ASSERT(v->parent == map)
  653. MapTree_Remove(&map->map.map_tree, 0, v);
  654. NCDValMem_Free(&v->map_parent.key_mem);
  655. v->parent = NULL;
  656. }
  657. static void value_map_remove2 (struct value *map, struct value *v, NCDValMem *out_mem, NCDValSafeRef *out_key)
  658. {
  659. ASSERT(map->type == NCDVAL_MAP)
  660. ASSERT(v->parent == map)
  661. ASSERT(out_mem)
  662. ASSERT(out_key)
  663. MapTree_Remove(&map->map.map_tree, 0, v);
  664. *out_mem = v->map_parent.key_mem;
  665. *out_key = NCDVal_ToSafe(v->map_parent.key);
  666. v->parent = NULL;
  667. }
  668. static struct value * value_init_fromvalue (NCDModuleInst *i, NCDValRef value)
  669. {
  670. ASSERT((NCDVal_Type(value), 1))
  671. struct value *v;
  672. switch (NCDVal_Type(value)) {
  673. case NCDVAL_STRING: {
  674. if (NCDVal_IsStoredString(value)) {
  675. v = value_init_storedstring(i, NCDVal_StringData(value), NCDVal_StringLength(value));
  676. } else if (NCDVal_IsIdString(value)) {
  677. v = value_init_idstring(i, NCDVal_IdStringId(value), NCDVal_IdStringStringIndex(value));
  678. } else if (NCDVal_IsExternalString(value)) {
  679. v = value_init_externalstring(i, NCDVal_StringData(value), NCDVal_StringLength(value), NCDVal_ExternalStringTarget(value));
  680. } else if (NCDVal_IsComposedString(value)) {
  681. v = value_init_composedstring(i, NCDVal_ComposedStringResource(value), NCDVal_ComposedStringOffset(value), NCDVal_StringLength(value));
  682. } else {
  683. size_t length = NCDVal_StringLength(value);
  684. v = value_init_storedstring(i, NULL, length);
  685. if (v) {
  686. NCDVal_StringCopyOut(value, 0, length, v->storedstring.data);
  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->storedstring.data, v->storedstring.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->storedstring.length) {
  1016. ModuleLog(i, BLOG_ERROR, "too much data to append");
  1017. return 0;
  1018. }
  1019. size_t new_length = v->storedstring.length + append_length;
  1020. char *new_string = BRealloc(v->storedstring.data, 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->storedstring.length);
  1026. v->storedstring.data = new_string;
  1027. v->storedstring.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 length = value_string_length(v);
  1037. size_t append_length = NCDVal_StringLength(data);
  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. case NCDVAL_LIST: {
  1053. struct value *nv = value_init_fromvalue(i, data);
  1054. if (!nv) {
  1055. return 0;
  1056. }
  1057. if (!value_list_insert(i, v, nv, value_list_len(v))) {
  1058. value_cleanup(nv);
  1059. return 0;
  1060. }
  1061. } break;
  1062. default:
  1063. ModuleLog(i, BLOG_ERROR, "append is only defined for strings and lists");
  1064. return 0;
  1065. }
  1066. return 1;
  1067. }
  1068. static void valref_init (struct valref *r, struct value *v)
  1069. {
  1070. r->v = v;
  1071. if (v) {
  1072. LinkedList0_Prepend(&v->refs_list, &r->refs_list_node);
  1073. }
  1074. }
  1075. static void valref_free (struct valref *r)
  1076. {
  1077. if (r->v) {
  1078. LinkedList0_Remove(&r->v->refs_list, &r->refs_list_node);
  1079. value_cleanup(r->v);
  1080. }
  1081. }
  1082. static struct value * valref_val (struct valref *r)
  1083. {
  1084. return r->v;
  1085. }
  1086. static void valref_break (struct valref *r)
  1087. {
  1088. ASSERT(r->v)
  1089. LinkedList0_Remove(&r->v->refs_list, &r->refs_list_node);
  1090. r->v = NULL;
  1091. }
  1092. static void func_new_common (void *vo, NCDModuleInst *i, struct value *v, value_deinit_func deinit_func, void *deinit_data)
  1093. {
  1094. struct instance *o = vo;
  1095. o->i = i;
  1096. // init value references
  1097. valref_init(&o->ref, v);
  1098. // remember deinit
  1099. o->deinit_func = deinit_func;
  1100. o->deinit_data = deinit_data;
  1101. NCDModuleInst_Backend_Up(i);
  1102. return;
  1103. }
  1104. static void func_die (void *vo)
  1105. {
  1106. struct instance *o = vo;
  1107. // deinit
  1108. if (o->deinit_func) {
  1109. o->deinit_func(o->deinit_data, o->i);
  1110. }
  1111. // free value reference
  1112. valref_free(&o->ref);
  1113. NCDModuleInst_Backend_Dead(o->i);
  1114. }
  1115. static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  1116. {
  1117. struct instance *o = vo;
  1118. struct value *v = valref_val(&o->ref);
  1119. if (name == ModuleString(o->i, STRING_EXISTS)) {
  1120. *out = ncd_make_boolean(mem, !!v, o->i->params->iparams->string_index);
  1121. return 1;
  1122. }
  1123. if (name != NCD_STRING_TYPE && name != NCD_STRING_LENGTH &&
  1124. name != ModuleString(o->i, STRING_KEYS) && name != NCD_STRING_EMPTY) {
  1125. return 0;
  1126. }
  1127. if (!v) {
  1128. ModuleLog(o->i, BLOG_ERROR, "value was deleted");
  1129. return 0;
  1130. }
  1131. if (name == NCD_STRING_TYPE) {
  1132. *out = NCDVal_NewString(mem, get_type_str(v->type));
  1133. }
  1134. else if (name == NCD_STRING_LENGTH) {
  1135. size_t len = 0; // to remove warning
  1136. switch (v->type) {
  1137. case NCDVAL_LIST:
  1138. len = value_list_len(v);
  1139. break;
  1140. case NCDVAL_MAP:
  1141. len = value_map_len(v);
  1142. break;
  1143. default:
  1144. ASSERT(value_is_string(v))
  1145. len = value_string_length(v);
  1146. break;
  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->storedstring.data + 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. };