Sha256: d2ec187a8c315cbbd6974b7fc536fb4e5c53cba47ae871ea1ed1b1508fd837d4

Contents?: true

Size: 1.46 KB

Versions: 16

Compression:

Stored size: 1.46 KB

Contents

#!/usr/bin/env ruby

require 'opal'

module Opal
  class V8Context
    def initialize
      @builder = Opal::Builder.new
    end

    def run(filename = nil)
      return if @v8

      begin
        require 'v8'
      rescue LoadError
        abort 'therubyracer must be installed'
      end

      @v8 = V8::Context.new
      @v8['console'] = self
      @v8.eval Opal::Builder.new.build('opal').to_s

      if filename
        if File.exist? filename
          eval_ruby File.read(filename)
        else
          fail "file does not exist: #{filename}"
        end
      end

      run_repl
    end

    def log(*str)
      puts(*str)
    end

    def run_repl
      require 'readline'

      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" or line.nil?
          break
        end

        puts "=> #{eval_ruby line}"
      end

      finish
    end

    def eval_ruby(str)
      code = Opal::Builder.new.build_str(str, '(irb)', :irb => true, :const_missing => true)
      code.processed[0...-1].each{ |c| @v8.eval(c.to_s) }
      @v8.eval "var $_result = #{code.processed.last.to_s} ($_result == null ? 'nil' : $_result.$inspect());"
    rescue => e
      puts "#{e.message}\n\t#{e.backtrace.join("\n\t")}"
    end

    def finish
      @v8 = nil
    end
  end
end

Opal::V8Context.new.run ARGV.first

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
opal-0.8.1 bin/opal-repl
opal-0.8.1.rc1 bin/opal-repl
opal-0.8.0 bin/opal-repl
opal-0.8.0.rc3 bin/opal-repl
opal-0.8.0.rc2 bin/opal-repl
opal-0.8.0.rc1 bin/opal-repl
opal-0.8.0.beta1 bin/opal-repl
opal-0.7.2 bin/opal-repl
opal-0.7.1 bin/opal-repl
opal-0.7.0 bin/opal-repl
opal-0.7.0.rc1 bin/opal-repl
opal-0.7.0.beta3 bin/opal-repl
opal-0.7.0.beta2 bin/opal-repl
opal-cj-0.7.0.beta2 bin/opal-repl
opal-cj-0.7.0.beta1 bin/opal-repl
opal-0.7.0.beta1 bin/opal-repl