| 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 | import org.mozilla.javascript.*; |
| 8 | import java.util.List; |
| 9 | import java.util.ArrayList; |
| 10 | |
| 11 | /** |
| 12 | * Matrix: An example host object class that implements the Scriptable interface. |
| 13 | * |
| 14 | * Built-in JavaScript arrays don't handle multiple dimensions gracefully: the |
| 15 | * script writer must create every array in an array of arrays. The Matrix class |
| 16 | * takes care of that by automatically allocating arrays for every index that |
| 17 | * is accessed. What's more, the Matrix constructor takes a integer argument |
| 18 | * that specifies the dimension of the Matrix. If m is a Matrix with dimension 3, |
| 19 | * then m[0] will be a Matrix with dimension 1, and m[0][0] will be an Array. |
| 20 | * |
| 21 | * Here's a shell session showing the Matrix object in action: |
| 22 | * <pre> |
| 23 | * js> defineClass("Matrix") |
| 24 | * js> var m = new Matrix(2); // A constructor call, see "Matrix(int dimension)" |
| 25 | * js> m // Object.toString will call "Matrix.getClassName()" |
| 26 | * [object Matrix] |
| 27 | * js> m[0][0] = 3; |
| 28 | * 3 |
| 29 | * js> uneval(m[0]); // an array was created automatically! |
| 30 | * [3] |
| 31 | * js> uneval(m[1]); // array is created even if we don't set a value |
| 32 | * [] |
| 33 | * js> m.dim; // we can access the "dim" property |
| 34 | * 2 |
| 35 | * js> m.dim = 3; |
| 36 | * 3 |
| 37 | * js> m.dim; // but not modify the "dim" property |
| 38 | * 2 |
| 39 | * </pre> |
| 40 | * |
| 41 | * @see org.mozilla.javascript.Context |
| 42 | * @see org.mozilla.javascript.Scriptable |
| 43 | * |
| 44 | * @author Norris Boyd |
| 45 | */ |
| 46 | public class Matrix implements Scriptable { |
| 47 | |
| 48 | /** |
| 49 | * The zero-parameter constructor. |
| 50 | * |
| 51 | * When ScriptableObject.defineClass is called with this class, it will |
| 52 | * construct Matrix.prototype using this constructor. |
| 53 | */ |
| 54 | public Matrix() { |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * The Java constructor, also used to define the JavaScript constructor. |
| 59 | */ |
| 60 | public Matrix(int dimension) { |
| 61 | if (dimension <= 0) { |
| 62 | throw Context.reportRuntimeError( |
| 63 | "Dimension of Matrix must be greater than zero"); |
| 64 | } |
| 65 | dim = dimension; |
| 66 | list = new ArrayList<Object>(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns the name of this JavaScript class, "Matrix". |
| 71 | */ |
| 72 | public String getClassName() { |
| 73 | return "Matrix"; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Defines the "dim" property by returning true if name is |
| 78 | * equal to "dim". |
| 79 | * <p> |
| 80 | * Defines no other properties, i.e., returns false for |
| 81 | * all other names. |
| 82 | * |
| 83 | * @param name the name of the property |
| 84 | * @param start the object where lookup began |
| 85 | */ |
| 86 | public boolean has(String name, Scriptable start) { |
| 87 | return name.equals("dim"); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Defines all numeric properties by returning true. |
| 92 | * |
| 93 | * @param index the index of the property |
| 94 | * @param start the object where lookup began |
| 95 | */ |
| 96 | public boolean has(int index, Scriptable start) { |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Get the named property. |
| 102 | * <p> |
| 103 | * Handles the "dim" property and returns NOT_FOUND for all |
| 104 | * other names. |
| 105 | * @param name the property name |
| 106 | * @param start the object where the lookup began |
| 107 | */ |
| 108 | public Object get(String name, Scriptable start) { |
| 109 | if (name.equals("dim")) |
| 110 | return new Integer(dim); |
| 111 | |
| 112 | return NOT_FOUND; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Get the indexed property. |
| 117 | * <p> |
| 118 | * Look up the element in the associated list and return |
| 119 | * it if it exists. If it doesn't exist, create it.<p> |
| 120 | * @param index the index of the integral property |
| 121 | * @param start the object where the lookup began |
| 122 | */ |
| 123 | public Object get(int index, Scriptable start) { |
| 124 | while (index >= list.size()) { |
| 125 | list.add(null); |
| 126 | } |
| 127 | Object result = list.get(index); |
| 128 | if (result != null) |
| 129 | return result; |
| 130 | if (dim > 2) { |
| 131 | Matrix m = new Matrix(dim-1); |
| 132 | m.setParentScope(getParentScope()); |
| 133 | m.setPrototype(getPrototype()); |
| 134 | result = m; |
| 135 | } else { |
| 136 | Context cx = Context.getCurrentContext(); |
| 137 | Scriptable scope = ScriptableObject.getTopLevelScope(start); |
| 138 | result = cx.newArray(scope, 0); |
| 139 | } |
| 140 | list.set(index, result); |
| 141 | return result; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Set a named property. |
| 146 | * |
| 147 | * We do nothing here, so all properties are effectively read-only. |
| 148 | */ |
| 149 | public void put(String name, Scriptable start, Object value) { |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Set an indexed property. |
| 154 | * |
| 155 | * We do nothing here, so all properties are effectively read-only. |
| 156 | */ |
| 157 | public void put(int index, Scriptable start, Object value) { |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Remove a named property. |
| 162 | * |
| 163 | * This method shouldn't even be called since we define all properties |
| 164 | * as PERMANENT. |
| 165 | */ |
| 166 | public void delete(String id) { |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Remove an indexed property. |
| 171 | * |
| 172 | * This method shouldn't even be called since we define all properties |
| 173 | * as PERMANENT. |
| 174 | */ |
| 175 | public void delete(int index) { |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Get prototype. |
| 180 | */ |
| 181 | public Scriptable getPrototype() { |
| 182 | return prototype; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Set prototype. |
| 187 | */ |
| 188 | public void setPrototype(Scriptable prototype) { |
| 189 | this.prototype = prototype; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Get parent. |
| 194 | */ |
| 195 | public Scriptable getParentScope() { |
| 196 | return parent; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Set parent. |
| 201 | */ |
| 202 | public void setParentScope(Scriptable parent) { |
| 203 | this.parent = parent; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Get properties. |
| 208 | * |
| 209 | * We return an empty array since we define all properties to be DONTENUM. |
| 210 | */ |
| 211 | public Object[] getIds() { |
| 212 | return new Object[0]; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Default value. |
| 217 | * |
| 218 | * Use the convenience method from Context that takes care of calling |
| 219 | * toString, etc. |
| 220 | */ |
| 221 | public Object getDefaultValue(Class<?> typeHint) { |
| 222 | return "[object Matrix]"; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * instanceof operator. |
| 227 | * |
| 228 | * We mimick the normal JavaScript instanceof semantics, returning |
| 229 | * true if <code>this</code> appears in <code>value</code>'s prototype |
| 230 | * chain. |
| 231 | */ |
| 232 | public boolean hasInstance(Scriptable value) { |
| 233 | Scriptable proto = value.getPrototype(); |
| 234 | while (proto != null) { |
| 235 | if (proto.equals(this)) |
| 236 | return true; |
| 237 | proto = proto.getPrototype(); |
| 238 | } |
| 239 | |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Some private data for this class. |
| 245 | */ |
| 246 | private int dim; |
| 247 | private List<Object> list; |
| 248 | private Scriptable prototype, parent; |
| 249 | } |