package com.oracle.truffle.js.nodes.access;
import java.util.Set;
import com.oracle.truffle.api.dsl.Executed;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.Frame;
import com.oracle.truffle.api.frame.FrameDescriptor;
import com.oracle.truffle.api.frame.FrameSlot;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.instrumentation.InstrumentableNode;
import com.oracle.truffle.api.instrumentation.StandardTags;
import com.oracle.truffle.api.instrumentation.Tag;
import com.oracle.truffle.js.nodes.JavaScriptNode;
import com.oracle.truffle.js.nodes.instrumentation.JSTags;
import com.oracle.truffle.js.nodes.instrumentation.JSTags.WriteVariableTag;
import com.oracle.truffle.js.nodes.instrumentation.NodeObjectDescriptor;
import com.oracle.truffle.js.runtime.JSFrameUtil;
import com.oracle.truffle.js.runtime.SafeInteger;
public abstract class JSWriteFrameSlotNode extends FrameSlotNode.WithDescriptor implements WriteNode {
protected JSWriteFrameSlotNode(FrameSlot frameSlot, FrameDescriptor frameDescriptor) {
super(frameSlot, frameDescriptor);
}
@Override
public boolean hasTag(Class<? extends Tag> tag) {
if (tag == WriteVariableTag.class || tag == StandardTags.WriteVariableTag.class) {
return !JSFrameUtil.isInternal(frameSlot);
} else if (tag == JSTags.InputNodeTag.class) {
return !JSFrameUtil.isInternal(frameSlot);
} else {
return super.hasTag(tag);
}
}
@Override
public Object getNodeObject() {
String name = JSFrameUtil.getPublicName(frameSlot);
NodeObjectDescriptor descriptor = JSTags.createNodeObjectDescriptor("name", name);
descriptor.addProperty(StandardTags.WriteVariableTag.NAME, name);
return descriptor;
}
@Override
public InstrumentableNode materializeInstrumentableNodes(Set<Class<? extends Tag>> materializedTags) {
if (materializedTags.contains(WriteVariableTag.class) || materializedTags.contains(StandardTags.WriteVariableTag.class)) {
if (getRhs() != null && !getRhs().hasSourceSection() && this.hasSourceSection()) {
transferSourceSectionAddExpressionTag(this, getRhs());
}
}
return this;
}
@Override
public abstract JavaScriptNode getRhs();
public abstract Object executeWithFrame(Frame frame, Object value);
public static JSWriteFrameSlotNode create(FrameSlot frameSlot, int frameLevel, int scopeLevel, FrameDescriptor frameDescriptor, FrameSlot[] parentSlots, JavaScriptNode rhs,
boolean hasTemporalDeadZone) {
if (frameLevel == 0 && scopeLevel == 0 && !hasTemporalDeadZone) {
return JSWriteCurrentFrameSlotNodeGen.create(frameSlot, rhs, frameDescriptor);
}
return create(frameSlot, ScopeFrameNode.create(frameLevel, scopeLevel, parentSlots), rhs, frameDescriptor, hasTemporalDeadZone);
}
public static JSWriteFrameSlotNode create(FrameSlot frameSlot, ScopeFrameNode levelFrameNode, JavaScriptNode rhs, FrameDescriptor frameDescriptor, boolean hasTemporalDeadZone) {
return JSWriteScopeFrameSlotNodeGen.create(frameSlot, levelFrameNode, hasTemporalDeadZone ? TemporalDeadZoneCheckNode.create(frameSlot, levelFrameNode, rhs) : rhs, frameDescriptor);
}
@Override
public final boolean isResultAlwaysOfType(Class<?> clazz) {
return getRhs().isResultAlwaysOfType(clazz);
}
}
abstract class JSWriteScopeFrameSlotNode extends JSWriteFrameSlotNode {
@Child @Executed ScopeFrameNode scopeFrameNode;
@Child @Executed JavaScriptNode rhsNode;
protected JSWriteScopeFrameSlotNode(FrameSlot frameSlot, ScopeFrameNode scopeFrameNode, JavaScriptNode rhsNode, FrameDescriptor frameDescriptor) {
super(frameSlot, frameDescriptor);
this.scopeFrameNode = scopeFrameNode;
this.rhsNode = rhsNode;
}
@Specialization(guards = "isBooleanKind(levelFrame)")
protected final boolean doBoolean(Frame levelFrame, boolean value) {
levelFrame.setBoolean(frameSlot, value);
return value;
}
@Specialization(guards = "(isIntegerKind(frame) || isLongKind(frame)) || isDoubleKind(frame)")
protected final int doInteger(Frame frame, int value) {
if (isIntegerKind(frame)) {
frame.setInt(frameSlot, value);
} else if (isLongKind(frame)) {
frame.setLong(frameSlot, value);
} else if (isDoubleKind(frame)) {
frame.setDouble(frameSlot, value);
}
return value;
}
@Specialization(guards = "isLongKind(levelFrame)")
protected final SafeInteger doSafeInteger(Frame levelFrame, SafeInteger value) {
levelFrame.setLong(frameSlot, value.longValue());
return value;
}
@Specialization
protected final long doLong(Frame levelFrame, long value) {
ensureObjectKind(levelFrame);
levelFrame.setObject(frameSlot, value);
return value;
}
@Specialization(guards = "isDoubleKind(levelFrame)", replaces = {"doInteger", "doSafeInteger"})
protected final double doDouble(Frame levelFrame, double value) {
levelFrame.setDouble(frameSlot, value);
return value;
}
@Specialization(replaces = {"doBoolean", "doInteger", "doDouble", "doSafeInteger", "doLong"})
protected final Object doObject(Frame levelFrame, Object value) {
ensureObjectKind(levelFrame);
levelFrame.setObject(frameSlot, value);
return value;
}
@Override
public final Object executeWithFrame(Frame frame, Object value) {
return executeEvaluated(null, frame, value);
}
abstract Object executeEvaluated(VirtualFrame unusedCurrentFrame, Frame levelFrame, Object value);
@Override
public final Object executeWrite(VirtualFrame frame, Object value) {
return executeEvaluated(frame, getLevelFrameNode().executeFrame(frame), value);
}
@Override
public ScopeFrameNode getLevelFrameNode() {
return scopeFrameNode;
}
@Override
public JavaScriptNode getRhs() {
return rhsNode;
}
@Override
protected JavaScriptNode copyUninitialized(Set<Class<? extends Tag>> materializedTags) {
return JSWriteScopeFrameSlotNodeGen.create(frameSlot, getLevelFrameNode(), cloneUninitialized(getRhs(), materializedTags), frameDescriptor);
}
}
abstract class JSWriteCurrentFrameSlotNode extends JSWriteFrameSlotNode {
@Child @Executed JavaScriptNode rhsNode;
protected JSWriteCurrentFrameSlotNode(FrameSlot frameSlot, JavaScriptNode rhsNode, FrameDescriptor frameDescriptor) {
super(frameSlot, frameDescriptor);
this.rhsNode = rhsNode;
}
@Specialization(guards = "isBooleanKind(frame)")
protected final boolean doBoolean(VirtualFrame frame, boolean value) {
frame.setBoolean(frameSlot, value);
return value;
}
@Specialization(guards = "(isIntegerKind(frame) || isLongKind(frame)) || isDoubleKind(frame)")
protected final int doInteger(VirtualFrame frame, int value) {
if (isIntegerKind(frame)) {
frame.setInt(frameSlot, value);
} else if (isLongKind(frame)) {
frame.setLong(frameSlot, value);
} else if (isDoubleKind(frame)) {
frame.setDouble(frameSlot, value);
}
return value;
}
@Specialization(guards = "isLongKind(frame)")
protected final SafeInteger doSafeInteger(VirtualFrame frame, SafeInteger value) {
frame.setLong(frameSlot, value.longValue());
return value;
}
@Specialization
protected final long doLong(VirtualFrame frame, long value) {
ensureObjectKind(frame);
frame.setObject(frameSlot, value);
return value;
}
@Specialization(guards = "isDoubleKind(frame)", replaces = {"doInteger", "doSafeInteger"})
protected final double doDouble(VirtualFrame frame, double value) {
frame.setDouble(frameSlot, value);
return value;
}
@Specialization(replaces = {"doBoolean", "doInteger", "doDouble", "doSafeInteger", "doLong"})
protected final Object doObject(VirtualFrame frame, Object value) {
ensureObjectKind(frame);
frame.setObject(frameSlot, value);
return value;
}
@Override
public final Object executeWithFrame(Frame frame, Object value) {
return executeEvaluated((VirtualFrame) frame, value);
}
abstract Object executeEvaluated(VirtualFrame frame, Object value);
@Override
public final Object executeWrite(VirtualFrame frame, Object value) {
return executeEvaluated(frame, value);
}
@Override
public JavaScriptNode getRhs() {
return rhsNode;
}
@Override
protected JavaScriptNode copyUninitialized(Set<Class<? extends Tag>> materializedTags) {
return JSWriteCurrentFrameSlotNodeGen.create(frameSlot, cloneUninitialized(getRhs(), materializedTags), frameDescriptor);
}
@Override
public ScopeFrameNode getLevelFrameNode() {
return ScopeFrameNode.createCurrent();
}
}