package com.oracle.truffle.js.nodes.access;
import java.util.Set;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.ImportStatic;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.instrumentation.Tag;
import com.oracle.truffle.api.object.Shape;
import com.oracle.truffle.js.nodes.JavaScriptBaseNode;
import com.oracle.truffle.js.nodes.JavaScriptNode;
import com.oracle.truffle.js.nodes.access.IsArrayNodeGen.IsArrayWrappedNodeGen;
import com.oracle.truffle.js.nodes.unary.JSIsArrayNode;
import com.oracle.truffle.js.nodes.unary.JSUnaryNode;
import com.oracle.truffle.js.runtime.builtins.JSArgumentsArray;
import com.oracle.truffle.js.runtime.builtins.JSArray;
import com.oracle.truffle.js.runtime.builtins.JSArrayBufferView;
import com.oracle.truffle.js.runtime.builtins.JSClass;
import com.oracle.truffle.js.runtime.objects.JSDynamicObject;
import com.oracle.truffle.js.runtime.objects.JSObject;
@ImportStatic(value = {IsArrayNode.Kind.class})
public abstract class IsArrayNode extends JavaScriptBaseNode {
protected static final int MAX_SHAPE_COUNT = 1;
protected static final int MAX_JSCLASS_COUNT = 1;
final Kind kind;
protected enum Kind {
FastOrTypedArray,
FastArray,
Array,
AnyArray,
}
protected IsArrayNode(Kind kind) {
this.kind = kind;
}
public abstract boolean execute(Object operand);
@Specialization(guards = {"kind == Array", "isJSArray(object)"})
protected static boolean doJSArray(@SuppressWarnings("unused") Object object) {
return true;
}
@SuppressWarnings("unused")
@Specialization(guards = "cachedShape.check(object)", limit = "MAX_SHAPE_COUNT")
protected static boolean doIsArrayShape(JSDynamicObject object,
@Cached("object.getShape()") Shape cachedShape,
@Cached("isArray(object)") boolean cachedResult) {
return cachedResult;
}
@SuppressWarnings("unused")
@Specialization(replaces = "doIsArrayShape", guards = {"cachedClass != null", "cachedClass.isInstance(object)"}, limit = "MAX_JSCLASS_COUNT")
protected static boolean doIsArrayJSClass(JSDynamicObject object,
@Cached("isArray(object)") boolean cachedResult,
@Cached("getJSClassChecked(object)") JSClass cachedClass) {
return cachedResult;
}
@Specialization(replaces = "doIsArrayJSClass")
protected final boolean isArray(JSDynamicObject object) {
if (kind == Kind.FastOrTypedArray) {
return JSArray.isJSFastArray(object) || JSArgumentsArray.isJSFastArgumentsObject(object) || JSArrayBufferView.isJSArrayBufferView(object);
} else if (kind == Kind.FastArray) {
return JSArray.isJSFastArray(object);
} else if (kind == Kind.Array) {
return JSArray.isJSArray(object);
} else {
assert kind == Kind.AnyArray;
return JSObject.hasArray(object);
}
}
@Specialization(guards = "!isJSDynamicObject(object)")
protected static boolean isNotDynamicObject(@SuppressWarnings("unused") Object object) {
return false;
}
public static IsArrayNode createIsAnyArray() {
return IsArrayNodeGen.create(Kind.AnyArray);
}
public static IsArrayNode createIsArray() {
return IsArrayNodeGen.create(Kind.Array);
}
public static IsArrayNode createIsFastArray() {
return IsArrayNodeGen.create(Kind.FastArray);
}
public static IsArrayNode createIsFastOrTypedArray() {
return IsArrayNodeGen.create(Kind.FastOrTypedArray);
}
public abstract static class IsArrayWrappedNode extends JSUnaryNode {
@Child private IsArrayNode isArrayNode;
protected IsArrayWrappedNode(JavaScriptNode operandNode, IsArrayNode isArrayNode) {
super(operandNode);
this.isArrayNode = isArrayNode;
}
@Specialization
protected boolean doObject(Object operand) {
return isArrayNode.execute(operand);
}
public static JavaScriptNode createIsArray(JavaScriptNode operand) {
return IsArrayWrappedNodeGen.create(operand, IsArrayNode.createIsArray());
}
@Override
protected JavaScriptNode copyUninitialized(Set<Class<? extends Tag>> materializedTags) {
return createIsArray(cloneUninitialized(getOperand(), materializedTags));
}
}
}