This repository has been archived by the owner on Jun 10, 2018. It is now read-only.
forked from rails/execjs
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add therubyracer V8 runtime implementation
- Loading branch information
1 parent
5d0d51b
commit 957d632
Showing
6 changed files
with
59 additions
and
6 deletions.
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
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
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
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,48 @@ | ||
module ExecJS | ||
class V8Runtime | ||
def initialize(options) | ||
end | ||
|
||
def exec(source) | ||
if /\S/ =~ source | ||
eval "(function(){#{source}})()" | ||
end | ||
end | ||
|
||
def eval(source) | ||
if /\S/ =~ source | ||
context = ::V8::Context.new | ||
unbox context.eval("(#{source})") | ||
end | ||
rescue ::V8::JSError => e | ||
if e.value["name"] == "SyntaxError" | ||
raise RuntimeError, e | ||
else | ||
raise ProgramError, e | ||
end | ||
end | ||
|
||
def available? | ||
require "v8" | ||
true | ||
rescue LoadError | ||
false | ||
end | ||
|
||
def unbox(value) | ||
case value | ||
when ::V8::Function | ||
nil | ||
when ::V8::Array | ||
value.map { |v| unbox(v) } | ||
when ::V8::Object | ||
value.inject({}) do |vs, (k, v)| | ||
vs[k] = unbox(v) unless v.is_a?(::V8::Function) | ||
vs | ||
end | ||
else | ||
value | ||
end | ||
end | ||
end | ||
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
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