From 99bdbb80673b0d800368370ebd215f93dfb50f49 Mon Sep 17 00:00:00 2001 From: ojab Date: Tue, 8 Sep 2015 20:39:06 +0000 Subject: [PATCH] Add direct V8 support (via `d3`) --- .travis.yml | 5 +++++ README.md | 1 + lib/execjs/runtimes.rb | 10 +++++++++- lib/execjs/support/v8_runner.js | 18 ++++++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 lib/execjs/support/v8_runner.js diff --git a/.travis.yml b/.travis.yml index 65df6c1..1d035cc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,9 @@ language: ruby cache: bundler sudo: false +before_install: + - if [ "$EXECJS_RUNTIME" == "V8" ]; then brew update; fi + - if [ "$EXECJS_RUNTIME" == "V8" ]; then brew install v8; fi script: bundle exec ruby test/test_execjs.rb matrix: @@ -40,3 +43,5 @@ matrix: env: EXECJS_RUNTIME=Duktape - os: osx env: EXECJS_RUNTIME=RubyRacer + - os: osx + env: EXECJS_RUNTIME=V8 diff --git a/README.md b/README.md index ccef33e..fb6a2db 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ ExecJS supports these runtimes: * [Node.js](http://nodejs.org/) * Apple JavaScriptCore - Included with Mac OS X * [Microsoft Windows Script Host](http://msdn.microsoft.com/en-us/library/9bbdkx3k.aspx) (JScript) +* [Google V8](http://code.google.com/p/v8/) A short example: diff --git a/lib/execjs/runtimes.rb b/lib/execjs/runtimes.rb index 039babb..d84e02e 100644 --- a/lib/execjs/runtimes.rb +++ b/lib/execjs/runtimes.rb @@ -42,6 +42,13 @@ module Runtimes encoding: 'UTF-16LE' # CScript with //U returns UTF-16LE ) + V8 = ExternalRuntime.new( + name: "V8", + command: "d8", + runner_path: ExecJS.root + "/support/v8_runner.js", + encoding: 'UTF-8' + ) + def self.autodetect from_environment || best_available || @@ -75,7 +82,8 @@ def self.runtimes Node, JavaScriptCore, SpiderMonkey, - JScript + JScript, + V8 ] end end diff --git a/lib/execjs/support/v8_runner.js b/lib/execjs/support/v8_runner.js new file mode 100644 index 0000000..c57a944 --- /dev/null +++ b/lib/execjs/support/v8_runner.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(JSON.stringify(['err', '' + err, err.stack])); + } + } + } catch (err) { + print(JSON.stringify(['err', '' + err, err.stack])); + } +});