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