Permalink
Browse files

Define sh helper specific to ruby env

  • Loading branch information...
1 parent bce2e4f commit c9278dbe8a9b6264766d01a0d89d240275e89e27 @josh josh committed Sep 7, 2011
Showing with 22 additions and 20 deletions.
  1. +22 −20 lib/execjs/external_runtime.rb
@@ -120,26 +120,7 @@ def runner_source
end
def exec_runtime(filename)
- output = nil
-
- 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
-
+ output = sh("#{@binary} #{filename} 2>&1")
if $?.success?
output
else
@@ -174,5 +155,26 @@ def which(command)
nil
end
+ if "".respond_to?(:force_encoding)
+ def sh(command)
+ output, options = nil, {}
+ options[:external_encoding] = @encoding if @encoding
+ 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

8 comments on commit c9278db

Contributor

wagenet replied Sep 8, 2011

I'm getting some complaints on Windows that "ASCII incompatible encoding needs binmode". If I force :binmode => true for IO.popen then it works. Alternatively, I can set :internal_encoding => 'UTF-8'. It seems that we need to provide the way to set one of these. What are your thoughts?

Contributor

josh replied Sep 9, 2011

I think binmode disabling the encoding conversation. Wondering why that would work.

You're talking about the 1.9 part of the code, right?

I'd like the the return value of eval or exec to return a string with whatever encoding the user has set to default_internal. This is a the default behavior of most IO methods.

If we can't, I'm fine with always returning UTF-8 strings.

Contributor

wagenet replied Sep 9, 2011

Yeah, I'm talking about the Ruby 1.9 part. I'm with you in that it seems like :binmode shouldn't work, but it does. I also agree about tending to want to go with default_internal but in this case at least that doesn't seem possible unless I'm missing something. Maybe we could do: :internal_encoding => Encoding.default_internal || 'UTF-8'?

Contributor

josh replied Sep 9, 2011

Encoding.default_internal || 'UTF-8' 👍

Contributor

wagenet replied Sep 9, 2011

I'll give it a check in my app and see if it works.

Contributor

wagenet replied Sep 9, 2011

Actually, it would work for sure, since I don't set default_internal. The only question is whether there could be a value of default_internal that would cause problems. I don't think that's likely, but I'm not enough of an expert in this area to say for sure. If you feel comfortable with it, I guess you should just go ahead and make the change. I could send a pull request, but that seems slightly overkill :)

Contributor

josh replied Sep 9, 2011

You could make the pull if you want :)

We're adding this, correct?

options[:internal_encoding] = Encoding.default_internal || 'UTF-8'
Contributor

wagenet replied Sep 9, 2011

Please sign in to comment.