package jdk.nashorn.internal.objects;
import static jdk.nashorn.internal.lookup.Lookup.MH;
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import jdk.dynalink.linker.GuardedInvocation;
import jdk.dynalink.linker.LinkRequest;
import jdk.nashorn.internal.WeakValueCache;
import jdk.nashorn.internal.objects.annotations.Attribute;
import jdk.nashorn.internal.objects.annotations.Constructor;
import jdk.nashorn.internal.objects.annotations.Function;
import jdk.nashorn.internal.objects.annotations.Getter;
import jdk.nashorn.internal.objects.annotations.Property;
import jdk.nashorn.internal.objects.annotations.ScriptClass;
import jdk.nashorn.internal.objects.annotations.Where;
import jdk.nashorn.internal.runtime.JSType;
import jdk.nashorn.internal.runtime.PropertyMap;
import jdk.nashorn.internal.runtime.ScriptObject;
import jdk.nashorn.internal.runtime.ScriptRuntime;
import jdk.nashorn.internal.runtime.Symbol;
import jdk.nashorn.internal.runtime.Undefined;
import jdk.nashorn.internal.runtime.linker.PrimitiveLookup;
@ScriptClass("Symbol")
public final class NativeSymbol extends ScriptObject {
private final Symbol symbol;
static final MethodHandle WRAPFILTER = findOwnMH("wrapFilter", MH.type(NativeSymbol.class, Object.class));
private static final MethodHandle PROTOFILTER = findOwnMH("protoFilter", MH.type(Object.class, Object.class));
private static PropertyMap $nasgenmap$;
private static WeakValueCache<String, Symbol> globalSymbolRegistry = new WeakValueCache<>();
@Property(where = Where.CONSTRUCTOR, attributes = Attribute.NON_ENUMERABLE_CONSTANT, name = "iterator")
public static final Symbol iterator = new Symbol("Symbol.iterator");
NativeSymbol(final Symbol symbol) {
this(symbol, Global.instance());
}
NativeSymbol(final Symbol symbol, final Global global) {
this(symbol, global.getSymbolPrototype(), $nasgenmap$);
}
private NativeSymbol(final Symbol symbol, final ScriptObject prototype, final PropertyMap map) {
super(prototype, map);
this.symbol = symbol;
}
private static Symbol getSymbolValue(final Object self) {
if (self instanceof Symbol) {
return (Symbol) self;
} else if (self instanceof NativeSymbol) {
return ((NativeSymbol) self).symbol;
} else {
throw typeError("not.a.symbol", ScriptRuntime.safeToString(self));
}
}
public static GuardedInvocation lookupPrimitive(final LinkRequest request, final Object receiver) {
return PrimitiveLookup.lookupPrimitive(request, Symbol.class, new NativeSymbol((Symbol)receiver), WRAPFILTER, PROTOFILTER);
}
@Override
public Object getDefaultValue(final Class<?> typeHint) {
return symbol;
}
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String toString(final Object self) {
return getSymbolValue(self).toString();
}
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object valueOf(final Object self) {
return getSymbolValue(self);
}
@Constructor(arity = 1)
public static Object constructor(final boolean newObj, final Object self, final Object... args) {
if (newObj) {
throw typeError("symbol.as.constructor");
}
final String description = args.length > 0 && args[0] != Undefined.getUndefined() ?
JSType.toString(args[0]) : "";
return new Symbol(description);
}
@Function(name = "for", attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public synchronized static Object _for(final Object self, final Object arg) {
final String name = JSType.toString(arg);
return globalSymbolRegistry.getOrCreate(name, Symbol::new);
}
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public synchronized static Object keyFor(final Object self, final Object arg) {
if (!(arg instanceof Symbol)) {
throw typeError("not.a.symbol", ScriptRuntime.safeToString(arg));
}
final String name = ((Symbol) arg).getName();
return globalSymbolRegistry.get(name) == arg ? name : Undefined.getUndefined();
}
@SuppressWarnings("unused")
private static NativeSymbol wrapFilter(final Object receiver) {
return new NativeSymbol((Symbol)receiver);
}
@SuppressWarnings("unused")
private static Object protoFilter(final Object object) {
return Global.instance().getSymbolPrototype();
}
private static MethodHandle findOwnMH(final String name, final MethodType type) {
return MH.findStatic(MethodHandles.lookup(), NativeSymbol.class, name, type);
}
}