6 // Notes: A CODE object is a compiled set of bytecode instructions.
7 // What actually gets executed at runtime is a bound FUNC object,
8 // which combines the raw code with a pointer to a CLOSURE chain of
10 enum { T_STR, T_VEC, T_HASH, T_CODE, T_CLOSURE, T_FUNC, T_CCODE, T_GHOST,
11 NUM_NASAL_TYPES }; // V. important that this come last!
13 #define IS_REF(r) ((r).ref.reftag == NASAL_REFTAG)
14 #define IS_NUM(r) ((r).ref.reftag != NASAL_REFTAG)
15 #define IS_OBJ(r) (IS_REF((r)) && (r).ref.ptr.obj != 0)
16 #define IS_NIL(r) (IS_REF((r)) && (r).ref.ptr.obj == 0)
17 #define IS_STR(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_STR)
18 #define IS_VEC(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_VEC)
19 #define IS_HASH(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_HASH)
20 #define IS_CODE(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_CODE)
21 #define IS_FUNC(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_FUNC)
22 #define IS_CLOSURE(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_CLOSURE)
23 #define IS_CCODE(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_CCODE)
24 #define IS_GHOST(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_GHOST)
25 #define IS_CONTAINER(r) (IS_VEC(r)||IS_HASH(r))
26 #define IS_SCALAR(r) (IS_NUM((r)) || IS_STR((r)))
28 // This is a macro instead of a separate struct to allow compilers to
29 // avoid padding. GCC on x86, at least, will always padd the size of
30 // an embedded struct up to 32 bits. Doing it this way allows the
31 // implementing objects to pack in 16 bits worth of data "for free".
56 struct HashNode* next;
63 struct HashNode* nodes;
64 struct HashNode** table;
70 unsigned char* byteCode;
86 naRef next; // parent closure
104 struct Block* blocks;
105 int nfree; // number of entries in the free array
106 int freesz; // size of the free array
107 void** free; // pointers to usable elements
110 void naFree(void* m);
111 void* naAlloc(int n);
112 void naBZero(void* m, int n);
114 int naTypeSize(int type);
115 void naGarbageCollect();
116 naRef naObj(int type, struct naObj* o);
117 naRef naNew(naContext c, int type);
118 naRef naNewCode(naContext c);
119 naRef naNewClosure(naContext c, naRef namespace, naRef next);
121 int naStr_equal(naRef s1, naRef s2);
122 naRef naStr_fromnum(naRef dest, double num);
123 int naStr_numeric(naRef str);
124 int naStr_parsenum(char* str, int len, double* result);
125 int naStr_tonum(naRef str, double* out);
127 void naVec_init(naRef vec);
129 int naHash_tryset(naRef hash, naRef key, naRef val); // sets if exists
130 void naHash_init(naRef hash);
132 void naGC_init(struct naPool* p, int type);
133 struct naObj* naGC_get(struct naPool* p);
134 int naGC_size(struct naPool* p);
135 void naGC_mark(naRef r);
136 void naGC_reap(struct naPool* p);
138 void naStr_gcclean(struct naStr* s);
139 void naVec_gcclean(struct naVec* s);
140 void naHash_gcclean(struct naHash* s);