 "use strict"
 root = this
-
-# /home/doppio/vendor/classes/java/lang/Runtime.class - Causing magic number error as byte array is []
 
 root.init_editor = ->
     root.editor = ace.edit('source')
     JavaMode = require("ace/mode/java").Mode
     root.editor.getSession().setMode(new JavaMode())
-    root.editor.getSession().setValue ("public class Student {\n  public static void main(String[]args) {\n    System.out.println(\"Hello World\");\n  }\n}")
+    root.editor.getSession().setValue ('classes.doppio.JavaScript.eval("console.log(\\\"HELLO WORLD\\\");");')
     return
 
 root.preload = ->
     root.doppioAPI = new DoppioApi()
     root.init_editor()
 
-    # root.bs_cl = new ClassLoader.BootstrapClassLoader(read_classfile)
-
     $('#run_btn').click (e) ->
-        root.doppioAPI.compileAndRun root.editor.getValue()
+        root.doppioAPI.run root.editor.getValue()
         e.preventDefault()
         return
     $('#abort_btn').click (e) ->
-        console.log 'abort button clicked'
-        if doppioAPI.rs
-            $('#messages').text 'Stopping ...'
-            cb =-> $('#messages').text 'Stopped'
-            doppioAPI.rs.async_abort(cb)
-        else
-            console.log 'but nothing to do'
+        root.doppioAPI.abort()
         e.preventDefault()
         return
     return
 
 $(document).ready ->
     root.preload()
+
+### TEST CODE ###
+# classes.doppio.JavaScript.eval("console.log(\"HELLO WORLD\");");
 
 class window.DoppioApi
     ###

    ###
    constructor: () ->
        @load_mini_rt()
        return

    # Read in a binary classfile synchronously. Return an array of bytes.

        end_untar = (new Date()).getTime()
        console.log "Untarring took a total of #{end_untar-start_untar}ms."

    run: (studentCode) ->
        start_time = (new Date()).getTime()
        fname = 'program.bsh'
        @saveFile fname, studentCode
        stdout = (str) ->
            console.log str
            return str
        stdin = -> "\n"
        class_args = [fname]
        finish_cb = =>
            end_time = (new Date()).getTime()
            console.log 'Finished Run'
            console.log "Took #{end_time - start_time}ms."
            @rs = null
            return

        @rs = new runtime.RuntimeState(stdout, stdin, @bs_cl)
        jvm.run_class(@rs, 'bsh/Interpreter', class_args, finish_cb)
        return

    abort: =>
        console.log 'User Abort Requested'
        if @rs
            console.log 'Aborting Run'
            cb = =>
                console.log 'Aborted Successfully'
                @rs = null
            @rs.async_abort(cb)
        else
            console.log 'No Run Detected'
        return

    saveFile: (fname, contents) ->
        contents += '\n' unless contents[contents.length-1] == '\n'
        node.fs.writeFileSync(fname, contents)
        console.log("File saved as '#{fname}'.")
        return