execjs 1.1.3 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -2
- data/lib/execjs/johnson_runtime.rb +112 -0
- data/lib/execjs/module.rb +1 -2
- data/lib/execjs/runtimes.rb +9 -5
- data/lib/execjs/version.rb +3 -0
- metadata +65 -7
data/README.md
CHANGED
@@ -8,14 +8,16 @@ returns the result to you as a Ruby object.
|
|
8
8
|
ExecJS supports these runtimes:
|
9
9
|
|
10
10
|
* [therubyracer](https://github.com/cowboyd/therubyracer) - Google V8
|
11
|
-
embedded within
|
11
|
+
embedded within Ruby
|
12
12
|
* [therubyrhino](https://github.com/cowboyd/therubyrhino) - Mozilla
|
13
13
|
Rhino embedded within JRuby
|
14
|
+
* [Johnson](https://github.com/jbarnette/johnson) - Mozilla
|
15
|
+
SpiderMonkey embedded within Ruby
|
14
16
|
* [Mustang](https://github.com/nu7hatch/mustang) - Mustang V8
|
15
17
|
embedded within Ruby
|
16
18
|
* [Node.js](http://nodejs.org/)
|
17
19
|
* Apple JavaScriptCore - Included with Mac OS X
|
18
|
-
* [Mozilla
|
20
|
+
* [Mozilla SpiderMonkey](http://www.mozilla.org/js/spidermonkey/)
|
19
21
|
* [Microsoft Windows Script Host](http://msdn.microsoft.com/en-us/library/9bbdkx3k.aspx) (JScript)
|
20
22
|
|
21
23
|
A short example:
|
@@ -0,0 +1,112 @@
|
|
1
|
+
module ExecJS
|
2
|
+
class JohnsonRuntime
|
3
|
+
class Context
|
4
|
+
def initialize(source = "")
|
5
|
+
@runtime = Johnson::Runtime.new
|
6
|
+
@runtime.evaluate(source)
|
7
|
+
end
|
8
|
+
|
9
|
+
def exec(source, options = {})
|
10
|
+
souce = source.encode('UTF-8') if source.respond_to?(:encode)
|
11
|
+
|
12
|
+
if /\S/ =~ source
|
13
|
+
eval "(function(){#{source}})()", options
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def eval(source, options = {})
|
18
|
+
souce = source.encode('UTF-8') if source.respond_to?(:encode)
|
19
|
+
|
20
|
+
if /\S/ =~ source
|
21
|
+
unbox @runtime.evaluate("(#{source})")
|
22
|
+
end
|
23
|
+
rescue Johnson::Error => e
|
24
|
+
if syntax_error?(e)
|
25
|
+
raise RuntimeError, e.message
|
26
|
+
else
|
27
|
+
raise ProgramError, e.message
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def call(properties, *args)
|
32
|
+
unbox @runtime.evaluate(properties).call(*args)
|
33
|
+
rescue Johnson::Error => e
|
34
|
+
if syntax_error?(e)
|
35
|
+
raise RuntimeError, e.message
|
36
|
+
else
|
37
|
+
raise ProgramError, e.message
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def unbox(value)
|
42
|
+
case
|
43
|
+
when function?(value)
|
44
|
+
nil
|
45
|
+
when string?(value)
|
46
|
+
value.respond_to?(:force_encoding) ?
|
47
|
+
value.force_encoding('UTF-8') :
|
48
|
+
value
|
49
|
+
when array?(value)
|
50
|
+
value.map { |v| unbox(v) }
|
51
|
+
when object?(value)
|
52
|
+
value.inject({}) do |vs, (k, v)|
|
53
|
+
vs[k] = unbox(v) unless function?(v)
|
54
|
+
vs
|
55
|
+
end
|
56
|
+
else
|
57
|
+
value
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
def syntax_error?(error)
|
63
|
+
error.message =~ /^syntax error at /
|
64
|
+
end
|
65
|
+
|
66
|
+
def function?(value)
|
67
|
+
value.respond_to?(:function?) && value.function?
|
68
|
+
end
|
69
|
+
|
70
|
+
def string?(value)
|
71
|
+
value.is_a?(String)
|
72
|
+
end
|
73
|
+
|
74
|
+
def array?(value)
|
75
|
+
array_test.call(value)
|
76
|
+
end
|
77
|
+
|
78
|
+
def object?(value)
|
79
|
+
value.respond_to?(:inject)
|
80
|
+
end
|
81
|
+
|
82
|
+
def array_test
|
83
|
+
@array_test ||= @runtime.evaluate("(function(a) {return a instanceof [].constructor})")
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def name
|
88
|
+
"Johnson (SpiderMonkey)"
|
89
|
+
end
|
90
|
+
|
91
|
+
def exec(source)
|
92
|
+
context = Context.new
|
93
|
+
context.exec(source)
|
94
|
+
end
|
95
|
+
|
96
|
+
def eval(source)
|
97
|
+
context = Context.new
|
98
|
+
context.eval(source)
|
99
|
+
end
|
100
|
+
|
101
|
+
def compile(source)
|
102
|
+
Context.new(source)
|
103
|
+
end
|
104
|
+
|
105
|
+
def available?
|
106
|
+
require "johnson"
|
107
|
+
true
|
108
|
+
rescue LoadError
|
109
|
+
false
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
data/lib/execjs/module.rb
CHANGED
data/lib/execjs/runtimes.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "execjs/module"
|
2
2
|
require "execjs/external_runtime"
|
3
|
+
require "execjs/johnson_runtime"
|
3
4
|
require "execjs/mustang_runtime"
|
4
5
|
require "execjs/ruby_racer_runtime"
|
5
6
|
require "execjs/ruby_rhino_runtime"
|
@@ -10,6 +11,8 @@ module ExecJS
|
|
10
11
|
|
11
12
|
RubyRhino = RubyRhinoRuntime.new
|
12
13
|
|
14
|
+
Johnson = JohnsonRuntime.new
|
15
|
+
|
13
16
|
Mustang = MustangRuntime.new
|
14
17
|
|
15
18
|
Node = ExternalRuntime.new(
|
@@ -25,8 +28,8 @@ module ExecJS
|
|
25
28
|
:conversion => { :from => "ISO8859-1", :to => "UTF-8" }
|
26
29
|
)
|
27
30
|
|
28
|
-
Spidermonkey = ExternalRuntime.new(
|
29
|
-
:name => "
|
31
|
+
SpiderMonkey = Spidermonkey = ExternalRuntime.new(
|
32
|
+
:name => "SpiderMonkey",
|
30
33
|
:command => "js",
|
31
34
|
:runner_path => ExecJS.root + "/support/basic_runner.js"
|
32
35
|
)
|
@@ -54,7 +57,7 @@ module ExecJS
|
|
54
57
|
if runtime.available?
|
55
58
|
runtime if runtime.available?
|
56
59
|
else
|
57
|
-
raise RuntimeUnavailable, "#{name} runtime is not available on this system"
|
60
|
+
raise RuntimeUnavailable, "#{runtime.name} runtime is not available on this system"
|
58
61
|
end
|
59
62
|
elsif !name.empty?
|
60
63
|
raise RuntimeUnavailable, "#{name} runtime is not defined"
|
@@ -63,17 +66,18 @@ module ExecJS
|
|
63
66
|
end
|
64
67
|
|
65
68
|
def self.names
|
66
|
-
constants
|
69
|
+
@names ||= constants.inject({}) { |h, name| h.merge(const_get(name) => name) }.values
|
67
70
|
end
|
68
71
|
|
69
72
|
def self.runtimes
|
70
73
|
@runtimes ||= [
|
71
74
|
RubyRacer,
|
72
75
|
RubyRhino,
|
76
|
+
Johnson,
|
73
77
|
Mustang,
|
74
78
|
Node,
|
75
79
|
JavaScriptCore,
|
76
|
-
|
80
|
+
SpiderMonkey,
|
77
81
|
JScript
|
78
82
|
]
|
79
83
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: execjs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 1.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 1.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Sam Stephenson
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-06-
|
19
|
+
date: 2011-06-18 00:00:00 -05:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -35,7 +35,7 @@ dependencies:
|
|
35
35
|
type: :runtime
|
36
36
|
version_requirements: *id001
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
|
-
name:
|
38
|
+
name: johnson
|
39
39
|
prerelease: false
|
40
40
|
requirement: &id002 !ruby/object:Gem::Requirement
|
41
41
|
none: false
|
@@ -48,6 +48,62 @@ dependencies:
|
|
48
48
|
version: "0"
|
49
49
|
type: :development
|
50
50
|
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: mustang
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
type: :development
|
64
|
+
version_requirements: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: rake
|
67
|
+
prerelease: false
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 3
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
type: :development
|
78
|
+
version_requirements: *id004
|
79
|
+
- !ruby/object:Gem::Dependency
|
80
|
+
name: therubyracer
|
81
|
+
prerelease: false
|
82
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
hash: 3
|
88
|
+
segments:
|
89
|
+
- 0
|
90
|
+
version: "0"
|
91
|
+
type: :development
|
92
|
+
version_requirements: *id005
|
93
|
+
- !ruby/object:Gem::Dependency
|
94
|
+
name: therubyrhino
|
95
|
+
prerelease: false
|
96
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
hash: 3
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: "0"
|
105
|
+
type: :development
|
106
|
+
version_requirements: *id006
|
51
107
|
description: " ExecJS lets you run JavaScript code from Ruby.\n"
|
52
108
|
email:
|
53
109
|
- sstephenson@gmail.com
|
@@ -61,7 +117,9 @@ extra_rdoc_files: []
|
|
61
117
|
files:
|
62
118
|
- lib/execjs.rb
|
63
119
|
- lib/execjs/external_runtime.rb
|
120
|
+
- lib/execjs/johnson_runtime.rb
|
64
121
|
- lib/execjs/module.rb
|
122
|
+
- lib/execjs/version.rb
|
65
123
|
- lib/execjs/mustang_runtime.rb
|
66
124
|
- lib/execjs/ruby_racer_runtime.rb
|
67
125
|
- lib/execjs/ruby_rhino_runtime.rb
|
@@ -103,7 +161,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
103
161
|
requirements: []
|
104
162
|
|
105
163
|
rubyforge_project:
|
106
|
-
rubygems_version: 1.
|
164
|
+
rubygems_version: 1.5.0
|
107
165
|
signing_key:
|
108
166
|
specification_version: 3
|
109
167
|
summary: Run JavaScript code from Ruby
|