package com.oracle.truffle.js.nodes.access;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.*;
import com.oracle.truffle.api.instrumentation.Tag;
import com.oracle.truffle.api.object.*;
import com.oracle.truffle.js.nodes.*;
import com.oracle.truffle.js.runtime.*;
import com.oracle.truffle.js.runtime.objects.*;
import java.util.Set;
public abstract class GetTemplateObjectNode extends JavaScriptNode {
protected final JSContext context;
@Child private ArrayLiteralNode rawStrings;
@Child private ArrayLiteralNode cookedStrings;
private final Object identity;
protected GetTemplateObjectNode(JSContext context, ArrayLiteralNode rawStrings, ArrayLiteralNode cookedStrings) {
this.context = context;
this.rawStrings = rawStrings;
this.cookedStrings = cookedStrings;
this.identity = this;
}
protected GetTemplateObjectNode(JSContext context, ArrayLiteralNode rawStrings, ArrayLiteralNode cookedStrings, Object identity) {
this.context = context;
this.rawStrings = rawStrings;
this.cookedStrings = cookedStrings;
this.identity = identity;
}
public static GetTemplateObjectNode create(JSContext context, ArrayLiteralNode rawStrings, ArrayLiteralNode cookedStrings) {
return GetTemplateObjectNodeGen.create(context, rawStrings, cookedStrings);
}
@Specialization(guards = "!context.isMultiContext()", assumptions = "context.getSingleRealmAssumption()")
protected DynamicObject doCached(@SuppressWarnings("unused") VirtualFrame frame,
@Cached("doUncached(frame)") DynamicObject cachedTemplate) {
return cachedTemplate;
}
@Specialization(replaces = "doCached")
protected DynamicObject doUncached(VirtualFrame frame) {
DynamicObject cached = Boundaries.mapGet(context.getRealm().getTemplateRegistry(), identity);
if (cached != null) {
return cached;
}
cached = buildTemplateObject(frame);
Boundaries.mapPut(context.getRealm().getTemplateRegistry(), identity, cached);
return cached;
}
private DynamicObject buildTemplateObject(VirtualFrame frame) {
DynamicObject template = cookedStrings.execute(frame);
DynamicObject rawObj = rawStrings.execute(frame);
JSObject.setIntegrityLevel(rawObj, true);
JSObjectUtil.putDataProperty(context, template, "raw", rawObj, JSAttributes.notConfigurableNotEnumerableNotWritable());
JSObject.setIntegrityLevel(template, true);
return template;
}
@Override
protected JavaScriptNode copyUninitialized(Set<Class<? extends Tag>> materializedTags) {
return GetTemplateObjectNodeGen.create(context, cloneUninitialized(rawStrings, materializedTags), cloneUninitialized(cookedStrings, materializedTags), identity);
}
}