class Opal::Context

def argv=(args)

Set ARGV for the context
def argv=(args)
  puts "setting argv to #{args.inspect}"
  eval "opal.runtime.cs(opal.runtime.Object, 'ARGV', #{args.inspect});"
end

def eval(code, file = nil)

def eval(code, file = nil)
  @v8.eval code, file
end

def eval_ruby(content, line = "")

def eval_ruby(content, line = "")
  begin
    code = Opal::Parser.new(content).parse!.generate_top
    code = "opal.run(function() {var $rb = opal.runtime, self = $rb.top, __FILE__ = '(opal)';" + code + "});"
    # puts code
    @v8['$opal_irb_result'] = eval code, line
    eval "!($opal_irb_result == null || !$opal_irb_result.m$inspect) ? $opal_irb_result.m$inspect() : '(Object does not support #inspect)'"
  rescue => e
    puts e
    puts("\t" + e.backtrace.join("\n\t"))
  end
end

def finish

#setup_v8
then the v8 context will de removed. It can be reset by calling
the opal runtime to do it's at_exit() calls (if applicable) and
Finishes the context, i.e. tidy everything up. This will cause
def finish
  return unless @v8
  eval "opal.runtime.do_at_exit()", "(opal)"
  @v8 = nil
end

def initialize(root_dir = Dir.getwd)

def initialize(root_dir = Dir.getwd)
  @root_dir = root_dir
  @builder = Opal::Builder.new
  @load_paths = resolve_load_paths
end

def require_file(path)

passes the require through to the underlying context.
Require the given id as if it was required in the context. This simply
def require_file(path)
  setup_v8
  eval "opal.run(function() {opal.require('#{path}');});", path
  finish
end

def resolve_load_paths

Looks through vendor/ directory and adds all relevant load paths
def resolve_load_paths
  Dir['vendor/*/package.yml'].map do |package|
    File.expand_path File.join(File.dirname(package), 'lib')
  end
end

def setup_v8

default "browser" loader cannot access files from disk.
replace the loader etc with our custom loader for a Ruby environment. The
Setup the context. This basically loads opal.js into our context, and
def setup_v8
  return if @v8
  begin
    require 'v8'
  rescue LoadError => e
    abort "therubyracer is required for running javascript. Install it with `gem install therubyracer`"
  end
  @v8 = V8::Context.new
  @v8['console'] = Console.new
  eval @builder.build_core, '(opal)'
  opal = @v8['opal']
  opal['fs'] = FileSystem.new self
  # FIXME: we cant use a ruby array as a js array :(
  opal['loader'] = Loader.new self, eval("[]")
  @load_paths.each do |path|
    eval "opal.loader.paths.push('#{path}')"
  end
end

def start_repl

Start normal js repl
def start_repl
  require 'readline'
  setup_v8
  loop do
    # on SIGINT lets just return from the loop..
    trap("SIGINT") { finish; return }
    line = Readline.readline '>> ', true
    # if we type exit, then we need to close down context
    if line == "exit"
      break
    end
    puts "=> #{eval_ruby line, '(opal)'}"
  end
  finish
end