package com.oracle.truffle.js.builtins;
import com.oracle.truffle.api.Assumption;
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.js.nodes.cast.JSToStringNode;
import com.oracle.truffle.js.nodes.function.JSBuiltin;
import com.oracle.truffle.js.nodes.function.JSBuiltinNode;
import com.oracle.truffle.js.runtime.Boundaries;
import com.oracle.truffle.js.runtime.Errors;
import com.oracle.truffle.js.runtime.JSContext;
import com.oracle.truffle.js.runtime.JSFrameUtil;
import com.oracle.truffle.js.runtime.JSRealm;
import com.oracle.truffle.js.runtime.builtins.BuiltinEnum;
import com.oracle.truffle.js.runtime.builtins.JSRegExp;
import com.oracle.truffle.js.runtime.objects.Undefined;
import com.oracle.truffle.js.runtime.util.TRegexUtil;
public final class RegExpBuiltins extends JSBuiltinsContainer.SwitchEnum<RegExpBuiltins.RegExpBuiltin> {
public static final JSBuiltinsContainer BUILTINS = new RegExpBuiltins();
protected RegExpBuiltins() {
super(JSRegExp.CLASS_NAME, RegExpBuiltin.class);
}
public enum RegExpBuiltin implements BuiltinEnum<RegExpBuiltin> {
input,
setInput("set input", 1),
lastMatch,
lastParen,
leftContext,
rightContext,
multiline,
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8,
$9;
private final String key;
private final int length;
RegExpBuiltin() {
this.key = "get " + name();
this.length = 0;
}
RegExpBuiltin(String key, int length) {
this.key = key;
this.length = length;
}
@Override
public String getKey() {
return key;
}
@Override
public int getLength() {
return length;
}
}
@Override
protected Object createNode(JSContext context, JSBuiltin builtin, boolean construct, boolean newTarget, RegExpBuiltin builtinEnum) {
switch (builtinEnum) {
case input:
return RegExpBuiltinsFactory.JSRegExpStaticResultGetInputNodeGen.create(context, builtin, args().createArgumentNodes(context));
case setInput:
return RegExpBuiltinsFactory.JSRegExpStaticResultSetInputNodeGen.create(context, builtin, args().fixedArgs(builtinEnum.getLength()).createArgumentNodes(context));
case lastMatch:
return RegExpBuiltinsFactory.JSRegExpStaticResultGetGroupNodeGen.create(context, builtin, 0, args().createArgumentNodes(context));
case lastParen:
return RegExpBuiltinsFactory.JSRegExpStaticResultLastParenNodeGen.create(context, builtin, args().createArgumentNodes(context));
case leftContext:
return RegExpBuiltinsFactory.JSRegExpStaticResultLeftContextNodeGen.create(context, builtin, args().createArgumentNodes(context));
case rightContext:
return RegExpBuiltinsFactory.JSRegExpStaticResultRightContextNodeGen.create(context, builtin, args().createArgumentNodes(context));
case multiline:
return RegExpBuiltinsFactory.JSRegExpStaticResultMultilineNodeGen.create(context, builtin, args().createArgumentNodes(context));
case $1:
case $2:
case $3:
case $4:
case $5:
case $6:
case $7:
case $8:
case $9:
return RegExpBuiltinsFactory.JSRegExpStaticResultGetGroupNodeGen.create(context, builtin, builtinEnum.name().charAt(1) - '0', args().createArgumentNodes(context));
default:
return null;
}
}
private static void checkStaticRegexResultPropertyGet(JSContext context, Object thisValue) {
CompilerAsserts.partialEvaluationConstant(context);
if (!context.isOptionV8CompatibilityMode()) {
JSRealm realm = context.getRealm();
if (thisValue != realm.getRegExpConstructor() || realm.isRegexResultInvalidated()) {
throw Errors.createTypeError("Static RegExp result properties cannot be used with subclasses of RegExp.");
}
}
}
private static void checkStaticRegexResultPropertySet(JSContext context, Object thisValue) {
CompilerAsserts.partialEvaluationConstant(context);
if (!context.isOptionV8CompatibilityMode()) {
JSRealm realm = context.getRealm();
if (thisValue != realm.getRegExpConstructor()) {
throw Errors.createTypeError("Static RegExp result properties cannot be used with subclasses of RegExp.");
}
}
}
abstract static class GetStaticRegExpResultNode extends Node {
private final JSContext context;
@Child private TRegexUtil.TRegexCompiledRegexAccessor compiledRegexAccessor = TRegexUtil.TRegexCompiledRegexAccessor.create();
GetStaticRegExpResultNode(JSContext context) {
this.context = context;
}
static GetStaticRegExpResultNode create(JSContext context) {
return RegExpBuiltinsFactory.GetStaticRegExpResultNodeGen.create(context);
}
abstract Object execute();
@Specialization
Object get() {
return context.getRealm().getStaticRegexResult(context, compiledRegexAccessor);
}
}
abstract static class JSRegExpStaticResultGetInputNode extends JSBuiltinNode {
JSRegExpStaticResultGetInputNode(JSContext context, JSBuiltin builtin) {
super(context, builtin);
}
@Specialization
String getInputProp(VirtualFrame frame) {
checkStaticRegexResultPropertyGet(getContext(), JSFrameUtil.getThisObj(frame));
return getContext().getRealm().getStaticRegexResultInputString();
}
}
abstract static class JSRegExpStaticResultSetInputNode extends JSBuiltinNode {
@Child private JSToStringNode toStringNode = JSToStringNode.create();
JSRegExpStaticResultSetInputNode(JSContext context, JSBuiltin builtin) {
super(context, builtin);
}
@Specialization
Object setInputProp(VirtualFrame frame, Object val) {
checkStaticRegexResultPropertySet(getContext(), JSFrameUtil.getThisObj(frame));
getContext().getRealm().setStaticRegexResultInputString(toStringNode.executeString(val));
return Undefined.instance;
}
}
abstract static class JSRegExpStaticResultMultilineNode extends JSBuiltinNode {
@Child private TRegexUtil.TRegexCompiledRegexSingleFlagAccessor multilineAccessor = TRegexUtil.TRegexCompiledRegexSingleFlagAccessor.create(TRegexUtil.Props.Flags.MULTILINE);
JSRegExpStaticResultMultilineNode(JSContext context, JSBuiltin builtin) {
super(context, builtin);
}
@Specialization(guards = "getContext().isOptionNashornCompatibilityMode()")
boolean getMultilineLazyNashorn() {
return false;
}
@Specialization(assumptions = "getStaticResultUnusedAssumption()", guards = "!getContext().isOptionNashornCompatibilityMode()")
boolean getMultilineLazy() {
Object compiledRegex = getContext().getRealm().getStaticRegexResultCompiledRegex();
if (compiledRegex != null) {
return multilineAccessor.get(compiledRegex);
} else {
return false;
}
}
@Specialization
boolean getMultilineEager(@Cached("createGetResultNode()") GetStaticRegExpResultNode getResultNode,
@Cached("create()") TRegexUtil.TRegexResultAccessor resultAccessor) {
Object compiledRegex = getContext().getRealm().getStaticRegexResultCompiledRegex();
Object result = getResultNode.execute();
if (!getContext().isOptionNashornCompatibilityMode() && resultAccessor.isMatch(result)) {
return multilineAccessor.get(compiledRegex);
} else {
return false;
}
}
Assumption getStaticResultUnusedAssumption() {
return getContext().getRegExpStaticResultUnusedAssumption();
}
GetStaticRegExpResultNode createGetResultNode() {
return GetStaticRegExpResultNode.create(getContext());
}
}
abstract static class JSRegExpStaticResultPropertyNode extends JSBuiltinNode {
@Child GetStaticRegExpResultNode getResultNode;
@Child TRegexUtil.TRegexCompiledRegexAccessor compiledRegexAccessor;
@Child TRegexUtil.TRegexResultAccessor resultAccessor;
JSRegExpStaticResultPropertyNode(JSContext context, JSBuiltin builtin) {
super(context, builtin);
getResultNode = GetStaticRegExpResultNode.create(context);
compiledRegexAccessor = TRegexUtil.TRegexCompiledRegexAccessor.create();
resultAccessor = TRegexUtil.TRegexResultAccessor.create();
}
String getInput() {
return getContext().getRealm().getStaticRegexResultOriginalInputString();
}
}
abstract static class JSRegExpStaticResultGetGroupNode extends JSRegExpStaticResultPropertyNode {
private final int groupNumber;
JSRegExpStaticResultGetGroupNode(JSContext context, JSBuiltin builtin, int groupNumber) {
super(context, builtin);
assert groupNumber >= 0;
this.groupNumber = groupNumber;
}
@Specialization
String getGroup(VirtualFrame frame) {
checkStaticRegexResultPropertyGet(getContext(), JSFrameUtil.getThisObj(frame));
Object result = getResultNode.execute();
if (resultAccessor.isMatch(result) && compiledRegexAccessor.groupCount(getContext().getRealm().getStaticRegexResultCompiledRegex()) > groupNumber) {
int start = resultAccessor.captureGroupStart(result, groupNumber);
if (start >= 0) {
return Boundaries.substring(getInput(), start, resultAccessor.captureGroupEnd(result, groupNumber));
}
}
return "";
}
}
abstract static class JSRegExpStaticResultLastParenNode extends JSRegExpStaticResultPropertyNode {
JSRegExpStaticResultLastParenNode(JSContext context, JSBuiltin builtin) {
super(context, builtin);
}
@Specialization
String lastParen(VirtualFrame frame) {
checkStaticRegexResultPropertyGet(getContext(), JSFrameUtil.getThisObj(frame));
Object result = getResultNode.execute();
if (resultAccessor.isMatch(result)) {
int groupNumber = compiledRegexAccessor.groupCount(getContext().getRealm().getStaticRegexResultCompiledRegex()) - 1;
if (groupNumber > 0) {
int start = resultAccessor.captureGroupStart(result, groupNumber);
if (start >= 0) {
return Boundaries.substring(getInput(), start, resultAccessor.captureGroupEnd(result, groupNumber));
}
}
}
return "";
}
}
abstract static class JSRegExpStaticResultLeftContextNode extends JSRegExpStaticResultPropertyNode {
JSRegExpStaticResultLeftContextNode(JSContext context, JSBuiltin builtin) {
super(context, builtin);
}
@Specialization
String leftContext(VirtualFrame frame) {
checkStaticRegexResultPropertyGet(getContext(), JSFrameUtil.getThisObj(frame));
Object result = getResultNode.execute();
if (resultAccessor.isMatch(result)) {
int start = resultAccessor.captureGroupStart(result, 0);
return Boundaries.substring(getInput(), 0, start);
} else {
return "";
}
}
}
abstract static class JSRegExpStaticResultRightContextNode extends JSRegExpStaticResultPropertyNode {
JSRegExpStaticResultRightContextNode(JSContext context, JSBuiltin builtin) {
super(context, builtin);
}
@Specialization
String rightContext(VirtualFrame frame) {
checkStaticRegexResultPropertyGet(getContext(), JSFrameUtil.getThisObj(frame));
Object result = getResultNode.execute();
if (resultAccessor.isMatch(result)) {
int end = resultAccessor.captureGroupEnd(result, 0);
return Boundaries.substring(getInput(), end);
} else {
return "";
}
}
}
}