src/jdk/nashorn/internal/objects/NativeJavaImporter.java

Thu, 13 Jun 2013 16:08:35 +0530

author
sundar
date
Thu, 13 Jun 2013 16:08:35 +0530
changeset 344
b0dcc3727fc3
parent 90
5a820fb11814
child 380
80c66d3fd872
permissions
-rw-r--r--

8015355: Array.prototype functions don't honour non-writable length and / or index properties
Reviewed-by: lagergren, hannesw

jlaskey@3 1 /*
jlaskey@7 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
jlaskey@3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jlaskey@3 4 *
jlaskey@3 5 * This code is free software; you can redistribute it and/or modify it
jlaskey@3 6 * under the terms of the GNU General Public License version 2 only, as
jlaskey@3 7 * published by the Free Software Foundation. Oracle designates this
jlaskey@3 8 * particular file as subject to the "Classpath" exception as provided
jlaskey@3 9 * by Oracle in the LICENSE file that accompanied this code.
jlaskey@3 10 *
jlaskey@3 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jlaskey@3 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jlaskey@3 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jlaskey@3 14 * version 2 for more details (a copy is included in the LICENSE file that
jlaskey@3 15 * accompanied this code).
jlaskey@3 16 *
jlaskey@3 17 * You should have received a copy of the GNU General Public License version
jlaskey@3 18 * 2 along with this work; if not, write to the Free Software Foundation,
jlaskey@3 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jlaskey@3 20 *
jlaskey@3 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jlaskey@3 22 * or visit www.oracle.com if you need additional information or have any
jlaskey@3 23 * questions.
jlaskey@3 24 */
jlaskey@3 25
jlaskey@3 26 package jdk.nashorn.internal.objects;
jlaskey@3 27
attila@90 28 import jdk.internal.dynalink.CallSiteDescriptor;
attila@90 29 import jdk.internal.dynalink.beans.StaticClass;
attila@90 30 import jdk.internal.dynalink.linker.GuardedInvocation;
attila@90 31 import jdk.internal.dynalink.linker.LinkRequest;
jlaskey@3 32 import jdk.nashorn.internal.objects.annotations.Attribute;
jlaskey@3 33 import jdk.nashorn.internal.objects.annotations.Constructor;
jlaskey@3 34 import jdk.nashorn.internal.objects.annotations.Function;
jlaskey@3 35 import jdk.nashorn.internal.objects.annotations.ScriptClass;
jlaskey@3 36 import jdk.nashorn.internal.runtime.NativeJavaPackage;
jlaskey@3 37 import jdk.nashorn.internal.runtime.ScriptObject;
jlaskey@3 38
jlaskey@3 39 /**
jlaskey@3 40 * This is "JavaImporter" constructor. This constructor allows you to use Java types omitting explicit package names.
jlaskey@3 41 * Objects of this constructor are used along with {@code "with"} statements and as such are not usable in ECMAScript
jlaskey@3 42 * strict mode. Example:
jlaskey@3 43 * <pre>
jlaskey@3 44 * var imports = new JavaImporter(java.util, java.io);
jlaskey@3 45 * with (imports) {
jlaskey@3 46 * var m = new HashMap(); // java.util.HashMap
jlaskey@3 47 * var f = new File("."); // java.io.File
jlaskey@3 48 * ...
jlaskey@3 49 * }
jlaskey@3 50 * </pre>
jlaskey@3 51 * Note however that the preferred way for accessing Java types in Nashorn is through the use of
jlaskey@3 52 * {@link NativeJava#type(Object, Object) Java.type()} method.
jlaskey@3 53 */
jlaskey@3 54 @ScriptClass("JavaImporter")
sundar@82 55 public final class NativeJavaImporter extends ScriptObject {
jlaskey@3 56 private final Object[] args;
jlaskey@3 57
jlaskey@3 58 NativeJavaImporter(final Object[] args) {
jlaskey@3 59 this.args = args;
jlaskey@3 60 this.setProto(Global.instance().getJavaImporterPrototype());
jlaskey@3 61 }
jlaskey@3 62
jlaskey@3 63 @Override
jlaskey@3 64 public String getClassName() {
jlaskey@3 65 return "JavaImporter";
jlaskey@3 66 }
jlaskey@3 67
jlaskey@3 68 /**
jlaskey@3 69 * Constructor
jlaskey@3 70 * @param isNew is the new operator used for instantiating this NativeJavaImporter
jlaskey@3 71 * @param self self reference
jlaskey@3 72 * @param args arguments
jlaskey@3 73 * @return NativeJavaImporter instance
jlaskey@3 74 */
jlaskey@3 75 @Constructor(arity = 1)
jlaskey@3 76 public static Object constructor(final boolean isNew, final Object self, final Object... args) {
jlaskey@3 77 return new NativeJavaImporter(args);
jlaskey@3 78 }
jlaskey@3 79
jlaskey@3 80 /**
jlaskey@3 81 * "No such property" call placeholder.
jlaskey@3 82 *
jlaskey@3 83 * This can never be called as we override {@link ScriptObject#noSuchProperty}. We do declare it here as it's a signal
jlaskey@3 84 * to {@link jdk.nashorn.internal.runtime.WithObject} that it's worth trying doing a {@code noSuchProperty} on this object.
jlaskey@3 85 *
jlaskey@3 86 * @param self self reference
jlaskey@3 87 * @param name property name
jlaskey@3 88 * @return never returns
jlaskey@3 89 */
jlaskey@3 90 @Function(attributes = Attribute.NOT_ENUMERABLE)
jlaskey@3 91 public static Object __noSuchProperty__(final Object self, final Object name) {
jlaskey@3 92 throw new AssertionError("__noSuchProperty__ placeholder called");
jlaskey@3 93 }
jlaskey@3 94
jlaskey@3 95 /**
jlaskey@3 96 * "No such method call" placeholder
jlaskey@3 97 *
jlaskey@3 98 * This can never be called as we override {@link ScriptObject#noSuchMethod}. We do declare it here as it's a signal
jlaskey@3 99 * to {@link jdk.nashorn.internal.runtime.WithObject} that it's worth trying doing a noSuchProperty on this object.
jlaskey@3 100 *
jlaskey@3 101 * @param self self reference
jlaskey@3 102 * @param args arguments to method
jlaskey@3 103 * @return never returns
jlaskey@3 104 */
jlaskey@3 105 @Function(attributes = Attribute.NOT_ENUMERABLE)
jlaskey@3 106 public static Object __noSuchMethod__(final Object self, final Object... args) {
jlaskey@3 107 throw new AssertionError("__noSuchMethod__ placeholder called");
jlaskey@3 108 }
jlaskey@3 109
jlaskey@3 110 @Override
hannesw@51 111 public GuardedInvocation noSuchProperty(final CallSiteDescriptor desc, final LinkRequest request) {
hannesw@51 112 return createAndSetProperty(desc) ? super.lookup(desc, request) : super.noSuchProperty(desc, request);
jlaskey@3 113 }
jlaskey@3 114
jlaskey@3 115 @Override
hannesw@51 116 public GuardedInvocation noSuchMethod(final CallSiteDescriptor desc, final LinkRequest request) {
hannesw@51 117 return createAndSetProperty(desc) ? super.lookup(desc, request) : super.noSuchMethod(desc, request);
jlaskey@3 118 }
jlaskey@3 119
jlaskey@3 120 private boolean createAndSetProperty(final CallSiteDescriptor desc) {
jlaskey@3 121 final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
jlaskey@3 122 final Object value = createProperty(name);
jlaskey@3 123 if(value != null) {
sundar@344 124 set(name, value, false);
jlaskey@3 125 return true;
jlaskey@3 126 }
jlaskey@3 127 return false;
jlaskey@3 128 }
jlaskey@3 129
jlaskey@3 130 private Object createProperty(final String name) {
jlaskey@3 131 final int len = args.length;
jlaskey@3 132
jlaskey@3 133 for (int i = len - 1; i > -1; i--) {
jlaskey@3 134 final Object obj = args[i];
jlaskey@3 135
jlaskey@3 136 if (obj instanceof StaticClass) {
jlaskey@3 137 if (((StaticClass)obj).getRepresentedClass().getSimpleName().equals(name)) {
jlaskey@3 138 return obj;
jlaskey@3 139 }
jlaskey@3 140 } else if (obj instanceof NativeJavaPackage) {
jlaskey@3 141 final String pkgName = ((NativeJavaPackage)obj).getName();
jlaskey@3 142 final String fullName = pkgName.isEmpty() ? name : (pkgName + "." + name);
jlaskey@3 143 try {
jlaskey@3 144 return StaticClass.forClass(Class.forName(fullName));
jlaskey@3 145 } catch (final ClassNotFoundException e) {
jlaskey@3 146 // IGNORE
jlaskey@3 147 }
jlaskey@3 148 }
jlaskey@3 149 }
jlaskey@3 150 return null;
jlaskey@3 151 }
jlaskey@3 152 }

mercurial