value.c 58 KB

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