Skip to content

Commit 7e9dd52

Browse files
committed
[Truffle] Remove MODULE_FUNCTION_FLAG_FRAME_SLOT_ID.
* Since it is exclusive with other visibilities, we use the VISIBILITY_FRAME_SLOT_ID instead.
1 parent 4e96624 commit 7e9dd52

File tree

5 files changed

+25
-77
lines changed

5 files changed

+25
-77
lines changed

core/src/main/java/org/jruby/truffle/nodes/core/ModuleNodes.java

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -947,44 +947,11 @@ public ModuleFunctionNode(ModuleFunctionNode prev) {
947947
}
948948

949949
@Specialization
950-
public RubyNilClass moduleFunction(RubyModule module, Object... args) {
950+
public RubyModule moduleFunction(RubyModule module, Object... args) {
951951
notDesignedForCompilation();
952952

953-
if (args.length == 0) {
954-
final Frame unpacked = Truffle.getRuntime().getCallerFrame().getFrame(FrameInstance.FrameAccess.READ_WRITE, false);
955-
956-
final FrameSlot slot = unpacked.getFrameDescriptor().findFrameSlot(RubyModule.MODULE_FUNCTION_FLAG_FRAME_SLOT_ID);
957-
958-
/*
959-
* setObject, even though it's a boolean, so we can getObject and either get the
960-
* default Nil or the boolean value without triggering deoptimization.
961-
*/
962-
963-
unpacked.setObject(slot, true);
964-
} else {
965-
for (Object argument : args) {
966-
final String methodName;
967-
968-
if (argument instanceof RubySymbol) {
969-
methodName = ((RubySymbol) argument).toString();
970-
} else if (argument instanceof RubyString) {
971-
methodName = ((RubyString) argument).toString();
972-
} else {
973-
throw new UnsupportedOperationException();
974-
}
975-
976-
// TODO(cs): make this instance method private
977-
final RubyMethod method = ModuleOperations.lookupMethod(module, methodName);
978-
979-
if (method == null) {
980-
throw new UnsupportedOperationException();
981-
}
982-
983-
module.getSingletonClass(this).addMethod(this, method.withVisibility(Visibility.PUBLIC));
984-
}
985-
}
986-
987-
return getContext().getCoreLibrary().getNilObject();
953+
module.visibilityMethod(this, args, Visibility.MODULE_FUNCTION);
954+
return module;
988955
}
989956
}
990957

core/src/main/java/org/jruby/truffle/nodes/methods/AddMethodNode.java

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
*/
1010
package org.jruby.truffle.nodes.methods;
1111

12-
import com.oracle.truffle.api.frame.FrameSlot;
13-
import com.oracle.truffle.api.frame.FrameSlotTypeException;
1412
import com.oracle.truffle.api.frame.VirtualFrame;
1513
import com.oracle.truffle.api.source.SourceSection;
1614
import org.jruby.runtime.Visibility;
@@ -24,12 +22,12 @@
2422
public class AddMethodNode extends RubyNode {
2523

2624
@Child protected RubyNode receiver;
27-
@Child protected MethodDefinitionNode method;
25+
@Child protected MethodDefinitionNode methodNode;
2826

2927
public AddMethodNode(RubyContext context, SourceSection section, RubyNode receiver, MethodDefinitionNode method, boolean topLevel) {
3028
super(context, section);
3129
this.receiver = receiver;
32-
this.method = method;
30+
this.methodNode = method;
3331
}
3432

3533
@Override
@@ -38,7 +36,7 @@ public RubySymbol execute(VirtualFrame frame) {
3836

3937
final Object receiverObject = receiver.execute(frame);
4038

41-
final RubyMethod methodObject = (RubyMethod) method.execute(frame);
39+
final RubyMethod methodObject = (RubyMethod) methodNode.execute(frame);
4240

4341
RubyModule module;
4442

@@ -48,33 +46,15 @@ public RubySymbol execute(VirtualFrame frame) {
4846
module = ((RubyBasicObject) receiverObject).getSingletonClass(this);
4947
}
5048

51-
final RubyMethod methodWithDeclaringModule = methodObject.withDeclaringModule(module);
49+
final RubyMethod method = methodObject.withDeclaringModule(module);
5250

53-
if (moduleFunctionFlag(frame)) {
54-
module.addMethod(this, methodWithDeclaringModule.withVisibility(Visibility.PRIVATE));
55-
module.getSingletonClass(this).addMethod(this, methodWithDeclaringModule.withVisibility(Visibility.PUBLIC));
51+
if (method.getVisibility() == Visibility.MODULE_FUNCTION) {
52+
module.addMethod(this, method.withVisibility(Visibility.PRIVATE));
53+
module.getSingletonClass(this).addMethod(this, method.withVisibility(Visibility.PUBLIC));
5654
} else {
57-
module.addMethod(this, methodWithDeclaringModule);
55+
module.addMethod(this, method);
5856
}
5957

6058
return getContext().newSymbol(method.getName());
6159
}
62-
63-
private boolean moduleFunctionFlag(VirtualFrame frame) {
64-
final FrameSlot moduleFunctionFlagSlot = frame.getFrameDescriptor().findFrameSlot(RubyModule.MODULE_FUNCTION_FLAG_FRAME_SLOT_ID);
65-
66-
if (moduleFunctionFlagSlot == null) {
67-
return false;
68-
} else {
69-
Object moduleFunctionObject;
70-
71-
try {
72-
moduleFunctionObject = frame.getObject(moduleFunctionFlagSlot);
73-
} catch (FrameSlotTypeException e) {
74-
throw new RuntimeException(e);
75-
}
76-
77-
return (moduleFunctionObject instanceof Boolean) && (boolean) moduleFunctionObject;
78-
}
79-
}
8060
}

core/src/main/java/org/jruby/truffle/runtime/core/RubyModule.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@ public static void debugModuleChain(RubyModule module) {
8181
*/
8282
public static final Object VISIBILITY_FRAME_SLOT_ID = new Object();
8383

84-
/**
85-
* The slot within a module definition method frame where we store the implicit state that is
86-
* the flag for whether or not new methods will be module methods (functions is the term).
87-
*/
88-
public static final Object MODULE_FUNCTION_FLAG_FRAME_SLOT_ID = new Object();
89-
9084
// The context is stored here - objects can obtain it via their class (which is a module)
9185
private final RubyContext context;
9286

@@ -388,6 +382,8 @@ public void visibilityMethod(RubyNode currentNode, Object[] arguments, Visibilit
388382

389383
if (arg instanceof RubySymbol) {
390384
methodName = ((RubySymbol) arg).toString();
385+
} else if (arg instanceof RubyString) {
386+
methodName = ((RubyString) arg).toString();
391387
} else {
392388
throw new UnsupportedOperationException();
393389
}
@@ -399,12 +395,17 @@ public void visibilityMethod(RubyNode currentNode, Object[] arguments, Visibilit
399395
}
400396

401397
/*
402-
* If the method was already defined in this class, that's fine {@link addMethod}
403-
* will overwrite it, otherwise we do actually want to add a copy of the method with
404-
* a different visibility to this module.
398+
* If the method was already defined in this class, that's fine
399+
* {@link addMethod} will overwrite it, otherwise we do actually
400+
* want to add a copy of the method with a different visibility
401+
* to this module.
405402
*/
406-
407-
addMethod(currentNode, method.withVisibility(visibility));
403+
if (visibility == Visibility.MODULE_FUNCTION) {
404+
addMethod(currentNode, method.withVisibility(Visibility.PRIVATE));
405+
getSingletonClass(currentNode).addMethod(currentNode, method.withVisibility(Visibility.PUBLIC));
406+
} else {
407+
addMethod(currentNode, method.withVisibility(visibility));
408+
}
408409
}
409410
}
410411
}

core/src/main/java/org/jruby/truffle/runtime/methods/RubyMethod.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.oracle.truffle.api.CallTarget;
1313
import com.oracle.truffle.api.frame.MaterializedFrame;
1414
import com.oracle.truffle.api.nodes.Node;
15+
1516
import org.jruby.runtime.Visibility;
1617
import org.jruby.truffle.runtime.core.RubyClass;
1718
import org.jruby.truffle.runtime.core.RubyModule;
@@ -115,7 +116,7 @@ public boolean isVisibleTo(Node currentNode, RubyClass callerClass) {
115116
return false;
116117

117118
default:
118-
return false;
119+
throw new UnsupportedOperationException(visibility.name());
119120
}
120121
}
121122

core/src/main/java/org/jruby/truffle/translator/TranslatorEnvironment.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ public boolean getNeverAssignInParentScope() {
201201

202202
public void addMethodDeclarationSlots() {
203203
frameDescriptor.addFrameSlot(RubyModule.VISIBILITY_FRAME_SLOT_ID);
204-
frameDescriptor.addFrameSlot(RubyModule.MODULE_FUNCTION_FLAG_FRAME_SLOT_ID);
205204
}
206205

207206
public SharedMethodInfo getSharedMethodInfo() {

0 commit comments

Comments
 (0)