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 
  26 package jdk.nashorn.api.scripting;
  27 
  28 import java.util.Collection;
  29 import java.util.Collections;
  30 import 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  */
  42 @jdk.Exported
  43 public abstract class AbstractJSObject implements JSObject {
  44     /**
  45      * Call this object as a JavaScript function. This is equivalent to
  46      * 'func.apply(thiz, args)' in JavaScript.
  47      *
  48      * @param thiz 'this' object to be passed to the function
  49      * @param args arguments to method
  50      * @return result of call
  51      */
  52     @Override
  53     public Object call(final Object thiz, final Object... args) {
  54         throw new UnsupportedOperationException("call");
  55     }
  56 
  57     /**
  58      * Call this 'constructor' JavaScript function to create a new object.
  59      * This is equivalent to 'new func(arg1, arg2...)' in JavaScript.
  60      *
  61      * @param args arguments to method
  62      * @return result of constructor call
  63      */
  64     @Override
  65     public Object newObject(final Object... args) {
  66         throw new UnsupportedOperationException("newObject");
  67     }
  68 
  69     /**
  70      * Evaluate a JavaScript expression.
  71      *
  72      * @param s JavaScript expression to evaluate
  73      * @return evaluation result
  74      */
  75     @Override
  76     public Object eval(final String s) {
  77         throw new UnsupportedOperationException("eval");
  78     }
  79 
  80     /**
  81      * Retrieves a named member of this JavaScript object.
  82      *
  83      * @param name of member
  84      * @return member
  85      */
  86     @Override
  87     public Object getMember(final String name) {
  88         return null;
  89     }
  90 
  91     /**
  92      * Retrieves an indexed member of this JavaScript object.
  93      *
  94      * @param index index slot to retrieve
  95      * @return member
  96      */
  97     @Override
  98     public Object getSlot(final int index) {
  99         return null;
 100     }
 101 
 102     /**
 103      * Does this object have a named member?
 104      *
 105      * @param name name of member
 106      * @return true if this object has a member of the given name
 107      */
 108     @Override
 109     public boolean hasMember(final String name) {
 110         return false;
 111     }
 112 
 113     /**
 114      * Does this object have a indexed property?
 115      *
 116      * @param slot index to check
 117      * @return true if this object has a slot
 118      */
 119     @Override
 120     public boolean hasSlot(final int slot) {
 121         return false;
 122     }
 123 
 124     /**
 125      * Remove a named member from this JavaScript object
 126      *
 127      * @param name name of the member
 128      */
 129     @Override
 130     public void removeMember(final String name) {
 131         //empty
 132     }
 133 
 134     /**
 135      * Set a named member in this JavaScript object
 136      *
 137      * @param name  name of the member
 138      * @param value value of the member
 139      */
 140     @Override
 141     public void setMember(final String name, final Object value) {
 142         //empty
 143     }
 144 
 145     /**
 146      * Set an indexed member in this JavaScript object
 147      *
 148      * @param index index of the member slot
 149      * @param value value of the member
 150      */
 151     @Override
 152     public void setSlot(final int index, final Object value) {
 153         //empty
 154     }
 155 
 156     // property and value iteration
 157 
 158     /**
 159      * Returns the set of all property names of this object.
 160      *
 161      * @return set of property names
 162      */
 163     @Override
 164     public Set<String> keySet() {
 165         return Collections.emptySet();
 166     }
 167 
 168     /**
 169      * Returns the set of all property values of this object.
 170      *
 171      * @return set of property values.
 172      */
 173     @Override
 174     public Collection<Object> values() {
 175         return Collections.emptySet();
 176     }
 177 
 178     // JavaScript instanceof check
 179 
 180     /**
 181      * Checking whether the given object is an instance of 'this' object.
 182      *
 183      * @param instance instance to check
 184      * @return true if the given 'instance' is an instance of this 'function' object
 185      */
 186     @Override
 187     public boolean isInstance(final Object instance) {
 188         return false;
 189     }
 190 
 191     /**
 192      * Checking whether this object is an instance of the given 'clazz' object.
 193      *
 194      * @param clazz clazz to check
 195      * @return true if this object is an instance of the given 'clazz'
 196      */
 197     @Override
 198     public boolean isInstanceOf(final Object clazz) {
 199         if (clazz instanceof JSObject) {
 200             return ((JSObject)clazz).isInstance(this);
 201         }
 202 
 203         return false;
 204     }
 205 
 206     /**
 207      * ECMA [[Class]] property
 208      *
 209      * @return ECMA [[Class]] property value of this object
 210      */
 211     @Override
 212     public String getClassName() {
 213         return getClass().getName();
 214     }
 215 
 216     /**
 217      * Is this a function object?
 218      *
 219      * @return if this mirror wraps a ECMAScript function instance
 220      */
 221     @Override
 222     public boolean isFunction() {
 223         return false;
 224     }
 225 
 226     /**
 227      * Is this a 'use strict' function object?
 228      *
 229      * @return true if this mirror represents a ECMAScript 'use strict' function
 230      */
 231     @Override
 232     public boolean isStrictFunction() {
 233         return false;
 234     }
 235 
 236     /**
 237      * Is this an array object?
 238      *
 239      * @return if this mirror wraps a ECMAScript array object
 240      */
 241     @Override
 242     public boolean isArray() {
 243         return false;
 244     }
 245 
 246     /**
 247      * Returns this object's numeric value.
 248      *
 249      * @return this object's numeric value.
 250      * @deprecated use {@link #getDefaultValue(Class)} with {@link Number} hint instead.
 251      */
 252     @Override @Deprecated
 253     public double toNumber() {
 254         return Double.NaN;
 255     }
 256 
 257     /**
 258      * When passed an {@link AbstractJSObject}, invokes its {@link #getDefaultValue(Class)} method. When passed any
 259      * other {@link JSObject}, it will obtain its {@code [[DefaultValue]]} method as per ECMAScript 5.1 section
 260      * 8.6.2.
 261      *
 262      * @param jsobj the {@link JSObject} whose {@code [[DefaultValue]]} is obtained.
 263      * @param hint the type hint. Should be either {@code null}, {@code Number.class} or {@code String.class}.
 264      * @return this object's default value.
 265      * @throws UnsupportedOperationException if the conversion can't be performed. The engine will convert this
 266      * exception into a JavaScript {@code TypeError}.
 267      * @deprecated use {@link JSObject#getDefaultValue(Class)} instead.
 268      */
 269     @Deprecated
 270     public static Object getDefaultValue(final JSObject jsobj, final Class<?> hint) {
 271         return jsobj.getDefaultValue(hint);
 272     }
 273 }