AbstractJSObject.java revision 1209:36fbf759ab8d
1228753Smm/* 2228753Smm * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3228753Smm * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4228753Smm * 5228753Smm * This code is free software; you can redistribute it and/or modify it 6228753Smm * under the terms of the GNU General Public License version 2 only, as 7228753Smm * published by the Free Software Foundation. Oracle designates this 8228753Smm * particular file as subject to the "Classpath" exception as provided 9228753Smm * by Oracle in the LICENSE file that accompanied this code. 10228753Smm * 11228753Smm * This code is distributed in the hope that it will be useful, but WITHOUT 12228753Smm * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13228753Smm * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14228753Smm * version 2 for more details (a copy is included in the LICENSE file that 15228753Smm * accompanied this code). 16228753Smm * 17228753Smm * You should have received a copy of the GNU General Public License version 18228753Smm * 2 along with this work; if not, write to the Free Software Foundation, 19228753Smm * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20228753Smm * 21228753Smm * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22228753Smm * or visit www.oracle.com if you need additional information or have any 23228753Smm * questions. 24228753Smm */ 25228753Smm 26228753Smmpackage jdk.nashorn.api.scripting; 27228763Smm 28228753Smmimport java.util.Collection; 29228753Smmimport java.util.Collections; 30228753Smmimport java.util.Set; 31228753Smm 32228753Smm/** 33228753Smm * This is the base class for nashorn ScriptObjectMirror class. 34228753Smm * 35228753Smm * This class can also be subclassed by an arbitrary Java class. Nashorn will 36228753Smm * treat objects of such classes just like nashorn script objects. Usual nashorn 37228753Smm * operations like obj[i], obj.foo, obj.func(), delete obj.foo will be glued 38228753Smm * to appropriate method call of this class. 39228753Smm * 40228753Smm * @since 1.8u40 41228753Smm */ 42228753Smm@jdk.Exported 43228753Smmpublic abstract class AbstractJSObject implements JSObject { 44228753Smm /** 45228753Smm * Call this object as a JavaScript function. This is equivalent to 46228753Smm * 'func.apply(thiz, args)' in JavaScript. 47228753Smm * 48228753Smm * @param thiz 'this' object to be passed to the function 49228753Smm * @param args arguments to method 50228753Smm * @return result of call 51228753Smm */ 52228753Smm @Override 53228753Smm public Object call(final Object thiz, final Object... args) { 54228753Smm throw new UnsupportedOperationException("call"); 55228753Smm } 56228753Smm 57228753Smm /** 58228753Smm * Call this 'constructor' JavaScript function to create a new object. 59228753Smm * This is equivalent to 'new func(arg1, arg2...)' in JavaScript. 60228753Smm * 61228753Smm * @param args arguments to method 62228753Smm * @return result of constructor call 63228753Smm */ 64228753Smm @Override 65228753Smm public Object newObject(final Object... args) { 66228753Smm throw new UnsupportedOperationException("newObject"); 67228753Smm } 68228753Smm 69228753Smm /** 70228753Smm * Evaluate a JavaScript expression. 71228753Smm * 72228753Smm * @param s JavaScript expression to evaluate 73228753Smm * @return evaluation result 74228753Smm */ 75228753Smm @Override 76228753Smm public Object eval(final String s) { 77228753Smm throw new UnsupportedOperationException("eval"); 78228753Smm } 79228753Smm 80228753Smm /** 81228753Smm * Retrieves a named member of this JavaScript object. 82228753Smm * 83313570Smm * @param name of member 84228753Smm * @return member 85228753Smm */ 86228753Smm @Override 87228753Smm 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 @SuppressWarnings("unchecked") 165 public Set<String> keySet() { 166 return Collections.EMPTY_SET; 167 } 168 169 /** 170 * Returns the set of all property values of this object. 171 * 172 * @return set of property values. 173 */ 174 @Override 175 @SuppressWarnings("unchecked") 176 public Collection<Object> values() { 177 return Collections.EMPTY_SET; 178 } 179 180 // JavaScript instanceof check 181 182 /** 183 * Checking whether the given object is an instance of 'this' object. 184 * 185 * @param instance instace to check 186 * @return true if the given 'instance' is an instance of this 'function' object 187 */ 188 @Override 189 public boolean isInstance(final Object instance) { 190 return false; 191 } 192 193 /** 194 * Checking whether this object is an instance of the given 'clazz' object. 195 * 196 * @param clazz clazz to check 197 * @return true if this object is an instance of the given 'clazz' 198 */ 199 @Override 200 public boolean isInstanceOf(final Object clazz) { 201 if (clazz instanceof JSObject) { 202 return ((JSObject)clazz).isInstance(this); 203 } 204 205 return false; 206 } 207 208 /** 209 * ECMA [[Class]] property 210 * 211 * @return ECMA [[Class]] property value of this object 212 */ 213 @Override 214 public String getClassName() { 215 return getClass().getName(); 216 } 217 218 /** 219 * Is this a function object? 220 * 221 * @return if this mirror wraps a ECMAScript function instance 222 */ 223 @Override 224 public boolean isFunction() { 225 return false; 226 } 227 228 /** 229 * Is this a 'use strict' function object? 230 * 231 * @return true if this mirror represents a ECMAScript 'use strict' function 232 */ 233 @Override 234 public boolean isStrictFunction() { 235 return false; 236 } 237 238 /** 239 * Is this an array object? 240 * 241 * @return if this mirror wraps a ECMAScript array object 242 */ 243 @Override 244 public boolean isArray() { 245 return false; 246 } 247 248 /** 249 * Returns this object's numeric value. 250 * 251 * @return this object's numeric value. 252 */ 253 @SuppressWarnings("deprecation") 254 @Override 255 public double toNumber() { 256 return Double.NaN; 257 } 258} 259