lib/execjs/external_runtime.rb in execjs-1.2.4 vs lib/execjs/external_runtime.rb in execjs-1.2.5
- old
+ new
@@ -89,10 +89,11 @@
@name = options[:name]
@command = options[:command]
@runner_path = options[:runner_path]
@test_args = options[:test_args]
@test_match = options[:test_match]
+ @encoding = options[:encoding]
@binary = locate_binary
end
def exec(source)
context = Context.new(self)
@@ -117,12 +118,11 @@
def runner_source
@runner_source ||= IO.read(@runner_path)
end
def exec_runtime(filename)
- output = nil
- IO.popen("#{@binary} #{filename} 2>&1") { |f| output = f.read }
+ output = sh("#{@binary} #{filename} 2>&1")
if $?.success?
output
else
raise RuntimeError, output
end
@@ -141,18 +141,41 @@
def which(command)
Array(command).each do |name|
name, args = name.split(/\s+/, 2)
result = if ExecJS.windows?
- `#{ExecJS.root}/support/which.bat #{name}`
+ `"#{ExecJS.root}/support/which.bat" #{name}`
else
`command -v #{name} 2>/dev/null`
end
if path = result.strip.split("\n").first
return args ? "#{path} #{args}" : path
end
end
nil
+ end
+
+ if "".respond_to?(:force_encoding)
+ def sh(command)
+ output, options = nil, {}
+ options[:external_encoding] = @encoding if @encoding
+ options[:internal_encoding] = Encoding.default_internal || 'UTF-8'
+ IO.popen(command, options) { |f| output = f.read }
+ output
+ end
+ else
+ require "iconv"
+
+ def sh(command)
+ output = nil
+ IO.popen(command) { |f| output = f.read }
+
+ if @encoding
+ Iconv.new('UTF-8', @encoding).iconv(output)
+ else
+ output
+ end
+ end
end
end
end