ma_val.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /*
  2. * $$$Definition Maat Values and Objects
  3. * License: AGL, see LICENSE file for details.
  4. */
  5. #ifndef ma_val_h
  6. #define ma_val_h
  7. #include "ma_conf.h"
  8. #include "ma_limits.h"
  9. /* Vary type 't' with variant bits 'v'. */
  10. #define vary(t, vb) ((t) | (vb << 5))
  11. /* 'x' can either be a 'Value' or a collectable 'Object'. */
  12. #define type(x) raw_type(x)
  13. #define raw_type(x) ((x)->type)
  14. #define without_variant(x) (raw_type(x) & 0x1F)
  15. #define check_type(x, t) (without_variant(x) == t)
  16. #define check_rtype(x, t) (raw_type(x) == t)
  17. #define with_variant(v) (raw_type(v) & 0x7F)
  18. #define check_vartype(v, t) (with_variant(v) == t)
  19. /* $$Representation of a Maat value.
  20. *
  21. * $val: The value itself, which is a union of
  22. * $n: Maat's representation of a number (see 'ma_conf.h').
  23. * $gc_obj: Pointer to a collectable object.
  24. * $p: Pointer to a memory location representing the data of a
  25. * Cclass.
  26. * $f: Pointer to a C function, it either represents a cdata's
  27. * method or just a standalone function.
  28. *
  29. * $type: Determines $val's type, its bits are segmented into
  30. * three parts. This variable already gots all what is
  31. * necessary to represent values of the boolean and nil types.
  32. *
  33. * Bits distribution:
  34. * - Bits 0-4:
  35. * Represents the different types of types regardless of
  36. * whether they are collectable or not, here we have maximum
  37. * 32 objects which suffices.
  38. *
  39. * - Bits 5-6:
  40. * Represents variants of certain (O_)?.* types. You can have
  41. * at most 3 variants for each base type which also suffices.
  42. *
  43. * - Bit 7: is 1 if $val stores a collectable object and 0
  44. * otherwise.
  45. */
  46. typedef union _Value {
  47. Num n;
  48. CFunc f;
  49. void *p;
  50. struct Object *gc_obj;
  51. } _Value;
  52. #define Valuefields Ubyte type; _Value val
  53. typedef struct Value {
  54. Valuefields;
  55. } Value;
  56. #define val(v) ((v)->val)
  57. #define set_type(v, t) (type(v) = t)
  58. #define gco2val(v, o) set_type(v, ctb(o->type)); \
  59. (val(v).obj = x2gco(o))
  60. /*
  61. * Defines types of all non-collectable objects. A value 'v' is
  62. * collectable if the MSB of its $type is 1 meaning it stores
  63. * a collectable object.
  64. */
  65. #define V_NIL 0
  66. #define V_BOOL 1
  67. #define V_NUM 2
  68. #define V_CFUNC 3
  69. #define V_CDATA 4
  70. #define is_num(v) check_type(v, V_NUM)
  71. #define is_nil(v) check_type(v, V_NIL)
  72. #define is_bool(v) check_type(v, V_BOOL)
  73. #define is_cfunc(v) check_type(v, V_CFUNC)
  74. #define is_cdata(v) check_type(v, V_CDATA)
  75. #define IS_CTB_BIT (0b1 << 7)
  76. /* Check if value 'v' is collectable, set collectable bit. */
  77. #define is_ctb(v) (type(v) & IS_CTB_BIT)
  78. #define ctb(v) (type(v) | IS_CTB_BIT)
  79. #define as_bool(v) (ma_assert(is_bool(v)), (is_true(v)))
  80. #define as_num(v) (ma_assert(is_num(v)), (val(v).n))
  81. #define as_cfunc(v) (ma_assert(is_cfunc(v)), (val(v).f))
  82. #define as_cdata(v) (ma_assert(is_cdata(v)), (val(v).p))
  83. /*
  84. * 'as_gcobj()' for collectable objects defined below. Things
  85. * dealing with collectable objects are preceeded with '[oO]_'.
  86. */
  87. #define as_gcobj(v) (ma_assert(is_ctb(v)), (val(v).gc_obj))
  88. /*
  89. * Define variants of the 'nil' type and its singleton values.
  90. *
  91. * V_VFREE: This nil variant differentiates a key whose
  92. * corresponding value is a user-perceived 'nil' from a free
  93. * position in a map.
  94. *
  95. * V_VABSKEY: When indexing a hash map with a key that isn't
  96. * found, return a dummy value of type V_VABSKEY.
  97. */
  98. #define V_VNIL vary(V_NIL, 0)
  99. #define V_VFREE vary(V_NIL, 1)
  100. #define V_VABSKEY vary(V_NIL, 2)
  101. #define FREE (Value){ V_VFREE, { 0 } }
  102. #define ABSKEY (Value){ V_VABSKEY, { 0 } }
  103. #define is_nil(v) check_type(v, V_NIL)
  104. #define iss_nil(v) check_rtype(v, V_VNIL)
  105. #define is_free(v) check_rtype(v, V_VNILKEY)
  106. #define is_abskey(v) check_rtype(v, V_VABSKEY)
  107. /* Define the boolean type variants with its singleton values. */
  108. #define V_VFALSE vary(V_BOOL, 0)
  109. #define V_VTRUE vary(V_BOOL, 1)
  110. #define FALSE (Value){ V_VFALSE, { 0 } }
  111. #define TRUE (Value){ V_VTRUE, { 0 } }
  112. #define is_false(v) check_rtype(to_bool(v), V_VFALSE)
  113. #define is_true(v) check_rtype(to_bool(v), V_VTRUE)
  114. #define to_bool(v) (ma_likely(is_bool(v)) ? v : coerce_to_bool(v))
  115. /* Header common to all collectable Maat objects */
  116. #define Header Ubyte type; \
  117. Ubyte mark; \
  118. struct Class *next; \
  119. struct Object *next
  120. /*
  121. * $$Object struct inherited by all the below objects.
  122. *
  123. * $type: Type of the object.
  124. * $mark: Flag to mark the object during collection.
  125. * $class: The object's class.
  126. * $next: Next obj, to keep track of all objects.
  127. */
  128. typedef struct Object {
  129. Header;
  130. } Object;
  131. /* Here are base colletable objects, some do have variants */
  132. /* O_VCLASS, O_VCCLASS, O_VROLE */
  133. #define O_CLASS 5
  134. /* O_VINST O_VCINST */
  135. #define O_INST 6
  136. /* O_VSHTSTR, O_VLNGSTR */
  137. #define O_STR 7
  138. /* O_VULNGSTR, O_VUSHTSTR */
  139. #define O_U8STR 8
  140. /* O_VRANGE, O_VSRANGE? */
  141. #define O_RANGE 9
  142. /* O_VARRAY, O_VLIST */
  143. #define O_ARRAY 10
  144. /* O_VMAP, O_VNSMAP */
  145. #define O_MAP 11
  146. /* O_VCHAN, O_VSCHEDQ */
  147. #define O_RBQ 13
  148. /* O_VFUN, O_VCLOSURE */
  149. #define O_FUN 14
  150. /* O_VUPVAL */
  151. #define O_UPVAL 15
  152. /* O_VSTATE, O_VCO */
  153. #define O_STATE 16
  154. /* O_VMA */
  155. #define O_MA 17
  156. /* O_VWORK */
  157. #define O_WORK 18
  158. /* O_VNS */
  159. #define O_NS 19
  160. /* A thread-safe channel and scheduler queue */
  161. #define O_VCHAN vary(O_RBQ, 1)
  162. #define O_VSCHEDQ vary(O_RBQ, 2)
  163. #define is_chan(v) check_type(v, O_VCHAN)
  164. #define is_schedq(v) check_type(v, O_VSCHEDQ)
  165. /* $$Representation of all string objects. */
  166. #define O_VLNGSTR vary(O_STR, 0)
  167. #define O_VSHTSTR vary(O_STR, 1)
  168. #define O_VUSHTSTR vary(O_U8STR, 0)
  169. #define O_VULNGSTR vary(O_U8STR, 1)
  170. #define is_str(v) (is_astr(v) || is_ustr(v))
  171. #define is_astr(v) check_type(v, O_STR)
  172. #define is_ustr(v) check_type(v, O_U8STR)
  173. #define is_shtstr(v) (check_rtype(v, ctb(O_VSHTSTR)) || \
  174. check_rtype(v, ctb(O_VUSHTSTR)))
  175. #define is_lngstr(v) (check_rtype(v, ctb(O_VLNGSTR)) || \
  176. check_rtype(v, ctb(O_VULNGSTR)))
  177. #define as_str(v) (ma_assert(is_str(v)), cast(Str *, as_gcobj(v)))
  178. #define as_u8str(v) (ma_assert(is_u8str(v)), cast(U8Str *, as_gcobj(v)))
  179. #define Str_Header Object *next_sobj; \
  180. Ubyte sl; \
  181. Uint hash; \
  182. Byte *str; \
  183. union { size_t len; struct Str *snext; } u;
  184. /*
  185. * Ascii version of a Maat string, $str is the ascii string
  186. * itself and has hash value $hash.
  187. *
  188. * $sl: For short strings, last 3 bits of $sl is for the length
  189. * of $str whereas its first bit determines whether $str is
  190. * reserved. For long strings, $sl serves as a boolean value to
  191. * check if $str already has its $hash.
  192. * $u: For short strings, $u is $snext which is a pointer to
  193. * $str's next string in our hash map of short strings ($$SMap)
  194. * meanwhile for long strings, $u is the $len of $str.
  195. */
  196. typedef struct Str {
  197. Header;
  198. Str_Header;
  199. } Str;
  200. /*
  201. * UTF-8 encoded string object, has $Str_Header plus $ngraph.
  202. * $ngraph is the total size of the subparts of the grapheme
  203. * clusters of $str.
  204. */
  205. typedef struct U8Str {
  206. Header;
  207. Str_Header;
  208. size_t ngraph;
  209. } U8Str;
  210. /*
  211. * $$Range object with each bound inclusive.
  212. *
  213. * $a the start.
  214. * $b the end.
  215. * $c the step.
  216. */
  217. #define O_VRANGE vary(O_RANGE, 0)
  218. #define is_range(v) check_rtype(v, ctb(O_VRANGE))
  219. #define as_range(v) (ma_assert(is_range(v)), cast(Range *, as_gcobj(v)))
  220. #define range2v(v, r) gco2val(v, r)
  221. typedef struct Range {
  222. Header;
  223. Num a;
  224. Num b;
  225. Int c;
  226. } Range;
  227. /* $$Representation of Map objects. */
  228. #define O_VMAP vary(O_MAP, 0)
  229. #define O_VNSMAP vary(O_MAP, 1)
  230. #define is_map(v) check_rtype(v, ctb(O_VMAP))
  231. #define is_nsmap(v) check_rtype(v, ctb(O_VNSMAP))
  232. #define as_map(v) (ma_assert(is_map(v)), cast(Map *, as_gcobj(v)))
  233. #define as_nsmap(v) as_map(v)
  234. /* */
  235. #define Nodefields Ubyte key_t; \
  236. Int next; \
  237. _Value key_v
  238. /*
  239. * $$Node of an `O_VMAP' map object.
  240. *
  241. * $val:
  242. * Direct access to the Node's value, $val corresponds to
  243. * $Valuefields since Node is a union.
  244. * $k: Node's key.
  245. * $key_t: key's type.
  246. * $key_v: Key's value.
  247. * $next: Next node in case of collisions.
  248. */
  249. typedef union Node {
  250. struct {
  251. Valuefields;
  252. Nodefields;
  253. } k;
  254. Value val;
  255. } Node;
  256. /*
  257. * $$SNode of an `O_VNSMAP' map object, same as $$Node but has
  258. * mutex $m to manage concurrent access to entries. `O_VNSMAP'
  259. * maps are used by Namespaces to store their global symbols.
  260. */
  261. typedef union SNode {
  262. struct {
  263. Valuefields;
  264. Nodefields;
  265. Mutex m;
  266. } k;
  267. Value val;
  268. } SNode;
  269. /*
  270. * The Map object. Our map has two parts: an array and a hash
  271. * part, its array part is used in an optimized way to store
  272. * nodes with non-negative integer keys. This array is used in
  273. * such a way that more than half of its slots are in use.
  274. * Its hash part stores the rest of the nodes obtained after
  275. * we've got our optimized $array.
  276. *
  277. * $array: Array part of the Map.
  278. * $asize: Size of the array part of the Map.
  279. * $rasize:
  280. * Boolean value to check if $array's size $asize is actually
  281. * its real size.
  282. * $node: Hash part of this Map, points to a Node/SNode.
  283. * $lnode: The last free node in $node.
  284. * $lg2size: log2 of the size of #node.
  285. */
  286. typedef struct Map {
  287. Header;
  288. Ubyte rasize;
  289. Ubyte lg2size;
  290. Uint asize;
  291. Value *array;
  292. void *node;
  293. void *lnode;
  294. Object *next_sobj;
  295. } Map;
  296. /*
  297. * $$Representation of an Array object. Though we got a Map
  298. * optimized to be kind-of an array for fast access, Maat still
  299. * got a true Array object.
  300. *
  301. * $array: The array itself.
  302. * $size: The size of $array.
  303. * $cap: The capacity of $array.
  304. */
  305. /* Variant: list object: comma-separated list of values */
  306. #define O_VARRAY vary(O_ARRAY, 0)
  307. #define O_VLIST vary(O_ARRAY, 1)
  308. #define is_array(v) check_type(v, O_ARRAY)
  309. #define iss_array(v) check_rtype(v, ctb(O_VARRAY))
  310. #define is_list(v) check_rtype(v, ctb(O_VLIST))
  311. #define as_array(v) (ma_assert(is_array(v)), cast(Array *, as_gcobj(v)))
  312. #define as_list(v) as_array(v)
  313. typedef struct Array {
  314. Header;
  315. size_t size;
  316. size_t cap;
  317. Value *array;
  318. Object *next_sobj;
  319. } Array;
  320. /*
  321. * $$Representation of a class, every collectable object has a
  322. * class, even the class object itself. The Class of objects
  323. * which aren't first class values are probably useless, we
  324. * have for example Upvalues.
  325. *
  326. * $name: The class' name.
  327. * $meths:
  328. * Pointer to a map for the class' methods. A value to a key
  329. * here is a pointer to a Closure but can later on change to
  330. * an Array of closures so as to cache super methods, the
  331. * cache can never be invalidate since c3 linearization is
  332. * done at compile time.
  333. *
  334. * A C class is a class whose implemention is done in foreign
  335. * languages like C or C++.
  336. *
  337. * variants, here the object Class defines 3 variants which are
  338. * Class, Role and Cclasse.
  339. *
  340. * For a Maat class and a Role we have the following fields:
  341. * $c3: List of classes got after c3 linearization was applied.
  342. * $fields:
  343. * Pointer to an array of values, indexes correspond to
  344. * fields with values holding their defaults.
  345. * $roles: Keeps the list of roles that this class ":does".
  346. * $sups:
  347. * Keeps the list of directly inherited superclasses. $sups
  348. * exist mainly for introspection since $c3 handles super
  349. * calls.
  350. */
  351. #define O_VCLASS vary(O_CLASS, 0)
  352. #define O_VROLE vary(O_CLASS, 1)
  353. #define is_class(v) check_type(v, O_CLASS)
  354. #define iss_class(v) check_rtype(v, ctb(O_VCLASS))
  355. #define is_role(v) check_rtype(v, ctb(O_VROLE))
  356. #define as_class(v) (ma_assert(is_class(v)), cast(Class *, as_gcobj(v)))
  357. #define as_role(v) as_class(v)
  358. typedef struct Class {
  359. Header;
  360. Str *name;
  361. Value *fields;
  362. Map *meths;
  363. struct Class *roles;
  364. struct Class *sups;
  365. struct Class *c3;
  366. Object *next_sobj;
  367. } Class;
  368. /* $$A C class.
  369. *
  370. * $cdata: A pointer to a raw memory.
  371. * $size: The size of the memory pointed by $cdata.
  372. */
  373. #define O_VCCLASS vary(O_CLASS, 2)
  374. #define is_cclass(v) check_rtype(v, ctb(O_VCCLASS))
  375. #define as_cclass(v) (ma_assert(is_cclass(v)), cast(Cclass *, as_gcobj(v)))
  376. typedef struct Cclass {
  377. Header;
  378. Str *name;
  379. size_t size;
  380. Map *meths;
  381. } Cclass;
  382. /* $$Instance of a c?class. */
  383. #define is_inst(v) check_type(v, O_INST)
  384. #define O_VMINST vary(O_INST, 0)
  385. #define is_minst(v) check_rtype(v, ctb(O_VMINST))
  386. #define as_minst(v) (ma_assert(is_minst(v)), cast(MInst *, as_gcobj(v)))
  387. /*
  388. * $$Instance of a Maat class.
  389. *
  390. * $fields:
  391. * A pointer to a to-be-allocated array of values, each field
  392. * has a unique id which corresponds to an index in $fields.
  393. * This also holds inherited fields too.
  394. * $sup_level:
  395. * Keeps track of the level of super calls for each method
  396. * called on the instance, in otherwords it is a list of
  397. * indexes to super methods cached at the level of this
  398. * instance's class, it is set 'NULL' if none of the methods
  399. * of this instance's class does a super call.
  400. *
  401. * "self.SUPER::<method_name>(...)" Where SUPER is a pseudo
  402. * class that resolves to the next class in the instance's class
  403. * c3 list.
  404. */
  405. typedef struct MInst {
  406. Header;
  407. Value *fields;
  408. Map *sup_level;
  409. Object *next_sobj;
  410. } MInstance;
  411. /*
  412. * $$Instance of a Cclass.
  413. *
  414. * $cdata: Cclass' data.
  415. */
  416. #define O_VCINST vary(O_INST, 1)
  417. #define is_cinst(v) check_rtype(v, ctb(O_VMINST))
  418. #define as_cinst(v) (ma_assert(is_cinst(v)), cast(CInst *, as_gcobj(v)))
  419. typedef struct CInst {
  420. Header;
  421. void *cdata;
  422. } CInstance;
  423. /*
  424. * $$Represents a namespace e.g FOO::BAR. A namespace can either
  425. * be represented as a package, role or (c)class.
  426. * "FOO::BAR::x()" is a call to the function "x" in "FOO::BAR"
  427. * if ever there is.
  428. *
  429. * $name: Namespace's name.
  430. * $ours: Stores it globals (uses mutex when required)
  431. *
  432. * $ours of the package "main::" takes care of the following
  433. * type I & II special variables:
  434. *
  435. * ENV, ARGC, ARGV, INC, PATH, SIG, DATA, $v, $o, $,, $/, $\
  436. * $|, $", $$, $(, $), $<, $>, $f, and $0.
  437. *
  438. * Access to one these variables from another package most not
  439. * necessarily be done using its fully qualified form unless a
  440. * variables of the same name declared in a scope shields it.
  441. *
  442. * $exports: Names of to-be-exported globals when this namespace
  443. * is used by another.
  444. *
  445. * NB:
  446. * - The argument to the "use" statement never translates to a
  447. * namespace.
  448. * - Not all classes or roles are namespaces, some can be
  449. * lexically scoped and thus can never be fully qualified.
  450. * - For consistency, you cannot nest namespaces as they don't
  451. * really have an identity.
  452. *
  453. * $ns_val: The namespace value, a package, class, or role.
  454. */
  455. #define O_VNS vary(O_NS, 0)
  456. #define is_ns(v) check_rtype(v, ctb(O_VNS))
  457. #define as_ns(v) (ma_assert(is_ns(v)), cast(Namespace *, as_gcobj(v)))
  458. typedef struct Namespace {
  459. Header;
  460. Str *name;
  461. Map *ours;
  462. ExportBuf export;
  463. Value ns_val;
  464. } Namespace;
  465. /*
  466. * $$Struct of a Maat function.
  467. *
  468. * $arity: The number of arguments the function takes.
  469. * $code: Its bytecode.
  470. * $constants: The function's constant values.
  471. * $ns:
  472. * Index of the $NSBuf to access the namespace of this
  473. * function.
  474. */
  475. #define O_VFUN vary(O_FUN, 0)
  476. #define is_fun(v) check_rtype(v, ctb(O_VFUN))
  477. #define as_fun(v) (ma_assert(is_fun(v)), cast(Fun *, as_gcobj(v)))
  478. typedef struct Fun {
  479. Header;
  480. Ubyte arity;
  481. size_t ns;
  482. CodeBuf code;
  483. ValueBuf constants;
  484. } Fun;
  485. /*
  486. * $$A function that keeps track of its upvalues is called a
  487. * closure, upvalues initially are lexically scoped variables
  488. * living in vm's registers, they were supposed to stop existing
  489. * when the program goes out of their scopes of definition but
  490. * since some functions need them, these lexicals are kept as
  491. * upvalues.
  492. *
  493. * $p:
  494. * Pointer to upvalue, when an upvalue is opened, it points
  495. * to a value in a vm register but when closed, it points to
  496. * $state.
  497. * $state:
  498. * A pointer to the $next open upvalue when this one is still
  499. * opened otherwise it'll contain the vm register value $p
  500. * pointed to.
  501. * $m_uv:
  502. * Whenever an upvalue is shared by closures of States
  503. * distributed across maatines, a mutex must be used to manage
  504. * it concurrent access. This field is optional as this
  505. * condition can only be determined at runtime.
  506. */
  507. #define O_VUPVAL vary(O_UPVAL, 0)
  508. /* Upvals aren't first class values. */
  509. typedef struct Upval {
  510. Header;
  511. Value *p;
  512. union {
  513. Value val;
  514. struct Upval *next;
  515. } state;
  516. Mutex m_uv;
  517. } Upval;
  518. /* $$A closure is a variant of a function which keep tracks of
  519. * its upvalues.
  520. *
  521. * $fun: A pointer to the closure's function.
  522. * $nupvals: The number of upvalues.
  523. * $upvals: The List of upvalues the function has.
  524. */
  525. #define O_VCLOSURE vary(O_FUN, 1)
  526. #define is_closure(v) check_rtype(v, ctb(O_VCLOSURE))
  527. #define as_closure(v) (ma_assert(is_closure(v)), cast(Closure *, as_gcobj(v)))
  528. typedef struct Closure {
  529. Header;
  530. Object *next_sobj;
  531. Fun *fun;
  532. Ubyte nupvals;
  533. Upval **upvals;
  534. } Closure;
  535. /* $$The definition of these objects are in 'ma_state.h' */
  536. /* $$Macros of the State object. */
  537. #define O_VSTATE vary(O_STATE, 0)
  538. #define O_VCO vary(O_STATE, 1)
  539. #define is_state(v) check_type(v, O_STATE)
  540. #define iss_state(v) check_rtype(v, ctb(O_VSTATE))
  541. #define is_co(c) check_rtype(v, ctb(O_VCO))
  542. #define as_state(v) (ma_assert(is_state(v)), cast(State *, as_gcobj(v)))
  543. #define as_co(v) as_state(v)
  544. /* $$Macros of the Work object. */
  545. #define O_VWORK vary(O_WORK, 0)
  546. #define is_work(v) check_rtype(v, ctb(O_VWORK))
  547. #define as_work(v) (ma_assert(is_state(v)), cast(Work *, as_gcobj(v)))
  548. /* $$Macros of the Maatine object. */
  549. #define O_VMA vary(O_MA, 0)
  550. #define is_ma(v) check_rtype(v, ctb(O_VMA))
  551. #define as_ma(v) (ma_assert(is_ma(v)), cast(Ma *, as_gcobj(v)))
  552. /* $$Union of all collectable objects used for conversion. */
  553. /* Cast 'o' to a pointer to an Ounion struct. */
  554. #define ounion_of(o) cast(Ounion *, o)
  555. /*
  556. * ISO C99 says that a pointer to a union object, suitably
  557. * converted, points to each of its members, and vice versa.
  558. */
  559. #define gco2str(o) (ma_assert(check_type(o, O_STR) || check_type(v, O_U8STR)), &(ounion_of(o)->str))
  560. #define gco2ar(o) (ma_assert(check_type(o, O_ARRAY)), &(ounion_of(o)->ar))
  561. #define gco2map(o) (ma_assert(check_type(o, O_MAP)), &(ounion_of(o)->map))
  562. #define gco2rng(o) (ma_assert(check_type(o, O_RANGE)), &(ounion_of(o)->rng))
  563. #define gco2fun(o) (ma_assert(check_rtype(o, O_VFUN)), &(ounion_of(o)->fun))
  564. #define gco2clo(o) (ma_assert(check_rtype(o, O_VCLOSURE)), &(ounion_of(o)->clo))
  565. #define gco2uv(o) (ma_assert(check_type(o, O_UPVAL)), &(ounion_of(o)->uv))
  566. #define gco2cls(o) (ma_assert(check_rtype(o, O_VCLASS) || check_rtype(o, O_VROLE)), &(ounion_of(o)->cls))
  567. #define gco2ccls(o) (ma_assert(check_rtype(o, O_VCCLASS)), &(ounion_of(o)->ccls))
  568. #define gco2ins(o) (ma_assert(check_type(o, O_VINSTANCE)), &(ounion_of(o)->ins))
  569. #define gco2stt(o) (ma_assert(check_type(o, O_STATE)), &(ounion_of(o)->stt))
  570. #define gco2ma(o) (ma_assert(check_type(o, O_MA)), &(ounion_of(o)->ma))
  571. #define gco2wk(o) (ma_assert(check_rtype(o, O_VWORK)), &(ounion_of(o)->wk))
  572. #define gco2rbq(o) (ma_assert(check_rtype(o, O_VRBQ)), &(ounion_of(o)->rbq))
  573. #define gco2ns(o) (ma_assert(check_rtype(o, O_VNS)), &(ounion_of(o)->ns))
  574. /* The other way around. */
  575. #define x2gco(v) (ma_assert(is_obj(v)), &(ounion_of(o)->gc_obj))
  576. /* Union of all collectable objects */
  577. union Ounion {
  578. Object gc_obj;
  579. Str str;
  580. Array ar;
  581. Map map;
  582. Range rng;
  583. Fun fun;
  584. Closure clo;
  585. Upval uv;
  586. Class cls;
  587. Cclass ccls;
  588. Instance ins;
  589. State stt;
  590. Ma ma;
  591. Work wk;
  592. Rbq rbq;
  593. Namespace ns;
  594. } Ounion;
  595. /* It is on its own as this is needed at 'ma_str.c' */
  596. #define sunion_of(s) cast(Sunion *, s)
  597. #define a2u8(s) (ma_assert(check_type(s, O_STR)), &(sunion_of(s)->as))
  598. #define u82a(s) (ma_assert(check_type(s, O_U8STR)), &(sunion_of(s)->u8s))
  599. union Sunion {
  600. Str as;
  601. U8Str u8s;
  602. } Sunion;
  603. #endif