Skip to content

Commit

Permalink
pulling out bytestring parsing, fixing util require bug
Browse files Browse the repository at this point in the history
  • Loading branch information
perimosocordiae committed Mar 5, 2012
1 parent 995bd43 commit fa1d7fb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
8 changes: 4 additions & 4 deletions browser/frontend.coffee
Expand Up @@ -9,21 +9,21 @@ html_escape = (str) ->

# Read in a binary classfile synchronously. Return an array of bytes.
read_classfile = (cls) ->
rv = ''
rv = []
$.ajax "http://localhost:8000/third_party/#{cls}.class", {
type: 'GET'
dataType: 'text'
async: false
beforeSend: (jqXHR) -> jqXHR.overrideMimeType('text/plain; charset=x-user-defined')
success: (data) -> rv = data
success: (data) -> rv = util.bytestr_to_array data
error: (jqXHR, textStatus, errorThrown) ->
throw "AJAX error when loading class #{cls}: #{errorThrown}"
}
(rv.charCodeAt(i) & 0xff for i in [0...rv.length])
rv

process_bytecode = (bytecode_string) ->
$('#go_button').text('Parsing...')
bytes_array = (bytecode_string.charCodeAt(i) & 0xff for i in [0...bytecode_string.length])
bytes_array = util.bytestr_to_array bytecode_string
class_data = new ClassFile(bytes_array)
$('#disassembly').html html_escape(disassemble(class_data))
$('#run_button').removeAttr('disabled')
Expand Down
4 changes: 2 additions & 2 deletions console/disassembler.coffee
@@ -1,9 +1,9 @@
fs = require 'fs'
util = require '../src/util'
disassemble = require '../src/disassembler'
ClassFile = require '../src/class_file'

bytecode_string = fs.readFileSync '/dev/stdin', 'binary'
bytes_array = (bytecode_string.charCodeAt(i) for i in [0...bytecode_string.length])
bytes_array = util.bytestr_to_array fs.readFileSync('/dev/stdin', 'binary')
class_data = new ClassFile bytes_array

console.log disassemble class_data
4 changes: 2 additions & 2 deletions console/runner.coffee
@@ -1,10 +1,10 @@
fs = require 'fs'
jvm = require '../src/jvm'
util = require '../src/util'
ClassFile = require '../src/class_file'

read_binary_file = (filename) ->
bytecode_string = fs.readFileSync filename, 'binary'
(bytecode_string.charCodeAt(i) for i in [0...bytecode_string.length])
util.bytestr_to_array fs.readFileSync(filename, 'binary')

read_classfile = (cls) -> read_binary_file "third_party/#{cls}.class"

Expand Down
5 changes: 4 additions & 1 deletion src/util.coffee
@@ -1,6 +1,6 @@

# pull in external modules
_ ?= require './third_party/underscore-min.js'
_ ?= require '../third_party/underscore-min.js'

# things assigned to root will be available outside this module
root = exports ? this.util = {}
Expand Down Expand Up @@ -34,6 +34,9 @@ root.read_uint = (bytes) ->
# sum up the byte values shifted left to the right alignment.
root.sum(root.lshift(bytes[i],8*(n-i)) for i in [0..n])

root.bytestr_to_array = (bytecode_string) ->
(bytecode_string.charCodeAt(i) & 0xFF for i in [0...bytecode_string.length])

root.parse_flags = (flag_byte) ->
{
public: flag_byte & 0x1
Expand Down

0 comments on commit fa1d7fb

Please sign in to comment.