execjs 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 021a19b46b741b3d85475f23e28ab2fcaa92f7fb
4
- data.tar.gz: 7f284ac676523a23cff15992506f53e4538d0760
3
+ metadata.gz: b6c21deff91972c3dd78ad9cd59ac2b0c155aac7
4
+ data.tar.gz: bc434905c2f108e8e3a8a03b4c4acea3de19d481
5
5
  SHA512:
6
- metadata.gz: c77ce889ab7f9e871bc56d22ab505b0d175500b8dfd987fec7ba099b1211bae1d933a3001937f9d9d2285f3bc9bcfae37c8c02670e0af2d89392273b454720c2
7
- data.tar.gz: 59748635e20cbe379fe90e7ab229fa9ac3a563f472ea31ce676e03791b721734a1c30ce092a986ae60d522d38c1242dd7dc397025f70eda85494ca0d76f97d40
6
+ metadata.gz: 148ae528714f32a5c08728b48d24464a8f2a45ae8cae92dd5ac66b9b7e2618ca0236dcdcad4cec465298036e19cefec0fd533bed7cef198a5cc7daebf531227d
7
+ data.tar.gz: 76ad76dbcdb7cf54895e9d8137f9ecd2c9a7aee2c75b0704fe1afbfe6fa45ce7e3b42f69b1025be88c88ed9c41140bc11da123ff948085eae14fcc06cc66ecaf
@@ -1,4 +1,4 @@
1
- require "tempfile"
1
+ require "tmpdir"
2
2
  require "execjs/runtime"
3
3
 
4
4
  module ExecJS
@@ -22,9 +22,13 @@ module ExecJS
22
22
  def exec(source, options = {})
23
23
  source = encode(source)
24
24
  source = "#{@source}\n#{source}" if @source
25
+ source = @runtime.compile_source(source)
25
26
 
26
- compile_to_tempfile(source) do |file|
27
- extract_result(@runtime.send(:exec_runtime, file.path))
27
+ tmpfile = write_to_tempfile(source)
28
+ begin
29
+ extract_result(@runtime.exec_runtime(tmpfile.path))
30
+ ensure
31
+ File.unlink(tmpfile)
28
32
  end
29
33
  end
30
34
 
@@ -33,28 +37,21 @@ module ExecJS
33
37
  end
34
38
 
35
39
  protected
36
- def compile_to_tempfile(source)
37
- tempfile = Tempfile.open(['execjs', '.js'])
38
- tempfile.write compile(source)
39
- tempfile.close
40
- yield tempfile
41
- ensure
42
- tempfile.close!
40
+ # See Tempfile.create on Ruby 2.1
41
+ def create_tempfile(basename)
42
+ tmpfile = nil
43
+ Dir::Tmpname.create(basename) do |tmpname|
44
+ mode = File::WRONLY | File::CREAT | File::EXCL
45
+ tmpfile = File.open(tmpname, mode, 0600)
46
+ end
47
+ tmpfile
43
48
  end
44
49
 
45
- def compile(source)
46
- @runtime.send(:runner_source).dup.tap do |output|
47
- output.sub!('#{source}') do
48
- source
49
- end
50
- output.sub!('#{encoded_source}') do
51
- encoded_source = encode_unicode_codepoints(source)
52
- ::JSON.generate("(function(){ #{encoded_source} })()", quirks_mode: true)
53
- end
54
- output.sub!('#{json2_source}') do
55
- IO.read(ExecJS.root + "/support/json2.js")
56
- end
57
- end
50
+ def write_to_tempfile(contents)
51
+ tmpfile = create_tempfile(['execjs', 'js'])
52
+ tmpfile.write(contents)
53
+ tmpfile.close
54
+ tmpfile
58
55
  end
59
56
 
60
57
  def extract_result(output)
@@ -67,12 +64,6 @@ module ExecJS
67
64
  raise ProgramError, value
68
65
  end
69
66
  end
70
-
71
- def encode_unicode_codepoints(str)
72
- str.gsub(/[\u0080-\uffff]/) do |ch|
73
- "\\u%04x" % ch.codepoints.to_a
74
- end
75
- end
76
67
  end
77
68
 
78
69
  attr_reader :name
@@ -84,6 +75,14 @@ module ExecJS
84
75
  @encoding = options[:encoding]
85
76
  @deprecated = !!options[:deprecated]
86
77
  @binary = nil
78
+
79
+ @popen_options = {}
80
+ @popen_options[:external_encoding] = @encoding if @encoding
81
+ @popen_options[:internal_encoding] = ::Encoding.default_internal || 'UTF-8'
82
+
83
+ if @runner_path
84
+ instance_eval generate_compile_method(@runner_path)
85
+ end
87
86
  end
88
87
 
89
88
  def available?
@@ -117,18 +116,44 @@ module ExecJS
117
116
  end
118
117
 
119
118
  protected
120
- def runner_source
121
- @runner_source ||= IO.read(@runner_path)
119
+ def generate_compile_method(path)
120
+ <<-RUBY
121
+ def compile_source(source)
122
+ <<-RUNNER
123
+ #{IO.read(path)}
124
+ RUNNER
125
+ end
126
+ RUBY
127
+ end
128
+
129
+ def json2_source
130
+ @json2_source ||= IO.read(ExecJS.root + "/support/json2.js")
131
+ end
132
+
133
+ def encode_source(source)
134
+ encoded_source = encode_unicode_codepoints(source)
135
+ ::JSON.generate("(function(){ #{encoded_source} })()", quirks_mode: true)
136
+ end
137
+
138
+ def encode_unicode_codepoints(str)
139
+ str.gsub(/[\u0080-\uffff]/) do |ch|
140
+ "\\u%04x" % ch.codepoints.to_a
141
+ end
122
142
  end
123
143
 
124
144
  def exec_runtime(filename)
125
- output = sh(binary.split(' ') + [filename, {err: [:child, :out]}])
145
+ io = IO.popen(binary.split(' ') + [filename, {err: [:child, :out]}], @popen_options)
146
+ output = io.read
147
+ io.close
148
+
126
149
  if $?.success?
127
150
  output
128
151
  else
129
152
  raise RuntimeError, output
130
153
  end
131
154
  end
155
+ # Internally exposed for Context.
156
+ public :exec_runtime
132
157
 
133
158
  def which(command)
134
159
  Array(command).find do |name|
@@ -140,13 +165,5 @@ module ExecJS
140
165
  args ? "#{path} #{args}" : path
141
166
  end
142
167
  end
143
-
144
- def sh(command)
145
- output, options = nil, {}
146
- options[:external_encoding] = @encoding if @encoding
147
- options[:internal_encoding] = ::Encoding.default_internal || 'UTF-8'
148
- IO.popen(command, options) { |f| output = f.read }
149
- output
150
- end
151
168
  end
152
169
  end
@@ -1,5 +1,5 @@
1
1
  (function(program, execJS) { execJS(program) })(function() {
2
- return eval(#{encoded_source});
2
+ return eval(#{encode_source(source)});
3
3
  }, function(program) {
4
4
  var output;
5
5
  try {
@@ -1,5 +1,5 @@
1
1
  (function(program, execJS) { execJS(program) })(function() {
2
- return eval(#{encoded_source});
2
+ return eval(#{encode_source(source)});
3
3
  }, function(program) {
4
4
  #{json2_source}
5
5
  var output, print = function(string) {
@@ -1,3 +1,3 @@
1
1
  module ExecJS
2
- VERSION = "2.1.0"
2
+ VERSION = "2.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: execjs
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Stephenson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-05-27 00:00:00.000000000 Z
12
+ date: 2014-06-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake