]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/data.h
c350b6574e1191cbc86897f93fb7c884a7b17406
[simgear.git] / simgear / nasal / data.h
1 #ifndef _DATA_H
2 #define _DATA_H
3
4 #include "nasal.h"
5
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
9 // namespaces.
10 enum { T_STR, T_VEC, T_HASH, T_CODE, T_CLOSURE, T_FUNC, T_CCODE,
11        NUM_NASAL_TYPES }; // V. important that this come last!
12
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_CONTAINER(r) (IS_VEC(r)||IS_HASH(r))
25 #define IS_SCALAR(r) (IS_NUM((r)) || IS_STR((r)))
26
27 // This is a macro instead of a separate struct to allow compilers to
28 // avoid padding.  GCC on x86, at least, will always padd the size of
29 // an embedded struct up to 32 bits.  Doing it this way allows the
30 // implementing objects to pack in 16 bits worth of data "for free".
31 #define GC_HEADER \
32     unsigned char mark; \
33     unsigned char type
34
35 struct naObj {
36     GC_HEADER;
37 };
38
39 struct naStr {
40     GC_HEADER;
41     int len;
42     unsigned char* data;
43 };
44
45 struct naVec {
46     GC_HEADER;
47     int size;
48     int alloced;
49     naRef* array;
50 };
51
52 struct HashNode {
53     naRef key;
54     naRef val;
55     struct HashNode* next;
56 };
57
58 struct naHash {
59     GC_HEADER;
60     int size;
61     int lgalloced;
62     struct HashNode* nodes;
63     struct HashNode** table;
64     int nextnode;
65 };
66
67 struct naCode {
68     GC_HEADER;
69     unsigned char* byteCode;
70     int nBytes;
71     naRef* constants;
72     int nConstants;
73     naRef srcFile;
74 };
75
76 struct naFunc {
77     GC_HEADER;
78     naRef code;
79     naRef closure;
80 };
81
82 struct naClosure {
83     GC_HEADER;
84     naRef namespace;
85     naRef next; // parent closure
86 };
87
88 struct naCCode {
89     GC_HEADER;
90     naCFunction fptr;
91 };
92
93 struct naPool {
94     int           type;
95     int           elemsz;
96     int           nblocks;
97     struct Block* blocks;
98     int           nfree;   // number of entries in the free array
99     int           freesz;  // size of the free array
100     void**        free;    // pointers to usable elements
101 };
102
103 void naFree(void* m);
104 void* naAlloc(int n);
105 void naBZero(void* m, int n);
106
107 int naTypeSize(int type);
108 void naGarbageCollect();
109 naRef naObj(int type, struct naObj* o);
110 naRef naNew(naContext c, int type);
111 naRef naNewCode(naContext c);
112 naRef naNewClosure(naContext c, naRef namespace, naRef next);
113
114 int naStr_equal(naRef s1, naRef s2);
115 naRef naStr_fromnum(naRef dest, double num);
116 int naStr_numeric(naRef str);
117 int naStr_parsenum(char* str, int len, double* result);
118 int naStr_tonum(naRef str, double* out);
119
120 void naVec_init(naRef vec);
121
122 int naHash_tryset(naRef hash, naRef key, naRef val); // sets if exists
123 void naHash_init(naRef hash);
124
125 void naGC_init(struct naPool* p, int type);
126 struct naObj* naGC_get(struct naPool* p);
127 int naGC_size(struct naPool* p);
128 void naGC_mark(naRef r);
129 void naGC_reap(struct naPool* p);
130
131 void naStr_gcclean(struct naStr* s);
132 void naVec_gcclean(struct naVec* s);
133 void naHash_gcclean(struct naHash* s);
134
135 #endif // _DATA_H