13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 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.runtime.ECMAErrors.typeError; 29 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; 30 31 import java.lang.invoke.MethodHandles; 32 import java.lang.reflect.Array; 33 import java.util.Collection; 34 import java.util.Deque; 35 import java.util.List; 36 import java.util.Map; 37 import java.util.Queue; 38 import jdk.internal.dynalink.beans.StaticClass; 39 import jdk.internal.dynalink.support.TypeUtilities; 40 import jdk.nashorn.api.scripting.JSObject; 41 import jdk.nashorn.api.scripting.ScriptObjectMirror; 42 import jdk.nashorn.internal.objects.annotations.Attribute; 43 import jdk.nashorn.internal.objects.annotations.Function; 44 import jdk.nashorn.internal.objects.annotations.ScriptClass; 45 import jdk.nashorn.internal.objects.annotations.Where; 46 import jdk.nashorn.internal.runtime.Context; 47 import jdk.nashorn.internal.runtime.JSType; 48 import jdk.nashorn.internal.runtime.ListAdapter; 49 import jdk.nashorn.internal.runtime.PropertyMap; 50 import jdk.nashorn.internal.runtime.ScriptFunction; 51 import jdk.nashorn.internal.runtime.ScriptObject; 52 import jdk.nashorn.internal.runtime.ScriptRuntime; 53 import jdk.nashorn.internal.runtime.linker.Bootstrap; 54 import jdk.nashorn.internal.runtime.linker.JavaAdapterFactory; 55 56 /** 57 * This class is the implementation for the {@code Java} global object exposed to programs running under Nashorn. This 424 return new NativeArray(((Object[])objArray).clone()); 425 } else if (objArray instanceof int[]) { 426 return new NativeArray(((int[])objArray).clone()); 427 } else if (objArray instanceof double[]) { 428 return new NativeArray(((double[])objArray).clone()); 429 } else if (objArray instanceof long[]) { 430 return new NativeArray(((long[])objArray).clone()); 431 } else if (objArray instanceof byte[]) { 432 return new NativeArray(copyArray((byte[])objArray)); 433 } else if (objArray instanceof short[]) { 434 return new NativeArray(copyArray((short[])objArray)); 435 } else if (objArray instanceof char[]) { 436 return new NativeArray(copyArray((char[])objArray)); 437 } else if (objArray instanceof float[]) { 438 return new NativeArray(copyArray((float[])objArray)); 439 } else if (objArray instanceof boolean[]) { 440 return new NativeArray(copyArray((boolean[])objArray)); 441 } 442 443 throw typeError("cant.convert.to.javascript.array", objArray.getClass().getName()); 444 } 445 446 private static int[] copyArray(final byte[] in) { 447 final int[] out = new int[in.length]; 448 for(int i = 0; i < in.length; ++i) { 449 out[i] = in[i]; 450 } 451 return out; 452 } 453 454 private static int[] copyArray(final short[] in) { 455 final int[] out = new int[in.length]; 456 for(int i = 0; i < in.length; ++i) { 457 out[i] = in[i]; 458 } 459 return out; 460 } 461 462 private static int[] copyArray(final char[] in) { 463 final int[] out = new int[in.length]; | 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 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.runtime.ECMAErrors.typeError; 29 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; 30 31 import java.lang.invoke.MethodHandles; 32 import java.lang.reflect.Array; 33 import java.util.ArrayList; 34 import java.util.Collection; 35 import java.util.Collections; 36 import java.util.Deque; 37 import java.util.List; 38 import java.util.Map; 39 import java.util.Queue; 40 import jdk.internal.dynalink.beans.BeansLinker; 41 import jdk.internal.dynalink.beans.StaticClass; 42 import jdk.internal.dynalink.support.TypeUtilities; 43 import jdk.nashorn.api.scripting.JSObject; 44 import jdk.nashorn.api.scripting.ScriptObjectMirror; 45 import jdk.nashorn.internal.objects.annotations.Attribute; 46 import jdk.nashorn.internal.objects.annotations.Function; 47 import jdk.nashorn.internal.objects.annotations.ScriptClass; 48 import jdk.nashorn.internal.objects.annotations.Where; 49 import jdk.nashorn.internal.runtime.Context; 50 import jdk.nashorn.internal.runtime.JSType; 51 import jdk.nashorn.internal.runtime.ListAdapter; 52 import jdk.nashorn.internal.runtime.PropertyMap; 53 import jdk.nashorn.internal.runtime.ScriptFunction; 54 import jdk.nashorn.internal.runtime.ScriptObject; 55 import jdk.nashorn.internal.runtime.ScriptRuntime; 56 import jdk.nashorn.internal.runtime.linker.Bootstrap; 57 import jdk.nashorn.internal.runtime.linker.JavaAdapterFactory; 58 59 /** 60 * This class is the implementation for the {@code Java} global object exposed to programs running under Nashorn. This 427 return new NativeArray(((Object[])objArray).clone()); 428 } else if (objArray instanceof int[]) { 429 return new NativeArray(((int[])objArray).clone()); 430 } else if (objArray instanceof double[]) { 431 return new NativeArray(((double[])objArray).clone()); 432 } else if (objArray instanceof long[]) { 433 return new NativeArray(((long[])objArray).clone()); 434 } else if (objArray instanceof byte[]) { 435 return new NativeArray(copyArray((byte[])objArray)); 436 } else if (objArray instanceof short[]) { 437 return new NativeArray(copyArray((short[])objArray)); 438 } else if (objArray instanceof char[]) { 439 return new NativeArray(copyArray((char[])objArray)); 440 } else if (objArray instanceof float[]) { 441 return new NativeArray(copyArray((float[])objArray)); 442 } else if (objArray instanceof boolean[]) { 443 return new NativeArray(copyArray((boolean[])objArray)); 444 } 445 446 throw typeError("cant.convert.to.javascript.array", objArray.getClass().getName()); 447 } 448 449 /** 450 * Return properties of the given object. Properties also include "method names". 451 * This is meant for source code completion in interactive shells or editors. 452 * 453 * @param object the object whose properties are returned. 454 * @return list of properties 455 */ 456 public static List<String> getProperties(final Object object) { 457 if (object instanceof StaticClass) { 458 // static properties of the given class 459 final Class<?> clazz = ((StaticClass)object).getRepresentedClass(); 460 final ArrayList<String> props = new ArrayList<>(); 461 try { 462 Bootstrap.checkReflectionAccess(clazz, true); 463 // Usually writable properties are a subset as 'write-only' properties are rare 464 props.addAll(BeansLinker.getReadableStaticPropertyNames(clazz)); 465 props.addAll(BeansLinker.getStaticMethodNames(clazz)); 466 } catch (Exception ignored) {} 467 return props; 468 } else if (object instanceof JSObject) { 469 final JSObject jsObj = ((JSObject)object); 470 final ArrayList<String> props = new ArrayList<>(); 471 props.addAll(jsObj.keySet()); 472 return props; 473 } else if (object != null && object != UNDEFINED) { 474 // instance properties of the given object 475 final Class<?> clazz = object.getClass(); 476 final ArrayList<String> props = new ArrayList<>(); 477 try { 478 Bootstrap.checkReflectionAccess(clazz, false); 479 // Usually writable properties are a subset as 'write-only' properties are rare 480 props.addAll(BeansLinker.getReadableInstancePropertyNames(clazz)); 481 props.addAll(BeansLinker.getInstanceMethodNames(clazz)); 482 } catch (Exception ignored) {} 483 return props; 484 } 485 486 // don't know about that object 487 return Collections.<String>emptyList(); 488 } 489 490 private static int[] copyArray(final byte[] in) { 491 final int[] out = new int[in.length]; 492 for(int i = 0; i < in.length; ++i) { 493 out[i] = in[i]; 494 } 495 return out; 496 } 497 498 private static int[] copyArray(final short[] in) { 499 final int[] out = new int[in.length]; 500 for(int i = 0; i < in.length; ++i) { 501 out[i] = in[i]; 502 } 503 return out; 504 } 505 506 private static int[] copyArray(final char[] in) { 507 final int[] out = new int[in.length]; |