OLD | NEW |
(Empty) | |
| 1 // Copyright (C) 2012 Google Inc. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 /** |
| 16 * @fileoverview Mitigate deviations between SES and ES5-strict code |
| 17 * by rewriting programs where possible. The output of this stage is |
| 18 * outside the TCB. |
| 19 * See http://code.google.com/p/google-caja/wiki/SES#Source-SES_vs_Target-SES |
| 20 * for a list of these differences. |
| 21 * |
| 22 * TODO(jasvir): Rewrite top level function declarations |
| 23 * |
| 24 * Note that the parse tree manipulated in this file uses the SpiderMonkey |
| 25 * AST format |
| 26 * (https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API) |
| 27 *· |
| 28 * @author Jasvir Nagra (jasvir@google.com) |
| 29 * @overrides ses |
| 30 */ |
| 31 |
| 32 (function() { |
| 33 function introducesScope(node) { |
| 34 return node.type === 'FunctionExpression' || |
| 35 node.type === 'FunctionDeclaration'; |
| 36 } |
| 37 ·· |
| 38 function isTypeOf(node) { |
| 39 return (node.type === 'UnaryExpression' && |
| 40 node.operator === 'typeof' && |
| 41 !node.synthetic); |
| 42 } |
| 43 ·· |
| 44 function isVariableDecl(node) { |
| 45 return (node.type === 'VariableDeclaration'); |
| 46 } |
| 47 |
| 48 /** |
| 49 * Rewrite var decls in place into assignments on the global object |
| 50 * turning expression "var x, y = 2, z" to |
| 51 * window.x = window.x, window.y = 2, window.z = window.z |
| 52 */ |
| 53 function rewriteVars(node) { |
| 54 var assignments = []; |
| 55 node.declarations.forEach(function(decl) { |
| 56 assignments.push({ |
| 57 "type": "AssignmentExpression", |
| 58 "operator": "=", |
| 59 "left": globalVarAst(decl.id), |
| 60 "right": decl.init || globalVarAst(decl.id) |
| 61 }); |
| 62 ·· |
| 63 }); |
| 64 node.type = "ExpressionStatement"; |
| 65 node.expression = { |
| 66 "type": "SequenceExpression", |
| 67 "expressions": assignments |
| 68 }; |
| 69 } |
| 70 ·· |
| 71 function globalVarAst(varName) { |
| 72 return { |
| 73 "type": "MemberExpression", |
| 74 "object": { |
| 75 "type": "Identifier", |
| 76 "name": "window" |
| 77 }, |
| 78 "property": varName |
| 79 }; |
| 80 } |
| 81 ·· |
| 82 /** |
| 83 * Rewrite node in place turning expression "typeof x" to |
| 84 * (function() { try { typeof x } catch (e) { return "undefined" } })() |
| 85 */ |
| 86 function rewriteTypeOf(node) { |
| 87 var arg = node.argument; |
| 88 node.type = "CallExpression"; |
| 89 node.arguments = []; |
| 90 node.callee = { |
| 91 "type": "FunctionExpression", |
| 92 "id": null, |
| 93 "params": [], |
| 94 "body": { |
| 95 "type": "BlockStatement", |
| 96 "body": [{ |
| 97 "type": "TryStatement", |
| 98 "block": { |
| 99 "type": "BlockStatement", |
| 100 "body": [{ |
| 101 "type": "ReturnStatement", |
| 102 "argument": { |
| 103 "synthetic": true, |
| 104 "type": "UnaryExpression", |
| 105 "operator": "typeof", |
| 106 "prefix": true, |
| 107 "argument": arg |
| 108 } |
| 109 } |
| 110 ] |
| 111 }, |
| 112 "handlers": [{ |
| 113 "type": "CatchClause", |
| 114 "param": { |
| 115 "type": "Identifier", |
| 116 "name": "e" |
| 117 }, |
| 118 "guard": null, |
| 119 "body": { |
| 120 "type": "BlockStatement", |
| 121 "body": [{ |
| 122 "type": "ReturnStatement", |
| 123 "argument": { |
| 124 "type": "Literal", |
| 125 "value": "undefined", |
| 126 "raw": "'undefined'" |
| 127 } |
| 128 }] |
| 129 } |
| 130 } |
| 131 ], |
| 132 "finalizer": null |
| 133 } |
| 134 ] |
| 135 } |
| 136 }; |
| 137 } |
| 138 ·· |
| 139 function resolveOptions(options) { |
| 140 function resolve(opt, defaultOption) { |
| 141 return (options && opt in options) ? opt : defaultOption; |
| 142 } |
| 143 var resolved = {}; |
| 144 resolved.rewriteTopLevelVars = resolve('rewriteTopLevelVars', true); |
| 145 resolved.rewriteTypeOf = resolve('rewriteTypeOf', true); |
| 146 return resolved; |
| 147 } |
| 148 |
| 149 ses.mitigateGotchas = function(programSrc, options) { |
| 150 try { |
| 151 options = resolveOptions(options); |
| 152 var scopeLevel = 0; |
| 153 var ast = ses.rewriter_.parse(programSrc); |
| 154 ses.rewriter_.traverse(ast, { |
| 155 enter: function enter(node) { |
| 156 if (introducesScope(node)) { |
| 157 scopeLevel++; |
| 158 } else if (options.rewriteTypeOf && isTypeOf(node)) { |
| 159 rewriteTypeOf(node); |
| 160 } else if (options.rewriteTopLevelVars && |
| 161 isVariableDecl(node) && scopeLevel === 0) { |
| 162 rewriteVars(node); |
| 163 } |
| 164 }, |
| 165 leave: function leave(node) { |
| 166 if (introducesScope(node)) { |
| 167 scopeLevel--; |
| 168 } |
| 169 } |
| 170 }); |
| 171 return ses.rewriter_.generate(ast); |
| 172 } catch (e) { |
| 173 ses.logger.warn("Failed to mitigate SES gotchas. Proceeding anyways.", e); |
| 174 return programSrc; |
| 175 } |
| 176 }; |
| 177 |
| 178 })(); |
OLD | NEW |