Package com.google.gwt.dev.js.ast

Examples of com.google.gwt.dev.js.ast.JsProgram


    PermProps props = new PermProps(Arrays.asList(
        new BindingProps(new BindingProperty[]{stackMode}, new String[]{"EMULATED"}, config)
    ));

    JsProgram jsProgram = new JsProgram();
    JavaToJavaScriptMap jjsmap = GenerateJavaScriptAST.exec(
        logger, jProgram, jsProgram, context, typeMapper,
        symbolTable, props).getLeft();

    // Finally, run the pass we care about.
View Full Code Here


    assertEquals("function a(){}\n;a();a();",
        optimize("function a(){};function b(){} a(); b();"));
  }

  public void testVirtualRemoveDuplicates() throws Exception {
    JsProgram program = new JsProgram();
    String js = "_.method1=function(){};_.method2=function(){};_.method1();_.method2();";
    List<JsStatement> input = JsParser.parse(SourceOrigin.UNKNOWN,
      program.getScope(), new StringReader(js));
    program.getGlobalBlock().getStatements().addAll(input);

    // Mark all functions as if they were translated from Java sources.
    setAllFromJava(program);

    String firstName = new MockNameGenerator().getFreshName();
View Full Code Here

  /**
   * Test for one of the bugs causing issue 8284. JsObfuscateNamer was reassigning the same names
   * across different fragments.
   */
  public void testDuplicateNamesWithCodeSplitterError() throws Exception {
    JsProgram program = new JsProgram();
    // Reference to a in .b is to the top level scope a function where the one in _.c is to the
    // local a definition.
    //
    // After deduping _.b and _.c the identifier a in the deduped function points to the top level
    // scope a function and runing a namer afterwards makes the deduping invalid.
    String fragment0js = "_.a=function (){return _.a;}; _.b=function (){return _.a}; _.a();_.b();";
    String fragment1js = "_.c=function (){return _.c;}; _.d=function (){return _.c}; _.c();_.d();";

    List<JsStatement> fragment0 = JsParser.parse(SourceOrigin.UNKNOWN,
        program.getScope(), new StringReader(fragment0js));
    List<JsStatement> fragment1 = JsParser.parse(SourceOrigin.UNKNOWN,
        program.getScope(), new StringReader(fragment1js));
    program.setFragmentCount(2);
    program.getFragmentBlock(0).getStatements().addAll(fragment0);
    program.getFragmentBlock(1).getStatements().addAll(fragment1);

    // Mark all functions as if they were translated from Java sources.
    setAllFromJava(program);

    optimize(program, JsSymbolResolver.class, JsDuplicateFunctionRemoverProxy.class);

    // There should be two distinct dedupped functions here.
    MockNameGenerator tempFreshNameGenerator = new MockNameGenerator();
    String firstName = tempFreshNameGenerator.getFreshName();
    String secondName = tempFreshNameGenerator.getFreshName();

    assertNotNull(program.getScope().findExistingName(secondName));
  }
View Full Code Here

   * Test for one of the bugs causing issue 8284. JsObfuscateNamer was used to assign
   * obfuscated names to deduped functions and as a result it might have modified the other names
   * that had been assigned invalidating the irrevocable decision made by the deduper.
   */
  public void testRerunNamerError() throws Exception {
    JsProgram program = new JsProgram();
    // Reference to a in _.b is to the top level scope a function where the one in _.c is to the
    // local a definition.
    //
    // After deduping _.b and _.c the identifier a in the deduped function points to the top level
    // scope a function and runing a namer afterwards makes the deduping invalid.

    // CAVEAT: The two functions that have {return a;} as their bodies are not actually duplicates
    // but we use them to model functions that refer to names at different scopes. Because this
    // optimization only runs on JsFunctions that come from Java source this situation does not
    // happen.
    String js = "var c; function a(){return f1;}; function f1() {_.b = function() {return a;} }; "
        + "function f2() { var a = null; _.c = function() {return a;} };f1();f2();_.b();_.c();";
    List<JsStatement> input = JsParser.parse(SourceOrigin.UNKNOWN,
        program.getScope(), new StringReader(js));
    program.getGlobalBlock().getStatements().addAll(input);

    // Mark all functions as if they were translated from Java sources.
    setAllFromJava(program);

    // Get the JsNames for the top level a and the f2() scoped a.
    JsName topScope_a = program.getScope().findExistingName("a");
    JsName f2_a = null;
    for (JsScope scope : program.getScope().getChildren()) {
      if (scope.toString().startsWith("function f2->")) {
        f2_a = scope.findExistingName("a");
      }
    }

View Full Code Here

    assertEquals("function fooLogger(){return 42}\n",
        rename("function fooLogger() { return 42; }"));
  }

  public void testAvoidDuplicatesSameScope() throws Exception {
    JsProgram program = parseJs(
        "function f1(){ return 1 }\n" +
        "function f2(){ return 2 }\n");

    program.getScope().findExistingName("f1").setShortIdent("thing");
    program.getScope().findExistingName("f2").setShortIdent("thing");

    assertEquals(
        "function thing(){return 1}\n" +
        "function thing_0(){return 2}\n",
        rename(program));
View Full Code Here

        "function thing_0(){return 2}\n",
        rename(program));
  }

  public void testAvoidDuplicatesChildScope() throws Exception {
    JsProgram program = parseJs(
        "function f1(){\n" +
        "  function f2(){ return 2 }\n" +
        "  return 1\n" +
        "}\n");

    program.getScope().findExistingName("f1").setShortIdent("thing");
    program.getScope().getChildren().get(0).findExistingName("f2").setShortIdent("thing");

    assertEquals("function thing_0(){function thing(){return 2}\nreturn 1}\n",
        rename(program));
  }
View Full Code Here

  }

  public void testPackageInfo() throws Exception {
    // Synthesize a function definition with an illegal name, "package-info" like can result from
    // JDT compilation of package-info.java files.
    JsProgram jsProgram = new JsProgram();
    JsScope scope = jsProgram.getScope();

    // Function declaration statement.
    JsName name = scope.declareName("package-info", "package-info");
    List<JsStatement> statements = jsProgram.getFragment(0).getGlobalBlock().getStatements();
    final SourceOrigin sourceInfo = SourceOrigin.UNKNOWN;
    JsFunction function = new JsFunction(sourceInfo, scope, name);
    function.setBody(new JsBlock(sourceInfo));
    statements.add(new JsExprStmt(sourceInfo, function));
View Full Code Here

        rename(parseJs("function foo() { return 42; }"), JsOutputOption.PRETTY, true));

    // Renaming with the reserved suffix is rejected.
    try {
      String functionName = "foo" + JsPersistentPrettyNamer.RESERVED_IDENT_SUFFIX;
      JsProgram jsProgram = parseJs(
          "function " + functionName + "() { return 42; }");
      jsProgram.getScope().findExistingName(functionName).setObfuscatable(false);
      rename(jsProgram, JsOutputOption.PRETTY, true);
      fail("Naming an unobfuscatable identifier containing the reserved suffix should have "
          + "thrown an exception in JsPersistentPrettyNamer.");
    } catch (IllegalNameException e) {
      // Expected path.
View Full Code Here

      // Expected path.
    }
  }

  private JsProgram parseJs(String js) throws IOException, JsParserException {
    JsProgram program = new JsProgram();
    List<JsStatement> expected = JsParser.parse(SourceOrigin.UNKNOWN,
        program.getScope(), new StringReader(js));
    program.getGlobalBlock().getStatements().addAll(expected);
    return program;
  }
View Full Code Here

  private JsProgram program;
  private JsBlock functionBody;

  @Override
  public void setUp() {
    program = new JsProgram();
    SourceInfo info = program.createSourceInfo(1, "Test.java");
    program.setIndexedFields(fields("CoverageUtil.coverage"));
    program.setIndexedFunctions(functions("CoverageUtil.cover", "CoverageUtil.onBeforeUnload"));
    JsBlock globalBlock = program.getGlobalBlock();
    JsFunction function = new JsFunction(info, program.getScope());
View Full Code Here

TOP

Related Classes of com.google.gwt.dev.js.ast.JsProgram

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.