value.c 62 KB

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