Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6969618
commit d885f24
Showing
3 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
source 'https://rubygems.org' | ||
gemspec | ||
|
||
group :repl do | ||
gem "therubyracer", :require => 'v8' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters