7 #define NASAL_BIG_ENDIAN_32_BIT
9 // This is a nasal "reference". They are always copied by value, and
10 // contain either a pointer to a garbage-collectable nasal object
11 // (string, vector, hash) or a floating point number. Keeping the
12 // number here is an optimization to prevent the generation of
13 // zillions of tiny "number" object that have to be collected. Note
14 // sneaky hack: on little endian systems, placing reftag after ptr and
15 // putting 1's in the top 13 (except the sign bit) bits makes the
16 // double value a NaN, and thus unmistakable (no actual number can
17 // appear as a reference, and vice versa). Swap the structure order
18 // on 32 bit big-endian systems. On 64 bit sytems of either
19 // endianness, reftag and the double won't be coincident anyway.
20 #define NASAL_REFTAG 0x7ff56789 // == 2,146,789,257 decimal
24 #ifdef NASAL_BIG_ENDIAN_32_BIT
25 int reftag; // Big-endian systems need this here!
34 struct naClosure* closure;
35 struct naCCode* ccode;
37 #ifndef NASAL_BIG_ENDIAN_32_BIT
38 int reftag; // Little-endian and 64 bit systems need this here!
43 typedef struct Context* naContext;
45 // The function signature for an extension function:
46 typedef naRef (*naCFunction)(naContext ctx, naRef args);
48 // All Nasal code runs under the watch of a naContext:
49 naContext naNewContext();
51 // Save this object in the context, preventing it (and objects
52 // referenced by it) from being garbage collected.
53 void naSave(naContext ctx, naRef obj);
55 // Parse a buffer in memory into a code object.
56 naRef naParseCode(naContext c, naRef srcFile, int firstLine,
57 char* buf, int len, int* errLine);
59 // Binds a bare code object (as returned from naParseCode) with a
60 // closure object (a hash) to act as the outer scope / namespace.
61 // FIXME: this API is weak. It should expose the recursive nature of
62 // closures, and allow for extracting the closure and namespace
63 // information from function objects.
64 naRef naBindFunction(naContext ctx, naRef code, naRef closure);
66 // Call a code or function object with the specifed arguments "on" the
67 // specified object and using the specified hash for the local
68 // variables. Any of args, obj or locals may be nil.
69 naRef naCall(naContext ctx, naRef func, naRef args, naRef obj, naRef locals);
71 // Throw an error from the current call stack. This function makes a
72 // longjmp call to a handler in naCall() and DOES NOT RETURN. It is
73 // intended for use in library code that cannot otherwise report an
74 // error via the return value, and MUST be used carefully. If in
75 // doubt, return naNil() as your error condition.
76 void naRuntimeError(naContext ctx, char* msg);
78 // Call a method on an object (NOTE: func is a function binding, *not*
79 // a code object as returned from naParseCode).
80 naRef naMethod(naContext ctx, naRef func, naRef object);
82 // Returns a hash containing functions from the Nasal standard library
83 // Useful for passing as a namespace to an initial function call
84 naRef naStdLib(naContext c);
86 // Ditto, with math functions
87 naRef naMathLib(naContext c);
89 // Current line number & error message
90 int naStackDepth(naContext ctx);
91 int naGetLine(naContext ctx, int frame);
92 naRef naGetSourceFile(naContext ctx, int frame);
93 char* naGetError(naContext ctx);
98 int naIsString(naRef r);
99 int naIsScalar(naRef r);
100 int naIsVector(naRef r);
101 int naIsHash(naRef r);
102 int naIsCode(naRef r);
103 int naIsFunc(naRef r);
104 int naIsCCode(naRef r);
106 // Allocators/generators:
108 naRef naNum(double num);
109 naRef naNewString(naContext c);
110 naRef naNewVector(naContext c);
111 naRef naNewHash(naContext c);
112 naRef naNewFunc(naContext c, naRef code);
113 naRef naNewCCode(naContext c, naCFunction fptr);
115 // Some useful conversion/comparison routines
116 int naEqual(naRef a, naRef b);
118 naRef naNumValue(naRef n);
119 naRef naStringValue(naContext c, naRef n);
122 int naStr_len(naRef s);
123 char* naStr_data(naRef s);
124 naRef naStr_fromdata(naRef dst, char* data, int len);
125 naRef naStr_concat(naRef dest, naRef s1, naRef s2);
126 naRef naStr_substr(naRef dest, naRef str, int start, int len);
129 int naVec_size(naRef v);
130 naRef naVec_get(naRef v, int i);
131 void naVec_set(naRef vec, int i, naRef o);
132 int naVec_append(naRef vec, naRef o);
133 naRef naVec_removelast(naRef vec);
136 int naHash_size(naRef h);
137 int naHash_get(naRef hash, naRef key, naRef* out);
138 naRef naHash_cget(naRef hash, char* key);
139 void naHash_set(naRef hash, naRef key, naRef val);
140 void naHash_cset(naRef hash, char* key, naRef val);
141 void naHash_delete(naRef hash, naRef key);
142 void naHash_keys(naRef dst, naRef hash);