Java Code Examples for com.google.javascript.rhino.Node#SideEffectFlags

The following examples show how to use com.google.javascript.rhino.Node#SideEffectFlags . 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: PureFunctionIdentifier.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Set no side effect property at pure-function call sites.
 */
private void markPureFunctionCalls() {
  for (Node callNode : allFunctionCalls) {
    Node name = callNode.getFirstChild();
    Collection<Definition> defs =
        getCallableDefinitions(definitionProvider, name);
    // Default to side effects, non-local results
    Node.SideEffectFlags flags = new Node.SideEffectFlags();
    if (defs == null) {
      flags.setMutatesGlobalState();
      flags.setThrows();
      flags.setReturnsTainted();
    } else {
      flags.clearAllFlags();
      for (Definition def : defs) {
        FunctionInformation functionInfo =
            functionSideEffectMap.get(def.getRValue());
        Preconditions.checkNotNull(functionInfo);
        // TODO(johnlenz): set the arguments separately from the
        // global state flag.
        if (functionInfo.mutatesGlobalState()) {
          flags.setMutatesGlobalState();
        }

        if (functionInfo.functionThrows) {
          flags.setThrows();
        }

        if (!callNode.isNew()) {
          if (functionInfo.taintsThis) {
            flags.setMutatesThis();
          }
        }

        if (functionInfo.taintsReturn) {
          flags.setReturnsTainted();
        }

        if (flags.areAllFlagsSet()) {
          break;
        }
      }
    }

    // Handle special cases (Math, RegExp)
    if (callNode.isCall()) {
      Preconditions.checkState(compiler != null);
      if (!NodeUtil.functionCallHasSideEffects(callNode, compiler)) {
        flags.clearSideEffectFlags();
      }
    } else if (callNode.isNew()) {
      // Handle known cases now (Object, Date, RegExp, etc)
      if (!NodeUtil.constructorCallHasSideEffects(callNode)) {
        flags.clearSideEffectFlags();
      }
    }

    callNode.setSideEffectFlags(flags.valueOf());
  }
}
 
Example 2
Source File: NodeUtilTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testLocalValue2() {
  Node newExpr = getNode("new x()");
  assertFalse(NodeUtil.evaluatesToLocalValue(newExpr));

  Preconditions.checkState(newExpr.isNew());
  Node.SideEffectFlags flags = new Node.SideEffectFlags();

  flags.clearAllFlags();
  newExpr.setSideEffectFlags(flags.valueOf());

  assertTrue(NodeUtil.evaluatesToLocalValue(newExpr));

  flags.clearAllFlags();
  flags.setMutatesThis();
  newExpr.setSideEffectFlags(flags.valueOf());

  assertTrue(NodeUtil.evaluatesToLocalValue(newExpr));

  flags.clearAllFlags();
  flags.setReturnsTainted();
  newExpr.setSideEffectFlags(flags.valueOf());

  assertTrue(NodeUtil.evaluatesToLocalValue(newExpr));

  flags.clearAllFlags();
  flags.setThrows();
  newExpr.setSideEffectFlags(flags.valueOf());

  assertFalse(NodeUtil.evaluatesToLocalValue(newExpr));

  flags.clearAllFlags();
  flags.setMutatesArguments();
  newExpr.setSideEffectFlags(flags.valueOf());

  assertFalse(NodeUtil.evaluatesToLocalValue(newExpr));

  flags.clearAllFlags();
  flags.setMutatesGlobalState();
  newExpr.setSideEffectFlags(flags.valueOf());

  assertFalse(NodeUtil.evaluatesToLocalValue(newExpr));
}
 
Example 3
Source File: NodeUtilTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testCallSideEffects() {
  Node callExpr = getNode("new x().method()");
  assertTrue(NodeUtil.functionCallHasSideEffects(callExpr));

  Node newExpr = callExpr.getFirstChild().getFirstChild();
  Preconditions.checkState(newExpr.isNew());
  Node.SideEffectFlags flags = new Node.SideEffectFlags();

  // No side effects, local result
  flags.clearAllFlags();
  newExpr.setSideEffectFlags(flags.valueOf());
  flags.clearAllFlags();
  callExpr.setSideEffectFlags(flags.valueOf());

  assertTrue(NodeUtil.evaluatesToLocalValue(callExpr));
  assertFalse(NodeUtil.functionCallHasSideEffects(callExpr));
  assertFalse(NodeUtil.mayHaveSideEffects(callExpr));

  // Modifies this, local result
  flags.clearAllFlags();
  newExpr.setSideEffectFlags(flags.valueOf());
  flags.clearAllFlags();
  flags.setMutatesThis();
  callExpr.setSideEffectFlags(flags.valueOf());

  assertTrue(NodeUtil.evaluatesToLocalValue(callExpr));
  assertFalse(NodeUtil.functionCallHasSideEffects(callExpr));
  assertFalse(NodeUtil.mayHaveSideEffects(callExpr));

  // Modifies this, non-local result
  flags.clearAllFlags();
  newExpr.setSideEffectFlags(flags.valueOf());
  flags.clearAllFlags();
  flags.setMutatesThis();
  flags.setReturnsTainted();
  callExpr.setSideEffectFlags(flags.valueOf());

  assertFalse(NodeUtil.evaluatesToLocalValue(callExpr));
  assertFalse(NodeUtil.functionCallHasSideEffects(callExpr));
  assertFalse(NodeUtil.mayHaveSideEffects(callExpr));

  // No modifications, non-local result
  flags.clearAllFlags();
  newExpr.setSideEffectFlags(flags.valueOf());
  flags.clearAllFlags();
  flags.setReturnsTainted();
  callExpr.setSideEffectFlags(flags.valueOf());

  assertFalse(NodeUtil.evaluatesToLocalValue(callExpr));
  assertFalse(NodeUtil.functionCallHasSideEffects(callExpr));
  assertFalse(NodeUtil.mayHaveSideEffects(callExpr));

  // The new modifies global state, no side-effect call, non-local result
  // This call could be removed, but not the new.
  flags.clearAllFlags();
  flags.setMutatesGlobalState();
  newExpr.setSideEffectFlags(flags.valueOf());
  flags.clearAllFlags();
  callExpr.setSideEffectFlags(flags.valueOf());

  assertTrue(NodeUtil.evaluatesToLocalValue(callExpr));
  assertFalse(NodeUtil.functionCallHasSideEffects(callExpr));
  assertTrue(NodeUtil.mayHaveSideEffects(callExpr));
}