<commit_before>package com.squarespace.v6.template.less.model;

import static com.squarespace.v6.template.less.core.LessUtils.safeEquals;

import java.util.ArrayList;
import java.util.List;

import com.squarespace.v6.template.less.LessException;
import com.squarespace.v6.template.less.core.Buffer;
import com.squarespace.v6.template.less.core.LessUtils;
import com.squarespace.v6.template.less.exec.ExecEnv;
import com.squarespace.v6.template.less.exec.Function;


public class FunctionCall extends BaseNode {

  private String name;
  
  private List<Node> args;

  private boolean evaluate;
  
  private boolean noimpl;
  
  public FunctionCall(String name) {
    this(name, null, false);
  }
  
  public FunctionCall(String name, List<Node> args) {
    this(name, args, false);
  }

  public FunctionCall(String name, List<Node> args, boolean noimpl) {
    if (name == null) {
      throw new IllegalArgumentException("Serious error: name cannot be null");
    }
    this.name = name;
    this.args = args;
    this.noimpl = noimpl;
  }
  
  public String name() {
    return name;
  }
  
  public List<Node> args() {
    return LessUtils.safeList(args);
  }
  
  public void add(Node arg) {
    args = LessUtils.initList(args, 3);
    args.add(arg);
    evaluate |= arg.needsEval();
  }
  
  @Override
  public boolean needsEval() {
    return !noimpl || evaluate;
  }
  
  @Override
  public Node eval(ExecEnv env) throws LessException {
    if (noimpl) {
      return evaluate ? new FunctionCall(name, evalArgs(env), true) : this;
    }
    
    // Check if this function is built-in.
    Function func = env.context().findFunction(name);
    if (func != null) {
      // Invoke built-in function
      List<Node> values = evalArgs(env);
      Node result = null;
      func.spec().validate(func, values);
      result = func.invoke(env, values);
      if (result != null) {
        return result;
      }
      
      // If we get null, fall through. Its a way for a function impl to signal
      // that it should be emitted, not executed. This hapepns in the context()
      // function -- it checks its args, and if the first arg is not of the expected
      // type, it returns null, indicating that the function call's repr should
      // be emitted, not an evaluated result.
    }
    
    // Function is not a built-in so render the function and its args.
    return evaluate ? new FunctionCall(name, evalArgs(env), true) : this;
  }
  
  @Override
  public boolean equals(Object obj) {
    if (obj instanceof FunctionCall) {
      FunctionCall other = (FunctionCall)obj;
      return safeEquals(name, other.name) && safeEquals(args, other.args);
    }
    return false;
  }
  
  @Override
  public NodeType type() {
    return NodeType.FUNCTION_CALL;
  }
  
  @Override
  public void repr(Buffer buf) {
    buf.append(name).append('(');
    int size = args.size();
    for (int i = 0; i < size; i++) {
      if (i > 0) {
        buf.append(", ");
      }
      args.get(i).repr(buf);
    }
    buf.append(')');
  }
  
  @Override
  public void modelRepr(Buffer buf) {<commit_msg>[less] Ensure null args are handled in repr.<commit_after>package com.squarespace.v6.template.less.model;

import static com.squarespace.v6.template.less.core.LessUtils.safeEquals;

import java.util.ArrayList;
import java.util.List;

import com.squarespace.v6.template.less.LessException;
import com.squarespace.v6.template.less.core.Buffer;
import com.squarespace.v6.template.less.core.LessUtils;
import com.squarespace.v6.template.less.exec.ExecEnv;
import com.squarespace.v6.template.less.exec.Function;


public class FunctionCall extends BaseNode {

  private String name;
  
  private List<Node> args;

  private boolean evaluate;
  
  private boolean noimpl;
  
  public FunctionCall(String name) {
    this(name, null, false);
  }
  
  public FunctionCall(String name, List<Node> args) {
    this(name, args, false);
  }

  public FunctionCall(String name, List<Node> args, boolean noimpl) {
    if (name == null) {
      throw new IllegalArgumentException("Serious error: name cannot be null");
    }
    this.name = name;
    this.args = args;
    this.noimpl = noimpl;
  }
  
  public String name() {
    return name;
  }
  
  public List<Node> args() {
    return LessUtils.safeList(args);
  }
  
  public void add(Node arg) {
    args = LessUtils.initList(args, 3);
    args.add(arg);
    evaluate |= arg.needsEval();
  }
  
  @Override
  public boolean needsEval() {
    return !noimpl || evaluate;
  }
  
  @Override
  public Node eval(ExecEnv env) throws LessException {
    if (noimpl) {
      return evaluate ? new FunctionCall(name, evalArgs(env), true) : this;
    }
    
    // Check if this function is built-in.
    Function func = env.context().findFunction(name);
    if (func != null) {
      // Invoke built-in function
      List<Node> values = evalArgs(env);
      Node result = null;
      func.spec().validate(func, values);
      result = func.invoke(env, values);
      if (result != null) {
        return result;
      }
      
      // If we get null, fall through. Its a way for a function impl to signal
      // that it should be emitted, not executed. This hapepns in the context()
      // function -- it checks its args, and if the first arg is not of the expected
      // type, it returns null, indicating that the function call's repr should
      // be emitted, not an evaluated result.
    }
    
    // Function is not a built-in so render the function and its args.
    return evaluate ? new FunctionCall(name, evalArgs(env), true) : this;
  }
  
  @Override
  public boolean equals(Object obj) {
    if (obj instanceof FunctionCall) {
      FunctionCall other = (FunctionCall)obj;
      return safeEquals(name, other.name) && safeEquals(args, other.args);
    }
    return false;
  }
  
  @Override
  public NodeType type() {
    return NodeType.FUNCTION_CALL;
  }
  
  @Override
  public void repr(Buffer buf) {
    buf.append(name).append('(');
    if (args != null) {
      int size = args.size();
      for (int i = 0; i < size; i++) {
        if (i > 0) {
          buf.append(", ");
        }
        args.get(i).repr(buf);
      }
    }
    buf.append(')');
  }
  
  @Override
  public void modelRepr(Buffer buf) {
{'fraction_non_alphanumeric': 0.11472794000319132, 'fraction_numerical': 0.0031913196106590075, 'mean_word_length': 2.733174508636093, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1376276', 'n_tokens_mistral': 1993, 'n_tokens_neox': 1967, 'n_words': 568}