forked from espruino/Espruino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsvariterator.h
More file actions
294 lines (243 loc) · 11.6 KB
/
jsvariterator.h
File metadata and controls
294 lines (243 loc) · 11.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
/*
* 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
* ----------------------------------------------------------------------------
*/
#ifndef JSVARITERATOR_H_
#define JSVARITERATOR_H_
#include "jsvar.h"
#ifdef SPIFLASH_BASE
#include "jshardware.h"
#endif
/// Callback function to be used with jsvIterateCallback
typedef void (*jsvIterateCallbackFn)(int item, void *callbackData);
/// Callback function to be used with jsvIterateBufferCallback
typedef void (*jsvIterateBufferCallbackFn)(unsigned char *data, unsigned int len, void *callbackData);
/** Iterate over the contents of var, 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 *var, jsvIterateCallbackFn callback, void *callbackData);
// Like jsvIterateCallback, but iterates over just bytes and calls with a pointer and length wherever it can
bool jsvIterateBufferCallback(
JsVar *data,
jsvIterateBufferCallbackFn callback,
void *callbackData
);
/** If jsvIterateCallback is called, how many times will it call the callback function? */
uint32_t jsvIterateCallbackCount(JsVar *var);
/** Write all data in array to the data pointer (of size dataSize bytes) */
unsigned int jsvIterateCallbackToBytes(JsVar *var, unsigned char *data, unsigned int dataSize);
// --------------------------------------------------------------------------------------------
typedef struct JsvStringIterator {
size_t charIdx; ///< index of character in var
size_t charsInVar; ///< total characters in var
size_t varIndex; ///< index in string of the start of this var
JsVar *var; ///< current StringExt we're looking at
char *ptr; ///< a pointer to string data
#ifdef SPIFLASH_BASE // when using flash strings, we need somewhere to put the data
char flashStringBuffer[16];
#endif
} JsvStringIterator;
// slight hack to ensure we can use string iterator with const JsVars
#define jsvStringIteratorNewConst(it,str,startIdx) jsvStringIteratorNew(it, (JsVar*)str, startIdx)
/// Create a new String iterator from a string, starting from a specific character. NOTE: This does not keep a lock to the first element, so make sure you do or the string will be freed!
void jsvStringIteratorNew(JsvStringIterator *it, JsVar *str, size_t startIdx);
/// Clone the string iterator
void jsvStringIteratorClone(JsvStringIterator *dstit, JsvStringIterator *it);
/// Gets the current character (or 0)
static ALWAYS_INLINE char jsvStringIteratorGetChar(JsvStringIterator *it) {
if (!it->ptr) return 0;
return (char)READ_FLASH_UINT8(&it->ptr[it->charIdx]);
}
/// Gets the current character (or 0) and increment iterator. Not inlined for speed
char jsvStringIteratorGetCharAndNext(JsvStringIterator *it);
/// Gets the current (>=0) character (or -1)
int jsvStringIteratorGetCharOrMinusOne(JsvStringIterator *it);
/// Do we have a character, or are we at the end?
static ALWAYS_INLINE bool jsvStringIteratorHasChar(JsvStringIterator *it) {
return it->charIdx < it->charsInVar;
}
/// Sets a character (will not extend the string - just overwrites)
void jsvStringIteratorSetChar(JsvStringIterator *it, char c);
/// Sets a character (will not extend the string - just overwrites) and moves on to next character
void jsvStringIteratorSetCharAndNext(JsvStringIterator *it, char c);
/// Gets the current index in the string
static ALWAYS_INLINE size_t jsvStringIteratorGetIndex(JsvStringIterator *it) {
return it->varIndex + it->charIdx;
}
/// Move to next character
void jsvStringIteratorNext(JsvStringIterator *it);
/// Returns a pointer to the next block of data and its length, and moves on to the data after
void jsvStringIteratorGetPtrAndNext(JsvStringIterator *it, unsigned char **data, unsigned int *len);
#ifdef SPIFLASH_BASE
// For 'Flash Strings' only - loads each block from flash memory as required
static void jsvStringIteratorLoadFlashString(JsvStringIterator *it) {
it->varIndex += it->charIdx;
it->charIdx = 0;
uint32_t l = (uint32_t)it->var->varData.nativeStr.len;
if (it->varIndex >= l) {
it->ptr = 0; // past end of string
it->charsInVar = 0;
} else {
it->charsInVar = l - it->varIndex;
if (it->charsInVar > sizeof(it->flashStringBuffer))
it->charsInVar = sizeof(it->flashStringBuffer);
jshFlashRead(it->flashStringBuffer, (uint32_t)it->varIndex+(uint32_t)(size_t)it->var->varData.nativeStr.ptr, (uint32_t)it->charsInVar);
it->ptr = (char*)it->flashStringBuffer;
}
}
#endif
/// Ensures that the correct JsVar is loaded with data for the Iterator. ONLY FOR INTERNAL USE
static ALWAYS_INLINE void jsvStringIteratorLoadInline(JsvStringIterator *it) {
it->charIdx -= it->charsInVar;
it->varIndex += it->charsInVar;
#ifdef SPIFLASH_BASE
if (jsvIsFlashString(it->var))
return jsvStringIteratorLoadFlashString(it);
#endif
if (it->var && 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 += it->charIdx;
it->charIdx = 0;
}
}
/// Move to next character (this one is inlined where speed is needed)
static ALWAYS_INLINE void jsvStringIteratorNextInline(JsvStringIterator *it) {
it->charIdx++;
if (it->charIdx >= it->charsInVar) {
jsvStringIteratorLoadInline(it);
}
}
/// Go to the end of the string iterator - for use with jsvStringIteratorAppend
void jsvStringIteratorGotoEnd(JsvStringIterator *it);
/// Go to the given position in the string iterator. Needs the string again in case we're going back and need to start from the beginning
void jsvStringIteratorGoto(JsvStringIterator *it, JsVar *str, size_t startIdx);
/// Append a character TO THE END of a string iterator
void jsvStringIteratorAppend(JsvStringIterator *it, char ch);
/// Append an entire JsVar string TO THE END of a string iterator
void jsvStringIteratorAppendString(JsvStringIterator *it, JsVar *str, size_t startIdx, int maxLength);
static ALWAYS_INLINE void jsvStringIteratorFree(JsvStringIterator *it) {
jsvUnLock(it->var);
}
/// Special version of append designed for use with vcbprintf_callback (See jsvAppendPrintf)
void jsvStringIteratorPrintfCallback(const char *str, void *user_data);
// --------------------------------------------------------------------------------------------
typedef struct JsvObjectIterator {
JsVar *var;
} JsvObjectIterator;
void jsvObjectIteratorNew(JsvObjectIterator *it, JsVar *obj);
/// Clone the iterator
void jsvObjectIteratorClone(JsvObjectIterator *dstit, JsvObjectIterator *it);
/// Gets the current object element key (or 0)
static ALWAYS_INLINE JsVar *jsvObjectIteratorGetKey(JsvObjectIterator *it) {
if (!it->var) return 0; // end of object
return jsvLockAgain(it->var);
}
/// Gets the current object element value (or 0)
static ALWAYS_INLINE JsVar *jsvObjectIteratorGetValue(JsvObjectIterator *it) {
if (!it->var) return 0; // end of object
return jsvSkipName(it->var); // might even be undefined
}
/// Do we have a key, or are we at the end?
static ALWAYS_INLINE bool jsvObjectIteratorHasValue(JsvObjectIterator *it) {
return it->var != 0;
}
/// Set the current array element
void jsvObjectIteratorSetValue(JsvObjectIterator *it, JsVar *value);
/// Move to next item
void jsvObjectIteratorNext(JsvObjectIterator *it);
/// Remove the current element and move to next element. Needs the parent supplied (the JsVar passed to jsvObjectIteratorNew) as we don't store it
void jsvObjectIteratorRemoveAndGotoNext(JsvObjectIterator *it, JsVar *parent);
static ALWAYS_INLINE void jsvObjectIteratorFree(JsvObjectIterator *it) {
jsvUnLock(it->var);
}
// --------------------------------------------------------------------------------------------
typedef struct JsvArrayBufferIterator {
JsvStringIterator it;
JsVarDataArrayBufferViewType type;
size_t byteLength;
size_t byteOffset;
size_t index;
bool hasAccessedElement;
} JsvArrayBufferIterator;
/* TODO: can we add it->getIntegerValue/etc that get set by jsvArrayBufferIteratorNew?
It's be way faster, especially for byte arrays
*/
void jsvArrayBufferIteratorNew(JsvArrayBufferIterator *it, JsVar *arrayBuffer, size_t index);
/// Clone the iterator
void jsvArrayBufferIteratorClone(JsvArrayBufferIterator *dstit, JsvArrayBufferIterator *it);
/** ArrayBuffers have the slightly odd side-effect that you can't write an element
* once you have read it. That's why we have jsvArrayBufferIteratorGetValueAndRewind
* which allows this, but is slower. */
JsVar *jsvArrayBufferIteratorGetValue(JsvArrayBufferIterator *it);
JsVar *jsvArrayBufferIteratorGetValueAndRewind(JsvArrayBufferIterator *it);
JsVarInt jsvArrayBufferIteratorGetIntegerValue(JsvArrayBufferIterator *it);
JsVarFloat jsvArrayBufferIteratorGetFloatValue(JsvArrayBufferIterator *it);
void jsvArrayBufferIteratorSetValue(JsvArrayBufferIterator *it, JsVar *value);
void jsvArrayBufferIteratorSetValueAndRewind(JsvArrayBufferIterator *it, JsVar *value);
void jsvArrayBufferIteratorSetIntegerValue(JsvArrayBufferIterator *it, JsVarInt value);
void jsvArrayBufferIteratorSetByteValue(JsvArrayBufferIterator *it, char c); ///< special case for when we know we're writing to a byte array
JsVar* jsvArrayBufferIteratorGetIndex(JsvArrayBufferIterator *it);
bool jsvArrayBufferIteratorHasElement(JsvArrayBufferIterator *it);
void jsvArrayBufferIteratorNext(JsvArrayBufferIterator *it);
void jsvArrayBufferIteratorFree(JsvArrayBufferIterator *it);
// --------------------------------------------------------------------------------------------
typedef struct {
JsvObjectIterator it;
JsVar *var; // underlying array when using JSVI_FULLARRAY
JsVarInt index; // index when using JSVI_FULLARRAY
} JsvIteratorObj;
union JsvIteratorUnion {
JsvStringIterator str;
JsvIteratorObj obj;
JsvArrayBufferIterator buf;
};
/** General Purpose iterator, for Strings, Arrays, Objects, Typed Arrays */
typedef struct JsvIterator {
enum {
JSVI_NONE,
JSVI_STRING,
JSVI_OBJECT,
JSVI_ARRAYBUFFER,
JSVI_FULLARRAY, // iterate over ALL array items - including not defined
} type;
union JsvIteratorUnion it;
} JsvIterator;
typedef enum {
JSIF_DEFINED_ARRAY_ElEMENTS = 0, ///< iterate only over defined array elements in sparse arrays
JSIF_EVERY_ARRAY_ELEMENT = 1, ///< iterate over every element in arrays, even if not defined
} JsvIteratorFlags;
/** Create a new iterator for any type of variable.
If iterating over an array and everyArrayElement is false, any
array elements that haven't been specified will be skipped */
void jsvIteratorNew(JsvIterator *it, JsVar *obj, JsvIteratorFlags flags);
JsVar *jsvIteratorGetKey(JsvIterator *it);
JsVar *jsvIteratorGetValue(JsvIterator *it);
JsVarInt jsvIteratorGetIntegerValue(JsvIterator *it);
JsVarFloat jsvIteratorGetFloatValue(JsvIterator *it);
JsVar *jsvIteratorSetValue(JsvIterator *it, JsVar *value); // set the value - return it in case we need to unlock it, eg. jsvUnLock(jsvIteratorSetValue(&it, jsvNew...));
bool jsvIteratorHasElement(JsvIterator *it);
void jsvIteratorNext(JsvIterator *it);
void jsvIteratorFree(JsvIterator *it);
void jsvIteratorClone(JsvIterator *dstit, JsvIterator *it);
#endif /* JSVAR_H_ */