package com.oracle.truffle.js.runtime.objects;
import com.oracle.truffle.api.Assumption;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.object.HiddenKey;
import com.oracle.truffle.api.object.Property;
import com.oracle.truffle.api.object.Shape;
import com.oracle.truffle.js.runtime.JSConfig;
import com.oracle.truffle.js.runtime.JSContext;
import com.oracle.truffle.js.runtime.JSRuntime;
import com.oracle.truffle.js.runtime.builtins.JSClass;
import com.oracle.truffle.js.runtime.builtins.JSDictionary;
import com.oracle.truffle.js.runtime.builtins.JSOrdinary;
import com.oracle.truffle.js.runtime.util.UnmodifiableArrayList;
import com.oracle.truffle.js.runtime.util.UnmodifiablePropertyKeyList;
public final class JSShape {
public static final int NOT_EXTENSIBLE_FLAG = 1 << 0;
public static final int SEALED_FLAG = 1 << 1;
public static final int FROZEN_FLAG = 1 << 2;
public static final int SEALED_FLAGS = NOT_EXTENSIBLE_FLAG | SEALED_FLAG;
public static final int FROZEN_FLAGS = NOT_EXTENSIBLE_FLAG | SEALED_FLAG | FROZEN_FLAG;
public static final int EXTERNAL_PROPERTIES_FLAG = 1 << 3;
private JSShape() {
}
public static Shape createPrototypeShape(JSContext context, JSClass jsclass, DynamicObject prototype) {
assert prototype == Null.instance || JSRuntime.isObject(prototype);
if (context.isMultiContext()) {
return JSObjectUtil.getProtoChildShape(null, jsclass, context);
} else {
return prototype == Null.instance ? context.getEmptyShapeNullPrototype() : JSObjectUtil.getProtoChildShape(prototype, jsclass, context);
}
}
static Shape createObjectShape(JSContext context, JSClass jsclass, DynamicObject prototype) {
Shape rootShape = newBuilder(context, jsclass, prototype).build();
return Shape.newBuilder(rootShape).addConstantProperty(JSObject.HIDDEN_PROTO, prototype, 0).build();
}
public static JSClass getJSClass(Shape shape) {
return (JSClass) shape.getDynamicType();
}
public static Object getJSClassNoCast(Shape shape) {
return shape.getDynamicType();
}
public static JSSharedData getSharedData(Shape shape) {
return (JSSharedData) shape.getSharedData();
}
public static Shape getProtoChildTree(DynamicObject prototype, JSClass jsclass) {
JSPrototypeData prototypeData = JSObjectUtil.getPrototypeData(prototype);
if (prototypeData != null) {
return prototypeData.getProtoChildTree(jsclass);
}
return null;
}
public static boolean isExtensible(Shape shape) {
return (shape.getFlags() & NOT_EXTENSIBLE_FLAG) == 0;
}
public static boolean isPrototypeInShape(Shape shape) {
return getPrototypeProperty(shape).getLocation().isConstant();
}
public static Property getPrototypeProperty(Shape shape) {
return shape.getProperty(JSObject.HIDDEN_PROTO);
}
public static Assumption getPropertyAssumption(Shape shape, Object key) {
return getPropertyAssumption(shape, key, false);
}
public static Assumption getPropertyAssumption(Shape shape, Object key, boolean prototype) {
assert JSRuntime.isPropertyKey(key) || key instanceof HiddenKey;
if (prototype && JSConfig.LeafShapeAssumption) {
return shape.getLeafAssumption();
}
return shape.getPropertyAssumption(key);
}
public static JSContext getJSContext(Shape shape) {
return getSharedData(shape).getContext();
}
public static Assumption getPrototypeAssumption(Shape shape) {
return getSharedData(shape).getPrototypeAssumption();
}
public static void invalidatePrototypeAssumption(Shape shape) {
getSharedData(shape).invalidatePrototypeAssumption();
}
public static UnmodifiableArrayList<Property> getProperties(Shape shape) {
assert JSConfig.FastOwnKeys;
return JSShapeData.getProperties(shape);
}
public static <T> UnmodifiablePropertyKeyList<T> getPropertyKeyList(Shape shape, boolean strings, boolean symbols) {
assert JSConfig.FastOwnKeys;
return JSShapeData.getPropertyKeyList(shape, strings, symbols);
}
public static UnmodifiableArrayList<String> getEnumerablePropertyNames(Shape shape) {
assert JSConfig.FastOwnKeys;
return JSShapeData.getEnumerablePropertyNames(shape);
}
public static UnmodifiableArrayList<Property> getPropertiesIfHasEnumerablePropertyNames(Shape shape) {
assert JSConfig.FastOwnKeys;
return JSShapeData.getPropertiesIfHasEnumerablePropertyNames(shape);
}
public static Shape makeStaticRoot(JSClass jsclass) {
return Shape.newBuilder().layout(getLayout(jsclass)).dynamicType(jsclass).build();
}
public static Shape makeEmptyRoot(JSClass jsclass, JSContext context) {
return createObjectShape(context, jsclass, Null.instance);
}
public static Shape createRootWithNullProto(JSContext context, JSClass jsclass) {
return createObjectShape(context, jsclass, Null.instance);
}
public static Shape createRootWithProto(JSContext context, JSClass jsclass, JSDynamicObject prototype) {
return createObjectShape(context, jsclass, prototype);
}
public static Shape makeEmptyRootWithInstanceProto(JSContext context, JSClass jsclass) {
return newBuilder(context, jsclass, null).build();
}
public static JSSharedData makeJSSharedData(JSContext context, JSDynamicObject proto) {
return new JSSharedData(context, proto);
}
public static Class<? extends DynamicObject> getLayout(JSClass jsclass) {
if (jsclass == JSOrdinary.INSTANCE || jsclass == JSDictionary.INSTANCE) {
return JSOrdinaryObject.DefaultLayout.class;
}
return JSDynamicObject.class;
}
public static Shape.Builder newBuilder(JSContext context, JSClass jsclass, DynamicObject proto) {
assert !context.isMultiContext() || (proto == null || proto == Null.instance);
return Shape.newBuilder().
layout(getLayout(jsclass)).
dynamicType(jsclass).
sharedData(JSShape.makeJSSharedData(context, (JSDynamicObject) proto)).
shapeFlags(getDefaultShapeFlags(jsclass)).
allowImplicitCastIntToDouble(true);
}
public static int getDefaultShapeFlags(JSClass jsclass) {
if (jsclass == JSDictionary.INSTANCE) {
return EXTERNAL_PROPERTIES_FLAG;
}
return 0;
}
public static boolean hasExternalProperties(int shapeFlags) {
return (shapeFlags & EXTERNAL_PROPERTIES_FLAG) != 0;
}
}