/
jsparse.c
3398 lines (3162 loc) · 121 KB
/
jsparse.c
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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/.
*
* ----------------------------------------------------------------------------
* Recursive descent parser for code execution
* ----------------------------------------------------------------------------
*/
#include "jsparse.h"
#include "jsinteractive.h"
#include "jswrapper.h"
#include "jsnative.h"
#include "jswrap_object.h" // for function_replacewith
#include "jswrap_functions.h" // insane check for eval in jspeFunctionCall
#include "jswrap_json.h" // for jsfPrintJSON
#include "jswrap_espruino.h" // for jswrap_espruino_memoryArea
#include "jswrap_string.h" // for jswrap_string_charAt
#ifndef ESPR_NO_REGEX
#include "jswrap_regexp.h" // for jswrap_regexp_constructor
#endif
#ifdef ESPR_JIT
#include "jsjit.h"
#endif
/* Info about execution when Parsing - this saves passing it on the stack
* for each call */
JsExecInfo execInfo;
// ----------------------------------------------- Forward decls
JsVar *jspeAssignmentExpression();
JsVar *jspeExpression();
JsVar *jspeUnaryExpression();
void jspeBlock();
void jspeBlockNoBrackets();
JsVar *jspeStatement();
JsVar *jspeFactor();
void jspEnsureIsPrototype(JsVar *instanceOf, JsVar *prototypeName);
#ifndef ESPR_NO_ARROW_FN
JsVar *jspeArrowFunction(JsVar *funcVar, JsVar *a);
#endif
// ----------------------------------------------- Utils
#define JSP_MATCH_WITH_CLEANUP_AND_RETURN(TOKEN, CLEANUP_CODE, RETURN_VAL) { if (!jslMatch((TOKEN))) { CLEANUP_CODE; return RETURN_VAL; } }
#define JSP_MATCH_WITH_RETURN(TOKEN, RETURN_VAL) JSP_MATCH_WITH_CLEANUP_AND_RETURN(TOKEN, , RETURN_VAL)
#define JSP_MATCH(TOKEN) JSP_MATCH_WITH_CLEANUP_AND_RETURN(TOKEN, , 0) // Match where the user could have given us the wrong token
#define JSP_ASSERT_MATCH(TOKEN) { assert(0+lex->tk==(TOKEN));jslGetNextToken(); } // Match where if we have the wrong token, it's an internal error
#define JSP_SAVE_EXECUTE() JsExecFlags oldExecute = execInfo.execute
#define JSP_RESTORE_EXECUTE() execInfo.execute = (execInfo.execute&(JsExecFlags)(~EXEC_SAVE_RESTORE_MASK)) | (oldExecute&EXEC_SAVE_RESTORE_MASK);
#define JSP_HAS_ERROR (((execInfo.execute)&EXEC_ERROR_MASK)!=0)
#define JSP_SHOULDNT_PARSE (((execInfo.execute)&EXEC_NO_PARSE_MASK)!=0)
ALWAYS_INLINE void jspDebuggerLoopIfCtrlC() {
#ifdef USE_DEBUGGER
if (execInfo.execute & EXEC_CTRL_C_WAIT && JSP_SHOULD_EXECUTE)
jsiDebuggerLoop();
#endif
}
/// if interrupting execution, this is set
bool jspIsInterrupted() {
return (execInfo.execute & EXEC_INTERRUPTED)!=0;
}
/// if interrupting execution, this is set
void jspSetInterrupted(bool interrupt) {
if (interrupt)
execInfo.execute = execInfo.execute | EXEC_INTERRUPTED;
else
execInfo.execute = execInfo.execute & (JsExecFlags)~EXEC_INTERRUPTED;
}
/// Set the error flag - set lineReported if we've already output the line number
void jspSetError(bool lineReported) {
execInfo.execute = (execInfo.execute & (JsExecFlags)~EXEC_YES) | EXEC_ERROR;
if (lineReported)
execInfo.execute |= EXEC_ERROR_LINE_REPORTED;
}
bool jspHasError() {
return JSP_HAS_ERROR;
}
void jspeiClearScopes() {
jsvUnLock(execInfo.scopesVar);
execInfo.scopesVar = 0;
}
bool jspeiAddScope(JsVar *scope) {
if (!execInfo.scopesVar)
execInfo.scopesVar = jsvNewEmptyArray();
if (!execInfo.scopesVar) return false;
jsvArrayPush(execInfo.scopesVar, scope);
return true;
}
void jspeiRemoveScope() {
if (!execInfo.scopesVar || !jsvGetArrayLength(execInfo.scopesVar)) {
// This should never happen unless there's an interpreter error - no need to have an error message
assert(0);
//jsExceptionHere(JSET_INTERNALERROR, "Too many scopes removed");
//jspSetError(false);
return;
}
jsvUnLock(jsvArrayPop(execInfo.scopesVar));
if (!jsvGetFirstChild(execInfo.scopesVar)) {
jsvUnLock(execInfo.scopesVar);
execInfo.scopesVar = 0;
}
}
JsVar *jspeiFindInScopes(const char *name) {
if (execInfo.scopesVar) {
JsVar *it = jsvLockSafe(jsvGetLastChild(execInfo.scopesVar));
while (it) {
JsVar *scope = jsvSkipName(it);
JsVarRef next = jsvGetPrevSibling(it);
JsVar *ref = jsvFindChildFromString(scope, name);
jsvUnLock2(it, scope);
if (ref) return ref;
it = jsvLockSafe(next);
}
}
return jsvFindChildFromString(execInfo.root, name);
}
/// Return the topmost scope (and lock it)
JsVar *jspeiGetTopScope() {
if (execInfo.scopesVar) {
JsVar *scope = jsvGetLastArrayItem(execInfo.scopesVar);
if (scope) return scope;
}
return jsvLockAgain(execInfo.root);
}
JsVar *jspFindPrototypeFor(const char *className) {
JsVar *obj = jsvObjectGetChildIfExists(execInfo.root, className);
if (!obj) return 0;
JsVar *proto = jsvObjectGetChildIfExists(obj, JSPARSE_PROTOTYPE_VAR);
jsvUnLock(obj);
return proto;
}
/** Here we assume that we have already looked in the parent itself -
* and are now going down looking at the stuff it inherited */
JsVar *jspeiFindChildFromStringInParents(JsVar *parent, const char *name) {
if (jsvIsObject(parent)) {
// If an object, look for an 'inherits' var
JsVar *inheritsFrom = jsvObjectGetChildIfExists(parent, JSPARSE_INHERITS_VAR);
// if there's no inheritsFrom, just default to 'Object.prototype'
if (!inheritsFrom)
inheritsFrom = jspFindPrototypeFor("Object");
if (inheritsFrom && inheritsFrom!=parent) {
// we have what it inherits from (this is ACTUALLY the prototype var)
// https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/proto
JsVar *child = jsvFindChildFromString(inheritsFrom, name);
if (!child)
child = jspeiFindChildFromStringInParents(inheritsFrom, name);
jsvUnLock(inheritsFrom);
if (child) return child;
} else
jsvUnLock(inheritsFrom);
} else { // Not actually an object - but might be an array/string/etc
const char *objectName = jswGetBasicObjectName(parent);
while (objectName) {
JsVar *objName = jsvFindChildFromString(execInfo.root, objectName);
if (!objName) {
objName = jspNewPrototype(objectName, true/*object*/);
}
if (objName) {
JsVar *result = 0;
JsVar *obj = jsvSkipNameAndUnLock(objName);
// could be something the user has made - eg. 'Array=1'
if (jsvHasChildren(obj)) {
// We have found an object with this name - search for the prototype var
JsVar *proto = jspGetNamedField(obj, JSPARSE_PROTOTYPE_VAR, false);
if (proto) {
result = jsvFindChildFromString(proto, name);
jsvUnLock(proto);
}
}
jsvUnLock(obj);
if (result) return result;
}
/* We haven't found anything in the actual object, we should check the 'Object' itself
eg, we tried 'String', so now we should try 'Object'. Built-in types don't have room for
a prototype field, so we hard-code it */
objectName = jswGetBasicObjectPrototypeName(objectName);
}
}
// no luck!
return 0;
}
JsVar *jspeiGetScopesAsVar() {
if (!execInfo.scopesVar) return 0; // no scopes!
// If just one element, return it (no array)
if (jsvGetArrayLength(execInfo.scopesVar)==1) {
JsVar *v = jsvGetLastArrayItem(execInfo.scopesVar); // this is faster than getting by index
return v;
}
// Copy this - because if we just returned it, the underlying array would get altered
return jsvCopy(execInfo.scopesVar, true);
}
void jspeiLoadScopesFromVar(JsVar *arr) {
jsvUnLock(execInfo.scopesVar);
execInfo.scopesVar = 0;
if (arr) {
if (jsvIsArray(arr)) {
// TODO: copy on write? would make function calls faster
execInfo.scopesVar = jsvCopy(arr, true);
} else {
// just a single item,but we must package it in an array
execInfo.scopesVar = jsvNewArray(&arr, 1);
}
}
}
// -----------------------------------------------
/// Check that we have enough stack to recurse. Return true if all ok, error if not.
bool jspCheckStackPosition() {
if (jsuGetFreeStack() < 512) { // giving us 512 bytes leeway
jsExceptionHere(JSET_ERROR, "Too much recursion - the stack is about to overflow");
jspSetInterrupted(true);
return false;
}
return true;
}
// Set execFlags such that we are not executing
void jspSetNoExecute() {
execInfo.execute = (execInfo.execute & (JsExecFlags)(int)~EXEC_RUN_MASK) | EXEC_NO;
}
void jspAppendStackTrace(JsVar *stackTrace) {
JsvStringIterator it;
jsvStringIteratorNew(&it, stackTrace, 0);
jsvStringIteratorGotoEnd(&it);
jslPrintPosition((vcbprintf_callback)jsvStringIteratorPrintfCallback, &it, lex->tokenLastStart);
jslPrintTokenLineMarker((vcbprintf_callback)jsvStringIteratorPrintfCallback, &it, lex->tokenLastStart, 0);
jsvStringIteratorFree(&it);
}
/// We had an exception (argument is the exception's value)
void jspSetException(JsVar *value) {
// Add the exception itself to a variable in root scope
JsVar *exception = jsvFindOrAddChildFromString(execInfo.hiddenRoot, JSPARSE_EXCEPTION_VAR);
if (exception) {
jsvSetValueOfName(exception, value);
jsvUnLock(exception);
}
// Set the exception flag
execInfo.execute = execInfo.execute | EXEC_EXCEPTION;
// Try and do a stack trace
if (lex) {
JsVar *stackTrace = jsvObjectGetChild(execInfo.hiddenRoot, JSPARSE_STACKTRACE_VAR, JSV_STRING_0);
if (stackTrace) {
jsvAppendPrintf(stackTrace, " at ");
jspAppendStackTrace(stackTrace);
jsvUnLock(stackTrace);
// stop us from printing the trace in the same block
execInfo.execute = execInfo.execute | EXEC_ERROR_LINE_REPORTED;
}
}
}
/** Return the reported exception if there was one (and clear it). May return undefined even if there was an exception - eg `throw undefined` */
JsVar *jspGetException() {
JsVar *exceptionName = jsvFindChildFromString(execInfo.hiddenRoot, JSPARSE_EXCEPTION_VAR);
if (exceptionName) {
JsVar *exception = jsvSkipName(exceptionName);
jsvRemoveChildAndUnLock(execInfo.hiddenRoot, exceptionName);
JsVar *stack = jspGetStackTrace();
if (stack && jsvHasChildren(exception)) {
jsvObjectSetChild(exception, "stack", stack);
}
jsvUnLock(stack);
return exception;
}
return 0;
}
/** Return a stack trace string if there was one (and clear it) */
JsVar *jspGetStackTrace() {
JsVar *stackTraceName = jsvFindChildFromString(execInfo.hiddenRoot, JSPARSE_STACKTRACE_VAR);
if (stackTraceName) {
JsVar *stackTrace = jsvSkipName(stackTraceName);
jsvRemoveChildAndUnLock(execInfo.hiddenRoot, stackTraceName);
return stackTrace;
}
return 0;
}
// ----------------------------------------------
// we return a value so that JSP_MATCH can return 0 if it fails (if we pass 0, we just parse all args)
NO_INLINE bool jspeFunctionArguments(JsVar *funcVar) {
JSP_MATCH('(');
while (lex->tk!=')') {
if (funcVar) {
char buf[JSLEX_MAX_TOKEN_LENGTH+1];
buf[0] = '\xFF';
strcpy(&buf[1], jslGetTokenValueAsString());
JsVar *param = jsvAddNamedChild(funcVar, 0, buf);
if (!param) { // out of memory
jspSetError(false);
return false;
}
param = jsvMakeFunctionParameter(param); // force this to be called a function parameter
jsvUnLock(param);
}
JSP_MATCH(LEX_ID);
if (lex->tk!=')') JSP_MATCH(',');
}
JSP_MATCH(')');
return true;
}
// Parse function, assuming we're on '{'. funcVar can be 0. returns 'true' is the function included the 'this' keyword
NO_INLINE bool jspeFunctionDefinitionInternal(JsVar *funcVar, bool expressionOnly) {
JslCharPos funcBegin;
bool forcePretokenise = false;
if (expressionOnly) {
if (funcVar)
funcVar->flags = (funcVar->flags & ~JSV_VARTYPEMASK) | JSV_FUNCTION_RETURN;
} else {
JsExecFlags oldExec = execInfo.execute;
execInfo.execute = EXEC_YES; // we need to force EXEC_YES so that the next token is parsed if it's a string, for compiled/ram/jit test
JSP_MATCH('{');
execInfo.execute = oldExec;
#ifndef SAVE_ON_FLASH
if (lex->tk==LEX_STR) {
JsVar *tokenValue = jslGetTokenValueAsVar();
if (jsvIsStringEqual(tokenValue, "compiled")) {
jsWarn("Function marked with \"compiled\" uploaded in source form");
}
#ifndef ESPR_NO_PRETOKENISE
else if (jsvIsStringEqual(tokenValue, "ram")) {
JSP_ASSERT_MATCH(LEX_STR);
if (lex->tk==';') JSP_ASSERT_MATCH(';');
forcePretokenise = true;
}
#endif
#ifdef ESPR_JIT
else if (jsvIsStringEqual(tokenValue, "jit")) {
JslCharPos funcCodeStart;
jslCharPosFromLex(&funcCodeStart);
JSP_ASSERT_MATCH(LEX_STR);
if (lex->tk==';') {
jslCharPosFree(&funcCodeStart);
jslCharPosFromLex(&funcCodeStart); // re-save lex position without ';' in
JSP_ASSERT_MATCH(';');
}
// save start position so if we fail we go back to a normal function parse
JsVar *funcCodeVar = jsjParseFunction();
if (jsvIsFlatString(funcCodeVar)) { // compilation could have failed!
jsvAddNamedChildAndUnLock(funcVar, funcCodeVar, JSPARSE_FUNCTION_JIT_CODE_NAME);
// scope var
JsVar *funcScopeVar = jspeiGetScopesAsVar();
if (funcScopeVar)
jsvAddNamedChildAndUnLock(funcVar, funcScopeVar, JSPARSE_FUNCTION_SCOPE_NAME);
JSP_MATCH('}');
jslCharPosFree(&funcCodeStart);
return true;
} else {
if (funcCodeVar) {
jsiConsolePrintf("JIT ok, but unable to create flat string\n");
}
jsvUnLock(funcCodeVar);
// Set lex to start back... now we parse function as normal JS like before
jslSeekToP(&funcCodeStart);
jslCharPosFree(&funcCodeStart);
}
}
#endif // ESPR_JIT
jsvUnLock(tokenValue);
}
#endif // SAVE_ON_FLASH
/* If the function starts with return, treat it specially -
* we don't want to store the 'return' part of it
*/
if (funcVar && lex->tk==LEX_R_RETURN) {
funcVar->flags = (funcVar->flags & ~JSV_VARTYPEMASK) | JSV_FUNCTION_RETURN;
JSP_ASSERT_MATCH(LEX_R_RETURN);
}
}
#ifndef ESPR_NO_LINE_NUMBERS
// Get the line number (if needed)
JsVarInt lineNumber = 0;
if (funcVar && lex->lineNumberOffset && !(forcePretokenise||jsfGetFlag(JSF_PRETOKENISE))) {
// jslGetLineNumber is slow, so we only do it if we have debug info
lineNumber = (JsVarInt)jslGetLineNumber() + (JsVarInt)lex->lineNumberOffset - 1;
}
#endif
// Get the code - parse it and figure out where it stops
jslSkipWhiteSpace();
jslCharPosNew(&funcBegin, lex->sourceVar, lex->tokenStart);
int lastTokenEnd = -1;
lex->hadThisKeyword = lex->tk == LEX_R_THIS;
if (!expressionOnly) {
int brackets = 0;
JsExecFlags oldExec = execInfo.execute;
execInfo.execute = EXEC_NO; // set no execute so we don't parse strings
while (lex->tk && (brackets || lex->tk != '}')) {
if (lex->tk == '{') brackets++;
if (lex->tk == '}') brackets--;
lastTokenEnd = (int)jsvStringIteratorGetIndex(&lex->it)-1;
JSP_ASSERT_MATCH(lex->tk);
}
execInfo.execute = oldExec; // restore correct exec state
// FIXME: we might be including whitespace after the last token
} else {
JsExecFlags oldExec = execInfo.execute;
execInfo.execute = EXEC_NO;
jsvUnLock(jspeAssignmentExpression());
execInfo.execute = oldExec;
lastTokenEnd = (int)lex->tokenStart;
}
bool hadThisKeyword = lex->hadThisKeyword;
// Then create var and set (if there was any code!)
if (funcVar && lastTokenEnd>0) {
// code var
JsVar *funcCodeVar;
if (!forcePretokenise && jsvIsNativeString(lex->sourceVar)) {
/* If we're parsing from a Native String (eg. E.memoryArea, E.setBootCode) then
use another Native String to load function code straight from flash */
int s = (int)jsvStringIteratorGetIndex(&funcBegin.it) - 1;
funcCodeVar = jsvNewNativeString(lex->sourceVar->varData.nativeStr.ptr + s, (unsigned int)(lastTokenEnd - s));
#ifdef SPIFLASH_BASE
} else if (!forcePretokenise && jsvIsFlashString(lex->sourceVar)) {
/* If we're parsing from a Flash String (eg. loaded from Storage on Bangle.js) then
use another Flash String to load function code straight from flash*/
int s = (int)jsvStringIteratorGetIndex(&funcBegin.it) - 1;
funcCodeVar = jsvNewFlashString(lex->sourceVar->varData.nativeStr.ptr + s, (unsigned int)(lastTokenEnd - s));
#endif
} else {
#ifndef ESPR_NO_PRETOKENISE
if (jsfGetFlag(JSF_PRETOKENISE) || forcePretokenise)
funcCodeVar = jslNewTokenisedStringFromLexer(&funcBegin, (size_t)lastTokenEnd);
else
#endif
funcCodeVar = jslNewStringFromLexer(&funcBegin, (size_t)lastTokenEnd);
}
jsvAddNamedChildAndUnLock(funcVar, funcCodeVar, JSPARSE_FUNCTION_CODE_NAME);
// scope var
JsVar *funcScopeVar = jspeiGetScopesAsVar();
if (funcScopeVar) {
jsvAddNamedChildAndUnLock(funcVar, funcScopeVar, JSPARSE_FUNCTION_SCOPE_NAME);
}
#ifndef ESPR_NO_LINE_NUMBERS
// If we've got a line number, add a var for it
if (lineNumber) {
JsVar *funcLineNumber = jsvNewFromInteger(lineNumber);
if (funcLineNumber) {
jsvAddNamedChildAndUnLock(funcVar, funcLineNumber, JSPARSE_FUNCTION_LINENUMBER_NAME);
}
}
#endif
}
jslCharPosFree(&funcBegin);
if (!expressionOnly) JSP_MATCH('}');
return hadThisKeyword;
}
// Parse function (after 'function' has occurred
NO_INLINE JsVar *jspeFunctionDefinition(bool parseNamedFunction) {
// actually parse a function... We assume that the LEX_FUNCTION and name
// have already been parsed
JsVar *funcVar = 0;
bool actuallyCreateFunction = JSP_SHOULD_EXECUTE;
if (actuallyCreateFunction)
funcVar = jsvNewWithFlags(JSV_FUNCTION);
JsVar *functionInternalName = 0;
if (parseNamedFunction && lex->tk==LEX_ID) {
// you can do `var a = function foo() { foo(); };` - so cope with this
if (funcVar) functionInternalName = jslGetTokenValueAsVar();
// note that we don't add it to the beginning, because it would mess up our function call code
JSP_ASSERT_MATCH(LEX_ID);
}
// Get arguments save them to the structure
if (!jspeFunctionArguments(funcVar)) {
jsvUnLock2(functionInternalName, funcVar);
// parse failed
return 0;
}
// Parse the actual function block
jspeFunctionDefinitionInternal(funcVar, false);
// if we had a function name, add it to the end (if we don't it gets confused with arguments)
if (funcVar && functionInternalName)
jsvObjectSetChildAndUnLock(funcVar, JSPARSE_FUNCTION_NAME_NAME, functionInternalName);
return funcVar;
}
/* Parse just the brackets of a function - and throw
* everything away */
NO_INLINE bool jspeParseFunctionCallBrackets() {
assert(!JSP_SHOULD_EXECUTE);
JSP_MATCH('(');
while (!JSP_SHOULDNT_PARSE && lex->tk != ')') {
jsvUnLock(jspeAssignmentExpression());
#ifndef ESPR_NO_ARROW_FN
if (lex->tk==LEX_ARROW_FUNCTION) {
jsvUnLock(jspeArrowFunction(0, 0));
}
#endif
if (lex->tk!=')') JSP_MATCH(',');
}
if (!JSP_SHOULDNT_PARSE) JSP_MATCH(')');
return 0;
}
/** Handle a function call (assumes we've parsed the function name and we're
* on the start bracket). 'thisArg' is the value of the 'this' variable when the
* function is executed (it's usually the parent object)
*
*
* NOTE: this does not set the execInfo flags - so if execInfo==EXEC_NO, it won't execute
*
* If !isParsing and arg0!=0, argument 0 is set to what is supplied (same with arg1)
*
* functionName is used only for error reporting - and can be 0
*/
NO_INLINE JsVar *jspeFunctionCall(JsVar *function, JsVar *functionName, JsVar *thisArg, bool isParsing, int argCount, JsVar **argPtr) {
if (JSP_SHOULD_EXECUTE && !function) {
if (functionName)
jsExceptionHere(JSET_ERROR, "Function %q not found!", functionName);
else
jsExceptionHere(JSET_ERROR, "Function not found!", functionName);
return 0;
}
if (JSP_SHOULD_EXECUTE) if (!jspCheckStackPosition()) return 0; // try and ensure that we won't overflow our stack
if (JSP_SHOULD_EXECUTE && function) {
JsVar *returnVar = 0;
if (!jsvIsFunction(function)) {
jsExceptionHere(JSET_ERROR, "Expecting function, got %t", function);
return 0;
}
JsVar *thisVar = jsvLockAgainSafe(thisArg);
if (isParsing) JSP_MATCH('(');
/* Ok, so we have 4 options here.
*
* 1: we're native.
* a) args have been pre-parsed, which is awesome
* b) we have to parse our own args into an array
* 2: we're not native
* a) args were pre-parsed and we have to populate the function
* b) we parse our own args, which is possibly better
*/
if (jsvIsNativeFunction(function)) { // ------------------------------------- NATIVE
unsigned int argPtrSize = 0;
int boundArgs = 0;
// Add 'bound' parameters if there were any
JsvObjectIterator it;
jsvObjectIteratorNew(&it, function);
JsVar *param = jsvObjectIteratorGetKey(&it);
while (jsvIsFunctionParameter(param)) {
if ((unsigned)argCount>=argPtrSize) {
// allocate more space on stack if needed
unsigned int newArgPtrSize = (argPtrSize?argPtrSize:(unsigned int)argCount)*4;
size_t newArgPtrByteSize = sizeof(JsVar*)*newArgPtrSize;
if (jsuGetFreeStack() < 256+newArgPtrByteSize) {
jsExceptionHere(JSET_ERROR, "Insufficient stack for this many arguments");
jsvUnLock(thisVar);
return 0;
}
JsVar **newArgPtr = (JsVar**)alloca(newArgPtrByteSize);
memcpy(newArgPtr, argPtr, (unsigned)argCount*sizeof(JsVar*));
argPtr = newArgPtr;
argPtrSize = newArgPtrSize;
}
// if we already had arguments - shift them up...
int i;
for (i=argCount-1;i>=boundArgs;i--)
argPtr[i+1] = argPtr[i];
// add bound argument
argPtr[boundArgs] = jsvSkipName(param);
argCount++;
boundArgs++;
jsvUnLock(param);
jsvObjectIteratorNext(&it);
param = jsvObjectIteratorGetKey(&it);
}
// check if 'this' was defined
while (param) {
if (jsvIsStringEqual(param, JSPARSE_FUNCTION_THIS_NAME)) {
jsvUnLock(thisVar);
thisVar = jsvSkipName(param);
break;
}
jsvUnLock(param);
jsvObjectIteratorNext(&it);
param = jsvObjectIteratorGetKey(&it);
}
jsvUnLock(param);
jsvObjectIteratorFree(&it);
// Now, if we're parsing add the rest of the arguments
int allocatedArgCount = boundArgs;
if (isParsing) {
while (!JSP_HAS_ERROR && lex->tk!=')' && lex->tk!=LEX_EOF) {
if ((unsigned)argCount>=argPtrSize) {
// allocate more space on stack
unsigned int newArgPtrSize = argPtrSize?argPtrSize*4:16;
JsVar **newArgPtr = (JsVar**)alloca(sizeof(JsVar*)*newArgPtrSize);
memcpy(newArgPtr, argPtr, (unsigned)argCount*sizeof(JsVar*));
argPtr = newArgPtr;
argPtrSize = newArgPtrSize;
}
argPtr[argCount++] = jsvSkipNameAndUnLock(jspeAssignmentExpression());
if (lex->tk!=')') JSP_MATCH_WITH_CLEANUP_AND_RETURN(',',jsvUnLockMany((unsigned)argCount, argPtr);jsvUnLock(thisVar);, 0);
}
JSP_MATCH_WITH_CLEANUP_AND_RETURN(')',jsvUnLockMany((unsigned)argCount, argPtr);jsvUnLock(thisVar);, 0);
allocatedArgCount = argCount;
}
void *nativePtr = jsvGetNativeFunctionPtr(function);
JsVar *oldThisVar = execInfo.thisVar;
if (thisVar)
execInfo.thisVar = jsvRef(thisVar);
else {
if (nativePtr==jswrap_eval) { // eval gets to use the current scope
/* Note: proper JS has some utterly insane code that depends on whether
* eval is an lvalue or not:
*
* http://stackoverflow.com/questions/9107240/1-evalthis-vs-evalthis-in-javascript
*
* Doing this in Espruino is quite an upheaval for that one
* slightly insane case - so it's not implemented. */
if (execInfo.thisVar) execInfo.thisVar = jsvRef(execInfo.thisVar);
} else {
execInfo.thisVar = jsvRef(execInfo.root); // 'this' should always default to root
}
}
if (nativePtr && !JSP_HAS_ERROR) {
returnVar = jsnCallFunction(nativePtr, function->varData.native.argTypes, thisVar, argPtr, argCount);
assert(!jsvIsName(returnVar));
} else {
returnVar = 0;
}
// unlock values if we locked them
jsvUnLockMany((unsigned)allocatedArgCount, argPtr);
/* Return to old 'this' var. No need to unlock as we never locked before */
if (execInfo.thisVar) jsvUnRef(execInfo.thisVar);
execInfo.thisVar = oldThisVar;
} else { // ----------------------------------------------------- NOT NATIVE
// create a new symbol table entry for execution of this function
// OPT: can we cache this function execution environment + param variables?
// OPT: Probably when calling a function ONCE, use it, otherwise when recursing, make new?
JsVar *functionRoot = jsvNewWithFlags(JSV_FUNCTION);
if (!functionRoot) { // out of memory
jspSetError(false);
jsvUnLock(thisVar);
return 0;
}
JsVar *functionScope = 0;
JsVar *functionCode = 0;
JsVar *functionInternalName = 0;
#ifndef ESPR_NO_LINE_NUMBERS
uint16_t functionLineNumber = 0;
#endif
#ifdef ESPR_JIT
bool functionIsJIT = false; // is functionCode actually Thumb Assembly (for JS)
#endif
/** NOTE: We expect that the function object will have:
*
* * Parameters
* * Code/Scope/Name
*
* IN THAT ORDER.
*/
JsvObjectIterator it;
jsvObjectIteratorNew(&it, function);
JsVar *param = jsvObjectIteratorGetKey(&it);
JsVar *value = jsvObjectIteratorGetValue(&it);
while (jsvIsFunctionParameter(param) && value) {
jsvAddFunctionParameter(functionRoot, jsvNewFromStringVar(param,1,JSVAPPENDSTRINGVAR_MAXLENGTH), value);
jsvUnLock2(value, param);
jsvObjectIteratorNext(&it);
param = jsvObjectIteratorGetKey(&it);
value = jsvObjectIteratorGetValue(&it);
}
jsvUnLock2(value, param);
if (isParsing) {
int hadParams = 0;
// grab in all parameters. We go around this loop until we've run out
// of named parameters AND we've parsed all the supplied arguments
while (!JSP_SHOULDNT_PARSE && lex->tk!=')') {
JsVar *param = jsvObjectIteratorGetKey(&it);
bool paramDefined = jsvIsFunctionParameter(param);
if (lex->tk!=')' || paramDefined) {
hadParams++;
JsVar *value = 0;
// ONLY parse this if it was supplied, otherwise leave 0 (undefined)
if (lex->tk!=')')
value = jspeAssignmentExpression();
// and if execute, copy it over
value = jsvSkipNameAndUnLock(value);
jsvAddFunctionParameter(functionRoot, paramDefined?jsvNewFromStringVar(param,1,JSVAPPENDSTRINGVAR_MAXLENGTH):0, value);
jsvUnLock(value);
if (lex->tk!=')') JSP_MATCH(',');
}
jsvUnLock(param);
if (paramDefined) jsvObjectIteratorNext(&it);
}
JSP_MATCH(')');
} else { // and NOT isParsing
int args = 0;
while (args<argCount) {
JsVar *param = jsvObjectIteratorGetKey(&it);
bool paramDefined = jsvIsFunctionParameter(param);
jsvAddFunctionParameter(functionRoot, paramDefined?jsvNewFromStringVar(param,1,JSVAPPENDSTRINGVAR_MAXLENGTH):0, argPtr[args]);
args++;
jsvUnLock(param);
if (paramDefined) jsvObjectIteratorNext(&it);
}
}
// Now go through what's left
while (jsvObjectIteratorHasValue(&it)) {
JsVar *param = jsvObjectIteratorGetKey(&it);
if (jsvIsString(param)) {
if (jsvIsStringEqual(param, JSPARSE_FUNCTION_SCOPE_NAME)) functionScope = jsvSkipName(param);
else if (jsvIsStringEqual(param, JSPARSE_FUNCTION_CODE_NAME)) functionCode = jsvSkipName(param);
#ifdef ESPR_JIT
else if (jsvIsStringEqual(param, JSPARSE_FUNCTION_JIT_CODE_NAME)) { functionCode = jsvSkipName(param); functionIsJIT = true; }
#endif
else if (jsvIsStringEqual(param, JSPARSE_FUNCTION_NAME_NAME)) functionInternalName = jsvSkipName(param);
else if (jsvIsStringEqual(param, JSPARSE_FUNCTION_THIS_NAME)) {
jsvUnLock(thisVar);
thisVar = jsvSkipName(param);
}
#ifndef ESPR_NO_LINE_NUMBERS
else if (jsvIsStringEqual(param, JSPARSE_FUNCTION_LINENUMBER_NAME)) functionLineNumber = (uint16_t)jsvGetIntegerAndUnLock(jsvSkipName(param));
#endif
else if (jsvIsFunctionParameter(param)) {
JsVar *defaultVal = jsvSkipName(param);
jsvAddFunctionParameter(functionRoot, jsvNewFromStringVar(param,1,JSVAPPENDSTRINGVAR_MAXLENGTH), defaultVal);
jsvUnLock(defaultVal);
}
}
jsvUnLock(param);
jsvObjectIteratorNext(&it);
}
jsvObjectIteratorFree(&it);
// setup a the function's name (if a named function)
if (functionInternalName) {
JsVar *name = jsvMakeIntoVariableName(jsvNewFromStringVarComplete(functionInternalName), function);
jsvAddName(functionRoot, name);
jsvUnLock2(name, functionInternalName);
}
if (!JSP_HAS_ERROR) {
// save old scopes and reset scope list
JsVar *oldScopeVar = execInfo.scopesVar;
execInfo.scopesVar = 0;
// if we have a scope var, load it up. We may not have one if there were no scopes apart from root
if (functionScope) {
jspeiLoadScopesFromVar(functionScope);
jsvUnLock(functionScope);
}
// add the function's execute space to the symbol table so we can recurse
if (jspeiAddScope(functionRoot)) {
/* Adding scope may have failed - we may have descended too deep - so be sure
* not to pull somebody else's scope off */
#ifndef ESPR_NO_LET_SCOPING
JsVar *oldBaseScope = execInfo.baseScope;
uint8_t oldBlockCount = execInfo.blockCount;
execInfo.baseScope = functionRoot;
execInfo.blockCount = 0;
#endif
JsVar *oldThisVar = execInfo.thisVar;
if (thisVar)
execInfo.thisVar = jsvRef(thisVar);
else
execInfo.thisVar = jsvRef(execInfo.root); // 'this' should always default to root
#ifdef ESPR_JIT
/* Handle the case where we're executing a JITed function. In this case functionCode
* points to assembly code...
*/
if (functionIsJIT) {
void *nativePtr = jsvGetFlatStringPointer(functionCode);
if (nativePtr)
returnVar = jsnCallFunction(nativePtr+1/*thumb*/, JSWAT_JSVAR/*JS Variable as return type*/, thisVar, NULL, 0);
} else
#endif
/* we just want to execute the block, but something could
* have messed up and left us with the wrong Lexer, so
* we want to be careful here... */
if (functionCode) {
#ifdef USE_DEBUGGER
bool hadDebuggerNextLineOnly = false;
if (execInfo.execute&EXEC_DEBUGGER_STEP_INTO) {
if (functionName)
jsiConsolePrintf("Stepping into %v\n", functionName);
else
jsiConsolePrintf("Stepping into function\n", functionName);
} else {
hadDebuggerNextLineOnly = execInfo.execute&EXEC_DEBUGGER_NEXT_LINE;
if (hadDebuggerNextLineOnly)
execInfo.execute &= (JsExecFlags)~EXEC_DEBUGGER_NEXT_LINE;
}
#endif
JsLex newLex;
JsLex *oldLex = jslSetLex(&newLex);
jslInit(functionCode);
#ifndef ESPR_NO_LINE_NUMBERS
newLex.lineNumberOffset = functionLineNumber;
#endif
JSP_SAVE_EXECUTE();
// force execute without any previous state
#ifdef USE_DEBUGGER
execInfo.execute = EXEC_YES | (execInfo.execute&(EXEC_CTRL_C_MASK|EXEC_ERROR_MASK|EXEC_DEBUGGER_NEXT_LINE));
#else
execInfo.execute = EXEC_YES | (execInfo.execute&(EXEC_CTRL_C_MASK|EXEC_ERROR_MASK));
#endif
if (jsvIsFunctionReturn(function)) {
#ifdef USE_DEBUGGER
// we didn't parse a statement so wouldn't trigger the debugger otherwise
if (execInfo.execute&EXEC_DEBUGGER_NEXT_LINE && JSP_SHOULD_EXECUTE) {
lex->tokenLastStart = lex->tokenStart;
jsiDebuggerLoop();
}
#endif
// implicit return - we just need an expression (optional)
if (lex->tk != ';' && lex->tk != '}')
returnVar = jsvSkipNameAndUnLock(jspeExpression());
} else {
// setup a return variable
JsVar *returnVarName = jsvAddNamedChild(functionRoot, 0, JSPARSE_RETURN_VAR);
// parse the whole block
#ifndef ESPR_NO_LET_SCOPING
execInfo.blockCount--; // jspeBlockNoBrackets immediately increments the block count
#endif
jspeBlockNoBrackets();
#ifndef ESPR_NO_LET_SCOPING
execInfo.blockCount++; // jspeBlockNoBrackets decrements the block count after
#endif
/* get the real return var before we remove it from our function.
* We can unlock below because returnVarName is still part of
* functionRoot, so won't get freed. */
returnVar = jsvSkipName(returnVarName);
if (returnVarName) { // could have failed with out of memory
jsvRemoveChildAndUnLock(functionRoot, returnVarName); // remove return value (helps stops circular references, saves RAM)
}
}
// Store a stack trace if we had an error
JsExecFlags hasError = execInfo.execute&EXEC_ERROR_MASK;
JSP_RESTORE_EXECUTE(); // because return will probably have set execute to false
#ifdef USE_DEBUGGER
bool calledDebugger = false;
if (execInfo.execute & EXEC_DEBUGGER_MASK) {
jsiConsolePrint("Value returned is =");
jsfPrintJSON(returnVar, JSON_LIMIT | JSON_SOME_NEWLINES | JSON_PRETTY | JSON_SHOW_DEVICES);
jsiConsolePrintChar('\n');
if (execInfo.execute & EXEC_DEBUGGER_FINISH_FUNCTION) {
calledDebugger = true;
jsiDebuggerLoop();
}
}
if (hadDebuggerNextLineOnly && !calledDebugger)
execInfo.execute |= EXEC_DEBUGGER_NEXT_LINE;
#endif
jslKill();
jslSetLex(oldLex);
if (hasError) {
execInfo.execute |= hasError; // propogate error
JsVar *stackTrace = jsvObjectGetChild(execInfo.hiddenRoot, JSPARSE_STACKTRACE_VAR, JSV_STRING_0);
if (stackTrace) {
jsvAppendPrintf(stackTrace, jsvIsString(functionName)?"in function %q called from ":
"in function called from ", functionName);
if (lex) {
jspAppendStackTrace(stackTrace);
} else
jsvAppendPrintf(stackTrace, "system\n");
jsvUnLock(stackTrace);
}
}
}
/* Return to old 'this' var. No need to unlock as we never locked before */
if (execInfo.thisVar) jsvUnRef(execInfo.thisVar);
execInfo.thisVar = oldThisVar;
#ifndef ESPR_NO_LET_SCOPING
jspeiRemoveScope();
execInfo.baseScope = oldBaseScope;
execInfo.blockCount = oldBlockCount;
#endif
}
// Unlock scopes and restore old ones
jsvUnLock(execInfo.scopesVar);
execInfo.scopesVar = oldScopeVar;
}
jsvUnLock2(functionCode, functionRoot);
}
jsvUnLock(thisVar);
/* It's possible that this call or a subcall may have compacted the filesystem
which changed the address the var in the iterator pointed to. If so we need to
copy it again. This should be quick-ish so lets' just do it rather than checking
whether we have compacted or not. https://github.com/espruino/Espruino/issues/2431 */
if (lex) jsvStringIteratorUpdatePtr(&lex->it);
return returnVar;
} else if (isParsing) { // ---------------------------------- function, but not executing - just parse args and be done
if (jspCheckStackPosition()) // check for stack overflow
jspeParseFunctionCallBrackets();
/* Do not return function, as it will be unlocked! */
return 0;
} else return 0;
}
// Find a variable (or built-in function) based on the current scopes
JsVar *jspGetNamedVariable(const char *tokenName) {
JsVar *a = JSP_SHOULD_EXECUTE ? jspeiFindInScopes(tokenName) : 0;
if (JSP_SHOULD_EXECUTE && !a) {
// We haven't found the variable, so check and see if it's one of our builtins...
a = jswFindBuiltInFunction(0, tokenName);
if (a) {
/* FIXME: Ideally we shouldn't have to add built-in objects to the global namespace here.
The issue is that we only add to the symbol table on write usually (to avoid wasting RAM)
but we do this using jsvCreateNewChild to reference the parent, but we only do it one level down.
So String.foo=5 works, but String.prototype.foo=5 won't add String to the global namespace
because jsvReplaceWithOrAddToRoot doesn't have any reference to `String`.
If we fix that we won't need to call jswIsBuiltInObject so we can avoid having to do a string search twice,
but it's not easy since by the time we get to the second '.' in jspeFactorMember we've forgotten the name
of the first object.
If we merge the real_prototype_chain branch we shouldn't need this either, which is probably the best option. */
if (jswIsBuiltInObject(tokenName)) {
// OPT: Could we instead have jswIsBuiltInObjectWithoutConstructor?
JsVar *name = jsvAddNamedChild(execInfo.root, a, tokenName);
jsvUnLock(a);
a = name;
}
} else {
/* Variable doesn't exist! JavaScript says we should create it
* (we won't add it here. This is done in the assignment operator)*/
a = jsvNewNameFromString(tokenName);
}
}
return a;
}
/// Used by jspGetNamedField / jspGetVarNamedField
static NO_INLINE JsVar *jspGetNamedFieldInParents(JsVar *object, const char* name, bool returnName) {
// Now look in prototypes
JsVar * child = jspeiFindChildFromStringInParents(object, name);
/* Check for builtins via separate function
* This way we save on RAM for built-ins because everything comes out of program code */
if (!child) {
child = jswFindBuiltInFunction(object, name);
}
/* We didn't get here if we found a child in the object itself, so
* if we're here then we probably have the wrong name - so for example
* with `a.b = c;` could end up setting `a.prototype.b` (bug #360)
*
* Also we might have got a built-in, which wouldn't have a name on it
* anyway - so in both cases, strip the name if it is there, and create