Skip to content

Commit

Permalink
Allow JsInliner to inline functions defined in JSNI blocks.
Browse files Browse the repository at this point in the history
Change-Id: I4ec8e1d2d9dce39f720c5232f26c71af9dc87346
  • Loading branch information
rluble committed Sep 10, 2015
1 parent daa2249 commit 03f973a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
Expand Up @@ -3117,6 +3117,14 @@ public void endVisit(JMethod x, Context ctx) {
if (function != null && function.getBody() != null) { if (function != null && function.getBody() != null) {
functionsForJsInlining.add(function); functionsForJsInlining.add(function);
} }
// Add all functions declared inside JSNI blocks as well.
assert function != null;
new JsModVisitor() {
@Override
public void endVisit(JsFunction x, JsContext ctx) {
functionsForJsInlining.add(x);
}
}.accept(function);
} }


currentMethod = null; currentMethod = null;
Expand Down
Expand Up @@ -49,34 +49,54 @@ public void setUp() throws Exception {
} }


public void testInlineJSNIMethod() throws UnableToCompleteException { public void testInlineJSNIMethod() throws UnableToCompleteException {
StringBuilder code = new StringBuilder(); String code = Joiner.on('\n').join(
code.append("package test;\n"); "package test;",
code.append("import com.google.gwt.core.client.GWT;\n"); "import com.google.gwt.core.client.GWT;",
code.append("import com.google.gwt.core.client.RunAsyncCallback;\n"); "import com.google.gwt.core.client.RunAsyncCallback;",
code.append("public class EntryPoint {\n"); "public class EntryPoint {",
code.append(" public static native void inlinableJSNI() /*-{ $wnd; }-*/; "); " public static native void inlinableJSNI() /*-{ $wnd; }-*/; ",
code.append(" public static void functionA() { onModuleLoad(); }\n"); " public static void functionA() { onModuleLoad(); }",
code.append(" public static void onModuleLoad() {\n"); " public static void onModuleLoad() {",
code.append(" inlinableJSNI();"); " inlinableJSNI();",
code.append(" }\n"); " }",
code.append("}\n"); "}");


Set<JsNode> functionForJsInlining = compileSnippetToJS(code.toString()).getRight(); Set<JsNode> functionForJsInlining = compileSnippetToJS(code).getRight();
assertContainsAll(functionForJsInlining, "onModuleLoad", "inlinableJSNI"); assertContainsAll(functionForJsInlining, "onModuleLoad", "inlinableJSNI");
assertDoesNotContainsAny(functionForJsInlining, "functionA"); assertDoesNotContainsAny(functionForJsInlining, "functionA");
} }


public void testInlineFunctionDefinedInJSNI() throws UnableToCompleteException {
String code = Joiner.on('\n').join(
"package test;",
"import com.google.gwt.core.client.GWT;",
"import com.google.gwt.core.client.RunAsyncCallback;",
"public class EntryPoint {",
" public static native void inlinableJSNI() /*-{ (function () { return $wnd;})(); }-*/;",
" public static void functionA() { onModuleLoad(); }",
" public static void onModuleLoad() {",
" inlinableJSNI();",
" }",
"}");

Set<JsNode> functionForJsInlining = compileSnippetToJS(code.toString()).getRight();
assertContainsAll(functionForJsInlining, "onModuleLoad", "inlinableJSNI",
"function(){ return $wnd; }");
assertDoesNotContainsAny(functionForJsInlining, "functionA");
}

private void assertContainsAll(Set<JsNode> functionsForJsInlining, private void assertContainsAll(Set<JsNode> functionsForJsInlining,
String... functionNames) { String... functionNamesorContents) {
Set<String> remainingFunctions = Sets.newHashSet(functionNames); Set<String> remainingFunctions = Sets.newHashSet(functionNamesorContents);
for (JsFunction function : Iterables.filter(functionsForJsInlining, JsFunction.class)) { for (JsFunction function : Iterables.filter(functionsForJsInlining, JsFunction.class)) {
JsName name = function.getName(); JsName name = function.getName();
if (name == null) { if (name == null) {
remainingFunctions.remove(function.toString().replaceAll("\\s+"," ").trim());
continue; continue;
} }
remainingFunctions.remove(name.getShortIdent()); remainingFunctions.remove(name.getShortIdent());
} }
assertTrue("{" + (Joiner.on(",").join(remainingFunctions)) + "} marked for consideration in " assertTrue("{" + (Joiner.on(",").join(remainingFunctions)) + "} not marked for consideration in "
+ "JsInliner", remainingFunctions.isEmpty()); + "JsInliner", remainingFunctions.isEmpty());
} }


Expand Down

0 comments on commit 03f973a

Please sign in to comment.