<commit_before>
# pull in external modules
_ ?= require '../third_party/underscore-min.js'
util ?= require './util'
opcodes ?= require './opcodes'
make_attributes ?= require './attributes'

# things assigned to root will be available outside this module
root = exports ? this.methods = {}

class AbstractMethodField
  """ Subclasses need to implement parse_descriptor(String) """
  constructor: (@class_name) ->

  parse: (bytes_array,constant_pool) ->
    @access_flags = util.parse_flags(util.read_uint(bytes_array.splice(0,2)))
    @name = constant_pool.get(util.read_uint(bytes_array.splice(0,2))).value
    @raw_descriptor = constant_pool.get(util.read_uint(bytes_array.splice(0,2))).value
    @parse_descriptor @raw_descriptor
    [@attrs,bytes_array] = make_attributes(bytes_array,constant_pool)
    return bytes_array
  
  parse_field_type: (char_array) ->
    c = char_array.shift()
    switch c
      when 'B' then { type: 'byte' }
      when 'C' then { type: 'char' }
      when 'D' then { type: 'double' }
      when 'F' then { type: 'float' }
      when 'I' then { type: 'int' }
      when 'J' then { type: 'long' }
      when 'L' then {
        type: 'reference'
        ref_type: 'class'
        referent: {
          type: 'class' # not technically a legal type
          class_name: (c while (c = char_array.shift()) != ';').join('')
        }
      }
      when 'S' then { type: 'short' }
      when 'Z' then { type: 'boolean' }
      when '[' then {
        type: 'reference'
        ref_type: 'array'
        referent: @parse_field_type char_array
      }
      else
        char_array.unshift(c)
        return null

class root.Field extends AbstractMethodField
  parse_descriptor: (raw_descriptor) ->
    @type = @parse_field_type raw_descriptor.split ''
    if @access_flags.static
      @static_value = null  # loaded in when getstatic is called

trapped_methods = {
  'java/lang/System::setJavaLangAccess()V': (rs) -> #NOP
  'java/lang/System::loadLibrary(Ljava/lang/String;)V': (rs) -> console.log "warning: library loads are NYI"
  'java/lang/System::adjustPropertiesForBackwardCompatibility(Ljava/util/Properties;)V': (rs) -> #NOP (apple-java specific?)
  'java/lang/ThreadLocal::<clinit>()V': (rs) -> #NOP
  'java/lang/ThreadLocal::<init>()V': (rs) -> #NOP
  'java/lang/Thread::<clinit>()V': (rs) -> #NOP
  'java/lang/Thread::getThreadGroup()Ljava/lang/ThreadGroup;': (rs) -> rs.push rs.set_obj({'type':'java/lang/ThreadGroup'}) # mock
  'java/lang/ThreadGroup::add(Ljava/lang/Thread;)V': (rs) -> #NOP (used in System init code, on mock objects)
  'java/lang/Terminator::setup()V': (rs) -> #NOP
  'java/util/concurrent/atomic/AtomicInteger::<clinit>()V': (rs) -> #NOP
  'java/util/concurrent/atomic/AtomicInteger::compareAndSet(II)Z': (rs) -> rs.push 1  # always true
  'sun/misc/Unsafe::getUnsafe()Lsun/misc/Unsafe;': ((rs) -> # avoid reflection
    rs.static_get({'class':'sun/misc/Unsafe','sig':{'name':'theUnsafe'}}))
  'java/util/concurrent/atomic/AtomicReferenceFieldUpdater::newUpdater(Ljava/lang/Class;Ljava/lang/Class;Ljava/lang/String;)Ljava/util/concurrent/atomic/AtomicReferenceFieldUpdater;': (rs) -> rs.push 0 # null
  'java/nio/charset/Charset$3::run()Ljava/lang/Object;': (rs) -> rs.push 0 # null
  'java/nio/Bits::byteOrder()Ljava/nio/ByteOrder;': (rs) -> rs.static_get {'class':'java/nio/ByteOrder','sig':{'name':'LITTLE_ENDIAN'}}
  'java/lang/Class::newInstance0()Ljava/lang/Object;': ((rs) -> #implemented here to avoid reflection
    classname = rs.get_obj(rs.curr_frame().locals[0]).name
    rs.push (oref = rs.init_object(classname))
    rs.method_lookup({'class':classname,'sig':{'name':'<init>'}}).run(rs)
    rs.push oref
    )
}
  
native_methods = {
  'java/lang/System::arraycopy(Ljava/lang/Object;ILjava/lang/Object;II)V': ((rs) -> 
    args = rs.curr_frame().locals
    src_array = rs.get_obj(args[0]).array
    src_pos = args[1]
    dest_array = rs.get_obj(args[2]).array
    dest_pos = args[3]
    length = args[4]
    j = dest_pos
    for i in [src_pos...src_pos+length]
      dest_array[j++] = src_array[i]<commit_msg>WOOOOOOO we can printgit diff! not the cleanest, but hey<commit_after>
# pull in external modules
_ ?= require '../third_party/underscore-min.js'
util ?= require './util'
opcodes ?= require './opcodes'
make_attributes ?= require './attributes'

# things assigned to root will be available outside this module
root = exports ? this.methods = {}

class AbstractMethodField
  """ Subclasses need to implement parse_descriptor(String) """
  constructor: (@class_name) ->

  parse: (bytes_array,constant_pool) ->
    @access_flags = util.parse_flags(util.read_uint(bytes_array.splice(0,2)))
    @name = constant_pool.get(util.read_uint(bytes_array.splice(0,2))).value
    @raw_descriptor = constant_pool.get(util.read_uint(bytes_array.splice(0,2))).value
    @parse_descriptor @raw_descriptor
    [@attrs,bytes_array] = make_attributes(bytes_array,constant_pool)
    return bytes_array
  
  parse_field_type: (char_array) ->
    c = char_array.shift()
    switch c
      when 'B' then { type: 'byte' }
      when 'C' then { type: 'char' }
      when 'D' then { type: 'double' }
      when 'F' then { type: 'float' }
      when 'I' then { type: 'int' }
      when 'J' then { type: 'long' }
      when 'L' then {
        type: 'reference'
        ref_type: 'class'
        referent: {
          type: 'class' # not technically a legal type
          class_name: (c while (c = char_array.shift()) != ';').join('')
        }
      }
      when 'S' then { type: 'short' }
      when 'Z' then { type: 'boolean' }
      when '[' then {
        type: 'reference'
        ref_type: 'array'
        referent: @parse_field_type char_array
      }
      else
        char_array.unshift(c)
        return null

class root.Field extends AbstractMethodField
  parse_descriptor: (raw_descriptor) ->
    @type = @parse_field_type raw_descriptor.split ''
    if @access_flags.static
      @static_value = null  # loaded in when getstatic is called

trapped_methods = {
  'java/lang/System::setJavaLangAccess()V': (rs) -> #NOP
  'java/lang/System::loadLibrary(Ljava/lang/String;)V': (rs) -> console.log "warning: library loads are NYI"
  'java/lang/System::adjustPropertiesForBackwardCompatibility(Ljava/util/Properties;)V': (rs) -> #NOP (apple-java specific?)
  'java/lang/ThreadLocal::<clinit>()V': (rs) -> #NOP
  'java/lang/ThreadLocal::<init>()V': (rs) -> #NOP
  'java/lang/Thread::<clinit>()V': (rs) -> #NOP
  'java/lang/Thread::getThreadGroup()Ljava/lang/ThreadGroup;': (rs) -> rs.push rs.set_obj({'type':'java/lang/ThreadGroup'}) # mock
  'java/lang/ThreadGroup::add(Ljava/lang/Thread;)V': (rs) -> #NOP (used in System init code, on mock objects)
  'java/lang/Terminator::setup()V': (rs) -> #NOP
  'java/util/concurrent/atomic/AtomicInteger::<clinit>()V': (rs) -> #NOP
  'java/util/concurrent/atomic/AtomicInteger::compareAndSet(II)Z': (rs) -> rs.push 1  # always true
  'sun/misc/Unsafe::getUnsafe()Lsun/misc/Unsafe;': ((rs) -> # avoid reflection
    rs.static_get({'class':'sun/misc/Unsafe','sig':{'name':'theUnsafe'}}))
  'java/util/concurrent/atomic/AtomicReferenceFieldUpdater::newUpdater(Ljava/lang/Class;Ljava/lang/Class;Ljava/lang/String;)Ljava/util/concurrent/atomic/AtomicReferenceFieldUpdater;': (rs) -> rs.push 0 # null
  'java/nio/charset/Charset$3::run()Ljava/lang/Object;': (rs) -> rs.push 0 # null
  'java/nio/Bits::byteOrder()Ljava/nio/ByteOrder;': (rs) -> rs.static_get {'class':'java/nio/ByteOrder','sig':{'name':'LITTLE_ENDIAN'}}
  'java/lang/Class::newInstance0()Ljava/lang/Object;': ((rs) -> #implemented here to avoid reflection
    classname = rs.get_obj(rs.curr_frame().locals[0]).name
    rs.push (oref = rs.init_object(classname))
    rs.method_lookup({'class':classname,'sig':{'name':'<init>'}}).run(rs)
    rs.push oref
    )
  'java/io/PrintStream::write(Ljava/lang/String;)V': ((rs) ->
    args = rs.curr_frame().locals
    str = rs.jvm2js_str(rs.get_obj(args[1]))
    rs.static_get {'class':'java/lang/System','sig':{'name':'out'}}; sysout = rs.pop()
    rs.static_get {'class':'java/lang/System','sig':{'name':'err'}}; syserr = rs.pop()
    if args[0] is sysout
      rs.print str
    else if args[0] is syserr
      rs.print str
    else
      throw "You tried to write to a PrintStream that wasn't System.out or System.err! For shame!"
    )
}
  
native_methods = {
  'java/lang/System::arraycopy(Ljava/lang/Object;ILjava/lang/Object;II)V': ((rs) -> 
    args = rs.curr_frame().locals
    src_array = rs.get_obj(args[0]).array
    src_pos = args[1]
    dest_array = rs.get_obj(args[2]).array
    dest_pos = args[3]
    length = args[4]
    j = dest_pos
    for i in [src_pos...src_pos+length]
      dest_array[j++] = src_array[i]
{'fraction_non_alphanumeric': 0.17218467178370947, 'fraction_numerical': 0.0043533050750372325, 'mean_word_length': 3.801980198019802, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, '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': '7176988', 'n_tokens_mistral': 3084, 'n_tokens_neox': 3064, 'n_words': 575}