16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nashorn.internal.objects;
27
28 import static jdk.nashorn.internal.lookup.Lookup.MH;
29 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
30 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
31
32 import java.io.IOException;
33 import java.io.PrintWriter;
34 import java.lang.invoke.MethodHandle;
35 import java.lang.invoke.MethodHandles;
36 import java.lang.ref.SoftReference;
37 import java.lang.reflect.Field;
38 import java.util.Arrays;
39 import java.util.LinkedHashMap;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.concurrent.Callable;
43 import java.util.concurrent.ConcurrentHashMap;
44 import jdk.internal.dynalink.linker.GuardedInvocation;
45 import jdk.internal.dynalink.linker.LinkRequest;
46 import jdk.nashorn.internal.objects.annotations.Attribute;
47 import jdk.nashorn.internal.objects.annotations.Property;
48 import jdk.nashorn.internal.objects.annotations.ScriptClass;
49 import jdk.nashorn.internal.runtime.ConsString;
50 import jdk.nashorn.internal.runtime.Context;
51 import jdk.nashorn.internal.runtime.GlobalFunctions;
52 import jdk.nashorn.internal.runtime.GlobalObject;
53 import jdk.nashorn.internal.runtime.JSType;
54 import jdk.nashorn.internal.runtime.NativeJavaPackage;
55 import jdk.nashorn.internal.runtime.PropertyMap;
674 @Override
675 public PropertyDescriptor newAccessorDescriptor(final Object get, final Object set, final boolean configurable, final boolean enumerable) {
676 final AccessorPropertyDescriptor desc = new AccessorPropertyDescriptor(configurable, enumerable, get == null ? UNDEFINED : get, set == null ? UNDEFINED : set, this);
677
678 if (get == null) {
679 desc.delete(PropertyDescriptor.GET, false);
680 }
681
682 if (set == null) {
683 desc.delete(PropertyDescriptor.SET, false);
684 }
685
686 return desc;
687 }
688
689
690 /**
691 * Cache for compiled script classes.
692 */
693 @SuppressWarnings("serial")
694 private static class ClassCache extends LinkedHashMap<Source, SoftReference<Class<?>>> {
695 private final int size;
696
697 ClassCache(int size) {
698 super(size, 0.75f, true);
699 this.size = size;
700 }
701
702 @Override
703 protected boolean removeEldestEntry(final Map.Entry<Source, SoftReference<Class<?>>> eldest) {
704 return size() >= size;
705 }
706 }
707
708 // Class cache management
709 @Override
710 public Class<?> findCachedClass(final Source source) {
711 assert classCache != null : "Class cache used without being initialized";
712 SoftReference<Class<?>> ref = classCache.get(source);
713 if (ref != null) {
714 final Class<?> clazz = ref.get();
715 if (clazz == null) {
716 classCache.remove(source);
717 }
718 return clazz;
719 }
720
721 return null;
722 }
723
724 @Override
725 public void cacheClass(final Source source, final Class<?> clazz) {
726 assert classCache != null : "Class cache used without being initialized";
727 classCache.put(source, new SoftReference<Class<?>>(clazz));
728 }
729
730 private static <T> T getLazilyCreatedValue(final Object key, final Callable<T> creator, final Map<Object, T> map) {
731 final T obj = map.get(key);
732 if (obj != null) {
733 return obj;
734 }
735
736 try {
737 final T newObj = creator.call();
738 final T existingObj = map.putIfAbsent(key, newObj);
739 return existingObj != null ? existingObj : newObj;
740 } catch (final Exception exp) {
741 throw new RuntimeException(exp);
742 }
743 }
744
745 private final Map<Object, InvokeByName> namedInvokers = new ConcurrentHashMap<>();
746
747 @Override
|
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nashorn.internal.objects;
27
28 import static jdk.nashorn.internal.lookup.Lookup.MH;
29 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
30 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
31
32 import java.io.IOException;
33 import java.io.PrintWriter;
34 import java.lang.invoke.MethodHandle;
35 import java.lang.invoke.MethodHandles;
36 import java.lang.ref.ReferenceQueue;
37 import java.lang.ref.SoftReference;
38 import java.lang.reflect.Field;
39 import java.util.Arrays;
40 import java.util.LinkedHashMap;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.concurrent.Callable;
44 import java.util.concurrent.ConcurrentHashMap;
45 import jdk.internal.dynalink.linker.GuardedInvocation;
46 import jdk.internal.dynalink.linker.LinkRequest;
47 import jdk.nashorn.internal.objects.annotations.Attribute;
48 import jdk.nashorn.internal.objects.annotations.Property;
49 import jdk.nashorn.internal.objects.annotations.ScriptClass;
50 import jdk.nashorn.internal.runtime.ConsString;
51 import jdk.nashorn.internal.runtime.Context;
52 import jdk.nashorn.internal.runtime.GlobalFunctions;
53 import jdk.nashorn.internal.runtime.GlobalObject;
54 import jdk.nashorn.internal.runtime.JSType;
55 import jdk.nashorn.internal.runtime.NativeJavaPackage;
56 import jdk.nashorn.internal.runtime.PropertyMap;
675 @Override
676 public PropertyDescriptor newAccessorDescriptor(final Object get, final Object set, final boolean configurable, final boolean enumerable) {
677 final AccessorPropertyDescriptor desc = new AccessorPropertyDescriptor(configurable, enumerable, get == null ? UNDEFINED : get, set == null ? UNDEFINED : set, this);
678
679 if (get == null) {
680 desc.delete(PropertyDescriptor.GET, false);
681 }
682
683 if (set == null) {
684 desc.delete(PropertyDescriptor.SET, false);
685 }
686
687 return desc;
688 }
689
690
691 /**
692 * Cache for compiled script classes.
693 */
694 @SuppressWarnings("serial")
695 private static class ClassCache extends LinkedHashMap<Source, ClassReference> {
696 private final int size;
697 private final ReferenceQueue<Class<?>> queue;
698
699 ClassCache(int size) {
700 super(size, 0.75f, true);
701 this.size = size;
702 this.queue = new ReferenceQueue<>();
703 }
704
705 void cache(final Source source, final Class<?> clazz) {
706 put(source, new ClassReference(clazz, queue, source));
707 }
708
709 @Override
710 protected boolean removeEldestEntry(final Map.Entry<Source, ClassReference> eldest) {
711 return size() >= size;
712 }
713
714 @Override
715 public ClassReference get(Object key) {
716 for (ClassReference ref; (ref = (ClassReference)queue.poll()) != null; ) {
717 remove(ref.source);
718 }
719 return super.get(key);
720 }
721
722 }
723
724 private static class ClassReference extends SoftReference<Class<?>> {
725 private final Source source;
726
727 ClassReference(final Class<?> clazz, final ReferenceQueue<Class<?>> queue, final Source source) {
728 super(clazz, queue);
729 this.source = source;
730 }
731 }
732
733 // Class cache management
734 @Override
735 public Class<?> findCachedClass(final Source source) {
736 assert classCache != null : "Class cache used without being initialized";
737 ClassReference ref = classCache.get(source);
738 return ref != null ? ref.get() : null;
739 }
740
741 @Override
742 public void cacheClass(final Source source, final Class<?> clazz) {
743 assert classCache != null : "Class cache used without being initialized";
744 classCache.cache(source, clazz);
745 }
746
747 private static <T> T getLazilyCreatedValue(final Object key, final Callable<T> creator, final Map<Object, T> map) {
748 final T obj = map.get(key);
749 if (obj != null) {
750 return obj;
751 }
752
753 try {
754 final T newObj = creator.call();
755 final T existingObj = map.putIfAbsent(key, newObj);
756 return existingObj != null ? existingObj : newObj;
757 } catch (final Exception exp) {
758 throw new RuntimeException(exp);
759 }
760 }
761
762 private final Map<Object, InvokeByName> namedInvokers = new ConcurrentHashMap<>();
763
764 @Override
|