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