execjs 2.2.2 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e37eb677376847f2bfe3abf9691b02bb7ac413eb
4
+ data.tar.gz: 0e44ddbc91755ce29cf30dc4f4bf775afde29f89
5
+ SHA512:
6
+ metadata.gz: 5d1e57303da891beb4ed994fe590bfa1480785edebc12c6babd02075a0dbf8051d1e2f73089aea28e3da70c41f3702fd0c65cbd76fd8d316d63cabbfbe974279
7
+ data.tar.gz: 2e12d615380795a7ea63b519f9c3ae556536fc72aa4856e56a5cb467167c941cd78c3e03e87eaf943c753f527957f29a79277fc81a01e81ab05a0fde2c9def01
data/README.md CHANGED
@@ -52,6 +52,25 @@ in. If you want to access the Node API, you should check another library like
52
52
  [commonjs.rb](https://github.com/cowboyd/commonjs.rb) designed to provide a
53
53
  consistent interface.
54
54
 
55
+ **Why can't I use `setTimeout`?**
56
+
57
+ For similar reasons as modules, not all runtimes guarantee a full JavaScript
58
+ event loop. So `setTimeout`, `setInterval` and other timers are not defined.
59
+
60
+ **Why can't I use ES5 features?**
61
+
62
+ Some runtimes like Node will implement many of the latest ES5 features. However
63
+ older stock runtimes like JSC on OSX and JScript on Windows may not. You should
64
+ only count on ES3 features being available. Prefer feature checking these APIs
65
+ rather than hard coding support for specific runtimes.
66
+
67
+ **Can I ExecJS be used to sandbox scripts?**
68
+
69
+ No, ExecJS shouldn't be used for any security related sandboxing. Since runtimes
70
+ are automatically detected, each runtime has different sandboxing properties.
71
+ You shouldn't use `ExecJS.eval` on any inputs you wouldn't feel comfortable Ruby
72
+ `eval()`ing.
73
+
55
74
 
56
75
  # License
57
76
 
@@ -9,6 +9,9 @@ module ExecJS
9
9
 
10
10
  @runtime = runtime
11
11
  @source = source
12
+
13
+ # Test compile context source
14
+ exec("")
12
15
  end
13
16
 
14
17
  def eval(source, options = {})
@@ -166,6 +169,21 @@ module ExecJS
166
169
  arg
167
170
  }.join(" ")
168
171
  end
172
+ elsif RUBY_ENGINE == 'jruby'
173
+ require 'shellwords'
174
+
175
+ def exec_runtime(filename)
176
+ command = "#{Shellwords.join(binary.split(' ') << filename)} 2>&1"
177
+ io = IO.popen(command, @popen_options)
178
+ output = io.read
179
+ io.close
180
+
181
+ if $?.success?
182
+ output
183
+ else
184
+ raise RuntimeError, output
185
+ end
186
+ end
169
187
  else
170
188
  def exec_runtime(filename)
171
189
  io = IO.popen(binary.split(' ') << filename, @popen_options.merge({err: [:child, :out]}))
@@ -8,7 +8,16 @@ module ExecJS
8
8
 
9
9
  lock do
10
10
  @v8_context = ::V8::Context.new
11
- @v8_context.eval(source)
11
+
12
+ begin
13
+ @v8_context.eval(source)
14
+ rescue ::V8::JSError => e
15
+ if e.value["name"] == "SyntaxError"
16
+ raise RuntimeError, e.value.to_s
17
+ else
18
+ raise ProgramError, e.value.to_s
19
+ end
20
+ end
12
21
  end
13
22
  end
14
23
 
@@ -9,6 +9,8 @@ module ExecJS
9
9
  @rhino_context = ::Rhino::Context.new
10
10
  fix_memory_limit! @rhino_context
11
11
  @rhino_context.eval(source)
12
+ rescue Exception => e
13
+ reraise_error(e)
12
14
  end
13
15
 
14
16
  def exec(source, options = {})
@@ -25,22 +27,14 @@ module ExecJS
25
27
  if /\S/ =~ source
26
28
  unbox @rhino_context.eval("(#{source})")
27
29
  end
28
- rescue ::Rhino::JSError => e
29
- if e.message =~ /^syntax error/
30
- raise RuntimeError, e.message
31
- else
32
- raise ProgramError, e.message
33
- end
30
+ rescue Exception => e
31
+ reraise_error(e)
34
32
  end
35
33
 
36
34
  def call(properties, *args)
37
35
  unbox @rhino_context.eval(properties).call(*args)
38
- rescue ::Rhino::JSError => e
39
- if e.message == "syntax error"
40
- raise RuntimeError, e.message
41
- else
42
- raise ProgramError, e.message
43
- end
36
+ rescue Exception => e
37
+ reraise_error(e)
44
38
  end
45
39
 
46
40
  def unbox(value)
@@ -64,6 +58,19 @@ module ExecJS
64
58
  end
65
59
  end
66
60
 
61
+ def reraise_error(e)
62
+ case e
63
+ when ::Rhino::JSError
64
+ if e.message == "syntax error"
65
+ raise RuntimeError, e.message
66
+ else
67
+ raise ProgramError, e.message
68
+ end
69
+ else
70
+ raise e
71
+ end
72
+ end
73
+
67
74
  private
68
75
  # Disables bytecode compiling which limits you to 64K scripts
69
76
  def fix_memory_limit!(context)
@@ -1,8 +1,6 @@
1
1
  require "execjs/module"
2
2
  require "execjs/disabled_runtime"
3
3
  require "execjs/external_runtime"
4
- require "execjs/johnson_runtime"
5
- require "execjs/mustang_runtime"
6
4
  require "execjs/ruby_racer_runtime"
7
5
  require "execjs/ruby_rhino_runtime"
8
6
 
@@ -14,10 +12,6 @@ module ExecJS
14
12
 
15
13
  RubyRhino = RubyRhinoRuntime.new
16
14
 
17
- Johnson = JohnsonRuntime.new
18
-
19
- Mustang = MustangRuntime.new
20
-
21
15
  Node = ExternalRuntime.new(
22
16
  name: "Node.js (V8)",
23
17
  command: ["nodejs", "node"],
@@ -78,10 +72,8 @@ module ExecJS
78
72
  @runtimes ||= [
79
73
  RubyRacer,
80
74
  RubyRhino,
81
- Johnson,
82
- Mustang,
83
- Node,
84
75
  JavaScriptCore,
76
+ Node,
85
77
  SpiderMonkey,
86
78
  JScript
87
79
  ]
@@ -1,5 +1,4 @@
1
- (function(program, execJS) { execJS(program) })(function() {
2
- return eval(#{encode_source(source)});
1
+ (function(program, execJS) { execJS(program) })(function() { #{source}
3
2
  }, function(program) {
4
3
  var output;
5
4
  try {
@@ -1,4 +1,4 @@
1
- (function(program, execJS) { execJS(program) })(function(module, exports, require, console) { #{source}
1
+ (function(program, execJS) { execJS(program) })(function(global, module, exports, require, console, setTimeout, setInterval, clearTimeout, clearInterval, setImmediate, clearImmediate) { #{source}
2
2
  }, function(program) {
3
3
  var output, print = function(string) {
4
4
  process.stdout.write('' + string);
@@ -1,6 +1,5 @@
1
1
  (function(program, execJS) { execJS(program) })(function() { #{source}
2
2
  }, function(program) {
3
- #{json2_source}
4
3
  var output;
5
4
  try {
6
5
  result = program();
@@ -1,3 +1,3 @@
1
1
  module ExecJS
2
- VERSION = "2.2.2"
2
+ VERSION = "2.3.0"
3
3
  end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: execjs
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.2
5
- prerelease:
4
+ version: 2.3.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Sam Stephenson
@@ -10,22 +9,20 @@ authors:
10
9
  autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2014-10-14 00:00:00.000000000 Z
12
+ date: 2015-02-03 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: rake
17
16
  requirement: !ruby/object:Gem::Requirement
18
- none: false
19
17
  requirements:
20
- - - ! '>='
18
+ - - ">="
21
19
  - !ruby/object:Gem::Version
22
20
  version: '0'
23
21
  type: :development
24
22
  prerelease: false
25
23
  version_requirements: !ruby/object:Gem::Requirement
26
- none: false
27
24
  requirements:
28
- - - ! '>='
25
+ - - ">="
29
26
  - !ruby/object:Gem::Version
30
27
  version: '0'
31
28
  description: ExecJS lets you run JavaScript code from Ruby.
@@ -36,14 +33,13 @@ executables: []
36
33
  extensions: []
37
34
  extra_rdoc_files: []
38
35
  files:
39
- - README.md
40
36
  - LICENSE
37
+ - README.md
38
+ - lib/execjs.rb
41
39
  - lib/execjs/disabled_runtime.rb
42
40
  - lib/execjs/encoding.rb
43
41
  - lib/execjs/external_runtime.rb
44
- - lib/execjs/johnson_runtime.rb
45
42
  - lib/execjs/module.rb
46
- - lib/execjs/mustang_runtime.rb
47
43
  - lib/execjs/ruby_racer_runtime.rb
48
44
  - lib/execjs/ruby_rhino_runtime.rb
49
45
  - lib/execjs/runtime.rb
@@ -54,30 +50,28 @@ files:
54
50
  - lib/execjs/support/node_runner.js
55
51
  - lib/execjs/support/spidermonkey_runner.js
56
52
  - lib/execjs/version.rb
57
- - lib/execjs.rb
58
53
  homepage: https://github.com/sstephenson/execjs
59
54
  licenses:
60
55
  - MIT
56
+ metadata: {}
61
57
  post_install_message:
62
58
  rdoc_options: []
63
59
  require_paths:
64
60
  - lib
65
61
  required_ruby_version: !ruby/object:Gem::Requirement
66
- none: false
67
62
  requirements:
68
- - - ! '>='
63
+ - - ">="
69
64
  - !ruby/object:Gem::Version
70
65
  version: '0'
71
66
  required_rubygems_version: !ruby/object:Gem::Requirement
72
- none: false
73
67
  requirements:
74
- - - ! '>='
68
+ - - ">="
75
69
  - !ruby/object:Gem::Version
76
70
  version: '0'
77
71
  requirements: []
78
72
  rubyforge_project:
79
- rubygems_version: 1.8.23
73
+ rubygems_version: 2.2.2
80
74
  signing_key:
81
- specification_version: 3
75
+ specification_version: 4
82
76
  summary: Run JavaScript code from Ruby
83
77
  test_files: []
@@ -1,104 +0,0 @@
1
- require "execjs/runtime"
2
-
3
- module ExecJS
4
- class JohnsonRuntime < Runtime
5
- class Context < Runtime::Context
6
- def initialize(runtime, source = "")
7
- source = encode(source)
8
-
9
- @runtime = Johnson::Runtime.new
10
- @runtime.evaluate(source)
11
- end
12
-
13
- def exec(source, options = {})
14
- source = encode(source)
15
-
16
- if /\S/ =~ source
17
- eval "(function(){#{source}})()", options
18
- end
19
- end
20
-
21
- def eval(source, options = {})
22
- source = encode(source)
23
-
24
- if /\S/ =~ source
25
- unbox @runtime.evaluate("(#{source})")
26
- end
27
- rescue Johnson::Error => e
28
- if syntax_error?(e)
29
- raise RuntimeError, e.message
30
- else
31
- raise ProgramError, e.message
32
- end
33
- end
34
-
35
- def call(properties, *args)
36
- unbox @runtime.evaluate(properties).call(*args)
37
- rescue Johnson::Error => e
38
- if syntax_error?(e)
39
- raise RuntimeError, e.message
40
- else
41
- raise ProgramError, e.message
42
- end
43
- end
44
-
45
- def unbox(value)
46
- case
47
- when function?(value)
48
- nil
49
- when string?(value)
50
- value.force_encoding('UTF-8')
51
- when array?(value)
52
- value.map { |v| unbox(v) }
53
- when object?(value)
54
- value.inject({}) do |vs, (k, v)|
55
- vs[k] = unbox(v) unless function?(v)
56
- vs
57
- end
58
- else
59
- value
60
- end
61
- end
62
-
63
- private
64
- def syntax_error?(error)
65
- error.message =~ /^syntax error at /
66
- end
67
-
68
- def function?(value)
69
- value.respond_to?(:function?) && value.function?
70
- end
71
-
72
- def string?(value)
73
- value.is_a?(String)
74
- end
75
-
76
- def array?(value)
77
- array_test.call(value)
78
- end
79
-
80
- def object?(value)
81
- value.respond_to?(:inject)
82
- end
83
-
84
- def array_test
85
- @array_test ||= @runtime.evaluate("(function(a) {return a instanceof [].constructor})")
86
- end
87
- end
88
-
89
- def name
90
- "Johnson (SpiderMonkey)"
91
- end
92
-
93
- def available?
94
- require "johnson"
95
- true
96
- rescue LoadError
97
- false
98
- end
99
-
100
- def deprecated?
101
- true
102
- end
103
- end
104
- end
@@ -1,76 +0,0 @@
1
- require "execjs/runtime"
2
-
3
- module ExecJS
4
- class MustangRuntime < Runtime
5
- class Context < Runtime::Context
6
- def initialize(runtime, source = "")
7
- source = encode(source)
8
-
9
- @v8_context = ::Mustang::Context.new
10
- @v8_context.eval(source)
11
- end
12
-
13
- def exec(source, options = {})
14
- source = encode(source)
15
-
16
- if /\S/ =~ source
17
- eval "(function(){#{source}})()", options
18
- end
19
- end
20
-
21
- def eval(source, options = {})
22
- source = encode(source)
23
-
24
- if /\S/ =~ source
25
- unbox @v8_context.eval("(#{source})")
26
- end
27
- end
28
-
29
- def call(properties, *args)
30
- unbox @v8_context.eval(properties).call(*args)
31
- rescue NoMethodError => e
32
- raise ProgramError, e.message
33
- end
34
-
35
- def unbox(value)
36
- case value
37
- when Mustang::V8::Array
38
- value.map { |v| unbox(v) }
39
- when Mustang::V8::Boolean
40
- value.to_bool
41
- when Mustang::V8::NullClass, Mustang::V8::UndefinedClass
42
- nil
43
- when Mustang::V8::Function
44
- nil
45
- when Mustang::V8::SyntaxError
46
- raise RuntimeError, value.message
47
- when Mustang::V8::Error
48
- raise ProgramError, value.message
49
- when Mustang::V8::Object
50
- value.inject({}) { |h, (k, v)|
51
- v = unbox(v)
52
- h[k] = v if v
53
- h
54
- }
55
- else
56
- value.respond_to?(:delegate) ? value.delegate : value
57
- end
58
- end
59
- end
60
-
61
- def name
62
- "Mustang (V8)"
63
- end
64
-
65
- def available?
66
- require "mustang"
67
- true
68
- rescue LoadError
69
- false
70
- end
71
-
72
- def deprecated?
73
- true
74
- end
75
- end
76
- end