AbstractJSObject.java revision 1354:a5e202d6eb99
1139749Simp/*
2123579Sgibbs * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
365942Sgibbs * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
471717Sgibbs *
565942Sgibbs * This code is free software; you can redistribute it and/or modify it
665942Sgibbs * under the terms of the GNU General Public License version 2 only, as
765942Sgibbs * published by the Free Software Foundation.  Oracle designates this
865942Sgibbs * particular file as subject to the "Classpath" exception as provided
965942Sgibbs * by Oracle in the LICENSE file that accompanied this code.
1065942Sgibbs *
1165942Sgibbs * This code is distributed in the hope that it will be useful, but WITHOUT
1265942Sgibbs * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1365942Sgibbs * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1465942Sgibbs * version 2 for more details (a copy is included in the LICENSE file that
1565942Sgibbs * accompanied this code).
1665942Sgibbs *
1765942Sgibbs * You should have received a copy of the GNU General Public License version
1865942Sgibbs * 2 along with this work; if not, write to the Free Software Foundation,
1965942Sgibbs * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2065942Sgibbs *
2165942Sgibbs * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2265942Sgibbs * or visit www.oracle.com if you need additional information or have any
2365942Sgibbs * questions.
2465942Sgibbs */
2565942Sgibbs
2665942Sgibbspackage jdk.nashorn.api.scripting;
2765942Sgibbs
2865942Sgibbsimport java.util.Collection;
2965942Sgibbsimport java.util.Collections;
3065942Sgibbsimport java.util.Set;
31123579Sgibbs
3265942Sgibbs/**
3365942Sgibbs * This is the base class for nashorn ScriptObjectMirror class.
34119418Sobrien *
35119418Sobrien * This class can also be subclassed by an arbitrary Java class. Nashorn will
36119418Sobrien * treat objects of such classes just like nashorn script objects. Usual nashorn
3795378Sgibbs * operations like obj[i], obj.foo, obj.func(), delete obj.foo will be glued
3865942Sgibbs * to appropriate method call of this class.
3965942Sgibbs *
40123579Sgibbs * @since 1.8u40
41123579Sgibbs */
4265942Sgibbs@jdk.Exported
4365942Sgibbspublic abstract class AbstractJSObject implements JSObject {
4465942Sgibbs    /**
4565942Sgibbs     * Call this object as a JavaScript function. This is equivalent to
46123579Sgibbs     * 'func.apply(thiz, args)' in JavaScript.
47123579Sgibbs     *
4865942Sgibbs     * @param thiz 'this' object to be passed to the function
4965942Sgibbs     * @param args arguments to method
50103811Sscottl     * @return result of call
51103811Sscottl     */
52153072Sru    @Override
5365942Sgibbs    public Object call(final Object thiz, final Object... args) {
5465942Sgibbs        throw new UnsupportedOperationException("call");
5576634Sgibbs    }
5665942Sgibbs
5765942Sgibbs    /**
5865942Sgibbs     * Call this 'constructor' JavaScript function to create a new object.
5965942Sgibbs     * This is equivalent to 'new func(arg1, arg2...)' in JavaScript.
6065942Sgibbs     *
6165942Sgibbs     * @param args arguments to method
6265942Sgibbs     * @return result of constructor call
6365942Sgibbs     */
6465942Sgibbs    @Override
6565942Sgibbs    public Object newObject(final Object... args) {
6665942Sgibbs        throw new UnsupportedOperationException("newObject");
6765942Sgibbs    }
6865942Sgibbs
6965942Sgibbs    /**
7066269Sgibbs     * Evaluate a JavaScript expression.
7165942Sgibbs     *
7265942Sgibbs     * @param s JavaScript expression to evaluate
7365942Sgibbs     * @return evaluation result
7465942Sgibbs     */
7566269Sgibbs    @Override
7666269Sgibbs    public Object eval(final String s) {
7765942Sgibbs        throw new UnsupportedOperationException("eval");
7865942Sgibbs    }
7965942Sgibbs
8066269Sgibbs    /**
8165942Sgibbs     * Retrieves a named member of this JavaScript object.
8265942Sgibbs     *
8365942Sgibbs     * @param name of member
8465942Sgibbs     * @return member
8565942Sgibbs     */
8666269Sgibbs    @Override
8765942Sgibbs    public Object getMember(final String name) {
8865942Sgibbs        return null;
8995378Sgibbs    }
9095378Sgibbs
9195378Sgibbs    /**
9295378Sgibbs     * Retrieves an indexed member of this JavaScript object.
93133911Sgibbs     *
94133911Sgibbs     * @param index index slot to retrieve
9595378Sgibbs     * @return member
96133911Sgibbs     */
97133911Sgibbs    @Override
98133911Sgibbs    public Object getSlot(final int index) {
99133911Sgibbs        return null;
100133911Sgibbs    }
101133911Sgibbs
102133911Sgibbs    /**
103133911Sgibbs     * Does this object have a named member?
104133911Sgibbs     *
105133911Sgibbs     * @param name name of member
106133911Sgibbs     * @return true if this object has a member of the given name
107133911Sgibbs     */
10895378Sgibbs    @Override
10995378Sgibbs    public boolean hasMember(final String name) {
110168807Sscottl        return false;
111168807Sscottl    }
11295378Sgibbs
11395378Sgibbs    /**
11495378Sgibbs     * Does this object have a indexed property?
11595378Sgibbs     *
11695378Sgibbs     * @param slot index to check
11795378Sgibbs     * @return true if this object has a slot
11895378Sgibbs     */
119133911Sgibbs    @Override
120133911Sgibbs    public boolean hasSlot(final int slot) {
121133911Sgibbs        return false;
122133911Sgibbs    }
123133911Sgibbs
124133911Sgibbs    /**
125133911Sgibbs     * Remove a named member from this JavaScript object
126133911Sgibbs     *
127133911Sgibbs     * @param name name of the member
128133911Sgibbs     */
129133911Sgibbs    @Override
130133911Sgibbs    public void removeMember(final String name) {
131133911Sgibbs        //empty
132133911Sgibbs    }
133133911Sgibbs
134133911Sgibbs    /**
135133911Sgibbs     * Set a named member in this JavaScript object
136133911Sgibbs     *
137133911Sgibbs     * @param name  name of the member
138133911Sgibbs     * @param value value of the member
139133911Sgibbs     */
14065942Sgibbs    @Override
14165942Sgibbs    public void setMember(final String name, final Object value) {
14265942Sgibbs        //empty
14365942Sgibbs    }
14465942Sgibbs
14565942Sgibbs    /**
14665942Sgibbs     * Set an indexed member in this JavaScript object
14765942Sgibbs     *
14865942Sgibbs     * @param index index of the member slot
14965942Sgibbs     * @param value value of the member
15065942Sgibbs     */
15165942Sgibbs    @Override
15265942Sgibbs    public void setSlot(final int index, final Object value) {
15365942Sgibbs        //empty
15465942Sgibbs    }
15565942Sgibbs
15665942Sgibbs    // property and value iteration
15765942Sgibbs
15865942Sgibbs    /**
15965942Sgibbs     * Returns the set of all property names of this object.
160162051Smjacob     *
161162051Smjacob     * @return set of property names
16265942Sgibbs     */
163168807Sscottl    @Override
164123579Sgibbs    public Set<String> keySet() {
165123579Sgibbs        return Collections.emptySet();
166123579Sgibbs    }
167123579Sgibbs
168123579Sgibbs    /**
169123579Sgibbs     * Returns the set of all property values of this object.
17065942Sgibbs     *
17165942Sgibbs     * @return set of property values.
172168807Sscottl     */
173123579Sgibbs    @Override
17465942Sgibbs    public Collection<Object> values() {
17565942Sgibbs        return Collections.emptySet();
17665942Sgibbs    }
17765942Sgibbs
17872552Sgibbs    // JavaScript instanceof check
17974094Sgibbs
18065942Sgibbs    /**
18165942Sgibbs     * Checking whether the given object is an instance of 'this' object.
18265942Sgibbs     *
18365942Sgibbs     * @param instance instance to check
18465942Sgibbs     * @return true if the given 'instance' is an instance of this 'function' object
18565942Sgibbs     */
18665942Sgibbs    @Override
18765942Sgibbs    public boolean isInstance(final Object instance) {
18865942Sgibbs        return false;
18965942Sgibbs    }
19071390Sgibbs
19165942Sgibbs    /**
19265942Sgibbs     * Checking whether this object is an instance of the given 'clazz' object.
19365942Sgibbs     *
19465942Sgibbs     * @param clazz clazz to check
19565942Sgibbs     * @return true if this object is an instance of the given 'clazz'
19665942Sgibbs     */
19766104Sgibbs    @Override
19866104Sgibbs    public boolean isInstanceOf(final Object clazz) {
199168807Sscottl        if (clazz instanceof JSObject) {
20065942Sgibbs            return ((JSObject)clazz).isInstance(this);
20165942Sgibbs        }
20265942Sgibbs
20365942Sgibbs        return false;
20465942Sgibbs    }
205170872Sscottl
20665942Sgibbs    /**
20765942Sgibbs     * ECMA [[Class]] property
20865942Sgibbs     *
20965942Sgibbs     * @return ECMA [[Class]] property value of this object
21065942Sgibbs     */
21165942Sgibbs    @Override
21265942Sgibbs    public String getClassName() {
21365942Sgibbs        return getClass().getName();
21465942Sgibbs    }
21565942Sgibbs
21665942Sgibbs    /**
21765942Sgibbs     * Is this a function object?
21865942Sgibbs     *
21965942Sgibbs     * @return if this mirror wraps a ECMAScript function instance
22065942Sgibbs     */
22165942Sgibbs    @Override
22265942Sgibbs    public boolean isFunction() {
22365942Sgibbs        return false;
22465942Sgibbs    }
22565942Sgibbs
22665942Sgibbs    /**
22765942Sgibbs     * Is this a 'use strict' function object?
22865942Sgibbs     *
22965942Sgibbs     * @return true if this mirror represents a ECMAScript 'use strict' function
230168752Sscottl     */
231168807Sscottl    @Override
23271390Sgibbs    public boolean isStrictFunction() {
23365942Sgibbs        return false;
23465942Sgibbs    }
23565942Sgibbs
23665942Sgibbs    /**
23765942Sgibbs     * Is this an array object?
23865942Sgibbs     *
23965942Sgibbs     * @return if this mirror wraps a ECMAScript array object
240170872Sscottl     */
241170872Sscottl    @Override
24265942Sgibbs    public boolean isArray() {
24365942Sgibbs        return false;
24465942Sgibbs    }
24565942Sgibbs
24665942Sgibbs    /**
24765942Sgibbs     * Returns this object's numeric value.
24865942Sgibbs     *
24965942Sgibbs     * @return this object's numeric value.
25065942Sgibbs     * @deprecated use {@link #getDefaultValue(Class)} with {@link Number} hint instead.
25165942Sgibbs     */
25265942Sgibbs    @Override @Deprecated
25365942Sgibbs    public double toNumber() {
25465942Sgibbs        return Double.NaN;
25565942Sgibbs    }
25665942Sgibbs
25765942Sgibbs    /**
25865942Sgibbs     * When passed an {@link AbstractJSObject}, invokes its {@link #getDefaultValue(Class)} method. When passed any
25965942Sgibbs     * other {@link JSObject}, it will obtain its {@code [[DefaultValue]]} method as per ECMAScript 5.1 section
26065942Sgibbs     * 8.6.2.
26165942Sgibbs     *
26265942Sgibbs     * @param jsobj the {@link JSObject} whose {@code [[DefaultValue]]} is obtained.
26365942Sgibbs     * @param hint the type hint. Should be either {@code null}, {@code Number.class} or {@code String.class}.
26465942Sgibbs     * @return this object's default value.
26565942Sgibbs     * @throws UnsupportedOperationException if the conversion can't be performed. The engine will convert this
26665942Sgibbs     * exception into a JavaScript {@code TypeError}.
26765942Sgibbs     * @deprecated use {@link JSObject#getDefaultValue(Class)} instead.
26865942Sgibbs     */
26965942Sgibbs    @Deprecated
27065942Sgibbs    public static Object getDefaultValue(final JSObject jsobj, final Class<?> hint) {
27172552Sgibbs        return jsobj.getDefaultValue(hint);
27274094Sgibbs    }
27365942Sgibbs}
27465942Sgibbs