Skip to content

Commit

Permalink
Showing 5 changed files with 69 additions and 41 deletions.
3 changes: 3 additions & 0 deletions truffle/src/main/java/org/jruby/truffle/core/CoreLibrary.java
Original file line number Diff line number Diff line change
@@ -87,6 +87,7 @@
import org.jruby.truffle.core.rubinius.WeakRefPrimitiveNodesFactory;
import org.jruby.truffle.core.string.StringNodesFactory;
import org.jruby.truffle.core.string.StringOperations;
import org.jruby.truffle.core.string.TruffleStringNodesFactory;
import org.jruby.truffle.core.symbol.SymbolNodesFactory;
import org.jruby.truffle.core.thread.ThreadBacktraceLocationLayoutImpl;
import org.jruby.truffle.core.thread.ThreadBacktraceLocationNodesFactory;
@@ -615,6 +616,7 @@ public CoreLibrary(RubyContext context) {
defineModule(truffleModule, "Ropes");
defineModule(truffleModule, "GC");
defineModule(truffleModule, "Array");
defineModule(truffleModule, "String");
final DynamicObject attachments = defineModule(truffleModule, "Attachments");
defineModule(attachments, "Internal");
defineModule(truffleModule, "Boot");
@@ -846,6 +848,7 @@ public void addCoreMethods() {
coreMethodNodeManager.addCoreMethodNodes(TruffleDebugNodesFactory.getFactories());
coreMethodNodeManager.addCoreMethodNodes(TruffleBindingNodesFactory.getFactories());
coreMethodNodeManager.addCoreMethodNodes(TruffleArrayNodesFactory.getFactories());
coreMethodNodeManager.addCoreMethodNodes(TruffleStringNodesFactory.getFactories());
coreMethodNodeManager.addCoreMethodNodes(BCryptNodesFactory.getFactories());
return null;
}));
Original file line number Diff line number Diff line change
@@ -89,7 +89,6 @@
import org.jruby.truffle.builtins.CoreMethod;
import org.jruby.truffle.builtins.CoreMethodArrayArgumentsNode;
import org.jruby.truffle.builtins.CoreMethodNode;
import org.jruby.truffle.builtins.NonStandard;
import org.jruby.truffle.builtins.Primitive;
import org.jruby.truffle.builtins.PrimitiveArrayArgumentsNode;
import org.jruby.truffle.builtins.PrimitiveNode;
@@ -1490,38 +1489,6 @@ public Object lstripBang(DynamicObject string) {
}
}

@NonStandard
@CoreMethod(names = "num_bytes=", lowerFixnumParameters = 0, required = 1)
public abstract static class SetNumBytesNode extends CoreMethodArrayArgumentsNode {

@Child private RopeNodes.MakeSubstringNode makeSubstringNode;

public SetNumBytesNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
makeSubstringNode = RopeNodesFactory.MakeSubstringNodeGen.create(null, null, null);
}

@Specialization
public DynamicObject setNumBytes(DynamicObject string, int count,
@Cached("create()") BranchProfile errorProfile) {
final Rope rope = rope(string);

if (count > rope.byteLength()) {
errorProfile.enter();
throw new RaiseException(coreExceptions().argumentError(formatError(count, rope), this));
}

StringOperations.setRope(string, makeSubstringNode.executeMake(rope, 0, count));

return string;
}

@TruffleBoundary
private String formatError(int count, final Rope rope) {
return String.format("Invalid byte count: %d exceeds string size of %d bytes", count, rope.byteLength());
}
}

@CoreMethod(names = "ord")
@ImportStatic(StringGuards.class)
public abstract static class OrdNode extends CoreMethodArrayArgumentsNode {
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.core.string;


import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.object.DynamicObject;
import org.jruby.truffle.builtins.CoreClass;
import org.jruby.truffle.builtins.CoreMethod;
import org.jruby.truffle.builtins.CoreMethodArrayArgumentsNode;
import org.jruby.truffle.core.rope.Rope;
import org.jruby.truffle.core.rope.RopeNodes;
import org.jruby.truffle.language.RubyGuards;
import org.jruby.truffle.language.control.RaiseException;

import static org.jruby.truffle.core.string.StringOperations.rope;

@CoreClass("Truffle::String")
public class TruffleStringNodes {

@CoreMethod(names = "truncate", onSingleton = true, required = 2, lowerFixnumParameters = 1)
public abstract static class TruncateNode extends CoreMethodArrayArgumentsNode {

@Specialization(guards = { "isRubyString(string)", "isNewLengthTooLarge(string, newByteLength)" })
@TruffleBoundary
public DynamicObject truncateError(DynamicObject string, int newByteLength) {
throw new RaiseException(
getContext().getCoreExceptions().argumentError(formatError(newByteLength, rope(string)), this));
}

@Specialization(guards = { "isRubyString(string)" })
public DynamicObject stealStorage(DynamicObject string, int newByteLength,
@Cached("createX()") RopeNodes.MakeSubstringNode makeSubstringNode) {

StringOperations.setRope(string, makeSubstringNode.executeMake(rope(string), 0, newByteLength));

return string;
}

protected static boolean isNewLengthTooLarge(DynamicObject string, int newByteLength) {
assert RubyGuards.isRubyString(string);

return newByteLength > rope(string).byteLength();
}

@TruffleBoundary
private String formatError(int count, final Rope rope) {
return String.format("Invalid byte count: %d exceeds string size of %d bytes", count, rope.byteLength());
}

}

}
10 changes: 3 additions & 7 deletions truffle/src/main/ruby/core/string.rb
Original file line number Diff line number Diff line change
@@ -112,10 +112,6 @@ def find_character(offset)
raise PrimitiveFailure, "String#find_character primitive failed"
end

def num_bytes
bytesize
end

def byte_append(str)
Truffle.primitive :string_byte_append
raise TypeError, "String#byte_append primitive only accepts Strings"
@@ -436,7 +432,7 @@ def suffix?(other)

def shorten!(size)
return if bytesize == 0
self.num_bytes -= size
Truffle::String.truncate(self, bytesize - size)
end

def each_codepoint
@@ -937,7 +933,7 @@ def chop!
end
end

self.num_bytes = bytes
Truffle::String.truncate(self, bytes)

self
end
@@ -1003,7 +999,7 @@ def chomp!(sep=undefined)
bytes = bytesize - size
end

self.num_bytes = bytes
Truffle::String.truncate(self, bytes)

self
end
2 changes: 1 addition & 1 deletion truffle/src/main/ruby/core/symbol.rb
Original file line number Diff line number Diff line change
@@ -117,7 +117,7 @@ def [](index, other = undefined)
end

str = to_s
match_data = index.search_region(str, 0, str.num_bytes, true)
match_data = index.search_region(str, 0, str.bytesize, true)
Regexp.last_match = match_data
if match_data
result = match_data.to_s

0 comments on commit 1041cf6

Please sign in to comment.