| 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | * |
| 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 | |
| 7 | // API class |
| 8 | |
| 9 | package org.mozilla.javascript; |
| 10 | |
| 11 | /** |
| 12 | * This is interface that all objects in JavaScript must implement. |
| 13 | * The interface provides for the management of properties and for |
| 14 | * performing conversions. |
| 15 | * <p> |
| 16 | * Host system implementors may find it easier to extend the ScriptableObject |
| 17 | * class rather than implementing Scriptable when writing host objects. |
| 18 | * <p> |
| 19 | * There are many static methods defined in ScriptableObject that perform |
| 20 | * the multiple calls to the Scriptable interface needed in order to |
| 21 | * manipulate properties in prototype chains. |
| 22 | * <p> |
| 23 | * |
| 24 | * @see org.mozilla.javascript.ScriptableObject |
| 25 | * @author Norris Boyd |
| 26 | * @author Nick Thompson |
| 27 | * @author Brendan Eich |
| 28 | */ |
| 29 | |
| 30 | public interface Scriptable { |
| 31 | |
| 32 | /** |
| 33 | * Get the name of the set of objects implemented by this Java class. |
| 34 | * This corresponds to the [[Class]] operation in ECMA and is used |
| 35 | * by Object.prototype.toString() in ECMA.<p> |
| 36 | * See ECMA 8.6.2 and 15.2.4.2. |
| 37 | */ |
| 38 | public String getClassName(); |
| 39 | |
| 40 | /** |
| 41 | * Value returned from <code>get</code> if the property is not |
| 42 | * found. |
| 43 | */ |
| 44 | public static final Object NOT_FOUND = UniqueTag.NOT_FOUND; |
| 45 | |
| 46 | /** |
| 47 | * Get a named property from the object. |
| 48 | * |
| 49 | * Looks property up in this object and returns the associated value |
| 50 | * if found. Returns NOT_FOUND if not found. |
| 51 | * Note that this method is not expected to traverse the prototype |
| 52 | * chain. This is different from the ECMA [[Get]] operation. |
| 53 | * |
| 54 | * Depending on the property selector, the runtime will call |
| 55 | * this method or the form of <code>get</code> that takes an |
| 56 | * integer: |
| 57 | * <table> |
| 58 | * <tr><th>JavaScript code</th><th>Java code</th></tr> |
| 59 | * <tr><td>a.b </td><td>a.get("b", a)</td></tr> |
| 60 | * <tr><td>a["foo"] </td><td>a.get("foo", a)</td></tr> |
| 61 | * <tr><td>a[3] </td><td>a.get(3, a)</td></tr> |
| 62 | * <tr><td>a["3"] </td><td>a.get(3, a)</td></tr> |
| 63 | * <tr><td>a[3.0] </td><td>a.get(3, a)</td></tr> |
| 64 | * <tr><td>a["3.0"] </td><td>a.get("3.0", a)</td></tr> |
| 65 | * <tr><td>a[1.1] </td><td>a.get("1.1", a)</td></tr> |
| 66 | * <tr><td>a[-4] </td><td>a.get(-4, a)</td></tr> |
| 67 | * </table> |
| 68 | * <p> |
| 69 | * The values that may be returned are limited to the following: |
| 70 | * <UL> |
| 71 | * <LI>java.lang.Boolean objects</LI> |
| 72 | * <LI>java.lang.String objects</LI> |
| 73 | * <LI>java.lang.Number objects</LI> |
| 74 | * <LI>org.mozilla.javascript.Scriptable objects</LI> |
| 75 | * <LI>null</LI> |
| 76 | * <LI>The value returned by Context.getUndefinedValue()</LI> |
| 77 | * <LI>NOT_FOUND</LI> |
| 78 | * </UL> |
| 79 | * @param name the name of the property |
| 80 | * @param start the object in which the lookup began |
| 81 | * @return the value of the property (may be null), or NOT_FOUND |
| 82 | * @see org.mozilla.javascript.Context#getUndefinedValue |
| 83 | */ |
| 84 | public Object get(String name, Scriptable start); |
| 85 | |
| 86 | /** |
| 87 | * Get a property from the object selected by an integral index. |
| 88 | * |
| 89 | * Identical to <code>get(String, Scriptable)</code> except that |
| 90 | * an integral index is used to select the property. |
| 91 | * |
| 92 | * @param index the numeric index for the property |
| 93 | * @param start the object in which the lookup began |
| 94 | * @return the value of the property (may be null), or NOT_FOUND |
| 95 | * @see org.mozilla.javascript.Scriptable#get(String,Scriptable) |
| 96 | */ |
| 97 | public Object get(int index, Scriptable start); |
| 98 | |
| 99 | /** |
| 100 | * Indicates whether or not a named property is defined in an object. |
| 101 | * |
| 102 | * Does not traverse the prototype chain.<p> |
| 103 | * |
| 104 | * The property is specified by a String name |
| 105 | * as defined for the <code>get</code> method.<p> |
| 106 | * |
| 107 | * @param name the name of the property |
| 108 | * @param start the object in which the lookup began |
| 109 | * @return true if and only if the named property is found in the object |
| 110 | * @see org.mozilla.javascript.Scriptable#get(String, Scriptable) |
| 111 | * @see org.mozilla.javascript.ScriptableObject#getProperty(Scriptable, String) |
| 112 | */ |
| 113 | public boolean has(String name, Scriptable start); |
| 114 | |
| 115 | /** |
| 116 | * Indicates whether or not an indexed property is defined in an object. |
| 117 | * |
| 118 | * Does not traverse the prototype chain.<p> |
| 119 | * |
| 120 | * The property is specified by an integral index |
| 121 | * as defined for the <code>get</code> method.<p> |
| 122 | * |
| 123 | * @param index the numeric index for the property |
| 124 | * @param start the object in which the lookup began |
| 125 | * @return true if and only if the indexed property is found in the object |
| 126 | * @see org.mozilla.javascript.Scriptable#get(int, Scriptable) |
| 127 | * @see org.mozilla.javascript.ScriptableObject#getProperty(Scriptable, int) |
| 128 | */ |
| 129 | public boolean has(int index, Scriptable start); |
| 130 | |
| 131 | /** |
| 132 | * Sets a named property in this object. |
| 133 | * <p> |
| 134 | * The property is specified by a string name |
| 135 | * as defined for <code>get</code>. |
| 136 | * <p> |
| 137 | * The possible values that may be passed in are as defined for |
| 138 | * <code>get</code>. A class that implements this method may choose |
| 139 | * to ignore calls to set certain properties, in which case those |
| 140 | * properties are effectively read-only.<p> |
| 141 | * For properties defined in a prototype chain, |
| 142 | * use <code>putProperty</code> in ScriptableObject. <p> |
| 143 | * Note that if a property <i>a</i> is defined in the prototype <i>p</i> |
| 144 | * of an object <i>o</i>, then evaluating <code>o.a = 23</code> will cause |
| 145 | * <code>set</code> to be called on the prototype <i>p</i> with |
| 146 | * <i>o</i> as the <i>start</i> parameter. |
| 147 | * To preserve JavaScript semantics, it is the Scriptable |
| 148 | * object's responsibility to modify <i>o</i>. <p> |
| 149 | * This design allows properties to be defined in prototypes and implemented |
| 150 | * in terms of getters and setters of Java values without consuming slots |
| 151 | * in each instance.<p> |
| 152 | * <p> |
| 153 | * The values that may be set are limited to the following: |
| 154 | * <UL> |
| 155 | * <LI>java.lang.Boolean objects</LI> |
| 156 | * <LI>java.lang.String objects</LI> |
| 157 | * <LI>java.lang.Number objects</LI> |
| 158 | * <LI>org.mozilla.javascript.Scriptable objects</LI> |
| 159 | * <LI>null</LI> |
| 160 | * <LI>The value returned by Context.getUndefinedValue()</LI> |
| 161 | * </UL><p> |
| 162 | * Arbitrary Java objects may be wrapped in a Scriptable by first calling |
| 163 | * <code>Context.toObject</code>. This allows the property of a JavaScript |
| 164 | * object to contain an arbitrary Java object as a value.<p> |
| 165 | * Note that <code>has</code> will be called by the runtime first before |
| 166 | * <code>set</code> is called to determine in which object the |
| 167 | * property is defined. |
| 168 | * Note that this method is not expected to traverse the prototype chain, |
| 169 | * which is different from the ECMA [[Put]] operation. |
| 170 | * @param name the name of the property |
| 171 | * @param start the object whose property is being set |
| 172 | * @param value value to set the property to |
| 173 | * @see org.mozilla.javascript.Scriptable#has(String, Scriptable) |
| 174 | * @see org.mozilla.javascript.Scriptable#get(String, Scriptable) |
| 175 | * @see org.mozilla.javascript.ScriptableObject#putProperty(Scriptable, String, Object) |
| 176 | * @see org.mozilla.javascript.Context#toObject(Object, Scriptable) |
| 177 | */ |
| 178 | public void put(String name, Scriptable start, Object value); |
| 179 | |
| 180 | /** |
| 181 | * Sets an indexed property in this object. |
| 182 | * <p> |
| 183 | * The property is specified by an integral index |
| 184 | * as defined for <code>get</code>.<p> |
| 185 | * |
| 186 | * Identical to <code>put(String, Scriptable, Object)</code> except that |
| 187 | * an integral index is used to select the property. |
| 188 | * |
| 189 | * @param index the numeric index for the property |
| 190 | * @param start the object whose property is being set |
| 191 | * @param value value to set the property to |
| 192 | * @see org.mozilla.javascript.Scriptable#has(int, Scriptable) |
| 193 | * @see org.mozilla.javascript.Scriptable#get(int, Scriptable) |
| 194 | * @see org.mozilla.javascript.ScriptableObject#putProperty(Scriptable, int, Object) |
| 195 | * @see org.mozilla.javascript.Context#toObject(Object, Scriptable) |
| 196 | */ |
| 197 | public void put(int index, Scriptable start, Object value); |
| 198 | |
| 199 | /** |
| 200 | * Removes a property from this object. |
| 201 | * This operation corresponds to the ECMA [[Delete]] except that |
| 202 | * the no result is returned. The runtime will guarantee that this |
| 203 | * method is called only if the property exists. After this method |
| 204 | * is called, the runtime will call Scriptable.has to see if the |
| 205 | * property has been removed in order to determine the boolean |
| 206 | * result of the delete operator as defined by ECMA 11.4.1. |
| 207 | * <p> |
| 208 | * A property can be made permanent by ignoring calls to remove |
| 209 | * it.<p> |
| 210 | * The property is specified by a String name |
| 211 | * as defined for <code>get</code>. |
| 212 | * <p> |
| 213 | * To delete properties defined in a prototype chain, |
| 214 | * see deleteProperty in ScriptableObject. |
| 215 | * @param name the identifier for the property |
| 216 | * @see org.mozilla.javascript.Scriptable#get(String, Scriptable) |
| 217 | * @see org.mozilla.javascript.ScriptableObject#deleteProperty(Scriptable, String) |
| 218 | */ |
| 219 | public void delete(String name); |
| 220 | |
| 221 | /** |
| 222 | * Removes a property from this object. |
| 223 | * |
| 224 | * The property is specified by an integral index |
| 225 | * as defined for <code>get</code>. |
| 226 | * <p> |
| 227 | * To delete properties defined in a prototype chain, |
| 228 | * see deleteProperty in ScriptableObject. |
| 229 | * |
| 230 | * Identical to <code>delete(String)</code> except that |
| 231 | * an integral index is used to select the property. |
| 232 | * |
| 233 | * @param index the numeric index for the property |
| 234 | * @see org.mozilla.javascript.Scriptable#get(int, Scriptable) |
| 235 | * @see org.mozilla.javascript.ScriptableObject#deleteProperty(Scriptable, int) |
| 236 | */ |
| 237 | public void delete(int index); |
| 238 | |
| 239 | /** |
| 240 | * Get the prototype of the object. |
| 241 | * @return the prototype |
| 242 | */ |
| 243 | public Scriptable getPrototype(); |
| 244 | |
| 245 | /** |
| 246 | * Set the prototype of the object. |
| 247 | * @param prototype the prototype to set |
| 248 | */ |
| 249 | public void setPrototype(Scriptable prototype); |
| 250 | |
| 251 | /** |
| 252 | * Get the parent scope of the object. |
| 253 | * @return the parent scope |
| 254 | */ |
| 255 | public Scriptable getParentScope(); |
| 256 | |
| 257 | /** |
| 258 | * Set the parent scope of the object. |
| 259 | * @param parent the parent scope to set |
| 260 | */ |
| 261 | public void setParentScope(Scriptable parent); |
| 262 | |
| 263 | /** |
| 264 | * Get an array of property ids. |
| 265 | * |
| 266 | * Not all property ids need be returned. Those properties |
| 267 | * whose ids are not returned are considered non-enumerable. |
| 268 | * |
| 269 | * @return an array of Objects. Each entry in the array is either |
| 270 | * a java.lang.String or a java.lang.Number |
| 271 | */ |
| 272 | public Object[] getIds(); |
| 273 | |
| 274 | /** |
| 275 | * Get the default value of the object with a given hint. |
| 276 | * The hints are String.class for type String, Number.class for type |
| 277 | * Number, Scriptable.class for type Object, and Boolean.class for |
| 278 | * type Boolean. <p> |
| 279 | * |
| 280 | * A <code>hint</code> of null means "no hint". |
| 281 | * |
| 282 | * See ECMA 8.6.2.6. |
| 283 | * |
| 284 | * @param hint the type hint |
| 285 | * @return the default value |
| 286 | */ |
| 287 | public Object getDefaultValue(Class<?> hint); |
| 288 | |
| 289 | /** |
| 290 | * The instanceof operator. |
| 291 | * |
| 292 | * <p> |
| 293 | * The JavaScript code "lhs instanceof rhs" causes rhs.hasInstance(lhs) to |
| 294 | * be called. |
| 295 | * |
| 296 | * <p> |
| 297 | * The return value is implementation dependent so that embedded host objects can |
| 298 | * return an appropriate value. See the JS 1.3 language documentation for more |
| 299 | * detail. |
| 300 | * |
| 301 | * <p>This operator corresponds to the proposed EMCA [[HasInstance]] operator. |
| 302 | * |
| 303 | * @param instance The value that appeared on the LHS of the instanceof |
| 304 | * operator |
| 305 | * |
| 306 | * @return an implementation dependent value |
| 307 | */ |
| 308 | public boolean hasInstance(Scriptable instance); |
| 309 | } |
| 310 |