<commit_before>import formats from './formats';
import {transforms} from 'vega-dataflow';
import {load} from 'vega-loader';
import {
  error, extend, isArray, isObject, isString, pad, truncate, truthy
} from 'vega-util';
import {parse, context} from 'vega-runtime';
import {scaleGradient} from 'vega-scenegraph';
import {rgb, lab, hcl, hsl} from 'd3-color';

function scale(name, ctx) {
  var s = isString(name) ? ctx.scales[name]
    : isObject(name) && name.signal ? ctx.signals[name.signal]
    : undefined;
  return s && s.value;
}

function functions(fn, ctx) {
  extend(fn, formats());
  fn.pad = pad;
  fn.truncate = truncate;

  fn.rgb = rgb;
  fn.lab = lab;
  fn.hcl = hcl;
  fn.hsl = hsl;
  fn.gradient = scaleGradient;

  fn.pinchDistance = function() {
    return 'Math.sqrt('
      + 'pow(event.touches[0].clientX - event.touches[1].clientX, 2) + '
      + 'pow(event.touches[0].clientY - event.touches[1].clientY, 2)'
      + ')';
  };

  fn.pinchAngle = function() {
    return 'Math.atan2('
      + 'event.touches[1].clientY - event.touches[0].clientY,'
      + 'event.touches[1].clientX - event.touches[0].clientX'
      + ')';
  };

  fn.open = function(uri, name) {
    if (typeof window !== 'undefined' && window && window.open) {
      var opt = ctx.dataflow.loadOptions(),
          url = load.sanitize(uri, extend({type:'open', name:name}, opt));
      if (url) {
        window.open(url, name);
      } else {
        error('Invalid URL: ' + opt.url);
      }
    } else {
      error('Open function can only be invoked in a browser.');
    }
  };

  fn.span = function(array) {
    return array[array.length-1] - array[0];
  };

  fn.range = function(name) {
    var s = scale(name, ctx);
    return s && s.range ? s.range() : [0, 0];
  };

  fn.bandwidth = function(name) {
    var s = scale(name, ctx);
    return s && s.bandwidth ? s.bandwidth() : 0;
  };

  fn.scale = function(name, value) {
    var s = scale(name, ctx);
    return s ? s(value) : undefined;
  };

  fn.scaleInvert = function(name, range) {
    var s = scale(name, ctx);
    return !s ? undefined
      : isArray(range) ? (s.invertRange || s.invert)(range)
      : (s.invert || s.invertExtent)(range);
  };

  fn.scaleCopy = function(name) {
    var s = scale(name, ctx);
    return s ? s.copy() : undefined;
  };

  fn.indata = function(name, field, value) {
    var index = ctx.data[name]['index:' + field],
        entry = index ? index.value[value] : undefined;
    return entry ? entry.count : entry;
  };

  fn.encode = function(item, name, retval) {
    if (item) {
      var df = ctx.dataflow,
          target = item.mark.source;
      df.pulse(target, df.changeset().encode(item, name));
    }
    return retval !== undefined ? retval : item;
  };

  fn.modify = function(name, insert, remove, toggle, modify, values) {
    var df = ctx.dataflow,
        data = ctx.data[name],
        input = data.input,
        changes = data.changes,
        stamp = df.stamp(),
        predicate, key;

    if (!(input.value.length || insert || toggle)) {
      // nothing to do!
      return 0;
    }

    if (!changes || changes.stamp < stamp) {
      data.changes = (changes = df.changeset());
      changes.stamp = stamp;
      df.runAfter(function() { df.pulse(input, changes).run(); });
    }

    if (remove) {
      changes.remove(remove === true ? truthy : remove);
    }

    if (insert) {
      changes.insert(insert);
    }

    if (toggle) {
      predicate = function(_) {
        for (key in toggle) {
          if (_[key] !== toggle[key]) return false;
        }
        return true;
      };

      if (input.value.filter(predicate).length) {
        changes.remove(predicate);
      } else {
        changes.insert(toggle);
      }
    }

    if (modify) {
      for (key in values) {
        changes.modify(modify, key, values[key]);
      }
    }

    return 1;
  };

  return ctx;
}

export default function(view, spec) {
  var fn = {},
      ctx = context(view, transforms, fn);
  return parse(spec, functions(fn, ctx));
}<commit_msg>Update runtime to use dynamic context lookup.<commit_after>import formats from './formats';
import {transforms} from 'vega-dataflow';
import {load} from 'vega-loader';
import {
  error, extend, isArray, isObject, isString, pad, truncate, truthy
} from 'vega-util';
import {parse, context} from 'vega-runtime';
import {scaleGradient} from 'vega-scenegraph';
import {rgb, lab, hcl, hsl} from 'd3-color';

function scale(name, ctx) {
  var s = isString(name) ? ctx.scales[name]
    : isObject(name) && name.signal ? ctx.signals[name.signal]
    : undefined;
  return s && s.value;
}

function functions() {
  var fn = formats();
  fn.pad = pad;
  fn.truncate = truncate;

  fn.rgb = rgb;
  fn.lab = lab;
  fn.hcl = hcl;
  fn.hsl = hsl;
  fn.gradient = scaleGradient;

  fn.pinchDistance = function() {
    return 'Math.sqrt('
      + 'pow(event.touches[0].clientX - event.touches[1].clientX, 2) + '
      + 'pow(event.touches[0].clientY - event.touches[1].clientY, 2)'
      + ')';
  };

  fn.pinchAngle = function() {
    return 'Math.atan2('
      + 'event.touches[1].clientY - event.touches[0].clientY,'
      + 'event.touches[1].clientX - event.touches[0].clientX'
      + ')';
  };

  fn.open = function(uri, name) {
    if (typeof window !== 'undefined' && window && window.open) {
      var opt = this.context.dataflow.loadOptions(),
          url = load.sanitize(uri, extend({type:'open', name:name}, opt));
      if (url) {
        window.open(url, name);
      } else {
        error('Invalid URL: ' + opt.url);
      }
    } else {
      error('Open function can only be invoked in a browser.');
    }
  };

  fn.span = function(array) {
    return array[array.length-1] - array[0];
  };

  fn.range = function(name) {
    var s = scale(name, this.context);
    return s && s.range ? s.range() : [0, 0];
  };

  fn.bandwidth = function(name) {
    var s = scale(name, this.context);
    return s && s.bandwidth ? s.bandwidth() : 0;
  };

  fn.scale = function(name, value) {
    var s = scale(name, this.context);
    return s ? s(value) : undefined;
  };

  fn.scaleInvert = function(name, range) {
    var s = scale(name, this.context);
    return !s ? undefined
      : isArray(range) ? (s.invertRange || s.invert)(range)
      : (s.invert || s.invertExtent)(range);
  };

  fn.scaleCopy = function(name) {
    var s = scale(name, this.context);
    return s ? s.copy() : undefined;
  };

  fn.indata = function(name, field, value) {
    var index = this.context.data[name]['index:' + field],
        entry = index ? index.value[value] : undefined;
    return entry ? entry.count : entry;
  };

  fn.encode = function(item, name, retval) {
    if (item) {
      var df = this.context.dataflow,
          target = item.mark.source;
      df.pulse(target, df.changeset().encode(item, name));
    }
    return retval !== undefined ? retval : item;
  };

  fn.modify = function(name, insert, remove, toggle, modify, values) {
    var df = this.context.dataflow,
        data = this.context.data[name],
        input = data.input,
        changes = data.changes,
        stamp = df.stamp(),
        predicate, key;

    if (!(input.value.length || insert || toggle)) {
      // nothing to do!
      return 0;
    }

    if (!changes || changes.stamp < stamp) {
      data.changes = (changes = df.changeset());
      changes.stamp = stamp;
      df.runAfter(function() { df.pulse(input, changes).run(); });
    }

    if (remove) {
      changes.remove(remove === true ? truthy : remove);
    }

    if (insert) {
      changes.insert(insert);
    }

    if (toggle) {
      predicate = function(_) {
        for (key in toggle) {
          if (_[key] !== toggle[key]) return false;
        }
        return true;
      };

      if (input.value.filter(predicate).length) {
        changes.remove(predicate);
      } else {
        changes.insert(toggle);
      }
    }

    if (modify) {
      for (key in values) {
        changes.modify(modify, key, values[key]);
      }
    }

    return 1;
  };

  return fn;
}

export default function(view, spec) {
  return parse(spec, context(view, transforms, functions()));
}
{'fraction_non_alphanumeric': 0.16379204892966362, 'fraction_numerical': 0.004648318042813455, 'mean_word_length': 2.504500642948993, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '657260', 'n_tokens_mistral': 2844, 'n_tokens_neox': 2784, 'n_words': 643}