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 namespace and a pointer to
9 // parent function in the lexical closure.
10 enum { T_STR, T_VEC, T_HASH, T_CODE, 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_OBJ(r) (IS_REF((r)) && (r).ref.ptr.obj != 0 && (((r).ref.ptr.obj->type == 123) ? *(int*)0 : 1))
17 #define IS_NIL(r) (IS_REF((r)) && (r).ref.ptr.obj == 0)
18 #define IS_STR(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_STR)
19 #define IS_VEC(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_VEC)
20 #define IS_HASH(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_HASH)
21 #define IS_CODE(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_CODE)
22 #define IS_FUNC(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_FUNC)
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)))
27 #define IDENTICAL(a, b) (IS_REF(a) && IS_REF(b) \
28 && a.ref.ptr.obj == b.ref.ptr.obj)
30 // This is a macro instead of a separate struct to allow compilers to
31 // avoid padding. GCC on x86, at least, will always padd the size of
32 // an embedded struct up to 32 bits. Doing it this way allows the
33 // implementing objects to pack in 16 bits worth of data "for free".
46 unsigned int hashcode;
63 struct HashNode* next;
70 struct HashNode* nodes;
71 struct HashNode* table[];
82 unsigned char nOptArgs;
83 unsigned char needArgVector;
84 unsigned short nConstants;
85 unsigned short nLines;
86 unsigned short codesz;
87 unsigned short* byteCode;
89 int* argSyms; // indices into constants
92 unsigned short* lineIps; // pairs of {ip, line}
94 naRef restArgSym; // The "..." vector name, defaults to "arg"
101 naRef next; // parent closure
118 struct Block* blocks;
119 void** free0; // pointer to the alloced buffer
120 int freesz; // size of the alloced buffer
121 void** free; // current "free frame"
122 int nfree; // down-counting index within the free frame
123 int freetop; // curr. top of the free list
126 void naFree(void* m);
127 void* naAlloc(int n);
128 void naBZero(void* m, int n);
130 int naTypeSize(int type);
131 naRef naObj(int type, struct naObj* o);
132 naRef naNew(naContext c, int type);
133 naRef naNewCode(naContext c);
135 int naStr_equal(naRef s1, naRef s2);
136 naRef naStr_fromnum(naRef dest, double num);
137 int naStr_numeric(naRef str);
138 int naStr_parsenum(char* str, int len, double* result);
139 int naStr_tonum(naRef str, double* out);
141 int naHash_tryset(naRef hash, naRef key, naRef val); // sets if exists
142 int naHash_sym(struct naHash* h, struct naStr* sym, naRef* out);
143 void naHash_newsym(struct naHash* h, naRef* sym, naRef* val);
145 void naGC_init(struct naPool* p, int type);
146 struct naObj** naGC_get(struct naPool* p, int n, int* nout);
147 void naGC_swapfree(void** target, void* val);
148 void naGC_freedead();
150 void naStr_gcclean(struct naStr* s);
151 void naVec_gcclean(struct naVec* s);
152 void naHash_gcclean(struct naHash* s);