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: c23d280a0d2a
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 52351c4433d8
Choose a head ref
  • 2 commits
  • 4 files changed
  • 1 contributor

Commits on Jan 5, 2015

  1. Copy the full SHA
    41d41d1 View commit details
  2. Remove tainting from patterns in RubyDateFormatter.

    * It is an orthogonal concern to formatting time.
    * Allows to reuse RubyDateFormatter with a simple ByteList directly.
    eregon committed Jan 5, 2015
    Copy the full SHA
    52351c4 View commit details
27 changes: 27 additions & 0 deletions core/src/main/java/org/jruby/truffle/nodes/core/DirNodes.java
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;

import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.core.RubyProc;
@@ -59,6 +60,32 @@ public Object chdir(VirtualFrame frame, RubyString path, RubyProc block) {

}

@CoreMethod(names = { "delete", "rmdir", "unlink" }, onSingleton = true, optional = 1)
public abstract static class DeleteNode extends CoreMethodNode {

public DeleteNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public DeleteNode(DeleteNode prev) {
super(prev);
}

@Specialization
public int delete(RubyString path) {
notDesignedForCompilation();

File dir = new File(path.toString());
if (!dir.isDirectory()) {
throw new UnsupportedOperationException(path.toString());
}
dir.delete();

return 0;
}

}

@CoreMethod(names = {"exist?", "exists?"}, onSingleton = true, optional = 1)
public abstract static class ExistsNode extends CoreMethodNode {

Original file line number Diff line number Diff line change
@@ -213,10 +213,9 @@ public RubyString timeStrftime(VirtualFrame frame, RubyTime time, RubyString for


@CompilerDirectives.TruffleBoundary
public ByteList format(DateTime time, long nanoseconds, ByteList format) {
// TODO: converts everything to JRuby objects and back - should find a more direct way using ByteList
public ByteList format(DateTime time, long nanoseconds, ByteList pattern) {
final RubyDateFormatter rdf = getContext().getRuntime().getCurrentContext().getRubyDateFormatter();
return rdf.compileAndFormat(org.jruby.RubyString.newString(getContext().getRuntime(), format), false, time, nanoseconds, null).getByteList();
return rdf.formatToByteList(rdf.compilePattern(pattern, false), time, nanoseconds, null);
}

}
30 changes: 14 additions & 16 deletions core/src/main/java/org/jruby/util/RubyDateFormatter.java
Original file line number Diff line number Diff line change
@@ -67,8 +67,6 @@ public class RubyDateFormatter {
static enum Format {
/** encoding to give to output */
FORMAT_ENCODING,
/** taint output */
FORMAT_TAINT,
/** raw string, no formatting */
FORMAT_STRING,
/** formatter */
@@ -243,7 +241,10 @@ private void addToPattern(List<Token> compiledPattern, String str) {
}

public List<Token> compilePattern(RubyString format, boolean dateLibrary) {
ByteList pattern = format.getByteList();
return compilePattern(format.getByteList(), dateLibrary);
}

public List<Token> compilePattern(ByteList pattern, boolean dateLibrary) {
List<Token> compiledPattern = new LinkedList<Token>();

Encoding enc = pattern.getEncoding();
@@ -254,10 +255,6 @@ public List<Token> compilePattern(RubyString format, boolean dateLibrary) {
compiledPattern.add(new Token(Format.FORMAT_ENCODING, enc));
}

if (format.isTaint()) {
compiledPattern.add(new Token(Format.FORMAT_TAINT));
}

ByteArrayInputStream in = new ByteArrayInputStream(pattern.getUnsafeBytes(), pattern.getBegin(), pattern.getRealSize());
Reader reader = new InputStreamReader(in, context.runtime.getEncodingService().charsetForEncoding(pattern.getEncoding()));
lexer.yyreset(reader);
@@ -358,13 +355,20 @@ static enum FieldType {

/** Convenience method when using no pattern caching */
public RubyString compileAndFormat(RubyString pattern, boolean dateLibrary, DateTime dt, long nsec, IRubyObject sub_millis) {
return format(compilePattern(pattern, dateLibrary), dt, nsec, sub_millis);
RubyString out = format(compilePattern(pattern, dateLibrary), dt, nsec, sub_millis);
if (pattern.isTaint()) {
out.taint(context);
}
return out;
}

public RubyString format(List<Token> compiledPattern, DateTime dt, long nsec, IRubyObject sub_millis) {
return context.runtime.newString(formatToByteList(compiledPattern, dt, nsec, sub_millis));
}

public ByteList formatToByteList(List<Token> compiledPattern, DateTime dt, long nsec, IRubyObject sub_millis) {
RubyTimeOutputFormatter formatter = RubyTimeOutputFormatter.DEFAULT_FORMATTER;
ByteList toAppendTo = new ByteList();
boolean taint = false;

for (Token token: compiledPattern) {
String output = null;
@@ -376,9 +380,6 @@ public RubyString format(List<Token> compiledPattern, DateTime dt, long nsec, IR
case FORMAT_ENCODING:
toAppendTo.setEncoding((Encoding) token.getData());
continue; // go to next token
case FORMAT_TAINT:
taint = true;
continue; // go to next token
case FORMAT_OUTPUT:
formatter = (RubyTimeOutputFormatter) token.getData();
continue; // go to next token
@@ -553,10 +554,7 @@ public RubyString format(List<Token> compiledPattern, DateTime dt, long nsec, IR
toAppendTo.append(output.getBytes(context.runtime.getEncodingService().charsetForEncoding(toAppendTo.getEncoding())));
}

RubyString str = context.runtime.newString(toAppendTo);
if (taint)
str.taint(context);
return str;
return toAppendTo;
}

/**
4 changes: 3 additions & 1 deletion lib/ruby/stdlib/date/format.rb
Original file line number Diff line number Diff line change
@@ -135,7 +135,9 @@ def to_hash
end

def strftime(fmt='%F')
JRuby.runtime.current_context.getRubyDateFormatter.compileAndFormat(fmt, true, @dt, 0, @sub_millis.nonzero?)
out = JRuby.runtime.current_context.getRubyDateFormatter.compileAndFormat(fmt, true, @dt, 0, @sub_millis.nonzero?)
out.taint if fmt.tainted?
out
end

# alias_method :format, :strftime