diff --git a/lib/execjs.rb b/lib/execjs.rb index 688f04b..f41e0e2 100644 --- a/lib/execjs.rb +++ b/lib/execjs.rb @@ -15,6 +15,6 @@ module ExecJS end def self.runtime - @runtime ||= Runtimes::Node.new + @runtime ||= Runtimes.runtime end end diff --git a/lib/execjs/runtimes.rb b/lib/execjs/runtimes.rb index aaa1ecc..30aa486 100644 --- a/lib/execjs/runtimes.rb +++ b/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 diff --git a/lib/execjs/runtimes/v8.js b/lib/execjs/runtimes/v8.js new file mode 100644 index 0000000..f7fbedb --- /dev/null +++ b/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])); + } +}); diff --git a/lib/execjs/runtimes/v8.rb b/lib/execjs/runtimes/v8.rb new file mode 100644 index 0000000..aec0030 --- /dev/null +++ b/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 diff --git a/readme.md b/readme.md index 4bffb5e..01f6154 100644 --- a/readme.md +++ b/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/) diff --git a/test/test_node_runtime.rb b/test/test_runtime.rb similarity index 87% rename from test/test_node_runtime.rb rename to test/test_runtime.rb index e4fbd4e..ef028e6 100644 --- a/test/test_node_runtime.rb +++ b/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 @@ class TestNodeRuntime < Test::Unit::TestCase 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