core: dicts (and arrays) now preserve insertion order
[jimtcl.git] / jim.h
bloba1e4c4659322a070c34aaa6353de104ed1a6248c
1 /* Jim - A small embeddable Tcl interpreter
3 * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
4 * Copyright 2005 Clemens Hintze <c.hintze@gmx.net>
5 * Copyright 2005 patthoyts - Pat Thoyts <patthoyts@users.sf.net>
6 * Copyright 2008 oharboe - Øyvind Harboe - oyvind.harboe@zylin.com
7 * Copyright 2008 Andrew Lunn <andrew@lunn.ch>
8 * Copyright 2008 Duane Ellis <openocd@duaneellis.com>
9 * Copyright 2008 Uwe Klein <uklein@klein-messgeraete.de>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials
20 * provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
23 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * The views and conclusions contained in the software and documentation
36 * are those of the authors and should not be interpreted as representing
37 * official policies, either expressed or implied, of the Jim Tcl Project.
39 *--- Inline Header File Documentation ---
40 * [By Duane Ellis, openocd@duaneellis.com, 8/18/8]
42 * Belief is "Jim" would greatly benefit if Jim Internals where
43 * documented in some way - form whatever, and perhaps - the package:
44 * 'doxygen' is the correct approach to do that.
46 * Details, see: http://www.stack.nl/~dimitri/doxygen/
48 * To that end please follow these guide lines:
50 * (A) Document the PUBLIC api in the .H file.
52 * (B) Document JIM Internals, in the .C file.
54 * (C) Remember JIM is embedded in other packages, to that end do
55 * not assume that your way of documenting is the right way, Jim's
56 * public documentation should be agnostic, such that it is some
57 * what agreeable with the "package" that is embedding JIM inside
58 * of it's own doxygen documentation.
60 * (D) Use minimal Doxygen tags.
62 * This will be an "ongoing work in progress" for some time.
63 **/
65 #ifndef __JIM__H
66 #define __JIM__H
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
72 #include <time.h>
73 #include <limits.h>
74 #include <stdio.h> /* for the FILE typedef definition */
75 #include <stdlib.h> /* In order to export the Jim_Free() macro */
76 #include <stdarg.h> /* In order to get type va_list */
78 /* -----------------------------------------------------------------------------
79 * System configuration
80 * autoconf (configure) will set these
81 * ---------------------------------------------------------------------------*/
82 #include <jim-win32compat.h>
84 #ifndef HAVE_NO_AUTOCONF
85 #include <jim-config.h>
86 #endif
88 /* -----------------------------------------------------------------------------
89 * Compiler specific fixes.
90 * ---------------------------------------------------------------------------*/
92 /* Long Long type and related issues */
93 #ifndef jim_wide
94 # ifdef HAVE_LONG_LONG
95 # define jim_wide long long
96 # ifndef LLONG_MAX
97 # define LLONG_MAX 9223372036854775807LL
98 # endif
99 # ifndef LLONG_MIN
100 # define LLONG_MIN (-LLONG_MAX - 1LL)
101 # endif
102 # define JIM_WIDE_MIN LLONG_MIN
103 # define JIM_WIDE_MAX LLONG_MAX
104 # else
105 # define jim_wide long
106 # define JIM_WIDE_MIN LONG_MIN
107 # define JIM_WIDE_MAX LONG_MAX
108 # endif
110 /* -----------------------------------------------------------------------------
111 * LIBC specific fixes
112 * ---------------------------------------------------------------------------*/
114 # ifdef HAVE_LONG_LONG
115 # define JIM_WIDE_MODIFIER "lld"
116 # else
117 # define JIM_WIDE_MODIFIER "ld"
118 # define strtoull strtoul
119 # endif
120 #endif
122 #define UCHAR(c) ((unsigned char)(c))
124 /* -----------------------------------------------------------------------------
125 * Exported defines
126 * ---------------------------------------------------------------------------*/
128 #define JIM_OK 0
129 #define JIM_ERR 1
130 #define JIM_RETURN 2
131 #define JIM_BREAK 3
132 #define JIM_CONTINUE 4
133 #define JIM_SIGNAL 5
134 #define JIM_EXIT 6
135 /* The following are internal codes and should never been seen/used */
136 #define JIM_EVAL 7
138 #define JIM_MAX_CALLFRAME_DEPTH 1000 /* default max nesting depth for procs */
139 #define JIM_MAX_EVAL_DEPTH 2000 /* default max nesting depth for eval */
141 /* Some function get an integer argument with flags to change
142 * the behaviour. */
144 /* Starting from 1 << 20 flags are reserved for private uses of
145 * different calls. This way the same 'flags' argument may be used
146 * to pass both global flags and private flags. */
147 #define JIM_PRIV_FLAG_SHIFT 20
149 #define JIM_NONE 0 /* no flags set */
150 #define JIM_ERRMSG 1 /* set an error message in the interpreter. */
151 #define JIM_ENUM_ABBREV 2 /* Jim_GetEnum() - Allow unambiguous abbreviation */
152 #define JIM_UNSHARED 4 /* Jim_GetVariable() - return unshared object */
153 #define JIM_MUSTEXIST 8 /* Jim_SetDictKeysVector() - fail if non-existent */
155 /* Flags for Jim_SubstObj() */
156 #define JIM_SUBST_NOVAR 1 /* don't perform variables substitutions */
157 #define JIM_SUBST_NOCMD 2 /* don't perform command substitutions */
158 #define JIM_SUBST_NOESC 4 /* don't perform escapes substitutions */
159 #define JIM_SUBST_FLAG 128 /* flag to indicate that this is a real substitution object */
161 /* Flags used by API calls getting a 'nocase' argument. */
162 #define JIM_CASESENS 0 /* case sensitive */
163 #define JIM_NOCASE 1 /* no case */
164 #define JIM_OPT_END 2 /* if implemented by a command (e.g. regexp), add -- to the argument list */
166 /* Filesystem related */
167 #define JIM_PATH_LEN 1024
169 /* Unused arguments generate annoying warnings... */
170 #define JIM_NOTUSED(V) ((void) V)
172 #define JIM_LIBPATH "auto_path"
173 #define JIM_INTERACTIVE "tcl_interactive"
175 /* -----------------------------------------------------------------------------
176 * Stack
177 * ---------------------------------------------------------------------------*/
179 typedef struct Jim_Stack {
180 int len;
181 int maxlen;
182 void **vector;
183 } Jim_Stack;
185 /* -----------------------------------------------------------------------------
186 * Hash table
187 * ---------------------------------------------------------------------------*/
189 typedef struct Jim_HashEntry {
190 void *key;
191 union {
192 void *val;
193 int intval;
194 } u;
195 struct Jim_HashEntry *next;
196 } Jim_HashEntry;
198 typedef struct Jim_HashTableType {
199 unsigned int (*hashFunction)(const void *key);
200 void *(*keyDup)(void *privdata, const void *key);
201 void *(*valDup)(void *privdata, const void *obj);
202 int (*keyCompare)(void *privdata, const void *key1, const void *key2);
203 void (*keyDestructor)(void *privdata, void *key);
204 void (*valDestructor)(void *privdata, void *obj);
205 } Jim_HashTableType;
207 typedef struct Jim_HashTable {
208 Jim_HashEntry **table;
209 const Jim_HashTableType *type;
210 void *privdata;
211 unsigned int size;
212 unsigned int sizemask;
213 unsigned int used;
214 unsigned int collisions;
215 unsigned int uniq;
216 } Jim_HashTable;
218 typedef struct Jim_HashTableIterator {
219 Jim_HashTable *ht;
220 Jim_HashEntry *entry, *nextEntry;
221 int index;
222 } Jim_HashTableIterator;
224 /* This is the initial size of every hash table */
225 #define JIM_HT_INITIAL_SIZE 16
227 /* ------------------------------- Macros ------------------------------------*/
228 #define Jim_FreeEntryVal(ht, entry) \
229 if ((ht)->type->valDestructor) \
230 (ht)->type->valDestructor((ht)->privdata, (entry)->u.val)
232 #define Jim_SetHashVal(ht, entry, _val_) do { \
233 if ((ht)->type->valDup) \
234 (entry)->u.val = (ht)->type->valDup((ht)->privdata, (_val_)); \
235 else \
236 (entry)->u.val = (_val_); \
237 } while(0)
239 #define Jim_SetHashIntVal(ht, entry, _val_) (entry)->u.intval = (_val_)
241 #define Jim_FreeEntryKey(ht, entry) \
242 if ((ht)->type->keyDestructor) \
243 (ht)->type->keyDestructor((ht)->privdata, (entry)->key)
245 #define Jim_SetHashKey(ht, entry, _key_) do { \
246 if ((ht)->type->keyDup) \
247 (entry)->key = (ht)->type->keyDup((ht)->privdata, (_key_)); \
248 else \
249 (entry)->key = (void *)(_key_); \
250 } while(0)
252 #define Jim_CompareHashKeys(ht, key1, key2) \
253 (((ht)->type->keyCompare) ? \
254 (ht)->type->keyCompare((ht)->privdata, (key1), (key2)) : \
255 (key1) == (key2))
257 #define Jim_HashKey(ht, key) ((ht)->type->hashFunction(key) + (ht)->uniq)
259 #define Jim_GetHashEntryKey(he) ((he)->key)
260 #define Jim_GetHashEntryVal(he) ((he)->u.val)
261 #define Jim_GetHashEntryIntVal(he) ((he)->u.intval)
262 #define Jim_GetHashTableCollisions(ht) ((ht)->collisions)
263 #define Jim_GetHashTableSize(ht) ((ht)->size)
264 #define Jim_GetHashTableUsed(ht) ((ht)->used)
266 /* -----------------------------------------------------------------------------
267 * Jim_Obj structure
268 * ---------------------------------------------------------------------------*/
270 /* -----------------------------------------------------------------------------
271 * Jim object. This is mostly the same as Tcl_Obj itself,
272 * with the addition of the 'prev' and 'next' pointers.
273 * In Jim all the objects are stored into a linked list for GC purposes,
274 * so that it's possible to access every object living in a given interpreter
275 * sequentially. When an object is freed, it's moved into a different
276 * linked list, used as object pool.
278 * The refcount of a freed object is always -1.
279 * ---------------------------------------------------------------------------*/
280 typedef struct Jim_Obj {
281 char *bytes; /* string representation buffer. NULL = no string repr. */
282 const struct Jim_ObjType *typePtr; /* object type. */
283 int refCount; /* reference count */
284 int length; /* number of bytes in 'bytes', not including the null term. */
285 /* Internal representation union */
286 union {
287 /* integer number type */
288 jim_wide wideValue;
289 /* generic integer value (e.g. index, return code) */
290 int intValue;
291 /* double number type */
292 double doubleValue;
293 /* Generic pointer */
294 void *ptr;
295 /* Generic two pointers value */
296 struct {
297 void *ptr1;
298 void *ptr2;
299 } twoPtrValue;
300 /* Generic pointer, int, int value */
301 struct {
302 void *ptr;
303 int int1;
304 int int2;
305 } ptrIntValue;
306 /* Variable object */
307 struct {
308 struct Jim_Var *varPtr;
309 unsigned long callFrameId; /* for caching */
310 int global; /* If the variable name is globally scoped with :: */
311 } varValue;
312 /* Command object */
313 struct {
314 struct Jim_Obj *nsObj;
315 struct Jim_Cmd *cmdPtr;
316 unsigned long procEpoch; /* for caching */
317 } cmdValue;
318 /* List object */
319 struct {
320 struct Jim_Obj **ele; /* Elements vector */
321 int len; /* Length */
322 int maxLen; /* Allocated 'ele' length */
323 } listValue;
324 /* dict object */
325 struct Jim_Dict *dictValue;
326 /* String type */
327 struct {
328 int maxLength;
329 int charLength; /* utf-8 char length. -1 if unknown */
330 } strValue;
331 /* Reference type */
332 struct {
333 unsigned long id;
334 struct Jim_Reference *refPtr;
335 } refValue;
336 /* Source type */
337 struct {
338 struct Jim_Obj *fileNameObj;
339 int lineNumber;
340 } sourceValue;
341 /* Dict substitution type */
342 struct {
343 struct Jim_Obj *varNameObjPtr;
344 struct Jim_Obj *indexObjPtr;
345 } dictSubstValue;
346 struct {
347 int line;
348 int argc;
349 } scriptLineValue;
350 } internalRep;
351 /* These fields add 8 or 16 bytes more for every object
352 * but this is required for efficient garbage collection
353 * of Jim references. */
354 struct Jim_Obj *prevObjPtr; /* pointer to the prev object. */
355 struct Jim_Obj *nextObjPtr; /* pointer to the next object. */
356 } Jim_Obj;
358 /* Jim_Obj related macros */
359 #define Jim_IncrRefCount(objPtr) \
360 ++(objPtr)->refCount
361 #define Jim_DecrRefCount(interp, objPtr) \
362 if (--(objPtr)->refCount <= 0) Jim_FreeObj(interp, objPtr)
363 #define Jim_IsShared(objPtr) \
364 ((objPtr)->refCount > 1)
366 /* This macro is used when we allocate a new object using
367 * Jim_New...Obj(), but for some error we need to destroy it.
368 * Instead to use Jim_IncrRefCount() + Jim_DecrRefCount() we
369 * can just call Jim_FreeNewObj. To call Jim_Free directly
370 * seems too raw, the object handling may change and we want
371 * that Jim_FreeNewObj() can be called only against objects
372 * that are believed to have refcount == 0. */
373 #define Jim_FreeNewObj Jim_FreeObj
375 /* Free the internal representation of the object. */
376 #define Jim_FreeIntRep(i,o) \
377 if ((o)->typePtr && (o)->typePtr->freeIntRepProc) \
378 (o)->typePtr->freeIntRepProc(i, o)
380 /* Get the internal representation pointer */
381 #define Jim_GetIntRepPtr(o) (o)->internalRep.ptr
383 /* Set the internal representation pointer */
384 #define Jim_SetIntRepPtr(o, p) \
385 (o)->internalRep.ptr = (p)
387 /* The object type structure.
388 * There are three methods.
390 * - freeIntRepProc is used to free the internal representation of the object.
391 * Can be NULL if there is nothing to free.
393 * - dupIntRepProc is used to duplicate the internal representation of the object.
394 * If NULL, when an object is duplicated, the internalRep union is
395 * directly copied from an object to another.
396 * Note that it's up to the caller to free the old internal repr of the
397 * object before to call the Dup method.
399 * - updateStringProc is used to create the string from the internal repr.
402 struct Jim_Interp;
404 typedef void (Jim_FreeInternalRepProc)(struct Jim_Interp *interp,
405 struct Jim_Obj *objPtr);
406 typedef void (Jim_DupInternalRepProc)(struct Jim_Interp *interp,
407 struct Jim_Obj *srcPtr, Jim_Obj *dupPtr);
408 typedef void (Jim_UpdateStringProc)(struct Jim_Obj *objPtr);
410 typedef struct Jim_ObjType {
411 const char *name; /* The name of the type. */
412 Jim_FreeInternalRepProc *freeIntRepProc;
413 Jim_DupInternalRepProc *dupIntRepProc;
414 Jim_UpdateStringProc *updateStringProc;
415 int flags;
416 } Jim_ObjType;
418 /* Jim_ObjType flags */
419 #define JIM_TYPE_NONE 0 /* No flags */
420 #define JIM_TYPE_REFERENCES 1 /* The object may contain references. */
422 /* -----------------------------------------------------------------------------
423 * Call frame, vars, commands structures
424 * ---------------------------------------------------------------------------*/
426 /* Call frame */
427 typedef struct Jim_CallFrame {
428 unsigned long id; /* Call Frame ID. Used for caching. */
429 int level; /* Level of this call frame. 0 = global */
430 struct Jim_HashTable vars; /* Where local vars are stored */
431 struct Jim_HashTable *staticVars; /* pointer to procedure static vars */
432 struct Jim_CallFrame *parent; /* The parent callframe */
433 Jim_Obj *const *argv; /* object vector of the current procedure call. */
434 int argc; /* number of args of the current procedure call. */
435 Jim_Obj *procArgsObjPtr; /* arglist object of the running procedure */
436 Jim_Obj *procBodyObjPtr; /* body object of the running procedure */
437 struct Jim_CallFrame *next; /* Callframes are in a linked list */
438 Jim_Obj *nsObj; /* Namespace for this proc call frame */
439 Jim_Obj *fileNameObj; /* file and line of caller of this proc (if available) */
440 int line;
441 Jim_Stack *localCommands; /* commands to be destroyed when the call frame is destroyed */
442 struct Jim_Obj *tailcallObj; /* Pending tailcall invocation */
443 struct Jim_Cmd *tailcallCmd; /* Resolved command for pending tailcall invocation */
444 } Jim_CallFrame;
446 /* The var structure. It just holds the pointer of the referenced
447 * object. If linkFramePtr is not NULL the variable is a link
448 * to a variable of name stored in objPtr living in the given callframe
449 * (this happens when the [global] or [upvar] command is used).
450 * The interp in order to always know how to free the Jim_Obj associated
451 * with a given variable because in Jim objects memory management is
452 * bound to interpreters. */
453 typedef struct Jim_Var {
454 Jim_Obj *objPtr;
455 struct Jim_CallFrame *linkFramePtr;
456 } Jim_Var;
458 /* The cmd structure. */
459 typedef int Jim_CmdProc(struct Jim_Interp *interp, int argc,
460 Jim_Obj *const *argv);
461 typedef void Jim_DelCmdProc(struct Jim_Interp *interp, void *privData);
463 /* The dict structure. It uses the same approach as Python OrderedDict
464 * of storing a hash table of table offsets into a table containing keys and objects.
465 * This preserves order when adding and replacing elements.
467 typedef struct Jim_Dict {
468 struct JimDictHashEntry {
469 int offset;
470 unsigned hash;
471 } *ht; /* Allocated hash table of size 'size' */
472 unsigned int size; /* Size of the hash table (0 or power of two) */
473 unsigned int sizemask; /* mask to apply to hash to index into offsets table */
474 unsigned int uniq; /* unique value to add to hash generator */
475 Jim_Obj **table; /* Table of alternating key, value elements */
476 int len; /* Number of used elements in table */
477 int maxLen; /* Allocated length of table */
478 } Jim_Dict;
480 /* A command is implemented in C if isproc is 0, otherwise
481 * it is a Tcl procedure with the arglist and body represented by the
482 * two objects referenced by arglistObjPtr and bodyObjPtr. */
483 typedef struct Jim_Cmd {
484 int inUse; /* Reference count */
485 int isproc; /* Is this a procedure? */
486 struct Jim_Cmd *prevCmd; /* Previous command defn if cmd created 'local' */
487 union {
488 struct {
489 /* native (C) command */
490 Jim_CmdProc *cmdProc; /* The command implementation */
491 Jim_DelCmdProc *delProc; /* Called when the command is deleted if != NULL */
492 void *privData; /* command-private data available via Jim_CmdPrivData() */
493 } native;
494 struct {
495 /* Tcl procedure */
496 Jim_Obj *argListObjPtr;
497 Jim_Obj *bodyObjPtr;
498 Jim_HashTable *staticVars; /* Static vars hash table. NULL if no statics. */
499 int argListLen; /* Length of argListObjPtr */
500 int reqArity; /* Number of required parameters */
501 int optArity; /* Number of optional parameters */
502 int argsPos; /* Position of 'args', if specified, or -1 */
503 int upcall; /* True if proc is currently in upcall */
504 struct Jim_ProcArg {
505 Jim_Obj *nameObjPtr; /* Name of this arg */
506 Jim_Obj *defaultObjPtr; /* Default value, (or rename for $args) */
507 } *arglist;
508 Jim_Obj *nsObj; /* Namespace for this proc */
509 } proc;
510 } u;
511 } Jim_Cmd;
513 /* Pseudo Random Number Generator State structure */
514 typedef struct Jim_PrngState {
515 unsigned char sbox[256];
516 unsigned int i, j;
517 } Jim_PrngState;
519 /* -----------------------------------------------------------------------------
520 * Jim interpreter structure.
521 * Fields similar to the real Tcl interpreter structure have the same names.
522 * ---------------------------------------------------------------------------*/
523 typedef struct Jim_Interp {
524 Jim_Obj *result; /* object returned by the last command called. */
525 int errorLine; /* Error line where an error occurred. */
526 Jim_Obj *errorFileNameObj; /* Error file where an error occurred. */
527 int addStackTrace; /* > 0 if a level should be added to the stack trace */
528 int maxCallFrameDepth; /* Used for infinite loop detection. */
529 int maxEvalDepth; /* Used for infinite loop detection. */
530 int evalDepth; /* Current eval depth */
531 int returnCode; /* Completion code to return on JIM_RETURN. */
532 int returnLevel; /* Current level of 'return -level' */
533 int exitCode; /* Code to return to the OS on JIM_EXIT. */
534 long id; /* Hold unique id for various purposes */
535 int signal_level; /* A nesting level of catch -signal */
536 jim_wide sigmask; /* Bit mask of caught signals, or 0 if none */
537 int (*signal_set_result)(struct Jim_Interp *interp, jim_wide sigmask); /* Set a result for the sigmask */
538 Jim_CallFrame *framePtr; /* Pointer to the current call frame */
539 Jim_CallFrame *topFramePtr; /* toplevel/global frame pointer. */
540 struct Jim_HashTable commands; /* Commands hash table */
541 unsigned long procEpoch; /* Incremented every time the result
542 of procedures names lookup caching
543 may no longer be valid. */
544 unsigned long callFrameEpoch; /* Incremented every time a new
545 callframe is created. This id is used for the
546 'ID' field contained in the Jim_CallFrame
547 structure. */
548 int local; /* If 'local' is in effect, newly defined procs keep a reference to the old defn */
549 int quitting; /* Set to 1 during Jim_FreeInterp() */
550 Jim_Obj *liveList; /* Linked list of all the live objects. */
551 Jim_Obj *freeList; /* Linked list of all the unused objects. */
552 Jim_Obj *currentScriptObj; /* Script currently in execution. */
553 Jim_Obj *nullScriptObj; /* script representation of an empty string */
554 Jim_Obj *emptyObj; /* Shared empty string object. */
555 Jim_Obj *trueObj; /* Shared true int object. */
556 Jim_Obj *falseObj; /* Shared false int object. */
557 unsigned long referenceNextId; /* Next id for reference. */
558 struct Jim_HashTable references; /* References hash table. */
559 unsigned long lastCollectId; /* reference max Id of the last GC
560 execution. It's set to ~0 while the collection
561 is running as sentinel to avoid to recursive
562 calls via the [collect] command inside
563 finalizers. */
564 jim_wide lastCollectTime; /* unix time of the last GC execution */
565 Jim_Obj *stackTrace; /* Stack trace object. */
566 Jim_Obj *errorProc; /* Name of last procedure which returned an error */
567 Jim_Obj *unknown; /* Unknown command cache */
568 Jim_Obj *defer; /* "jim::defer" */
569 int unknown_called; /* The unknown command has been invoked */
570 int errorFlag; /* Set if an error occurred during execution. */
571 void *cmdPrivData; /* Used to pass the private data pointer to
572 a command. It is set to what the user specified
573 via Jim_CreateCommand(). */
575 Jim_Cmd *oldCmdCache; /* commands that have been deleted, but may still be cached */
576 int oldCmdCacheSize; /* Number of delete commands */
577 struct Jim_CallFrame *freeFramesList; /* list of CallFrame structures. */
578 struct Jim_HashTable assocData; /* per-interp storage for use by packages */
579 Jim_PrngState *prngState; /* per interpreter Random Number Gen. state. */
580 struct Jim_HashTable packages; /* Provided packages hash table */
581 Jim_Stack *loadHandles; /* handles of loaded modules [load] */
582 } Jim_Interp;
584 /* Currently provided as macro that performs the increment.
585 * At some point may be a real function doing more work.
586 * The proc epoch is used in order to know when a command lookup
587 * cached can no longer considered valid. */
588 #define Jim_SetResultString(i,s,l) Jim_SetResult(i, Jim_NewStringObj(i,s,l))
589 #define Jim_SetResultInt(i,intval) Jim_SetResult(i, Jim_NewIntObj(i,intval))
590 /* Note: Using trueObj and falseObj here makes some things slower...*/
591 #define Jim_SetResultBool(i,b) Jim_SetResultInt(i, b)
592 #define Jim_SetEmptyResult(i) Jim_SetResult(i, (i)->emptyObj)
593 #define Jim_GetResult(i) ((i)->result)
594 #define Jim_CmdPrivData(i) ((i)->cmdPrivData)
596 /* Note that 'o' is expanded only one time inside this macro,
597 * so it's safe to use side effects. */
598 #define Jim_SetResult(i,o) do { \
599 Jim_Obj *_resultObjPtr_ = (o); \
600 Jim_IncrRefCount(_resultObjPtr_); \
601 Jim_DecrRefCount(i,(i)->result); \
602 (i)->result = _resultObjPtr_; \
603 } while(0)
605 /* Use this for filehandles, etc. which need a unique id */
606 #define Jim_GetId(i) (++(i)->id)
608 /* Reference structure. The interpreter pointer is held within privdata member in HashTable */
609 #define JIM_REFERENCE_TAGLEN 7 /* The tag is fixed-length, because the reference
610 string representation must be fixed length. */
611 typedef struct Jim_Reference {
612 Jim_Obj *objPtr;
613 Jim_Obj *finalizerCmdNamePtr;
614 char tag[JIM_REFERENCE_TAGLEN+1];
615 } Jim_Reference;
617 /* -----------------------------------------------------------------------------
618 * Exported API prototypes.
619 * ---------------------------------------------------------------------------*/
621 #define Jim_NewEmptyStringObj(i) Jim_NewStringObj(i, "", 0)
622 #define Jim_FreeHashTableIterator(iter) Jim_Free(iter)
624 #define JIM_EXPORT
626 /* Memory allocation */
627 JIM_EXPORT void *Jim_Alloc (int size);
628 JIM_EXPORT void *Jim_Realloc(void *ptr, int size);
629 JIM_EXPORT void Jim_Free (void *ptr);
630 JIM_EXPORT char * Jim_StrDup (const char *s);
631 JIM_EXPORT char *Jim_StrDupLen(const char *s, int l);
633 /* environment */
634 JIM_EXPORT char **Jim_GetEnviron(void);
635 JIM_EXPORT void Jim_SetEnviron(char **env);
636 JIM_EXPORT int Jim_MakeTempFile(Jim_Interp *interp, const char *filename_template, int unlink_file);
638 /* evaluation */
639 JIM_EXPORT int Jim_Eval(Jim_Interp *interp, const char *script);
640 /* in C code, you can do this and get better error messages */
641 /* Jim_EvalSource( interp, __FILE__, __LINE__ , "some tcl commands"); */
642 JIM_EXPORT int Jim_EvalSource(Jim_Interp *interp, const char *filename, int lineno, const char *script);
643 /* Backwards compatibility */
644 #define Jim_Eval_Named(I, S, F, L) Jim_EvalSource((I), (F), (L), (S))
646 JIM_EXPORT int Jim_EvalGlobal(Jim_Interp *interp, const char *script);
647 JIM_EXPORT int Jim_EvalFile(Jim_Interp *interp, const char *filename);
648 JIM_EXPORT int Jim_EvalFileGlobal(Jim_Interp *interp, const char *filename);
649 JIM_EXPORT int Jim_EvalObj (Jim_Interp *interp, Jim_Obj *scriptObjPtr);
650 JIM_EXPORT int Jim_EvalObjVector (Jim_Interp *interp, int objc,
651 Jim_Obj *const *objv);
652 JIM_EXPORT int Jim_EvalObjList(Jim_Interp *interp, Jim_Obj *listObj);
653 JIM_EXPORT int Jim_EvalObjPrefix(Jim_Interp *interp, Jim_Obj *prefix,
654 int objc, Jim_Obj *const *objv);
655 #define Jim_EvalPrefix(i, p, oc, ov) Jim_EvalObjPrefix((i), Jim_NewStringObj((i), (p), -1), (oc), (ov))
656 JIM_EXPORT int Jim_EvalNamespace(Jim_Interp *interp, Jim_Obj *scriptObj, Jim_Obj *nsObj);
657 JIM_EXPORT int Jim_SubstObj (Jim_Interp *interp, Jim_Obj *substObjPtr,
658 Jim_Obj **resObjPtrPtr, int flags);
660 /* stack */
661 JIM_EXPORT void Jim_InitStack(Jim_Stack *stack);
662 JIM_EXPORT void Jim_FreeStack(Jim_Stack *stack);
663 JIM_EXPORT int Jim_StackLen(Jim_Stack *stack);
664 JIM_EXPORT void Jim_StackPush(Jim_Stack *stack, void *element);
665 JIM_EXPORT void * Jim_StackPop(Jim_Stack *stack);
666 JIM_EXPORT void * Jim_StackPeek(Jim_Stack *stack);
667 JIM_EXPORT void Jim_FreeStackElements(Jim_Stack *stack, void (*freeFunc)(void *ptr));
669 /* hash table */
670 JIM_EXPORT int Jim_InitHashTable (Jim_HashTable *ht,
671 const Jim_HashTableType *type, void *privdata);
672 JIM_EXPORT void Jim_ExpandHashTable (Jim_HashTable *ht,
673 unsigned int size);
674 JIM_EXPORT int Jim_AddHashEntry (Jim_HashTable *ht, const void *key,
675 void *val);
676 JIM_EXPORT int Jim_ReplaceHashEntry (Jim_HashTable *ht,
677 const void *key, void *val);
678 JIM_EXPORT int Jim_DeleteHashEntry (Jim_HashTable *ht,
679 const void *key);
680 JIM_EXPORT int Jim_FreeHashTable (Jim_HashTable *ht);
681 JIM_EXPORT Jim_HashEntry * Jim_FindHashEntry (Jim_HashTable *ht,
682 const void *key);
683 JIM_EXPORT Jim_HashTableIterator *Jim_GetHashTableIterator
684 (Jim_HashTable *ht);
685 JIM_EXPORT Jim_HashEntry * Jim_NextHashEntry
686 (Jim_HashTableIterator *iter);
688 /* objects */
689 JIM_EXPORT Jim_Obj * Jim_NewObj (Jim_Interp *interp);
690 JIM_EXPORT void Jim_FreeObj (Jim_Interp *interp, Jim_Obj *objPtr);
691 JIM_EXPORT void Jim_InvalidateStringRep (Jim_Obj *objPtr);
692 JIM_EXPORT Jim_Obj * Jim_DuplicateObj (Jim_Interp *interp,
693 Jim_Obj *objPtr);
694 JIM_EXPORT const char * Jim_GetString(Jim_Obj *objPtr,
695 int *lenPtr);
696 JIM_EXPORT const char *Jim_String(Jim_Obj *objPtr);
697 JIM_EXPORT int Jim_Length(Jim_Obj *objPtr);
699 /* string object */
700 JIM_EXPORT Jim_Obj * Jim_NewStringObj (Jim_Interp *interp,
701 const char *s, int len);
702 JIM_EXPORT Jim_Obj *Jim_NewStringObjUtf8(Jim_Interp *interp,
703 const char *s, int charlen);
704 JIM_EXPORT Jim_Obj * Jim_NewStringObjNoAlloc (Jim_Interp *interp,
705 char *s, int len);
706 JIM_EXPORT void Jim_AppendString (Jim_Interp *interp, Jim_Obj *objPtr,
707 const char *str, int len);
708 JIM_EXPORT void Jim_AppendObj (Jim_Interp *interp, Jim_Obj *objPtr,
709 Jim_Obj *appendObjPtr);
710 JIM_EXPORT void Jim_AppendStrings (Jim_Interp *interp,
711 Jim_Obj *objPtr, ...);
712 JIM_EXPORT int Jim_StringEqObj(Jim_Obj *aObjPtr, Jim_Obj *bObjPtr);
713 JIM_EXPORT int Jim_StringMatchObj (Jim_Interp *interp, Jim_Obj *patternObjPtr,
714 Jim_Obj *objPtr, int nocase);
715 JIM_EXPORT Jim_Obj * Jim_StringRangeObj (Jim_Interp *interp,
716 Jim_Obj *strObjPtr, Jim_Obj *firstObjPtr,
717 Jim_Obj *lastObjPtr);
718 JIM_EXPORT Jim_Obj * Jim_FormatString (Jim_Interp *interp,
719 Jim_Obj *fmtObjPtr, int objc, Jim_Obj *const *objv);
720 JIM_EXPORT Jim_Obj * Jim_ScanString (Jim_Interp *interp, Jim_Obj *strObjPtr,
721 Jim_Obj *fmtObjPtr, int flags);
722 JIM_EXPORT int Jim_CompareStringImmediate (Jim_Interp *interp,
723 Jim_Obj *objPtr, const char *str);
724 JIM_EXPORT int Jim_StringCompareObj(Jim_Interp *interp, Jim_Obj *firstObjPtr,
725 Jim_Obj *secondObjPtr, int nocase);
726 JIM_EXPORT int Jim_Utf8Length(Jim_Interp *interp, Jim_Obj *objPtr);
728 /* reference object */
729 JIM_EXPORT Jim_Obj * Jim_NewReference (Jim_Interp *interp,
730 Jim_Obj *objPtr, Jim_Obj *tagPtr, Jim_Obj *cmdNamePtr);
731 JIM_EXPORT Jim_Reference * Jim_GetReference (Jim_Interp *interp,
732 Jim_Obj *objPtr);
733 JIM_EXPORT int Jim_SetFinalizer (Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *cmdNamePtr);
734 JIM_EXPORT int Jim_GetFinalizer (Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj **cmdNamePtrPtr);
736 /* interpreter */
737 JIM_EXPORT Jim_Interp * Jim_CreateInterp (void);
738 JIM_EXPORT void Jim_FreeInterp (Jim_Interp *i);
739 JIM_EXPORT int Jim_GetExitCode (Jim_Interp *interp);
740 JIM_EXPORT const char *Jim_ReturnCode(int code);
741 JIM_EXPORT void Jim_SetResultFormatted(Jim_Interp *interp, const char *format, ...);
743 /* commands */
744 JIM_EXPORT void Jim_RegisterCoreCommands (Jim_Interp *interp);
745 JIM_EXPORT int Jim_CreateCommand (Jim_Interp *interp,
746 const char *cmdName, Jim_CmdProc *cmdProc, void *privData,
747 Jim_DelCmdProc *delProc);
748 JIM_EXPORT int Jim_DeleteCommand (Jim_Interp *interp,
749 Jim_Obj *cmdNameObj);
750 JIM_EXPORT int Jim_RenameCommand (Jim_Interp *interp,
751 Jim_Obj *oldNameObj, Jim_Obj *newNameObj);
752 JIM_EXPORT Jim_Cmd * Jim_GetCommand (Jim_Interp *interp,
753 Jim_Obj *objPtr, int flags);
754 JIM_EXPORT int Jim_SetVariable (Jim_Interp *interp,
755 Jim_Obj *nameObjPtr, Jim_Obj *valObjPtr);
756 JIM_EXPORT int Jim_SetVariableStr (Jim_Interp *interp,
757 const char *name, Jim_Obj *objPtr);
758 JIM_EXPORT int Jim_SetGlobalVariableStr (Jim_Interp *interp,
759 const char *name, Jim_Obj *objPtr);
760 JIM_EXPORT int Jim_SetVariableStrWithStr (Jim_Interp *interp,
761 const char *name, const char *val);
762 JIM_EXPORT int Jim_SetVariableLink (Jim_Interp *interp,
763 Jim_Obj *nameObjPtr, Jim_Obj *targetNameObjPtr,
764 Jim_CallFrame *targetCallFrame);
765 JIM_EXPORT Jim_Obj * Jim_MakeGlobalNamespaceName(Jim_Interp *interp,
766 Jim_Obj *nameObjPtr);
767 JIM_EXPORT Jim_Obj * Jim_GetVariable (Jim_Interp *interp,
768 Jim_Obj *nameObjPtr, int flags);
769 JIM_EXPORT Jim_Obj * Jim_GetGlobalVariable (Jim_Interp *interp,
770 Jim_Obj *nameObjPtr, int flags);
771 JIM_EXPORT Jim_Obj * Jim_GetVariableStr (Jim_Interp *interp,
772 const char *name, int flags);
773 JIM_EXPORT Jim_Obj * Jim_GetGlobalVariableStr (Jim_Interp *interp,
774 const char *name, int flags);
775 JIM_EXPORT int Jim_UnsetVariable (Jim_Interp *interp,
776 Jim_Obj *nameObjPtr, int flags);
778 /* call frame */
779 JIM_EXPORT Jim_CallFrame *Jim_GetCallFrameByLevel(Jim_Interp *interp,
780 Jim_Obj *levelObjPtr);
782 /* garbage collection */
783 JIM_EXPORT int Jim_Collect (Jim_Interp *interp);
784 JIM_EXPORT void Jim_CollectIfNeeded (Jim_Interp *interp);
786 /* index object */
787 JIM_EXPORT int Jim_GetIndex (Jim_Interp *interp, Jim_Obj *objPtr,
788 int *indexPtr);
790 /* list object */
791 JIM_EXPORT Jim_Obj * Jim_NewListObj (Jim_Interp *interp,
792 Jim_Obj *const *elements, int len);
793 JIM_EXPORT void Jim_ListInsertElements (Jim_Interp *interp,
794 Jim_Obj *listPtr, int listindex, int objc, Jim_Obj *const *objVec);
795 JIM_EXPORT void Jim_ListAppendElement (Jim_Interp *interp,
796 Jim_Obj *listPtr, Jim_Obj *objPtr);
797 JIM_EXPORT void Jim_ListAppendList (Jim_Interp *interp,
798 Jim_Obj *listPtr, Jim_Obj *appendListPtr);
799 JIM_EXPORT int Jim_ListLength (Jim_Interp *interp, Jim_Obj *objPtr);
800 JIM_EXPORT int Jim_ListIndex (Jim_Interp *interp, Jim_Obj *listPrt,
801 int listindex, Jim_Obj **objPtrPtr, int seterr);
802 JIM_EXPORT Jim_Obj *Jim_ListGetIndex(Jim_Interp *interp, Jim_Obj *listPtr, int idx);
803 JIM_EXPORT int Jim_SetListIndex (Jim_Interp *interp,
804 Jim_Obj *varNamePtr, Jim_Obj *const *indexv, int indexc,
805 Jim_Obj *newObjPtr);
806 JIM_EXPORT Jim_Obj * Jim_ConcatObj (Jim_Interp *interp, int objc,
807 Jim_Obj *const *objv);
808 JIM_EXPORT Jim_Obj *Jim_ListJoin(Jim_Interp *interp,
809 Jim_Obj *listObjPtr, const char *joinStr, int joinStrLen);
811 /* dict object */
812 JIM_EXPORT Jim_Obj * Jim_NewDictObj (Jim_Interp *interp,
813 Jim_Obj *const *elements, int len);
814 JIM_EXPORT int Jim_DictKey (Jim_Interp *interp, Jim_Obj *dictPtr,
815 Jim_Obj *keyPtr, Jim_Obj **objPtrPtr, int flags);
816 JIM_EXPORT int Jim_DictKeysVector (Jim_Interp *interp,
817 Jim_Obj *dictPtr, Jim_Obj *const *keyv, int keyc,
818 Jim_Obj **objPtrPtr, int flags);
819 JIM_EXPORT int Jim_SetDictKeysVector (Jim_Interp *interp,
820 Jim_Obj *varNamePtr, Jim_Obj *const *keyv, int keyc,
821 Jim_Obj *newObjPtr, int flags);
822 JIM_EXPORT Jim_Obj **Jim_DictPairs(Jim_Interp *interp,
823 Jim_Obj *dictPtr, int *len);
824 JIM_EXPORT int Jim_DictAddElement(Jim_Interp *interp, Jim_Obj *objPtr,
825 Jim_Obj *keyObjPtr, Jim_Obj *valueObjPtr);
827 #define JIM_DICTMATCH_KEYS 0x0001
828 #define JIM_DICTMATCH_VALUES 0x002
830 JIM_EXPORT int Jim_DictMatchTypes(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *patternObj, int match_type, int return_types);
831 JIM_EXPORT int Jim_DictSize(Jim_Interp *interp, Jim_Obj *objPtr);
832 JIM_EXPORT int Jim_DictInfo(Jim_Interp *interp, Jim_Obj *objPtr);
833 JIM_EXPORT Jim_Obj *Jim_DictMerge(Jim_Interp *interp, int objc, Jim_Obj *const *objv);
835 /* return code object */
836 JIM_EXPORT int Jim_GetReturnCode (Jim_Interp *interp, Jim_Obj *objPtr,
837 int *intPtr);
839 /* expression object */
840 JIM_EXPORT int Jim_EvalExpression (Jim_Interp *interp,
841 Jim_Obj *exprObjPtr);
842 JIM_EXPORT int Jim_GetBoolFromExpr (Jim_Interp *interp,
843 Jim_Obj *exprObjPtr, int *boolPtr);
845 /* boolean object */
846 JIM_EXPORT int Jim_GetBoolean(Jim_Interp *interp, Jim_Obj *objPtr,
847 int *booleanPtr);
849 /* integer object */
850 JIM_EXPORT int Jim_GetWide (Jim_Interp *interp, Jim_Obj *objPtr,
851 jim_wide *widePtr);
852 JIM_EXPORT int Jim_GetLong (Jim_Interp *interp, Jim_Obj *objPtr,
853 long *longPtr);
854 #define Jim_NewWideObj Jim_NewIntObj
855 JIM_EXPORT Jim_Obj * Jim_NewIntObj (Jim_Interp *interp,
856 jim_wide wideValue);
858 /* double object */
859 JIM_EXPORT int Jim_GetDouble(Jim_Interp *interp, Jim_Obj *objPtr,
860 double *doublePtr);
861 JIM_EXPORT void Jim_SetDouble(Jim_Interp *interp, Jim_Obj *objPtr,
862 double doubleValue);
863 JIM_EXPORT Jim_Obj * Jim_NewDoubleObj(Jim_Interp *interp, double doubleValue);
865 /* commands utilities */
866 JIM_EXPORT void Jim_WrongNumArgs (Jim_Interp *interp, int argc,
867 Jim_Obj *const *argv, const char *msg);
868 JIM_EXPORT int Jim_GetEnum (Jim_Interp *interp, Jim_Obj *objPtr,
869 const char * const *tablePtr, int *indexPtr, const char *name, int flags);
870 JIM_EXPORT int Jim_CheckShowCommands(Jim_Interp *interp, Jim_Obj *objPtr,
871 const char *const *tablePtr);
872 JIM_EXPORT int Jim_ScriptIsComplete(Jim_Interp *interp,
873 Jim_Obj *scriptObj, char *stateCharPtr);
876 * Find a matching name in the array of the given length.
878 * NULL entries are ignored.
880 * Returns the matching index if found, or -1 if not.
882 JIM_EXPORT int Jim_FindByName(const char *name, const char * const array[], size_t len);
884 /* package utilities */
885 typedef void (Jim_InterpDeleteProc)(Jim_Interp *interp, void *data);
886 JIM_EXPORT void * Jim_GetAssocData(Jim_Interp *interp, const char *key);
887 JIM_EXPORT int Jim_SetAssocData(Jim_Interp *interp, const char *key,
888 Jim_InterpDeleteProc *delProc, void *data);
889 JIM_EXPORT int Jim_DeleteAssocData(Jim_Interp *interp, const char *key);
891 /* Packages C API */
892 /* jim-package.c */
893 JIM_EXPORT int Jim_PackageProvide (Jim_Interp *interp,
894 const char *name, const char *ver, int flags);
895 JIM_EXPORT int Jim_PackageRequire (Jim_Interp *interp,
896 const char *name, int flags);
898 /* error messages */
899 JIM_EXPORT void Jim_MakeErrorMessage (Jim_Interp *interp);
901 /* interactive mode */
902 JIM_EXPORT int Jim_InteractivePrompt (Jim_Interp *interp);
903 JIM_EXPORT void Jim_HistoryLoad(const char *filename);
904 JIM_EXPORT void Jim_HistorySave(const char *filename);
905 JIM_EXPORT char *Jim_HistoryGetline(Jim_Interp *interp, const char *prompt);
906 JIM_EXPORT void Jim_HistorySetCompletion(Jim_Interp *interp, Jim_Obj *commandObj);
907 JIM_EXPORT void Jim_HistoryAdd(const char *line);
908 JIM_EXPORT void Jim_HistoryShow(void);
910 /* Misc */
911 JIM_EXPORT int Jim_InitStaticExtensions(Jim_Interp *interp);
912 JIM_EXPORT int Jim_StringToWide(const char *str, jim_wide *widePtr, int base);
913 JIM_EXPORT int Jim_IsBigEndian(void);
916 * Returns 1 if a signal has been received while
917 * in a catch -signal {} clause.
919 #define Jim_CheckSignal(i) ((i)->signal_level && (i)->sigmask)
921 /* jim-load.c */
922 JIM_EXPORT int Jim_LoadLibrary(Jim_Interp *interp, const char *pathName);
923 JIM_EXPORT void Jim_FreeLoadHandles(Jim_Interp *interp);
925 /* jim-aio.c */
926 JIM_EXPORT FILE *Jim_AioFilehandle(Jim_Interp *interp, Jim_Obj *command);
928 /* type inspection - avoid where possible */
929 JIM_EXPORT int Jim_IsDict(Jim_Obj *objPtr);
930 JIM_EXPORT int Jim_IsList(Jim_Obj *objPtr);
932 #ifdef __cplusplus
934 #endif
936 #endif /* __JIM__H */
939 * Local Variables: ***
940 * c-basic-offset: 4 ***
941 * tab-width: 4 ***
942 * End: ***