Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 3609f62a6210
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 2205afe12a72
Choose a head ref
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on Dec 14, 2016

  1. [Truffle] Fix indentation.

    eregon committed Dec 14, 2016
    Copy the full SHA
    0f8e5d7 View commit details
  2. [Truffle] Simplify the default internal and external encoding handling.

    * Be more eager and less lazy.
    * Convert on set and not lazily.
    eregon committed Dec 14, 2016
    Copy the full SHA
    2205afe View commit details
Original file line number Diff line number Diff line change
@@ -10,7 +10,6 @@
package org.jruby.truffle.core.encoding;

import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
@@ -24,8 +23,6 @@
import org.jruby.truffle.builtins.CoreMethod;
import org.jruby.truffle.builtins.CoreMethodArrayArgumentsNode;
import org.jruby.truffle.builtins.YieldingCoreMethodNode;
import org.jruby.truffle.core.cast.ToStrNode;
import org.jruby.truffle.core.cast.ToStrNodeGen;
import org.jruby.truffle.core.string.ByteList;
import org.jruby.truffle.core.string.StringOperations;
import org.jruby.truffle.language.control.RaiseException;
@@ -36,69 +33,34 @@ public abstract class TruffleEncodingNodes {
@CoreMethod(names = "default_external=", onSingleton = true, required = 1)
public abstract static class SetDefaultExternalNode extends CoreMethodArrayArgumentsNode {

@Child private ToStrNode toStrNode;

@Specialization(guards = "isRubyEncoding(encoding)")
public DynamicObject defaultExternalEncoding(DynamicObject encoding) {
getContext().getEncodingManager().setDefaultExternalEncoding(EncodingOperations.getEncoding(encoding));

return encoding;
}

@Specialization(guards = "isRubyString(encodingString)")
public DynamicObject defaultExternal(DynamicObject encodingString) {
final DynamicObject rubyEncoding = getContext().getEncodingManager().getRubyEncoding(StringOperations.getString(encodingString));
getContext().getEncodingManager().setDefaultExternalEncoding(EncodingOperations.getEncoding(rubyEncoding));

return rubyEncoding;
}

@Specialization(guards = "isNil(nil)")
public DynamicObject defaultExternal(Object nil) {
throw new RaiseException(coreExceptions().argumentError("default external can not be nil", this));
}

@Specialization(guards = { "!isRubyEncoding(encoding)", "!isRubyString(encoding)", "!isNil(encoding)" })
public DynamicObject defaultExternal(VirtualFrame frame, Object encoding) {
if (toStrNode == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
toStrNode = insert(ToStrNodeGen.create(getContext(), null, null));
}

return defaultExternal(toStrNode.executeToStr(frame, encoding));
}

}

@CoreMethod(names = "default_internal=", onSingleton = true, required = 1)
public abstract static class SetDefaultInternalNode extends CoreMethodArrayArgumentsNode {

@Child private ToStrNode toStrNode;

@Specialization(guards = "isRubyEncoding(encoding)")
public DynamicObject defaultInternal(DynamicObject encoding) {
getContext().getEncodingManager().setDefaultInternalEncoding(EncodingOperations.getEncoding(encoding));

return encoding;
}

@Specialization(guards = "isNil(encoding)")
public DynamicObject defaultInternal(Object encoding) {
getContext().getEncodingManager().setDefaultInternalEncoding(null);

return nil();
}

@Specialization(guards = { "!isRubyEncoding(encoding)", "!isNil(encoding)" })
public DynamicObject defaultInternal(VirtualFrame frame, Object encoding) {
if (toStrNode == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
toStrNode = insert(ToStrNodeGen.create(getContext(), null, null));
}

return toStrNode.executeToStr(frame, encoding);
}

}

@CoreMethod(names = "each_alias", onSingleton = true, needsBlock = true)
16 changes: 6 additions & 10 deletions truffle/src/main/ruby/core/encoding.rb
Original file line number Diff line number Diff line change
@@ -81,8 +81,8 @@ def build_encoding_map
TranscodingMap = Encoding::Converter.transcoding_map
EncodingMap = build_encoding_map

@default_external = undefined
@default_internal = undefined
@default_external = Truffle::Encoding.get_default_encoding('external')
@default_internal = Truffle::Encoding.get_default_encoding('internal')

class UndefinedConversionError < EncodingError
attr_accessor :source_encoding_name
@@ -576,31 +576,27 @@ def self.set_alias_index(name, obj)
private_class_method :set_alias_index

def self.default_external
if undefined.equal? @default_external
@default_external = find "external"
end
@default_external
end

def self.default_external=(enc)
raise ArgumentError, "default external encoding cannot be nil" if enc.nil?

enc = find(enc)
set_alias_index "external", enc
set_alias_index "filesystem", enc
@default_external = undefined
@default_external = enc
Truffle::Encoding.default_external = enc
end

def self.default_internal
if undefined.equal? @default_internal
@default_internal = find "internal"
end
@default_internal
end

def self.default_internal=(enc)
enc = find(enc) unless enc.nil?
set_alias_index "internal", enc
@default_internal = undefined
@default_internal = enc
Truffle::Encoding.default_internal = enc
end

58 changes: 29 additions & 29 deletions truffle/src/main/ruby/core/string.rb
Original file line number Diff line number Diff line change
@@ -470,42 +470,42 @@ def encode!(to=undefined, from=undefined, options=undefined)
Truffle.check_frozen

case to
when Encoding
to_enc = to
when Hash
options = to
to_enc = Encoding.default_internal
when undefined
when Encoding
to_enc = to
when Hash
options = to
to_enc = Encoding.default_internal
when undefined
to_enc = Encoding.default_internal
return self unless to_enc
else
opts = Rubinius::Type::check_convert_type to, Hash, :to_hash

if opts
options = opts
to_enc = Encoding.default_internal
return self unless to_enc
else
opts = Rubinius::Type::check_convert_type to, Hash, :to_hash

if opts
options = opts
to_enc = Encoding.default_internal
else
to_enc = Rubinius::Type.try_convert_to_encoding to
end
to_enc = Rubinius::Type.try_convert_to_encoding to
end
end

case from
when undefined
from_enc = encoding
when Encoding
from_enc = from
when Hash
options = from
when undefined
from_enc = encoding
when Encoding
from_enc = from
when Hash
options = from
from_enc = encoding
else
opts = Rubinius::Type::check_convert_type from, Hash, :to_hash

if opts
options = opts
from_enc = encoding
else
opts = Rubinius::Type::check_convert_type from, Hash, :to_hash

if opts
options = opts
from_enc = encoding
else
from_enc = Rubinius::Type.coerce_to_encoding from
end
from_enc = Rubinius::Type.coerce_to_encoding from
end
end

if undefined.equal? from_enc or undefined.equal? to_enc