forked from espruino/Espruino
-
Notifications
You must be signed in to change notification settings - Fork 4
/
jsutils.h
454 lines (382 loc) · 15.9 KB
/
jsutils.h
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
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2013 Gordon Williams <[email protected]>
*
* 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/.
*
* ----------------------------------------------------------------------------
* Misc utils and cheapskate stdlib implementation
* ----------------------------------------------------------------------------
*/
#ifndef JSUTILS_H_
#define JSUTILS_H_
#include "platform_config.h"
#include <stddef.h>
#ifndef FAKE_STDLIB
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#endif
#include <stdarg.h> // for va_args
#include <stdint.h>
#include <stdbool.h>
#if defined(LINUX) || defined(ARDUINO_AVR)
#include <math.h>
#else
// these are in maths, but are used all over the place
extern int isnan ( double );
extern int isfinite ( double );
#define NAN (((JsVarFloat)0)/(JsVarFloat)0)
#define INFINITY (((JsVarFloat)1)/(JsVarFloat)0)
#endif
#ifndef BUILDNUMBER
#define JS_VERSION "1v84"
#else
#define JS_VERSION "1v84." BUILDNUMBER
#endif
/*
In code:
TODO - should be fixed
FIXME - will probably break if used
OPT - potential for speed optimisation
*/
#if defined(ARM) || defined(AVR)
#define alloca(x) __builtin_alloca(x)
#endif
#if defined(ESP8266)
/** Place constant strings into flash when we can in order to save RAM space. Strings in flash
must be accessed with word reads on aligned boundaries, so we'll have to copy them before
regular use. */
#define FLASH_STR(name, x) static char name[] __attribute__((section(".irom.literal"))) __attribute__((aligned(4))) = x
/// Get the length of a string in flash
size_t flash_strlen(const char *str);
/// Copy a string from flash to RAM
char *flash_strncpy(char *dest, const char *source, size_t cap);
/** Read a uint8_t from this pointer, which could be in RAM or Flash.
On ESP8266 you have to read flash in 32 bit chunks, so force a 32 bit read
and extract just the 8 bits we want */
#define READ_FLASH_UINT8(ptr) ({ uint32_t __p = (uint32_t)(char*)(ptr); volatile uint32_t __d = *(uint32_t*)(__p & (uint32_t)~3); ((uint8_t*)&__d)[__p & 3]; })
#else
/** Read a uint8_t from this pointer, which could be in RAM or Flash.
On ARM this is just a standard read, it's different on ESP8266 */
#define READ_FLASH_UINT8(ptr) (*(uint8_t*)(ptr))
#endif
#if defined(ESP8266)
/** For the esp8266 we need to add CALLED_FROM_INTERRUPT to all functions that may execute at
interrupt time so they get loaded into static RAM instead of flash. We define
it as a no-op for everyone else. This is identical the ICACHE_RAM_ATTR used elsewhere. */
#define CALLED_FROM_INTERRUPT __attribute__((section(".iram1.text")))
#else
#define CALLED_FROM_INTERRUPT
#endif
#if !defined(__USB_TYPE_H) && !defined(CPLUSPLUS) && !defined(__cplusplus) // it is defined in this file too!
#undef FALSE
#undef TRUE
//typedef enum {FALSE = 0, TRUE = !FALSE} bool;
#define FALSE (0)
#define TRUE (1)
//typedef unsigned char bool;
#endif
#define DBL_MIN 2.2250738585072014e-308
#define DBL_MAX 1.7976931348623157e+308
/* Number of Js Variables allowed and Js Reference format.
JsVarRef = uint8_t -> 15 bytes/JsVar so JSVAR_CACHE_SIZE = (RAM - 3000) / 15
JsVarRef = uint16_t -> 20 bytes/JsVar so JSVAR_CACHE_SIZE = (RAM - 3000) / 20
JsVarRef = uint32_t -> 26 bytes/JsVar so JSVAR_CACHE_SIZE = (RAM - 3000) / 26
NOTE: JSVAR_CACHE_SIZE must be at least 2 less than the number we can fit in JsVarRef
See jshardware.c FLASH constants - all this must be able to fit in flash
*/
#ifdef RESIZABLE_JSVARS
// probably linux - allow us to allocate more blocks of variables
typedef uint32_t JsVarRef;
typedef int32_t JsVarRefSigned;
#define JSVARREF_MIN (-2147483648)
#define JSVARREF_MAX 2147483647
#define JSVARREF_SIZE 4
#else
/** JsVerRef stores References for variables - We treat 0 as null
* NOTE: we store JSVAR_DATA_STRING_* as actual values so we can do #if on them below
*
*/
#if JSVAR_CACHE_SIZE <= 254
typedef uint8_t JsVarRef;
typedef int8_t JsVarRefSigned;
#define JSVARREF_MIN (-128)
#define JSVARREF_MAX 127
#define JSVARREF_SIZE 1
#elif JSVAR_CACHE_SIZE <= 1023
/* for this, we use 10 bit refs. GCC can't do that so store refs as
* single bytes and then manually pack an extra 2 bits for each of
* the refs into a byte called 'pack'
*
* We also put the 2 bits from lastChild into 'flags', because then
* we can use 'pack' for character data in a stringext
*
* Note that JsVarRef/JsVarRefSigned are still 2 bytes, which means
* we're only messing around when loading/storing refs - not when
* passing them around.
*/
#define JSVARREF_PACKED_BITS 2
typedef uint16_t JsVarRef;
typedef int16_t JsVarRefSigned;
#define JSVARREF_MIN (-512)
#define JSVARREF_MAX 511
#define JSVARREF_SIZE 1
#else
typedef uint16_t JsVarRef;
typedef int16_t JsVarRefSigned;
#define JSVARREF_MIN (-32768)
#define JSVARREF_MAX 32767
#define JSVARREF_SIZE 2
#endif
#endif
#if defined(__WORDSIZE) && __WORDSIZE == 64
// 64 bit needs extra space to be able to store a function pointer
/// Max length of JSV_NAME_ strings
#define JSVAR_DATA_STRING_NAME_LEN 8
#else
/// Max length of JSV_NAME_ strings
#define JSVAR_DATA_STRING_NAME_LEN 4
#endif
/// Max length for a JSV_STRING
#define JSVAR_DATA_STRING_LEN (JSVAR_DATA_STRING_NAME_LEN+(3*JSVARREF_SIZE))
/// Max length for a JSV_STRINGEXT
#ifdef JSVARREF_PACKED_BITS
#define JSVAR_DATA_STRING_MAX_LEN (JSVAR_DATA_STRING_NAME_LEN+(3*JSVARREF_SIZE)+JSVARREF_SIZE+1) // (JSVAR_DATA_STRING_LEN + sizeof(JsVarRef)*3 + sizeof(JsVarRefCounter) + sizeof(pack))
#else
#define JSVAR_DATA_STRING_MAX_LEN (JSVAR_DATA_STRING_NAME_LEN+(3*JSVARREF_SIZE)+JSVARREF_SIZE) // (JSVAR_DATA_STRING_LEN + sizeof(JsVarRef)*3 + sizeof(JsVarRefCounter))
#endif
/** This is the amount of characters at which it'd be more efficient to use
* a flat string than to use a normal string... */
#define JSV_FLAT_STRING_BREAK_EVEN (JSVAR_DATA_STRING_LEN + JSVAR_DATA_STRING_MAX_LEN)
typedef int32_t JsVarInt;
typedef uint32_t JsVarIntUnsigned;
#ifdef USE_FLOATS
typedef float JsVarFloat;
#else
typedef double JsVarFloat;
#endif
#define JSSYSTIME_MAX 0x7FFFFFFFFFFFFFFFLL
typedef int64_t JsSysTime;
#define JSSYSTIME_INVALID ((JsSysTime)-1)
#define JSLEX_MAX_TOKEN_LENGTH 64 ///< Maximum length we allow tokens (eg. variable names) to be
#define JS_ERROR_TOKEN_BUF_SIZE 16 ///< see jslTokenAsString
#define JS_NUMBER_BUFFER_SIZE 66 ///< 64 bit base 2 + minus + terminating 0
#define JS_VARS_BEFORE_IDLE_GC 32 ///< If we have less free variables than this, do a garbage collect on Idle
#define JSPARSE_MAX_SCOPES 8
#define STRINGIFY_HELPER(x) #x
#define STRINGIFY(x) STRINGIFY_HELPER(x)
#define NOT_USED(x) ( (void)(x) )
// javascript specific names
#define JSPARSE_RETURN_VAR "return" // variable name used for returning function results
#define JSPARSE_PROTOTYPE_VAR "prototype"
#define JSPARSE_CONSTRUCTOR_VAR "constructor"
#define JSPARSE_INHERITS_VAR "__proto__"
// internal names that hopefully nobody will be able to access
#define JS_HIDDEN_CHAR '\xFF' // initial character of var name determines that we shouldn't see this stuff
#define JS_HIDDEN_CHAR_STR "\xFF"
#define JSPARSE_FUNCTION_CODE_NAME JS_HIDDEN_CHAR_STR"cod" // the function's code!
#define JSPARSE_FUNCTION_SCOPE_NAME JS_HIDDEN_CHAR_STR"sco" // the scope of the function's definition
#define JSPARSE_FUNCTION_THIS_NAME JS_HIDDEN_CHAR_STR"ths" // the 'this' variable - for bound functions
#define JSPARSE_FUNCTION_NAME_NAME JS_HIDDEN_CHAR_STR"nam" // for named functions (a = function foo() { foo(); })
#define JSPARSE_FUNCTION_LINENUMBER_NAME JS_HIDDEN_CHAR_STR"lin" // The line number offset of the function
#define JS_EVENT_PREFIX "#on"
#define JSPARSE_EXCEPTION_VAR "except" // when exceptions are thrown, they're stored in the root scope
#define JSPARSE_STACKTRACE_VAR "sTrace" // for errors/exceptions, a stack trace is stored as a string
#define JSPARSE_MODULE_CACHE_NAME "modules"
#if !defined(NO_ASSERT)
#ifdef FLASH_STR
// Place assert strings into flash to save RAM
#define assert(X) do { \
FLASH_STR(flash_X, __STRING(X)); \
if (!(X)) jsAssertFail(__FILE__,__LINE__,flash_X); \
} while(0)
#elif defined(__STRING)
// Normal assert with string in RAM
#define assert(X) do { if (!(X)) jsAssertFail(__FILE__,__LINE__,__STRING(X)); } while(0)
#else
// Limited assert due to compiler not being able to stringify a parameter
#define assert(X) do { if (!(X)) jsAssertFail(__FILE__,__LINE__,""); } while(0)
#endif
#else
#define assert(X) do { } while(0)
#endif
/// Used when we have enums we want to squash down
#define PACKED_FLAGS __attribute__ ((__packed__))
/// Used before functions that we want to ensure are not inlined (eg. "void NO_INLINE foo() {}")
#define NO_INLINE __attribute__ ((noinline))
/// Put before functions that we always want inlined
#if defined(__GNUC__) && !defined(__clang__)
#if defined(LINK_TIME_OPTIMISATION) && !defined(SAVE_ON_FLASH) && !defined(DEBUG)
#define ALWAYS_INLINE __attribute__ ((gnu_inline)) __attribute__((always_inline)) inline
#else
#define ALWAYS_INLINE __attribute__ ((gnu_inline)) inline
#endif
#else
// clang seems to hate being asked to inline when the definition
// isn't in the same file
#define ALWAYS_INLINE
#endif
/// Maximum amount of locks we ever expect to have on a variable (this could limit recursion) must be 2^n-1
#define JSV_LOCK_MAX 15
/// preprocessor power of 2 - suitable up to 16 bits
#define NEXT_POWER_2(X) \
(((X) | (X)>>1 | (X)>>2 | (X)>>3 | \
(X)>>4 | (X)>>5 | (X)>>6 | (X)>>7 | \
(X)>>8 | (X)>>9 | (X)>>10 | (X)>>11 | \
(X)>>12 | (X)>>13 | (X)>>14 | (X)>>15)+1)
/// Proprocessor get bit number
#define GET_BIT_NUMBER(X) \
(((X)== 1)? 0: \
((X)== 2)? 1: \
((X)== 4)? 2: \
((X)== 8)? 3: \
((X)== 16)? 4: \
((X)== 32)? 5: \
((X)== 64)? 6: \
((X)== 128)? 7: \
((X)== 256)? 8: \
((X)== 512)? 9: \
((X)== 1024)?10: \
((X)== 2048)?11: \
((X)== 4096)?12: \
((X)== 8192)?13: \
((X)==16384)?14: \
((X)==32768)?15:10000/*error*/)
// To handle variable size bit fields
#define BITFIELD_DECL(BITFIELD, N) uint32_t BITFIELD[(N+31)/32]
#define BITFIELD_GET(BITFIELD, N) ((BITFIELD[(N)>>5] >> ((N)&31))&1)
#define BITFIELD_SET(BITFIELD, N, VALUE) (BITFIELD[(N)>>5] = (BITFIELD[(N)>>5]& (uint32_t)~(1 << ((N)&31))) | (uint32_t)((VALUE)?(1 << ((N)&31)):0) )
#define BITFIELD_CLEAR(BITFIELD) memset(BITFIELD, 0, sizeof(BITFIELD)) ///< Clear all elements
static inline bool isWhitespace(char ch) {
return (ch==0x09) || // \t - tab
(ch==0x0B) || // vertical tab
(ch==0x0C) || // form feed
(ch==0x20) || // space
(((unsigned char)ch)==0xA0) || // no break space
(ch=='\n') ||
(ch=='\r');
}
static inline bool isNumeric(char ch) {
return (ch>='0') && (ch<='9');
}
static inline bool isHexadecimal(char ch) {
return ((ch>='0') && (ch<='9')) ||
((ch>='a') && (ch<='f')) ||
((ch>='A') && (ch<='F'));
}
static inline bool isAlpha(char ch) {
return ((ch>='a') && (ch<='z')) || ((ch>='A') && (ch<='Z')) || ch=='_';
}
bool isIDString(const char *s);
/** escape a character - if it is required. This may return a reference to a static array,
so you can't store the value it returns in a variable and call it again. */
const char *escapeCharacter(char ch);
/// Convert a character to the hexadecimal equivalent (or -1)
int chtod(char ch);
/* convert a number in the given radix to an int. if radix=0, autodetect */
long long stringToIntWithRadix(const char *s, int radix, bool *hasError);
/* convert hex, binary, octal or decimal string into an int */
long long stringToInt(const char *s);
// forward decl
struct JsLex;
// ------------
typedef enum {
JSET_STRING,
JSET_ERROR,
JSET_SYNTAXERROR,
JSET_TYPEERROR,
JSET_INTERNALERROR,
JSET_REFERENCEERROR
} JsExceptionType;
void jsWarnAt(const char *message, struct JsLex *lex, size_t tokenPos);
void jsAssertFail(const char *file, int line, const char *expr);
#ifndef FLASH_STR
// Normal functions thet place format string in ram
void jsExceptionHere(JsExceptionType type, const char *fmt, ...);
void jsError(const char *fmt, ...);
void jsWarn(const char *fmt, ...);
#else
// Special jsError and jsWarn functions that place the format string into flash to save RAM
#define jsExceptionHere(type, fmt, ...) do { \
FLASH_STR(flash_str, fmt); \
jsExceptionHere_flash(type, flash_str, ##__VA_ARGS__); \
} while(0)
void jsExceptionHere_flash(JsExceptionType type, const char *fmt, ...);
#define jsError(fmt, ...) do { \
FLASH_STR(flash_str, fmt); \
jsError_flash(flash_str, ##__VA_ARGS__); \
} while(0)
void jsError_flash(const char *fmt, ...);
#define jsWarn(fmt, ...) do { \
FLASH_STR(flash_str, fmt); \
jsWarn_flash(flash_str, ##__VA_ARGS__); \
} while(0)
void jsWarn_flash(const char *fmt, ...);
#endif
// ------------
typedef enum {
JSERR_NONE = 0,
JSERR_RX_FIFO_FULL = 1, ///< The IO buffer (ioBuffer in jsdevices.c) is full and data was lost. Happens for character data and watch events
JSERR_BUFFER_FULL = 2, ///< eg. Serial1's buffer exceeded the max size. Doesn't happen if you have an on('data') callback
JSERR_CALLBACK = 4, ///< A callback (on data/watch/timer) caused an error and was removed
JSERR_LOW_MEMORY = 8, ///< Memory is running low - Espruino had to run a garbage collection pass or remove some of the command history
JSERR_MEMORY = 16, ///< Espruino ran out of memory and was unable to allocate some data that it needed.
} PACKED_FLAGS JsErrorFlags;
/** Error flags for things that we don't really want to report on the console,
* but which are good to know about */
extern JsErrorFlags jsErrorFlags;
#ifdef FAKE_STDLIB
char *strncat(char *dst, const char *src, size_t c);
char *strncpy(char *dst, const char *src, size_t c);
size_t strlen(const char *s);
int strcmp(const char *a, const char *b);
void *memcpy(void *dst, const void *src, size_t size);
void *memset(void *dst, int c, size_t size);
#define RAND_MAX (0x7FFFFFFFU) // needs to be unsigned!
int rand();
void srand(unsigned int seed);
#endif
JsVarFloat stringToFloatWithRadix(const char *s, int forceRadix);
JsVarFloat stringToFloat(const char *str);
void itostr_extra(JsVarInt vals,char *str,bool signedVal,unsigned int base); // like itoa, but uses JsVarInt (good on non-32 bit systems)
static ALWAYS_INLINE void itostr(JsVarInt val,char *str,unsigned int base) {
itostr_extra(val, str, true, base);
}
char itoch(int val);
// super ftoa that does fixed point and radix
void ftoa_bounded_extra(JsVarFloat val,char *str, size_t len, int radix, int fractionalDigits);
// normal ftoa with bounds checking
void ftoa_bounded(JsVarFloat val,char *str, size_t len);
/// Wrap a value so it is always between 0 and size (eg. wrapAround(angle, 360))
JsVarFloat wrapAround(JsVarFloat val, JsVarFloat size);
typedef void (*vcbprintf_callback)(const char *str, void *user_data);
/** Espruino-special printf with a callback
* Supported are:
* %d = int
* %0#d = int padded to length # with 0s
* %x = int as hex
* %L = JsVarInt
* %Lx = JsVarInt as hex
* %f = JsVarFloat
* %s = string (char *)
* %c = char
* %v = JsVar * (doesn't have to be a string - it'll be converted)
* %q = JsVar * (in quotes, and escaped)
* %j = Variable printed as JSON
* %t = Type of variable
* %p = Pin
*
* Anything else will assert
*/
void vcbprintf(vcbprintf_callback user_callback, void *user_data, const char *fmt, va_list argp);
/// This one is directly usable..
void cbprintf(vcbprintf_callback user_callback, void *user_data, const char *fmt, ...);
/// a snprintf replacement so mbedtls doesn't try and pull in the whole stdlib to cat two strings together
int espruino_snprintf( char * s, size_t n, const char * fmt, ... );
/** get the amount of free stack we have, in bytes */
size_t jsuGetFreeStack();
#endif /* JSUTILS_H_ */