src/jdk/nashorn/internal/runtime/ListAdapter.java

Thu, 31 Aug 2017 15:30:47 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:30:47 +0800
changeset 952
6d5471a497fb
parent 771
5ab19753ce4a
parent 0
b1a7da25b547
child 1205
4112748288bb
permissions
-rw-r--r--

merge

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.internal.runtime;
aoqi@0 27
aoqi@0 28 import java.util.AbstractList;
aoqi@0 29 import java.util.Deque;
aoqi@0 30 import java.util.Iterator;
aoqi@0 31 import java.util.ListIterator;
aoqi@0 32 import java.util.NoSuchElementException;
aoqi@0 33 import java.util.RandomAccess;
aoqi@0 34 import java.util.concurrent.Callable;
aoqi@0 35 import jdk.nashorn.api.scripting.JSObject;
aoqi@0 36 import jdk.nashorn.api.scripting.ScriptObjectMirror;
aoqi@0 37 import jdk.nashorn.internal.objects.Global;
aoqi@0 38 import jdk.nashorn.internal.runtime.linker.Bootstrap;
aoqi@0 39 import jdk.nashorn.internal.runtime.linker.InvokeByName;
aoqi@0 40
aoqi@0 41 /**
aoqi@0 42 * An adapter that can wrap any ECMAScript Array-like object (that adheres to the array rules for the property
aoqi@0 43 * {@code length} and having conforming {@code push}, {@code pop}, {@code shift}, {@code unshift}, and {@code splice}
aoqi@0 44 * methods) and expose it as both a Java list and double-ended queue. While script arrays aren't necessarily efficient
aoqi@0 45 * as dequeues, it's still slightly more efficient to be able to translate dequeue operations into pushes, pops, shifts,
aoqi@0 46 * and unshifts, than to blindly translate all list's add/remove operations into splices. Also, it is conceivable that a
aoqi@0 47 * custom script object that implements an Array-like API can have a background data representation that is optimized
aoqi@0 48 * for dequeue-like access. Note that with ECMAScript arrays, {@code push} and {@code pop} operate at the end of the
aoqi@0 49 * array, while in Java {@code Deque} they operate on the front of the queue and as such the Java dequeue
aoqi@0 50 * {@link #push(Object)} and {@link #pop()} operations will translate to {@code unshift} and {@code shift} script
aoqi@0 51 * operations respectively, while {@link #addLast(Object)} and {@link #removeLast()} will translate to {@code push} and
aoqi@0 52 * {@code pop}.
aoqi@0 53 */
aoqi@0 54 public abstract class ListAdapter extends AbstractList<Object> implements RandomAccess, Deque<Object> {
aoqi@0 55 // These add to the back and front of the list
aoqi@0 56 private static final Object PUSH = new Object();
aoqi@0 57 private static InvokeByName getPUSH() {
aoqi@0 58 return Context.getGlobal().getInvokeByName(PUSH,
aoqi@0 59 new Callable<InvokeByName>() {
aoqi@0 60 @Override
aoqi@0 61 public InvokeByName call() {
aoqi@0 62 return new InvokeByName("push", Object.class, void.class, Object.class);
aoqi@0 63 }
aoqi@0 64 });
aoqi@0 65 }
aoqi@0 66
aoqi@0 67 private static final Object UNSHIFT = new Object();
aoqi@0 68 private static InvokeByName getUNSHIFT() {
aoqi@0 69 return Context.getGlobal().getInvokeByName(UNSHIFT,
aoqi@0 70 new Callable<InvokeByName>() {
aoqi@0 71 @Override
aoqi@0 72 public InvokeByName call() {
aoqi@0 73 return new InvokeByName("unshift", Object.class, void.class, Object.class);
aoqi@0 74 }
aoqi@0 75 });
aoqi@0 76 }
aoqi@0 77
aoqi@0 78 // These remove from the back and front of the list
aoqi@0 79 private static final Object POP = new Object();
aoqi@0 80 private static InvokeByName getPOP() {
aoqi@0 81 return Context.getGlobal().getInvokeByName(POP,
aoqi@0 82 new Callable<InvokeByName>() {
aoqi@0 83 @Override
aoqi@0 84 public InvokeByName call() {
aoqi@0 85 return new InvokeByName("pop", Object.class, Object.class);
aoqi@0 86 }
aoqi@0 87 });
aoqi@0 88 }
aoqi@0 89
aoqi@0 90 private static final Object SHIFT = new Object();
aoqi@0 91 private static InvokeByName getSHIFT() {
aoqi@0 92 return Context.getGlobal().getInvokeByName(SHIFT,
aoqi@0 93 new Callable<InvokeByName>() {
aoqi@0 94 @Override
aoqi@0 95 public InvokeByName call() {
aoqi@0 96 return new InvokeByName("shift", Object.class, Object.class);
aoqi@0 97 }
aoqi@0 98 });
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 // These insert and remove in the middle of the list
aoqi@0 102 private static final Object SPLICE_ADD = new Object();
aoqi@0 103 private static InvokeByName getSPLICE_ADD() {
aoqi@0 104 return Context.getGlobal().getInvokeByName(SPLICE_ADD,
aoqi@0 105 new Callable<InvokeByName>() {
aoqi@0 106 @Override
aoqi@0 107 public InvokeByName call() {
aoqi@0 108 return new InvokeByName("splice", Object.class, void.class, int.class, int.class, Object.class);
aoqi@0 109 }
aoqi@0 110 });
aoqi@0 111 }
aoqi@0 112
aoqi@0 113 private static final Object SPLICE_REMOVE = new Object();
aoqi@0 114 private static InvokeByName getSPLICE_REMOVE() {
aoqi@0 115 return Context.getGlobal().getInvokeByName(SPLICE_REMOVE,
aoqi@0 116 new Callable<InvokeByName>() {
aoqi@0 117 @Override
aoqi@0 118 public InvokeByName call() {
aoqi@0 119 return new InvokeByName("splice", Object.class, void.class, int.class, int.class);
aoqi@0 120 }
aoqi@0 121 });
aoqi@0 122 }
aoqi@0 123
aoqi@0 124 /** wrapped object */
aoqi@0 125 protected final Object obj;
aoqi@0 126
aoqi@0 127 // allow subclasses only in this package
aoqi@0 128 ListAdapter(final Object obj) {
aoqi@0 129 this.obj = obj;
aoqi@0 130 }
aoqi@0 131
aoqi@0 132 /**
aoqi@0 133 * Factory to create a ListAdapter for a given script object.
aoqi@0 134 *
aoqi@0 135 * @param obj script object to wrap as a ListAdapter
aoqi@0 136 * @return A ListAdapter wrapper object
aoqi@0 137 */
aoqi@0 138 public static ListAdapter create(final Object obj) {
aoqi@0 139 if (obj instanceof ScriptObject) {
aoqi@0 140 final Object mirror = ScriptObjectMirror.wrap(obj, Context.getGlobal());
aoqi@0 141 return new JSObjectListAdapter((JSObject)mirror);
aoqi@0 142 } else if (obj instanceof JSObject) {
aoqi@0 143 return new JSObjectListAdapter((JSObject)obj);
aoqi@0 144 } else {
aoqi@0 145 throw new IllegalArgumentException("ScriptObject or JSObject expected");
aoqi@0 146 }
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 @Override
aoqi@0 150 public final Object get(final int index) {
aoqi@0 151 checkRange(index);
aoqi@0 152 return getAt(index);
aoqi@0 153 }
aoqi@0 154
aoqi@0 155 /**
aoqi@0 156 * Get object at an index
aoqi@0 157 * @param index index in list
aoqi@0 158 * @return object
aoqi@0 159 */
aoqi@0 160 protected abstract Object getAt(final int index);
aoqi@0 161
aoqi@0 162 @Override
aoqi@0 163 public Object set(final int index, final Object element) {
aoqi@0 164 checkRange(index);
aoqi@0 165 final Object prevValue = getAt(index);
aoqi@0 166 setAt(index, element);
aoqi@0 167 return prevValue;
aoqi@0 168 }
aoqi@0 169
aoqi@0 170 /**
aoqi@0 171 * Set object at an index
aoqi@0 172 * @param index index in list
aoqi@0 173 * @param element element
aoqi@0 174 */
aoqi@0 175 protected abstract void setAt(final int index, final Object element);
aoqi@0 176
aoqi@0 177 private void checkRange(int index) {
aoqi@0 178 if(index < 0 || index >= size()) {
aoqi@0 179 throw invalidIndex(index);
aoqi@0 180 }
aoqi@0 181 }
aoqi@0 182
aoqi@0 183 @Override
aoqi@0 184 public final void push(final Object e) {
aoqi@0 185 addFirst(e);
aoqi@0 186 }
aoqi@0 187
aoqi@0 188 @Override
aoqi@0 189 public final boolean add(final Object e) {
aoqi@0 190 addLast(e);
aoqi@0 191 return true;
aoqi@0 192 }
aoqi@0 193
aoqi@0 194 @Override
aoqi@0 195 public final void addFirst(final Object e) {
aoqi@0 196 try {
aoqi@0 197 final InvokeByName unshiftInvoker = getUNSHIFT();
aoqi@0 198 final Object fn = unshiftInvoker.getGetter().invokeExact(obj);
aoqi@0 199 checkFunction(fn, unshiftInvoker);
aoqi@0 200 unshiftInvoker.getInvoker().invokeExact(fn, obj, e);
aoqi@0 201 } catch(RuntimeException | Error ex) {
aoqi@0 202 throw ex;
aoqi@0 203 } catch(Throwable t) {
aoqi@0 204 throw new RuntimeException(t);
aoqi@0 205 }
aoqi@0 206 }
aoqi@0 207
aoqi@0 208 @Override
aoqi@0 209 public final void addLast(final Object e) {
aoqi@0 210 try {
aoqi@0 211 final InvokeByName pushInvoker = getPUSH();
aoqi@0 212 final Object fn = pushInvoker.getGetter().invokeExact(obj);
aoqi@0 213 checkFunction(fn, pushInvoker);
aoqi@0 214 pushInvoker.getInvoker().invokeExact(fn, obj, e);
aoqi@0 215 } catch(RuntimeException | Error ex) {
aoqi@0 216 throw ex;
aoqi@0 217 } catch(Throwable t) {
aoqi@0 218 throw new RuntimeException(t);
aoqi@0 219 }
aoqi@0 220 }
aoqi@0 221
aoqi@0 222 @Override
aoqi@0 223 public final void add(final int index, final Object e) {
aoqi@0 224 try {
aoqi@0 225 if(index < 0) {
aoqi@0 226 throw invalidIndex(index);
aoqi@0 227 } else if(index == 0) {
aoqi@0 228 addFirst(e);
aoqi@0 229 } else {
aoqi@0 230 final int size = size();
aoqi@0 231 if(index < size) {
aoqi@0 232 final InvokeByName spliceAddInvoker = getSPLICE_ADD();
aoqi@0 233 final Object fn = spliceAddInvoker.getGetter().invokeExact(obj);
aoqi@0 234 checkFunction(fn, spliceAddInvoker);
aoqi@0 235 spliceAddInvoker.getInvoker().invokeExact(fn, obj, index, 0, e);
aoqi@0 236 } else if(index == size) {
aoqi@0 237 addLast(e);
aoqi@0 238 } else {
aoqi@0 239 throw invalidIndex(index);
aoqi@0 240 }
aoqi@0 241 }
aoqi@0 242 } catch(final RuntimeException | Error ex) {
aoqi@0 243 throw ex;
aoqi@0 244 } catch(final Throwable t) {
aoqi@0 245 throw new RuntimeException(t);
aoqi@0 246 }
aoqi@0 247 }
aoqi@0 248 private static void checkFunction(final Object fn, final InvokeByName invoke) {
aoqi@0 249 if(!(Bootstrap.isCallable(fn))) {
aoqi@0 250 throw new UnsupportedOperationException("The script object doesn't have a function named " + invoke.getName());
aoqi@0 251 }
aoqi@0 252 }
aoqi@0 253
aoqi@0 254 private static IndexOutOfBoundsException invalidIndex(final int index) {
aoqi@0 255 return new IndexOutOfBoundsException(String.valueOf(index));
aoqi@0 256 }
aoqi@0 257
aoqi@0 258 @Override
aoqi@0 259 public final boolean offer(final Object e) {
aoqi@0 260 return offerLast(e);
aoqi@0 261 }
aoqi@0 262
aoqi@0 263 @Override
aoqi@0 264 public final boolean offerFirst(final Object e) {
aoqi@0 265 addFirst(e);
aoqi@0 266 return true;
aoqi@0 267 }
aoqi@0 268
aoqi@0 269 @Override
aoqi@0 270 public final boolean offerLast(final Object e) {
aoqi@0 271 addLast(e);
aoqi@0 272 return true;
aoqi@0 273 }
aoqi@0 274
aoqi@0 275 @Override
aoqi@0 276 public final Object pop() {
aoqi@0 277 return removeFirst();
aoqi@0 278 }
aoqi@0 279
aoqi@0 280 @Override
aoqi@0 281 public final Object remove() {
aoqi@0 282 return removeFirst();
aoqi@0 283 }
aoqi@0 284
aoqi@0 285 @Override
aoqi@0 286 public final Object removeFirst() {
aoqi@0 287 checkNonEmpty();
aoqi@0 288 return invokeShift();
aoqi@0 289 }
aoqi@0 290
aoqi@0 291 @Override
aoqi@0 292 public final Object removeLast() {
aoqi@0 293 checkNonEmpty();
aoqi@0 294 return invokePop();
aoqi@0 295 }
aoqi@0 296
aoqi@0 297 private void checkNonEmpty() {
aoqi@0 298 if(isEmpty()) {
aoqi@0 299 throw new NoSuchElementException();
aoqi@0 300 }
aoqi@0 301 }
aoqi@0 302
aoqi@0 303 @Override
aoqi@0 304 public final Object remove(final int index) {
aoqi@0 305 if(index < 0) {
aoqi@0 306 throw invalidIndex(index);
aoqi@0 307 } else if (index == 0) {
aoqi@0 308 return invokeShift();
aoqi@0 309 } else {
aoqi@0 310 final int maxIndex = size() - 1;
aoqi@0 311 if(index < maxIndex) {
aoqi@0 312 final Object prevValue = get(index);
aoqi@0 313 invokeSpliceRemove(index, 1);
aoqi@0 314 return prevValue;
aoqi@0 315 } else if(index == maxIndex) {
aoqi@0 316 return invokePop();
aoqi@0 317 } else {
aoqi@0 318 throw invalidIndex(index);
aoqi@0 319 }
aoqi@0 320 }
aoqi@0 321 }
aoqi@0 322
aoqi@0 323 private Object invokeShift() {
aoqi@0 324 try {
aoqi@0 325 final InvokeByName shiftInvoker = getSHIFT();
aoqi@0 326 final Object fn = shiftInvoker.getGetter().invokeExact(obj);
aoqi@0 327 checkFunction(fn, shiftInvoker);
aoqi@0 328 return shiftInvoker.getInvoker().invokeExact(fn, obj);
aoqi@0 329 } catch(RuntimeException | Error ex) {
aoqi@0 330 throw ex;
aoqi@0 331 } catch(Throwable t) {
aoqi@0 332 throw new RuntimeException(t);
aoqi@0 333 }
aoqi@0 334 }
aoqi@0 335
aoqi@0 336 private Object invokePop() {
aoqi@0 337 try {
aoqi@0 338 final InvokeByName popInvoker = getPOP();
aoqi@0 339 final Object fn = popInvoker.getGetter().invokeExact(obj);
aoqi@0 340 checkFunction(fn, popInvoker);
aoqi@0 341 return popInvoker.getInvoker().invokeExact(fn, obj);
aoqi@0 342 } catch(RuntimeException | Error ex) {
aoqi@0 343 throw ex;
aoqi@0 344 } catch(Throwable t) {
aoqi@0 345 throw new RuntimeException(t);
aoqi@0 346 }
aoqi@0 347 }
aoqi@0 348
aoqi@0 349 @Override
aoqi@0 350 protected final void removeRange(final int fromIndex, final int toIndex) {
aoqi@0 351 invokeSpliceRemove(fromIndex, toIndex - fromIndex);
aoqi@0 352 }
aoqi@0 353
aoqi@0 354 private void invokeSpliceRemove(final int fromIndex, final int count) {
aoqi@0 355 try {
aoqi@0 356 final InvokeByName spliceRemoveInvoker = getSPLICE_REMOVE();
aoqi@0 357 final Object fn = spliceRemoveInvoker.getGetter().invokeExact(obj);
aoqi@0 358 checkFunction(fn, spliceRemoveInvoker);
aoqi@0 359 spliceRemoveInvoker.getInvoker().invokeExact(fn, obj, fromIndex, count);
aoqi@0 360 } catch(RuntimeException | Error ex) {
aoqi@0 361 throw ex;
aoqi@0 362 } catch(Throwable t) {
aoqi@0 363 throw new RuntimeException(t);
aoqi@0 364 }
aoqi@0 365 }
aoqi@0 366
aoqi@0 367 @Override
aoqi@0 368 public final Object poll() {
aoqi@0 369 return pollFirst();
aoqi@0 370 }
aoqi@0 371
aoqi@0 372 @Override
aoqi@0 373 public final Object pollFirst() {
aoqi@0 374 return isEmpty() ? null : invokeShift();
aoqi@0 375 }
aoqi@0 376
aoqi@0 377 @Override
aoqi@0 378 public final Object pollLast() {
aoqi@0 379 return isEmpty() ? null : invokePop();
aoqi@0 380 }
aoqi@0 381
aoqi@0 382 @Override
aoqi@0 383 public final Object peek() {
aoqi@0 384 return peekFirst();
aoqi@0 385 }
aoqi@0 386
aoqi@0 387 @Override
aoqi@0 388 public final Object peekFirst() {
aoqi@0 389 return isEmpty() ? null : get(0);
aoqi@0 390 }
aoqi@0 391
aoqi@0 392 @Override
aoqi@0 393 public final Object peekLast() {
aoqi@0 394 return isEmpty() ? null : get(size() - 1);
aoqi@0 395 }
aoqi@0 396
aoqi@0 397 @Override
aoqi@0 398 public final Object element() {
aoqi@0 399 return getFirst();
aoqi@0 400 }
aoqi@0 401
aoqi@0 402 @Override
aoqi@0 403 public final Object getFirst() {
aoqi@0 404 checkNonEmpty();
aoqi@0 405 return get(0);
aoqi@0 406 }
aoqi@0 407
aoqi@0 408 @Override
aoqi@0 409 public final Object getLast() {
aoqi@0 410 checkNonEmpty();
aoqi@0 411 return get(size() - 1);
aoqi@0 412 }
aoqi@0 413
aoqi@0 414 @Override
aoqi@0 415 public final Iterator<Object> descendingIterator() {
aoqi@0 416 final ListIterator<Object> it = listIterator(size());
aoqi@0 417 return new Iterator<Object>() {
aoqi@0 418 @Override
aoqi@0 419 public boolean hasNext() {
aoqi@0 420 return it.hasPrevious();
aoqi@0 421 }
aoqi@0 422
aoqi@0 423 @Override
aoqi@0 424 public Object next() {
aoqi@0 425 return it.previous();
aoqi@0 426 }
aoqi@0 427
aoqi@0 428 @Override
aoqi@0 429 public void remove() {
aoqi@0 430 it.remove();
aoqi@0 431 }
aoqi@0 432 };
aoqi@0 433 }
aoqi@0 434
aoqi@0 435 @Override
aoqi@0 436 public final boolean removeFirstOccurrence(final Object o) {
aoqi@0 437 return removeOccurrence(o, iterator());
aoqi@0 438 }
aoqi@0 439
aoqi@0 440 @Override
aoqi@0 441 public final boolean removeLastOccurrence(final Object o) {
aoqi@0 442 return removeOccurrence(o, descendingIterator());
aoqi@0 443 }
aoqi@0 444
aoqi@0 445 private static boolean removeOccurrence(final Object o, final Iterator<Object> it) {
aoqi@0 446 while(it.hasNext()) {
aoqi@0 447 final Object e = it.next();
aoqi@0 448 if(o == null ? e == null : o.equals(e)) {
aoqi@0 449 it.remove();
aoqi@0 450 return true;
aoqi@0 451 }
aoqi@0 452 }
aoqi@0 453 return false;
aoqi@0 454 }
aoqi@0 455 }

mercurial