2003/05/12 05:10:30
[org.ibex.core.git] / src / org / mozilla / javascript / NativeJavaArray.java
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-\r
2  *\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
7  *\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
12  *\r
13  * The Original Code is Rhino code, released\r
14  * May 6, 1999.\r
15  *\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
19  * Rights Reserved.\r
20  *\r
21  * Contributor(s): \r
22  * Norris Boyd\r
23  * Frank Mitchell\r
24  * Mike Shaver\r
25  *\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
36  */\r
37 \r
38 package org.mozilla.javascript;\r
39 \r
40 import java.lang.reflect.Array;\r
41 \r
42 /**\r
43  * This class reflects Java arrays into the JavaScript environment.\r
44  *\r
45  * @author Mike Shaver\r
46  * @see NativeJavaClass\r
47  * @see NativeJavaObject\r
48  * @see NativeJavaPackage\r
49  */\r
50 \r
51 public class NativeJavaArray extends NativeJavaObject {\r
52 \r
53     public String getClassName() {\r
54         return "JavaArray";\r
55     }\r
56 \r
57     public static NativeJavaArray wrap(Scriptable scope, Object array) {\r
58         return new NativeJavaArray(scope, array);\r
59     }\r
60 \r
61     public Object unwrap() {\r
62         return array;\r
63     }\r
64 \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
70         }\r
71         this.array = array;\r
72         this.length = Array.getLength(array);\r
73         this.cls = cl.getComponentType();\r
74     }\r
75 \r
76     public boolean has(String id, Scriptable start) {\r
77         return id.equals("length") || super.has(id, start);\r
78     }\r
79 \r
80     public boolean has(int index, Scriptable start) {\r
81         return 0 <= index && index < length;\r
82     }\r
83 \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
90         {\r
91             throw Context.reportRuntimeError2(\r
92                 "msg.java.member.not.found", array.getClass().getName(), id);\r
93         }\r
94         return result;  \r
95     }\r
96 \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
101     }\r
102 \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
107     }\r
108     \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
112             return;\r
113         }\r
114         super.put(index, start, value);\r
115     }\r
116 \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
124         return this;\r
125     }\r
126     \r
127     public Object[] getIds() {\r
128         Object[] result = new Object[length];\r
129         int i = length;\r
130         while (--i >= 0)\r
131             result[i] = new Integer(i);\r
132         return result;\r
133     }\r
134 \r
135     public boolean hasInstance(Scriptable value) {\r
136         if (!(value instanceof Wrapper))\r
137             return false;\r
138         Object instance = ((Wrapper)value).unwrap();\r
139         return cls.isInstance(instance);\r
140     }\r
141 \r
142     public Scriptable getPrototype() {\r
143         if (prototype == null) {\r
144             prototype = \r
145                 ScriptableObject.getClassPrototype(this.getParentScope(),\r
146                                                    "Array");\r
147         }\r
148         return prototype;\r
149     }\r
150 \r
151     Object array;\r
152     int length;\r
153     Class cls;\r
154     Scriptable prototype;\r
155 }\r