This repository has been archived by the owner on Nov 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
runtime.js
2198 lines (2056 loc) · 96.2 KB
/
runtime.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() {
var Program = {};
Program["com.mcleodgaming.as3js.enums.AS3Encapsulation"] = function(module, exports) {
var AS3Encapsulation = function AS3Encapsulation() {};
AS3Encapsulation.PUBLIC = null;
AS3Encapsulation.PRIVATE = null;
AS3Encapsulation.PROTECTED = null;
AS3Encapsulation.$cinit = function() {
AS3Encapsulation.PUBLIC = "public";
AS3Encapsulation.PRIVATE = "private";
AS3Encapsulation.PROTECTED = "protected";
};
AS3Encapsulation.prototype.$init = function() {}
module.exports = AS3Encapsulation;
};
Program["com.mcleodgaming.as3js.enums.AS3MemberType"] = function(module, exports) {
var AS3MemberType = function AS3MemberType() {};
AS3MemberType.VAR = null;
AS3MemberType.CONST = null;
AS3MemberType.FUNCTION = null;
AS3MemberType.$cinit = function() {
AS3MemberType.VAR = "var";
AS3MemberType.CONST = "const";
AS3MemberType.FUNCTION = "function";
};
AS3MemberType.prototype.$init = function() {}
module.exports = AS3MemberType;
};
Program["com.mcleodgaming.as3js.enums.AS3ParseState"] = function(module, exports) {
var AS3ParseState = function AS3ParseState() {};
AS3ParseState.START = null;
AS3ParseState.PACKAGE_NAME = null;
AS3ParseState.PACKAGE = null;
AS3ParseState.CLASS_NAME = null;
AS3ParseState.CLASS = null;
AS3ParseState.CLASS_EXTENDS = null;
AS3ParseState.CLASS_IMPLEMENTS = null;
AS3ParseState.COMMENT_INLINE = null;
AS3ParseState.COMMENT_MULTILINE = null;
AS3ParseState.STRING_SINGLE_QUOTE = null;
AS3ParseState.STRING_DOUBLE_QUOTE = null;
AS3ParseState.STRING_REGEX = null;
AS3ParseState.MEMBER_VARIABLE = null;
AS3ParseState.MEMBER_FUNCTION = null;
AS3ParseState.LOCAL_VARIABLE = null;
AS3ParseState.LOCAL_FUNCTION = null;
AS3ParseState.IMPORT_PACKAGE = null;
AS3ParseState.REQUIRE_MODULE = null;
AS3ParseState.$cinit = function() {
AS3ParseState.START = "start";
AS3ParseState.PACKAGE_NAME = "packageName";
AS3ParseState.PACKAGE = "package";
AS3ParseState.CLASS_NAME = "className";
AS3ParseState.CLASS = "class";
AS3ParseState.CLASS_EXTENDS = "classExtends";
AS3ParseState.CLASS_IMPLEMENTS = "classImplements";
AS3ParseState.COMMENT_INLINE = "commentInline";
AS3ParseState.COMMENT_MULTILINE = "commentMultiline";
AS3ParseState.STRING_SINGLE_QUOTE = "stringSingleQuote";
AS3ParseState.STRING_DOUBLE_QUOTE = "stringDoubleQuote";
AS3ParseState.STRING_REGEX = "stringRegex";
AS3ParseState.MEMBER_VARIABLE = "memberVariable";
AS3ParseState.MEMBER_FUNCTION = "memberFunction";
AS3ParseState.LOCAL_VARIABLE = "localVariable";
AS3ParseState.LOCAL_FUNCTION = "localFunction";
AS3ParseState.IMPORT_PACKAGE = "importPackage";
AS3ParseState.REQUIRE_MODULE = "requireModule";
};
AS3ParseState.prototype.$init = function() {}
module.exports = AS3ParseState;
};
Program["com.mcleodgaming.as3js.enums.AS3Pattern"] = function(module, exports) {
var AS3Pattern = function AS3Pattern() {};
AS3Pattern.IDENTIFIER = null;
AS3Pattern.OBJECT = null;
AS3Pattern.IMPORT = null;
AS3Pattern.REQUIRE = null;
AS3Pattern.CURLY_BRACE = null;
AS3Pattern.VARIABLE = null;
AS3Pattern.VARIABLE_TYPE = null;
AS3Pattern.VARIABLE_DECLARATION = null;
AS3Pattern.ASSIGN_START = null;
AS3Pattern.ASSIGN_UPTO = null;
AS3Pattern.VECTOR = null;
AS3Pattern.ARRAY = null;
AS3Pattern.DICTIONARY = null;
AS3Pattern.REST_ARG = null;
AS3Pattern.$cinit = function() {
AS3Pattern.IDENTIFIER = [/\w/g, /\w/g];
AS3Pattern.OBJECT = [/[\w\.]/g, /[\w(\w(\.\w)+)]/g];
AS3Pattern.IMPORT = [/[0-9a-zA-Z_$.*]/g, /[a-zA-Z_$][0-9a-zA-Z_$]([.][a-zA-Z_$][0-9a-zA-Z_$])*\*?/g];
AS3Pattern.REQUIRE = [/./g, /["'](.*?)['"]/g];
AS3Pattern.CURLY_BRACE = [/[\{|\}]/g, /[\{|\}]/g];
AS3Pattern.VARIABLE = [/[0-9a-zA-Z_$]/g, /[a-zA-Z_$][0-9a-zA-Z_$]*/g];
AS3Pattern.VARIABLE_TYPE = [/[a-zA-Z_$<>.*][0-9a-zA-Z_$<>.]*/g, /[a-zA-Z_$<>.*][0-9a-zA-Z_$<>.]*/g];
AS3Pattern.VARIABLE_DECLARATION = [/[0-9a-zA-Z_$:<>.*]/g, /[a-zA-Z_$][0-9a-zA-Z_$]*\s*:\s*([a-zA-Z_$<>\.\*][0-9a-zA-Z_$<>\.]*)/g];
AS3Pattern.ASSIGN_START = [/[=\r\n]/g, /[=\r\n]/g];
AS3Pattern.ASSIGN_UPTO = [new RegExp("[^;\\r\\n]", "g"), /(.*?)/g];
AS3Pattern.VECTOR = [/new[\s\t]+Vector\.<(.*?)>\((.*?)\)/g, /new[\s\t]+Vector\.<(.*?)>\((.*?)\)/];
AS3Pattern.ARRAY = [/new[\s\t]+Array\((.*?)\)/g, /new[\s\t]+Array\((.*?)\)/];
AS3Pattern.DICTIONARY = [/new[\s\t]+Dictionary\((.*?)\)/g];
AS3Pattern.REST_ARG = [/\.\.\.[a-zA-Z_$][0-9a-zA-Z_$]*/g, /\.\.\.[a-zA-Z_$][0-9a-zA-Z_$]*/g];
};
AS3Pattern.prototype.$init = function() {}
module.exports = AS3Pattern;
};
Program["com.mcleodgaming.as3js.Main"] = function(module, exports) {
var path = (function() {
try {
return require("path");
} catch (e) {
return undefined;
}
})();
var fs = (function() {
try {
return require("fs");
} catch (e) {
return undefined;
}
})();
var AS3Parser;
module.inject = function() {
AS3Parser = module.import('com.mcleodgaming.as3js.parser', 'AS3Parser');
};
var Main = function() {
this.$init();
};
Main.DEBUG_MODE = false;
Main.SILENT = false;
Main.debug = function() {
if (Main.SILENT) {
return;
}
if (Main.DEBUG_MODE) {
console.log.apply(console, arguments);
}
};
Main.log = function() {
if (Main.SILENT) {
return;
}
console.log.apply(console, arguments);
};
Main.warn = function() {
if (Main.SILENT) {
return;
}
console.warn.apply(console, arguments);
};
Main.$cinit = function() {
Main.DEBUG_MODE = false;
Main.SILENT = false;
};
Main.prototype.$init = function() {};
Main.prototype.compile = function(options) {
options = AS3JS.Utils.getDefaultValue(options, null);
var packages = {}; //Will contain the final map of package names to source text
var i;
var j;
var k;
var m;
var tmp;
options = options || {};
var srcPaths = options.srcPaths || {};
var rawPackages = options.rawPackages || [];
var parserOptions = {
safeRequire: options.safeRequire,
ignoreFlash: options.ignoreFlash
};
//Temp classes for holding raw class info
var rawClass;
var rawParser;
var pkgLists = {};
for (i in srcPaths) {
pkgLists[srcPaths[i]] = this.buildPackageList(srcPaths[i]);
}
Main.DEBUG_MODE = options.verbose || Main.DEBUG_MODE;
Main.SILENT = options.silent || Main.SILENT;
var classes = {};
var buffer = "";
//First, parse through the file-based classes and get the basic information
for (i in pkgLists) {
for (j in pkgLists[i]) {
Main.log('Analyzing class path: ' + pkgLists[i][j].classPath);
classes[pkgLists[i][j].classPath] = pkgLists[i][j].parse(parserOptions);
Main.debug(classes[pkgLists[i][j].classPath]);
}
}
// Now parse through any raw string classes
for (i = 0; i < rawPackages.length; i++) {
Main.log('Analyzing class: ' + i);
rawParser = new AS3Parser(rawPackages[i]);
rawClass = rawParser.parse(parserOptions);
classes[rawParser.classPath] = rawClass;
}
//Resolve all possible package name wildcards
for (i in classes) {
//For every class
for (j in classes[i].importWildcards) {
Main.debug('Resolving ' + classes[i].className + '\'s ' + classes[i].importWildcards[j] + ' ...')
//For every wild card in the class
for (k in srcPaths) {
//For each possible source path (should hopefully just be 1 most of the time -_-)
tmp = srcPaths[k] + path.sep + classes[i].importWildcards[j].replace(/\./g, path.sep).replace(path.sep + '*', '');
tmp = tmp.replace(/\\/g, '/');
tmp = tmp.replace(/[\/]/g, path.sep);
if (fs.existsSync(tmp)) {
Main.debug('Searching path ' + tmp + '...')
//Path exists, read the files in the directory
var files = fs.readdirSync(tmp);
for (m in files) {
//See if this is an ActionScript file
if (fs.statSync(tmp + path.sep + files[m]).isFile() && files[m].lastIndexOf('.as') == files[m].length - 3) {
//See if the class needs the file
if (classes[i].needsImport(classes[i].importWildcards[j].replace(/\*/g, files[m].substr(0, files[m].length - 3)))) {
Main.debug('Auto imported ' + files[m].substr(0, files[m].length - 3));
classes[i].addImport(classes[i].importWildcards[j].replace(/\*/g, files[m].substr(0, files[m].length - 3))); //Pass in package name with wild card replaced
}
}
}
} else {
Main.warn('Warning, could not find directory: ' + tmp);
}
}
// Must do again for classes in case there we
for (k in classes) {
if (classes[i].needsImport(AS3Parser.fixClassPath(classes[k].packageName + '.' + classes[k].className))) {
Main.debug('Auto imported ' + AS3Parser.fixClassPath(classes[k].packageName + '.' + classes[k].className));
classes[i].addImport(AS3Parser.fixClassPath(classes[k].packageName + '.' + classes[k].className)); //Pass in package name with wild card replaced
}
}
}
}
//Add extra imports before registring them (these will not be imported in the output code, but rather will provide insight for AS3JS to determine variable types)
for (i in classes) {
for (j in classes) {
classes[i].addExtraImport(AS3Parser.fixClassPath(classes[j].packageName + '.' + classes[j].className));
}
}
//Resolve import map
for (i in classes) {
classes[i].registerImports(classes);
}
//Resolve parent imports
for (i in classes) {
classes[i].findParents(classes);
}
//Walk through the class members that had assignments in the class scope
for (i in classes) {
classes[i].checkMembersWithAssignments();
}
//Process the function text to comply with JS
for (i in classes) {
Main.log('Parsing package: ' + AS3Parser.fixClassPath(classes[i].packageName + "." + classes[i].className));
classes[i].process(classes);
}
// Load stringified versions of snippets/main-snippet.js and snippets/class-snippet.js
var mainTemplate = "(function(){var Program={};{{packages}}if(typeof module !== 'undefined'){module.exports=AS3JS.load({program:Program,entry:\"{{entryPoint}}\",entryMode:\"{{entryMode}}\"});}else if(typeof window!=='undefined'&&typeof AS3JS!=='undefined'){window['{{entryPoint}}']=AS3JS.load({program:Program,entry:\"{{entryPoint}}\",entryMode:\"{{entryMode}}\"});}})();";
var classTemplate = "Program[\"{{module}}\"]=function(module, exports){{{source}}};";
var packageObjects = [];
var classObjects = null;
var currentClass = "";
if (options.entry) {
// Entry point should be in the format "mode:path.to.package.Class"
var currentPackage = options.entry;
var mode = options.entryMode || 'instance';
// Update template with entry points
mainTemplate = mainTemplate.replace(/\{\{entryPoint\}\}/g, AS3Parser.fixClassPath(classes[currentPackage].packageName + '.' + classes[currentPackage].className));
mainTemplate = mainTemplate.replace(/\{\{entryMode\}\}/g, mode);
} else {
mainTemplate = mainTemplate.replace(/\{\{entryPoint\}\}/g, "");
mainTemplate = mainTemplate.replace(/\{\{entryMode\}\}/g, "");
}
//Retrieve converted class code
var groupByPackage = {};
for (i in classes) {
groupByPackage[classes[i].packageName] = groupByPackage[classes[i].packageName] || [];
groupByPackage[classes[i].packageName].push(classes[i]);
}
for (i in groupByPackage) {
classObjects = [];
for (j in groupByPackage[i]) {
packages[AS3Parser.fixClassPath(i + "." + groupByPackage[i][j].className)] = groupByPackage[i][j].toString();
currentClass = classTemplate;
currentClass = currentClass.replace(/\{\{module\}\}/g, AS3Parser.fixClassPath(groupByPackage[i][j].packageName + "." + groupByPackage[i][j].className));
currentClass = currentClass.replace(/\{\{source\}\}/g, AS3Parser.increaseIndent(packages[AS3Parser.fixClassPath(i + "." + groupByPackage[i][j].className)], " "));
classObjects.push(currentClass);
}
packageObjects.push(AS3Parser.increaseIndent(classObjects.join(""), " "));
}
mainTemplate = mainTemplate.replace(/\{\{packages\}\}/g, packageObjects.join(""));
mainTemplate = mainTemplate.replace(/\t/g, " ");
buffer += mainTemplate;
Main.log("Done.");
return {
compiledSource: buffer,
packageSources: packages
};
};
Main.prototype.readDirectory = function(location, pkgBuffer, obj) {
var files = fs.readdirSync(location);
for (var i in files) {
var pkg = pkgBuffer;
if (fs.statSync(location + path.sep + files[i]).isDirectory()) {
var splitPath = location.split(path.sep);
if (pkg != '') {
pkg += '.';
}
this.readDirectory(location + path.sep + files[i], pkg + files[i], obj)
} else if (fs.statSync(location + path.sep + files[i]).isFile() && files[i].lastIndexOf('.as') == files[i].length - 3) {
if (pkg != '') {
pkg += '.';
}
pkg += files[i].substr(0, files[i].length - 3);
var f = fs.readFileSync(location + path.sep + files[i]);
obj[pkg] = new AS3Parser(f.toString(), pkg);
Main.debug("Loaded file: ", location + path.sep + files[i] + " (package: " + pkg + ")");
}
}
};
Main.prototype.buildPackageList = function(location) {
var obj = {};
var topLevel = location;
location = location.replace(/\\/g, '/');
location = location.replace(/[\/]/g, path.sep);
if (fs.existsSync(location) && fs.statSync(location).isDirectory()) {
var splitPath = location.split(path.sep);
this.readDirectory(location, '', obj);
return obj;
} else {
throw new Error("Error could not find directory: " + location);
}
}
module.exports = Main;
};
Program["com.mcleodgaming.as3js.parser.AS3Class"] = function(module, exports) {
var Main, AS3Parser, AS3MemberType, AS3Pattern, AS3Function, AS3Variable;
module.inject = function() {
Main = module.import('com.mcleodgaming.as3js', 'Main');
AS3Parser = module.import('com.mcleodgaming.as3js.parser', 'AS3Parser');
AS3MemberType = module.import('com.mcleodgaming.as3js.enums', 'AS3MemberType');
AS3Pattern = module.import('com.mcleodgaming.as3js.enums', 'AS3Pattern');
AS3Function = module.import('com.mcleodgaming.as3js.types', 'AS3Function');
AS3Variable = module.import('com.mcleodgaming.as3js.types', 'AS3Variable');
};
var AS3Class = function(options) {
this.$init();
options = AS3JS.Utils.getDefaultValue(options, null);
options = options || {};
this.safeRequire = false;
if (typeof options.safeRequire !== 'undefined') {
this.safeRequire = options.safeRequire;
}
if (typeof options.ignoreFlash !== 'undefined') {
this.ignoreFlash = options.ignoreFlash;
}
this.packageName = null;
this.className = null;
this.imports = [];
this.requires = [];
this.importWildcards = [];
this.importExtras = [];
this.interfaces = [];
this.parent = null;
this.parentDefinition = null;
this.members = [];
this.staticMembers = [];
this.getters = [];
this.setters = [];
this.staticGetters = [];
this.staticSetters = [];
this.membersWithAssignments = [];
this.isInterface = false;
this.fieldMap = {};
this.staticFieldMap = {};
this.classMap = {};
this.classMapFiltered = {};
this.packageMap = {};
var $init = new AS3Function();
$init.name = "$init";
$init.value = "{}";
$init.type === AS3MemberType.FUNCTION;
$init.isStatic = false;
this.members.push($init);
this.registerField($init.name, $init);
};
AS3Class.reservedWords = null;
AS3Class.nativeTypes = null;
AS3Class.$cinit = function() {
AS3Class.reservedWords = ["as", "class", "delete", "false", "if", "instanceof", "native", "private", "super", "to", "use", "with", "break", "const", "do", "finally", "implements", "new", "protected", "switch", "true", "var", "case", "continue", "else", "for", "import", "internal", "null", "public", "this", "try", "void", "catch", "default", "extends", "function", "in", "is", "package", "return", "throw", "typeof", "while", "each", "get", "set", "namespace", "include", "dynamic", "final", "natiev", "override", "static", "abstract", "char", "export", "long", "throws", "virtual", "boolean", "debugger", "float", "prototype", "to", "volatile", "byte", "double", "goto", "short", "transient", "cast", "enum", "intrinsic", "synchronized", "type"];
AS3Class.nativeTypes = ["Boolean", "Number", "int", "uint", "String"];
};
AS3Class.prototype.$init = function() {
this.imports = null;
this.requires = null;
this.importWildcards = null;
this.importExtras = null;
this.interfaces = null;
this.parentDefinition = null;
this.members = null;
this.staticMembers = null;
this.getters = null;
this.setters = null;
this.staticGetters = null;
this.staticSetters = null;
this.membersWithAssignments = null;
this.fieldMap = null;
this.staticFieldMap = null;
this.classMap = null;
this.classMapFiltered = null;
this.packageMap = null;
};
AS3Class.prototype.packageName = null;
AS3Class.prototype.className = null;
AS3Class.prototype.imports = null;
AS3Class.prototype.requires = null;
AS3Class.prototype.importWildcards = null;
AS3Class.prototype.importExtras = null;
AS3Class.prototype.interfaces = null;
AS3Class.prototype.parent = null;
AS3Class.prototype.parentDefinition = null;
AS3Class.prototype.members = null;
AS3Class.prototype.staticMembers = null;
AS3Class.prototype.getters = null;
AS3Class.prototype.setters = null;
AS3Class.prototype.staticGetters = null;
AS3Class.prototype.staticSetters = null;
AS3Class.prototype.isInterface = false;
AS3Class.prototype.membersWithAssignments = null;
AS3Class.prototype.fieldMap = null;
AS3Class.prototype.staticFieldMap = null;
AS3Class.prototype.classMap = null;
AS3Class.prototype.classMapFiltered = null;
AS3Class.prototype.packageMap = null;
AS3Class.prototype.safeRequire = false;
AS3Class.prototype.ignoreFlash = false;
AS3Class.prototype.registerImports = function(clsList) {
var i;
for (i in this.imports) {
if (clsList[this.imports[i]]) {
var lastIndex = this.imports[i].lastIndexOf(".");
var shorthand = (lastIndex < 0) ? this.imports[i] : this.imports[i].substr(lastIndex + 1);
this.classMap[shorthand] = clsList[this.imports[i]];
}
}
for (i in this.importExtras) {
if (clsList[this.importExtras[i]]) {
var lastIndex = this.importExtras[i].lastIndexOf(".");
var shorthand = (lastIndex < 0) ? this.importExtras[i] : this.importExtras[i].substr(lastIndex + 1);
this.classMap[shorthand] = clsList[this.importExtras[i]];
}
}
this.packageMap = clsList;
};
AS3Class.prototype.registerField = function(name, value) {
if (value && value.isStatic) {
this.staticFieldMap[name] = this.staticFieldMap[name] || value;
} else {
this.fieldMap[name] = this.fieldMap[name] || value;
}
};
AS3Class.prototype.retrieveField = function(name, isStatic) {
if (isStatic) {
if (this.staticFieldMap[name]) {
return this.staticFieldMap[name];
} else if (this.parentDefinition) {
return this.parentDefinition.retrieveField(name, isStatic);
} else {
return null;
}
} else {
if (this.fieldMap[name]) {
return this.fieldMap[name];
} else if (this.parentDefinition) {
return this.parentDefinition.retrieveField(name, isStatic);
} else {
return null;
}
}
};
AS3Class.prototype.needsImport = function(pkg) {
var i;
var j;
var lastIndex = pkg.lastIndexOf(".");
var shorthand = (lastIndex < 0) ? pkg : pkg.substr(lastIndex + 1);
var matches;
if (this.imports.indexOf(pkg) >= 0) {
return false; //Class was already imported
}
if (shorthand == this.className && pkg == this.packageName) {
return true; //Don't need self
}
if (shorthand == this.parent) {
return true; //Parent class is in another package
}
//Now we must parse through all members one by one, looking at functions and variable types to determine the necessary imports
for (i in this.members) {
//See if the function definition or variable assigment have a need for this package
if (this.members[i] instanceof AS3Function) {
matches = this.members[i].value.match(AS3Pattern.VARIABLE_DECLARATION[1]);
for (j in matches) {
if (matches[j].split(":")[1] == shorthand)
return true;
}
for (j in this.members[i].argList) {
if (typeof this.members[i].argList[j].type == 'string' && this.members[i].argList[j].type == shorthand)
return true;
}
}
if (typeof this.members[i].value == 'string' && this.members[i].value.match(new RegExp("([^a-zA-Z_$.])" + shorthand + "([^0-9a-zA-Z_$])", "g"))) {
return true;
} else if (typeof this.members[i].type == 'string' && this.members[i].type == shorthand) {
return true;
}
}
for (i in this.staticMembers) {
//See if the function definition or variable assigment have a need for this package
if (this.staticMembers[i] instanceof AS3Function) {
matches = this.staticMembers[i].value.match(AS3Pattern.VARIABLE_DECLARATION[1]);
for (j in matches) {
if (matches[j].split(":")[1] == shorthand) {
return true;
}
}
for (j in this.staticMembers[i].argList) {
if (typeof this.staticMembers[i].argList[j].type == 'string' && this.staticMembers[i].argList[j].type == shorthand) {
return true;
}
}
}
if (typeof this.staticMembers[i].value == 'string' && this.staticMembers[i].value.match(new RegExp("([^a-zA-Z_$.])" + shorthand + "([^0-9a-zA-Z_$])", "g"))) {
return true;
} else if (typeof this.staticMembers[i].type == 'string' && this.staticMembers[i].type == shorthand) {
return true;
}
}
for (i in this.getters) {
//See if the function definition or variable assigment have a need for this package
matches = this.getters[i].value.match(AS3Pattern.VARIABLE_DECLARATION[1]);
for (j in matches) {
if (matches[j].split(":")[1] == shorthand) {
return true;
}
}
for (j in this.getters[i].argList) {
if (typeof this.getters[i].argList[j].type == 'string' && this.getters[i].argList[j].type == shorthand) {
return true;
}
}
if (typeof this.getters[i].value == 'string' && this.getters[i].value.match(new RegExp("([^a-zA-Z_$.])" + shorthand + "([^0-9a-zA-Z_$])", "g"))) {
return true;
} else if (typeof this.getters[i].type == 'string' && this.getters[i].type == shorthand) {
return true;
}
}
for (i in this.setters) {
matches = this.setters[i].value.match(AS3Pattern.VARIABLE_DECLARATION[1]);
for (j in matches) {
if (matches[j].split(":")[1] == shorthand) {
return true;
}
}
//See if the function definition or variable assigment have a need for this package
for (j in this.setters[i].argList) {
if (typeof this.setters[i].argList[j].type == 'string' && this.setters[i].argList[j].type == shorthand) {
return true;
}
}
if (typeof this.setters[i].value == 'string' && this.setters[i].value.match(new RegExp("([^a-zA-Z_$.])" + shorthand + "([^0-9a-zA-Z_$])", "g"))) {
return true;
} else if (typeof this.setters[i].type == 'string' && this.setters[i].type == shorthand) {
return true;
}
}
for (i in this.staticGetters) {
matches = this.staticGetters[i].value.match(AS3Pattern.VARIABLE_DECLARATION[1]);
for (j in matches) {
if (matches[j].split(":")[1] == shorthand) {
return true;
}
}
//See if the function definition or variable assigment have a need for this package
for (j in this.staticGetters[i].argList) {
if (typeof this.staticGetters[i].argList[j].type == 'string' && this.staticGetters[i].argList[j].type == shorthand) {
return true;
}
}
if (typeof this.staticGetters[i].value == 'string' && this.staticGetters[i].value.match(new RegExp("([^a-zA-Z_$.])" + shorthand + "([^0-9a-zA-Z_$])", "g"))) {
return true;
} else if (typeof this.staticGetters[i].type == 'string' && this.staticGetters[i].type == shorthand) {
return true;
}
}
for (i in this.staticSetters) {
matches = this.staticSetters[i].value.match(AS3Pattern.VARIABLE_DECLARATION[1]);
for (j in matches) {
if (matches[j].split(":")[1] == shorthand) {
return true;
}
}
for (j in this.staticSetters[i].argList) {
if (typeof this.staticSetters[i].argList[j].type == 'string' && this.staticSetters[i].argList[j].type == shorthand) {
return true;
}
}
//See if the function definition or variable assigment have a need for this package
if (typeof this.staticSetters[i].value == 'string' && this.staticSetters[i].value.match(new RegExp("([^a-zA-Z_$.])" + shorthand + "([^0-9a-zA-Z_$])", "g"))) {
return true;
} else if (typeof this.staticSetters[i].type == 'string' && this.staticSetters[i].type == shorthand) {
return true;
}
}
var classMember;
// Same logic as checkMembersWithAssignments()
// For each member that has an assignment at the top-level scope
for (i = 0; i < this.membersWithAssignments.length; i++) {
classMember = this.membersWithAssignments[i];
// Make a dumb attempt to identify use of the class as assignments here
if (classMember.value && classMember.value.indexOf(shorthand) >= 0 && !(this.parentDefinition && this.parentDefinition.packageName + "." + this.parentDefinition.className === pkg)) {
return true;
}
}
return false;
};
AS3Class.prototype.addImport = function(pkg) {
if (this.imports.indexOf(pkg) < 0) {
this.imports.push(pkg);
}
};
AS3Class.prototype.addExtraImport = function(pkg) {
if (this.importExtras.indexOf(pkg) < 0) {
this.importExtras.push(pkg);
}
};
AS3Class.prototype.findParents = function(classes) {
if (!this.parent) {
return;
}
for (var i in classes) {
//Only gather vars from the parent
if (classes[i] != this && this.parent == classes[i].className) {
this.parentDefinition = classes[i]; //Found our parent
return;
}
}
};
AS3Class.prototype.checkMembersWithAssignments = function() {
var i;
var j;
var classMember;
// If the type of this param is a Class
for (i = 0; i < this.membersWithAssignments.length; i++) {
classMember = this.membersWithAssignments[i];
// Make a dumb attempt to identify use of the class as assignments here
for (j in this.imports) {
if (this.packageMap[this.imports[j]] && classMember.value.indexOf(this.packageMap[this.imports[j]].className) >= 0 && this.parentDefinition !== this.packageMap[this.imports[j]]) {
// If this is a token that matches a class from an import statement, store it in the filtered classMap
this.classMapFiltered[this.packageMap[this.imports[j]].className] = this.packageMap[this.imports[j]];
}
}
}
};
AS3Class.prototype.stringifyFunc = function(fn) {
var buffer = "";
if (fn instanceof AS3Function) {
//Functions need to be handled differently
//Prepend sub-type if it exists
if (fn.subType) {
buffer += fn.subType + '_';
}
//Print out the rest of the name and start the function definition
buffer += fn.name
buffer += " = function(";
//Concat all of the arguments together
tmpArr = [];
for (j = 0; j < fn.argList.length; j++) {
if (!fn.argList[j].isRestParam) {
tmpArr.push(fn.argList[j].name);
}
}
buffer += tmpArr.join(", ") + ") ";
//Function definition is finally added
buffer += fn.value + ";\n";
} else if (fn instanceof AS3Variable) {
//Variables can be added immediately
buffer += fn.name;
buffer += " = " + fn.value + ";\n";
}
return buffer;
};
AS3Class.prototype.process = function(classes) {
var self = this;
var i;
var index;
var currParent = this;
var allMembers = [];
var allFuncs = [];
var allStaticMembers = [];
var allStaticFuncs = [];
while (currParent) {
//Parse members of this parent
for (i in currParent.setters) {
allMembers.push(currParent.setters[i]);
}
for (i in currParent.staticSetters) {
allStaticMembers.push(currParent.staticSetters[i]);
}
for (i in currParent.getters) {
allMembers.push(currParent.getters[i]);
}
for (i in currParent.staticGetters) {
allStaticMembers.push(currParent.staticGetters[i]);
}
for (i in currParent.members) {
allMembers.push(currParent.members[i]);
}
for (i in currParent.staticMembers) {
allStaticMembers.push(currParent.staticMembers[i]);
}
//Go to the next parent
currParent = currParent.parentDefinition;
}
//Add copies of the setters and getters to the "all" arrays (for convenience)
for (i in this.setters) {
if (this.setters[i] instanceof AS3Function) {
allFuncs.push(this.setters[i]);
}
}
for (i in this.staticSetters) {
if (this.staticSetters[i] instanceof AS3Function) {
allStaticFuncs.push(this.staticSetters[i]);
}
}
for (i in this.getters) {
if (this.getters[i] instanceof AS3Function) {
allFuncs.push(this.getters[i]);
}
}
for (i in this.staticGetters) {
if (this.staticGetters[i] instanceof AS3Function) {
allStaticFuncs.push(this.staticGetters[i]);
}
}
for (i in this.members) {
if (this.members[i] instanceof AS3Function) {
allFuncs.push(this.members[i]);
}
if (this.members[i] instanceof AS3Variable) {
// Fix any obvious assignments that rely on implicit static class name (only works for simple statements)
if (this.members[i].value && this.retrieveField(this.members[i].value.replace(/^([a-zA-Z_$][0-9a-zA-Z_$]*)(.*?)$/g, "$1"), true)) {
this.members[i].value = this.className + '.' + this.members[i].value;
}
}
}
for (i in this.staticMembers) {
if (this.staticMembers[i] instanceof AS3Function) {
allStaticFuncs.push(this.staticMembers[i]);
}
if (this.staticMembers[i] instanceof AS3Variable) {
// Fix any obvious assignments that rely on implicit static class name (only works for simple statements)
if (this.staticMembers[i].value && this.retrieveField(this.staticMembers[i].value.replace(/^([a-zA-Z_$][0-9a-zA-Z_$]*)(.*?)$/g, "$1"), true)) {
this.staticMembers[i].value = this.className + '.' + this.staticMembers[i].value;
}
}
}
// Insert $init function for instantiations
for (i in allFuncs) {
Main.debug("Now parsing function: " + this.className + ":" + allFuncs[i].name);
allFuncs[i].value = AS3Parser.parseFunc(this, allFuncs[i].value, allFuncs[i].buildLocalVariableStack(), allFuncs[i].isStatic)[0];
allFuncs[i].value = AS3Parser.checkArguments(allFuncs[i]);
if (allFuncs[i].name === "$init") {
//Inject instantiations here
allFuncs[i].value = AS3Parser.injectInstantiations(this, allFuncs[i]);
}
if (allFuncs[i].name === this.className) {
//Inject $init() into constructor
allFuncs[i].value = AS3Parser.injectInit(this, allFuncs[i]);
}
allFuncs[i].value = AS3Parser.cleanup(allFuncs[i].value);
//Fix supers
allFuncs[i].value = allFuncs[i].value.replace(/super\.(.*?)\(/g, this.parent + '.prototype.$1.call(this, ').replace(/\.call\(this,\s*\)/g, ".call(this)");
allFuncs[i].value = allFuncs[i].value.replace(/super\(/g, this.parent + '.call(this, ').replace(/\.call\(this,\s*\)/g, ".call(this)");
allFuncs[i].value = allFuncs[i].value.replace(new RegExp("this[.]" + this.parent, "g"), this.parent); //Fix extra 'this' on the parent
}
for (i in allStaticFuncs) {
Main.debug("Now parsing static function: " + this.className + ":" + allStaticFuncs[i].name);
allStaticFuncs[i].value = AS3Parser.parseFunc(this, allStaticFuncs[i].value, allStaticFuncs[i].buildLocalVariableStack(), allStaticFuncs[i].isStatic)[0];
allStaticFuncs[i].value = AS3Parser.checkArguments(allStaticFuncs[i]);
allStaticFuncs[i].value = AS3Parser.cleanup(allStaticFuncs[i].value);
}
};
AS3Class.prototype.toString = function() {
//Outputs the class inside a JS function
var i;
var j;
var buffer = "";
if (this.requires.length > 0) {
if (this.safeRequire) {
for (i in this.requires) {
buffer += 'var ' + this.requires[i].substring(1, this.requires[i].length - 1) + ' = (function () { try { return require(' + this.requires[i] + '); } catch(e) { return undefined; }})();\n';
}
} else {
for (i in this.requires) {
buffer += 'var ' + this.requires[i].substring(1, this.requires[i].length - 1) + ' = require(' + this.requires[i] + ');\n';
}
}
buffer += "\n";
}
var tmpArr = null;
//Parent class must be imported if it exists
if (this.parentDefinition) {
buffer += "var " + this.parentDefinition.className + " = module.import('" + this.parentDefinition.packageName + "', '" + this.parentDefinition.className + "');\n";
}
//Create refs for all the other classes
if (this.imports.length > 0) {
tmpArr = [];
for (i in this.imports) {
if (!(this.ignoreFlash && this.imports[i].indexOf('flash.') >= 0) && this.parent != this.imports[i].substr(this.imports[i].lastIndexOf('.') + 1) && this.packageName + '.' + this.className != this.imports[i]) //Ignore flash imports
{
// Must be in the filtered map, otherwise no point in writing
if (!this.packageMap[this.imports[i]]) {
Main.warn("Warning, missing class path: " + this.imports[i] + " (found in " + this.packageName + '.' + this.className + ")");
} else if (this.classMapFiltered[this.packageMap[this.imports[i]].className]) {
tmpArr.push(this.imports[i].substr(this.imports[i].lastIndexOf('.') + 1)); //<-This will return characters after the final '.', or the entire String if no '.'
}
}
}
//Join up separated by commas
if (tmpArr.length > 0) {
buffer += 'var ';
buffer += tmpArr.join(", ") + ";\n";
}
}
//Check for injection function code
var injectedText = "";
for (i in this.imports) {
if (!(this.ignoreFlash && this.imports[i].indexOf('flash.') >= 0) && this.packageName + '.' + this.className != this.imports[i] && !(this.parentDefinition && this.parentDefinition.packageName + '.' + this.parentDefinition.className == this.imports[i])) //Ignore flash imports and parent for injections
{
// Must be in the filtered map, otherwise no point in writing
if (!this.packageMap[this.imports[i]]) {
Main.warn("Warning, missing class path: " + this.imports[i] + " (found in " + this.packageName + '.' + this.className + ")");
} else if (this.classMapFiltered[this.packageMap[this.imports[i]].className]) {
injectedText += "\t" + this.imports[i].substr(this.imports[i].lastIndexOf('.') + 1) + " = module.import('" + this.packageMap[this.imports[i]].packageName + "', '" + this.packageMap[this.imports[i]].className + "');\n";
}
}
}
if (injectedText.length > 0) {
buffer += "module.inject = function () {\n";
buffer += injectedText;
buffer += "};\n";
}
buffer += '\n';
buffer += (this.fieldMap[this.className]) ? "var " + this.stringifyFunc(this.fieldMap[this.className]) : "var " + this.className + " = function " + this.className + "() {};";
buffer += '\n';
buffer += '\n';
if (this.parent) {
//Extend parent if necessary
buffer += this.className + ".prototype = Object.create(" + this.parent + ".prototype);";
}
buffer += '\n\n';
// Deal with static member assigments
if (this.staticMembers.length > 0) {
//Place defaults first
for (i in this.staticMembers) {
if (this.staticMembers[i] instanceof AS3Function) {
buffer += this.className + "." + this.stringifyFunc(this.staticMembers[i]);
} else if (this.staticMembers[i].type === "Number" || this.staticMembers[i].type === "int" || this.staticMembers[i].type === "uint") {
if (isNaN(parseInt(this.staticMembers[i].value))) {
buffer += this.className + "." + this.staticMembers[i].name + ' = 0;\n';
} else {
buffer += this.className + "." + this.stringifyFunc(this.staticMembers[i]);
}
} else if (this.staticMembers[i].type === "Boolean") {
buffer += this.className + "." + this.staticMembers[i].name + ' = false;\n';
} else {
buffer += this.className + "." + this.staticMembers[i].name + ' = null;\n';
}
}
for (i in this.staticGetters) {
buffer += this.className + "." + this.stringifyFunc(this.staticGetters[i]);
}
for (i in this.staticSetters) {
buffer += this.className + "." + this.stringifyFunc(this.staticSetters[i]);
}
buffer += '\n';
buffer += this.className + ".$cinit = function () {\n";
// Now do the assignments for the rest
for (i in this.staticMembers) {
if (!(this.staticMembers[i] instanceof AS3Function)) {
buffer += "\t" + AS3Parser.cleanup(this.className + '.' + this.staticMembers[i].name + ' = ' + this.staticMembers[i].value + ";\n");
}
}
buffer += '\n';
buffer += "};\n";
}
buffer += "\n";
for (i in this.getters) {
buffer += this.className + ".prototype." + this.stringifyFunc(this.getters[i]);
}
for (i in this.setters) {
buffer += this.className + ".prototype." + this.stringifyFunc(this.setters[i]);
}
for (i in this.members) {
if (this.members[i].name === this.className) {
continue;
}
if (this.members[i] instanceof AS3Function || (AS3Class.nativeTypes.indexOf(this.members[i].type) >= 0 && this.members[i].value)) {
buffer += this.className + ".prototype." + this.stringifyFunc(this.members[i]); //Print functions immediately
} else if (this.members[i].type === "Number" || this.members[i].type === "int" || this.members[i].type === "uint") {
if (isNaN(parseInt(this.members[i].value))) {
buffer += this.className + ".prototype." + this.members[i].name + ' = 0;\n';
} else {
buffer += this.className + ".prototype." + this.stringifyFunc(this.members[i]);
}
} else if (this.members[i].type === "Boolean") {
buffer += this.className + ".prototype." + this.members[i].name + ' = false;\n';