10 /* This marks the function as having no side effects and depending on
11 * nothing but its arguments, which allows the optimizer to avoid
12 * duplicate calls to naNil(). */
13 #define GCC_PURE __attribute__((__pure__))
18 typedef struct Context* naContext;
20 // The function signature for an extension function:
21 typedef naRef (*naCFunction)(naContext ctx, naRef me, int argc, naRef* args);
23 // The function signature for an extension function with userdata passed back:
24 typedef naRef (*naCFunctionU)
25 (naContext ctx, naRef me, int argc, naRef* args, void* user_data);
27 // All Nasal code runs under the watch of a naContext:
28 naContext naNewContext();
29 void naFreeContext(naContext c);
31 // Use this when making a call to a new context "underneath" a
32 // preexisting context on the same stack. It allows stack walking to
33 // see through the boundary, and eliminates the need to release the
34 // mod lock (i.e. must be called with the mod lock held!)
35 naContext naSubContext(naContext super);
37 // The naContext supports a user data pointer that can be used to
38 // store data specific to an naCall invocation without exposing it to
39 // Nasal as a ghost. FIXME: this API is semi-dangerous, there is no
40 // provision for sharing it, nor for validating the source or type of
41 // the pointer returned.
42 void naSetUserData(naContext c, void* p);
43 void* naGetUserData(naContext c) GCC_PURE;
45 // "Save" this object in the context, preventing it (and objects
46 // referenced by it) from being garbage collected.
47 void naSave(naContext ctx, naRef obj);
49 // Similar, but the object is automatically released when the
50 // context next runs native bytecode. Useful for saving off C-space
51 // temporaries to protect them before passing back into a naCall.
52 void naTempSave(naContext c, naRef r);
54 // Parse a buffer in memory into a code object. The srcFile parameter
55 // is a Nasal string representing the "file" from which the code is
56 // read. The "first line" is typically 1, but is settable for
57 // situations where the Nasal code is embedded in another context with
58 // its own numbering convetions. If an error occurs, returns nil and
59 // sets the errLine pointer to point to the line at fault. The string
60 // representation of the error can be retrieved with naGetError() on
62 naRef naParseCode(naContext c, naRef srcFile, int firstLine,
63 char* buf, int len, int* errLine);
65 // Binds a bare code object (as returned from naParseCode) with a
66 // closure object (a hash) to act as the outer scope / namespace.
67 naRef naBindFunction(naContext ctx, naRef code, naRef closure);
69 // Similar, but it binds to the current context's closure (i.e. the
70 // namespace at the top of the current call stack).
71 naRef naBindToContext(naContext ctx, naRef code);
73 // Call a code or function object with the specified arguments "on"
74 // the specified object and using the specified hash for the local
75 // variables. Passing a null args array skips the parameter variables
76 // (e.g. "arg") assignments; to get a zero-length arg instead, pass in
77 // argc==0 and a non-null args vector. The obj or locals parameters
78 // may be nil. Will attempt to acquire the mod lock, so call
79 // naModUnlock() first if the lock is already held.
80 naRef naCall(naContext ctx, naRef func, int argc, naRef* args,
81 naRef obj, naRef locals);
83 // As naCall(), but continues execution at the operation after a
84 // previous die() call or runtime error. Useful to do "yield"
85 // semantics, leaving the context in a condition where it can be
86 // restarted from C code. Cannot be used currently to restart a
87 // failed operation. Will attempt to acquire the mod lock, so call
88 // naModUnlock() first if the lock is already held.
89 naRef naContinue(naContext ctx);
91 // Throw an error from the current call stack. This function makes a
92 // longjmp call to a handler in naCall() and DOES NOT RETURN. It is
93 // intended for use in library code that cannot otherwise report an
94 // error via the return value, and MUST be used carefully. If in
95 // doubt, return naNil() as your error condition. Works like
97 void naRuntimeError(naContext c, const char* fmt, ...);
99 // "Re-throws" a runtime error caught from the subcontext. Acts as a
100 // naRuntimeError() called on the parent context. Does not return.
101 void naRethrowError(naContext subc);
103 // Retrieve the specified member from the object, respecting the
104 // "parents" array as for "object.field". Returns zero for missing
106 int naMember_get(naContext c, naRef obj, naRef field, naRef* out);
107 int naMember_cget(naContext c, naRef obj, const char* field, naRef* out);
109 // Returns a hash containing functions from the Nasal standard library
110 // Useful for passing as a namespace to an initial function call
111 naRef naInit_std(naContext c);
113 // Ditto, for other core libraries
114 naRef naInit_math(naContext c);
115 naRef naInit_bits(naContext c);
116 naRef naInit_io(naContext c);
117 naRef naInit_regex(naContext c);
118 naRef naInit_unix(naContext c);
119 naRef naInit_thread(naContext c);
120 naRef naInit_utf8(naContext c);
121 naRef naInit_sqlite(naContext c);
122 naRef naInit_readline(naContext c);
123 naRef naInit_gtk(naContext ctx);
124 naRef naInit_cairo(naContext ctx);
126 // Returns a hash which can be used to add methods callable on strings
127 naRef naInit_string(naContext c);
129 // Context stack inspection, frame zero is the "top"
130 int naStackDepth(naContext ctx);
131 int naGetLine(naContext ctx, int frame);
132 naRef naGetSourceFile(naContext ctx, int frame);
133 char* naGetError(naContext ctx);
136 int naIsNil(naRef r) GCC_PURE;
137 int naIsNum(naRef r) GCC_PURE;
138 int naIsString(naRef r) GCC_PURE;
139 int naIsScalar(naRef r) GCC_PURE;
140 int naIsVector(naRef r) GCC_PURE;
141 int naIsHash(naRef r) GCC_PURE;
142 int naIsCode(naRef r) GCC_PURE;
143 int naIsFunc(naRef r) GCC_PURE;
144 int naIsCCode(naRef r) GCC_PURE;
146 // Allocators/generators:
147 naRef naNil() GCC_PURE;
148 naRef naNum(double num) GCC_PURE;
149 naRef naNewString(naContext c);
150 naRef naNewVector(naContext c);
151 naRef naNewHash(naContext c);
152 naRef naNewFunc(naContext c, naRef code);
155 * Register extension function
157 * @param fptr Pointer to C-function
158 * @param user_data Optional user data passed back on calling the function
159 * @param destroy Optional callback called if function gets freed by garbage
160 * collector to free user data if required.
162 naRef naNewCCode(naContext c, naCFunction fptr);
163 naRef naNewCCodeU(naContext c, naCFunctionU fptr, void* user_data);
164 naRef naNewCCodeUD(naContext c, naCFunctionU fptr, void* user_data,
165 void (*destroy)(void*));
167 // Some useful conversion/comparison routines
168 int naEqual(naRef a, naRef b) GCC_PURE;
169 int naStrEqual(naRef a, naRef b) GCC_PURE;
170 int naTrue(naRef b) GCC_PURE;
171 naRef naNumValue(naRef n) GCC_PURE;
172 naRef naStringValue(naContext c, naRef n);
175 int naStr_len(naRef s) GCC_PURE;
176 char* naStr_data(naRef s) GCC_PURE;
177 naRef naStr_fromdata(naRef dst, const char* data, int len);
178 naRef naStr_concat(naRef dest, naRef s1, naRef s2);
179 naRef naStr_substr(naRef dest, naRef str, int start, int len);
180 naRef naInternSymbol(naRef sym);
181 naRef getStringMethods(naContext c);
184 int naVec_size(naRef v);
185 naRef naVec_get(naRef v, int i);
186 void naVec_set(naRef vec, int i, naRef o);
187 int naVec_append(naRef vec, naRef o);
188 void naVec_setsize(naContext c, naRef vec, int sz);
191 * Remove and retrieve the first element of the vector.
193 * This operation reduces the size of the vector by one and moves all elements
194 * by one towards the begin of the vector.
196 * @return The element removed from the begin
198 naRef naVec_removefirst(naRef vec);
201 * Remove and retrieve the last element of the vector.
203 * This operation reduces the size of the vector by one.
205 * @return The element removed from the end
207 naRef naVec_removelast(naRef vec);
210 int naHash_size(naRef h);
211 int naHash_get(naRef hash, naRef key, naRef* out);
212 naRef naHash_cget(naRef hash, char* key);
213 void naHash_set(naRef hash, naRef key, naRef val);
214 void naHash_cset(naRef hash, char* key, naRef val);
215 void naHash_delete(naRef hash, naRef key);
217 * Store the keys in ::hash into the vector at ::dst
221 void naHash_keys(naRef dst, naRef hash);
224 typedef struct naGhostType {
225 void(*destroy)(void*);
227 const char*(*get_member)(naContext c, void*, naRef key, naRef* out);
228 void(*set_member)(naContext c, void*, naRef key, naRef val);
232 * Create a ghost for an object without any attributes. If ::t contains pointers
233 * to get_member or set_member function they will be ignored.
235 naRef naNewGhost(naContext c, naGhostType* t, void* ghost);
237 * Create a ghost for an object. This version uses the get_member and set_member
238 * function pointers in ::t upon trying to get or set a member respectively from
241 naRef naNewGhost2(naContext c, naGhostType* t, void* ghost);
242 naGhostType* naGhost_type(naRef ghost);
243 void* naGhost_ptr(naRef ghost);
244 int naIsGhost(naRef r);
246 // Acquires a "modification lock" on a context, allowing the C code to
247 // modify Nasal data without fear that such data may be "lost" by the
248 // garbage collector (nasal data on the C stack is not examined in
249 // GC!). This disallows garbage collection until the current thread
250 // can be blocked. The lock should be acquired whenever nasal objects
251 // are being modified. It need not be acquired when only read access
252 // is needed, PRESUMING that the Nasal data being read is findable by
253 // the collector (via naSave, for example) and that another Nasal
254 // thread cannot or will not delete the reference to the data. It
255 // MUST NOT be acquired by naCFunction's, as those are called with the
256 // lock already held; acquiring two locks for the same thread will
257 // cause a deadlock when the GC is invoked. It should be UNLOCKED by
258 // naCFunction's when they are about to do any long term non-nasal
259 // processing and/or blocking I/O. Note that naModLock() may need to
260 // block to allow garbage collection to occur, and that garbage
261 // collection by other threads may be blocked until naModUnlock() is
262 // called. It must also be UNLOCKED by threads that hold a lock
263 // already before making a naCall() or naContinue() call -- these
264 // functions will attempt to acquire the lock again.
268 // Library utilities. Generate namespaces and add symbols.
269 typedef struct { char* name; naCFunction func; } naCFuncItem;
270 naRef naGenLib(naContext c, naCFuncItem *funcs);
271 void naAddSym(naContext c, naRef ns, char *sym, naRef val);