Skip to content

Commit

Permalink
Add bin/opal-repl for simple repl
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Sep 26, 2013
1 parent 6969618 commit d885f24
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Gemfile
@@ -1,2 +1,6 @@
source 'https://rubygems.org'
gemspec

group :repl do
gem "therubyracer", :require => 'v8'
end
63 changes: 63 additions & 0 deletions bin/opal-repl
@@ -0,0 +1,63 @@
#!/usr/bin/env ruby

require 'opal'

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

def run
return if @v8

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

@v8 = V8::Context.new
@v8['console'] = self
@v8.eval @builder.build 'opal'

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"
break
end

puts "=> #{eval_ruby line}"
end

finish
end

def eval_ruby(str)
code = @builder.build_str str, :irb => true
@v8.eval "var $_result = #{code} $_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
2 changes: 1 addition & 1 deletion opal.gemspec
Expand Up @@ -13,7 +13,7 @@ Gem::Specification.new do |s|
s.description = 'Ruby runtime and core library for javascript.'

s.files = `git ls-files`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
s.executables = ['opal']
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.require_paths = ['lib']

Expand Down

0 comments on commit d885f24

Please sign in to comment.