55#define JIM_SCOPES_CAPACITY 128
66#endif // JIM_SCOPES_CAPACITY
77
8- #include <stddef.h>
8+ #include <assert.h>
9+ #include <stdlib.h>
910
1011typedef void * Jim_Sink ;
1112typedef size_t (* Jim_Write )(const void * ptr , size_t size , size_t nmemb , Jim_Sink sink );
1213
1314typedef enum {
1415 JIM_OK = 0 ,
1516 JIM_WRITE_ERROR ,
16- JIM_SCOPES_OVERFLOW ,
1717 JIM_SCOPES_UNDERFLOW ,
1818 JIM_OUT_OF_SCOPE_KEY ,
1919 JIM_DOUBLE_KEY
@@ -36,8 +36,9 @@ typedef struct {
3636 Jim_Sink sink ;
3737 Jim_Write write ;
3838 Jim_Error error ;
39- Jim_Scope scopes [JIM_SCOPES_CAPACITY ];
40- size_t scopes_size ;
39+ Jim_Scope * scopes ;
40+ size_t scopes_count ;
41+ size_t scopes_capacity ;
4142} Jim ;
4243
4344void jim_null (Jim * jim );
@@ -74,22 +75,24 @@ static size_t jim_strlen(const char *s)
7475static void jim_scope_push (Jim * jim , Jim_Scope_Kind kind )
7576{
7677 if (jim -> error == JIM_OK ) {
77- if (jim -> scopes_size < JIM_SCOPES_CAPACITY ) {
78- jim -> scopes [jim -> scopes_size ].kind = kind ;
79- jim -> scopes [jim -> scopes_size ].tail = 0 ;
80- jim -> scopes [jim -> scopes_size ].key = 0 ;
81- jim -> scopes_size += 1 ;
82- } else {
83- jim -> error = JIM_SCOPES_OVERFLOW ;
78+ if (jim -> scopes_count >= jim -> scopes_capacity ) {
79+ if (jim -> scopes_capacity == 0 ) jim -> scopes_capacity = JIM_SCOPES_CAPACITY ;
80+ else jim -> scopes_capacity *= 2 ;
81+ jim -> scopes = realloc (jim -> scopes , sizeof (* jim -> scopes )* jim -> scopes_capacity );
82+ assert (jim -> scopes );
8483 }
84+ jim -> scopes [jim -> scopes_count ].kind = kind ;
85+ jim -> scopes [jim -> scopes_count ].tail = 0 ;
86+ jim -> scopes [jim -> scopes_count ].key = 0 ;
87+ jim -> scopes_count += 1 ;
8588 }
8689}
8790
8891static void jim_scope_pop (Jim * jim )
8992{
9093 if (jim -> error == JIM_OK ) {
91- if (jim -> scopes_size > 0 ) {
92- jim -> scopes_size -- ;
94+ if (jim -> scopes_count > 0 ) {
95+ jim -> scopes_count -- ;
9396 } else {
9497 jim -> error = JIM_SCOPES_UNDERFLOW ;
9598 }
@@ -99,8 +102,8 @@ static void jim_scope_pop(Jim *jim)
99102static Jim_Scope * jim_current_scope (Jim * jim )
100103{
101104 if (jim -> error == JIM_OK ) {
102- if (jim -> scopes_size > 0 ) {
103- return & jim -> scopes [jim -> scopes_size - 1 ];
105+ if (jim -> scopes_count > 0 ) {
106+ return & jim -> scopes [jim -> scopes_count - 1 ];
104107 }
105108 }
106109
@@ -165,8 +168,6 @@ const char *jim_error_string(Jim_Error error)
165168 return "There is no error. The developer of this software just had a case of \"Task failed successfully\" https://i.imgur.com/Bdb3rkq.jpg - Please contact the developer and tell them that they are very lazy for not checking errors properly." ;
166169 case JIM_WRITE_ERROR :
167170 return "Write error" ;
168- case JIM_SCOPES_OVERFLOW :
169- return "Stack of Scopes Overflow" ;
170171 case JIM_SCOPES_UNDERFLOW :
171172 return "Stack of Scopes Underflow" ;
172173 case JIM_OUT_OF_SCOPE_KEY :
0 commit comments