~ubuntu-branches/ubuntu/karmic/rhino/karmic

« back to all changes in this revision

Viewing changes to src/org/mozilla/javascript/NativeJavaArray.java

  • Committer: Bazaar Package Importer
  • Author(s): Jerry Haltom
  • Date: 2005-03-19 16:56:07 UTC
  • mto: (11.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050319165607-geu3j3fnqlkpqkh1
Tags: upstream-1.6.R1
ImportĀ upstreamĀ versionĀ 1.6.R1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2
 
 *
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/
7
 
 *
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.
12
 
 *
13
 
 * The Original Code is Rhino code, released
14
 
 * May 6, 1999.
15
 
 *
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
19
 
 * Rights Reserved.
20
 
 *
21
 
 * Contributor(s): 
22
 
 * Norris Boyd
23
 
 * Frank Mitchell
24
 
 * Mike Shaver
25
 
 *
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.
36
 
 */
37
 
 
38
 
package org.mozilla.javascript;
39
 
 
40
 
import java.lang.reflect.Array;
41
 
 
42
 
/**
43
 
 * This class reflects Java arrays into the JavaScript environment.
44
 
 *
45
 
 * @author Mike Shaver
46
 
 * @see NativeJavaClass
47
 
 * @see NativeJavaObject
48
 
 * @see NativeJavaPackage
49
 
 */
50
 
 
51
 
public class NativeJavaArray extends NativeJavaObject {
52
 
 
53
 
    public String getClassName() {
54
 
        return "JavaArray";
55
 
    }
56
 
 
57
 
    public static NativeJavaArray wrap(Scriptable scope, Object array) {
58
 
        return new NativeJavaArray(scope, array);
59
 
    }
60
 
 
61
 
    public Object unwrap() {
62
 
        return array;
63
 
    }
64
 
 
65
 
    public NativeJavaArray(Scriptable scope, Object array) {
66
 
        super(scope, null, ScriptRuntime.ObjectClass);
67
 
        Class cl = array.getClass();
68
 
        if (!cl.isArray()) {
69
 
            throw new RuntimeException("Array expected");
70
 
        }
71
 
        this.array = array;
72
 
        this.length = Array.getLength(array);
73
 
        this.cls = cl.getComponentType();
74
 
    }
75
 
 
76
 
    public boolean has(String id, Scriptable start) {
77
 
        return id.equals("length") || super.has(id, start);
78
 
    }
79
 
 
80
 
    public boolean has(int index, Scriptable start) {
81
 
        return 0 <= index && index < length;
82
 
    }
83
 
 
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)) 
90
 
        {
91
 
            throw Context.reportRuntimeError2(
92
 
                "msg.java.member.not.found", array.getClass().getName(), id);
93
 
        }
94
 
        return result;  
95
 
    }
96
 
 
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;
101
 
    }
102
 
 
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);
107
 
    }
108
 
    
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));
112
 
            return;
113
 
        }
114
 
        super.put(index, start, value);
115
 
    }
116
 
 
117
 
    public Object getDefaultValue(Class hint) {
118
 
        if (hint == null || hint == ScriptRuntime.StringClass) 
119
 
            return array.toString();
120
 
        if (hint == ScriptRuntime.BooleanClass)
121
 
            return Boolean.TRUE;
122
 
        if (hint == ScriptRuntime.NumberClass)
123
 
            return ScriptRuntime.NaNobj;
124
 
        return this;
125
 
    }
126
 
    
127
 
    public Object[] getIds() {
128
 
        Object[] result = new Object[length];
129
 
        int i = length;
130
 
        while (--i >= 0)
131
 
            result[i] = new Integer(i);
132
 
        return result;
133
 
    }
134
 
 
135
 
    public boolean hasInstance(Scriptable value) {
136
 
        if (!(value instanceof Wrapper))
137
 
            return false;
138
 
        Object instance = ((Wrapper)value).unwrap();
139
 
        return cls.isInstance(instance);
140
 
    }
141
 
 
142
 
    public Scriptable getPrototype() {
143
 
        if (prototype == null) {
144
 
            prototype = 
145
 
                ScriptableObject.getClassPrototype(this.getParentScope(),
146
 
                                                   "Array");
147
 
        }
148
 
        return prototype;
149
 
    }
150
 
 
151
 
    Object array;
152
 
    int length;
153
 
    Class cls;
154
 
    Scriptable prototype;
155
 
}
 
1
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 
2
 *
 
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/
 
7
 *
 
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.
 
12
 *
 
13
 * The Original Code is Rhino code, released
 
14
 * May 6, 1999.
 
15
 *
 
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
 
19
 * Rights Reserved.
 
20
 *
 
21
 * Contributor(s):
 
22
 * Norris Boyd
 
23
 * Igor Bukanov
 
24
 * Frank Mitchell
 
25
 * Mike Shaver
 
26
 * Kemal Bayram
 
27
 *
 
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.
 
38
 */
 
39
 
 
40
package org.mozilla.javascript;
 
41
 
 
42
import java.lang.reflect.Array;
 
43
import java.io.*;
 
44
 
 
45
/**
 
46
 * This class reflects Java arrays into the JavaScript environment.
 
47
 *
 
48
 * @author Mike Shaver
 
49
 * @see NativeJavaClass
 
50
 * @see NativeJavaObject
 
51
 * @see NativeJavaPackage
 
52
 */
 
53
 
 
54
public class NativeJavaArray extends NativeJavaObject {
 
55
 
 
56
    public String getClassName() {
 
57
        return "JavaArray";
 
58
    }
 
59
 
 
60
    public static NativeJavaArray wrap(Scriptable scope, Object array) {
 
61
        return new NativeJavaArray(scope, array);
 
62
    }
 
63
 
 
64
    public Object unwrap() {
 
65
        return array;
 
66
    }
 
67
 
 
68
    public NativeJavaArray(Scriptable scope, Object array) {
 
69
        super(scope, null, ScriptRuntime.ObjectClass);
 
70
        Class cl = array.getClass();
 
71
        if (!cl.isArray()) {
 
72
            throw new RuntimeException("Array expected");
 
73
        }
 
74
        this.array = array;
 
75
        this.length = Array.getLength(array);
 
76
        this.cls = cl.getComponentType();
 
77
    }
 
78
 
 
79
    public boolean has(String id, Scriptable start) {
 
80
        return id.equals("length") || super.has(id, start);
 
81
    }
 
82
 
 
83
    public boolean has(int index, Scriptable start) {
 
84
        return 0 <= index && index < length;
 
85
    }
 
86
 
 
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))
 
93
        {
 
94
            throw Context.reportRuntimeError2(
 
95
                "msg.java.member.not.found", array.getClass().getName(), id);
 
96
        }
 
97
        return result;
 
98
    }
 
99
 
 
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);
 
105
        }
 
106
        return Undefined.instance;
 
107
    }
 
108
 
 
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);
 
113
    }
 
114
 
 
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,
 
118
                                                                true));
 
119
            return;
 
120
        }
 
121
        super.put(index, start, value);
 
122
    }
 
123
 
 
124
    public Object getDefaultValue(Class hint) {
 
125
        if (hint == null || hint == ScriptRuntime.StringClass)
 
126
            return array.toString();
 
127
        if (hint == ScriptRuntime.BooleanClass)
 
128
            return Boolean.TRUE;
 
129
        if (hint == ScriptRuntime.NumberClass)
 
130
            return ScriptRuntime.NaNobj;
 
131
        return this;
 
132
    }
 
133
 
 
134
    public Object[] getIds() {
 
135
        Object[] result = new Object[length];
 
136
        int i = length;
 
137
        while (--i >= 0)
 
138
            result[i] = new Integer(i);
 
139
        return result;
 
140
    }
 
141
 
 
142
    public boolean hasInstance(Scriptable value) {
 
143
        if (!(value instanceof Wrapper))
 
144
            return false;
 
145
        Object instance = ((Wrapper)value).unwrap();
 
146
        return cls.isInstance(instance);
 
147
    }
 
148
 
 
149
    public Scriptable getPrototype() {
 
150
        if (prototype == null) {
 
151
            prototype =
 
152
                ScriptableObject.getClassPrototype(this.getParentScope(),
 
153
                                                   "Array");
 
154
        }
 
155
        return prototype;
 
156
    }
 
157
 
 
158
    Object array;
 
159
    int length;
 
160
    Class cls;
 
161
    Scriptable prototype;
 
162
}