| 90 | | |
|---|
| 91 | | public Object get(int index, Scriptable start) { |
|---|
| 92 | | if (hasAdapterFunctions && isAdapter.get()) { |
|---|
| 93 | | Function func = getAdapteeFunction(GET_PROP); |
|---|
| 94 | | if (func != null) { |
|---|
| 95 | | return call(func, new Object[] { index }); |
|---|
| 96 | | } |
|---|
| 97 | | } |
|---|
| 98 | | return super.get(index, start); |
|---|
| 99 | | } |
|---|
| 100 | | |
|---|
| 101 | | public boolean has(String name, Scriptable start) { |
|---|
| 102 | | if (hasAdapterFunctions && isAdapter.get()) { |
|---|
| 103 | | Function func = getAdapteeFunction(HAS_PROP); |
|---|
| 104 | | if (func != null) { |
|---|
| 105 | | Object res = call(func, new Object[] { name }); |
|---|
| 106 | | return Context.toBoolean(res); |
|---|
| 107 | | } |
|---|
| 108 | | } |
|---|
| 109 | | return super.has(name, start); |
|---|
| 110 | | } |
|---|
| 111 | | |
|---|
| 112 | | public boolean has(int index, Scriptable start) { |
|---|
| 113 | | if (hasAdapterFunctions && isAdapter.get()) { |
|---|
| 114 | | Function func = getAdapteeFunction(HAS_PROP); |
|---|
| 115 | | if (func != null) { |
|---|
| 116 | | Object res = call(func, new Object[] { index }); |
|---|
| 117 | | return Context.toBoolean(res); |
|---|
| 118 | | } |
|---|
| 119 | | } |
|---|
| 120 | | return super.has(index, start); |
|---|
| 121 | | } |
|---|
| 122 | | |
|---|
| 123 | | public void put(String name, Scriptable start, Object value) { |
|---|
| 124 | | if (start == this) { |
|---|
| 125 | | if (hasAdapterFunctions && isAdapter.get()) { |
|---|
| 126 | | Function func = getAdapteeFunction(PUT_PROP); |
|---|
| 127 | | if (func != null) { |
|---|
| 128 | | call(func, new Object[] { name, value }); |
|---|
| 129 | | return; |
|---|
| 130 | | } |
|---|
| 131 | | } |
|---|
| 132 | | super.put(name, start, value); |
|---|
| 133 | | } else { |
|---|
| 134 | | start.put(name, start, value); |
|---|
| 135 | | } |
|---|
| 136 | | } |
|---|
| 137 | | |
|---|
| 138 | | public void put(int index, Scriptable start, Object value) { |
|---|
| 139 | | if (start == this) { |
|---|
| 140 | | if (hasAdapterFunctions && isAdapter.get()) { |
|---|
| 141 | | Function func = getAdapteeFunction(PUT_PROP); |
|---|
| 142 | | if( func != null) { |
|---|
| 143 | | call(func, new Object[] { index, value }); |
|---|
| 144 | | return; |
|---|
| 145 | | } |
|---|
| 146 | | } |
|---|
| 147 | | super.put(index, start, value); |
|---|
| 148 | | } else { |
|---|
| 149 | | start.put(index, start, value); |
|---|
| 150 | | } |
|---|
| 151 | | } |
|---|
| 152 | | |
|---|
| 153 | | public void delete(String name) { |
|---|
| 154 | | if (hasAdapterFunctions && isAdapter.get()) { |
|---|
| 155 | | Function func = getAdapteeFunction(DEL_PROP); |
|---|
| 156 | | if (func != null) { |
|---|
| 157 | | call(func, new Object[] { name }); |
|---|
| 158 | | return; |
|---|
| 159 | | } |
|---|
| 160 | | } |
|---|
| 161 | | super.delete(name); |
|---|
| 162 | | } |
|---|
| 163 | | |
|---|
| 164 | | public void delete(int index) { |
|---|
| 165 | | if (hasAdapterFunctions && isAdapter.get()) { |
|---|
| 166 | | Function func = getAdapteeFunction(DEL_PROP); |
|---|
| 167 | | if (func != null) { |
|---|
| 168 | | call(func, new Object[] { index }); |
|---|
| 169 | | return; |
|---|
| 170 | | } |
|---|
| 171 | | } |
|---|
| 172 | | super.delete(index); |
|---|
| 173 | | } |
|---|
| 174 | | |
|---|
| 175 | | public Object[] getIds() { |
|---|
| 176 | | if (hasAdapterFunctions && isAdapter.get()) { |
|---|
| 177 | | Function func = getAdapteeFunction(GET_PROPIDS); |
|---|
| 178 | | if (func != null) { |
|---|
| 179 | | Object val = call(func, RhinoEngine.EMPTY_ARGS); |
|---|
| 180 | | // in most cases, adaptee would return native JS array |
|---|
| 181 | | if (val instanceof NativeArray) { |
|---|
| 182 | | NativeArray array = (NativeArray) val; |
|---|
| 183 | | Object[] res = new Object[(int)array.getLength()]; |
|---|
| 184 | | for (int index = 0; index < res.length; index++) { |
|---|
| 185 | | res[index] = mapToId(array.get(index, array)); |
|---|
| 186 | | } |
|---|
| 187 | | return res; |
|---|
| 188 | | } else if (val instanceof NativeJavaArray) { |
|---|
| 189 | | // may be attempt wrapped Java array |
|---|
| 190 | | Object tmp = ((NativeJavaArray)val).unwrap(); |
|---|
| 191 | | Object[] res; |
|---|
| 192 | | if (tmp.getClass() == Object[].class) { |
|---|
| 193 | | Object[] array = (Object[]) tmp; |
|---|
| 194 | | res = new Object[array.length]; |
|---|
| 195 | | for (int index = 0; index < array.length; index++) { |
|---|
| 196 | | res[index] = mapToId(array[index]); |
|---|
| 197 | | } |
|---|
| 198 | | } else { |
|---|
| 199 | | // just return an empty array |
|---|
| 200 | | res = Context.emptyArgs; |
|---|
| 201 | | } |
|---|
| 202 | | return res; |
|---|
| 203 | | } else { |
|---|
| 204 | | // some other return type, just return empty array |
|---|
| 205 | | return Context.emptyArgs; |
|---|
| 206 | | } |
|---|
| 207 | | } |
|---|
| 208 | | } |
|---|
| 209 | | return super.getIds(); |
|---|
| 210 | | } |
|---|
| 211 | | |
|---|
| 212 | | private Function getAdapteeFunction(String name) { |
|---|
| 213 | | Object o = super.get(name, this); |
|---|
| 214 | | if (o instanceof Function) return (Function) o; |
|---|
| 215 | | Scriptable proto = getPrototype(); |
|---|
| 216 | | if (proto != null) { |
|---|
| 217 | | o = ScriptableObject.getProperty(proto, name); |
|---|
| 218 | | } |
|---|
| 219 | | return (o instanceof Function)? (Function)o : null; |
|---|
| 220 | | } |
|---|
| 221 | | |
|---|
| 222 | | private Object call(Function func, Object[] args) { |
|---|
| 223 | | Context cx = Context.getCurrentContext(); |
|---|
| 224 | | try { |
|---|
| 225 | | isAdapter.set(false); |
|---|
| 226 | | return func.call(cx, this, this, args); |
|---|
| 227 | | } catch (RhinoException re) { |
|---|
| 228 | | throw Context.reportRuntimeError(re.getMessage()); |
|---|
| 229 | | } finally { |
|---|
| 230 | | isAdapter.set(true); |
|---|
| 231 | | } |
|---|
| 232 | | } |
|---|
| 233 | | |
|---|
| 234 | | // map a property id. Property id can only be an Integer or String |
|---|
| 235 | | private Object mapToId(Object tmp) { |
|---|
| 236 | | if (tmp instanceof Number) { |
|---|
| 237 | | return ((Number)tmp).intValue(); |
|---|
| 238 | | } else { |
|---|
| 239 | | return Context.toString(tmp); |
|---|
| 240 | | } |
|---|
| 241 | | } |
|---|
| 242 | | |
|---|
| 243 | | class AdapterFlag extends ThreadLocal<Boolean> { |
|---|
| 244 | | protected Boolean initialValue() { |
|---|
| 245 | | return hasAdapterFunctions; |
|---|
| 246 | | } |
|---|
| 247 | | } |
|---|
| 248 | | |
|---|
| 249 | | // names of adaptee JavaScript functions |
|---|
| 250 | | private static final String GET_PROP = "__get__"; |
|---|
| 251 | | private static final String HAS_PROP = "__has__"; |
|---|
| 252 | | private static final String PUT_PROP = "__put__"; |
|---|
| 253 | | private static final String DEL_PROP = "__delete__"; |
|---|
| 254 | | private static final String GET_PROPIDS = "__getIds__"; |
|---|
| 255 | | |
|---|