Skip to content

Instantly share code, notes, and snippets.

@headius
Created January 26, 2023 17:43
Show Gist options
  • Save headius/40a9a59c1d2afa13c1a375984f3eca15 to your computer and use it in GitHub Desktop.
Save headius/40a9a59c1d2afa13c1a375984f3eca15 to your computer and use it in GitHub Desktop.
diff --git a/core/src/main/java/org/jruby/RubyKernel.java b/core/src/main/java/org/jruby/RubyKernel.java
index b930ac552d..fc84726df7 100644
--- a/core/src/main/java/org/jruby/RubyKernel.java
+++ b/core/src/main/java/org/jruby/RubyKernel.java
@@ -2047,7 +2047,7 @@ public class RubyKernel {
return enumeratorizeWithSize(context, self, method, args, sizeFn, keywords);
}
- @JRubyMethod(name = { "__method__", "__callee__" }, module = true, visibility = PRIVATE, reads = METHODNAME, omit = true)
+ @JRubyMethod(name = { "__method__" }, module = true, visibility = PRIVATE, reads = METHODNAME, omit = true)
public static IRubyObject __method__(ThreadContext context, IRubyObject recv) {
String frameName = context.getFrameName();
if (frameName == null || frameName == Ruby.ROOT_FRAME_NAME) {
@@ -2056,6 +2056,20 @@ public class RubyKernel {
return context.runtime.newSymbol(frameName);
}
+ @JRubyMethod(name = { "__callee__" }, module = true, visibility = PRIVATE, reads = METHODNAME, omit = true)
+ public static IRubyObject __callee__(ThreadContext context, IRubyObject recv) {
+ String callee = context.callee;
+ if (callee != null) {
+ return context.runtime.newSymbol(callee);
+ }
+
+ String frameName = context.getFrameName();
+ if (frameName == null || frameName == Ruby.ROOT_FRAME_NAME) {
+ return context.nil;
+ }
+ return context.runtime.newSymbol(frameName);
+ }
+
@JRubyMethod(name = "__dir__", module = true, visibility = PRIVATE, reads = FILENAME)
public static IRubyObject __dir__(ThreadContext context, IRubyObject recv) {
// NOTE: not using __FILE__ = context.getFile() since it won't work with JIT
diff --git a/core/src/main/java/org/jruby/RubyModule.java b/core/src/main/java/org/jruby/RubyModule.java
index e47e41b883..a8f4be54c0 100644
--- a/core/src/main/java/org/jruby/RubyModule.java
+++ b/core/src/main/java/org/jruby/RubyModule.java
@@ -2089,7 +2089,7 @@ public class RubyModule extends RubyObject {
public void putAlias(String id, DynamicMethod method, String oldName) {
if (id.equals(oldName)) return;
- putMethod(getRuntime(), id, new AliasMethod(this, new CacheEntry(method, method.getImplementationClass(), generation), oldName));
+ putMethod(getRuntime(), id, new AliasMethod(this, new CacheEntry(method, method.getImplementationClass(), generation), id, oldName));
if (isRefinement()) {
addRefinedMethodEntry(id, method);
@@ -2106,7 +2106,7 @@ public class RubyModule extends RubyObject {
public void putAlias(String id, CacheEntry entry, String oldName) {
if (id.equals(oldName)) return;
- putMethod(getRuntime(), id, new AliasMethod(this, entry, oldName));
+ putMethod(getRuntime(), id, new AliasMethod(this, entry, id, oldName));
if (isRefinement()) {
addRefinedMethodEntry(id, entry.method);
diff --git a/core/src/main/java/org/jruby/internal/runtime/methods/AliasMethod.java b/core/src/main/java/org/jruby/internal/runtime/methods/AliasMethod.java
index 262cbbe5ec..c01ea05531 100644
--- a/core/src/main/java/org/jruby/internal/runtime/methods/AliasMethod.java
+++ b/core/src/main/java/org/jruby/internal/runtime/methods/AliasMethod.java
@@ -53,16 +53,18 @@ import org.jruby.runtime.callsite.CacheEntry;
public class AliasMethod extends DynamicMethod {
private final CacheEntry entry;
private final boolean findImplementer;
+ private final String newName;
/**
* For some java native methods it is convenient to pass in a String instead
* of a ByteList.
*/
- public AliasMethod(RubyModule implementationClass, CacheEntry entry, String oldName) {
+ public AliasMethod(RubyModule implementationClass, CacheEntry entry, String newName, String oldName) {
super(implementationClass, entry.method.getVisibility(), oldName);
entry.method.getRealMethod().adjustAliasCount(1);
this.entry = entry;
+ this.newName = newName;
boolean findImplementer = true;
@@ -79,56 +81,116 @@ public class AliasMethod extends DynamicMethod {
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule klazz, String unused) {
- return entry.method.call(context, self, calculateSourceModule(self, klazz), name);
+ String callee = context.callee;
+ try {
+ context.callee = newName;
+ return entry.method.call(context, self, calculateSourceModule(self, klazz), name);
+ } finally {
+ context.callee = callee;
+ }
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule klazz, String unused, IRubyObject arg) {
- return entry.method.call(context, self, calculateSourceModule(self, klazz), name, arg);
+ String callee = context.callee;
+ try {
+ context.callee = newName;
+ return entry.method.call(context, self, calculateSourceModule(self, klazz), name, arg);
+ } finally {
+ context.callee = callee;
+ }
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule klazz, String unused, IRubyObject arg1, IRubyObject arg2) {
- return entry.method.call(context, self, calculateSourceModule(self, klazz), name, arg1, arg2);
+ String callee = context.callee;
+ try {
+ context.callee = newName;
+ return entry.method.call(context, self, calculateSourceModule(self, klazz), name, arg1, arg2);
+ } finally {
+ context.callee = callee;
+ }
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule klazz, String unused, IRubyObject arg1, IRubyObject arg2, IRubyObject arg3) {
- return entry.method.call(context, self, calculateSourceModule(self, klazz), name, arg1, arg2, arg3);
+ String callee = context.callee;
+ try {
+ context.callee = newName;
+ return entry.method.call(context, self, calculateSourceModule(self, klazz), name, arg1, arg2, arg3);
+ } finally {
+ context.callee = callee;
+ }
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule klazz, String unused, IRubyObject[] args) {
- return entry.method.call(context, self, calculateSourceModule(self, klazz), name, args);
+ String callee = context.callee;
+ try {
+ context.callee = newName;
+ return entry.method.call(context, self, calculateSourceModule(self, klazz), name, args);
+ } finally {
+ context.callee = callee;
+ }
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule klazz, String unused, Block block) {
- return entry.method.call(context, self, calculateSourceModule(self, klazz), name, block);
+ String callee = context.callee;
+ try {
+ context.callee = newName;
+ return entry.method.call(context, self, calculateSourceModule(self, klazz), name, block);
+ } finally {
+ context.callee = callee;
+ }
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule klazz, String unused, IRubyObject arg1, Block block) {
- return entry.method.call(context, self, calculateSourceModule(self, klazz), name, arg1, block);
+ String callee = context.callee;
+ try {
+ context.callee = newName;
+ return entry.method.call(context, self, calculateSourceModule(self, klazz), name, arg1, block);
+ } finally {
+ context.callee = callee;
+ }
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule klazz, String unused, IRubyObject arg1, IRubyObject arg2, Block block) {
- return entry.method.call(context, self, calculateSourceModule(self, klazz), name, arg1, arg2, block);
+ String callee = context.callee;
+ try {
+ context.callee = newName;
+ return entry.method.call(context, self, calculateSourceModule(self, klazz), name, arg1, arg2, block);
+ } finally {
+ context.callee = callee;
+ }
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule klazz, String unused, IRubyObject arg1, IRubyObject arg2, IRubyObject arg3, Block block) {
- return entry.method.call(context, self, calculateSourceModule(self, klazz), name, arg1, arg2, arg3, block);
+ String callee = context.callee;
+ try {
+ context.callee = newName;
+ return entry.method.call(context, self, calculateSourceModule(self, klazz), name, arg1, arg2, arg3, block);
+ } finally {
+ context.callee = callee;
+ }
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule klazz, String unused, IRubyObject[] args, Block block) {
- return entry.method.call(context, self, calculateSourceModule(self, klazz), name, args, block);
+ String callee = context.callee;
+ try {
+ context.callee = newName;
+ return entry.method.call(context, self, calculateSourceModule(self, klazz), name, args, block);
+ } finally {
+ context.callee = callee;
+ }
}
public DynamicMethod dup() {
- return new AliasMethod(implementationClass, entry, name);
+ return new AliasMethod(implementationClass, entry, newName, name);
}
diff --git a/core/src/main/java/org/jruby/runtime/ThreadContext.java b/core/src/main/java/org/jruby/runtime/ThreadContext.java
index 5708fd2685..83afb8d730 100644
--- a/core/src/main/java/org/jruby/runtime/ThreadContext.java
+++ b/core/src/main/java/org/jruby/runtime/ThreadContext.java
@@ -191,6 +191,8 @@ public final class ThreadContext {
private RubyMatchData matchData;
+ public String callee;
+
@SuppressWarnings("deprecation")
public SecureRandom getSecureRandom() {
SecureRandom secureRandom = this.secureRandom;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment