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