1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
\r
3 * The contents of this file are subject to the Netscape Public
\r
4 * License Version 1.1 (the "License"); you may not use this file
\r
5 * except in compliance with the License. You may obtain a copy of
\r
6 * the License at http://www.mozilla.org/NPL/
\r
8 * Software distributed under the License is distributed on an "AS
\r
9 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
\r
10 * implied. See the License for the specific language governing
\r
11 * rights and limitations under the License.
\r
13 * The Original Code is Rhino code, released
\r
16 * The Initial Developer of the Original Code is Netscape
\r
17 * Communications Corporation. Portions created by Netscape are
\r
18 * Copyright (C) 1997-1999 Netscape Communications Corporation. All
\r
26 * Alternatively, the contents of this file may be used under the
\r
27 * terms of the GNU Public License (the "GPL"), in which case the
\r
28 * provisions of the GPL are applicable instead of those above.
\r
29 * If you wish to allow use of your version of this file only
\r
30 * under the terms of the GPL and not to allow others to use your
\r
31 * version of this file under the NPL, indicate your decision by
\r
32 * deleting the provisions above and replace them with the notice
\r
33 * and other provisions required by the GPL. If you do not delete
\r
34 * the provisions above, a recipient may use your version of this
\r
35 * file under either the NPL or the GPL.
\r
38 package org.mozilla.javascript;
\r
40 import java.lang.reflect.Array;
\r
43 * This class reflects Java arrays into the JavaScript environment.
\r
45 * @author Mike Shaver
\r
46 * @see NativeJavaClass
\r
47 * @see NativeJavaObject
\r
48 * @see NativeJavaPackage
\r
51 public class NativeJavaArray extends NativeJavaObject {
\r
53 public String getClassName() {
\r
57 public static NativeJavaArray wrap(Scriptable scope, Object array) {
\r
58 return new NativeJavaArray(scope, array);
\r
61 public Object unwrap() {
\r
65 public NativeJavaArray(Scriptable scope, Object array) {
\r
66 super(scope, null, ScriptRuntime.ObjectClass);
\r
67 Class cl = array.getClass();
\r
68 if (!cl.isArray()) {
\r
69 throw new RuntimeException("Array expected");
\r
72 this.length = Array.getLength(array);
\r
73 this.cls = cl.getComponentType();
\r
76 public boolean has(String id, Scriptable start) {
\r
77 return id.equals("length") || super.has(id, start);
\r
80 public boolean has(int index, Scriptable start) {
\r
81 return 0 <= index && index < length;
\r
84 public Object get(String id, Scriptable start) {
\r
85 if (id.equals("length"))
\r
86 return new Integer(length);
\r
87 Object result = super.get(id, start);
\r
88 if (result == NOT_FOUND &&
\r
89 !ScriptRuntime.hasProp(getPrototype(), id))
\r
91 throw Context.reportRuntimeError2(
\r
92 "msg.java.member.not.found", array.getClass().getName(), id);
\r
97 public Object get(int index, Scriptable start) {
\r
98 if (0 <= index && index < length)
\r
99 return NativeJavaObject.wrap(this, Array.get(array, index), cls);
\r
100 return Undefined.instance;
\r
103 public void put(String id, Scriptable start, Object value) {
\r
104 // Ignore assignments to "length"--it's readonly.
\r
105 if (!id.equals("length"))
\r
106 super.put(id, start, value);
\r
109 public void put(int index, Scriptable start, Object value) {
\r
110 if (0 <= index && index < length) {
\r
111 Array.set(array, index, NativeJavaObject.coerceType(cls, value));
\r
114 super.put(index, start, value);
\r
117 public Object getDefaultValue(Class hint) {
\r
118 if (hint == null || hint == ScriptRuntime.StringClass)
\r
119 return array.toString();
\r
120 if (hint == ScriptRuntime.BooleanClass)
\r
121 return Boolean.TRUE;
\r
122 if (hint == ScriptRuntime.NumberClass)
\r
123 return ScriptRuntime.NaNobj;
\r
127 public Object[] getIds() {
\r
128 Object[] result = new Object[length];
\r
131 result[i] = new Integer(i);
\r
135 public boolean hasInstance(Scriptable value) {
\r
136 if (!(value instanceof Wrapper))
\r
138 Object instance = ((Wrapper)value).unwrap();
\r
139 return cls.isInstance(instance);
\r
142 public Scriptable getPrototype() {
\r
143 if (prototype == null) {
\r
145 ScriptableObject.getClassPrototype(this.getParentScope(),
\r
154 Scriptable prototype;
\r