| OLD | NEW |
| 1 // This script rewrites AST to be compatible with JavaScript 1.5 and decompiles | 1 // This script rewrites AST to be compatible with JavaScript 1.5 and decompiles |
| 2 // the modified tree then | 2 // the modified tree then |
| 3 | 3 |
| 4 include("../scripts/astDecompile.js"); | 4 include("../scripts/astDecompile.js"); |
| 5 include("../utils/beautify.js"); | 5 include("../utils/beautify.js"); |
| 6 | 6 |
| 7 let headerPrinted = false; | 7 let headerPrinted = false; |
| 8 | 8 |
| 9 // See https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API for | 9 // See https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API for |
| 10 // AST structure. | 10 // AST structure. |
| 11 | 11 |
| 12 let options = { | 12 let options = { |
| 13 filename: null, | 13 filename: null, |
| 14 module: false, | 14 module: false, |
| 15 autoload: "", |
| 15 varIndex: 0, | 16 varIndex: 0, |
| 16 indent_size: 2, | 17 indent_size: 2, |
| 17 preserve_newlines: false, | 18 preserve_newlines: false, |
| 18 brace_style: "expand", | 19 brace_style: "expand", |
| 19 source_repo: "" | 20 source_repo: "" |
| 20 }; | 21 }; |
| 21 let global = this; | 22 let global = this; |
| 22 | 23 |
| 23 function Literal(value) | 24 function Literal(value) |
| 24 { | 25 { |
| (...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 489 return { | 490 return { |
| 490 type: "CallExpression", | 491 type: "CallExpression", |
| 491 callee: Member(options.generatorVar, "push"), | 492 callee: Member(options.generatorVar, "push"), |
| 492 arguments: [ast.argument] | 493 arguments: [ast.argument] |
| 493 }; | 494 }; |
| 494 } | 495 } |
| 495 else | 496 else |
| 496 return null; | 497 return null; |
| 497 } | 498 } |
| 498 | 499 |
| 499 process_js = function(ast, filename, args) | 500 var process_js = function(ast, filename, args) |
| 500 { | 501 { |
| 501 for (let arg of args.split(/\s+/)) | 502 for (let arg of args.split(/\s+/)) |
| 502 { | 503 { |
| 503 let match = /^(\w+)\s*=\s*(.*)/.exec(arg); | 504 let match = /^(\w+)\s*=\s*(.*)/.exec(arg); |
| 504 if (match && typeof options[match[1]] == "boolean") | 505 if (match && typeof options[match[1]] == "boolean") |
| 505 options[match[1]] = (match[2] == "true"); | 506 options[match[1]] = (match[2] == "true"); |
| 506 else if (match && typeof options[match[1]] == "string") | 507 else if (match && typeof options[match[1]] == "string") |
| 507 options[match[1]] = match[2]; | 508 options[match[1]] = match[2]; |
| 508 } | 509 } |
| 509 | 510 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 549 | 550 |
| 550 if (options.module) | 551 if (options.module) |
| 551 { | 552 { |
| 552 // Wrap the entire module into a function to give it an independent scope. | 553 // Wrap the entire module into a function to give it an independent scope. |
| 553 // Return exported symbols: | 554 // Return exported symbols: |
| 554 // | 555 // |
| 555 // require.scopes["foobar"] = (function() { | 556 // require.scopes["foobar"] = (function() { |
| 556 // var exports = {}; | 557 // var exports = {}; |
| 557 // ... | 558 // ... |
| 558 // return exports; | 559 // return exports; |
| 559 // })(); | 560 // }); |
| 560 let code = 'require.scopes["' + options.filename + '"] = (function() {\n' + | 561 let code = 'require.scopes["' + options.filename + '"] = (function() {\n' + |
| 561 decompileAST(ast).replace(/^("use strict";\n)?/, | 562 decompileAST(ast).replace(/^("use strict";\n)?/, |
| 562 "$1var exports = {};\n") + | 563 "$1var exports = {};\n") + |
| 563 'return exports;\n' + | 564 'return exports;\n' + |
| 564 '})();\n'; | 565 '});\n'; |
| 565 _print(js_beautify(code, options)); | 566 _print(js_beautify(code, options)); |
| 566 } | 567 } |
| 567 else | 568 else |
| 568 _print(js_beautify(decompileAST(ast), options)); | 569 _print(js_beautify(decompileAST(ast), options)); |
| 569 } | 570 }; |
| 571 |
| 572 var done_processing = function() |
| 573 { |
| 574 let autoloadModules = options.autoload.split(/[\s,]+/); |
| 575 for (let module of autoloadModules) |
| 576 { |
| 577 if (module == "") |
| 578 continue; |
| 579 |
| 580 _print(decompileAST({ |
| 581 type: "ExpressionStatement", |
| 582 expression: { |
| 583 type: "CallExpression", |
| 584 callee: Identifier("require"), |
| 585 arguments: [Literal(module)] |
| 586 } |
| 587 })); |
| 588 } |
| 589 }; |
| OLD | NEW |