-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjsiEval.c
More file actions
1887 lines (1753 loc) · 69.7 KB
/
Copy pathjsiEval.c
File metadata and controls
1887 lines (1753 loc) · 69.7 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
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
/* The interpreter evaluation engine for jsi. */
#ifndef JSI_LITE_ONLY
#ifndef JSI_AMALGAMATION
#include "jsiInt.h"
#endif
#include <math.h>
/*#define USE_INLINE*/
#ifdef INLINE_ALL
#define INLINE inline
#else
#define INLINE
#endif
#define obj_this (interp->Obj_this)
#define stack (interp->Stack)
#define StackIdx(s) interp->Stack[s]
#define ObjThisIdx(s) interp->Obj_this[s]
#define TOP (interp->Stack[interp->Sp-1])
#define TOQ (interp->Stack[interp->Sp-2])
#define StackCopy(interp, l, r) Jsi_ValueCopy(interp, l, r)
static Jsi_Value** ValuesAlloc(Jsi_Interp *interp, int cnt, Jsi_Value**old, int oldsz) {
Jsi_Value **v, *vt, vs = VALINIT;
int i;
v = Jsi_Realloc(old, cnt* sizeof(Jsi_Value*));
for (i=oldsz; i<cnt; i++) {
v[i] = NULL; continue;
vt = Jsi_ValueNew1(interp);
*vt = vs;
v[i] = vt;
}
return v;
}
static void SetupStack(Jsi_Interp *interp)
{
int oldsz = interp->maxStack;
if (interp->maxStack)
interp->maxStack += STACK_INCR_SIZE;
else
interp->maxStack = STACK_INIT_SIZE;
stack = ValuesAlloc(interp, interp->maxStack, stack, oldsz);
obj_this = ValuesAlloc(interp, interp->maxStack, obj_this, oldsz);
}
static void Push(Jsi_Interp* interp, int n) {
int i = 0;
do {
if (!StackIdx(interp->Sp))
StackIdx(interp->Sp) = Jsi_ValueNew1(interp);
if (!ObjThisIdx(interp->Sp))
ObjThisIdx(interp->Sp) = Jsi_ValueNew1(interp);
if (i++ >= n) break;
interp->Sp++;
} while (1);
}
static void StackReplace(Jsi_Interp *interp, Jsi_Value *to, Jsi_Value *from) {
Jsi_ValueMakeUndef(interp, to);
*to = *from;
}
/* Before setting a value in the stack/obj, unlink any reference to it. */
static void ClearStack(register Jsi_Interp *interp, int ofs) {
Jsi_Value *v = StackIdx(interp->Sp-ofs);
if (!v) return;
#ifndef XX_NEWSTACK
Jsi_ValueReset(interp, v);
#else
if (v->refCnt<=1)
Jsi_ValueReset(interp, v);
else {
Jsi_DecrRefCount(interp, v);
StackIdx(interp->Sp-ofs) = Jsi_ValueNew1(interp);
}
#endif
}
static void ClearThis(register Jsi_Interp *interp, int ofs) {
Jsi_Value *v = ObjThisIdx(ofs);
if (!v) return;
#ifndef XX_NEWSTACK
Jsi_ValueReset(interp, v);
#else
if (v->refCnt<=1)
Jsi_ValueReset(interp, v);
else {
Jsi_DecrRefCount(interp, v);
ObjThisIdx(ofs) = Jsi_ValueNew1(interp);
}
#endif
}
/* interp->Sp[-2] = interp->Sp[-1]->var */
static void inline ValueAssign(Jsi_Interp *interp)
{
Jsi_Value *v, *dst = TOQ, *src = TOP;
if (dst->vt != JSI_VT_VARIABLE) {
Jsi_LogError("operand not a left value");
} else {
v = dst->d.lval;
SIGASSERT(v, VALUE);
if (v == src)
return;
if (v->f.bits.readonly) {
Jsi_LogWarn("ignore assign to readonly variable");
return;
}
Jsi_ValueCopy(interp,v, src);
SIGASSERT(v, VALUE);
}
}
/* pop n values from stack */
static INLINE void Pop(Jsi_Interp* interp, int n) {
int t = n;
while (t > 0) {
Assert((interp->Sp-t)>=0);
/* Jsi_Value *v = StackIdx(interp->Sp-t);
if (v->refCnt>1) puts("OO");*/
ClearStack(interp,t);
--t;
}
interp->Sp -= n;
}
/* Convert preceding stack variable(s) into value(s). */
static INLINE void VarDeref(Jsi_Interp* interp, int n) {
Assert(interp->Sp>=n);
int i;
for (i=1; i<=n; i++) {
Jsi_Value *vb = StackIdx(interp->Sp - i);
if (vb->vt == JSI_VT_VARIABLE) {
SIGASSERT(vb->d.lval, VALUE);
/*Jsi_IncrRefCount(interp, v); TODO: memory??? */
Jsi_ValueCopy(interp, vb, vb->d.lval);
}
}
}
#define common_math_opr(opr) { \
VarDeref(interp,2); \
if (!Jsi_ValueIsType(interp,TOP, JSI_VT_NUMBER)) Jsi_ValueToNumber(interp, TOP); \
if (!Jsi_ValueIsType(interp,TOQ, JSI_VT_NUMBER)) Jsi_ValueToNumber(interp, TOQ); \
TOQ->d.num = TOQ->d.num opr TOP->d.num; \
Pop(interp, 1); \
}
#define common_bitwise_opr(opr) { \
int a, b; \
VarDeref(interp,2); \
if (!Jsi_ValueIsType(interp,TOP, JSI_VT_NUMBER)) Jsi_ValueToNumber(interp, TOP); \
if (!Jsi_ValueIsType(interp,TOQ, JSI_VT_NUMBER)) Jsi_ValueToNumber(interp, TOQ); \
a = TOQ->d.num; b = TOP->d.num; \
TOQ->d.num = (Jsi_Number)(a opr b); \
Pop(interp, 1); \
}
static INLINE void logic_less(Jsi_Interp* interp, int i1, int i2) {
Jsi_Value *v1 = stack[interp->Sp-i1], *v2 = stack[interp->Sp-i2], *res = TOQ;
int val = 0;
Jsi_ValueToPrimitive(interp, v1);
Jsi_ValueToPrimitive(interp, v2);
if (v1->vt == JSI_VT_STRING && v2->vt == JSI_VT_STRING) {
int v1len = v1->d.s.len;
int v2len = v2->d.s.len;
if (v1len == -1 && v2len == -1) {
val = strcmp(v1->d.s.str, v2->d.s.str);
} else {
if (v1len < 0) v1len = strlen(v1->d.s.str);
if (v2len < 0) v1len = strlen(v2->d.s.str);
int mlen = v1len < v2len ? v1len : v2len;
/* TODO: use memcmp may cause bug */
val = memcmp(v1->d.s.str, v2->d.s.str, mlen * sizeof(char));
}
if (val > 0) val = 0;
else if (val < 0) val = 1;
else val = (v1len < v2len);
ClearStack(interp,2);
Jsi_ValueMakeBool(interp,res, val);
} else {
Jsi_ValueToNumber(interp, v1);
Jsi_ValueToNumber(interp, v2);
if (jsi_ieee_isnan(v1->d.num) || jsi_ieee_isnan(v2->d.num)) {
ClearStack(interp,2);
Jsi_ValueMakeUndef(interp,res);
} else {
val = (v1->d.num < v2->d.num);
ClearStack(interp,2);
Jsi_ValueMakeBool(interp,res, val);
}
}
}
static const char *vprint(Jsi_Value *v)
{
static char buf[100];
if (v->vt == JSI_VT_NUMBER) {
snprintf(buf, 100, "NUM:%" JSI_NUMGFMT " ", v->d.num);
} else if (v->vt == JSI_VT_BOOL) {
snprintf(buf, 100, "BOO:%d ", v->d.val);
} else if (v->vt == JSI_VT_STRING) {
snprintf(buf, 100, "STR:%s ", v->d.s.str);
} else if (v->vt == JSI_VT_VARIABLE) {
snprintf(buf, 100, "VAR:%x ", (int)v->d.lval);
} else if (v->vt == JSI_VT_NULL) {
snprintf(buf, 100, "NUL:null ");
} else if (v->vt == JSI_VT_OBJECT) {
snprintf(buf, 100, "OBJ:%x", (int)v->d.obj);
} else if (v->vt == JSI_VT_UNDEF) {
snprintf(buf, 100, "UND:undefined");
}
return buf;
}
typedef enum {
TL_TRY,
TL_WITH,
} ttype; /* type of try */
typedef struct TryList {
ttype type;
union {
struct { /* try data */
OpCode *tstart; /* try start ip */
OpCode *tend; /* try end ip */
OpCode *cstart; /* ...*/
OpCode *cend;
OpCode *fstart;
OpCode *fend;
int tsp;
enum {
LOP_NOOP,
LOP_THROW,
LOP_JMP
} last_op; /* what to do after finally block */
/* depend on last jmp code in catch block */
union {
OpCode *tojmp;
} ld; /* jmp out of catch (target)*/
} td;
struct { /* with data */
OpCode *wstart; /* with start */
OpCode *wend; /* with end */
} wd;
} d;
jsi_ScopeChain *scope_save; /* saved scope (used in catch block/with block)*/
Jsi_Value *curscope_save; /* saved current scope */
struct TryList *next;
} TryList;
/* destroy top of trylist */
#define pop_try(head) _pop_try(interp, &head)
static INLINE void _pop_try(Jsi_Interp* interp, TryList **head)
{
interp->tryDepth--;
TryList *t = (*head)->next;
Jsi_Free((*head));
(*head) = t;
}
#define push_try(head, n) _push_try(interp, &head, n)
static INLINE void _push_try(Jsi_Interp* interp, TryList **head, TryList *n)
{
interp->tryDepth++;
(n)->next = (*head);
(*head) = (n);
}
/* restore scope chain */
#define restore_scope() _restore_scope(interp, ps, trylist, \
&scope, ¤tScope, &context_id)
static INLINE void _restore_scope(Jsi_Interp* interp, jsi_Pstate *ps, TryList* trylist,
jsi_ScopeChain **scope, Jsi_Value **currentScope, int *context_id) {
/* restore_scope(scope_save, curscope_save)*/
if (*scope != (trylist->scope_save)) {
jsi_ScopeChainFree(interp, *scope);
*scope = (trylist->scope_save);
interp->ingsc = *scope;
}
if (*currentScope != (trylist->curscope_save)) {
Jsi_DecrRefCount(interp, *currentScope);
*currentScope = (trylist->curscope_save);
interp->incsc = *currentScope;
}
*context_id = ps->_context_id++;
}
#define do_throw() if (_do_throw(interp, ps, &ip, &trylist,&scope, ¤tScope, &context_id, (interp->Sp?TOP:NULL)) != JSI_OK) { rc = JSI_ERROR; break; }
static int _do_throw(Jsi_Interp *interp, jsi_Pstate *ps, OpCode **ipp, TryList **tlp,
jsi_ScopeChain **scope, Jsi_Value **currentScope, int *context_id, Jsi_Value *top) {
if (Jsi_InterpGone(interp)) return JSI_ERROR;
TryList *trylist = *tlp;
while (1) {
if (trylist == NULL) {
char *str = (top?Jsi_ValueString(interp, top, NULL):"");
if (str)
Jsi_LogError("%s", str);
return JSI_ERROR;
}
if (trylist->type == TL_TRY) {
int n = interp->Sp - trylist->d.td.tsp;
Pop(interp, n);
if (*ipp >= trylist->d.td.tstart && *ipp < trylist->d.td.tend) {
*ipp = trylist->d.td.cstart - 1;
break;
} else if (*ipp >= trylist->d.td.cstart && *ipp < trylist->d.td.cend) {
trylist->d.td.last_op = LOP_THROW;
*ipp = trylist->d.td.fstart - 1;
break;
} else if (*ipp >= trylist->d.td.fstart && *ipp < trylist->d.td.fend) {
_pop_try(interp, tlp);
trylist = *tlp;
} else Jsi_LogBug("Throw within a try, but not in its scope?");
} else {
_restore_scope(interp, ps, trylist, scope, currentScope, context_id);
_pop_try(interp, tlp);
trylist = *tlp;
}
}
return JSI_OK;
}
static TryList *trylist_new(ttype t, jsi_ScopeChain *scope_save, Jsi_Value *curscope_save)
{
TryList *n = Jsi_Calloc(1,sizeof(*n));
n->type = t;
n->curscope_save = curscope_save;
/*Jsi_IncrRefCount(interp, curscope_save);*/
n->scope_save = scope_save;
return n;
}
static void DumpInstr(Jsi_Interp *interp, jsi_Pstate *ps, Jsi_Value *_this,
TryList *trylist, OpCode *ip, OpCodes *opcodes)
{
int i;
if (0 && interp->debug <= 0xf) {
return;
if (ip->op != OP_FCALL) return;
const char *s = interp->lastPushStr;
const char *ff, *fname = ip->fname?ip->fname:"";
if (interp->debug<0xf && ((ff=strrchr(fname,'/'))))
fname = ff+1;
printf("CALL: %s() in %s:%d\n", s?s:"", fname, ip->line);
return;
}
printf("STACK%d: ", interp->Sp);
for (i = 0; i < interp->Sp; ++i) {
printf("%s ", vprint(StackIdx(i)));
}
printf("\tthis: %s ", vprint(_this));
TryList *tlt = trylist;
for (i = 0; tlt; tlt = tlt->next) i++;
printf("TL: %d, excpt: %s\n", i, vprint(&ps->last_exception));
jsi_code_decode(ip, ip - opcodes->codes);
}
static int cmpstringp(const void *p1, const void *p2)
{
return strcmp(* (char * const *) p1, * (char * const *) p2);
}
static void SortDString(Jsi_Interp *interp, Jsi_DString *dStr) {
int argc, i;
char **argv;
Jsi_DString sStr;
Jsi_DSInit(&sStr);
Jsi_SplitStr(Jsi_DSValue(dStr), &argc, &argv, " ", &sStr);
qsort(argv, argc, sizeof(char*), cmpstringp);
Jsi_DSFree(dStr);
Jsi_DSInit(dStr);
for (i=0; i<argc; i++) {
Jsi_DSAppend(dStr, (i?" ":""), argv[i], NULL);
}
Jsi_DSFree(&sStr);
}
static void ValueObjDelete(Jsi_Interp *interp, Jsi_Value *target, Jsi_Value *key, int force)
{
const char *kstr;
if (target->vt != JSI_VT_OBJECT) return;
Jsi_ValueToString(interp, key);
kstr = key->d.s.str;
Jsi_TreeEntry *hPtr;
if (!key->f.bits.isstrkey) {
Jsi_HashEntry *hePtr = Jsi_HashEntryFind(target->d.obj->tree->interp->strKeyTbl, key->d.s.str);
if (hePtr)
kstr = Jsi_HashKeyGet(hePtr);
}
hPtr = Jsi_TreeEntryFind(target->d.obj->tree, kstr);
if (hPtr == NULL || (hPtr->f.bits.dontdel && !force))
return;
Jsi_TreeEntryDelete(hPtr);
}
static void ObjGetNames(Jsi_Interp *interp, Jsi_Obj *obj, Jsi_DString* dStr, int flags) {
Jsi_TreeEntry *hPtr;
Jsi_TreeSearch srch;
Jsi_Value *v;
int m = 0;
Jsi_DSInit(dStr);
if (obj->isArray)
obj = interp->Array_prototype->d.obj;
for (hPtr=Jsi_TreeSearchFirst(obj->tree, &srch, JSI_TREE_INORDER); hPtr; hPtr=Jsi_TreeSearchNext(&srch)) {
v = Jsi_TreeValueGet(hPtr);
if (!v) continue;
if ((flags&JSI_NAME_FUNCTIONS) && !Jsi_ValueIsFunction(interp,v)) {
continue;
}
if ((flags&JSI_NAME_DATA) && Jsi_ValueIsFunction(interp,v)) {
continue;
}
Jsi_DSAppend(dStr, (m++?" ":""), Jsi_TreeKeyGet(hPtr), NULL);
}
Jsi_TreeSearchDone(&srch);
}
static void DumpFunctions(Jsi_Interp *interp) {
Jsi_DString dStr;
Jsi_DSInit(&dStr);
Jsi_HashEntry *hPtr;
Jsi_HashSearch search;
Jsi_CmdSpecItem *csi = NULL;
Jsi_CmdSpec *cs;
Jsi_Value *lsf = &interp->lastSubscriptFail;
const char *spnam = "";
int m = 0;
if (interp->lastFuncIndex) {
if (interp->lastFuncIndex->cmdSpec)
spnam = interp->lastFuncIndex->cmdSpec->name;
} else if (lsf->vt == JSI_VT_OBJECT) {
spnam = interp->lastSubscriptFailStr;
if (!spnam) spnam = interp->lastPushStr;
if (lsf->d.obj->ot == JSI_OT_USEROBJ && lsf->d.obj->d.uobj->reg && lsf->d.obj->d.uobj->interp == interp) {
cs = lsf->d.obj->d.uobj->reg->spec;
if (cs)
goto dumpspec;
} else if (lsf->d.obj->ot == JSI_OT_OBJECT) {
ObjGetNames(interp, lsf->d.obj, &dStr, JSI_NAME_FUNCTIONS);
Jsi_LogError("'%s', functions are: %s.",
spnam, Jsi_DSValue(&dStr));
Jsi_DSFree(&dStr);
return;
} else {
const char *sustr = NULL;
switch (lsf->d.obj->ot) {
case JSI_OT_STRING: sustr = "String"; break;
case JSI_OT_NUMBER: sustr = "Number"; break;
case JSI_OT_BOOL: sustr = "Boolean"; break;
}
if (sustr) {
hPtr = Jsi_HashEntryFind(interp->cmdSpecTbl, sustr);
csi = Jsi_HashValueGet(hPtr);
cs = csi->spec;
if (!spnam[0])
spnam = sustr;
goto dumpspec;
}
}
}
if (!*spnam) {
for (hPtr = Jsi_HashEntryFirst(interp->cmdSpecTbl, &search);
hPtr; hPtr = Jsi_HashEntryNext(&search)) {
csi = Jsi_HashValueGet(hPtr);
if (csi->name && csi->name[0])
Jsi_DSAppend(&dStr, (m++?" ":""), csi->name, NULL);
}
}
if ((hPtr = Jsi_HashEntryFind(interp->cmdSpecTbl, spnam))) {
csi = Jsi_HashValueGet(hPtr);
while (csi) {
int n;
cs = csi->spec;
dumpspec:
n = 0;
while (cs->name) {
if (n != 0 || !(cs->flags & JSI_CMD_IS_CONSTRUCTOR)) {
if (!*cs->name) continue;
Jsi_DSAppend(&dStr, (m?" ":""), cs->name, NULL);
n++; m++;
}
cs++;
}
csi = (csi?csi->next:NULL);
}
SortDString(interp, &dStr);
if (*spnam==0 && interp->lastPushVar)
spnam = interp->lastPushVar;
Jsi_LogError("'%s' is not one of: %s.",
spnam, Jsi_DSValue(&dStr));
Jsi_DSFree(&dStr);
} else {
Jsi_LogError("can not execute expression: '%s' not a function\n",
interp->lastPushVar ? interp->lastPushVar : "");
}
}
/* Attempt to dynamically load function XX by doing an eval of jsiIndex.XX */
/* TODO: prevent infinite loop/recursion. */
static Jsi_Value *LoadFunction(Jsi_Interp *interp, const char *str, Jsi_Value *tret) {
Jsi_DString dStr = {};
Jsi_Value *v;
int i;
for (i=0; i<2; i++) {
Jsi_DSAppend(&dStr, "jsiIndex.", str, NULL);
Jsi_VarLookup(interp, Jsi_DSValue(&dStr));
v = Jsi_NameLookup(interp, Jsi_DSValue(&dStr));
Jsi_DSFree(&dStr);
if (v) {
const char *cp = Jsi_ValueGetDString(interp, v, &dStr, 0);
/*printf("JSS: %s\n", cp);*/
v = NULL;
if (Jsi_EvalString(interp, cp, 0) == JSI_OK)
v = Jsi_NameLookup(interp, str);
Jsi_DSFree(&dStr);
if (v) {
tret = v;
break;
}
}
if (interp->indexLoaded++ || i>0)
break;
/* Index not in memory, so try loading jsiIndex from the file jsiIndex.jsi */
if (interp->indexFiles == NULL)
return tret;
Jsi_Value **ifs = &interp->indexFiles;
int i, ifn = 1;
if (Jsi_ValueIsArray(interp, interp->indexFiles)) {
ifs = interp->indexFiles->d.obj->arr;
ifn = interp->indexFiles->d.obj->arrCnt;
}
for (i=0; i<ifn; i++) {
if (Jsi_EvalFile(interp, ifs[i], 0) != JSI_OK)
break;
interp->indexLoaded++;
}
}
interp->lastPushVar = (char*)str;
return tret;
}
static INLINE int EvalFunction(register jsi_Pstate *ps, OpCode *ip, int discard) {
int excpt_ret = JSI_OK;
register Jsi_Interp *interp = ps->interp;
int as_constructor = (ip->op == OP_NEWFCALL);
int stackargc = (int)ip->data;
VarDeref(interp, stackargc + 1);
int tocall_index = interp->Sp - stackargc - 1, adds;
Jsi_Value *tocall = StackIdx(tocall_index);
char* failStr = interp->lastSubscriptFailStr;
if (Jsi_ValueIsUndef(interp, tocall) && interp->lastPushVar && failStr == 0) {
tocall = LoadFunction(interp, interp->lastPushVar, tocall);
interp->curIp = ip;
}
if (!Jsi_ValueIsFunction(interp, tocall)) {
DumpFunctions(interp);
excpt_ret = JSI_ERROR;
interp->lastSubscriptFailStr = NULL;
goto empty_func;
}
interp->lastSubscriptFailStr = NULL;
interp->lastFuncIndex = NULL;
if (!tocall->d.obj->d.fobj) { /* empty function */
empty_func:
Pop(interp, stackargc);
ClearStack(interp,1);
Jsi_ValueMakeUndef(interp,TOP);
} else {
Jsi_Func *fstatic = tocall->d.obj->d.fobj->func;
if (interp->nDebug && fstatic->callback == jsi_AssertCmd)
goto empty_func;
adds = fstatic->callflags.bits.addargs;
fstatic->callflags.bits.addargs = 0;
if (adds && (fstatic->cmdSpec->flags&JSI_CMDSPEC_NONTHIS))
adds = 0;
/* create new scope, prepare arguments */
/* here we shared scope and 'arguments' with the same object */
/* so that arguments[0] is easier to shared space with first local variable */
Jsi_Value *newscope = Jsi_ValueNew1(interp);
Jsi_Obj *ao = Jsi_ObjNewArray(interp, stack+(interp->Sp - stackargc), stackargc);
if (fstatic->bindArgs) {
int nc, bargc = Jsi_ValueGetLength(interp, fstatic->bindArgs)-1;
if (bargc > 0) {
jsi_ArraySizer(interp, ao, nc=(ao->arrCnt+bargc));
memmove(ao->arr+bargc, ao->arr, sizeof(Jsi_Value*)*ao->arrCnt);
memmove(ao->arr, fstatic->bindArgs->d.obj->arr+1, sizeof(Jsi_Value*)*bargc);
ao->arrCnt = nc;
}
}
Jsi_ValueMakeObject(interp, newscope, ao);
newscope->d.obj->__proto__ = interp->Object_prototype; /* ecma */
jsi_SharedArgs(interp, newscope, fstatic->argnames, 1); /* make arg vars to shared arguments */
jsi_InitLocalVar(interp, newscope, fstatic);
jsi_SetCallee(interp, newscope, tocall);
Pop(interp, stackargc);
Jsi_Value newthis = VALINIT;
if (fstatic->bindArgs && Jsi_ValueTypeGet(fstatic->bindArgs->d.obj->arr[0]) != JSI_VT_UNDEF) {
Jsi_ValueCopy(interp,&newthis, Jsi_ValueArrayIndex(interp, fstatic->bindArgs, 0));
} else if (ObjThisIdx(tocall_index)->vt == JSI_VT_OBJECT) {
Jsi_ValueCopy(interp,&newthis, ObjThisIdx(tocall_index));
ClearThis(interp, tocall_index);
} else {
Jsi_ValueCopy(interp,&newthis, interp->Top_object);
}
if (as_constructor) { /* new Constructor */
Jsi_Obj *newobj = Jsi_ObjNewType(interp, JSI_OT_OBJECT);
Jsi_Value *proto = Jsi_ValueObjLookup(interp, tocall, "prototype", 0);
if (proto && proto->vt == JSI_VT_OBJECT)
newobj->__proto__ = proto;
Jsi_ValueReset(interp, &newthis);
Jsi_ValueMakeObject(interp, &newthis, newobj);
/* TODO: constructor specifics??? */
}
if (interp->traceCalls && fstatic->name) {
const char *ff, *fname = ip->fname?ip->fname:"";
if (interp->debug<0xf && ((ff=strrchr(fname,'/'))))
fname = ff+1;
if (interp->traceHook)
(*interp->traceHook)(interp, fstatic->name, ip->fname, ip->line, fstatic->cmdSpec, &newthis, newscope);
else
printf("%*s<#%d>: %s() in %s:%d\n", (interp->level-1)*2, "", interp->level, fstatic->name, fname, ip->line);
}
Jsi_Value spret = VALINIT;
Jsi_Value *spretPtr = &spret;
if (fstatic->type == FC_NORMAL) {
excpt_ret = jsi_evalcode(ps, fstatic->opcodes, tocall->d.obj->d.fobj->scope,
newscope, &newthis, &spretPtr);
interp->funcCallCnt++;
} else if (!fstatic->callback) {
Jsi_LogError("can not call:\"%s()\"", fstatic->name);
} else {
int docall = 1, oldcf = fstatic->callflags.i;
fstatic->callflags.bits.iscons = (as_constructor?JSI_CALL_CONSTRUCTOR:0);
if (fstatic->f.bits.hasattr)
{
#define SPTR(s) (s?s:"")
if ((fstatic->f.bits.isobj) && newthis.vt != JSI_VT_OBJECT) {
excpt_ret = JSI_ERROR;
docall = 0;
Jsi_LogError("'this' is not object: \"%s()\"", fstatic->name);
} else if ((!(fstatic->f.bits.iscons)) && as_constructor) {
excpt_ret = JSI_ERROR;
docall = 0;
Jsi_LogError("can not call as constructor: \"%s()\"", fstatic->name);
} else {
int aCnt = Jsi_ValueGetLength(interp, newscope);
if (aCnt<(fstatic->cmdSpec->minArgs+adds)) {
Jsi_LogError("missing args, expected \"%s(%s)\" ", fstatic->cmdSpec->name, SPTR(fstatic->cmdSpec->argStr));
excpt_ret = JSI_ERROR;
docall = 0;
} else if (fstatic->cmdSpec->maxArgs>=0 && (aCnt>fstatic->cmdSpec->maxArgs+adds)) {
Jsi_LogError("extra args, expected \"%s(%s)\" ", fstatic->cmdSpec->name, SPTR(fstatic->cmdSpec->argStr));
excpt_ret = JSI_ERROR;
docall = 0;
}
}
}
if (docall) {
fstatic->callflags.bits.isdiscard = discard;
excpt_ret = fstatic->callback(interp, newscope,
&newthis, &spretPtr, fstatic);
interp->cmdCallCnt++;
}
fstatic->callflags.i = oldcf;
}
if (as_constructor) {
if (newthis.vt == JSI_VT_OBJECT)
newthis.d.obj->constructor = tocall->d.obj;
if (spret.vt != JSI_VT_OBJECT) {
Jsi_ValueReset(interp,&spret);
Jsi_ValueCopy(interp,&spret, &newthis);
}
}
jsi_SharedArgs(interp, newscope, fstatic->argnames, 0); /* make arg vars to shared arguments */
Jsi_ValueReset(interp,&newthis);
ClearStack(interp,1);
if (spretPtr == &spret)
StackReplace(interp, TOP, &spret);
else {
/*TODO: support c called extension returning a (non-copied) value reference?*/
Jsi_DecrRefCount(interp, TOP);
*TOP = *spretPtr;
}
Jsi_DecrRefCount(interp, newscope);
}
return excpt_ret;
}
static INLINE void PushVar(jsi_Pstate *ps, OpCode *ip, jsi_ScopeChain *scope, Jsi_Value *currentScope, int context_id) {
register Jsi_Interp *interp = ps->interp;
FastVar *n = ip->data;
SIGASSERT(n,FASTVAR);
Jsi_Value *dv = StackIdx(interp->Sp), *v = NULL;
int isglob = 0;
if (n->context_id == context_id && n->ps == ps) {
v = n->var.lval;
} else {
char *varname = n->var.varname;
if (*varname == 'n' && Jsi_Strcmp(varname,"null") == 0)
v = interp->NullValue;
else {
v = Jsi_ValueObjLookup(interp, currentScope, varname, 1);
}
if (!v)
v = jsi_ScopeChainObjLookupUni(scope, varname);
if (!v) {
/* add to global scope. TODO: do not define if a right_val??? */
Jsi_Value *global_scope = scope->chains_cnt > 0 ? scope->chains[0]:currentScope;
Jsi_Value key = VALINIT;
Jsi_ValueMakeStringKey(interp, &key, varname);
interp->lastPushVar = key.d.s.str;
v = jsi_ValueObjKeyAssign(interp, global_scope, &key, NULL, JSI_OM_DONTENUM);
int isNew;
Jsi_HashEntry *hPtr = Jsi_HashEntryCreate(interp->varTbl, varname, &isNew);
if (hPtr && isNew)
Jsi_HashValueSet(hPtr, 0);
}
Jsi_IncrRefCount(interp, v);
if (v->vt == JSI_VT_OBJECT && v->d.obj->ot == JSI_OT_OBJECT)
v->f.bits.onstack = 1; /* Indicate that a double free is required for object. */
n->context_id = context_id;
if (n->var.lval != v) {
n->isglob = isglob;
n->var.lval = v;
SIGASSERT(v, VALUE);
}
}
if (dv->vt != JSI_VT_VARIABLE || dv->d.lval != v) {
if (dv->vt != JSI_VT_VARIABLE)
Jsi_ValueReset(interp,dv);
dv->vt = JSI_VT_VARIABLE;
SIGASSERT(v, VALUE);
dv->d.lval = v;
}
SIGASSERT(v, VALUE);
Push(interp,1);
}
static INLINE void PushFunc(jsi_Pstate *ps, OpCode *ip, jsi_ScopeChain *scope, Jsi_Value *currentScope) {
/* TODO: now that we're caching ps, may need to reference function ps for context_id??? */
Jsi_Interp *interp = ps->interp;
Jsi_FuncObj *fo = jsi_FuncObjNew(interp, (Jsi_Func *)ip->data);
fo->scope = jsi_ScopeChainDupNext(interp, scope, currentScope);
Jsi_Obj *obj = Jsi_ObjNewType(interp, JSI_OT_FUNCTION);
obj->d.fobj = fo;
Jsi_Value *v = StackIdx(interp->Sp), *fun_prototype = jsi_ObjValueNew(interp);
fun_prototype->d.obj->__proto__ = interp->Object_prototype;
Jsi_ValueMakeObject(interp, v, obj);
Jsi_ValueInsert(interp, v, "prototype", fun_prototype, JSI_OM_DONTDEL|JSI_OM_DONTENUM);
/* TODO: make own prototype and prototype.constructor */
if (interp->lastPushVar && interp->Sp == 1 && StackIdx(0)->vt == JSI_VT_VARIABLE) {
int isNew;
char *key = interp->lastPushVar;
Jsi_HashEntry *hPtr;
hPtr = Jsi_HashEntryCreate(interp->funcTbl, key, &isNew);
if (hPtr && isNew) {
if (!fo->func->name)
fo->func->name = interp->lastPushVar;
Jsi_HashValueSet(hPtr, obj);
hPtr = Jsi_HashEntryCreate(interp->varTbl, key, &isNew);
if (hPtr)
Jsi_HashValueSet(hPtr, obj);
}
}
Push(interp,1);
}
int _jsi_evalcode(register jsi_Pstate *ps, OpCodes *opcodes,
jsi_ScopeChain *scope, Jsi_Value *currentScope,
Jsi_Value *_this, Jsi_Value *vret)
{
register Jsi_Interp* interp = ps->interp;
OpCode *ip = &opcodes->codes[0];
int rc = JSI_OK, context_id = ps->_context_id++;
OpCode *end = &opcodes->codes[opcodes->code_len];
TryList *trylist = NULL;
if (currentScope->vt != JSI_VT_OBJECT) {
Jsi_LogBug("Eval: current scope is not a object\n");
return JSI_ERROR;
}
while(ip < end && rc == JSI_OK) {
if (interp->exited) {
rc = JSI_ERROR;
break;
}
interp->opCnt++;
if (interp->maxOpCnt && interp->maxOpCnt > interp->opCnt) {
Jsi_LogError("Execution cap exceeded");
rc = JSI_ERROR;
}
if (interp->level > interp->maxDepth) {
Jsi_LogError("Infinite recursion detected?");
rc = JSI_ERROR;
break;
}
if (interp->debug) {
DumpInstr(interp, ps, _this, trylist, ip, opcodes);
}
#ifndef USE_STATIC_STACK
if ((interp->maxStack-interp->Sp)<STACK_MIN_PAD)
SetupStack(interp);
#endif
Push(interp,0);
interp->curIp = ip;
if (interp->evalHook && (*interp->evalHook)(interp, interp->curFile, interp->curIp->line) != JSI_OK) {
rc = JSI_ERROR;
break;
}
switch(ip->op) {
case OP_NOP:
case OP_LASTOP:
break;
case OP_PUSHUND:
Jsi_ValueMakeUndef(interp,StackIdx(interp->Sp));
Push(interp,1);
break;
case OP_PUSHBOO:
Jsi_ValueMakeBool(interp,StackIdx(interp->Sp), (int)ip->data);
Push(interp,1);
break;
case OP_PUSHNUM:
Jsi_ValueMakeNumber(interp,StackIdx(interp->Sp), (*((Jsi_Number *)ip->data)));
Push(interp,1);
break;
case OP_PUSHSTR: {
interp->lastFuncIndex = NULL;
Jsi_ValueMakeStringKey(interp,StackIdx(interp->Sp), ip->data);
interp->lastPushStr = StackIdx(interp->Sp)->d.s.str;
Push(interp,1);
break;
}
case OP_PUSHVAR: {
PushVar(ps, ip, scope, currentScope, context_id);
break;
}
case OP_PUSHFUN: {
PushFunc(ps, ip, scope, currentScope);
break;
}
case OP_NEWFCALL:
if (interp->maxUserObjs && interp->userObjCnt > interp->maxUserObjs) {
Jsi_LogError("Max 'new' count exceeded");
rc = JSI_ERROR;
break;
}
case OP_FCALL: {
/* TODO: need reliable way to capture func string name to handle unknown functions.*/
int discard = ((ip+2)<end && ip[1].op == OP_POP);
if (EvalFunction(ps, ip, discard) != JSI_OK) { /* throw an execption */
do_throw();
}
/* TODO: new Function return a function without scopechain, add here */
break;
}
case OP_SUBSCRIPT: {
Jsi_Value *src = TOQ, *idx = TOP;
VarDeref(interp,2);
Jsi_Value res = VALINIT;
if (Jsi_ValueIsString(interp, src) && Jsi_ValueIsNumber(interp, idx)) {
int sLen;
char bbuf[10], *cp = Jsi_ValueString(interp, src, &sLen);
int n = (int)idx->d.num;
if (n<0 || n>=sLen) {
Jsi_ValueMakeUndef(interp, src);
} else {
bbuf[1] = 0;
bbuf[0] = cp[n];
Jsi_ValueMakeStringDup(interp, src, bbuf);
}
Pop(interp, 1);
break;
}
Jsi_ValueToObject(interp, src);
if (interp->hasCallee && src->d.obj == currentScope->d.obj) {
if (idx->vt == JSI_VT_STRING && Jsi_Strcmp(idx->d.s.str, "callee") == 0) {
ClearStack(interp,1);
Jsi_ValueMakeStringKey(interp, idx, "\1callee\1");
}
}
if (interp->lastSubscriptFail.vt != JSI_VT_UNDEF) {
Jsi_ValueReset(interp, &interp->lastSubscriptFail);
interp->lastSubscriptFailStr = NULL;
}
if (src->vt != JSI_VT_UNDEF) {
jsi_ValueSubscriptLen(interp, src, idx, &res, (int)ip->data);
int isfcall = ((ip+3)<end && ip[2].op == OP_FCALL);
if (res.vt == JSI_VT_UNDEF && isfcall) {
/* eg. so we can list available commands for "db.xx()" */
Jsi_ValueCopy(interp, &interp->lastSubscriptFail, src);
if (idx->vt == JSI_VT_STRING)
interp->lastSubscriptFailStr = idx->d.s.str;
}
ClearStack(interp,2);
StackCopy(interp, src, &res); /*TODO: is this right*/
}
Pop(interp, 1);
break;
}
case OP_PUSHREG: {
Jsi_Obj *obj = Jsi_ObjNewType(interp, JSI_OT_REGEXP);
obj->d.robj = (Jsi_Regex *)ip->data;
Jsi_ValueMakeObject(interp,StackIdx(interp->Sp), obj);
Push(interp,1);
break;
}
case OP_PUSHARG:
Jsi_ValueCopy(interp,StackIdx(interp->Sp), currentScope);
Jsi_ObjIncrRefCount(interp, currentScope->d.obj);
Push(interp,1);
break;
case OP_PUSHTHS:
Jsi_ValueCopy(interp,StackIdx(interp->Sp), _this);
Jsi_ObjIncrRefCount(interp, _this->d.obj);
Push(interp,1);
break;
case OP_PUSHTOP:
Jsi_ValueCopy(interp,StackIdx(interp->Sp), TOP);
Push(interp,1);
break;
case OP_UNREF:
VarDeref(interp,1);
break;
case OP_PUSHTOP2:
Jsi_ValueCopy(interp, StackIdx(interp->Sp), TOQ);
Jsi_ValueCopy(interp, StackIdx(interp->Sp+1), TOP);
Push(interp, 2);
break;
case OP_CHTHIS: {
if (ip->data) {
int t = interp->Sp - 2;
Assert(t>=0);
Jsi_Value *v = ObjThisIdx(t);
ClearThis(interp, t);
Jsi_ValueCopy(interp, v, TOQ);
if (v->vt == JSI_VT_VARIABLE) {
Jsi_ValueCopy(interp, v, v->d.lval);
}
Jsi_ValueToObject(interp, v);
}
break;
}
case OP_LOCAL: {
Jsi_Value key = VALINIT;
Jsi_ValueMakeString(interp, &key, ip->data);
jsi_ValueObjKeyAssign(interp, currentScope, &key, NULL, JSI_OM_DONTENUM);
/* make all FastVar to be relocated */
context_id = ps->_context_id++;
break;
}
case OP_POP:
if ((interp->evalFlags&JSI_EVAL_RETURN) && (ip+1) >= end &&
(Jsi_ValueIsObjType(interp, TOP, JSI_OT_ITER)==0 &&
Jsi_ValueIsObjType(interp, TOP, JSI_OT_FUNCTION)==0)) {
/* Interactive and last instruction is a pop: save result. */
StackCopy(interp, vret,TOP); /*TODO: correct?*/
TOP->vt = JSI_VT_UNDEF;
}
Pop(interp, (int)ip->data);
break;
case OP_NEG:
VarDeref(interp,1);
Jsi_ValueToNumber(interp, TOP);
TOP->d.num = -(TOP->d.num);
break;