1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
package io.github.joker1007;
import java.util.stream.Stream;
import org.jruby.anno.JRubyMethod;
import org.jruby.anno.JRubyModule;
import org.jruby.ast.util.ArgsUtil;
import org.jruby.internal.runtime.methods.JavaMethod;
import org.jruby.Ruby;
import org.jruby.RubyBasicObject;
import org.jruby.RubyBinding;
import org.jruby.RubyClass;
import org.jruby.RubyHash;
import org.jruby.RubyMethod;
import org.jruby.RubyModule;
import org.jruby.RubyProc;
import org.jruby.RubySymbol;
import org.jruby.runtime.Block;
import org.jruby.runtime.Constants;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
@JRubyModule(name = "BindingNinja")
public class RubyBindingNinja {
static String OPTIONS_ID = "@auto_inject_binding_options";
static String EXTENSIONS_ID = "@auto_inject_binding_extensions";
static Integer[] jrubyVersionNums = Stream.of(Constants.VERSION.split("\\.")).map(Integer::parseInt).toArray(Integer[]::new);
@JRubyMethod(name = "auto_inject_binding", module = true, visibility = Visibility.PRIVATE, required = 1, optional = 1)
public static IRubyObject autoInjectBinding(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
final Ruby runtime = context.getRuntime();
final IRubyObject extensions = runtime.getModule("BindingNinja").getInstanceVariable(EXTENSIONS_ID);
final IRubyObject methodSym = args[0];
IRubyObject ivar = ((RubyModule)recv).getInstanceVariable(OPTIONS_ID);
final IRubyObject options;
if (ivar.isTrue()) {
options = ivar;
} else {
options = RubyHash.newHash(runtime);
((RubyModule)recv).setInstanceVariable(OPTIONS_ID, options);
}
IRubyObject extModTmp =
extensions instanceof RubyHash ?
((RubyHash)extensions).op_aref(context, recv) :
context.nil;
if (extModTmp.isNil()) {
extModTmp = RubyModule.newModule(runtime);
((RubyHash)extensions).op_aset(context, recv, extModTmp);
}
final RubyModule extMod = (RubyModule)extModTmp;
if (!((RubyModule)recv).hasModuleInHierarchy(extMod)) {
((RubyModule)recv).prependModule(extMod);
}
IRubyObject cond = RubyBasicObject.UNDEF;
if (args.length == 2) {
final RubyHash kwArgs = args[1].convertToHash();
final IRubyObject[] rets = ArgsUtil.extractKeywordArgs(runtime.getCurrentContext(), kwArgs, "if");
cond = rets[0];
}
if (cond != RubyBasicObject.UNDEF) {
((RubyHash)options).op_aset(context, methodSym, cond);
}
final String mid = methodSym.asJavaString();
if (cond == RubyBasicObject.UNDEF) {
extMod.addMethod(mid, autoInjectBindingInvokeWithoutCond(extMod, mid));
} else {
final RubyClass klass = cond.getMetaClass().getRealClass();
if (klass == runtime.getProc() || klass == runtime.getSymbol()) {
extMod.addMethod(mid, autoInjectBindingInvoke(extMod, mid));
} else if (cond.isTrue()) {
extMod.addMethod(mid, autoInjectBindingInvokeWithoutCond(extMod, mid));
} else {
extMod.addMethod(mid, autoInjectBindingInvokeStub(extMod, mid));
}
}
return methodSym;
}
private static JavaMethod autoInjectBindingInvoke(final RubyModule extMod, String name) {
return new JavaMethod(extMod, Visibility.PUBLIC, name) {
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
final IRubyObject options = Helpers.invoke(context, self.getMetaClass(), "auto_inject_binding_options");
IRubyObject cond = ((org.jruby.RubyHash)options).op_aref(context, RubySymbol.newSymbol(context.getRuntime(), name));
final RubyClass klass = cond.getMetaClass().getRealClass();
if (klass == context.getRuntime().getProc()) {
if (Math.abs(((RubyProc)cond).arity().getIntValue()) > 0) {
cond = ((RubyProc)cond).call(context, new IRubyObject[]{self});
} else {
cond = ((RubyProc)cond).call(context, new IRubyObject[]{});
}
} else if (klass == context.getRuntime().getSymbol()) {
final IRubyObject method = ((RubyBasicObject)self).method(cond);
final IRubyObject proc = ((RubyMethod) method).to_proc(context);
cond = ((RubyProc)proc).call(context, new IRubyObject[]{});
}
final IRubyObject[] unshiftedArgs = new IRubyObject[args.length + 1];
if (cond.isTrue()) {
unshiftedArgs[0] = RubyBinding.newBinding(context.getRuntime(), context.currentBinding());
} else {
unshiftedArgs[0] = context.nil;
}
System.arraycopy(args, 0, unshiftedArgs, 1, args.length);
if (jrubyVersionNums[0] >= 9 && jrubyVersionNums[1] >= 2 && jrubyVersionNums[2] >= 7) {
return Helpers.invokeSuper(context, self, clazz, name, unshiftedArgs, block);
} else {
return Helpers.invokeSuper(context, self, extMod, name, unshiftedArgs, block);
}
}
};
}
private static JavaMethod autoInjectBindingInvokeStub(final RubyModule extMod, String name) {
return new JavaMethod(extMod, Visibility.PUBLIC, name) {
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
final IRubyObject[] unshiftedArgs = new IRubyObject[args.length + 1];
unshiftedArgs[0] = context.nil;
System.arraycopy(args, 0, unshiftedArgs, 1, args.length);
if (jrubyVersionNums[0] >= 9 && jrubyVersionNums[1] >= 2 && jrubyVersionNums[2] >= 7) {
return Helpers.invokeSuper(context, self, clazz, name, unshiftedArgs, block);
} else {
return Helpers.invokeSuper(context, self, extMod, name, unshiftedArgs, block);
}
}
};
}
private static JavaMethod autoInjectBindingInvokeWithoutCond(final RubyModule extMod, String name) {
return new JavaMethod(extMod, Visibility.PUBLIC, name) {
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
final IRubyObject[] unshiftedArgs = new IRubyObject[args.length + 1];
unshiftedArgs[0] = RubyBinding.newBinding(context.getRuntime(), context.currentBinding());
System.arraycopy(args, 0, unshiftedArgs, 1, args.length);
if (jrubyVersionNums[0] >= 9 && jrubyVersionNums[1] >= 2 && jrubyVersionNums[2] >= 7) {
return Helpers.invokeSuper(context, self, clazz, name, unshiftedArgs, block);
} else {
return Helpers.invokeSuper(context, self, extMod, name, unshiftedArgs, block);
}
}
};
}
}
|