forked from espruino/Espruino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsvariterator.c
More file actions
589 lines (533 loc) · 20.6 KB
/
jsvariterator.c
File metadata and controls
589 lines (533 loc) · 20.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2013 Gordon Williams <gw@pur3.co.uk>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* Iterators for Variables
* ----------------------------------------------------------------------------
*/
#include "jsvariterator.h"
#include "jsvarcache.h"
/**
* Iterate over the contents of the content of a variable, calling callback for each.
* Contents may be:
* * numeric -> output
* * a string -> output each character
* * array/arraybuffer -> call itself on each element
* object -> call itself object.count times, on object.data
*/
bool jsvIterateCallback(
JsVar *data, // The data to iterate over.
void (*callback)(int item, void *callbackData), // The callback function invoke.
void *callbackData // Data to be passed to the callback function
) {
bool ok = true;
// Handle the data being a single numeric.
if (jsvIsNumeric(data)) {
callback((int)jsvGetInteger(data), callbackData);
}
// Handle the data being an object.
else if (jsvIsObject(data)) {
JsVar *countVar = jsvObjectGetChild(data, "count", 0);
JsVar *dataVar = jsvObjectGetChild(data, "data", 0);
if (countVar && dataVar && jsvIsNumeric(countVar)) {
int n = (int)jsvGetInteger(countVar);
while (ok && n-- > 0) {
ok = jsvIterateCallback(dataVar, callback, callbackData);
}
} else {
jsExceptionHere(JSET_TYPEERROR, "If specifying an object, it must be of the form {data : ..., count : N} - got %j", data);
}
jsvUnLock2(countVar, dataVar);
}
// Handle the data being a string
else if (jsvIsString(data)) {
JsvStringIterator it;
jsvStringIteratorNew(&it, data, 0);
while (jsvStringIteratorHasChar(&it) && ok) {
char ch = jsvStringIteratorGetChar(&it);
callback(ch, callbackData);
jsvStringIteratorNext(&it);
}
jsvStringIteratorFree(&it);
}
// Handle the data being an array buffer
else if (jsvIsArrayBuffer(data)) {
JsvArrayBufferIterator it;
jsvArrayBufferIteratorNew(&it, data, 0);
if (JSV_ARRAYBUFFER_GET_SIZE(it.type) == 1 && !JSV_ARRAYBUFFER_IS_SIGNED(it.type)) {
// faster for single byte arrays.
while (jsvArrayBufferIteratorHasElement(&it)) {
callback((int)(unsigned char)jsvStringIteratorGetChar(&it.it), callbackData);
jsvArrayBufferIteratorNext(&it);
}
} else {
while (jsvArrayBufferIteratorHasElement(&it)) {
callback((int)jsvArrayBufferIteratorGetIntegerValue(&it), callbackData);
jsvArrayBufferIteratorNext(&it);
}
}
jsvArrayBufferIteratorFree(&it);
}
// Handle the data being iterable
else if (jsvIsIterable(data)) {
JsvIterator it;
jsvIteratorNew(&it, data);
while (jsvIteratorHasElement(&it) && ok) {
JsVar *el = jsvIteratorGetValue(&it);
ok = jsvIterateCallback(el, callback, callbackData);
jsvUnLock(el);
jsvIteratorNext(&it);
}
jsvIteratorFree(&it);
} else {
jsExceptionHere(JSET_TYPEERROR, "Expecting a number or something iterable, got %t", data);
ok = false;
}
return ok;
}
/**
* An iterable callback that counts how many times it was called.
* This is a function that can be supplied to `jsvIterateCallback`.
*/
static void jsvIterateCallbackCountCb(
int n, //!< The current item being iterated. Not used.
void *data //!< A pointer to an int that counts how many times we were called.
) {
NOT_USED(n);
int *count = (int*)data;
(*count)++;
}
/**
* Determine how many items are in this variable that will be iterated over.
* \return The number of iterations we will call for this variable.
*/
int jsvIterateCallbackCount(JsVar *var) {
// Actually iterate over the variable where the callback function merely increments a counter
// that is initially zero. The result will be the number of times the callback for iteration
// was invoked and hence the iteration count of the variable.
int count = 0;
jsvIterateCallback(var, jsvIterateCallbackCountCb, (void *)&count);
return count;
}
typedef struct { unsigned char *buf; unsigned int idx, length; } JsvIterateCallbackToBytesData;
static void jsvIterateCallbackToBytesCb(int data, void *userData) {
JsvIterateCallbackToBytesData *cbData = (JsvIterateCallbackToBytesData*)userData;
if (cbData->idx < cbData->length)
cbData->buf[cbData->idx] = (unsigned char)data;
cbData->idx++;
}
/** Write all data in array to the data pointer (of size dataSize bytes) */
unsigned int jsvIterateCallbackToBytes(JsVar *var, unsigned char *data, unsigned int dataSize) {
JsvIterateCallbackToBytesData cbData;
cbData.buf = (unsigned char *)data;
cbData.idx = 0;
cbData.length = dataSize;
jsvIterateCallback(var, jsvIterateCallbackToBytesCb, (void*)&cbData);
return cbData.idx;
}
// --------------------------------------------------------------------------------------------
void jsvStringIteratorNew(JsvStringIterator *it, JsVar *str, size_t startIdx) {
assert(jsvHasCharacterData(str));
it->var = jsvLockAgain(str);
it->varIndex = 0;
it->charsInVar = jsvGetCharactersInVar(str);
it->charIdx = startIdx;
if (jsvIsFlatString(str)) {
it->ptr = jsvGetFlatStringPointer(it->var);
} else if (jsvIsNativeString(str)) {
it->ptr = (char*)it->var->varData.nativeStr.ptr;
} else{
it->ptr = &it->var->varData.str[0];
}
while (it->charIdx>0 && it->charIdx >= it->charsInVar) {
it->charIdx -= it->charsInVar;
it->varIndex += it->charsInVar;
if (it->var) {
if (jsvGetLastChild(it->var)) {
JsVar *next = jsvLock(jsvGetLastChild(it->var));
jsvUnLock(it->var);
it->var = next;
it->ptr = &next->varData.str[0];
it->charsInVar = jsvGetCharactersInVar(it->var);
} else {
jsvUnLock(it->var);
it->var = 0;
it->ptr = 0;
it->charsInVar = 0;
it->varIndex = startIdx - it->charIdx;
return; // at end of string - get out of loop
}
}
}
}
JsvStringIterator jsvStringIteratorClone(JsvStringIterator *it) {
JsvStringIterator i = *it;
if (i.var) jsvLockAgain(i.var);
return i;
}
void jsvStringIteratorSetChar(JsvStringIterator *it, char c) {
if (jsvStringIteratorHasChar(it))
it->ptr[it->charIdx] = c;
}
void jsvStringIteratorNext(JsvStringIterator *it) {
jsvStringIteratorNextInline(it);
}
void jsvStringIteratorGotoEnd(JsvStringIterator *it) {
assert(it->var);
while (jsvGetLastChild(it->var)) {
JsVar *next = jsvLock(jsvGetLastChild(it->var));
jsvUnLock(it->var);
it->var = next;
it->varIndex += it->charsInVar;
it->charsInVar = jsvGetCharactersInVar(it->var);
}
it->ptr = &it->var->varData.str[0];
if (it->charsInVar) it->charIdx = it->charsInVar-1;
else it->charIdx = 0;
}
void jsvStringIteratorAppend(JsvStringIterator *it, char ch) {
if (!it->var) return;
if (it->charsInVar>0) {
assert(it->charIdx+1 == it->charsInVar /* check at end */);
it->charIdx++;
} else
assert(it->charIdx == 0);
/* Note: jsvGetMaxCharactersInVar will return the wrong length when
* applied to flat strings, but we don't care because the length will
* be smaller than charIdx, which will force a new string to be
* appended onto the end */
if (it->charIdx >= jsvGetMaxCharactersInVar(it->var)) {
assert(!jsvGetLastChild(it->var));
JsVar *next = jsvNewWithFlags(JSV_STRING_EXT_0);
if (!next) {
jsvUnLock(it->var);
it->var = 0;
it->ptr = 0;
it->charIdx = 0;
return; // out of memory
}
// we don't ref, because StringExts are never reffed as they only have one owner (and ALWAYS have an owner)
jsvSetLastChild(it->var, jsvGetRef(next));
jsvUnLock(it->var);
it->var = next;
it->ptr = &next->varData.str[0];
it->varIndex += it->charIdx;
it->charIdx = 0; // it's new, so empty
}
it->ptr[it->charIdx] = ch;
it->charsInVar = it->charIdx+1;
jsvSetCharactersInVar(it->var, it->charsInVar);
}
// --------------------------------------------------------------------------------------------
void jsvObjectIteratorNew(JsvObjectIterator *it, JsVar *obj) {
assert(jsvIsArray(obj) || jsvIsObject(obj) || jsvIsFunction(obj));
it->var = jsvGetFirstChild(obj) ? jsvLock(jsvGetFirstChild(obj)) : 0;
}
/// Clone the iterator
JsvObjectIterator jsvObjectIteratorClone(JsvObjectIterator *it) {
JsvObjectIterator i = *it;
if (i.var) jsvLockAgain(i.var);
return i;
}
/// Move to next item
void jsvObjectIteratorNext(JsvObjectIterator *it) {
if (it->var) {
JsVarRef next = jsvGetNextSibling(it->var);
jsvUnLock(it->var);
it->var = next ? jsvLock(next) : 0;
}
}
void jsvObjectIteratorSetValue(JsvObjectIterator *it, JsVar *value) {
if (!it->var) return; // end of object
jsvSetValueOfName(it->var, value);
}
void jsvObjectIteratorRemoveAndGotoNext(JsvObjectIterator *it, JsVar *parent) {
if (it->var) {
JsVarRef next = jsvGetNextSibling(it->var);
jsvRemoveChild(parent, it->var);
jsvUnLock(it->var);
it->var = next ? jsvLock(next) : 0;
}
}
// --------------------------------------------------------------------------------------------
void jsvArrayBufferIteratorNew(JsvArrayBufferIterator *it, JsVar *arrayBuffer, size_t index) {
assert(jsvIsArrayBuffer(arrayBuffer));
it->index = index;
it->type = arrayBuffer->varData.arraybuffer.type;
it->byteLength = arrayBuffer->varData.arraybuffer.length * JSV_ARRAYBUFFER_GET_SIZE(it->type);
it->byteOffset = arrayBuffer->varData.arraybuffer.byteOffset;
JsVar *arrayBufferData = jsvGetArrayBufferBackingString(arrayBuffer);
it->byteLength += it->byteOffset; // because we'll check if we have more bytes using this
it->byteOffset = it->byteOffset + index*JSV_ARRAYBUFFER_GET_SIZE(it->type);
if (it->byteOffset>=(it->byteLength+1-JSV_ARRAYBUFFER_GET_SIZE(it->type))) {
jsvUnLock(arrayBufferData);
it->type = ARRAYBUFFERVIEW_UNDEFINED;
return;
}
jsvStringIteratorNew(&it->it, arrayBufferData, (size_t)it->byteOffset);
jsvUnLock(arrayBufferData);
it->hasAccessedElement = false;
}
/// Clone the iterator
ALWAYS_INLINE JsvArrayBufferIterator jsvArrayBufferIteratorClone(JsvArrayBufferIterator *it) {
JsvArrayBufferIterator i = *it;
i.it = jsvStringIteratorClone(&it->it);
return i;
}
static void jsvArrayBufferIteratorGetValueData(JsvArrayBufferIterator *it, char *data) {
if (it->type == ARRAYBUFFERVIEW_UNDEFINED) return;
assert(!it->hasAccessedElement); // we just haven't implemented this case yet
unsigned int i,dataLen = JSV_ARRAYBUFFER_GET_SIZE(it->type);
for (i=0;i<dataLen;i++) {
data[i] = jsvStringIteratorGetChar(&it->it);
if (dataLen!=1) jsvStringIteratorNext(&it->it);
}
if (dataLen!=1) it->hasAccessedElement = true;
}
static JsVarInt jsvArrayBufferIteratorDataToInt(JsvArrayBufferIterator *it, char *data) {
unsigned int dataLen = JSV_ARRAYBUFFER_GET_SIZE(it->type);
JsVarInt v = 0;
if (dataLen==1) v = *(int8_t*)data;
else if (dataLen==2) v = *(short*)data;
else if (dataLen==4) v = *(int*)data;
else assert(0);
if ((!JSV_ARRAYBUFFER_IS_SIGNED(it->type)))
v = v & (JsVarInt)((1UL << (8*dataLen))-1);
return v;
}
static JsVarFloat jsvArrayBufferIteratorDataToFloat(JsvArrayBufferIterator *it, char *data) {
unsigned int dataLen = JSV_ARRAYBUFFER_GET_SIZE(it->type);
JsVarFloat v = 0;
if (dataLen==4) v = *(float*)data;
else if (dataLen==8) v = *(double*)data;
else assert(0);
return v;
}
JsVar *jsvArrayBufferIteratorGetValue(JsvArrayBufferIterator *it) {
if (it->type == ARRAYBUFFERVIEW_UNDEFINED) return 0;
char data[8];
jsvArrayBufferIteratorGetValueData(it, data);
if (JSV_ARRAYBUFFER_IS_FLOAT(it->type)) {
return jsvNewFromFloat(jsvArrayBufferIteratorDataToFloat(it, data));
} else {
JsVarInt i = jsvArrayBufferIteratorDataToInt(it, data);
if (it->type == ARRAYBUFFERVIEW_UINT32)
return jsvNewFromLongInteger((long long)(uint32_t)i);
return jsvNewFromInteger(i);
}
}
JsVar *jsvArrayBufferIteratorGetValueAndRewind(JsvArrayBufferIterator *it) {
JsvStringIterator oldIt = jsvStringIteratorClone(&it->it);
JsVar *v = jsvArrayBufferIteratorGetValue(it);
jsvStringIteratorFree(&it->it);
it->it = oldIt;
it->hasAccessedElement = false;
return v;
}
JsVarInt jsvArrayBufferIteratorGetIntegerValue(JsvArrayBufferIterator *it) {
if (it->type == ARRAYBUFFERVIEW_UNDEFINED) return 0;
char data[8];
jsvArrayBufferIteratorGetValueData(it, data);
if (JSV_ARRAYBUFFER_IS_FLOAT(it->type)) {
return (JsVarInt)jsvArrayBufferIteratorDataToFloat(it, data);
} else {
return jsvArrayBufferIteratorDataToInt(it, data);
}
}
JsVarFloat jsvArrayBufferIteratorGetFloatValue(JsvArrayBufferIterator *it) {
if (it->type == ARRAYBUFFERVIEW_UNDEFINED) return 0;
char data[8];
jsvArrayBufferIteratorGetValueData(it, data);
if (JSV_ARRAYBUFFER_IS_FLOAT(it->type)) {
return jsvArrayBufferIteratorDataToFloat(it, data);
} else {
return (JsVarFloat)jsvArrayBufferIteratorDataToInt(it, data);
}
}
static void jsvArrayBufferIteratorIntToData(char *data, unsigned int dataLen, int type, JsVarInt v) {
if (JSV_ARRAYBUFFER_IS_CLAMPED(type)) {
assert(dataLen==1 && !JSV_ARRAYBUFFER_IS_SIGNED(type)); // all we support right now
if (v<0) v=0;
if (v>255) v=255;
}
// we don't care about sign when writing - as it gets truncated
if (dataLen==1) { data[0] = (char)v; }
else if (dataLen==2) { *(short*)data = (short)v; }
else if (dataLen==4) { *(int*)data = (int)v; }
else if (dataLen==8) { *(long long*)data = (long long)v; }
else assert(0);
}
static void jsvArrayBufferIteratorFloatToData(char *data, unsigned int dataLen, int type, JsVarFloat v) {
NOT_USED(type);
if (dataLen==4) { *(float*)data = (float)v; }
else if (dataLen==8) { *(double*)data = (double)v; }
else assert(0);
}
void jsvArrayBufferIteratorSetIntegerValue(JsvArrayBufferIterator *it, JsVarInt v) {
if (it->type == ARRAYBUFFERVIEW_UNDEFINED) return;
assert(!it->hasAccessedElement); // we just haven't implemented this case yet
char data[8];
unsigned int i,dataLen = JSV_ARRAYBUFFER_GET_SIZE(it->type);
if (JSV_ARRAYBUFFER_IS_FLOAT(it->type)) {
jsvArrayBufferIteratorFloatToData(data, dataLen, it->type, (JsVarFloat)v);
} else {
jsvArrayBufferIteratorIntToData(data, dataLen, it->type, v);
}
for (i=0;i<dataLen;i++) {
jsvStringIteratorSetChar(&it->it, data[i]);
if (dataLen!=1) jsvStringIteratorNext(&it->it);
}
if (dataLen!=1) it->hasAccessedElement = true;
}
void jsvArrayBufferIteratorSetValue(JsvArrayBufferIterator *it, JsVar *value) {
if (it->type == ARRAYBUFFERVIEW_UNDEFINED) return;
assert(!it->hasAccessedElement); // we just haven't implemented this case yet
char data[8];
unsigned int i,dataLen = JSV_ARRAYBUFFER_GET_SIZE(it->type);
if (JSV_ARRAYBUFFER_IS_FLOAT(it->type)) {
jsvArrayBufferIteratorFloatToData(data, dataLen, it->type, jsvGetFloat(value));
} else {
jsvArrayBufferIteratorIntToData(data, dataLen, it->type, jsvGetInteger(value));
}
for (i=0;i<dataLen;i++) {
jsvStringIteratorSetChar(&it->it, data[i]);
if (dataLen!=1) jsvStringIteratorNext(&it->it);
}
if (dataLen!=1) it->hasAccessedElement = true;
}
void jsvArrayBufferIteratorSetByteValue(JsvArrayBufferIterator *it, char c) {
if (JSV_ARRAYBUFFER_GET_SIZE(it->type)!=1) {
assert(0);
return;
}
jsvStringIteratorSetChar(&it->it, c);
}
void jsvArrayBufferIteratorSetValueAndRewind(JsvArrayBufferIterator *it, JsVar *value) {
JsvStringIterator oldIt = jsvStringIteratorClone(&it->it);
jsvArrayBufferIteratorSetValue(it, value);
jsvStringIteratorFree(&it->it);
it->it = oldIt;
it->hasAccessedElement = false;
}
JsVar* jsvArrayBufferIteratorGetIndex(JsvArrayBufferIterator *it) {
return jsvNewFromInteger((JsVarInt)it->index);
}
bool jsvArrayBufferIteratorHasElement(JsvArrayBufferIterator *it) {
if (it->type == ARRAYBUFFERVIEW_UNDEFINED) return false;
if (it->hasAccessedElement) return true;
return it->byteOffset <= (it->byteLength-JSV_ARRAYBUFFER_GET_SIZE(it->type));
}
void jsvArrayBufferIteratorNext(JsvArrayBufferIterator *it) {
it->index++;
it->byteOffset += JSV_ARRAYBUFFER_GET_SIZE(it->type);
if (!it->hasAccessedElement) {
unsigned int dataLen = JSV_ARRAYBUFFER_GET_SIZE(it->type);
while (dataLen--)
jsvStringIteratorNext(&it->it);
} else
it->hasAccessedElement = false;
}
void jsvArrayBufferIteratorFree(JsvArrayBufferIterator *it) {
if (it->type == ARRAYBUFFERVIEW_UNDEFINED) return;
jsvStringIteratorFree(&it->it);
}
// --------------------------------------------------------------------------------------------
/* General Purpose iterator, for Strings, Arrays, Objects, Typed Arrays */
void jsvIteratorNew(JsvIterator *it, JsVar *obj) {
if (jsvIsArray(obj) || jsvIsObject(obj) || jsvIsFunction(obj)) {
it->type = JSVI_OBJECT;
jsvObjectIteratorNew(&it->it.obj, obj);
} else if (jsvIsArrayBuffer(obj)) {
it->type = JSVI_ARRAYBUFFER;
jsvArrayBufferIteratorNew(&it->it.buf, obj, 0);
} else if (jsvHasCharacterData(obj)) {
it->type = JSVI_STRING;
jsvStringIteratorNew(&it->it.str, obj, 0);
} else assert(0);
}
JsVar *jsvIteratorGetKey(JsvIterator *it) {
switch (it->type) {
case JSVI_OBJECT : return jsvObjectIteratorGetKey(&it->it.obj);
case JSVI_STRING : return jsvMakeIntoVariableName(jsvNewFromInteger((JsVarInt)jsvStringIteratorGetIndex(&it->it.str)), 0); // some things expect a veriable name
case JSVI_ARRAYBUFFER : return jsvMakeIntoVariableName(jsvArrayBufferIteratorGetIndex(&it->it.buf), 0); // some things expect a veriable name
default: assert(0); return 0;
}
}
JsVar *jsvIteratorGetValue(JsvIterator *it) {
switch (it->type) {
case JSVI_OBJECT : return jsvObjectIteratorGetValue(&it->it.obj);
case JSVI_STRING : { char buf[2] = {jsvStringIteratorGetChar(&it->it.str),0}; return jsvNewFromString(buf); }
case JSVI_ARRAYBUFFER : return jsvArrayBufferIteratorGetValueAndRewind(&it->it.buf);
default: assert(0); return 0;
}
}
JsVarInt jsvIteratorGetIntegerValue(JsvIterator *it) {
switch (it->type) {
case JSVI_OBJECT : {
// fast path for arrays of ints
if (jsvIsNameInt(it->it.obj.var)) return (JsVarInt)jsvGetFirstChildSigned(it->it.obj.var);
return jsvGetIntegerAndUnLock(jsvObjectIteratorGetValue(&it->it.obj));
}
case JSVI_STRING : return (JsVarInt)jsvStringIteratorGetChar(&it->it.str);
case JSVI_ARRAYBUFFER : return jsvArrayBufferIteratorGetIntegerValue(&it->it.buf);
default: assert(0); return 0;
}
}
JsVarFloat jsvIteratorGetFloatValue(JsvIterator *it) {
switch (it->type) {
case JSVI_OBJECT : return jsvGetFloatAndUnLock(jsvObjectIteratorGetValue(&it->it.obj));
case JSVI_STRING : return (JsVarFloat)jsvStringIteratorGetChar(&it->it.str);
case JSVI_ARRAYBUFFER : return jsvArrayBufferIteratorGetFloatValue(&it->it.buf);
default: assert(0); return 0;
}
}
JsVar *jsvIteratorSetValue(JsvIterator *it, JsVar *value) {
switch (it->type) {
case JSVI_OBJECT : jsvObjectIteratorSetValue(&it->it.obj, value); break;
case JSVI_STRING : jsvStringIteratorSetChar(&it->it.str, (char)(jsvIsString(value) ? value->varData.str[0] : (char)jsvGetInteger(value))); break;
case JSVI_ARRAYBUFFER : jsvArrayBufferIteratorSetValueAndRewind(&it->it.buf, value); break;
default: assert(0); break;
}
return value;
}
bool jsvIteratorHasElement(JsvIterator *it) {
switch (it->type) {
case JSVI_OBJECT : return jsvObjectIteratorHasValue(&it->it.obj);
case JSVI_STRING : return jsvStringIteratorHasChar(&it->it.str);
case JSVI_ARRAYBUFFER : return jsvArrayBufferIteratorHasElement(&it->it.buf);
default: assert(0); return 0;
}
}
void jsvIteratorNext(JsvIterator *it) {
switch (it->type) {
case JSVI_OBJECT : jsvObjectIteratorNext(&it->it.obj); break;
case JSVI_STRING : jsvStringIteratorNext(&it->it.str); break;
case JSVI_ARRAYBUFFER : jsvArrayBufferIteratorNext(&it->it.buf); break;
default: assert(0); break;
}
}
void jsvIteratorFree(JsvIterator *it) {
switch (it->type) {
case JSVI_OBJECT : jsvObjectIteratorFree(&it->it.obj); break;
case JSVI_STRING : jsvStringIteratorFree(&it->it.str); break;
case JSVI_ARRAYBUFFER : jsvArrayBufferIteratorFree(&it->it.buf); break;
default: assert(0); break;
}
}
JsvIterator jsvIteratorClone(JsvIterator *it) {
JsvIterator newit;
newit.type = it->type;
switch (it->type) {
case JSVI_OBJECT : newit.it.obj = jsvObjectIteratorClone(&it->it.obj); break;
case JSVI_STRING : newit.it.str = jsvStringIteratorClone(&it->it.str); break;
case JSVI_ARRAYBUFFER : newit.it.buf = jsvArrayBufferIteratorClone(&it->it.buf); break;
default: assert(0); break;
}
return newit;
}