value.c 57 KB

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