2013-05-22 22:39:19 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2013 IBM Corporation.
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
*
|
|
|
|
* Contributors:
|
|
|
|
* IBM Corporation - initial API and implementation
|
|
|
|
*******************************************************************************/
|
2011-05-23 16:27:07 +00:00
|
|
|
package com.ibm.wala.cast.js.html;
|
|
|
|
|
|
|
|
import com.ibm.wala.cast.ir.translator.TranslatorToIR;
|
|
|
|
import com.ibm.wala.cast.js.loader.JavaScriptLoader;
|
|
|
|
import com.ibm.wala.cast.js.loader.JavaScriptLoaderFactory;
|
|
|
|
import com.ibm.wala.cast.js.ssa.JSInstructionFactory;
|
|
|
|
import com.ibm.wala.cast.js.translator.JSAstTranslator;
|
|
|
|
import com.ibm.wala.cast.js.translator.JavaScriptTranslatorFactory;
|
2014-03-16 22:05:49 +00:00
|
|
|
import com.ibm.wala.cast.js.types.JavaScriptTypes;
|
2011-05-23 16:27:07 +00:00
|
|
|
import com.ibm.wala.cast.tree.CAst;
|
2013-08-07 19:11:25 +00:00
|
|
|
import com.ibm.wala.cast.tree.CAstEntity;
|
2011-05-23 16:27:07 +00:00
|
|
|
import com.ibm.wala.cast.tree.CAstNode;
|
|
|
|
import com.ibm.wala.cast.tree.impl.CAstImpl;
|
|
|
|
import com.ibm.wala.cast.tree.impl.CAstOperator;
|
2012-10-03 14:53:04 +00:00
|
|
|
import com.ibm.wala.cast.tree.rewrite.CAstRewriterFactory;
|
2013-08-07 19:11:25 +00:00
|
|
|
import com.ibm.wala.cast.tree.visit.CAstVisitor;
|
2011-05-23 16:27:07 +00:00
|
|
|
import com.ibm.wala.classLoader.IClassLoader;
|
|
|
|
import com.ibm.wala.ipa.cha.IClassHierarchy;
|
2014-03-16 22:05:49 +00:00
|
|
|
import com.ibm.wala.types.TypeReference;
|
2011-05-23 16:27:07 +00:00
|
|
|
|
|
|
|
public class WebPageLoaderFactory extends JavaScriptLoaderFactory {
|
|
|
|
|
|
|
|
public WebPageLoaderFactory(JavaScriptTranslatorFactory factory) {
|
|
|
|
super(factory);
|
|
|
|
}
|
2012-01-06 21:17:39 +00:00
|
|
|
|
Fix nearly all Eclipse warnings about using raw types
Along the way, I also converted many "for (;;)" loops into modern
"for (:)" loops. I didn't systematically look for all opportunities
to do this, though. I merely made this change where I was already
converting raw Iterator uses into modern Iterator<...> uses.
Better use of generics also allowed many casts to become statically
redundant. I have removed all such redundant casts.
Only three raw-types warnings remain after this batch of fixes. All
three involve raw uses of CallGraphBuilder. I've tried to fix these
too, but it quickly snowballs into a cascade of changes that may or
may not eventually reach a statically-type-save fixed point. I may
give these last few problem areas another go in the future. For now,
though, the hundreds of other fixes seem worth keeping even if there
are a few stragglers.
This commit may change some public APIs, but only by making weaker
type signatures stronger by replacing raw types with generic types.
For example, we may change something like "Set" into "Set<String>",
but we're not adding new arguments, changing any
underlying (post-generics-erasure) types, etc.
2017-07-09 18:38:35 +00:00
|
|
|
public WebPageLoaderFactory(JavaScriptTranslatorFactory factory, CAstRewriterFactory<?, ?> preprocessor) {
|
2012-01-06 21:17:39 +00:00
|
|
|
super(factory, preprocessor);
|
|
|
|
}
|
2011-05-23 16:27:07 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected IClassLoader makeTheLoader(IClassHierarchy cha) {
|
2012-01-06 21:17:39 +00:00
|
|
|
return new JavaScriptLoader( cha, translatorFactory, preprocessor ) {
|
2011-05-23 16:27:07 +00:00
|
|
|
@Override
|
|
|
|
protected TranslatorToIR initTranslator() {
|
|
|
|
return new JSAstTranslator(this) {
|
|
|
|
private final CAst Ast = new CAstImpl();
|
|
|
|
|
2013-11-26 22:20:16 +00:00
|
|
|
private boolean isNestedWithinScriptBody(WalkContext context) {
|
|
|
|
return isScriptBody(context) || context.getName().contains("__WINDOW_MAIN__");
|
|
|
|
}
|
|
|
|
|
2011-05-23 16:27:07 +00:00
|
|
|
private boolean isScriptBody(WalkContext context) {
|
|
|
|
return context.top().getName().equals( "__WINDOW_MAIN__" );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-03-16 22:05:49 +00:00
|
|
|
protected int doGlobalRead(CAstNode n, WalkContext context, String name, TypeReference type) {
|
2011-05-23 16:27:07 +00:00
|
|
|
int result = context.currentScope().allocateTempValue();
|
2014-05-28 17:34:25 +00:00
|
|
|
if (isNestedWithinScriptBody(context) && ! "$$undefined".equals(name) && !"window".equals(name) && !name.startsWith("$$destructure")) {
|
2011-05-23 16:27:07 +00:00
|
|
|
|
|
|
|
// check if field is defined on 'window'
|
2014-03-16 22:05:49 +00:00
|
|
|
int windowVal = isScriptBody(context)? super.doLocalRead(context, "this", JavaScriptTypes.Root): super.doGlobalRead(n, context, "window", type);
|
2011-05-23 16:27:07 +00:00
|
|
|
int isDefined = context.currentScope().allocateTempValue();
|
|
|
|
context.currentScope().getConstantValue(name);
|
|
|
|
doIsFieldDefined(context, isDefined, windowVal, Ast.makeConstant(name));
|
|
|
|
context.cfg().addInstruction(
|
2014-04-09 11:40:22 +00:00
|
|
|
insts.ConditionalBranchInstruction(context.cfg().getCurrentInstruction(),
|
2011-05-23 16:27:07 +00:00
|
|
|
translateConditionOpcode(CAstOperator.OP_NE),
|
|
|
|
null,
|
|
|
|
isDefined,
|
2018-06-04 18:16:36 +00:00
|
|
|
context.currentScope().getConstantValue(Integer.valueOf(0)),
|
2014-07-08 18:44:06 +00:00
|
|
|
-1));
|
2011-05-23 16:27:07 +00:00
|
|
|
PreBasicBlock srcB = context.cfg().getCurrentBlock();
|
|
|
|
|
|
|
|
// field lookup of value
|
|
|
|
context.cfg().newBlock(true);
|
2014-04-09 11:40:22 +00:00
|
|
|
context.cfg().addInstruction(((JSInstructionFactory) insts).GetInstruction(context.cfg().getCurrentInstruction(), result, windowVal, name));
|
2014-07-08 18:44:06 +00:00
|
|
|
context.cfg().newBlock(true);
|
2014-07-28 15:09:02 +00:00
|
|
|
context.cfg().addInstruction(insts.GotoInstruction(context.cfg().getCurrentInstruction(), -1));
|
2011-05-23 16:27:07 +00:00
|
|
|
PreBasicBlock trueB = context.cfg().getCurrentBlock();
|
|
|
|
|
|
|
|
// read global
|
|
|
|
context.cfg().newBlock(false);
|
|
|
|
PreBasicBlock falseB = context.cfg().getCurrentBlock();
|
2014-03-16 22:05:49 +00:00
|
|
|
int sr = super.doGlobalRead(n, context, name, type);
|
2014-04-09 11:40:22 +00:00
|
|
|
context.cfg().addInstruction(((JSInstructionFactory) insts).AssignInstruction(context.cfg().getCurrentInstruction(), result, sr));
|
2011-05-23 16:27:07 +00:00
|
|
|
|
|
|
|
// end
|
|
|
|
context.cfg().newBlock(true);
|
|
|
|
|
|
|
|
context.cfg().addEdge(trueB, context.cfg().getCurrentBlock());
|
|
|
|
context.cfg().addEdge(srcB, falseB);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
} else {
|
2014-03-16 22:05:49 +00:00
|
|
|
return super.doGlobalRead(n, context, name, type);
|
2011-05-23 16:27:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-03-16 22:05:49 +00:00
|
|
|
protected void doLocalWrite(WalkContext context, String nm, TypeReference type, int rval) {
|
2014-05-28 17:34:25 +00:00
|
|
|
if (isNestedWithinScriptBody(context) && ! "$$undefined".equals(nm) && !"window".equals(nm) && !nm.startsWith("$$destructure")) {
|
2014-03-16 22:05:49 +00:00
|
|
|
int windowVal = super.doLocalRead(context, "this", type);
|
2011-05-23 16:27:07 +00:00
|
|
|
context.currentScope().getConstantValue(nm);
|
2014-04-09 11:40:22 +00:00
|
|
|
context.cfg().addInstruction(((JSInstructionFactory) insts).PutInstruction(context.cfg().getCurrentInstruction(), windowVal, rval, nm));
|
2011-05-23 16:27:07 +00:00
|
|
|
}
|
|
|
|
|
2014-03-16 22:05:49 +00:00
|
|
|
super.doLocalWrite(context, nm, type, rval);
|
2011-05-23 16:27:07 +00:00
|
|
|
}
|
2013-08-07 19:11:25 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void leaveFunctionStmt(CAstNode n, WalkContext context, CAstVisitor<WalkContext> visitor) {
|
|
|
|
super.leaveFunctionStmt(n, context, visitor);
|
|
|
|
if (isScriptBody(context)) {
|
|
|
|
CAstEntity fn = (CAstEntity) n.getChild(0).getValue();
|
|
|
|
int fnValue = context.currentScope().lookup(fn.getName()).valueNumber();
|
|
|
|
assert fnValue > 0;
|
2014-03-16 22:05:49 +00:00
|
|
|
int windowVal = super.doLocalRead(context, "this", JavaScriptTypes.Function);
|
2014-05-20 15:59:16 +00:00
|
|
|
context.cfg().addInstruction(((JSInstructionFactory) insts).PutInstruction(context.cfg().getCurrentInstruction(), windowVal, fnValue, fn.getName()));
|
2013-08-07 19:11:25 +00:00
|
|
|
}
|
2011-05-23 16:27:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|