JS inline: collect defined functions

This commit is contained in:
Alexey Tsvetkov
2014-07-18 13:10:39 +04:00
committed by Zalim Bashorov
parent 2e38833367
commit 3c7677e73e
2 changed files with 48 additions and 1 deletions
@@ -0,0 +1,47 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.k2js.inline;
import com.google.dart.compiler.backend.js.ast.*;
import java.util.IdentityHashMap;
public class FunctionCollector extends RecursiveJsVisitor {
private final IdentityHashMap<JsName, JsFunction> functions = new IdentityHashMap<JsName, JsFunction>();
public static IdentityHashMap<JsName, JsFunction> collectFunctions(JsNode root) {
FunctionCollector collector = new FunctionCollector();
collector.accept(root);
return collector.functions;
}
@Override
public void visitPropertyInitializer(JsPropertyInitializer x) {
super.visitPropertyInitializer(x);
JsExpression label = x.getLabelExpr();
JsExpression value = x.getValueExpr();
if (label instanceof JsNameRef && value instanceof JsFunction) {
JsName name = ((JsNameRef) label).getName();
JsFunction function = (JsFunction) value;
functions.put(name, function);
}
}
}
@@ -31,7 +31,7 @@ public class JsInliner extends JsVisitorWithContextImpl {
private final JsProgram program;
public static JsProgram process(JsProgram program) {
IdentityHashMap<JsName, JsFunction> functions = new IdentityHashMap<JsName, JsFunction>();
IdentityHashMap<JsName, JsFunction> functions = FunctionCollector.collectFunctions(program);
JsInliner inliner = new JsInliner(program, functions);
return inliner.process();
}