Skip to content

Commit

Permalink
Eliminate remaining uses of getInstanceVariableList
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Jun 20, 2024
1 parent 1284bf6 commit bcc71ef
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
18 changes: 11 additions & 7 deletions core/src/main/java/org/jruby/BasicObjectStub.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.util.Collections;
import java.util.List;
import java.util.function.BiConsumer;

import org.jruby.runtime.Helpers;
import org.jruby.runtime.Block;
Expand Down Expand Up @@ -259,13 +260,16 @@ public static IRubyObject inspect(IRubyObject self) {
*/
private static StringBuilder inspectObj(IRubyObject self, StringBuilder part) {
ThreadContext context = getRuntime(self).getCurrentContext();
String sep = "";

for (Variable<IRubyObject> ivar : getInstanceVariables(self).getInstanceVariableList()) {
part.append(sep).append(' ').append(ivar.getName()).append('=');
part.append(invokedynamic(context, ivar.getValue(), INSPECT));
sep = ",";
}
getInstanceVariables(self).forEachInstanceVariable(new BiConsumer<>() {
String sep = "";

@Override
public void accept(String name, IRubyObject value) {
part.append(sep).append(' ').append(name).append('=');
part.append(invokedynamic(context, value, INSPECT));
sep = ",";
}
});
part.append('>');
return part;
}
Expand Down
15 changes: 10 additions & 5 deletions core/src/main/java/org/jruby/RubyBasicObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiConsumer;

import org.jruby.anno.JRubyMethod;
import org.jruby.common.IRubyWarnings.ID;
Expand Down Expand Up @@ -1579,11 +1580,7 @@ public List<String> getInstanceVariableNameList() {
*/
@Override
public void copyInstanceVariablesInto(final InstanceVariables other) {
for (Variable<IRubyObject> var : getInstanceVariableList()) {
synchronized (this) {
other.setInstanceVariable(var.getName(), var.getValue());
}
}
forEachInstanceVariable(other::setInstanceVariable);
}

/**
Expand All @@ -1602,6 +1599,14 @@ public final void ensureInstanceVariablesSettable() {
}
}

public void forEachInstanceVariable(BiConsumer<String, IRubyObject> accessor) {
metaClass.getVariableAccessorsForRead().forEach((name, var) -> {
final Object value = var.get(this);
if (!(value instanceof IRubyObject rubyObject) || !IdUtil.isInstanceVariable(name)) return;
accessor.accept(name, rubyObject);
});
}

private void raiseFrozenError() throws RaiseException {
if (this instanceof RubyModule) {
throw getRuntime().newFrozenError("class/module ", this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.jruby.runtime.builtin;

import java.util.List;
import java.util.function.BiConsumer;

/**
* Interface that represents the instance variable aspect of Ruby
Expand Down Expand Up @@ -74,4 +75,8 @@ public interface InstanceVariables {
* Copies all instance variables from the given object into the receiver
*/
void copyInstanceVariablesInto(InstanceVariables other);

default void forEachInstanceVariable(BiConsumer<String, IRubyObject> accessor) {
getInstanceVariableList().forEach((var) -> accessor.accept(var.getName(), var.getValue()));
}
}

0 comments on commit bcc71ef

Please sign in to comment.