value.c 62 KB

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