<commit_before>var EventEmitter = require('events').EventEmitter
  , filename = __filename
  , current_trace_error = null
  , in_prepare = 0;

exports.empty_frame = '---------------------------------------------';
exports.async_trace_limit = 10;

exports.format_stack_frame = function(frame) {
  if (frame.getFileName() === exports.empty_frame) { return exports.empty_frame; }
  
  var format_location = function(frame) {
    if (frame.isNative()) { return 'native'; }
    if (frame.isEval()) { return 'eval at ' + frame.getEvalOrigin(); }
    var file = frame.getFileName()
      , line = frame.getLineNumber()
      , column = frame.getColumnNumber();
    return !file ? 'unknown source' : file + (!line ? '' : ':' + line + (!column ? '' : ':' + column));
  };
  var format_method = function(frame) {
    var function_name = frame.getFunctionName();
    
    if (!(frame.isToplevel() || frame.isConstructor())) {
      var method = frame.getMethodName();
      return frame.getTypeName() + '.' + (!function_name ? method || '<anonymous>' : function_name + (method && method !== function_name ? ' [as ' + method + ']' : ''));
    }
    if (frame.isConstructor()) { return 'new ' + (function_name || '<anonymous>'); }
    if (function_name) { return function_name; }
    return null;
  };
  
  var method = format_method(frame)
    , location = format_location(frame);
  return '    at ' + (!method ? location : method + ' (' + location + ')');
};

exports.format_stack = function(err, frames) {
  var lines = [];
  try {
    lines.push(err.toString());
  } catch (e) {
    console.log('Caught error in longjohn.  Please report this to <EMAIL>.');
  }
  Array.prototype.push.apply(lines, frames.map(exports.format_stack_frame));
  return lines.join('\n');
};

var create_callsite = function(location) {
  return Object.create({
    getFileName: function() { return location; },
    getLineNumber: function() { return null; },
    getFunctionName: function() { return null; },
    getTypeName: function() { return null; },
    getMethodName: function() { return null; },
    getColumnNumber: function() { return null; },
    isNative: function() { return null; }
  });
};

Error.prepareStackTrace = function(error, structured_stack_trace) {
  error.__cached_trace__ = structured_stack_trace.filter(function(f) {
    return f.getFileName() !== filename;
  });
  
  if (!error.__previous__ && current_trace_error) {
    error.__previous__ = current_trace_error;
  }
  
  ++in_prepare;
  var previous_trace = error.__previous__ ? error.__previous__.stack : null;
  --in_prepare;
  if (previous_trace) {
    error.__cached_trace__.push(create_callsite(exports.empty_frame));
    Array.prototype.push.apply(error.__cached_trace__, previous_trace);
  }

  if (in_prepare > 0) { return error.__cached_trace__; }
  return exports.format_stack(error, error.__cached_trace__);
};

var limit_frames = function(stack) {
  if (exports.async_trace_limit <= 0 || (stack && stack.__trace_count__ < exports.async_trace_limit)) { return; }
  
  var count = 1
    , previous = stack;
  while (previous) {
    if (count >= exports.async_trace_limit) {
      delete previous.__previous__;
      return;
    }
    previous = previous.__previous__;
    ++count;
  }
};

var wrap_callback = function(callback, location) {
  var trace_error = new Error();
  trace_error.__location__ = location;
  trace_error.__previous__ = current_trace_error;
  trace_error.__trace_count__ = current_trace_error ? current_trace_error.__trace_count__ + 1 : 1;

  limit_frames(trace_error);
  
  var new_callback = function() {
    current_trace_error = trace_error;
    try {
      callback.apply(this, arguments);
    } catch (e) {
      // Ensure we're formatting the Error in longjohn
      e.stack;
      throw e;
    } finally {
      current_trace_error = null;
    }
  };
  
  new_callback.__original_callback__ = callback;
  return new_callback;
};
<commit_msg>Clear trace_error variable from closure so it can potentially be garbage collected.<commit_after>var EventEmitter = require('events').EventEmitter
  , filename = __filename
  , current_trace_error = null
  , in_prepare = 0;

exports.empty_frame = '---------------------------------------------';
exports.async_trace_limit = 10;

exports.format_stack_frame = function(frame) {
  if (frame.getFileName() === exports.empty_frame) { return exports.empty_frame; }
  
  var format_location = function(frame) {
    if (frame.isNative()) { return 'native'; }
    if (frame.isEval()) { return 'eval at ' + frame.getEvalOrigin(); }
    var file = frame.getFileName()
      , line = frame.getLineNumber()
      , column = frame.getColumnNumber();
    return !file ? 'unknown source' : file + (!line ? '' : ':' + line + (!column ? '' : ':' + column));
  };
  var format_method = function(frame) {
    var function_name = frame.getFunctionName();
    
    if (!(frame.isToplevel() || frame.isConstructor())) {
      var method = frame.getMethodName();
      return frame.getTypeName() + '.' + (!function_name ? method || '<anonymous>' : function_name + (method && method !== function_name ? ' [as ' + method + ']' : ''));
    }
    if (frame.isConstructor()) { return 'new ' + (function_name || '<anonymous>'); }
    if (function_name) { return function_name; }
    return null;
  };
  
  var method = format_method(frame)
    , location = format_location(frame);
  return '    at ' + (!method ? location : method + ' (' + location + ')');
};

exports.format_stack = function(err, frames) {
  var lines = [];
  try {
    lines.push(err.toString());
  } catch (e) {
    console.log('Caught error in longjohn.  Please report this to matt.insler<EMAIL>.');
  }
  Array.prototype.push.apply(lines, frames.map(exports.format_stack_frame));
  return lines.join('\n');
};

var create_callsite = function(location) {
  return Object.create({
    getFileName: function() { return location; },
    getLineNumber: function() { return null; },
    getFunctionName: function() { return null; },
    getTypeName: function() { return null; },
    getMethodName: function() { return null; },
    getColumnNumber: function() { return null; },
    isNative: function() { return null; }
  });
};

Error.prepareStackTrace = function(error, structured_stack_trace) {
  error.__cached_trace__ = structured_stack_trace.filter(function(f) {
    return f.getFileName() !== filename;
  });
  
  if (!error.__previous__ && current_trace_error) {
    error.__previous__ = current_trace_error;
  }
  
  ++in_prepare;
  var previous_trace = error.__previous__ ? error.__previous__.stack : null;
  --in_prepare;
  if (previous_trace) {
    error.__cached_trace__.push(create_callsite(exports.empty_frame));
    Array.prototype.push.apply(error.__cached_trace__, previous_trace);
  }

  if (in_prepare > 0) { return error.__cached_trace__; }
  return exports.format_stack(error, error.__cached_trace__);
};

var limit_frames = function(stack) {
  if (exports.async_trace_limit <= 0 || (stack && stack.__trace_count__ < exports.async_trace_limit)) { return; }
  
  var count = 1
    , previous = stack;
  while (previous) {
    if (count >= exports.async_trace_limit) {
      delete previous.__previous__;
      return;
    }
    previous = previous.__previous__;
    ++count;
  }
};

var wrap_callback = function(callback, location) {
  var trace_error = new Error();
  trace_error.__location__ = location;
  trace_error.__previous__ = current_trace_error;
  trace_error.__trace_count__ = current_trace_error ? current_trace_error.__trace_count__ + 1 : 1;

  limit_frames(trace_error);
  
  var new_callback = function() {
    current_trace_error = trace_error;
    // Clear trace_error variable from the closure, so it can potentially be garbage collected.
    trace_error = null;

    try {
      callback.apply(this, arguments);
    } catch (e) {
      // Ensure we're formatting the Error in longjohn
      e.stack;
      throw e;
    } finally {
      current_trace_error = null;
    }
  };
  
  new_callback.__original_callback__ = callback;
  return new_callback;
};

{'fraction_non_alphanumeric': 0.14697120158887786, 'fraction_numerical': 0.0019860973187686196, 'mean_word_length': 3.518788558609086, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '5635947', 'n_tokens_mistral': 2457, 'n_tokens_neox': 2439, 'n_words': 532}