<commit_before>/* -*- Mode: js; js-indent-level: 2; indent-tabs-mode: nil; tab-width: 4 -*- */
var AbcStream = (function () {
  function abcStream(bytes) {
    this.bytes = bytes;
    this.view = new DataView(bytes.buffer, bytes.byteOffset);
    this.pos = 0;
  }

  var resultBuffer = new Int32Array(256);

  abcStream.prototype = {
    get position() {
      return this.pos;
    },
    remaining: function () {
      return this.bytes.length - this.pos;
    },
    seek: function(pos) {
      this.pos = pos;
    },
    readU8: function() {
      return this.bytes[this.pos++];
    },
    readU8s: function(count) {
      var b = new Uint8Array(count);
      b.set(this.bytes.subarray(this.pos, this.pos + count), 0);
      this.pos += count;
      return b;
    },
    readS8: function() {
      return this.bytes[this.pos++] << 24 >> 24;
    },
    readU32: function() {
      return this.readS32() >>> 0;
    },
    readU30: function() {
      var result = this.readU32();
      if (result & 0xc0000000) {
        // TODO: Spec says this is a corrupt ABC file, but it seems that some content
        // has this, e.g. 1000-0.abc
        // error("Corrupt ABC File");
        return result;
      }
      return result;
    },
    readU30Unsafe: function() {
      return this.readU32();
    },
    readS16: function() {
      return (this.readU30Unsafe() << 16) >> 16;
    },
    /**
     * Read a variable-length encoded 32-bit signed integer. The value may use one to five bytes (little endian),
     * each contributing 7 bits. The most significant bit of each byte indicates that the next byte is part of
     * the value. The spec indicates that the most significant bit of the last byte to be read is sign extended
     * but this turns out not to be the case in the real implementation, for instance 0x7f should technically be
     * -1, but instead it's 127. Moreover, what happens to the remaining 4 high bits of the fifth byte that is
     * read? Who knows, here we'll just stay true to the Tamarin implementation.
     */
    readS32: function() {
      var result = this.readU8();
      if (result & 0x80) {
        result = result & 0x7f | this.readU8() << 7;
        if (result & 0x4000) {
          result = result & 0x3fff | this.readU8() << 14;
          if (result & 0x200000) {
            result = result & 0x1fffff | this.readU8() << 21;
            if (result & 0x10000000) {
              result = result & 0x0fffffff | this.readU8() << 28;
              result = result & 0xffffffff;
            }
          }
        }
      }
      return result;
    },
    readWord: function() {
      var result = this.view.getUint32(this.pos, true);
      this.pos += 4;
      return result;
    },
    readS24: function() {
      var u = this.readU8() |
        (this.readU8() << 8) |
        (this.readU8() << 16);
      return (u << 8) >> 8;
    },
    readDouble: function() {
      var result = this.view.getFloat64(this.pos, true);
      this.pos += 8;
      return result;
    },
    readUTFString: function(length) {
      var pos = this.pos;
      var end = pos + length;
      var bytes = this.bytes;
      var i = 0;
      if (!resultBuffer || resultBuffer.length < length) {
        resultBuffer = new Int32Array(length * 2);
      }
      var result = resultBuffer;
      while(pos < end) {
        var c = bytes[pos++];
        if (c <= 0x7f) {
          result[i++] = c;
        }
        else if (c >= 0xc0) { // multibyte
          var code = 0;
          if (c < 0xe0) { // 2 bytes
            code = ((c & 0x1f) << 6) |
                   (bytes[pos++] & 0x3f);
          }
          else if (c < 0xf0) { // 3 bytes
            code = ((c & 0x0f) << 12) |
                   ((bytes[pos++] & 0x3f) << 6) |
                   (bytes[pos++] & 0x3f);
          } else { // 4 bytes
            // turned into two characters in JS as surrogate pair
            code = (((c & 0x07) << 18) |
                    ((bytes[pos++] & 0x3f) << 12) |
                    ((bytes[pos++] & 0x3f) << 6) |
                    (bytes[pos++] & 0x3f)) - 0x10000;
            // High surrogate
            result[i++] = ((code & 0xffc00) >>> 10) + 0xd800;
            // Low surrogate
            code = (code & 0x3ff) + 0xdc00;
          }
          result[i++] = code;
        } // Otherwise it's an invalid UTF8, skipped.
      }
      this.pos = pos;
      return String.fromCharCode.apply(null, result.subarray(0, i));
    }
  };

  return abcStream;
})();

function parseTraits(abc, stream, holder) {
  var count = stream.readU30();
  var traits = [];
  for (var i = 0; i < count; i++) {
    traits.push(new Trait(abc, stream, holder));
  }
  return traits;
}

var Trait = (function () {
  function trait(abc, stream, holder) {
    var constantPool = abc.constantPool;
    var methods = abc.methods;
    var classes = abc.classes;
    var metadata = abc.metadata;

    this.holder = holder;
    this.name = constantPool.multinames[stream.readU30()];
    var tag = stream.readU8();

    this.kind = tag & 0x0F;
    this.attributes = (tag >> 4) & 0x0F;
    release || assert(Multiname.isQName(this.name), "Name must be a QName: " + this.name + ", kind: " + this.kind);

    switch (this.kind) {
    case TRAIT_Slot:
    case TRAIT_Const:
      this.slotId = stream.readU30();
      this.typeName = constantPool.multinames[stream.readU30()];
      var valueIndex = stream.readU30();
      this.value = undefined;
      if (valueIndex !== 0) {
        this.value = constantPool.getValue(stream.readU8(), valueIndex);
      }
      break;
    case TRAIT_Method:
    case TRAIT_Setter:
    case TRAIT_Getter:
      this.dispId = stream.readU30();
      this.methodInfo = methods[stream.readU30()];
      this.methodInfo.name = this.name;
      // make sure that the holder was not already set
      attachHolder(this.methodInfo, this.holder);
      break;
    case TRAIT_Class:<commit_msg>Remember the ABC for each methodInfo.<commit_after>/* -*- Mode: js; js-indent-level: 2; indent-tabs-mode: nil; tab-width: 4 -*- */
var AbcStream = (function () {
  function abcStream(bytes) {
    this.bytes = bytes;
    this.view = new DataView(bytes.buffer, bytes.byteOffset);
    this.pos = 0;
  }

  var resultBuffer = new Int32Array(256);

  abcStream.prototype = {
    get position() {
      return this.pos;
    },
    remaining: function () {
      return this.bytes.length - this.pos;
    },
    seek: function(pos) {
      this.pos = pos;
    },
    readU8: function() {
      return this.bytes[this.pos++];
    },
    readU8s: function(count) {
      var b = new Uint8Array(count);
      b.set(this.bytes.subarray(this.pos, this.pos + count), 0);
      this.pos += count;
      return b;
    },
    readS8: function() {
      return this.bytes[this.pos++] << 24 >> 24;
    },
    readU32: function() {
      return this.readS32() >>> 0;
    },
    readU30: function() {
      var result = this.readU32();
      if (result & 0xc0000000) {
        // TODO: Spec says this is a corrupt ABC file, but it seems that some content
        // has this, e.g. 1000-0.abc
        // error("Corrupt ABC File");
        return result;
      }
      return result;
    },
    readU30Unsafe: function() {
      return this.readU32();
    },
    readS16: function() {
      return (this.readU30Unsafe() << 16) >> 16;
    },
    /**
     * Read a variable-length encoded 32-bit signed integer. The value may use one to five bytes (little endian),
     * each contributing 7 bits. The most significant bit of each byte indicates that the next byte is part of
     * the value. The spec indicates that the most significant bit of the last byte to be read is sign extended
     * but this turns out not to be the case in the real implementation, for instance 0x7f should technically be
     * -1, but instead it's 127. Moreover, what happens to the remaining 4 high bits of the fifth byte that is
     * read? Who knows, here we'll just stay true to the Tamarin implementation.
     */
    readS32: function() {
      var result = this.readU8();
      if (result & 0x80) {
        result = result & 0x7f | this.readU8() << 7;
        if (result & 0x4000) {
          result = result & 0x3fff | this.readU8() << 14;
          if (result & 0x200000) {
            result = result & 0x1fffff | this.readU8() << 21;
            if (result & 0x10000000) {
              result = result & 0x0fffffff | this.readU8() << 28;
              result = result & 0xffffffff;
            }
          }
        }
      }
      return result;
    },
    readWord: function() {
      var result = this.view.getUint32(this.pos, true);
      this.pos += 4;
      return result;
    },
    readS24: function() {
      var u = this.readU8() |
        (this.readU8() << 8) |
        (this.readU8() << 16);
      return (u << 8) >> 8;
    },
    readDouble: function() {
      var result = this.view.getFloat64(this.pos, true);
      this.pos += 8;
      return result;
    },
    readUTFString: function(length) {
      var pos = this.pos;
      var end = pos + length;
      var bytes = this.bytes;
      var i = 0;
      if (!resultBuffer || resultBuffer.length < length) {
        resultBuffer = new Int32Array(length * 2);
      }
      var result = resultBuffer;
      while(pos < end) {
        var c = bytes[pos++];
        if (c <= 0x7f) {
          result[i++] = c;
        }
        else if (c >= 0xc0) { // multibyte
          var code = 0;
          if (c < 0xe0) { // 2 bytes
            code = ((c & 0x1f) << 6) |
                   (bytes[pos++] & 0x3f);
          }
          else if (c < 0xf0) { // 3 bytes
            code = ((c & 0x0f) << 12) |
                   ((bytes[pos++] & 0x3f) << 6) |
                   (bytes[pos++] & 0x3f);
          } else { // 4 bytes
            // turned into two characters in JS as surrogate pair
            code = (((c & 0x07) << 18) |
                    ((bytes[pos++] & 0x3f) << 12) |
                    ((bytes[pos++] & 0x3f) << 6) |
                    (bytes[pos++] & 0x3f)) - 0x10000;
            // High surrogate
            result[i++] = ((code & 0xffc00) >>> 10) + 0xd800;
            // Low surrogate
            code = (code & 0x3ff) + 0xdc00;
          }
          result[i++] = code;
        } // Otherwise it's an invalid UTF8, skipped.
      }
      this.pos = pos;
      return String.fromCharCode.apply(null, result.subarray(0, i));
    }
  };

  return abcStream;
})();

function parseTraits(abc, stream, holder) {
  var count = stream.readU30();
  var traits = [];
  for (var i = 0; i < count; i++) {
    traits.push(new Trait(abc, stream, holder));
  }
  return traits;
}

var Trait = (function () {
  function trait(abc, stream, holder) {
    var constantPool = abc.constantPool;
    var methods = abc.methods;
    var classes = abc.classes;
    var metadata = abc.metadata;

    this.holder = holder;
    this.name = constantPool.multinames[stream.readU30()];
    var tag = stream.readU8();

    this.kind = tag & 0x0F;
    this.attributes = (tag >> 4) & 0x0F;
    release || assert(Multiname.isQName(this.name), "Name must be a QName: " + this.name + ", kind: " + this.kind);

    switch (this.kind) {
    case TRAIT_Slot:
    case TRAIT_Const:
      this.slotId = stream.readU30();
      this.typeName = constantPool.multinames[stream.readU30()];
      var valueIndex = stream.readU30();
      this.value = undefined;
      if (valueIndex !== 0) {
        this.value = constantPool.getValue(stream.readU8(), valueIndex);
      }
      break;
    case TRAIT_Method:
    case TRAIT_Setter:
    case TRAIT_Getter:
      this.dispId = stream.readU30();
      this.methodInfo = methods[stream.readU30()];
      this.methodInfo.name = this.name;
      // make sure that the holder was not already set
      attachHolder(this.methodInfo, this.holder);
      this.methodInfo.abc = abc;
      break;
    case TRAIT_Class:
{'fraction_non_alphanumeric': 0.13606875052658185, 'fraction_numerical': 0.035891819024349146, 'mean_word_length': 1.9498011928429424, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, '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': '3555608', 'n_tokens_mistral': 4113, 'n_tokens_neox': 3780, 'n_words': 1070}