Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -800,9 +800,23 @@ protected Operand buildCallKeywordArguments(KeywordHashNode node, int[] flags) {

if (hasOnlyRestKwargs(node.elements)) return buildRestKeywordArgs(node, flags);

if (hasOnlySimpleKeywordArguments(node.elements)) {
return buildHash(node.elements, containsVariableAssignment(node.elements), true);
}

return buildHash(node.elements, containsVariableAssignment(node.elements));
}

private static boolean hasOnlySimpleKeywordArguments(Node[] elements) {
for (Node element : elements) {
if (!(element instanceof AssocNode assoc) || !(assoc.key instanceof SymbolNode)) {
return false;
}
}

return true;
}

private Operand buildCallOrWrite(CallOrWriteNode node) {
return buildOpAsgn(node.receiver, node.value, symbol(node.read_name), symbol(node.write_name), symbol(OR_OR), node.isSafeNavigation());
}
Expand Down Expand Up @@ -1330,6 +1344,10 @@ private Operand buildGlobalVariableWrite(GlobalVariableWriteNode node) {
}

private Operand buildHash(Node[] elements, boolean hasAssignments) {
return buildHash(elements, hasAssignments, false);
}

private Operand buildHash(Node[] elements, boolean hasAssignments, boolean keywordArguments) {
List<KeyValuePair<Operand, Operand>> args = new ArrayList<>();
Set<Object> keysHack = new HashSet<>(); // Remove once prism #2005 is fixed.
Variable hash = null;
Expand Down Expand Up @@ -1380,6 +1398,7 @@ private Operand buildHash(Node[] elements, boolean hasAssignments) {
}

if (hash == null) { // non-**arg ordinary hash
if (keywordArguments) return new Hash(args, true);
hash = copy(new Hash(args));
} else if (!args.isEmpty()) { // ordinary hash values encountered after a **arg
addInstr(new RuntimeHelperCall(hash, MERGE_KWARGS, new Operand[] { hash, new Hash(args), duplicateCheck}));
Expand Down Expand Up @@ -2955,4 +2974,4 @@ public RubySymbol[] symbols(byte[][] tokens) {
}
return names;
}
}
}
11 changes: 7 additions & 4 deletions core/src/main/java/org/jruby/ir/operands/Hash.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// that actually build the hash
public class Hash extends Operand {
public final KeyValuePair<Operand, Operand>[] pairs;
// True when this literal is a call's keyword argument list, not an ordinary Hash value.
public final boolean keywords;

public Hash(List<KeyValuePair<Operand, Operand>> pairs) {
Expand Down Expand Up @@ -77,7 +78,7 @@ public Operand getSimplifiedOperand(Map<Operand, Operand> valueMap, boolean forc
pair.getValue().getSimplifiedOperand(valueMap, force)))
.toArray(n -> new KeyValuePair[n]);

return new Hash(newPairs);
return new Hash(newPairs, keywords);
}

/** Append the list of variables used in this operand to the input list */
Expand All @@ -97,7 +98,7 @@ public Operand cloneForLVarDepth(int newDepth) {
((DepthCloneable) pair.getValue()).cloneForDepth(newDepth)))
.toArray(n -> new KeyValuePair[n]);

return new Hash(newPairs);
return new Hash(newPairs, keywords);
}

@Override
Expand All @@ -112,7 +113,7 @@ public Operand cloneForInlining(CloneInfo ii) {
pair.getValue().cloneForInlining(ii)))
.toArray(n -> new KeyValuePair[n]);

return new Hash(newPairs);
return new Hash(newPairs, keywords);
}

public boolean isKeywordRest() {
Expand Down Expand Up @@ -163,6 +164,7 @@ public void visit(IRVisitor visitor) {
@Override
public void encode(IRWriterEncoder e) {
super.encode(e);
e.encode(keywords);
e.encode(pairs.length);
for (KeyValuePair<Operand, Operand> pair: pairs) {
e.encode(pair.getKey());
Expand All @@ -171,14 +173,15 @@ public void encode(IRWriterEncoder e) {
}

public static Hash decode(IRReaderDecoder d) {
boolean keywords = d.decodeBoolean();
int size = d.decodeInt();
KeyValuePair<Operand, Operand> pairs[] = new KeyValuePair[size];

for (int i = 0; i < size; i++) {
pairs[i] = new KeyValuePair(d.decodeOperand(), d.decodeOperand());
}

return new Hash(pairs);
return new Hash(pairs, keywords);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @author enebo
*/
interface IRPersistenceValues {
public static final int VERSION = 3;
public static final int VERSION = 4;
public final static int TWO_MEGS = 1024 * 1024 * 2;

// Operands and primitive values can be mixed together
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/org/jruby/runtime/CallArgument.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.Arrays;

/** A JVM stack slot in an outgoing Ruby call, in invocation order. */
public record CallArgument(int index, Type type, Identifier identifier) {
public enum Type {
POSITIONAL('p', IRubyObject.class),
Expand Down
55 changes: 55 additions & 0 deletions core/src/test/java/org/jruby/ir/operands/HashTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.jruby.ir.operands;

import junit.framework.TestCase;
import org.jruby.ir.persistence.IRReaderDecoder;
import org.jruby.ir.persistence.IRWriterEncoder;
import org.jruby.ir.transformations.inlining.SimpleCloneInfo;
import org.jruby.util.KeyValuePair;

import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class HashTest extends TestCase {
public void testKeywordMarkerSurvivesIRTransforms() {
Hash keywords = keywordHash();

assertTrue(((Hash) keywords.getSimplifiedOperand(Map.of(), false)).keywords);
assertTrue(((Hash) keywords.cloneForLVarDepth(0)).keywords);
assertTrue(((Hash) keywords.cloneForInlining(new SimpleCloneInfo(null, false))).keywords);
}

public void testKeywordMarkerSurvivesPersistence() {
List<Object> encoded = new ArrayList<>();
IRWriterEncoder writer = proxy(IRWriterEncoder.class, (proxy, method, args) -> {
if (method.getName().equals("encode")) encoded.add(args[0]);
return null;
});

new Hash(List.of(), true).encode(writer);

assertEquals(OperandType.HASH.getCoded(), encoded.get(0));
assertEquals(Boolean.TRUE, encoded.get(1));

Iterator<Object> fields = encoded.subList(1, encoded.size()).iterator();
IRReaderDecoder reader = proxy(IRReaderDecoder.class, (proxy, method, args) -> {
if (method.getName().equals("decodeBoolean")) return fields.next();
if (method.getName().equals("decodeInt")) return fields.next();
return null;
});

assertTrue(Hash.decode(reader).keywords);
}

private static Hash keywordHash() {
List<KeyValuePair<Operand, Operand>> pairs = new ArrayList<>();
pairs.add(new KeyValuePair<>(new Fixnum(1), new LocalVariable(null, 1, 0)));
return new Hash(pairs, true);
}

private static <T> T proxy(Class<T> type, java.lang.reflect.InvocationHandler handler) {
return type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[]{type}, handler));
}
}
10 changes: 10 additions & 0 deletions spec/compiler/general_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1913,5 +1913,15 @@ def z_super

expect(looped).to eq nonlooped
end

it "passes simple keyword arguments through compiled calls" do
run(<<~RUBY) { expect(it).to eq [1, 2] }
def keyword_target(required:, optional: 2)
[required, optional]
end

keyword_target(required: 1)
RUBY
end
end
end
Loading