| OLD | NEW |
| 1 // This is a simple script to find all interfaces that a JS code uses, at least | 1 // This is a simple script to find all interfaces that a JS code uses, at least |
| 2 // via Components.interfaces. | 2 // via Components.interfaces. |
| 3 | 3 |
| 4 include("../utils/cleanast.js"); | 4 include("../utils/cleanast.js"); |
| 5 include("../utils/dumpast.js"); | 5 include("../utils/dumpast.js"); |
| 6 | 6 |
| 7 function visit(root_ast, func) { | 7 function visit(root_ast, func) { |
| 8 function v_r(ast, func) { | 8 function v_r(ast, func) { |
| 9 if (ast == null) | 9 if (ast == null) |
| 10 return; | 10 return; |
| 11 func(ast); | 11 func(ast); |
| 12 » » for each (let child in ast.kids) | 12 » » for (let child of ast.kids) |
| 13 v_r(child, func); | 13 v_r(child, func); |
| 14 } | 14 } |
| 15 | 15 |
| 16 function sanitize(ast) { | 16 function sanitize(ast) { |
| 17 if (ast == null) | 17 if (ast == null) |
| 18 return null; | 18 return null; |
| 19 if (ast.op == JSOP_NAME && ast.atom in aliases) { | 19 if (ast.op == JSOP_NAME && ast.atom in aliases) { |
| 20 ast = sanitize(aliases[ast.atom]); | 20 ast = sanitize(aliases[ast.atom]); |
| 21 ast.expanded = true; | 21 ast.expanded = true; |
| 22 } | 22 } |
| 23 let sanitized_ast = { kids: [] }; | 23 let sanitized_ast = { kids: [] }; |
| 24 for (let key in ast) { | 24 for (let key in ast) { |
| 25 if (key == 'kids') { | 25 if (key == 'kids') { |
| 26 » » » » for each (let kid in ast.kids) { | 26 » » » » for (let kid of ast.kids) { |
| 27 sanitized_ast.kids.push(sanitize(kid)); | 27 sanitized_ast.kids.push(sanitize(kid)); |
| 28 } | 28 } |
| 29 } else { | 29 } else { |
| 30 sanitized_ast[key] = ast[key]; | 30 sanitized_ast[key] = ast[key]; |
| 31 } | 31 } |
| 32 } | 32 } |
| 33 return sanitized_ast; | 33 return sanitized_ast; |
| 34 } | 34 } |
| 35 | 35 |
| 36 v_r(sanitize(root_ast), func); | 36 v_r(sanitize(root_ast), func); |
| 37 } | 37 } |
| 38 | 38 |
| 39 let filename; | 39 let filename; |
| 40 | 40 |
| 41 function process_js(ast, f) { | 41 function process_js(ast, f) { |
| 42 filename = f; | 42 filename = f; |
| 43 let global = clean_ast(ast); | 43 let global = clean_ast(ast); |
| 44 » for each (let c in global.constants) { | 44 » for (let c of global.constants) { |
| 45 mark_globals(c); | 45 mark_globals(c); |
| 46 } | 46 } |
| 47 //_print(uneval(aliases)); | 47 //_print(uneval(aliases)); |
| 48 » for each (let v in global.variables) { | 48 » for (let v of global.variables) { |
| 49 visit(v.init, find_interfaces); | 49 visit(v.init, find_interfaces); |
| 50 } | 50 } |
| 51 » for each (let statement in global.code) | 51 » for (let statement of global.code) |
| 52 visit(statement, find_interfaces); | 52 visit(statement, find_interfaces); |
| 53 //_print(uneval(global)); | 53 //_print(uneval(global)); |
| 54 } | 54 } |
| 55 | 55 |
| 56 let aliases = {}; | 56 let aliases = {}; |
| 57 | 57 |
| 58 function mark_globals(constant) { | 58 function mark_globals(constant) { |
| 59 aliases[constant.name] = constant.init; | 59 aliases[constant.name] = constant.init; |
| 60 } | 60 } |
| 61 | 61 |
| 62 function find_interfaces(ast) { | 62 function find_interfaces(ast) { |
| 63 if (ast.op == JSOP_GETPROP && ast.kids[0]) { | 63 if (ast.op == JSOP_GETPROP && ast.kids[0]) { |
| 64 let check = ast.kids[0]; | 64 let check = ast.kids[0]; |
| 65 if (check.atom == "interfaces" && check.kids[0] && | 65 if (check.atom == "interfaces" && check.kids[0] && |
| 66 check.kids[0].atom == "Components") { | 66 check.kids[0].atom == "Components") { |
| 67 _print("Interface " + ast.atom + " used at " + loc2str(a
st)); | 67 _print("Interface " + ast.atom + " used at " + loc2str(a
st)); |
| 68 } else if (ast.atom && ast.atom == "nsIMimeStreamConverter") { | 68 } else if (ast.atom && ast.atom == "nsIMimeStreamConverter") { |
| 69 _print(uneval(ast)); | 69 _print(uneval(ast)); |
| 70 } | 70 } |
| 71 } | 71 } |
| 72 } | 72 } |
| 73 | 73 |
| 74 function loc2str(ast) { | 74 function loc2str(ast) { |
| 75 return filename + ":" + ast.line + ":" + ast.column; | 75 return filename + ":" + ast.line + ":" + ast.column; |
| 76 } | 76 } |
| OLD | NEW |