-
Notifications
You must be signed in to change notification settings - Fork 64
/
escodegen.js
executable file
·3511 lines (3066 loc) · 121 KB
/
escodegen.js
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
(function () {
'use strict';
var Syntax,
Precedence,
BinaryPrecedence,
SourceNode,
estraverse,
esutils,
base,
indent,
json,
renumber,
hexadecimal,
quotes,
escapeless,
newline,
space,
parentheses,
semicolons,
safeConcatenation,
directive,
extra,
parse,
sourceMap,
sourceCode,
preserveBlankLines,
FORMAT_MINIFY,
FORMAT_DEFAULTS;
estraverse = require('estraverse');
esutils = require('esutils');
Syntax = estraverse.Syntax;
//******************************添加的编译代码 开始**********************************
var my_stack = [] //临时栈
var my_varIDX = [] //变量索引栈
var tier = 1 //层数默认为100,
var esp = 15 //指针, 因为未考虑函数调用层级, 所以每次调用都会拷贝变量取, 设置15起始, 避免被参数覆盖,
var vm_str = ''
var tempIDXCount = 0 //下面数组的索引
var tempIDX = [] //临时记录的index数组
var ifESIDXCount = 0
var ifESIDX = []
var func_retn_count = []; //函数返回值数量计数
var OPCODE = {
RETURN:0,
PUSH_NOT_STACKTOP:1,
PUSH_VALUE:14,
EXPRESSION:66,
PUSH_NUM : 73,
PUSH_FLOAT_VALUE : 95,
MOV_VARS: 83,
MY_MOV_VARS: 94,
PUSH_VAR : 76,
PUSH_FUNC : 89,
PUSH_STACK_ARRAY : 59,
APPLY : 67,
PUSH0 : 70,
PUSH_NULL : 90,
IS_TURE : 75,
SKIP_BLOCK : 74,
EXPRESSION2 : 68,
PUSH_NOT_STACK_TOP : 1,
PUSH_THIS : 71,
GET_OBJ : 82,
NEW_OBJ : 78,
PUSH_SUBSTR : 4,
GET_ARY_VALUE : 96,
SET_ARY_VALUE : 97,
EXPRESSION_SINGLE : 98,
XOR_DECODE : 12,
SET_GLOBAL_VALUE : 99,
}
//getIDX
function getIDX(var_name)
{
if(var_name == 'length')
{
return undefined
}
for(var i = tier; i > 0; i--)
{
var temp = my_varIDX['$'+ i][var_name];//先检查普通
if(temp != undefined){
if(typeof(temp) != 'number')
return undefined
return temp;
}
}
}
//push obj
function push_obj(stmt)
{
var object_name = stmt.name
var IDX = getIDX(object_name)
if(IDX == undefined)
{
vm_str+=String.fromCharCode(OPCODE.PUSH_THIS + 32);
vm_str+=String.fromCharCode(OPCODE.GET_OBJ + 32);
vm_str+=String.fromCharCode(object_name.length + 32);
vm_str+=object_name;
}
else
{
push_var(IDX)
}
}
//push 运算符
function push_symbol(stmt, left_name)
{
if(stmt.operator.length > 1)
{
vm_str+=String.fromCharCode(OPCODE.EXPRESSION2 + 32);
vm_str+=String.fromCharCode(stmt.operator.length + 32);//计算符号大于1
vm_str+=stmt.operator;
if(!is_clac_bool(stmt.operator))
{
if(stmt.left.type == 'MemberExpression')
member_mov(stmt)
else
mov_var(getIDX(left_name))
}
}
else if(stmt.type == 'UnaryExpression')
{
vm_str+=String.fromCharCode(OPCODE.EXPRESSION_SINGLE + 32);
vm_str+=stmt.operator;
}
else
{
vm_str+=String.fromCharCode(OPCODE.EXPRESSION + 32);
vm_str+=stmt.operator;
}
}
//用于递归调用的计算函数
function myExpression(stmt, clac_tier)
{
var push_num;
if(stmt.type == 'BinaryExpression' || stmt.type == 'AssignmentExpression')
{
//递归调用
myExpression(stmt.left, clac_tier+1)
myExpression(stmt.right, clac_tier+1)
//计算
push_symbol(stmt, stmt.left.name)
}
else
op_num_type_push(stmt);
}
function myUpdateExpression(stmt)
{
if(stmt.operator == '++' || stmt.operator == '--')
{
vm_str+=String.fromCharCode(OPCODE.PUSH_VALUE + 32);
vm_str+=String.fromCharCode(1 + 32);
var IDX = getIDX(stmt.argument.name)
push_var(IDX)
if(stmt.operator == '++')
{
vm_str+=String.fromCharCode(OPCODE.EXPRESSION + 32);
vm_str+='+';
}
else if(stmt.operator == '--')
{
vm_str+=String.fromCharCode(OPCODE.EXPRESSION + 32);
vm_str+='-';
}
mov_var(IDX)
return IDX
}
else
{
console.log('unkown')
}
return undefined
}
function getStmt(stmt)
{
if(stmt != undefined)
{
var l_stmt;
if(stmt.object != undefined)
l_stmt = stmt
else if(stmt.init != undefined && stmt.init.object != undefined)
l_stmt = stmt.init
else
l_stmt = stmt
return l_stmt
}
return undefined
}
function push_value(type, name, value, stmt)
{
if(name == 'undefined')
{
var IDX = -99 //负数的下标肯定是undefined
push_var(IDX)
}
else if(type == 'ObjectExpression')
{
// var func_name = ''
// if(stmt.callee.type == 'MemberExpression')
// func_name = stmt.callee.object.name + '.' + stmt.callee.property.name
// else
// func_name = stmt.callee.name
// //压函数
// var IDX = getIDX('func$' + func_name)
// if(IDX == undefined && stmt.callee.property != undefined)
// IDX = getIDX('func$' + stmt.callee.property.name)
// push_var(IDX) //压入函数对象
// var mprototype = 'prototype'
// vm_str+=String.fromCharCode(OPCODE.GET_OBJ + 32);
// vm_str+=String.fromCharCode(mprototype.length + 32);
// vm_str+=mprototype;
// stmt.properties[0].key.name == 'success'
}
else if(type == 'Literal')
{
vm_str+=String.fromCharCode(OPCODE.PUSH_VALUE + 32);
vm_str+=String.fromCharCode(OPCODE.PUSH_VALUE + stmt.value);
}
else if(type == 'Identifier')
{
var IDX = getIDX(name)
push_var(IDX)
}
else if(type == 'UpdateExpression')
{
var IDX = myUpdateExpression(stmt);
push_var(IDX)
}
else if(type == 'AssignmentExpression')
{
myExpression(stmt);
}
else if(type == 'UnaryExpression')
{
op_num_type_push(stmt.argument)
push_symbol(stmt)
}
else if(type == 'ThisExpression')
{
vm_str+=String.fromCharCode(OPCODE.PUSH_THIS + 32);
}
else if(type == 'ArrayExpression')
{
for(var j = 0; j < stmt.elements.length; j++)
{
op_num_type_push(stmt.elements[j]);
}
//参数为数组
vm_str+=String.fromCharCode(OPCODE.PUSH_STACK_ARRAY + 32);
vm_str+=String.fromCharCode(stmt.elements.length + 32);//参数个数
}
else if(type == 'BinaryExpression')
{
myExpression(stmt)
}
else if(type == 'CallExpression')
{
var func_name = ''
if(stmt.callee.type == 'MemberExpression')
func_name = stmt.callee.object.name + '.' + stmt.callee.property.name
else
func_name = stmt.callee.name
var var_name = '$retn$' + func_name
if(func_retn_count[var_name] == undefined)
encrypt_code('CallExpression', '', stmt)
var IDX = getIDX(var_name + func_retn_count[var_name][1])
func_retn_count[var_name][1]++
func_retn_count[var_name][0]--
if(func_retn_count[var_name][0] == 0)
delete func_retn_count[var_name] //说明返回值已经用完了, 就删除喽
//func_retn_count.remove(1)
push_var(IDX)
}
else if(type == 'MemberExpression')
{
var l_stmt = getStmt(stmt)
if(l_stmt.object.type == 'ThisExpression' || l_stmt.object.name == 'global')
{
push_value('ThisExpression', '','', stmt)
}
else
{
var IDX = getIDX(l_stmt.object.name)
push_var(IDX)
}
if(l_stmt.property.type == 'Identifier')
{
var IDX = getIDX(l_stmt.property.name)
if(IDX == undefined)
{
vm_str+=String.fromCharCode(OPCODE.GET_OBJ + 32);
vm_str+=String.fromCharCode(l_stmt.property.name.length + 32);
vm_str+=l_stmt.property.name;
}
else
{
push_var(IDX)
vm_str+=String.fromCharCode(OPCODE.GET_ARY_VALUE + 32);
}
}
else
{
op_num_type_push(l_stmt.property, true)
vm_str+=String.fromCharCode(OPCODE.GET_ARY_VALUE + 32);
}
}
else if(type == 'ArrayExpression')
{
//数组声明
if(stmt.init.elements.length > 1)
{
var len = stmt.init.elements.length;
for(var i = 0; i < len; i++)
{
op_num_type_push(stmt.init.elements[i], true)
}
vm_str+=String.fromCharCode(OPCODE.PUSH_STACK_ARRAY + 32);
vm_str+=String.fromCharCode(len + 32);//参数个数
}
}
else if(type == 'ConditionalExpression_test')
{
var my_stmt = stmt.init == undefined ? stmt : stmt.init
//encrypt_code('IfStatement', "",my_stmt)
is_ture(stmt)
tempIDX[tempIDXCount] = vm_str.length
tempIDXCount++
}
else if(type == 'ConditionalExpression_consequent')
{
var my_stmt = stmt.init == undefined ? stmt : stmt.init
tempIDXCount--
var len = tempIDX[tempIDXCount]
op_num_type_push(my_stmt.consequent)
var diff = vm_str.length - len + 2 //加2 = sikp_block字节码和对应长度
vm_str = vm_str.substring(0, len) + String.fromCharCode(diff + 32) + vm_str.substring(len, vm_str.length)
vm_str+=String.fromCharCode(OPCODE.SKIP_BLOCK + 32);//执行op
tempIDX[tempIDXCount] = vm_str.length
tempIDXCount++
}
else if(type == 'ConditionalExpression_alternate')
{
var my_stmt = stmt.init == undefined ? stmt : stmt.init
tempIDXCount--
var len = tempIDX[tempIDXCount]
op_num_type_push(my_stmt.alternate)
var diff = vm_str.length - len
vm_str = vm_str.substring(0, len) + String.fromCharCode(diff + 32) + vm_str.substring(len, vm_str.length)
}
else if(type == 'LogicalExpression_')
{
op_num_type_push(stmt.left)
if(stmt.operator == '||')//stmt.operator == '||'
vm_str+=String.fromCharCode(OPCODE.PUSH_NOT_STACK_TOP + 32); //取反因为不成立, 才跳
vm_str+=String.fromCharCode(OPCODE.IS_TURE + 32);
var len = vm_str.length
op_num_type_push(stmt.right)
var diff = vm_str.length - len
vm_str = vm_str.substring(0, len) + String.fromCharCode(diff + 32) + vm_str.substring(len, vm_str.length)
if(stmt.operator == '||')
vm_str+=String.fromCharCode(OPCODE.PUSH_NOT_STACK_TOP + 32); //还原回去
}
}
//根据操作数类型处理
function op_num_type_push(stmt, not_push_substr)
{
if(stmt.value != undefined && typeof(stmt.value) == 'string')
{
if(!not_push_substr)
{
push_substring(stmt.value)
}
else
{
vm_str+=String.fromCharCode(stmt.value.length + 32);
vm_str+=stmt.value;
}
}
else if(stmt.value != undefined && typeof(stmt.value) == 'number')
{
if(stmt.value.toString().indexOf('.') > 0)
{
vm_str+=String.fromCharCode(OPCODE.PUSH_FLOAT_VALUE + 32);
var push_num = stmt.value.toString()
vm_str+=String.fromCharCode(push_num.length + 32);
vm_str+=push_num
}
else if(stmt.value < 37600 && stmt.value > -37600)
{
vm_str+=String.fromCharCode(OPCODE.PUSH_VALUE + 32);
vm_str+=String.fromCharCode(stmt.value + 32);
}
else
{
vm_str+=String.fromCharCode(OPCODE.PUSH_NUM + 32);
var push_num = stmt.value.toString(36)
vm_str+=String.fromCharCode(push_num.length + 32);
vm_str+=push_num
}
}
else if (stmt.name == 'global')
push_value('ThisExpression', stmt.name, '', stmt)
else if(stmt.type == 'Identifier')
push_value('Identifier', stmt.name, '', stmt)
else
push_value(stmt.type, stmt.name, '', stmt)
}
//右值是否没有运算表达式
function is_single(stmt)
{
if(stmt.type == 'Identifier' || stmt.type == 'Literal' || stmt.type == 'MemberExpression')
return true
return false
}
//是否普通赋值语句
function is_not_clac(str1, stmt, mode)
{
var value = null;
var dest_name = ''
if(mode == 0)
{
if(str1.indexOf(' ') < 0 && str1.indexOf('+') < 0 && str1.indexOf('-') < 0)
{
op_num_type_push(stmt.init)
mov_var(esp)
my_varIDX['$'+ tier][stmt.id.name] = esp++;
end()
return true;
}
}
else if(mode == 1)
{
var ary = str1.split(' ')
//if(ary.length == 3 && ary[1] == '=') //只有'='是防止出现+= -=这种
if(is_single(stmt.right))
{
if(stmt.right.type != 'BinaryExpression')
{
//mov_op(stmt.right, stmt.right.name, stmt.left, false)
if(stmt.right.type == 'Identifier')
{
var IDX = getIDX(stmt.right.name)
push_var(IDX)
}
else if(stmt.right.type == 'Literal')
{
op_num_type_push(stmt.right);
}
else if (stmt.right.type == 'MemberExpression')
{
push_value('MemberExpression', "","",stmt.right)
}
set_value(stmt, str1)
end()
return true;
}
}
}
return false;
}
//Member的类型赋值
function member_mov(stmt)
{
op_num_type_push(stmt.left.object) //压对象
if(stmt.left.computed == false)
{
//说明是this.xxx 这种访问
push_substring(stmt.left.property.name)
}
else
op_num_type_push(stmt.left.property) //压下标
vm_str+=String.fromCharCode(OPCODE.SET_ARY_VALUE + 32);
}
function set_value(stmt, str1)
{
try
{
if(stmt.operator != undefined && !is_clac_bool(stmt.operator))
{
//是否是简略的运算表达式, 如+=,-=,*=
myExpression(stmt, 0)
}
else if(stmt.left.type == 'MemberExpression')
{
member_mov(stmt)
}
else if(1)
{
var IDX = getIDX(stmt.left.name)
mov_var(IDX)
}
}
catch(err)
{
//不是member赋值
mov_var(esp)
esp++;
}
}
function myExpression_2(type, str1, stmt)
{
if(type == 'UpdateExpression')
{
myUpdateExpression(stmt);
return
}
else if(type == 'ArrayExpression')
{
push_value('ArrayExpression', "", str1, stmt)
}
else
{
myExpression(stmt, 0)
//赋值操作
}
}
function end()
{
var fs = require("fs");
var vmframe = fs.readFileSync('./vm框架/VmFrame_full.js').toString()
fs.writeFileSync('./build/vm.js', 'var codestr = \`' + vm_str.replace(/\\/g,"\\\\").replace(/\`/g,"\\\`") + '\`\n\n' + vmframe, {encoding:'utf-8'})
}
//加密
function encrypt_code(op, str1, stmt, )
{
var push_num;
//索引栈检测
console.log(op)
if(my_varIDX['$'+ tier] == undefined)
my_varIDX['$'+ tier] = new Array()
if(op == 'Expression')
{
if(str1 == 'uninit')
{
vm_str+=String.fromCharCode(OPCODE.PUSH0 + 32);
set_value(stmt, str1)
end()
return
}
if(is_not_clac(str1, stmt, 0)){return}
else{myExpression_2(stmt.init.type, str1, stmt.init)}
mov_var(esp)
my_varIDX['$'+ tier][stmt.id.name] = esp++;
}
else if(op.indexOf('ConditionalExpression') >= 0 || op == 'LogicalExpression_')
{
//三目运算 || 逻辑运算
push_value(op, "","",stmt)
}
else if(op == 'BinaryExpression')
{
myExpression(stmt, 0)
}
else if(op == 'ExpressionStatement' )
{
//普通赋值
// if(str1 == 'p += p1[a - 1]')
// console.log(str1)
if(is_not_clac(str1, stmt, 1)){return};
if(stmt.right.type == 'UpdateExpression')
{
myUpdateExpression(stmt.right);
return
}
//myExpression(stmt, 0)
if(!is_clac_bool(stmt.operator))//+= -= *=这种情况
{
myExpression(stmt, 0)
return
}
else
{
myExpression(stmt.right, 0)
}
set_value(stmt)
// var IDX = getIDX(stmt.left.name)
//mov_var(IDX)
}
else if(op == 'EnBlockStatement')
{
tier++; //层数增加
//console.log("Enter BlockStatement")
}
else if(op == 'LvBlockStatement')
{
delete my_varIDX['$'+tier]
tier--;//层数减少
//console.log("Lvave BlockStatement")
}
else if(op == 'generateFunctionParams')
{
var argIDX = 0
//参数全部移动到var里
var params_len = stmt.params.length - 1;
esp += stmt.params.length //防止第一次调用, 变量赋值覆盖参数
for(var i = 0; i < stmt.params.length; i++)
{
my_varIDX['$'+ tier][stmt.params[i].name] = i;
my_varIDX['$'+ tier][stmt.id.name + '$arg$' + argIDX] = i;
argIDX++;
}
}
else if(op == 'EnFunctionDeclaration')
{
vm_str+=String.fromCharCode(OPCODE.PUSH_FUNC + 32);//执行op
tempIDX[tempIDXCount] = vm_str.length
tempIDXCount++
// if(vm_str.length > 0)
// funcIDX -= 1
}
else if(op == 'LvFunctionDeclaration')
{
tempIDXCount--
var func_len = vm_str.length - tempIDX[tempIDXCount]
//var func_len = stringToByte(vm_str).length - tempIDX[tempIDXCount]
//把函数长度插入
vm_str = vm_str.substring(0, tempIDX[tempIDXCount]) + String.fromCharCode(func_len + 32) + vm_str.substring(tempIDX[tempIDXCount], vm_str.length)
//console.log(vm_str.length)
//移动到变量区
mov_var(esp)
//把函数移动到变量栈
my_varIDX['$'+ tier]['func$' + str1[3]] = esp;
esp++;
}
else if(op == 'CallExpression')
{
var func_name = ''
if(stmt.callee.type == 'MemberExpression')
func_name = stmt.callee.object.name + '.' + stmt.callee.property.name
else
func_name = stmt.callee.name
//压函数
var IDX = getIDX('func$' + func_name)
if(IDX == undefined && stmt.callee.property != undefined)
IDX = getIDX('func$' + stmt.callee.property.name)
if(IDX != undefined)
{
push_var(IDX)
}
else
{
//系统函数
if(stmt.callee.type == 'MemberExpression')
{
//获取obj
push_obj(stmt.callee.object)
//获取函数
var func_name2 = stmt.callee.property.name
vm_str+=String.fromCharCode(OPCODE.GET_OBJ + 32);
vm_str+=String.fromCharCode(func_name2.length + 32);
vm_str+=func_name2;
}
else if (stmt.callee.type == 'Identifier')
{
push_obj(stmt.callee)
}
}
if(getIDX('func$' + stmt.callee.name) != undefined)
vm_str+=String.fromCharCode(OPCODE.PUSH_NULL + 32);
else if(stmt.callee.type == 'MemberExpression')
push_obj(stmt.callee.object)
else
vm_str+=String.fromCharCode(OPCODE.PUSH_NULL + 32);
for(var i = 0; i < stmt.arguments.length; i++)
{
// if(stmt.arguments[i].type == 'ObjectExpression')
// {
// stmt.arguments[i].callee = stmt.callee
// }
op_num_type_push(stmt.arguments[i])
}
//组成数组方便apply调用
vm_str+=String.fromCharCode(OPCODE.PUSH_STACK_ARRAY + 32);
vm_str+=String.fromCharCode(stmt.arguments.length + 32);//参数个数
//apply调用
vm_str+=String.fromCharCode(OPCODE.APPLY + 32);
//储存函数调用结果, 防止函数嵌套调用取结果
vm_str+=String.fromCharCode(OPCODE.MY_MOV_VARS + 32);
vm_str+=String.fromCharCode(esp+32);
//初始化函数返回值计数
if(func_retn_count['$retn$' + func_name] == undefined)
{
func_retn_count['$retn$' + func_name] = new Array()
func_retn_count['$retn$' + func_name][0] = 0
func_retn_count['$retn$' + func_name][1] = 0
}
var var_name = '$retn$' + func_name
my_varIDX['$'+ tier][var_name + func_retn_count['$retn$' + func_name][0]] = esp++;
func_retn_count[var_name][0]++
}
else if(op == 'EnmaybeBlock' || op == 'IfStatement_ELSEBEGIN')
{
if(op == 'IfStatement_ELSEBEGIN')
{
//vm_str+=String.fromCharCode(OPCODE.PUSH_NOT_STACK_TOP + 32);
vm_str+=String.fromCharCode(OPCODE.SKIP_BLOCK + 32);
}
tempIDX[tempIDXCount] = vm_str.length
tempIDXCount++
}
else if(op == 'LvmaybeBlock'|| op == 'IfStatement_ELSEEND')
{
tempIDXCount--
var code_len = vm_str.length - tempIDX[tempIDXCount]
if(str1 == 'IFBLOCK' || str1 == 'IFESBLOCK')
code_len+=2 //增加ELSE的sikp长度
vm_str = vm_str.substring(0, tempIDX[tempIDXCount]) + String.fromCharCode(code_len + 32) + vm_str.substring(tempIDX[tempIDXCount], vm_str.length)
//对应ESLE IF的逻辑, 记录索引, 直接跳到ELSE
if(str1 == 'IFESBLOCK')
{
vm_str+=String.fromCharCode(OPCODE.SKIP_BLOCK + 32);
ifESIDX[ifESIDXCount] = vm_str.length
ifESIDXCount++
}
else if(op == 'IfStatement_ELSEEND')
{
//对应ESLE IF的逻辑, 记录索引, 直接跳到ELSE
for(var i = ifESIDX.length - 1; i >= 0; i--)
{
var code_len = vm_str.length - ifESIDX[i]
vm_str = vm_str.substring(0, ifESIDX[i]) + String.fromCharCode(code_len + 32) + vm_str.substring(ifESIDX[i], vm_str.length)
}
for(var i = ifESIDX.length - 1; i >= 0; i--)
{
delete ifESIDX[i]
}
ifESIDXCount = 0
ifESIDX.length = 0
}
}
else if(op == 'IfStatement')
{
//计算条件
is_ture(stmt)
}
else if(op == 'EnForStatement')
{
tempIDX[tempIDXCount] = vm_str.length
tempIDXCount++
}
else if(op == 'LvForStatement')
{
tempIDXCount--
var code_len = vm_str.length - tempIDX[tempIDXCount]
if(str1 == '1')
{
code_len = -(code_len + 2) //需跳回, 所以取负数, +2是最后下面sikp_block字节码长度
vm_str += String.fromCharCode(OPCODE.SKIP_BLOCK + 32);
vm_str += String.fromCharCode(code_len + 32);
}
else if(str1 == '2')
{
code_len += 2 //+2是最后下面sikp_block字节码长度
vm_str = vm_str.substring(0, tempIDX[tempIDXCount]) + String.fromCharCode(code_len + 32) + vm_str.substring(tempIDX[tempIDXCount], vm_str.length)
}
}
else if(op == 'ForStatement_test')
{
myExpression(stmt, 0)
vm_str+=String.fromCharCode(OPCODE.IS_TURE + 32);
}
else if(op == 'ForStatement_update')
{
if(stmt.type == 'UpdateExpression')
{
myUpdateExpression(stmt)
}
else
{
myExpression(stmt, 0)
}
}
else if(op == 'ReturnStatement')
{
if(stmt.argument.type == 'Identifier')
push_value('Identifier', stmt.argument.name, '')
else
op_num_type_push(stmt.argument)
vm_str+=String.fromCharCode(OPCODE.RETURN + 32);//执行op
}
end()
return ""
}
function is_ture(stmt)
{
myExpression(stmt.test, 0)
vm_str+=String.fromCharCode(OPCODE.IS_TURE + 32);
}
function push_substring(str)
{
vm_str+=String.fromCharCode(OPCODE.XOR_DECODE + 32);
var y = str
var d = []
for (var g = 0; g < y.length; g++)
d[g] = y.charCodeAt(g) ^ g + y.length;
vm_str+=String.fromCharCode(str.length + 32);
vm_str+=String.fromCharCode.apply(null, d);
// vm_str+=String.fromCharCode(OPCODE.PUSH_SUBSTR + 32);
// vm_str+=String.fromCharCode(str.length + 32);
// vm_str+=str;
}
function mov_var(idx)
{
vm_str+=String.fromCharCode(OPCODE.MOV_VARS + 32);
vm_str+=String.fromCharCode(idx+32);
}
function push_var(idx)
{
vm_str+=String.fromCharCode(OPCODE.PUSH_VAR + 32);
vm_str+=String.fromCharCode(idx+32);
}
//是否判断bool的简单运算符
function is_clac_bool(op_symbol)
{
if(op_symbol.length != 2 || op_symbol.indexOf('=') < 0){ return true}
if(op_symbol === '==' || op_symbol === '>=' || op_symbol === '<=' || op_symbol === '!=') { return true }
return false
}
//******************************添加的编译代码 结束**********************************
// Generation is done by generateExpression.
function isExpression(node) {
return CodeGenerator.Expression.hasOwnProperty(node.type);
}
// Generation is done by generateStatement.
function isStatement(node) {
return CodeGenerator.Statement.hasOwnProperty(node.type);
}
Precedence = {
Sequence: 0,
Yield: 1,
Await: 1,
Assignment: 1,
Conditional: 2,
ArrowFunction: 2,
LogicalOR: 3,
LogicalAND: 4,
BitwiseOR: 5,
BitwiseXOR: 6,
BitwiseAND: 7,
Equality: 8,
Relational: 9,
BitwiseSHIFT: 10,
Additive: 11,
Multiplicative: 12,
Unary: 13,
Postfix: 14,
Call: 15,
New: 16,
TaggedTemplate: 17,
Member: 18,
Primary: 19
};
BinaryPrecedence = {
'||': Precedence.LogicalOR,
'&&': Precedence.LogicalAND,
'|': Precedence.BitwiseOR,
'^': Precedence.BitwiseXOR,
'&': Precedence.BitwiseAND,
'==': Precedence.Equality,
'!=': Precedence.Equality,
'===': Precedence.Equality,
'!==': Precedence.Equality,
'is': Precedence.Equality,
'isnt': Precedence.Equality,
'<': Precedence.Relational,
'>': Precedence.Relational,
'<=': Precedence.Relational,
'>=': Precedence.Relational,
'in': Precedence.Relational,
'instanceof': Precedence.Relational,
'<<': Precedence.BitwiseSHIFT,
'>>': Precedence.BitwiseSHIFT,
'>>>': Precedence.BitwiseSHIFT,
'+': Precedence.Additive,
'-': Precedence.Additive,
'*': Precedence.Multiplicative,
'%': Precedence.Multiplicative,
'/': Precedence.Multiplicative
};
//Flags
var F_ALLOW_IN = 1,
F_ALLOW_CALL = 1 << 1,
F_ALLOW_UNPARATH_NEW = 1 << 2,
F_FUNC_BODY = 1 << 3,
F_DIRECTIVE_CTX = 1 << 4,
F_SEMICOLON_OPT = 1 << 5;
//Expression flag sets
//NOTE: Flag order:
// F_ALLOW_IN
// F_ALLOW_CALL
// F_ALLOW_UNPARATH_NEW
var E_FTT = F_ALLOW_CALL | F_ALLOW_UNPARATH_NEW,
E_TTF = F_ALLOW_IN | F_ALLOW_CALL,
E_TTT = F_ALLOW_IN | F_ALLOW_CALL | F_ALLOW_UNPARATH_NEW,
E_TFF = F_ALLOW_IN,
E_FFT = F_ALLOW_UNPARATH_NEW,
E_TFT = F_ALLOW_IN | F_ALLOW_UNPARATH_NEW;
//Statement flag sets
//NOTE: Flag order:
// F_ALLOW_IN
// F_FUNC_BODY