Fixed a lazySlot bug where lazy slots in different objects end up pointing to the...
[io/quag.git] / libs / iovm / source / IoState_symbols.c
blob1108e3fe83719c22e624383184e86c694a287e1e
1 /*
2 docCopyright("Steve Dekorte", 2002)
3 docLicense("BSD revised")
4 */
6 #include "IoState.h"
7 #include "IoObject.h"
8 #include "IoSeq.h"
9 #include "IoNumber.h"
11 #define MIN_CACHED_NUMBER -10
12 #define MAX_CACHED_NUMBER 256
14 // numbers ----------------------------------
16 void IoState_setupCachedNumbers(IoState *self)
18 int i;
20 self->cachedNumbers = List_new();
22 for (i = MIN_CACHED_NUMBER; i < MAX_CACHED_NUMBER + 1; i ++)
24 IoNumber *number = IoNumber_newWithDouble_(self, i);
25 List_append_(self->cachedNumbers, number);
26 IoState_retain_(self, number);
30 IoObject *IoState_numberWithDouble_(IoState *self, double n)
32 long i = (long)n;
34 if (self->cachedNumbers && i == n && i >= MIN_CACHED_NUMBER && i <= MAX_CACHED_NUMBER)
36 return List_at_(self->cachedNumbers, i - MIN_CACHED_NUMBER);
39 return IoNumber_newWithDouble_(self, n);
42 // strings ----------------------------------
45 IoSymbol *IoState_symbolWithUArray_copy_(IoState *self, UArray *ba, int copy)
47 IoSymbol *ioSymbol = SHash_at_(self->symbols, ba);
49 if (!ioSymbol)
51 ioSymbol = IoSeq_newSymbolWithUArray_copy_(self, ba, copy);
52 return IoState_addSymbol_(self, ioSymbol);
55 if (!copy)
57 UArray_free(ba);
60 IoState_stackRetain_(self, ioSymbol);
61 return ioSymbol;
64 IoSymbol *IoState_symbolWithCString_length_(IoState *self, const char *s, size_t length)
66 UArray *a = UArray_newWithData_type_size_copy_((char *)s, CTYPE_uint8_t, length, 1);
67 UArray_setEncoding_(a, CENCODING_UTF8);
68 UArray_convertToFixedSizeType(a);
69 return IoState_symbolWithUArray_copy_(self, a, 0);
72 IoSymbol *IoState_symbolWithCString_(IoState *self, const char *s)
74 return IoState_symbolWithCString_length_(self, s, strlen(s));
77 IoSymbol *IoState_addSymbol_(IoState *self, IoSymbol *s)
79 SHash_at_put_(self->symbols, IoSeq_rawUArray(s), s);
80 IoObject_isSymbol_(s, 1);
81 return s;
84 void IoState_removeSymbol_(IoState *self, IoSymbol *s)
86 SHash_removeKey_(self->symbols, IoSeq_rawUArray(s));