| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript.ast; |
| 8 |
|
| 9 |
import org.mozilla.javascript.Node; |
| 10 |
import org.mozilla.javascript.Token; |
| 11 |
|
| 12 |
import java.util.ArrayList; |
| 13 |
import java.util.Collections; |
| 14 |
import java.util.HashMap; |
| 15 |
import java.util.List; |
| 16 |
import java.util.Map; |
| 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 |
public class FunctionNode extends ScriptNode { |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public static final int FUNCTION_STATEMENT = 1; |
| 64 |
public static final int FUNCTION_EXPRESSION = 2; |
| 65 |
public static final int FUNCTION_EXPRESSION_STATEMENT = 3; |
| 66 |
public static final int ARROW_FUNCTION = 4; |
| 67 |
|
| 68 |
public static enum Form { FUNCTION, GETTER, SETTER, METHOD } |
| 69 |
|
| 70 |
private static final List<AstNode> NO_PARAMS = |
| 71 |
Collections.unmodifiableList(new ArrayList<AstNode>()); |
| 72 |
|
| 73 |
private Name functionName; |
| 74 |
private List<AstNode> params; |
| 75 |
private AstNode body; |
| 76 |
private boolean isExpressionClosure; |
| 77 |
private Form functionForm = Form.FUNCTION; |
| 78 |
private int lp = -1; |
| 79 |
private int rp = -1; |
| 80 |
|
| 81 |
|
| 82 |
private int functionType; |
| 83 |
private boolean needsActivation; |
| 84 |
private boolean isGenerator; |
| 85 |
private List<Node> generatorResumePoints; |
| 86 |
private Map<Node,int[]> liveLocals; |
| 87 |
private AstNode memberExprNode; |
| 88 |
|
| 89 |
{ |
| 90 |
type = Token.FUNCTION; |
| 91 |
} |
| 92 |
|
| 93 |
public FunctionNode() { |
| 94 |
} |
| 95 |
|
| 96 |
public FunctionNode(int pos) { |
| 97 |
super(pos); |
| 98 |
} |
| 99 |
|
| 100 |
public FunctionNode(int pos, Name name) { |
| 101 |
super(pos); |
| 102 |
setFunctionName(name); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public Name getFunctionName() { |
| 110 |
return functionName; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public void setFunctionName(Name name) { |
| 118 |
functionName = name; |
| 119 |
if (name != null) |
| 120 |
name.setParent(this); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public String getName() { |
| 128 |
return functionName != null ? functionName.getIdentifier() : ""; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public List<AstNode> getParams() { |
| 137 |
return params != null ? params : NO_PARAMS; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public void setParams(List<AstNode> params) { |
| 146 |
if (params == null) { |
| 147 |
this.params = null; |
| 148 |
} else { |
| 149 |
if (this.params != null) |
| 150 |
this.params.clear(); |
| 151 |
for (AstNode param : params) |
| 152 |
addParam(param); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public void addParam(AstNode param) { |
| 163 |
assertNotNull(param); |
| 164 |
if (params == null) { |
| 165 |
params = new ArrayList<AstNode>(); |
| 166 |
} |
| 167 |
params.add(param); |
| 168 |
param.setParent(this); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public boolean isParam(AstNode node) { |
| 177 |
return params == null ? false : params.contains(node); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public AstNode getBody() { |
| 187 |
return body; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public void setBody(AstNode body) { |
| 202 |
assertNotNull(body); |
| 203 |
this.body = body; |
| 204 |
if (Boolean.TRUE.equals(body.getProp(Node.EXPRESSION_CLOSURE_PROP))) { |
| 205 |
setIsExpressionClosure(true); |
| 206 |
} |
| 207 |
int absEnd = body.getPosition() + body.getLength(); |
| 208 |
body.setParent(this); |
| 209 |
this.setLength(absEnd - this.position); |
| 210 |
setEncodedSourceBounds(this.position, absEnd); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public int getLp() { |
| 217 |
return lp; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public void setLp(int lp) { |
| 224 |
this.lp = lp; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public int getRp() { |
| 231 |
return rp; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public void setRp(int rp) { |
| 238 |
this.rp = rp; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public void setParens(int lp, int rp) { |
| 245 |
this.lp = lp; |
| 246 |
this.rp = rp; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public boolean isExpressionClosure() { |
| 253 |
return isExpressionClosure; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public void setIsExpressionClosure(boolean isExpressionClosure) { |
| 260 |
this.isExpressionClosure = isExpressionClosure; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public boolean requiresActivation() { |
| 274 |
return needsActivation; |
| 275 |
} |
| 276 |
|
| 277 |
public void setRequiresActivation() { |
| 278 |
needsActivation = true; |
| 279 |
} |
| 280 |
|
| 281 |
public boolean isGenerator() { |
| 282 |
return isGenerator; |
| 283 |
} |
| 284 |
|
| 285 |
public void setIsGenerator() { |
| 286 |
isGenerator = true; |
| 287 |
} |
| 288 |
|
| 289 |
public void addResumptionPoint(Node target) { |
| 290 |
if (generatorResumePoints == null) |
| 291 |
generatorResumePoints = new ArrayList<Node>(); |
| 292 |
generatorResumePoints.add(target); |
| 293 |
} |
| 294 |
|
| 295 |
public List<Node> getResumptionPoints() { |
| 296 |
return generatorResumePoints; |
| 297 |
} |
| 298 |
|
| 299 |
public Map<Node,int[]> getLiveLocals() { |
| 300 |
return liveLocals; |
| 301 |
} |
| 302 |
|
| 303 |
public void addLiveLocals(Node node, int[] locals) { |
| 304 |
if (liveLocals == null) |
| 305 |
liveLocals = new HashMap<Node,int[]>(); |
| 306 |
liveLocals.put(node, locals); |
| 307 |
} |
| 308 |
|
| 309 |
@Override |
| 310 |
public int addFunction(FunctionNode fnNode) { |
| 311 |
int result = super.addFunction(fnNode); |
| 312 |
if (getFunctionCount() > 0) { |
| 313 |
needsActivation = true; |
| 314 |
} |
| 315 |
return result; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
public int getFunctionType() { |
| 322 |
return functionType; |
| 323 |
} |
| 324 |
|
| 325 |
public void setFunctionType(int type) { |
| 326 |
functionType = type; |
| 327 |
} |
| 328 |
|
| 329 |
public boolean isMethod() { |
| 330 |
return functionForm == Form.GETTER || functionForm == Form.SETTER || functionForm == Form.METHOD; |
| 331 |
} |
| 332 |
|
| 333 |
public boolean isGetterMethod() { |
| 334 |
return functionForm == Form.GETTER; |
| 335 |
} |
| 336 |
|
| 337 |
public boolean isSetterMethod() { |
| 338 |
return functionForm == Form.SETTER; |
| 339 |
} |
| 340 |
|
| 341 |
public boolean isNormalMethod() { |
| 342 |
return functionForm == Form.METHOD; |
| 343 |
} |
| 344 |
|
| 345 |
public void setFunctionIsGetterMethod() { |
| 346 |
functionForm = Form.GETTER; |
| 347 |
} |
| 348 |
|
| 349 |
public void setFunctionIsSetterMethod() { |
| 350 |
functionForm = Form.SETTER; |
| 351 |
} |
| 352 |
|
| 353 |
public void setFunctionIsNormalMethod() { |
| 354 |
functionForm = Form.METHOD; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
public void setMemberExprNode(AstNode node) { |
| 368 |
memberExprNode = node; |
| 369 |
if (node != null) |
| 370 |
node.setParent(this); |
| 371 |
} |
| 372 |
|
| 373 |
public AstNode getMemberExprNode() { |
| 374 |
return memberExprNode; |
| 375 |
} |
| 376 |
|
| 377 |
@Override |
| 378 |
public String toSource(int depth) { |
| 379 |
StringBuilder sb = new StringBuilder(); |
| 380 |
boolean isArrow = functionType == ARROW_FUNCTION; |
| 381 |
if (!isMethod()) { |
| 382 |
sb.append(makeIndent(depth)); |
| 383 |
if (!isArrow) { |
| 384 |
sb.append("function"); |
| 385 |
} |
| 386 |
} |
| 387 |
if (functionName != null) { |
| 388 |
sb.append(" "); |
| 389 |
sb.append(functionName.toSource(0)); |
| 390 |
} |
| 391 |
if (params == null) { |
| 392 |
sb.append("() "); |
| 393 |
} else if (isArrow && lp == -1) { |
| 394 |
|
| 395 |
printList(params, sb); |
| 396 |
sb.append(" "); |
| 397 |
} else { |
| 398 |
sb.append("("); |
| 399 |
printList(params, sb); |
| 400 |
sb.append(") "); |
| 401 |
} |
| 402 |
if (isArrow) { |
| 403 |
sb.append("=> "); |
| 404 |
} |
| 405 |
if (isExpressionClosure) { |
| 406 |
AstNode body = getBody(); |
| 407 |
if (body.getLastChild() instanceof ReturnStatement) { |
| 408 |
|
| 409 |
body = ((ReturnStatement) body.getLastChild()).getReturnValue(); |
| 410 |
sb.append(body.toSource(0)); |
| 411 |
if (functionType == FUNCTION_STATEMENT) { |
| 412 |
sb.append(";"); |
| 413 |
} |
| 414 |
} else { |
| 415 |
|
| 416 |
sb.append(" "); |
| 417 |
sb.append(body.toSource(0)); |
| 418 |
} |
| 419 |
} else { |
| 420 |
sb.append(getBody().toSource(depth).trim()); |
| 421 |
} |
| 422 |
if (functionType == FUNCTION_STATEMENT || isMethod()) { |
| 423 |
sb.append("\n"); |
| 424 |
} |
| 425 |
return sb.toString(); |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
@Override |
| 434 |
public void visit(NodeVisitor v) { |
| 435 |
if (v.visit(this)) { |
| 436 |
if (functionName != null) { |
| 437 |
functionName.visit(v); |
| 438 |
} |
| 439 |
for (AstNode param : getParams()) { |
| 440 |
param.visit(v); |
| 441 |
} |
| 442 |
getBody().visit(v); |
| 443 |
if (!isExpressionClosure) { |
| 444 |
if (memberExprNode != null) { |
| 445 |
memberExprNode.visit(v); |
| 446 |
} |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
} |