Skip to content

Commit 8d86fea

Browse files
committed
Allow jim_member_key() to be used only in the Object scope
Close #3
1 parent 5c9b05b commit 8d86fea

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

jim.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ typedef enum {
1919

2020
const char *jim_error_string(Jim_Error error);
2121

22+
typedef enum {
23+
JIM_ARRAY_SCOPE,
24+
JIM_OBJECT_SCOPE,
25+
} Jim_Scope_Kind;
26+
2227
typedef struct {
28+
Jim_Scope_Kind kind;
2329
int tail;
2430
int key;
2531
} Jim_Scope;
@@ -61,10 +67,11 @@ static size_t jim_strlen(const char *s)
6167
return count;
6268
}
6369

64-
static void jim_stack_push(Jim *jim)
70+
static void jim_stack_push(Jim *jim, Jim_Scope_Kind kind)
6571
{
6672
if (jim->error == JIM_OK) {
6773
if (jim->stack_size < JIM_STACK_CAPACITY) {
74+
jim->stack[jim->stack_size].kind = kind;
6875
jim->stack[jim->stack_size].tail = 0;
6976
jim->stack[jim->stack_size].key = 0;
7077
jim->stack_size += 1;
@@ -309,7 +316,7 @@ void jim_array_begin(Jim *jim)
309316
if (jim->error == JIM_OK) {
310317
jim_element_begin(jim);
311318
jim_write_cstr(jim, "[");
312-
jim_stack_push(jim);
319+
jim_stack_push(jim, JIM_ARRAY_SCOPE);
313320
}
314321
}
315322

@@ -328,17 +335,16 @@ void jim_object_begin(Jim *jim)
328335
if (jim->error == JIM_OK) {
329336
jim_element_begin(jim);
330337
jim_write_cstr(jim, "{");
331-
jim_stack_push(jim);
338+
jim_stack_push(jim, JIM_OBJECT_SCOPE);
332339
}
333340
}
334341

335342
void jim_member_key(Jim *jim, const char *str, const unsigned int *size)
336343
{
337-
// TODO(#3): jim_member_key does not throw an error when used inside of array scope instead of object scope
338344
if (jim->error == JIM_OK) {
339345
jim_element_begin(jim);
340346
Jim_Scope *scope = jim_stack_top(jim);
341-
if (scope) {
347+
if (scope && scope->kind == JIM_OBJECT_SCOPE) {
342348
if (!scope->key) {
343349
jim_string_no_element(jim, str, size);
344350
jim_write_cstr(jim, ":");

0 commit comments

Comments
 (0)