<commit_before>
# pull in external modules
_ ?= require '../third_party/underscore-min.js'
runtime ?= require './runtime'
util ?= require './util'
{log,debug,error,ext_classname} = util

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

show_state = (rs) ->
  cf = rs.curr_frame()
  debug "stack: [#{cf.stack}], local: [#{cf.locals}], heap:"
  i = Math.max(1,rs.heap.length-30)  # because the heap can get huge
  debug " ...omitted heap entries..." if i > 1
  while i < rs.heap.length
    obj = rs.heap[i]
    if obj.type is '[char' and rs.heap[i+1] and rs.heap[i+1].type is 'java/lang/String'
      debug " #{i},#{i+1}: String \"#{rs.jvm2js_str(rs.heap[i+1])}\""
      ++i
    else if obj.type is 'java/lang/String'
      try
        debug " #{i}: String \"#{rs.jvm2js_str(obj)}\""
      catch err
        debug " #{i}: String (null value)"
    else if obj.type[0] is '['
      debug " #{i}: #{obj.type.slice(1)}[#{obj.array.length}]"
    else
      debug " #{i}: #{rs.heap[i].type}"
    ++i

show_stacktrace = (rs,e) ->
  e_type = rs.get_obj(e.exception.fields.cause).type
  detail_ref = e.exception.fields.detailMessage
  detail = if detail_ref then rs.jvm2js_str rs.get_obj detail_ref else ''
  console.error "Exception in thread \"main\" #{ext_classname e_type}: #{detail}"
  for i in [e.stack.length-1..0] by -1
    entry = e.stack[i]
    console.error "\tat #{entry.cls}.#{entry.method}(#{entry.file}:#{entry.line}, code #{entry.op})"

# main function that gets called from the frontend
root.run = (class_data, print_func, load_func, cmdline_args) ->
  rs = new runtime.RuntimeState(class_data, print_func, load_func, cmdline_args)
  main_spec = {'class': class_data.this_class, 'sig': {'name': 'main'}}
  try
    rs.method_lookup(main_spec).run(rs)
  catch e
    if e instanceof util.JavaException
      console.error "\nUncaught Java Exception"
      show_state(rs)
      show_stacktrace(rs,e)
    else
      console.error "\nInternal JVM Error!"
      show_state(rs)
      console.error e.stack or e<commit_msg>better exception printing<commit_after>
# pull in external modules
_ ?= require '../third_party/underscore-min.js'
runtime ?= require './runtime'
util ?= require './util'
{log,debug,error,ext_classname} = util

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

show_state = (rs) ->
  cf = rs.curr_frame()
  debug "stack: [#{cf.stack}], local: [#{cf.locals}], heap:"
  i = Math.max(1,rs.heap.length-30)  # because the heap can get huge
  debug " ...omitted heap entries..." if i > 1
  while i < rs.heap.length
    obj = rs.heap[i]
    if obj.type is '[char' and rs.heap[i+1] and rs.heap[i+1].type is 'java/lang/String'
      debug " #{i},#{i+1}: String \"#{rs.jvm2js_str(rs.heap[i+1])}\""
      ++i
    else if obj.type is 'java/lang/String'
      try
        debug " #{i}: String \"#{rs.jvm2js_str(obj)}\""
      catch err
        debug " #{i}: String (null value)"
    else if obj.type[0] is '['
      debug " #{i}: #{obj.type.slice(1)}[#{obj.array.length}]"
    else
      debug " #{i}: #{rs.heap[i].type}"
    ++i

show_stacktrace = (rs,e) ->
  e_type = rs.get_obj(e.exception.fields.cause).type
  detail_ref = e.exception.fields.detailMessage
  if detail_ref
    detail = rs.jvm2js_str rs.get_obj detail_ref
    console.error "Exception in thread \"main\" #{detail}"
  else
    console.error "Exception in thread \"main\" #{ext_classname e_type}"
  for i in [e.stack.length-1..0] by -1
    entry = e.stack[i]
    console.error "\tat #{entry.cls}.#{entry.method}(#{entry.file}:#{entry.line}, code #{entry.op})"

# main function that gets called from the frontend
root.run = (class_data, print_func, load_func, cmdline_args) ->
  rs = new runtime.RuntimeState(class_data, print_func, load_func, cmdline_args)
  main_spec = {'class': class_data.this_class, 'sig': {'name': 'main'}}
  try
    rs.method_lookup(main_spec).run(rs)
  catch e
    if e instanceof util.JavaException
      console.error "\nUncaught Java Exception"
      show_state(rs)
      show_stacktrace(rs,e)
    else
      console.error "\nInternal JVM Error!"
      show_state(rs)
      console.error e.stack or e
{'fraction_non_alphanumeric': 0.16499761564139245, 'fraction_numerical': 0.007629947544110634, 'mean_word_length': 3.2675483214649033, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '4067852', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1494, 'n_words': 377}