<commit_before>/*
 * TODO: we should make this into an object rather than global functions and variables
 * i.e: Insight.glpInit = function() {...}
 */

var glpVerboseLogs = false;

/**
 * Instantiates messaging with the devtools panel
 */
function glpInit() {
    window.postMessage({ type: "init" }, "*");
}
glpInit();

/*
 * TODO: This should be moved to a completely different file because
 * it has nothing to do with webgl debugging toolkit
 * This section tries to figure out what line number called a certain function
 * Other things we can see other than line number include but are not limited to:
 * getThis, getTypeName, getFunction, getFunctionName, getMethodName, getFileName, getLineNumber,
 * getColumnNumber, getEvalOrigin, isToplevel, isEval, isNative, isConstructor
 *
 * To see more details, go to
 * http://stackoverflow.com/questions/11386492/accessing-line-number-in-v8-javascript-chrome-node-js
 * https://github.com/v8/v8/wiki/Stack%20Trace%20API
*/
function glpGetStack() {
    var orig = Error.prepareStackTrace;
    Error.prepareStackTrace = function(_, stack){ return stack; };
    var err = new Error;
    // console.log(arguments.callee.caller);
    Error.captureStackTrace(err, arguments.callee);
    var stack = err.stack;
    Error.prepareStackTrace = orig;
    return stack;
}

// Uses the stack and gets the first call's line number
function glpGetLineNumber() {
  var stack = glpGetStack();
  return stack[stack.length-1].getLineNumber();
}

// input: given a full stack trace down to glp
// output: gives the first stack that's not
// bug: if we refactor our code, we need to change this. not sure
// how to fix until we have a consistent file naming scheme
function glpGetFirstUserStack(fullStack) {
  // The first item in the full stack is this file
  var glpStackFileName = fullStack[0].getFileName();

  for(var i = 1; i < fullStack.length; i++) {
    if (fullStack[i].getFileName() != glpStackFileName)
      return fullStack[i];
  }

  return null;
}

/* End stack trace code */

/**
 * Sends messages to the devtools panel
 * @param {String} Message type
 * @param {Dictionary} Message data
 */
function glpSendMessage(type, data) {
    window.postMessage({ source:"content", type: type, data: data}, "*");
}

/**
 * Receive messages from the devtools panel
 */
window.addEventListener('message', function(event) {
  var message = event.data;

  // Only accept messages that we know are ours
  if (typeof message !== 'object' || message === null || message.source != "panel") {
    return;
  }

  if (message.type == "pixelInspector") {
    glpPixelInspectorToggle(message.data.enabled);
  } else if (message.type == "callStackRequest") {
    glpSendCallStack(message.data);
  } else if (message.type == "functionHistogramRequest") {
    glpSendFunctionHistogram(message.data.threshold);
  } else if (message.type == "beginProgramUsageCount") {
    glpBeginProgramUsageCount();
  } else if (message.type == "stopProgramUsageCount") {
    glpStopProgramUsageCount();
  } else if (message.type == "resetProgramUsageCount") {
    glpResetProgramUsageCount();
  } else if (message.type == "getProgramUsageCount") {
    glpGetCurrentProgramUsageCount();
  } else if (message.type == "toggleDuplicateProgramUsage") {
    glpToggleDuplicateProgramUsage(message.data.enabled);
  } else if (message.type == "getDuplicateProgramUse") {
    glpGetDuplicateProgramUsage();
  } else {
    console.log(message.data);
  }
});

WebGLRenderingContext.prototype.glpCallstackEnabled = true;
WebGLRenderingContext.prototype.glpCallstackMaxSize = 100;
WebGLRenderingContext.prototype.glpFunctionHistogramEnabled = true;
WebGLRenderingContext.prototype.glpMostRecentCalls = [];
WebGLRenderingContext.prototype.glpCallsSinceDraw = [];
/*
 * Define all glp functions to be bound
*/
var glpFcnBindings = {
    // The default function is called first before all other method calls
    default: function(original, args, name) {
        if (glpVerboseLogs) {
            console.log("Function Call: " + name)
        }
        if (this.glpCallstackEnabled) {
            var callDetails = [name, JSON.stringify(args)];

            if (this.glpMostRecentCalls.length > this.glpCallstackMaxSize) {
                this.glpMostRecentCalls.shift();
            }
            this.glpMostRecentCalls.push(callDetails);

            if (name == "drawElements" || name == "drawArrays") {
                this.glpCallsSinceDraw = [];
            }
            this.glpCallsSinceDraw.push(callDetails);
        }
        if (this.glpFunctionHistogramEnabled) {
          if (!this.glpFunctionHistogram) {
            this.glpFunctionHistogram = {};
          }
          if (!this.glpFunctionHistogram[name]) {
            this.glpFunctionHistogram[name] = 1;
          } else {
            this.glpFunctionHistogram[name] += 1;
          }
        }
        return original.apply(this, args);
    },
    attachShader : function(original, args, name) {
        var program = args[0];
        var shader = args[1];
        var shaderType = this.getShaderParameter(shader, this.SHADER_TYPE);

        // TODO: verify valid input
        // glpPixelInspector: store vertex shaders associated with program
        if (shaderType == this.VERTEX_SHADER) {<commit_msg>Add duration to call stack<commit_after>/*
 * TODO: we should make this into an object rather than global functions and variables
 * i.e: Insight.glpInit = function() {...}
 */

var glpVerboseLogs = false;

/**
 * Instantiates messaging with the devtools panel
 */
function glpInit() {
    window.postMessage({ type: "init" }, "*");
}
glpInit();

/*
 * TODO: This should be moved to a completely different file because
 * it has nothing to do with webgl debugging toolkit
 * This section tries to figure out what line number called a certain function
 * Other things we can see other than line number include but are not limited to:
 * getThis, getTypeName, getFunction, getFunctionName, getMethodName, getFileName, getLineNumber,
 * getColumnNumber, getEvalOrigin, isToplevel, isEval, isNative, isConstructor
 *
 * To see more details, go to
 * http://stackoverflow.com/questions/11386492/accessing-line-number-in-v8-javascript-chrome-node-js
 * https://github.com/v8/v8/wiki/Stack%20Trace%20API
*/
function glpGetStack() {
    var orig = Error.prepareStackTrace;
    Error.prepareStackTrace = function(_, stack){ return stack; };
    var err = new Error;
    // console.log(arguments.callee.caller);
    Error.captureStackTrace(err, arguments.callee);
    var stack = err.stack;
    Error.prepareStackTrace = orig;
    return stack;
}

// Uses the stack and gets the first call's line number
function glpGetLineNumber() {
  var stack = glpGetStack();
  return stack[stack.length-1].getLineNumber();
}

// input: given a full stack trace down to glp
// output: gives the first stack that's not
// bug: if we refactor our code, we need to change this. not sure
// how to fix until we have a consistent file naming scheme
function glpGetFirstUserStack(fullStack) {
  // The first item in the full stack is this file
  var glpStackFileName = fullStack[0].getFileName();

  for(var i = 1; i < fullStack.length; i++) {
    if (fullStack[i].getFileName() != glpStackFileName)
      return fullStack[i];
  }

  return null;
}

/* End stack trace code */

/**
 * Sends messages to the devtools panel
 * @param {String} Message type
 * @param {Dictionary} Message data
 */
function glpSendMessage(type, data) {
    window.postMessage({ source:"content", type: type, data: data}, "*");
}

/**
 * Receive messages from the devtools panel
 */
window.addEventListener('message', function(event) {
  var message = event.data;

  // Only accept messages that we know are ours
  if (typeof message !== 'object' || message === null || message.source != "panel") {
    return;
  }

  if (message.type == "pixelInspector") {
    glpPixelInspectorToggle(message.data.enabled);
  } else if (message.type == "callStackRequest") {
    glpSendCallStack(message.data);
  } else if (message.type == "functionHistogramRequest") {
    glpSendFunctionHistogram(message.data.threshold);
  } else if (message.type == "beginProgramUsageCount") {
    glpBeginProgramUsageCount();
  } else if (message.type == "stopProgramUsageCount") {
    glpStopProgramUsageCount();
  } else if (message.type == "resetProgramUsageCount") {
    glpResetProgramUsageCount();
  } else if (message.type == "getProgramUsageCount") {
    glpGetCurrentProgramUsageCount();
  } else if (message.type == "toggleDuplicateProgramUsage") {
    glpToggleDuplicateProgramUsage(message.data.enabled);
  } else if (message.type == "getDuplicateProgramUse") {
    glpGetDuplicateProgramUsage();
  } else {
    console.log(message.data);
  }
});

WebGLRenderingContext.prototype.glpCallstackEnabled = true;
WebGLRenderingContext.prototype.glpCallstackMaxSize = 100;
WebGLRenderingContext.prototype.glpFunctionHistogramEnabled = true;
WebGLRenderingContext.prototype.glpMostRecentCalls = [];
WebGLRenderingContext.prototype.glpCallsSinceDraw = [];
/*
 * Define all glp functions to be bound
*/
var glpFcnBindings = {
    // The default function is called first before all other method calls
    default: function(original, args, name) {
        if (glpVerboseLogs) {
            console.log("Function Call: " + name)
        }
        if (this.glpCallstackEnabled) {
            var callDetails = [name, JSON.stringify(args), window.performance.now()];

            if (this.glpMostRecentCalls.length > this.glpCallstackMaxSize) {
                this.glpMostRecentCalls.shift();
            }
            this.glpMostRecentCalls.push(callDetails);

            if (name == "drawElements" || name == "drawArrays") {
                this.glpCallsSinceDraw = [];
            }
            this.glpCallsSinceDraw.push(callDetails);
        }
        if (this.glpFunctionHistogramEnabled) {
          if (!this.glpFunctionHistogram) {
            this.glpFunctionHistogram = {};
          }
          if (!this.glpFunctionHistogram[name]) {
            this.glpFunctionHistogram[name] = 1;
          } else {
            this.glpFunctionHistogram[name] += 1;
          }
        }
        var ret = original.apply(this, args);
        if (this.glpCallstackEnabled) {
          var endTime = window.performance.now();
          if (this.glpCallsSinceDraw.length > 1) {
            var lastInStack = this.glpCallsSinceDraw[this.glpCallsSinceDraw.length - 1];
            if(lastInStack[0] == name) {
              lastInStack[2] = (endTime - lastInStack[2]) * 1000
            }
          }
        }
        return ret;
    },
    attachShader : function(original, args, name) {
        var program = args[0];
        var shader = args[1];
        var shaderType = this.getShaderParameter(shader, this.SHADER_TYPE);

        // TODO: verify valid input
        // glpPixelInspector: store vertex shaders associated with program
        if (shaderType == this.VERTEX_SHADER) {
{'fraction_non_alphanumeric': 0.1139055950428285, 'fraction_numerical': 0.005376344086021506, 'mean_word_length': 3.257176105508146, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 2, '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': '541758', 'n_tokens_mistral': 3254, 'n_tokens_neox': 3196, 'n_words': 846}