Skip to content

Commit

Permalink
[Truffle] Completed String#initialize.
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvdrum committed Mar 16, 2015
1 parent 49a3dfd commit 83a8b2e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
3 changes: 0 additions & 3 deletions spec/truffle/tags/core/string/initialize_tags.txt

This file was deleted.

Expand Up @@ -50,6 +50,8 @@
import org.jruby.truffle.nodes.dispatch.CallDispatchHeadNode;
import org.jruby.truffle.nodes.dispatch.DispatchHeadNode;
import org.jruby.truffle.nodes.dispatch.DispatchHeadNodeFactory;
import org.jruby.truffle.nodes.objects.IsFrozenNode;
import org.jruby.truffle.nodes.objects.IsFrozenNodeFactory;
import org.jruby.truffle.nodes.rubinius.StringPrimitiveNodes;
import org.jruby.truffle.nodes.rubinius.StringPrimitiveNodesFactory;
import org.jruby.truffle.runtime.RubyContext;
Expand Down Expand Up @@ -1210,12 +1212,17 @@ public RubyString inspect(RubyString string) {
@CoreMethod(names = "initialize", optional = 1, taintFromParameters = 0)
public abstract static class InitializeNode extends CoreMethodNode {

@Child private IsFrozenNode isFrozenNode;
@Child private ToStrNode toStrNode;

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

public InitializeNode(InitializeNode prev) {
super(prev);
isFrozenNode = prev.isFrozenNode;
toStrNode = prev.toStrNode;
}

@Specialization
Expand All @@ -1225,13 +1232,32 @@ public RubyString initialize(RubyString self, UndefinedPlaceholder from) {

@Specialization
public RubyString initialize(RubyString self, RubyString from) {
notDesignedForCompilation();
if (isFrozenNode == null) {
CompilerDirectives.transferToInterpreter();
isFrozenNode = insert(IsFrozenNodeFactory.create(getContext(), getSourceSection(), null));
}

if (isFrozenNode.executeIsFrozen(self)) {
CompilerDirectives.transferToInterpreter();
throw new RaiseException(
getContext().getCoreLibrary().frozenError(self.getLogicalClass().getName(), this));
}

self.set(from.getBytes());
self.setCodeRange(from.getCodeRange());

return self;
}

@Specialization(guards = { "!isRubyString(arguments[1])", "!isUndefinedPlaceholder(arguments[1])" })
public RubyString initialize(VirtualFrame frame, RubyString self, Object from) {
if (toStrNode == null) {
CompilerDirectives.transferToInterpreter();
toStrNode = insert(ToStrNodeFactory.create(getContext(), getSourceSection(), null));
}

return initialize(self, toStrNode.executeRubyString(frame, from));
}
}

@CoreMethod(names = "initialize_copy", visibility = Visibility.PRIVATE, required = 1)
Expand Down

0 comments on commit 83a8b2e

Please sign in to comment.