Skip to content

Instantly share code, notes, and snippets.

@headius
Created March 5, 2025 08:09
Show Gist options
  • Save headius/9fd443527cd3cf29a047318210c3ae1f to your computer and use it in GitHub Desktop.
Save headius/9fd443527cd3cf29a047318210c3ae1f to your computer and use it in GitHub Desktop.
diff --git a/core/src/main/java/org/jruby/RubyBasicObject.java b/core/src/main/java/org/jruby/RubyBasicObject.java
index 4edab9b18a..985f2d1911 100644
--- a/core/src/main/java/org/jruby/RubyBasicObject.java
+++ b/core/src/main/java/org/jruby/RubyBasicObject.java
@@ -1791,8 +1791,8 @@ public class RubyBasicObject implements Cloneable, IRubyObject, Serializable, Co
* it possible to emulate both instance_eval and instance_exec
* with this implementation.
*/
- protected IRubyObject yieldUnder(final ThreadContext context, RubyModule under, IRubyObject[] args, Block block, EvalType evalType) {
- context.preExecuteUnder(this, under, block);
+ protected IRubyObject yieldUnder(final ThreadContext context, IRubyObject[] args, Block block, EvalType evalType) {
+ context.preExecuteUnder(this, block);
IRubyObject savedBindingSelf = block.getBinding().getSelf();
IRubyObject savedFrameSelf = block.getBinding().getFrame().getSelf();
@@ -1838,8 +1838,8 @@ public class RubyBasicObject implements Cloneable, IRubyObject, Serializable, Co
* it possible to emulate both instance_eval and instance_exec
* with this implementation.
*/
- protected IRubyObject yieldUnder(final ThreadContext context, RubyModule under, Block block, EvalType evalType) {
- context.preExecuteUnder(this, under, block);
+ protected IRubyObject yieldUnder(final ThreadContext context, Block block, EvalType evalType) {
+ context.preExecuteUnder(this, block);
try {
return setupBlock(block, evalType).yieldNonArray(context, this, this); //, context.getRubyClass());
@@ -1859,10 +1859,10 @@ public class RubyBasicObject implements Cloneable, IRubyObject, Serializable, Co
* arguments in the args-array is optional, but can contain the
* filename and line of the string under evaluation.
*/
- public IRubyObject specificEval(ThreadContext context, RubyModule mod, Block block, EvalType evalType) {
+ public IRubyObject specificEval(ThreadContext context, Block block, EvalType evalType) {
if (!block.isGiven()) throw argumentError(context, 0, 1, 3);
- return yieldUnder(context, mod, block, evalType);
+ return yieldUnder(context, block, evalType);
}
/** specific_eval
@@ -1875,7 +1875,7 @@ public class RubyBasicObject implements Cloneable, IRubyObject, Serializable, Co
* arguments in the args-array is optional, but can contain the
* filename and line of the string under evaluation.
*/
- public IRubyObject specificEval(ThreadContext context, RubyModule mod, IRubyObject arg, Block block, EvalType evalType) {
+ public IRubyObject specificEval(ThreadContext context, IRubyObject arg, Block block, EvalType evalType) {
if (block.isGiven()) throw argumentError(context, 1, 0);
// We just want the TypeError if the argument doesn't convert to a String (JRUBY-386)
@@ -1884,7 +1884,7 @@ public class RubyBasicObject implements Cloneable, IRubyObject, Serializable, Co
String file = "(eval at " + singleBacktrace.getFileName() + ":" + + singleBacktrace.getLineNumber() + ")";
int line = 0;
- return evalUnder(context, mod, evalStr, file, line, evalType);
+ return evalUnder(context, evalStr, file, line, evalType);
}
/** specific_eval
@@ -1897,7 +1897,7 @@ public class RubyBasicObject implements Cloneable, IRubyObject, Serializable, Co
* arguments in the args-array is optional, but can contain the
* filename and line of the string under evaluation.
*/
- public IRubyObject specificEval(ThreadContext context, RubyModule mod, IRubyObject arg0, IRubyObject arg1, Block block, EvalType evalType) {
+ public IRubyObject specificEval(ThreadContext context, IRubyObject arg0, IRubyObject arg1, Block block, EvalType evalType) {
if (block.isGiven()) throw argumentError(context, 2, 0);
// We just want the TypeError if the argument doesn't convert to a String (JRUBY-386)
@@ -1905,7 +1905,7 @@ public class RubyBasicObject implements Cloneable, IRubyObject, Serializable, Co
String file = arg1.convertToString().asJavaString();
int line = 0;
- return evalUnder(context, mod, evalStr, file, line, evalType);
+ return evalUnder(context, evalStr, file, line, evalType);
}
/** specific_eval
@@ -1918,7 +1918,7 @@ public class RubyBasicObject implements Cloneable, IRubyObject, Serializable, Co
* arguments in the args-array is optional, but can contain the
* filename and line of the string under evaluation.
*/
- public IRubyObject specificEval(ThreadContext context, RubyModule mod, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2, Block block, EvalType evalType) {
+ public IRubyObject specificEval(ThreadContext context, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2, Block block, EvalType evalType) {
if (block.isGiven()) throw argumentError(context, 2, 0);
// We just want the TypeError if the argument doesn't convert to a String (JRUBY-386)
@@ -1926,7 +1926,7 @@ public class RubyBasicObject implements Cloneable, IRubyObject, Serializable, Co
String file = arg1.convertToString().asJavaString();
int line = toInt(context, arg2) - 1;
- return evalUnder(context, mod, evalStr, file, line, evalType);
+ return evalUnder(context, evalStr, file, line, evalType);
}
@Deprecated(since = "10.0")
@@ -1944,8 +1944,8 @@ public class RubyBasicObject implements Cloneable, IRubyObject, Serializable, Co
* Evaluates the string src with self set to the current object,
* using the module under as the context.
*/
- public IRubyObject evalUnder(final ThreadContext context, RubyModule under, RubyString src, String file, int line, EvalType evalType) {
- return Interpreter.evalSimple(context, under, this, src, file, line, evalType);
+ public IRubyObject evalUnder(final ThreadContext context, RubyString src, String file, int line, EvalType evalType) {
+ return Interpreter.evalSimple(context, metaClass, this, src, file, line, evalType);
}
/**
@@ -2604,25 +2604,25 @@ public class RubyBasicObject implements Cloneable, IRubyObject, Serializable, Co
reads = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE},
writes = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE})
public IRubyObject instance_eval(ThreadContext context, Block block) {
- return specificEval(context, getInstanceEvalClass(context), block, EvalType.INSTANCE_EVAL);
+ return specificEval(context, block, EvalType.INSTANCE_EVAL);
}
@JRubyMethod(name = "instance_eval",
reads = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE},
writes = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE})
public IRubyObject instance_eval(ThreadContext context, IRubyObject arg0, Block block) {
- return specificEval(context, getInstanceEvalClass(context), arg0, block, EvalType.INSTANCE_EVAL);
+ return specificEval(context, arg0, block, EvalType.INSTANCE_EVAL);
}
@JRubyMethod(name = "instance_eval",
reads = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE},
writes = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE})
public IRubyObject instance_eval(ThreadContext context, IRubyObject arg0, IRubyObject arg1, Block block) {
- return specificEval(context, getInstanceEvalClass(context), arg0, arg1, block, EvalType.INSTANCE_EVAL);
+ return specificEval(context, arg0, arg1, block, EvalType.INSTANCE_EVAL);
}
@JRubyMethod(name = "instance_eval",
reads = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE},
writes = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE})
public IRubyObject instance_eval(ThreadContext context, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2, Block block) {
- return specificEval(context, getInstanceEvalClass(context), arg0, arg1, arg2, block, EvalType.INSTANCE_EVAL);
+ return specificEval(context, arg0, arg1, arg2, block, EvalType.INSTANCE_EVAL);
}
// This is callable and will work but the rest = true is put so we can match the expected arity error message
@@ -2664,7 +2664,7 @@ public class RubyBasicObject implements Cloneable, IRubyObject, Serializable, Co
public IRubyObject instance_exec(ThreadContext context, IRubyObject[] args, Block block) {
if (!block.isGiven()) throw context.runtime.newLocalJumpErrorNoBlock();
- return yieldUnder(context, getInstanceEvalClass(context), args, block, EvalType.INSTANCE_EVAL);
+ return yieldUnder(context, args, block, EvalType.INSTANCE_EVAL);
}
/** rb_obj_extend
diff --git a/core/src/main/java/org/jruby/RubyModule.java b/core/src/main/java/org/jruby/RubyModule.java
index 43941c554e..02b12dcc90 100644
--- a/core/src/main/java/org/jruby/RubyModule.java
+++ b/core/src/main/java/org/jruby/RubyModule.java
@@ -4196,28 +4196,28 @@ public class RubyModule extends RubyObject {
writes = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE},
keywords = true)
public IRubyObject module_eval(ThreadContext context, Block block) {
- return specificEval(context, this, block, EvalType.MODULE_EVAL);
+ return specificEval(context, block, EvalType.MODULE_EVAL);
}
@JRubyMethod(name = {"module_eval", "class_eval"},
reads = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE},
writes = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE},
keywords = true)
public IRubyObject module_eval(ThreadContext context, IRubyObject arg0, Block block) {
- return specificEval(context, this, arg0, block, EvalType.MODULE_EVAL);
+ return specificEval(context, arg0, block, EvalType.MODULE_EVAL);
}
@JRubyMethod(name = {"module_eval", "class_eval"},
reads = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE},
writes = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE},
keywords = true)
public IRubyObject module_eval(ThreadContext context, IRubyObject arg0, IRubyObject arg1, Block block) {
- return specificEval(context, this, arg0, arg1, block, EvalType.MODULE_EVAL);
+ return specificEval(context, arg0, arg1, block, EvalType.MODULE_EVAL);
}
@JRubyMethod(name = {"module_eval", "class_eval"},
reads = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE},
writes = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE},
keywords = true)
public IRubyObject module_eval(ThreadContext context, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2, Block block) {
- return specificEval(context, this, arg0, arg1, arg2, block, EvalType.MODULE_EVAL);
+ return specificEval(context, arg0, arg1, arg2, block, EvalType.MODULE_EVAL);
}
// This is callable and will work but the rest = true is put so we can match the expected arity error message
@@ -4242,7 +4242,7 @@ public class RubyModule extends RubyObject {
keywords = true)
public IRubyObject module_exec(ThreadContext context, Block block) {
if (block.isGiven()) {
- return yieldUnder(context, this, IRubyObject.NULL_ARRAY, block.cloneBlockAndFrame(), EvalType.MODULE_EVAL);
+ return yieldUnder(context, IRubyObject.NULL_ARRAY, block.cloneBlockAndFrame(), EvalType.MODULE_EVAL);
} else {
throw context.runtime.newLocalJumpErrorNoBlock();
}
@@ -4254,7 +4254,7 @@ public class RubyModule extends RubyObject {
keywords = true)
public IRubyObject module_exec(ThreadContext context, IRubyObject[] args, Block block) {
if (block.isGiven()) {
- return yieldUnder(context, this, args, block.cloneBlockAndFrame(), EvalType.MODULE_EVAL);
+ return yieldUnder(context, args, block.cloneBlockAndFrame(), EvalType.MODULE_EVAL);
} else {
throw context.runtime.newLocalJumpErrorNoBlock();
}
diff --git a/core/src/main/java/org/jruby/RubyObject.java b/core/src/main/java/org/jruby/RubyObject.java
index bcce3c0aa6..0494f1ead9 100644
--- a/core/src/main/java/org/jruby/RubyObject.java
+++ b/core/src/main/java/org/jruby/RubyObject.java
@@ -340,7 +340,7 @@ public class RubyObject extends RubyBasicObject {
if (block.isGiven()) {
if (args.length > 0) throw argumentError(context, args.length, 0);
- return yieldUnder(context, mod, block, evalType);
+ return yieldUnder(context, block, evalType);
}
if (args.length == 0) throw argumentError(context, "block not supplied");
@@ -362,7 +362,7 @@ public class RubyObject extends RubyBasicObject {
line = 0;
}
- return evalUnder(context, mod, evalStr, file, line, evalType);
+ return evalUnder(context, evalStr, file, line, evalType);
}
// Methods of the Object class (rb_obj_*):
diff --git a/core/src/main/java/org/jruby/runtime/ThreadContext.java b/core/src/main/java/org/jruby/runtime/ThreadContext.java
index 1dbc3cadee..65ca2b64d1 100644
--- a/core/src/main/java/org/jruby/runtime/ThreadContext.java
+++ b/core/src/main/java/org/jruby/runtime/ThreadContext.java
@@ -39,6 +39,7 @@ package org.jruby.runtime;
import com.headius.backport9.stack.StackWalker;
import com.headius.backport9.stack.impl.StackWalker8;
import org.jcodings.Encoding;
+import org.jruby.EvalType;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyBoolean;
@@ -1133,13 +1134,14 @@ public final class ThreadContext {
popFrame();
}
- public void preExecuteUnder(IRubyObject executeUnderObj, RubyModule executeUnderClass, Block block) {
+ public void preExecuteUnder(IRubyObject executeUnderObj, Block block) {
Frame frame = getCurrentFrame();
DynamicScope scope = getCurrentScope();
StaticScope sScope = runtime.getStaticScopeFactory().newBlockScope(scope.getStaticScope());
- sScope.setModule(executeUnderClass);
- pushScope(DynamicScope.newDynamicScope(sScope, scope));
+ DynamicScope dynScope = DynamicScope.newDynamicScope(sScope, scope);
+ dynScope.setEvalType(EvalType.INSTANCE_EVAL);
+ pushScope(dynScope);
pushCallFrame(frame.getKlazz(), frame.getName(), executeUnderObj, block);
getCurrentFrame().setVisibility(getPreviousFrame().getVisibility());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment