Skip to content
Browse files

Add V8 runtime

  • Loading branch information...
1 parent 0be476d commit 9a5015e612280c28421ddfb2b5513846ca64cb0e @sstephenson committed Feb 6, 2011
Showing with 55 additions and 7 deletions.
  1. +1 −1 lib/execjs.rb
  2. +5 −0 lib/execjs/runtimes.rb
  3. +18 −0 lib/execjs/runtimes/v8.js
  4. +13 −0 lib/execjs/runtimes/v8.rb
  5. +1 −1 readme.md
  6. +17 −5 test/{test_node_runtime.rb → test_runtime.rb}
View
2 lib/execjs.rb
@@ -15,6 +15,6 @@ def self.eval(source)
end
def self.runtime
- @runtime ||= Runtimes::Node.new
+ @runtime ||= Runtimes.runtime
end
end
View
5 lib/execjs/runtimes.rb
@@ -1,5 +1,10 @@
module ExecJS
module Runtimes
autoload :Node, "execjs/runtimes/node"
+ autoload :V8, "execjs/runtimes/v8"
+
+ def self.runtime
+ V8.new
+ end
end
end
View
18 lib/execjs/runtimes/v8.js
@@ -0,0 +1,18 @@
+(function(program, execJS) { execJS(program) })(function() { #{source}
+}, function(program) {
+ var output;
+ try {
+ result = program();
+ if (typeof result == 'undefined' && result !== null) {
+ print('["ok"]');
+ } else {
+ try {
+ print(JSON.stringify(['ok', result]));
+ } catch (err) {
+ print('["err"]');
+ }
+ }
+ } catch (err) {
+ print(JSON.stringify(['err', '' + err]));
+ }
+});
View
13 lib/execjs/runtimes/v8.rb
@@ -0,0 +1,13 @@
+module ExecJS
+ module Runtimes
+ class V8 < Runtime
+ def command(filename)
+ "v8 #{filename}"
+ end
+
+ def runner_path
+ File.expand_path('../v8.js', __FILE__)
+ end
+ end
+ end
+end
View
2 readme.md
@@ -9,8 +9,8 @@ ExecJS supports these runtimes:
* [therubyracer](https://github.com/cowboyd/therubyracer) - Google V8
embedded within Ruby for exceptional performance
-* [Node.js](http://nodejs.org/)
* [Google V8](http://code.google.com/p/v8/)
+* [Node.js](http://nodejs.org/)
* Apple JavaScriptCore
* [Mozilla Spidermonkey](http://www.mozilla.org/js/spidermonkey/)
* [Mozilla Rhino](http://www.mozilla.org/rhino/)
View
22 test/test_node_runtime.rb → test/test_runtime.rb
@@ -1,11 +1,7 @@
require "execjs"
require "test/unit"
-class TestNodeRuntime < Test::Unit::TestCase
- def setup
- @runtime = ExecJS::Runtimes::Node.new
- end
-
+module TestRuntime
def test_exec
assert_nil @runtime.exec("1")
assert_nil @runtime.exec("return")
@@ -42,3 +38,19 @@ def test_thrown_exception
end
end
end
+
+class TestNodeRuntime < Test::Unit::TestCase
+ include TestRuntime
+
+ def setup
+ @runtime = ExecJS::Runtimes::Node.new
+ end
+end
+
+class TestV8Runtime < Test::Unit::TestCase
+ include TestRuntime
+
+ def setup
+ @runtime = ExecJS::Runtimes::V8.new
+ end
+end

0 comments on commit 9a5015e

Please sign in to comment.
Something went wrong with that request. Please try again.