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

The following examples show how to use com.google.javascript.rhino.IR#call() . 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: RuntimeTypeCheck.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a function call to check that the given expression matches the
 * given type at runtime.
 *
 * <p>For example, if the type is {@code (string|Foo)}, the function call is
 * {@code checkType(expr, [valueChecker('string'), classChecker('Foo')])}.
 *
 * @return the function call node or {@code null} if the type is not checked
 */
private Node createCheckTypeCallNode(JSType type, Node expr) {
  Node arrayNode = IR.arraylit();
  Collection<JSType> alternates;
  if (type.isUnionType()) {
    alternates = Sets.newTreeSet(ALPHA);
    Iterables.addAll(alternates, type.toMaybeUnionType().getAlternates());
  } else {
    alternates = ImmutableList.of(type);
  }
  for (JSType alternate : alternates) {
    Node checkerNode = createCheckerNode(alternate);
    if (checkerNode == null) {
      return null;
    }
    arrayNode.addChildToBack(checkerNode);
  }
  return IR.call(jsCode("checkType"), expr, arrayNode);
}
 
Example 2
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 3
Source File: Closure_10_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * A new CALL node with the "FREE_CALL" set based on call target.
 */
static Node newCallNode(Node callTarget, Node... parameters) {
  boolean isFreeCall = !isGet(callTarget);
  Node call = IR.call(callTarget);
  call.putBooleanProp(Node.FREE_CALL, isFreeCall);
  for (Node parameter : parameters) {
    call.addChildToBack(parameter);
  }
  return call;
}
 
Example 4
Source File: Cardumen_00200_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * A new CALL node with the "FREE_CALL" set based on call target.
 */
static Node newCallNode(Node callTarget, Node... parameters) {
  boolean isFreeCall = !isGet(callTarget);
  Node call = IR.call(callTarget);
  call.putBooleanProp(Node.FREE_CALL, isFreeCall);
  for (Node parameter : parameters) {
    call.addChildToBack(parameter);
  }
  return call;
}
 
Example 5
Source File: Closure_10_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * A new CALL node with the "FREE_CALL" set based on call target.
 */
static Node newCallNode(Node callTarget, Node... parameters) {
  boolean isFreeCall = !isGet(callTarget);
  Node call = IR.call(callTarget);
  call.putBooleanProp(Node.FREE_CALL, isFreeCall);
  for (Node parameter : parameters) {
    call.addChildToBack(parameter);
  }
  return call;
}
 
Example 6
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 5 votes vote down vote up
private Node tryMinimizeStringArrayLiteral(Node n) {
  if(!late) {
    return n;
  }

  int numElements = n.getChildCount();
  // We save two bytes per element.
  int saving = numElements * 2 - STRING_SPLIT_OVERHEAD;
  if (saving <= 0) {
    return n;
  }

  String[] strings = new String[n.getChildCount()];
  int idx = 0;
  for (Node cur = n.getFirstChild(); cur != null; cur = cur.getNext()) {
    strings[idx++] = cur.getString();
  }

  // These delimiters are chars that appears a lot in the program therefore
  // probably have a small Huffman encoding.
  String delimiter = pickDelimiter(strings);
  if (delimiter != null) {
    String template = Joiner.on(delimiter).join(strings);
    Node call = IR.call(
        IR.getprop(
            IR.string(template),
            IR.string("split")),
        IR.string("" + delimiter));
    call.copyInformationFromForTree(n);
    n.getParent().replaceChild(n, call);
    reportCodeChange();
    return call;
  }
  return n;
}
 
Example 7
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 5 votes vote down vote up
private Node tryMinimizeStringArrayLiteral(Node n) {
  if(!late) {
    return n;
  }

  int numElements = n.getChildCount();
  // We save two bytes per element.
  int saving = numElements * 2 - STRING_SPLIT_OVERHEAD;
  if (saving <= 0) {
    return n;
  }

  String[] strings = new String[n.getChildCount()];
  int idx = 0;
  for (Node cur = n.getFirstChild(); cur != null; cur = cur.getNext()) {
    strings[idx++] = cur.getString();
  }

  // These delimiters are chars that appears a lot in the program therefore
  // probably have a small Huffman encoding.
  String delimiter = pickDelimiter(strings);
  if (delimiter != null) {
    String template = Joiner.on(delimiter).join(strings);
    Node call = IR.call(
        IR.getprop(
            IR.string(template),
            IR.string("split")),
        IR.string("" + delimiter));
    call.copyInformationFromForTree(n);
    n.getParent().replaceChild(n, call);
    reportCodeChange();
    return call;
  }
  return n;
}
 
Example 8
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 5 votes vote down vote up
private Node tryMinimizeStringArrayLiteral(Node n) {
  if(!late) {
    return n;
  }

  int numElements = n.getChildCount();
  // We save two bytes per element.
  int saving = numElements * 2 - STRING_SPLIT_OVERHEAD;
  if (saving <= 0) {
    return n;
  }

  String[] strings = new String[n.getChildCount()];
  int idx = 0;
  for (Node cur = n.getFirstChild(); cur != null; cur = cur.getNext()) {
    strings[idx++] = cur.getString();
  }

  // These delimiters are chars that appears a lot in the program therefore
  // probably have a small Huffman encoding.
  String delimiter = pickDelimiter(strings);
  if (delimiter != null) {
    String template = Joiner.on(delimiter).join(strings);
    Node call = IR.call(
        IR.getprop(
            IR.string(template),
            IR.string("split")),
        IR.string("" + delimiter));
    call.copyInformationFromForTree(n);
    n.getParent().replaceChild(n, call);
    reportCodeChange();
    return call;
  }
  return n;
}
 
Example 9
Source File: RuntimeTypeCheck.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a node which evaluates to a checker for the given type (which
 * must not be a union). We have checkers for value types, classes and
 * interfaces.
 *
 * @return the checker node or {@code null} if the type is not checked
 */
private Node createCheckerNode(JSType type) {
  if (type.isNullType()) {
    return jsCode("nullChecker");

  } else if (type.isBooleanValueType()
      || type.isNumberValueType()
      || type.isStringValueType()
      || type.isVoidType()) {
    return IR.call(
        jsCode("valueChecker"),
        IR.string(type.toString()));

  } else if (type.isInstanceType()) {
    ObjectType objType = (ObjectType) type;

    String refName = objType.getReferenceName();

    StaticSourceFile sourceFile =
        NodeUtil.getSourceFile(objType.getConstructor().getSource());
    if (sourceFile == null || sourceFile.isExtern()) {
      return IR.call(
              jsCode("externClassChecker"),
              IR.string(refName));
    }

    return IR.call(
            jsCode(objType.getConstructor().isInterface() ?
                    "interfaceChecker" : "classChecker"),
            IR.string(refName));

  } else {
    // We don't check this type (e.g. unknown & all types).
    return null;
  }
}
 
Example 10
Source File: jMutRepair_003_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * A new CALL node with the "FREE_CALL" set based on call target.
 */
static Node newCallNode(Node callTarget, Node... parameters) {
  boolean isFreeCall = !isGet(callTarget);
  Node call = IR.call(callTarget);
  call.putBooleanProp(Node.FREE_CALL, isFreeCall);
  for (Node parameter : parameters) {
    call.addChildToBack(parameter);
  }
  return call;
}
 
Example 11
Source File: PeepholeSubstituteAlternateSyntax.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private Node tryMinimizeStringArrayLiteral(Node n) {
  if(!late) {
    return n;
  }

  int numElements = n.getChildCount();
  // We save two bytes per element.
  int saving = numElements * 2 - STRING_SPLIT_OVERHEAD;
  if (saving <= 0) {
    return n;
  }

  String[] strings = new String[n.getChildCount()];
  int idx = 0;
  for (Node cur = n.getFirstChild(); cur != null; cur = cur.getNext()) {
    strings[idx++] = cur.getString();
  }

  // These delimiters are chars that appears a lot in the program therefore
  // probably have a small Huffman encoding.
  String delimiter = pickDelimiter(strings);
  if (delimiter != null) {
    String template = Joiner.on(delimiter).join(strings);
    Node call = IR.call(
        IR.getprop(
            IR.string(template),
            IR.string("split")),
        IR.string("" + delimiter));
    call.copyInformationFromForTree(n);
    n.getParent().replaceChild(n, call);
    reportCodeChange();
    return call;
  }
  return n;
}
 
Example 12
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 13
Source File: Cardumen_0014_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * A new CALL node with the "FREE_CALL" set based on call target.
 */
static Node newCallNode(Node callTarget, Node... parameters) {
  boolean isFreeCall = !isGet(callTarget);
  Node call = IR.call(callTarget);
  call.putBooleanProp(Node.FREE_CALL, isFreeCall);
  for (Node parameter : parameters) {
    call.addChildToBack(parameter);
  }
  return call;
}
 
Example 14
Source File: Cardumen_0014_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * A new CALL node with the "FREE_CALL" set based on call target.
 */
static Node newCallNode(Node callTarget, Node... parameters) {
  boolean isFreeCall = !isGet(callTarget);
  Node call = IR.call(callTarget);
  call.putBooleanProp(Node.FREE_CALL, isFreeCall);
  for (Node parameter : parameters) {
    call.addChildToBack(parameter);
  }
  return call;
}
 
Example 15
Source File: Cardumen_00149_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * A new CALL node with the "FREE_CALL" set based on call target.
 */
static Node newCallNode(Node callTarget, Node... parameters) {
  boolean isFreeCall = !isGet(callTarget);
  Node call = IR.call(callTarget);
  call.putBooleanProp(Node.FREE_CALL, isFreeCall);
  for (Node parameter : parameters) {
    call.addChildToBack(parameter);
  }
  return call;
}
 
Example 16
Source File: InstrumentFunctions.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private Node newReportFunctionExitNode() {
  Node call = IR.call(
      IR.name(reportFunctionExitName),
      IR.number(functionId));
  call.putBooleanProp(Node.FREE_CALL, true);
  return call;
}
 
Example 17
Source File: AliasExternals.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Changes a.prop = b to SETPROP_prop(a, b);
 */
private void replaceMutator(Node getPropNode) {
  /*
     BEFORE
     exprstmt 1
         assign 128
             getprop
                 NodeTree A
                 string prop
             NODE TREE B

     AFTER
     exprstmt 1
         call
             name SETPROP_prop
             NodeTree A
             NODE TREE B
  */
  Node propNameNode = getPropNode.getLastChild();
  Node parentNode = getPropNode.getParent();

  Symbol prop = props.get(propNameNode.getString());
  if (prop.aliasMutator) {
    Node propSrc = getPropNode.getFirstChild();
    Node propDest = parentNode.getLastChild();

    // Remove the orphaned children
    getPropNode.removeChild(propSrc);
    getPropNode.removeChild(propNameNode);
    parentNode.removeChild(propDest);

    // Create the call GETPROP_prop() node, using the old propSrc as the
    // one parameter to GETPROP_prop() call.
    Node callName = IR.name(
        getMutatorFor(propNameNode.getString()));
    Node call = IR.call( callName, propSrc, propDest);
    call.putBooleanProp(Node.FREE_CALL, true);

    // And replace the assign statement with the new call
    replaceNode(parentNode.getParent(), parentNode, call);

    compiler.reportCodeChange();
  }
}
 
Example 18
Source File: TransformAMDToCJSModule.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Rewrite a single require call.
 */
private void handleRequire(NodeTraversal t, Node defineNode, Node script,
    Node callback, Node aliasNode, Node modNode) {
  String moduleName = null;
  if (modNode != null) {
    moduleName = handlePlugins(t, script, modNode.getString(), modNode);
  }

  if (isVirtualModuleName(moduleName)) {
    return;
  }

  String aliasName = aliasNode != null ? aliasNode.getString() : null;
  Scope globalScope = t.getScope();
  if (aliasName != null &&
      globalScope.isDeclared(aliasName, true)) {
    while (true) {
      String renamed = aliasName + VAR_RENAME_SUFFIX + renameIndex;
      if (!globalScope.isDeclared(renamed, true)) {
        NodeTraversal.traverse(compiler, callback,
            new RenameCallback(aliasName, renamed));
        aliasName = renamed;
        break;
      }
      renameIndex++;
    }
  }

  Node requireNode;
  if (moduleName != null) {
    Node call = IR.call(IR.name("require"), IR.string(moduleName));
    call.putBooleanProp(Node.FREE_CALL, true);
    if (aliasName != null) {
      requireNode = IR.var(IR.name(aliasName), call)
          .copyInformationFromForTree(aliasNode);
    } else {
      requireNode = IR.exprResult(call).
          copyInformationFromForTree(modNode);
    }
  } else {
    // ignore exports, require and module (because they are implicit
    // in CommonJS);
    if (isVirtualModuleName(aliasName)) {
      return;
    }
    requireNode = IR.var(IR.name(aliasName), IR.nullNode())
        .copyInformationFromForTree(aliasNode);
  }

  script.addChildBefore(requireNode,
      defineNode.getParent());
}
 
Example 19
Source File: Closure_23_PeepholeFoldConstants_s.java    From coming with MIT License 4 votes vote down vote up
private Node tryFoldObjectPropAccess(Node n, Node left, Node right) {
  Preconditions.checkArgument(NodeUtil.isGet(n));

  if (!left.isObjectLit() || !right.isString()) {
    return n;
  }

  if (isAssignmentTarget(n)) {
    // If GETPROP/GETELEM is used as assignment target the object literal is
    // acting as a temporary we can't fold it here:
    //    "{a:x}.a += 1" is not "x += 1"
    return n;
  }

  // find the last definition in the object literal
  Node key = null;
  Node value = null;
  for (Node c = left.getFirstChild(); c != null; c = c.getNext()) {
    if (c.getString().equals(right.getString())) {
      switch (c.getType()) {
        case Token.SETTER_DEF:
          continue;
        case Token.GETTER_DEF:
        case Token.STRING_KEY:
          if (value != null && mayHaveSideEffects(value)) {
            // The previously found value had side-effects
            return n;
          }
          key = c;
          value = key.getFirstChild();
          break;
        default:
          throw new IllegalStateException();
      }
    } else if (mayHaveSideEffects(c.getFirstChild())) {
      // We don't handle the side-effects here as they might need a temporary
      // or need to be reordered.
      return n;
    }
  }

  // Didn't find a definition of the name in the object literal, it might
  // be coming from the Object prototype
  if (value == null) {
    return n;
  }

  if (value.isFunction() && NodeUtil.referencesThis(value)) {
    // 'this' may refer to the object we are trying to remove
    return n;
  }

  Node replacement = value.detachFromParent();
  if (key.isGetterDef()){
    replacement = IR.call(replacement);
    replacement.putBooleanProp(Node.FREE_CALL, true);
  }

  n.getParent().replaceChild(n, replacement);
  reportCodeChange();
  return n;
}
 
Example 20
Source File: PeepholeFoldConstants.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private Node tryFoldObjectPropAccess(Node n, Node left, Node right) {
  Preconditions.checkArgument(NodeUtil.isGet(n));

  if (!left.isObjectLit() || !right.isString()) {
    return n;
  }

  if (isAssignmentTarget(n)) {
    // If GETPROP/GETELEM is used as assignment target the object literal is
    // acting as a temporary we can't fold it here:
    //    "{a:x}.a += 1" is not "x += 1"
    return n;
  }

  // find the last definition in the object literal
  Node key = null;
  Node value = null;
  for (Node c = left.getFirstChild(); c != null; c = c.getNext()) {
    if (c.getString().equals(right.getString())) {
      switch (c.getType()) {
        case Token.SETTER_DEF:
          continue;
        case Token.GETTER_DEF:
        case Token.STRING_KEY:
          if (value != null && mayHaveSideEffects(value)) {
            // The previously found value had side-effects
            return n;
          }
          key = c;
          value = key.getFirstChild();
          break;
        default:
          throw new IllegalStateException();
      }
    } else if (mayHaveSideEffects(c.getFirstChild())) {
      // We don't handle the side-effects here as they might need a temporary
      // or need to be reordered.
      return n;
    }
  }

  // Didn't find a definition of the name in the object literal, it might
  // be coming from the Object prototype
  if (value == null) {
    return n;
  }

  if (value.isFunction() && NodeUtil.referencesThis(value)) {
    // 'this' may refer to the object we are trying to remove
    return n;
  }

  Node replacement = value.detachFromParent();
  if (key.isGetterDef()){
    replacement = IR.call(replacement);
    replacement.putBooleanProp(Node.FREE_CALL, true);
  }

  n.getParent().replaceChild(n, replacement);
  reportCodeChange();
  return n;
}