src/jdk/internal/dynalink/linker/GuardedInvocation.java

Wed, 20 Aug 2014 10:26:01 +0200

author
attila
date
Wed, 20 Aug 2014 10:26:01 +0200
changeset 963
e2497b11a021
parent 962
ac62e33a99b0
child 1205
4112748288bb
child 1231
701c1dcdf733
permissions
-rw-r--r--

8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
8027958: NASHORN TEST: Create tests to test markdown javascript engine work with Nashorn
8028345: Remove nashorn repo "bin" scripts to avoid confusion with JDK bin launcher programs
8029090: Developers should be able to pass nashorn properties and enable/disable JFR from command line
8030169: Need regression test for bug JDK-8010731
8033105: Make sure Nashorn test harness can run zlib benchmark
8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
8035820: Optimistic recompilation
8035836: Array performance improvements
8036127: Prototype filter needs to be applied to getter guard as well, not just getter
8036986: Test should check that correctly type is returned running with optimistic. If optimistic assumption was wrong we should get the right one.
8037086: Check that deoptimizing recompilations are correct
8037177: -Dnashorn.optimistic should be enabled by default, meaning that it has to be explicitly set to false to run with the jdk 8 style conservative types
8037534: Use scope types to determine optimistic types
8037572: Add more test cases to check static types
8037967: Broke the build, by commiting without saving the last review comment
8038223: Symbol trace debug output takes time
8038396: fix for the compiler expression evaluator to be more inquisitive about types
8038398: OptimisticRecompilationTest fails on staging repo nashorn/jdk9/nashorn due to test framework
8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
8038413: NPE in unboxInteger
8038416: Access to undefined scoped variables deoptimized too much
8038426: Move all loggers from process wide scope into Global scope
8038799: Guard and unbox boxed primitives types on setting them in Properties to avoid megamorphisism
8038945: Simplify strict undefined checks
8039044: Expand undefined intrinsics for all commutative combinators of scrict undefined checks
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
8040024: BranchOptimizer produces bad code for NaN FP comparison
8040089: Apply to call transform was incomplete. Now passes all tests and performance is back
8040093: Make sure that optimistic splitting works in optimistic types
8040102: Remove all references to Unsafe and definition of anonymous clases from the code
8040655: When processing a RewriteException debug object, the return value has already been reset to null. We need to catch this value before that.
8041434: Add synchronization to the common global constants structure
8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
8041905: Fix apply2call bug that prevented avatar.js unit tests from running correctly
8041995: Problems when loading tree expressions with several optimistic program points when optimistically initializing ObjectNodes
8042118: Separate types from symbols
8043002: Improve performance of Nashorn equality operators
8043003: Use strongly referenced generic invokers
8043004: Reduce variability at JavaAdapter call sites
8043132: Nashorn : all tests failed with java.security.AccessControlException
8043133: Fix corner cases of JDK-8041995
8043137: Collapse long sequences of NOP in Nashorn bytecode output
8043232: Index selection of overloaded java new constructors
8043235: Type-based optimizations interfere with continuation methods
8043431: Fix yet another corner case of JDK-8041995
8043504: Octane test harness was missing argument to print_always at one callsite, causing erroneous logging
8043605: Enable history for empty property maps
8043608: Make equality tests inline better
8043611: Move timing dependent benchmark for apply2call specialization to currently_failing. It is dependent that nothing takes machine time when doing the two runs, causing spurious assertions. Suggest running octane.raytrace manually instead to verify that this works, or incorporating it in the nightly test suite
8043632: Parallelize class installation and various script fixes.
8043633: In order to remove global state outside of contexts, make sure Timing class is an instance and not a static global collection of data. Move into Context. Move -Dnashorn.timing to an official logging option.
8043956: Make code caching work with optimistic typing and lazy compilation
8044012: Integrate the latest best known performance flags int ant octane jobs, and make sure that it's easy to compare 'ant octane-nashorn' and 'ant octane-v8' at the push of a button. (or rather; the entry of a command line)
8044102: Ensure bechmark exclude list for Octane benchmarks is in only one place, project.properties, and fix benchmark harness
8044154: Nashorn : all tests failed with java.security.AccessControlException
8044171: Make optimistic exception handlers smaller
8044502: Get rid of global optimistic flag
8044518: Ensure exceptions related to optimistic recompilation are not serializable
8044533: Deoptimizing negation produces wrong result for zero
8044534: Constant folding for unary + should produce int for boolean literals
8044760: Avoid PropertyMap duplicate for global instances
8044786: Some tests fail with non-optimistic compilation
8044803: Unnecessary restOf check
8044816: On-demand compiled top-level program doesn't need :createProgramFunction
8044851: nashorn properties leak memory
8046013: TypeError: Cannot apply "with" to non script object
8046014: MultiGlobalCompiledScript should cache :createProgramFunction handle
8046025: AccessorProperty.getGetter is not threadsafe
8046026: CompiledFunction.relinkComposableInvoker assert is being hit
8046201: Avoid repeated flattening of nested ConsStrings
8046215: Running uncompilable scripts throws NullPointerException
8046898: Make sure that lazy compilation is the default, remove redundant "enable lazy compilation" flags, added warning message if compile logging is enabled and lazy is switched off. Verified existing test suite code coverage equivalence between lazy and eager.
8046905: apply on apply is broken
8046921: Deoptimization type information peristence
8047035: (function() "hello")() crashes in Lexer with jdk9
8047057: Add a regression test for the passing test cases from JDK-8042304
8047067: all eval arguments need to be copied in Lower
8047078: Fuzzing bug discovered when ArrayLiteralNodes weren't immutable
8047166: 'do with({}) break ; while(0);' crashes in CodeGenerator
8047331: Assertion in CompiledFunction when running earley-boyer after Merge
8047357: More precise synthetic return + unreachable throw
8047359: large string size RangeError should be thrown rather than reporting negative length
8047369: Add regression tests for passing test cases of JDK-8024971
8047371: local variable declaration in TypeEvaluator should use ScriptObject.addOwnProperty instead of .set
8047728: (function(x){var o={x:0}; with(o){delete x} return o.x})() evaluates to 0 instead of undefined
8047959: bindings created for declarations in eval code are not mutable
8048009: Type info caching accidentally defeated
8048071: eval within 'with' statement does not use correct scope if with scope expression has a copy of eval
8048079: Persistent code store is broken after optimistic types merge
8048505: ScriptingFunctions.readFully couldn't handle file names represented as ConsStrings
8048586: String concatenation with optimistic types is slow
8048718: JSON.parse('{"0":0, "64":0}') throws ArrayindexOutOfBoundsException
8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
8049086: Minor API convenience functions on "Java" object
8049222: JSType class exposes public mutable arrays
8049223: RewriteException class exposes public mutable arrays
8049242: Explicit constructor overload selection should work with StaticClass as well
8049318: Test hideLocationProperties.js fails on Window due to backslash in path
8049524: Global object initialization via javax.script API should be minimal
8050432: javax.script.filename variable should not be enumerable with nashorn engine's ENGINE_SCOPE bindings
8050964: OptimisticTypesPersistence.java should use java.util.Date instead of java.sql.Date
8051019: Separate src and test execution sandbox directories
8051346: Test262 tests for ECMAScript 5 now in branch "es5-tests"
8051439: Wrong type calculated for ADD operator with undefined operand
8051839: GuardedInvocation needs to clone an argument
8053908: jdeps is not PATH on Mac, results in ant clean test failure on Mac
8053910: ScriptObjectMirror causing havoc with Invocation interface
8053913: Auto format caused warning in CompositeTypeBasedGuardingDynamicLinker
8054223: Nashorn: AssertionError when use __DIR__ and ScriptEngine.eval()
8054411: Add nashorn.args.prepend system property
8054503: test/script/external/test262/test/suite/ch12/12.6/12.6.4/12.6.4-2.js fails with tip
8054651: Global.initConstructor and ScriptFunction.getPrototype(Object) can have stricter types
8054898: Avoid creation of empty type info files
8054993: type info cache may be disabled for test262 and tests explicitly changing that property should use @fork
8055034: jjs exits interactive mode if exception was thrown when trying to print value of last evaluated expression
8055042: Compile-time expression evaluator was missing variables
8055107: Extension directives to turn on callsite profiling, tracing, AST print and other debug features locally
8055139: test/script/trusted/JDK-8055107.js fails with access control exception
8055186: Backport Nashorn optimistic typing to 8u repository
8055529: Clean up the bin directory
Reviewed-by: jlaskey, lagergren, sundar
Contributed-by: marcus.largergren@oracle.com, hannes.wallnoefer@oracle.com, sundararajan.athijegannathan@oracle.com

attila@90 1 /*
attila@90 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
attila@90 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
attila@90 4 *
attila@90 5 * This code is free software; you can redistribute it and/or modify it
attila@90 6 * under the terms of the GNU General Public License version 2 only, as
attila@90 7 * published by the Free Software Foundation. Oracle designates this
attila@90 8 * particular file as subject to the "Classpath" exception as provided
attila@90 9 * by Oracle in the LICENSE file that accompanied this code.
attila@90 10 *
attila@90 11 * This code is distributed in the hope that it will be useful, but WITHOUT
attila@90 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
attila@90 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
attila@90 14 * version 2 for more details (a copy is included in the LICENSE file that
attila@90 15 * accompanied this code).
attila@90 16 *
attila@90 17 * You should have received a copy of the GNU General Public License version
attila@90 18 * 2 along with this work; if not, write to the Free Software Foundation,
attila@90 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
attila@90 20 *
attila@90 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
attila@90 22 * or visit www.oracle.com if you need additional information or have any
attila@90 23 * questions.
attila@90 24 */
attila@90 25
attila@90 26 /*
attila@90 27 * This file is available under and governed by the GNU General Public
attila@90 28 * License version 2 only, as published by the Free Software Foundation.
attila@90 29 * However, the following notice accompanied the original version of this
attila@90 30 * file, and Oracle licenses the original version of this file under the BSD
attila@90 31 * license:
attila@90 32 */
attila@90 33 /*
attila@90 34 Copyright 2009-2013 Attila Szegedi
attila@90 35
attila@90 36 Licensed under both the Apache License, Version 2.0 (the "Apache License")
attila@90 37 and the BSD License (the "BSD License"), with licensee being free to
attila@90 38 choose either of the two at their discretion.
attila@90 39
attila@90 40 You may not use this file except in compliance with either the Apache
attila@90 41 License or the BSD License.
attila@90 42
attila@90 43 If you choose to use this file in compliance with the Apache License, the
attila@90 44 following notice applies to you:
attila@90 45
attila@90 46 You may obtain a copy of the Apache License at
attila@90 47
attila@90 48 http://www.apache.org/licenses/LICENSE-2.0
attila@90 49
attila@90 50 Unless required by applicable law or agreed to in writing, software
attila@90 51 distributed under the License is distributed on an "AS IS" BASIS,
attila@90 52 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
attila@90 53 implied. See the License for the specific language governing
attila@90 54 permissions and limitations under the License.
attila@90 55
attila@90 56 If you choose to use this file in compliance with the BSD License, the
attila@90 57 following notice applies to you:
attila@90 58
attila@90 59 Redistribution and use in source and binary forms, with or without
attila@90 60 modification, are permitted provided that the following conditions are
attila@90 61 met:
attila@90 62 * Redistributions of source code must retain the above copyright
attila@90 63 notice, this list of conditions and the following disclaimer.
attila@90 64 * Redistributions in binary form must reproduce the above copyright
attila@90 65 notice, this list of conditions and the following disclaimer in the
attila@90 66 documentation and/or other materials provided with the distribution.
attila@90 67 * Neither the name of the copyright holder nor the names of
attila@90 68 contributors may be used to endorse or promote products derived from
attila@90 69 this software without specific prior written permission.
attila@90 70
attila@90 71 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
attila@90 72 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
attila@90 73 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
attila@90 74 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
attila@90 75 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
attila@90 76 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
attila@90 77 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
attila@90 78 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
attila@90 79 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
attila@90 80 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
attila@90 81 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
attila@90 82 */
attila@90 83
attila@90 84 package jdk.internal.dynalink.linker;
attila@90 85
attila@963 86 import static jdk.nashorn.internal.lookup.Lookup.MH;
attila@963 87
attila@90 88 import java.lang.invoke.MethodHandle;
attila@90 89 import java.lang.invoke.MethodHandles;
attila@90 90 import java.lang.invoke.MethodType;
attila@90 91 import java.lang.invoke.SwitchPoint;
attila@90 92 import java.lang.invoke.WrongMethodTypeException;
attila@90 93 import java.util.List;
attila@90 94 import jdk.internal.dynalink.CallSiteDescriptor;
attila@90 95 import jdk.internal.dynalink.support.Guards;
attila@90 96
attila@90 97 /**
attila@90 98 * Represents a conditionally valid method handle. It is an immutable triple of an invocation method handle, a guard
attila@90 99 * method handle that defines the applicability of the invocation handle, and a switch point that can be used for
attila@90 100 * external invalidation of the invocation handle. The invocation handle is suitable for invocation if the guard
attila@90 101 * handle returns true for its arguments, and as long as the switch point is not invalidated. Both the guard and the
attila@90 102 * switch point are optional; neither, one, or both can be present.
attila@90 103 *
attila@90 104 * @author Attila Szegedi
attila@90 105 */
attila@90 106 public class GuardedInvocation {
attila@90 107 private final MethodHandle invocation;
attila@90 108 private final MethodHandle guard;
attila@963 109 private final Class<? extends Throwable> exception;
attila@963 110 private final SwitchPoint[] switchPoints;
attila@963 111
attila@963 112 /**
attila@963 113 * Creates a new guarded invocation. This invocation is unconditional as it has no invalidations.
attila@963 114 *
attila@963 115 * @param invocation the method handle representing the invocation. Must not be null.
attila@963 116 * @throws NullPointerException if invocation is null.
attila@963 117 */
attila@963 118 public GuardedInvocation(final MethodHandle invocation) {
attila@963 119 this(invocation, null, (SwitchPoint)null, null);
attila@963 120 }
attila@90 121
attila@90 122 /**
attila@90 123 * Creates a new guarded invocation.
attila@90 124 *
attila@90 125 * @param invocation the method handle representing the invocation. Must not be null.
attila@90 126 * @param guard the method handle representing the guard. Must have the same method type as the invocation, except
attila@90 127 * it must return boolean. For some useful guards, check out the {@link Guards} class. It can be null to represent
attila@90 128 * an unconditional invocation, although that is unusual.
attila@90 129 * @throws NullPointerException if invocation is null.
attila@90 130 */
attila@962 131 public GuardedInvocation(final MethodHandle invocation, final MethodHandle guard) {
attila@963 132 this(invocation, guard, (SwitchPoint)null, null);
attila@963 133 }
attila@963 134
attila@963 135 /**
attila@963 136 * Creates a new guarded invocation.
attila@963 137 *
attila@963 138 * @param invocation the method handle representing the invocation. Must not be null.
attila@963 139 * @param switchPoint the optional switch point that can be used to invalidate this linkage.
attila@963 140 * @throws NullPointerException if invocation is null.
attila@963 141 */
attila@963 142 public GuardedInvocation(final MethodHandle invocation, final SwitchPoint switchPoint) {
attila@963 143 this(invocation, null, switchPoint, null);
attila@90 144 }
attila@90 145
attila@90 146 /**
attila@90 147 * Creates a new guarded invocation.
attila@90 148 *
attila@90 149 * @param invocation the method handle representing the invocation. Must not be null.
attila@90 150 * @param guard the method handle representing the guard. Must have the same method type as the invocation, except
attila@90 151 * it must return boolean. For some useful guards, check out the {@link Guards} class. It can be null. If both it
attila@90 152 * and the switch point are null, this represents an unconditional invocation, which is legal but unusual.
attila@90 153 * @param switchPoint the optional switch point that can be used to invalidate this linkage.
attila@90 154 * @throws NullPointerException if invocation is null.
attila@90 155 */
attila@962 156 public GuardedInvocation(final MethodHandle invocation, final MethodHandle guard, final SwitchPoint switchPoint) {
attila@963 157 this(invocation, guard, switchPoint, null);
attila@90 158 }
attila@90 159
attila@90 160 /**
attila@90 161 * Creates a new guarded invocation.
attila@90 162 *
attila@90 163 * @param invocation the method handle representing the invocation. Must not be null.
attila@90 164 * @param guard the method handle representing the guard. Must have the same method type as the invocation, except
attila@90 165 * it must return boolean. For some useful guards, check out the {@link Guards} class. It can be null. If both it
attila@90 166 * and the switch point are null, this represents an unconditional invocation, which is legal but unusual.
attila@963 167 * @param switchPoint the optional switch point that can be used to invalidate this linkage.
attila@963 168 * @param exception the optional exception type that is expected to be thrown by the invocation and that also
attila@963 169 * invalidates the linkage.
attila@90 170 * @throws NullPointerException if invocation is null.
attila@90 171 */
attila@963 172 public GuardedInvocation(final MethodHandle invocation, final MethodHandle guard, final SwitchPoint switchPoint, final Class<? extends Throwable> exception) {
attila@963 173 invocation.getClass(); // NPE check
attila@963 174 this.invocation = invocation;
attila@963 175 this.guard = guard;
attila@963 176 this.switchPoints = switchPoint == null ? null : new SwitchPoint[] { switchPoint };
attila@963 177 this.exception = exception;
attila@90 178 }
attila@963 179
attila@963 180 /**
attila@963 181 * Creates a new guarded invocation
attila@963 182 *
attila@963 183 * @param invocation the method handle representing the invocation. Must not be null.
attila@963 184 * @param guard the method handle representing the guard. Must have the same method type as the invocation, except
attila@963 185 * it must return boolean. For some useful guards, check out the {@link Guards} class. It can be null. If both it
attila@963 186 * and the switch point are null, this represents an unconditional invocation, which is legal but unusual.
attila@963 187 * @param switchPoints the optional switch points that can be used to invalidate this linkage.
attila@963 188 * @param exception the optional exception type that is expected to be thrown by the invocation and that also
attila@963 189 * invalidates the linkage.
attila@963 190 * @throws NullPointerException if invocation is null.
attila@963 191 */
attila@963 192 public GuardedInvocation(final MethodHandle invocation, final MethodHandle guard, final SwitchPoint[] switchPoints, final Class<? extends Throwable> exception) {
attila@963 193 invocation.getClass(); // NPE check
attila@963 194 this.invocation = invocation;
attila@963 195 this.guard = guard;
attila@963 196 this.switchPoints = switchPoints == null ? null : switchPoints.clone();
attila@963 197 this.exception = exception;
attila@963 198 }
attila@963 199
attila@90 200 /**
attila@90 201 * Returns the invocation method handle.
attila@90 202 *
attila@90 203 * @return the invocation method handle. It will never be null.
attila@90 204 */
attila@90 205 public MethodHandle getInvocation() {
attila@90 206 return invocation;
attila@90 207 }
attila@90 208
attila@90 209 /**
attila@90 210 * Returns the guard method handle.
attila@90 211 *
attila@90 212 * @return the guard method handle. Can be null.
attila@90 213 */
attila@90 214 public MethodHandle getGuard() {
attila@90 215 return guard;
attila@90 216 }
attila@90 217
attila@90 218 /**
attila@90 219 * Returns the switch point that can be used to invalidate the invocation handle.
attila@90 220 *
attila@90 221 * @return the switch point that can be used to invalidate the invocation handle. Can be null.
attila@90 222 */
attila@963 223 public SwitchPoint[] getSwitchPoints() {
attila@963 224 return switchPoints == null ? null : switchPoints.clone();
attila@963 225 }
attila@963 226
attila@963 227 /**
attila@963 228 * Returns the exception type that if thrown should be used to invalidate the linkage.
attila@963 229 *
attila@963 230 * @return the exception type that if thrown should be used to invalidate the linkage. Can be null.
attila@963 231 */
attila@963 232 public Class<? extends Throwable> getException() {
attila@963 233 return exception;
attila@90 234 }
attila@90 235
attila@90 236 /**
attila@90 237 * Returns true if and only if this guarded invocation has a switchpoint, and that switchpoint has been invalidated.
attila@90 238 * @return true if and only if this guarded invocation has a switchpoint, and that switchpoint has been invalidated.
attila@90 239 */
attila@90 240 public boolean hasBeenInvalidated() {
attila@963 241 if (switchPoints == null) {
attila@963 242 return false;
attila@963 243 }
attila@963 244 for (final SwitchPoint sp : switchPoints) {
attila@963 245 if (sp.hasBeenInvalidated()) {
attila@963 246 return true;
attila@963 247 }
attila@963 248 }
attila@963 249 return false;
attila@90 250 }
attila@90 251
attila@90 252 /**
attila@90 253 * Asserts that the invocation is of the specified type, and the guard (if present) is of the specified type with a
attila@90 254 * boolean return type.
attila@90 255 *
attila@90 256 * @param type the asserted type
attila@90 257 * @throws WrongMethodTypeException if the invocation and the guard are not of the expected method type.
attila@90 258 */
attila@962 259 public void assertType(final MethodType type) {
attila@90 260 assertType(invocation, type);
attila@963 261 if (guard != null) {
attila@90 262 assertType(guard, type.changeReturnType(Boolean.TYPE));
attila@90 263 }
attila@90 264 }
attila@90 265
attila@90 266 /**
attila@90 267 * Creates a new guarded invocation with different methods, preserving the switch point.
attila@90 268 *
attila@90 269 * @param newInvocation the new invocation
attila@90 270 * @param newGuard the new guard
attila@90 271 * @return a new guarded invocation with the replaced methods and the same switch point as this invocation.
attila@90 272 */
attila@962 273 public GuardedInvocation replaceMethods(final MethodHandle newInvocation, final MethodHandle newGuard) {
attila@963 274 return new GuardedInvocation(newInvocation, newGuard, switchPoints, exception);
attila@963 275 }
attila@963 276
attila@963 277 /**
attila@963 278 * Add a switchpoint to this guarded invocation
attila@963 279 * @param newSwitchPoint new switchpoint, or null for nop
attila@963 280 * @return new guarded invocation with the extra switchpoint
attila@963 281 */
attila@963 282 public GuardedInvocation addSwitchPoint(final SwitchPoint newSwitchPoint) {
attila@963 283 if (newSwitchPoint == null) {
attila@963 284 return this;
attila@963 285 }
attila@963 286
attila@963 287 final SwitchPoint[] newSwitchPoints;
attila@963 288 if (switchPoints != null) {
attila@963 289 newSwitchPoints = new SwitchPoint[switchPoints.length + 1];
attila@963 290 System.arraycopy(switchPoints, 0, newSwitchPoints, 0, switchPoints.length);
attila@963 291 newSwitchPoints[switchPoints.length] = newSwitchPoint;
attila@963 292 } else {
attila@963 293 newSwitchPoints = new SwitchPoint[] { newSwitchPoint };
attila@963 294 }
attila@963 295
attila@963 296 return new GuardedInvocation(invocation, guard, newSwitchPoints, exception);
attila@90 297 }
attila@90 298
attila@962 299 private GuardedInvocation replaceMethodsOrThis(final MethodHandle newInvocation, final MethodHandle newGuard) {
attila@963 300 if (newInvocation == invocation && newGuard == guard) {
attila@90 301 return this;
attila@90 302 }
attila@90 303 return replaceMethods(newInvocation, newGuard);
attila@90 304 }
attila@90 305
attila@90 306 /**
attila@90 307 * Changes the type of the invocation, as if {@link MethodHandle#asType(MethodType)} was applied to its invocation
attila@90 308 * and its guard, if it has one (with return type changed to boolean, and parameter count potentially truncated for
attila@90 309 * the guard). If the invocation already is of the required type, returns this object.
attila@90 310 * @param newType the new type of the invocation.
attila@90 311 * @return a guarded invocation with the new type applied to it.
attila@90 312 */
attila@962 313 public GuardedInvocation asType(final MethodType newType) {
attila@90 314 return replaceMethodsOrThis(invocation.asType(newType), guard == null ? null : Guards.asType(guard, newType));
attila@90 315 }
attila@90 316
attila@90 317 /**
attila@90 318 * Changes the type of the invocation, as if {@link LinkerServices#asType(MethodHandle, MethodType)} was applied to
attila@90 319 * its invocation and its guard, if it has one (with return type changed to boolean, and parameter count potentially
attila@90 320 * truncated for the guard). If the invocation already is of the required type, returns this object.
attila@90 321 * @param linkerServices the linker services to use for the conversion
attila@90 322 * @param newType the new type of the invocation.
attila@90 323 * @return a guarded invocation with the new type applied to it.
attila@90 324 */
attila@962 325 public GuardedInvocation asType(final LinkerServices linkerServices, final MethodType newType) {
attila@90 326 return replaceMethodsOrThis(linkerServices.asType(invocation, newType), guard == null ? null :
attila@90 327 Guards.asType(linkerServices, guard, newType));
attila@90 328 }
attila@90 329
attila@90 330 /**
attila@963 331 * Changes the type of the invocation, as if {@link LinkerServices#asTypeLosslessReturn(MethodHandle, MethodType)} was
attila@963 332 * applied to its invocation and {@link LinkerServices#asType(MethodHandle, MethodType)} applied to its guard, if it
attila@963 333 * has one (with return type changed to boolean, and parameter count potentially truncated for the guard). If the
attila@963 334 * invocation doesn't change its type, returns this object.
attila@963 335 * @param linkerServices the linker services to use for the conversion
attila@963 336 * @param newType the new type of the invocation.
attila@963 337 * @return a guarded invocation with the new type applied to it.
attila@963 338 */
attila@963 339 public GuardedInvocation asTypeSafeReturn(final LinkerServices linkerServices, final MethodType newType) {
attila@963 340 return replaceMethodsOrThis(linkerServices.asTypeLosslessReturn(invocation, newType), guard == null ? null :
attila@963 341 Guards.asType(linkerServices, guard, newType));
attila@963 342 }
attila@963 343
attila@963 344 /**
attila@90 345 * Changes the type of the invocation, as if {@link MethodHandle#asType(MethodType)} was applied to its invocation
attila@90 346 * and its guard, if it has one (with return type changed to boolean for guard). If the invocation already is of the
attila@90 347 * required type, returns this object.
attila@90 348 * @param desc a call descriptor whose method type is adapted.
attila@90 349 * @return a guarded invocation with the new type applied to it.
attila@90 350 */
attila@962 351 public GuardedInvocation asType(final CallSiteDescriptor desc) {
attila@90 352 return asType(desc.getMethodType());
attila@90 353 }
attila@90 354
attila@90 355 /**
attila@90 356 * Applies argument filters to both the invocation and the guard (if there is one).
attila@90 357 * @param pos the position of the first argumen being filtered
attila@90 358 * @param filters the argument filters
attila@90 359 * @return a filtered invocation
attila@90 360 */
attila@962 361 public GuardedInvocation filterArguments(final int pos, final MethodHandle... filters) {
attila@90 362 return replaceMethods(MethodHandles.filterArguments(invocation, pos, filters), guard == null ? null :
attila@90 363 MethodHandles.filterArguments(guard, pos, filters));
attila@90 364 }
attila@90 365
attila@90 366 /**
attila@90 367 * Makes an invocation that drops arguments in both the invocation and the guard (if there is one).
attila@90 368 * @param pos the position of the first argument being dropped
attila@90 369 * @param valueTypes the types of the values being dropped
attila@90 370 * @return an invocation that drops arguments
attila@90 371 */
attila@962 372 public GuardedInvocation dropArguments(final int pos, final List<Class<?>> valueTypes) {
attila@90 373 return replaceMethods(MethodHandles.dropArguments(invocation, pos, valueTypes), guard == null ? null :
attila@90 374 MethodHandles.dropArguments(guard, pos, valueTypes));
attila@90 375 }
attila@90 376
attila@90 377 /**
attila@90 378 * Makes an invocation that drops arguments in both the invocation and the guard (if there is one).
attila@90 379 * @param pos the position of the first argument being dropped
attila@90 380 * @param valueTypes the types of the values being dropped
attila@90 381 * @return an invocation that drops arguments
attila@90 382 */
attila@962 383 public GuardedInvocation dropArguments(final int pos, final Class<?>... valueTypes) {
attila@90 384 return replaceMethods(MethodHandles.dropArguments(invocation, pos, valueTypes), guard == null ? null :
attila@90 385 MethodHandles.dropArguments(guard, pos, valueTypes));
attila@90 386 }
attila@90 387
attila@90 388
attila@90 389 /**
attila@90 390 * Composes the invocation, switchpoint, and the guard into a composite method handle that knows how to fall back.
attila@90 391 * @param fallback the fallback method handle in case switchpoint is invalidated or guard returns false.
attila@90 392 * @return a composite method handle.
attila@90 393 */
attila@962 394 public MethodHandle compose(final MethodHandle fallback) {
attila@963 395 return compose(fallback, fallback, fallback);
attila@90 396 }
attila@90 397
attila@90 398 /**
attila@90 399 * Composes the invocation, switchpoint, and the guard into a composite method handle that knows how to fall back.
attila@90 400 * @param switchpointFallback the fallback method handle in case switchpoint is invalidated.
attila@90 401 * @param guardFallback the fallback method handle in case guard returns false.
attila@963 402 * @param catchFallback the fallback method in case the exception handler triggers
attila@90 403 * @return a composite method handle.
attila@90 404 */
attila@963 405 public MethodHandle compose(final MethodHandle guardFallback, final MethodHandle switchpointFallback, final MethodHandle catchFallback) {
attila@90 406 final MethodHandle guarded =
attila@963 407 guard == null ?
attila@963 408 invocation :
attila@963 409 MethodHandles.guardWithTest(
attila@963 410 guard,
attila@963 411 invocation,
attila@963 412 guardFallback);
attila@963 413
attila@963 414 final MethodHandle catchGuarded =
attila@963 415 exception == null ?
attila@963 416 guarded :
attila@963 417 MH.catchException(
attila@963 418 guarded,
attila@963 419 exception,
attila@963 420 MethodHandles.dropArguments(
attila@963 421 catchFallback,
attila@963 422 0,
attila@963 423 exception));
attila@963 424
attila@963 425 if (switchPoints == null) {
attila@963 426 return catchGuarded;
attila@963 427 }
attila@963 428
attila@963 429 MethodHandle spGuarded = catchGuarded;
attila@963 430 for (final SwitchPoint sp : switchPoints) {
attila@963 431 spGuarded = sp.guardWithTest(spGuarded, switchpointFallback);
attila@963 432 }
attila@963 433
attila@963 434 return spGuarded;
attila@90 435 }
attila@90 436
attila@962 437 private static void assertType(final MethodHandle mh, final MethodType type) {
attila@90 438 if(!mh.type().equals(type)) {
attila@90 439 throw new WrongMethodTypeException("Expected type: " + type + " actual type: " + mh.type());
attila@90 440 }
attila@90 441 }
attila@101 442 }

mercurial