Java Code Examples for com.google.javascript.rhino.IR#exprResult()

The following examples show how to use com.google.javascript.rhino.IR#exprResult() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Nopol2017_0010_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Creates a dotted namespace assignment expression
 * (e.g. <code>foo.bar = {};</code>).
 */
private Node makeAssignmentExprNode() {
  Node decl = IR.exprResult(
      IR.assign(
          NodeUtil.newQualifiedNameNode(
              compiler.getCodingConvention(), namespace,
              firstNode /* real source info will be filled in below */,
              namespace),
          createNamespaceLiteral()));
  decl.putBooleanProp(Node.IS_NAMESPACE, true);
  if (candidateDefinition == null) {
    decl.getFirstChild().setJSDocInfo(createConstantJsDoc());
  }
  Preconditions.checkState(isNamespacePlaceholder(decl));
  setSourceInfo(decl);
  return decl;
}
 
Example 2
Source File: ProcessClosurePrimitives.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a dotted namespace assignment expression
 * (e.g. <code>foo.bar = {};</code>).
 */
private Node makeAssignmentExprNode() {
  Node decl = IR.exprResult(
      IR.assign(
          NodeUtil.newQualifiedNameNode(
              compiler.getCodingConvention(), namespace,
              firstNode /* real source info will be filled in below */,
              namespace),
          createNamespaceLiteral()));
  decl.putBooleanProp(Node.IS_NAMESPACE, true);
  if (candidateDefinition == null) {
    decl.getFirstChild().setJSDocInfo(createConstantJsDoc());
  }
  Preconditions.checkState(isNamespacePlaceholder(decl));
  setSourceInfo(decl);
  return decl;
}
 
Example 3
Source File: OptimizeParameters.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds a variable to the top of a function block.
 * @param function A function node.
 * @param varName The name of the variable.
 * @param value The initial value of the variable.
 */
private void addVariableToFunction(Node function, Node varName, Node value) {
  Preconditions.checkArgument(function.isFunction(),
      "Node must be a function.");

  Node block = function.getLastChild();
  Preconditions.checkArgument(block.isBlock(),
      "Node must be a block.");

  Preconditions.checkState(value.getParent() == null);
  Node stmt;
  if (varName != null) {
    stmt = NodeUtil.newVarNode(varName.getString(), value);
  } else {
    stmt = IR.exprResult(value);
  }
  block.addChildToFront(stmt);
  compiler.reportCodeChange();
}
 
Example 4
Source File: ExportTestFunctions.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void exportTestFunctionAsSymbol(String testFunctionName, Node node,
    Node scriptNode) {

  Node exportCallTarget = NodeUtil.newQualifiedNameNode(
      compiler.getCodingConvention(),
      exportSymbolFunction, node, testFunctionName);
  Node call = IR.call( exportCallTarget);
  if (exportCallTarget.isName()) {
    call.putBooleanProp(Node.FREE_CALL, true);
  }
  call.addChildToBack(IR.string(testFunctionName));
  call.addChildToBack(NodeUtil.newQualifiedNameNode(
      compiler.getCodingConvention(),
      testFunctionName, node, testFunctionName));

  Node expression = IR.exprResult(call);

  scriptNode.addChildAfter(expression, node);
  compiler.reportCodeChange();
}
 
Example 5
Source File: Reader.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void transformPrologueDecl(JsonML element, Node parent)
    throws JsonMLException {
  String directive = getStringAttribute(element, TagAttr.DIRECTIVE);

  if (ALLOWED_DIRECTIVES.contains(directive)) {
    Set<String> directives = parent.getDirectives();
    if (directives == null) {
      directives = Sets.newHashSet();
    }
    directives.add(directive);
    parent.setDirectives(directives);
  } else {
    // for a directive which is not supported, we create a regular node
    Node node = IR.exprResult(IR.string(directive));
    parent.addChildToBack(node);
  }
}
 
Example 6
Source File: RuntimeTypeCheck.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void addBoilerplateCode() {
  Node newNode = compiler.ensureLibraryInjected("runtime_type_check");
  if (newNode != null && logFunction != null) {
    // Inject the custom log function.
    Node logOverride = IR.exprResult(
        IR.assign(
            NodeUtil.newQualifiedNameNode(
                compiler.getCodingConvention(),
                "$jscomp.typecheck.log"),
            NodeUtil.newQualifiedNameNode(
                compiler.getCodingConvention(),
                logFunction)));
    newNode.getParent().addChildAfter(logOverride, newNode);
    compiler.reportCodeChange();
  }
}
 
Example 7
Source File: Closure_113_ProcessClosurePrimitives_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Creates a dotted namespace assignment expression
 * (e.g. <code>foo.bar = {};</code>).
 */
private Node makeAssignmentExprNode() {
  Node decl = IR.exprResult(
      IR.assign(
          NodeUtil.newQualifiedNameNode(
              compiler.getCodingConvention(), namespace,
              firstNode /* real source info will be filled in below */,
              namespace),
          createNamespaceLiteral()));
  decl.putBooleanProp(Node.IS_NAMESPACE, true);
  if (candidateDefinition == null) {
    decl.getFirstChild().setJSDocInfo(createConstantJsDoc());
  }
  Preconditions.checkState(isNamespacePlaceholder(decl));
  setSourceInfo(decl);
  return decl;
}
 
Example 8
Source File: Closure_113_ProcessClosurePrimitives_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Creates a dotted namespace assignment expression
 * (e.g. <code>foo.bar = {};</code>).
 */
private Node makeAssignmentExprNode() {
  Node decl = IR.exprResult(
      IR.assign(
          NodeUtil.newQualifiedNameNode(
              compiler.getCodingConvention(), namespace,
              firstNode /* real source info will be filled in below */,
              namespace),
          createNamespaceLiteral()));
  decl.putBooleanProp(Node.IS_NAMESPACE, true);
  if (candidateDefinition == null) {
    decl.getFirstChild().setJSDocInfo(createConstantJsDoc());
  }
  Preconditions.checkState(isNamespacePlaceholder(decl));
  setSourceInfo(decl);
  return decl;
}
 
Example 9
Source File: Nopol2017_0010_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Creates a dotted namespace assignment expression
 * (e.g. <code>foo.bar = {};</code>).
 */
private Node makeAssignmentExprNode() {
  Node decl = IR.exprResult(
      IR.assign(
          NodeUtil.newQualifiedNameNode(
              compiler.getCodingConvention(), namespace,
              firstNode /* real source info will be filled in below */,
              namespace),
          createNamespaceLiteral()));
  decl.putBooleanProp(Node.IS_NAMESPACE, true);
  if (candidateDefinition == null) {
    decl.getFirstChild().setJSDocInfo(createConstantJsDoc());
  }
  Preconditions.checkState(isNamespacePlaceholder(decl));
  setSourceInfo(decl);
  return decl;
}
 
Example 10
Source File: RuntimeTypeCheck.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private Node addMarker(
        FunctionType funType,
        Node nodeToInsertAfter,
        @Nullable ObjectType interfaceType) {

  if (funType.getSource() == null) {
    return nodeToInsertAfter;
  }

  String className = NodeUtil.getFunctionName(funType.getSource());

  // This can happen with anonymous classes declared with the type
  // {@code Function}.
  if (className == null) {
    return nodeToInsertAfter;
  }

  Node classNode = NodeUtil.newQualifiedNameNode(
      compiler.getCodingConvention(), className);

  Node marker = IR.string(
          interfaceType == null ?
          "instance_of__" + className :
          "implements__" + interfaceType.getReferenceName());

  Node assign = IR.exprResult(IR.assign(
      IR.getelem(
          IR.getprop(
              classNode,
              IR.string("prototype")), marker),
      IR.trueNode()));

  nodeToInsertAfter.getParent().addChildAfter(assign, nodeToInsertAfter);
  compiler.reportCodeChange();
  nodeToInsertAfter = assign;
  return nodeToInsertAfter;
}
 
Example 11
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 5 votes vote down vote up
private Node trySplitComma(Node n) {
  if (late) {
    return n;
  }
  Node parent = n.getParent();
  Node left = n.getFirstChild();
  Node right = n.getLastChild();

  if (parent.isExprResult()
      && !parent.getParent().isLabel()) {
    // split comma
    n.detachChildren();
    // Replace the original expression with the left operand.
    parent.replaceChild(n, left);
    // Add the right expression afterward.
    Node newStatement = IR.exprResult(right);
    newStatement.copyInformationFrom(n);

    //This modifies outside the subtree, which is not
    //desirable in a peephole optimization.
    parent.getParent().addChildAfter(newStatement, parent);
    reportCodeChange();
    return left;
  } else {
    return n;
  }
}
 
Example 12
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 5 votes vote down vote up
private Node trySplitComma(Node n) {
  if (late) {
    return n;
  }
  Node parent = n.getParent();
  Node left = n.getFirstChild();
  Node right = n.getLastChild();

  if (parent.isExprResult()
      && !parent.getParent().isLabel()) {
    // split comma
    n.detachChildren();
    // Replace the original expression with the left operand.
    parent.replaceChild(n, left);
    // Add the right expression afterward.
    Node newStatement = IR.exprResult(right);
    newStatement.copyInformationFrom(n);

    //This modifies outside the subtree, which is not
    //desirable in a peephole optimization.
    parent.getParent().addChildAfter(newStatement, parent);
    reportCodeChange();
    return left;
  } else {
    return n;
  }
}
 
Example 13
Source File: AliasKeywords.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void aliasNode(Node throwNode, Node parent) {
  Node name = NodeUtil.newName(
      compiler.getCodingConvention(),
      getAliasName(), throwNode, getAliasName());
  Node aliasCall = IR.call( name, throwNode.removeFirstChild());
  aliasCall.putBooleanProp(Node.FREE_CALL, true);
  Node exprResult = IR.exprResult(aliasCall);
  parent.replaceChild(throwNode, exprResult);
}
 
Example 14
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 5 votes vote down vote up
private Node trySplitComma(Node n) {
  if (late) {
    return n;
  }
  Node parent = n.getParent();
  Node left = n.getFirstChild();
  Node right = n.getLastChild();

  if (parent.isExprResult()
      && !parent.getParent().isLabel()) {
    // split comma
    n.detachChildren();
    // Replace the original expression with the left operand.
    parent.replaceChild(n, left);
    // Add the right expression afterward.
    Node newStatement = IR.exprResult(right);
    newStatement.copyInformationFrom(n);

    //This modifies outside the subtree, which is not
    //desirable in a peephole optimization.
    parent.getParent().addChildAfter(newStatement, parent);
    reportCodeChange();
    return left;
  } else {
    return n;
  }
}
 
Example 15
Source File: InstrumentFunctions.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param body  body of function with id == this.functionId
 */
void process(Node body) {
  NodeTraversal.traverse(compiler, body, this);

  if (!allPathsReturn(body)) {
    Node call = newReportFunctionExitNode();
    Node expr = IR.exprResult(call);
    body.addChildToBack(expr);
    compiler.reportCodeChange();
  }
}
 
Example 16
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 5 votes vote down vote up
private Node trySplitComma(Node n) {
  if (late) {
    return n;
  }
  Node parent = n.getParent();
  Node left = n.getFirstChild();
  Node right = n.getLastChild();

  if (parent.isExprResult()
      && !parent.getParent().isLabel()) {
    // split comma
    n.detachChildren();
    // Replace the original expression with the left operand.
    parent.replaceChild(n, left);
    // Add the right expression afterward.
    Node newStatement = IR.exprResult(right);
    newStatement.copyInformationFrom(n);

    //This modifies outside the subtree, which is not
    //desirable in a peephole optimization.
    parent.getParent().addChildAfter(newStatement, parent);
    reportCodeChange();
    return left;
  } else {
    return n;
  }
}
 
Example 17
Source File: PeepholeSubstituteAlternateSyntax.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private Node trySplitComma(Node n) {
  if (late) {
    return n;
  }
  Node parent = n.getParent();
  Node left = n.getFirstChild();
  Node right = n.getLastChild();

  if (parent.isExprResult()
      && !parent.getParent().isLabel()) {
    // split comma
    n.detachChildren();
    // Replace the original expression with the left operand.
    parent.replaceChild(n, left);
    // Add the right expression afterward.
    Node newStatement = IR.exprResult(right);
    newStatement.copyInformationFrom(n);

    //This modifies outside the subtree, which is not
    //desirable in a peephole optimization.
    parent.getParent().addChildAfter(newStatement, parent);
    reportCodeChange();
    return left;
  } else {
    return n;
  }
}
 
Example 18
Source File: RuntimeTypeCheck.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Insert checks for the parameters of the function.
 */
private void visitFunction(NodeTraversal t, Node n) {
  FunctionType funType = JSType.toMaybeFunctionType(n.getJSType());
  Node block = n.getLastChild();
  Node paramName = NodeUtil.getFunctionParameters(n).getFirstChild();
  Node insertionPoint = null;

  // To satisfy normalization constraints, the type checking must be
  // added after any inner function declarations.
  for (Node next = block.getFirstChild();
       next != null && NodeUtil.isFunctionDeclaration(next);
       next = next.getNext()) {
    insertionPoint = next;
  }

  for (Node paramType : funType.getParameters()) {
    // Can this ever happen?
    if (paramName == null) {
      return;
    }

    Node checkNode = createCheckTypeCallNode(
        paramType.getJSType(), paramName.cloneTree());

    if (checkNode == null) {
      // We don't know how to check this parameter type.
      paramName = paramName.getNext();
      continue;
    }

    checkNode = IR.exprResult(checkNode);
    if (insertionPoint == null) {
      block.addChildToFront(checkNode);
    } else {
      block.addChildAfter(checkNode, insertionPoint);
    }

    compiler.reportCodeChange();
    paramName = paramName.getNext();
    insertionPoint = checkNode;
  }
}
 
Example 19
Source File: GenerateExports.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void process(Node externs, Node root) {
  FindExportableNodes findExportableNodes = new FindExportableNodes(compiler);
  NodeTraversal.traverse(compiler, root, findExportableNodes);
  Map<String, GenerateNodeContext> exports = findExportableNodes
      .getExports();

  CodingConvention convention = compiler.getCodingConvention();
  for (Map.Entry<String, GenerateNodeContext> entry : exports.entrySet()) {
    String export = entry.getKey();
    GenerateNodeContext context = entry.getValue();

    // Emit the proper CALL expression.
    // This is an optimization to avoid exporting everything as a symbol
    // because exporting a property is significantly simpler/faster.
    // Only export the property if the parent is being exported or
    // if the parent is "prototype" and the grandparent is being exported.
    String parent = null;
    String grandparent = null;

    Node node = context.getNode().getFirstChild();
    if (node.isGetProp()) {
      parent = node.getFirstChild().getQualifiedName();
      if (node.getFirstChild().isGetProp() &&
          getPropertyName(node.getFirstChild()).equals(PROTOTYPE_PROPERTY)) {
        grandparent = node.getFirstChild().getFirstChild().getQualifiedName();
      }
    }

    boolean useExportSymbol = true;
    if (grandparent != null && exports.containsKey(grandparent)) {
      useExportSymbol = false;
    } else if (parent != null && exports.containsKey(parent)) {
      useExportSymbol = false;
    }

    Node call;
    if (useExportSymbol) {
      // exportSymbol(publicPath, object);
      call = IR.call(
          NodeUtil.newQualifiedNameNode(
              convention, exportSymbolFunction,
              context.getNode(), export),
          IR.string(export),
          NodeUtil.newQualifiedNameNode(
              convention, export,
              context.getNode(), export));
    } else {
      // exportProperty(object, publicName, symbol);
      String property = getPropertyName(node);
      call = IR.call(
          NodeUtil.newQualifiedNameNode(
              convention, exportPropertyFunction,
              context.getNode(), exportPropertyFunction),
          NodeUtil.newQualifiedNameNode(
              convention, parent,
              context.getNode(), exportPropertyFunction),
          IR.string(property),
          NodeUtil.newQualifiedNameNode(
              convention, export,
              context.getNode(), exportPropertyFunction));
    }

    Node expression = IR.exprResult(call);
    annotate(expression);

    // It's important that any class-building calls (goog.inherits)
    // come right after the class definition, so move the export after that.
    Node insertionPoint = context.getContextNode().getNext();
    while (insertionPoint != null &&
        NodeUtil.isExprCall(insertionPoint) &&
        convention.getClassesDefinedByCall(
            insertionPoint.getFirstChild()) != null) {
      insertionPoint = insertionPoint.getNext();
    }

    if (insertionPoint == null) {
      context.getScriptNode().addChildToBack(expression);
    } else {
      context.getScriptNode().addChildBefore(expression, insertionPoint);
    }
    compiler.reportCodeChange();
  }
}