10 # include <machine/endian.h>
11 # elif defined(__CYGWIN__) || defined(__MINGW32__)
12 # include <sys/param.h>
16 # ifndef LITTLE_ENDIAN
17 # define LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
20 # define BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */
23 # if defined(ultrix) || defined(__alpha__) || defined(__alpha) || \
24 defined(__i386__) || defined(__i486__) || defined(_X86_) || \
26 # define BYTE_ORDER LITTLE_ENDIAN
28 # define BYTE_ORDER BIG_ENDIAN
31 #endif /* BYTE_ORDER */
33 #if BYTE_ORDER == BIG_ENDIAN
35 # if (LONG_MAX == 2147483647)
36 # define NASAL_BIG_ENDIAN_32_BIT 1
40 // This is a nasal "reference". They are always copied by value, and
41 // contain either a pointer to a garbage-collectable nasal object
42 // (string, vector, hash) or a floating point number. Keeping the
43 // number here is an optimization to prevent the generation of
44 // zillions of tiny "number" object that have to be collected. Note
45 // sneaky hack: on little endian systems, placing reftag after ptr and
46 // putting 1's in the top 13 (except the sign bit) bits makes the
47 // double value a NaN, and thus unmistakable (no actual number can
48 // appear as a reference, and vice versa). Swap the structure order
49 // on 32 bit big-endian systems. On 64 bit sytems of either
50 // endianness, reftag and the double won't be coincident anyway.
51 #define NASAL_REFTAG 0x7ff56789 // == 2,146,789,257 decimal
55 #ifdef NASAL_BIG_ENDIAN_32_BIT
56 int reftag; // Big-endian systems need this here!
65 struct naClosure* closure;
66 struct naCCode* ccode;
67 struct naGhost* ghost;
69 #ifndef NASAL_BIG_ENDIAN_32_BIT
70 int reftag; // Little-endian and 64 bit systems need this here!
75 typedef struct Context* naContext;
77 // The function signature for an extension function:
78 typedef naRef (*naCFunction)(naContext ctx, naRef args);
80 // All Nasal code runs under the watch of a naContext:
81 naContext naNewContext();
83 // Save this object in the context, preventing it (and objects
84 // referenced by it) from being garbage collected.
85 void naSave(naContext ctx, naRef obj);
87 // Parse a buffer in memory into a code object.
88 naRef naParseCode(naContext c, naRef srcFile, int firstLine,
89 char* buf, int len, int* errLine);
91 // Binds a bare code object (as returned from naParseCode) with a
92 // closure object (a hash) to act as the outer scope / namespace.
93 // FIXME: this API is weak. It should expose the recursive nature of
94 // closures, and allow for extracting the closure and namespace
95 // information from function objects.
96 naRef naBindFunction(naContext ctx, naRef code, naRef closure);
98 // Call a code or function object with the specifed arguments "on" the
99 // specified object and using the specified hash for the local
100 // variables. Any of args, obj or locals may be nil.
101 naRef naCall(naContext ctx, naRef func, naRef args, naRef obj, naRef locals);
103 // Throw an error from the current call stack. This function makes a
104 // longjmp call to a handler in naCall() and DOES NOT RETURN. It is
105 // intended for use in library code that cannot otherwise report an
106 // error via the return value, and MUST be used carefully. If in
107 // doubt, return naNil() as your error condition.
108 void naRuntimeError(naContext ctx, char* msg);
110 // Call a method on an object (NOTE: func is a function binding, *not*
111 // a code object as returned from naParseCode).
112 naRef naMethod(naContext ctx, naRef func, naRef object);
114 // Returns a hash containing functions from the Nasal standard library
115 // Useful for passing as a namespace to an initial function call
116 naRef naStdLib(naContext c);
118 // Ditto, with math functions
119 naRef naMathLib(naContext c);
121 // Current line number & error message
122 int naStackDepth(naContext ctx);
123 int naGetLine(naContext ctx, int frame);
124 naRef naGetSourceFile(naContext ctx, int frame);
125 char* naGetError(naContext ctx);
128 int naIsNil(naRef r);
129 int naIsNum(naRef r);
130 int naIsString(naRef r);
131 int naIsScalar(naRef r);
132 int naIsVector(naRef r);
133 int naIsHash(naRef r);
134 int naIsCode(naRef r);
135 int naIsFunc(naRef r);
136 int naIsCCode(naRef r);
138 // Allocators/generators:
140 naRef naNum(double num);
141 naRef naNewString(naContext c);
142 naRef naNewVector(naContext c);
143 naRef naNewHash(naContext c);
144 naRef naNewFunc(naContext c, naRef code);
145 naRef naNewCCode(naContext c, naCFunction fptr);
147 // Some useful conversion/comparison routines
148 int naEqual(naRef a, naRef b);
150 naRef naNumValue(naRef n);
151 naRef naStringValue(naContext c, naRef n);
154 int naStr_len(naRef s);
155 char* naStr_data(naRef s);
156 naRef naStr_fromdata(naRef dst, char* data, int len);
157 naRef naStr_concat(naRef dest, naRef s1, naRef s2);
158 naRef naStr_substr(naRef dest, naRef str, int start, int len);
161 int naVec_size(naRef v);
162 naRef naVec_get(naRef v, int i);
163 void naVec_set(naRef vec, int i, naRef o);
164 int naVec_append(naRef vec, naRef o);
165 naRef naVec_removelast(naRef vec);
166 void naVec_setsize(naRef vec, int sz);
169 int naHash_size(naRef h);
170 int naHash_get(naRef hash, naRef key, naRef* out);
171 naRef naHash_cget(naRef hash, char* key);
172 void naHash_set(naRef hash, naRef key, naRef val);
173 void naHash_cset(naRef hash, char* key, naRef val);
174 void naHash_delete(naRef hash, naRef key);
175 void naHash_keys(naRef dst, naRef hash);
178 typedef struct naGhostType {
179 void (*destroy)(void* ghost);
181 naRef naNewGhost(naContext c, naGhostType* t, void* ghost);
182 naGhostType* naGhost_type(naRef ghost);
183 void* naGhost_ptr(naRef ghost);
184 int naIsGhost(naRef r);