1 /* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.nashorn.internal.runtime.linker; 27 28 import java.util.Arrays; 29 import jdk.nashorn.internal.runtime.ScriptRuntime; 30 31 /** 32 * Represents a Nashorn callable bound to a receiver and optionally arguments. Note that objects of this class 33 * are just the tuples of a callable and a bound this and arguments, without any behavior. All the behavior is 34 * defined in the {@code BoundCallableLinker}. 35 */ 36 public final class BoundCallable { 37 private final Object callable; 38 private final Object boundThis; 39 private final Object[] boundArgs; 40 41 BoundCallable(final Object callable, final Object boundThis, final Object[] boundArgs) { 42 this.callable = callable; 43 this.boundThis = boundThis; 44 this.boundArgs = isEmptyArray(boundArgs) ? ScriptRuntime.EMPTY_ARRAY : boundArgs.clone(); 45 } 46 47 private BoundCallable(final BoundCallable original, final Object[] extraBoundArgs) { 48 this.callable = original.callable; 49 this.boundThis = original.boundThis; 50 this.boundArgs = original.concatenateBoundArgs(extraBoundArgs); 51 } 52 53 Object getCallable() { 54 return callable; 55 } 56 57 Object getBoundThis() { 58 return boundThis; 59 } 60 61 Object[] getBoundArgs() { 62 return boundArgs; 63 } 64 65 BoundCallable bind(final Object[] extraBoundArgs) { 66 if (isEmptyArray(extraBoundArgs)) { 67 return this; 68 } 69 return new BoundCallable(this, extraBoundArgs); 70 } 71 72 private Object[] concatenateBoundArgs(final Object[] extraBoundArgs) { 73 if (boundArgs.length == 0) { 74 return extraBoundArgs.clone(); 75 } 76 final int origBoundArgsLen = boundArgs.length; 77 final int extraBoundArgsLen = extraBoundArgs.length; 78 final Object[] newBoundArgs = new Object[origBoundArgsLen + extraBoundArgsLen]; 79 System.arraycopy(boundArgs, 0, newBoundArgs, 0, origBoundArgsLen); 80 System.arraycopy(extraBoundArgs, 0, newBoundArgs, origBoundArgsLen, extraBoundArgsLen); 81 return newBoundArgs; 82 } 83 84 private static boolean isEmptyArray(final Object[] a) { 85 return a == null || a.length == 0; 86 } 87 88 @Override 89 public String toString() { 90 final StringBuilder b = new StringBuilder(callable.toString()).append(" on ").append(boundThis); 91 if (boundArgs.length != 0) { 92 b.append(" with ").append(Arrays.toString(boundArgs)); 93 } 94 return b.toString(); 95 } 96 }