package org.jruby.ir.targets;
import com.headius.invokebinder.Binder;
import com.headius.invokebinder.SmartBinder;
import org.jruby.RubyRegexp;
import org.jruby.RubyString;
import org.jruby.runtime.ThreadContext;
import org.jruby.util.RegexpOptions;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Opcodes;
import java.lang.invoke.CallSite;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import static org.jruby.util.CodegenUtils.p;
import static org.jruby.util.CodegenUtils.sig;
public class DRegexpObjectSite extends ConstructObjectSite {
protected final RegexpOptions options;
private volatile RubyRegexp cache;
private static final AtomicReferenceFieldUpdater CACHE_UPDATER = AtomicReferenceFieldUpdater.newUpdater(DRegexpObjectSite.class, RubyRegexp.class, "cache");
public DRegexpObjectSite(MethodType type, int embeddedOptions) {
super(type);
options = RegexpOptions.fromEmbeddedOptions(embeddedOptions);
}
public static final Handle BOOTSTRAP = new Handle(
Opcodes.H_INVOKESTATIC,
p(DRegexpObjectSite.class),
"bootstrap",
sig(CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class, int.class),
false);
public static CallSite bootstrap(MethodHandles.Lookup lookup, String name, MethodType type, int options) {
return new DRegexpObjectSite(type, options).bootstrap(lookup);
}
@Override
public Binder prepareBinder() {
String[] argNames = new String[type().parameterCount()];
Class[] argTypes = new Class[argNames.length];
argNames[0] = "context";
argTypes[0] = ThreadContext.class;
for (int i = 1; i < argNames.length; i++) {
argNames[i] = "part" + i;
argTypes[i] = RubyString.class;
}
return SmartBinder
.from(RubyRegexp.class, argNames, argTypes)
.collect("parts", "part.*")
.binder();
}
@Override
public String initialTarget() {
if (options.isOnce()) return "constructOnce";
return super.initialTarget();
}
public RubyRegexp construct(ThreadContext context, RubyString[] pieces) {
RubyString pattern = RubyRegexp.preprocessDRegexp(context, options, pieces);
RubyRegexp re = RubyRegexp.newDRegexp(context.runtime, pattern, options);
re.setLiteral();
return re;
}
public RubyRegexp constructOnce(ThreadContext context, RubyString[] pieces) {
RubyRegexp re = construct(context, pieces);
if (CACHE_UPDATER.compareAndSet(this, null, re)) {
setTarget(Binder.from(type()).dropAll().constant(re));
return re;
}
return this.cache;
}
}