AbstractJSObject.java revision 1590:1916a2c680d8
1/*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
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
26package jdk.nashorn.api.scripting;
27
28import java.util.Collection;
29import java.util.Collections;
30import java.util.Set;
31
32/**
33 * This is the base class for nashorn ScriptObjectMirror class.
34 *
35 * This class can also be subclassed by an arbitrary Java class. Nashorn will
36 * treat objects of such classes just like nashorn script objects. Usual nashorn
37 * operations like obj[i], obj.foo, obj.func(), delete obj.foo will be glued
38 * to appropriate method call of this class.
39 *
40 * @since 1.8u40
41 */
42public abstract class AbstractJSObject implements JSObject {
43    /**
44     * Call this object as a JavaScript function. This is equivalent to
45     * 'func.apply(thiz, args)' in JavaScript.
46     *
47     * @param thiz 'this' object to be passed to the function
48     * @param args arguments to method
49     * @return result of call
50     */
51    @Override
52    public Object call(final Object thiz, final Object... args) {
53        throw new UnsupportedOperationException("call");
54    }
55
56    /**
57     * Call this 'constructor' JavaScript function to create a new object.
58     * This is equivalent to 'new func(arg1, arg2...)' in JavaScript.
59     *
60     * @param args arguments to method
61     * @return result of constructor call
62     */
63    @Override
64    public Object newObject(final Object... args) {
65        throw new UnsupportedOperationException("newObject");
66    }
67
68    /**
69     * Evaluate a JavaScript expression.
70     *
71     * @param s JavaScript expression to evaluate
72     * @return evaluation result
73     */
74    @Override
75    public Object eval(final String s) {
76        throw new UnsupportedOperationException("eval");
77    }
78
79    /**
80     * Retrieves a named member of this JavaScript object.
81     *
82     * @param name of member
83     * @return member
84     */
85    @Override
86    public Object getMember(final String name) {
87        return null;
88    }
89
90    /**
91     * Retrieves an indexed member of this JavaScript object.
92     *
93     * @param index index slot to retrieve
94     * @return member
95     */
96    @Override
97    public Object getSlot(final int index) {
98        return null;
99    }
100
101    /**
102     * Does this object have a named member?
103     *
104     * @param name name of member
105     * @return true if this object has a member of the given name
106     */
107    @Override
108    public boolean hasMember(final String name) {
109        return false;
110    }
111
112    /**
113     * Does this object have a indexed property?
114     *
115     * @param slot index to check
116     * @return true if this object has a slot
117     */
118    @Override
119    public boolean hasSlot(final int slot) {
120        return false;
121    }
122
123    /**
124     * Remove a named member from this JavaScript object
125     *
126     * @param name name of the member
127     */
128    @Override
129    public void removeMember(final String name) {
130        //empty
131    }
132
133    /**
134     * Set a named member in this JavaScript object
135     *
136     * @param name  name of the member
137     * @param value value of the member
138     */
139    @Override
140    public void setMember(final String name, final Object value) {
141        //empty
142    }
143
144    /**
145     * Set an indexed member in this JavaScript object
146     *
147     * @param index index of the member slot
148     * @param value value of the member
149     */
150    @Override
151    public void setSlot(final int index, final Object value) {
152        //empty
153    }
154
155    // property and value iteration
156
157    /**
158     * Returns the set of all property names of this object.
159     *
160     * @return set of property names
161     */
162    @Override
163    public Set<String> keySet() {
164        return Collections.emptySet();
165    }
166
167    /**
168     * Returns the set of all property values of this object.
169     *
170     * @return set of property values.
171     */
172    @Override
173    public Collection<Object> values() {
174        return Collections.emptySet();
175    }
176
177    // JavaScript instanceof check
178
179    /**
180     * Checking whether the given object is an instance of 'this' object.
181     *
182     * @param instance instance to check
183     * @return true if the given 'instance' is an instance of this 'function' object
184     */
185    @Override
186    public boolean isInstance(final Object instance) {
187        return false;
188    }
189
190    /**
191     * Checking whether this object is an instance of the given 'clazz' object.
192     *
193     * @param clazz clazz to check
194     * @return true if this object is an instance of the given 'clazz'
195     */
196    @Override
197    public boolean isInstanceOf(final Object clazz) {
198        if (clazz instanceof JSObject) {
199            return ((JSObject)clazz).isInstance(this);
200        }
201
202        return false;
203    }
204
205    /**
206     * ECMA [[Class]] property
207     *
208     * @return ECMA [[Class]] property value of this object
209     */
210    @Override
211    public String getClassName() {
212        return getClass().getName();
213    }
214
215    /**
216     * Is this a function object?
217     *
218     * @return if this mirror wraps a ECMAScript function instance
219     */
220    @Override
221    public boolean isFunction() {
222        return false;
223    }
224
225    /**
226     * Is this a 'use strict' function object?
227     *
228     * @return true if this mirror represents a ECMAScript 'use strict' function
229     */
230    @Override
231    public boolean isStrictFunction() {
232        return false;
233    }
234
235    /**
236     * Is this an array object?
237     *
238     * @return if this mirror wraps a ECMAScript array object
239     */
240    @Override
241    public boolean isArray() {
242        return false;
243    }
244
245    /**
246     * Returns this object's numeric value.
247     *
248     * @return this object's numeric value.
249     * @deprecated use {@link #getDefaultValue(Class)} with {@link Number} hint instead.
250     */
251    @Override @Deprecated
252    public double toNumber() {
253        return Double.NaN;
254    }
255
256    /**
257     * When passed an {@link AbstractJSObject}, invokes its {@link #getDefaultValue(Class)} method. When passed any
258     * other {@link JSObject}, it will obtain its {@code [[DefaultValue]]} method as per ECMAScript 5.1 section
259     * 8.6.2.
260     *
261     * @param jsobj the {@link JSObject} whose {@code [[DefaultValue]]} is obtained.
262     * @param hint the type hint. Should be either {@code null}, {@code Number.class} or {@code String.class}.
263     * @return this object's default value.
264     * @throws UnsupportedOperationException if the conversion can't be performed. The engine will convert this
265     * exception into a JavaScript {@code TypeError}.
266     * @deprecated use {@link JSObject#getDefaultValue(Class)} instead.
267     */
268    @Deprecated
269    public static Object getDefaultValue(final JSObject jsobj, final Class<?> hint) {
270        return jsobj.getDefaultValue(hint);
271    }
272}
273