Skip to content
Browse files

Avoid 'force_encoding'

  • Loading branch information...
1 parent adc3312 commit 09f3a8fd99e9d2e737a32a3b4229fc5e3c7b669d @wagenet wagenet committed Sep 7, 2011
Showing with 19 additions and 15 deletions.
  1. +19 −15 lib/execjs/external_runtime.rb
View
34 lib/execjs/external_runtime.rb
@@ -121,7 +121,25 @@ def runner_source
def exec_runtime(filename)
output = nil
- IO.popen("#{@binary} #{filename} 2>&1") { |f| output = fix_encoding(f.read) }
+
+ cmd = "#{@binary} #{filename} 2>&1"
+
+ if @encoding
+ if "".respond_to?(:encode)
+ IO.popen(cmd, :internal_encoding => 'UTF-8', :external_encoding => @encoding) do |f|
+ output = f.read
+ end
+ else
+ require 'iconv'
+ IO.popen(cmd) do |f|
+ ic = Iconv.new('UTF-8', @encoding)
+ output = ic.iconv(f.read)
+ end
+ end
+ else
+ IO.popen(cmd){|f| output = f.read }
+ end
+
if $?.success?
output
else
@@ -156,19 +174,5 @@ def which(command)
nil
end
- private
-
- def fix_encoding(data)
- return data unless @encoding
-
- if data.respond_to?(:encode)
- data.force_encoding(@encoding).encode('UTF-8')
- else
- require 'iconv'
- ic = Iconv.new('UTF-8', @encoding)
- ic.iconv(data)
- end
- end
-
end
end

0 comments on commit 09f3a8f

Please sign in to comment.
Something went wrong with that request. Please try again.