nashorn/src/jdk/internal/dynalink/beans/BeanLinker.java
author attila
Wed, 26 Feb 2014 13:17:57 +0100
changeset 24719 f726e9d67629
parent 16245 6a1c6c8bc113
child 24769 de4dcfa9380f
permissions -rw-r--r--
8035820: Optimistic recompilation Reviewed-by: hannesw, jlaskey, sundar Contributed-by: attila.szegedi@oracle.com, marcus.lagergren@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     1
/*
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     4
 *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    10
 *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    15
 * accompanied this code).
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    16
 *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    20
 *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    23
 * questions.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    24
 */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    25
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    26
/*
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    27
 * This file is available under and governed by the GNU General Public
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    28
 * License version 2 only, as published by the Free Software Foundation.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    29
 * However, the following notice accompanied the original version of this
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    30
 * file, and Oracle licenses the original version of this file under the BSD
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    31
 * license:
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    32
 */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    33
/*
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    34
   Copyright 2009-2013 Attila Szegedi
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    35
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    36
   Licensed under both the Apache License, Version 2.0 (the "Apache License")
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    37
   and the BSD License (the "BSD License"), with licensee being free to
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    38
   choose either of the two at their discretion.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    39
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    40
   You may not use this file except in compliance with either the Apache
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    41
   License or the BSD License.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    42
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    43
   If you choose to use this file in compliance with the Apache License, the
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    44
   following notice applies to you:
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    45
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    46
       You may obtain a copy of the Apache License at
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    47
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    48
           http://www.apache.org/licenses/LICENSE-2.0
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    49
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    50
       Unless required by applicable law or agreed to in writing, software
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    51
       distributed under the License is distributed on an "AS IS" BASIS,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    52
       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    53
       implied. See the License for the specific language governing
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    54
       permissions and limitations under the License.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    55
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    56
   If you choose to use this file in compliance with the BSD License, the
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    57
   following notice applies to you:
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    58
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    59
       Redistribution and use in source and binary forms, with or without
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    60
       modification, are permitted provided that the following conditions are
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    61
       met:
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    62
       * Redistributions of source code must retain the above copyright
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    63
         notice, this list of conditions and the following disclaimer.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    64
       * Redistributions in binary form must reproduce the above copyright
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    65
         notice, this list of conditions and the following disclaimer in the
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    66
         documentation and/or other materials provided with the distribution.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    67
       * Neither the name of the copyright holder nor the names of
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    68
         contributors may be used to endorse or promote products derived from
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    69
         this software without specific prior written permission.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    70
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    71
       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    72
       IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    73
       TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    74
       PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    75
       BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    76
       CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    77
       SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    78
       BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    79
       WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    80
       OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    81
       ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    82
*/
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    83
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    84
package jdk.internal.dynalink.beans;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    85
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    86
import java.lang.invoke.MethodHandle;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    87
import java.lang.invoke.MethodHandles;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    88
import java.lang.invoke.MethodType;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    89
import java.lang.reflect.Array;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    90
import java.util.Collection;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    91
import java.util.List;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    92
import java.util.Map;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    93
import jdk.internal.dynalink.CallSiteDescriptor;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    94
import jdk.internal.dynalink.beans.GuardedInvocationComponent.ValidationType;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    95
import jdk.internal.dynalink.linker.GuardedInvocation;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    96
import jdk.internal.dynalink.linker.LinkerServices;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    97
import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    98
import jdk.internal.dynalink.support.Guards;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    99
import jdk.internal.dynalink.support.Lookup;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   100
import jdk.internal.dynalink.support.TypeUtilities;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   101
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   102
/**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   103
 * A class that provides linking capabilities for a single POJO class. Normally not used directly, but managed by
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   104
 * {@link BeansLinker}.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   105
 *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   106
 * @author Attila Szegedi
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   107
 */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   108
class BeanLinker extends AbstractJavaLinker implements TypeBasedGuardingDynamicLinker {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   109
    BeanLinker(Class<?> clazz) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   110
        super(clazz, Guards.getClassGuard(clazz), Guards.getInstanceOfGuard(clazz));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   111
        if(clazz.isArray()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   112
            // Some languages won't have a notion of manipulating collections. Exposing "length" on arrays as an
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   113
            // explicit property is beneficial for them.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   114
            // REVISIT: is it maybe a code smell that "dyn:getLength" is not needed?
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   115
            setPropertyGetter("length", GET_ARRAY_LENGTH, ValidationType.IS_ARRAY);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   116
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   117
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   118
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   119
    @Override
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   120
    public boolean canLinkType(Class<?> type) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   121
        return type == clazz;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   122
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   123
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   124
    @Override
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   125
    FacetIntrospector createFacetIntrospector() {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   126
        return new BeanIntrospector(clazz);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   127
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   128
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   129
    @Override
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   130
    protected GuardedInvocationComponent getGuardedInvocationComponent(CallSiteDescriptor callSiteDescriptor,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   131
            LinkerServices linkerServices, List<String> operations) throws Exception {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   132
        final GuardedInvocationComponent superGic = super.getGuardedInvocationComponent(callSiteDescriptor,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   133
                linkerServices, operations);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   134
        if(superGic != null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   135
            return superGic;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   136
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   137
        if(operations.isEmpty()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   138
            return null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   139
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   140
        final String op = operations.get(0);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   141
        // dyn:getElem(this, id)
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   142
        // id is typically either an int (for arrays and lists) or an object (for maps). linkerServices can provide
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   143
        // conversion from call site argument type though.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   144
        if("getElem".equals(op)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   145
            return getElementGetter(callSiteDescriptor, linkerServices, pop(operations));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   146
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   147
        if("setElem".equals(op)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   148
            return getElementSetter(callSiteDescriptor, linkerServices, pop(operations));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   149
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   150
        // dyn:getLength(this) (works on Java arrays, collections, and maps)
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   151
        if("getLength".equals(op)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   152
            return getLengthGetter(callSiteDescriptor);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   153
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   154
        return null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   155
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   156
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   157
    private static MethodHandle GET_LIST_ELEMENT = Lookup.PUBLIC.findVirtual(List.class, "get",
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   158
            MethodType.methodType(Object.class, int.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   159
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   160
    private static MethodHandle GET_MAP_ELEMENT = Lookup.PUBLIC.findVirtual(Map.class, "get",
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   161
            MethodType.methodType(Object.class, Object.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   162
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   163
    private static MethodHandle LIST_GUARD = Guards.getInstanceOfGuard(List.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   164
    private static MethodHandle MAP_GUARD = Guards.getInstanceOfGuard(Map.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   165
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   166
    private GuardedInvocationComponent getElementGetter(final CallSiteDescriptor callSiteDescriptor,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   167
            final LinkerServices linkerServices, List<String> operations) throws Exception {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   168
        final MethodType callSiteType = callSiteDescriptor.getMethodType();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   169
        final Class<?> declaredType = callSiteType.parameterType(0);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   170
        final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   171
                linkerServices, operations);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   172
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   173
        // If declared type of receiver at the call site is already an array, a list or map, bind without guard. Thing
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   174
        // is, it'd be quite stupid of a call site creator to go though invokedynamic when it knows in advance they're
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   175
        // dealing with an array, or a list or map, but hey...
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   176
        // Note that for arrays and lists, using LinkerServices.asType() will ensure that any language specific linkers
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   177
        // in use will get a chance to perform any (if there's any) implicit conversion to integer for the indices.
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   178
        final GuardedInvocationComponent gic;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   179
        final boolean isMap;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   180
        if(declaredType.isArray()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   181
            gic = new GuardedInvocationComponent(MethodHandles.arrayElementGetter(declaredType));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   182
            isMap = false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   183
        } else if(List.class.isAssignableFrom(declaredType)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   184
            gic = new GuardedInvocationComponent(GET_LIST_ELEMENT);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   185
            isMap = false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   186
        } else if(Map.class.isAssignableFrom(declaredType)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   187
            gic = new GuardedInvocationComponent(GET_MAP_ELEMENT);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   188
            isMap = true;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   189
        } else if(clazz.isArray()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   190
            gic = getClassGuardedInvocationComponent(MethodHandles.arrayElementGetter(clazz), callSiteType);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   191
            isMap = false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   192
        } else if(List.class.isAssignableFrom(clazz)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   193
            gic = new GuardedInvocationComponent(GET_LIST_ELEMENT, Guards.asType(LIST_GUARD, callSiteType), List.class,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   194
                    ValidationType.INSTANCE_OF);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   195
            isMap = false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   196
        } else if(Map.class.isAssignableFrom(clazz)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   197
            gic = new GuardedInvocationComponent(GET_MAP_ELEMENT, Guards.asType(MAP_GUARD, callSiteType), Map.class,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   198
                    ValidationType.INSTANCE_OF);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   199
            isMap = true;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   200
        } else {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   201
            // Can't retrieve elements for objects that are neither arrays, nor list, nor maps.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   202
            return nextComponent;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   203
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   204
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   205
        // We can have "dyn:getElem:foo", especially in composites, i.e. "dyn:getElem|getProp|getMethod:foo"
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   206
        final String fixedKey = getFixedKey(callSiteDescriptor);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   207
        // Convert the key to a number if we're working with a list or array
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   208
        final Object typedFixedKey;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   209
        if(!isMap && fixedKey != null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   210
            typedFixedKey = convertKeyToInteger(fixedKey, linkerServices);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   211
            if(typedFixedKey == null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   212
                // key is not numeric, it can never succeed
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   213
                return nextComponent;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   214
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   215
        } else {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   216
            typedFixedKey = fixedKey;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   217
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   218
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   219
        final GuardedInvocation gi = gic.getGuardedInvocation();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   220
        final Binder binder = new Binder(linkerServices, callSiteType, typedFixedKey);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   221
        final MethodHandle invocation = gi.getInvocation();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   222
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   223
        if(nextComponent == null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   224
            return gic.replaceInvocation(binder.bind(invocation));
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   225
        }
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   226
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   227
        final MethodHandle checkGuard;
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   228
        if(invocation == GET_LIST_ELEMENT) {
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   229
            checkGuard = convertArgToInt(RANGE_CHECK_LIST, linkerServices, callSiteDescriptor);
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   230
        } else if(invocation == GET_MAP_ELEMENT) {
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   231
            // TODO: A more complex solution could be devised for maps, one where we do a get() first, and fold it
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   232
            // into a GWT that tests if it returned null, and if it did, do another GWT with containsKey()
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   233
            // that returns constant null (on true), or falls back to next component (on false)
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   234
            checkGuard = CONTAINS_MAP;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   235
        } else {
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   236
            checkGuard = convertArgToInt(RANGE_CHECK_ARRAY, linkerServices, callSiteDescriptor);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   237
        }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   238
        final MethodPair matchedInvocations = matchReturnTypes(binder.bind(invocation),
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   239
                nextComponent.getGuardedInvocation().getInvocation());
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   240
        return nextComponent.compose(matchedInvocations.guardWithTest(binder.bindTest(checkGuard)), gi.getGuard(),
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   241
                gic.getValidatorClass(), gic.getValidationType());
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   242
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   243
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   244
    private static String getFixedKey(final CallSiteDescriptor callSiteDescriptor) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   245
        return callSiteDescriptor.getNameTokenCount() == 2 ? null : callSiteDescriptor.getNameToken(
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   246
                CallSiteDescriptor.NAME_OPERAND);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   247
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   248
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   249
    private static Object convertKeyToInteger(String fixedKey, LinkerServices linkerServices) throws Exception {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   250
        try {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   251
            if(linkerServices.canConvert(String.class, Number.class)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   252
                try {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   253
                    final Object val = linkerServices.getTypeConverter(String.class, Number.class).invoke(fixedKey);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   254
                    if(!(val instanceof Number)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   255
                        return null; // not a number
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   256
                    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   257
                    final Number n = (Number)val;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   258
                    if(n instanceof Integer) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   259
                        return n;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   260
                    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   261
                    final int intIndex = n.intValue();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   262
                    final double doubleValue = n.doubleValue();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   263
                    if(intIndex != doubleValue && !Double.isInfinite(doubleValue)) { // let infinites trigger IOOBE
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   264
                        return null; // not an exact integer
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   265
                    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   266
                    return Integer.valueOf(intIndex);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   267
                } catch(Exception|Error e) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   268
                    throw e;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   269
                } catch(Throwable t) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   270
                    throw new RuntimeException(t);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   271
                }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   272
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   273
            return Integer.valueOf(fixedKey);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   274
        } catch(NumberFormatException e) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   275
            // key is not a number
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   276
            return null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   277
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   278
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   279
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   280
    private static MethodHandle convertArgToInt(MethodHandle mh, LinkerServices ls, CallSiteDescriptor desc) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   281
        final Class<?> sourceType = desc.getMethodType().parameterType(1);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   282
        if(TypeUtilities.isMethodInvocationConvertible(sourceType, Number.class)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   283
            return mh;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   284
        } else if(ls.canConvert(sourceType, Number.class)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   285
            final MethodHandle converter = ls.getTypeConverter(sourceType, Number.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   286
            return MethodHandles.filterArguments(mh, 1, converter.asType(converter.type().changeReturnType(
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   287
                    mh.type().parameterType(1))));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   288
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   289
        return mh;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   290
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   291
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   292
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   293
     * Contains methods to adapt an item getter/setter method handle to the requested type, optionally binding it to a
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   294
     * fixed key first.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   295
     * @author Attila Szegedi
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   296
     * @version $Id: $
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   297
     */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   298
    private static class Binder {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   299
        private final LinkerServices linkerServices;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   300
        private final MethodType methodType;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   301
        private final Object fixedKey;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   302
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   303
        Binder(LinkerServices linkerServices, MethodType methodType, Object fixedKey) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   304
            this.linkerServices = linkerServices;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   305
            this.methodType = fixedKey == null ? methodType : methodType.insertParameterTypes(1, fixedKey.getClass());
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   306
            this.fixedKey = fixedKey;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   307
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   308
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   309
        /*private*/ MethodHandle bind(MethodHandle handle) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   310
            return bindToFixedKey(linkerServices.asTypeLosslessReturn(handle, methodType));
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   311
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   312
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   313
        /*private*/ MethodHandle bindTest(MethodHandle handle) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   314
            return bindToFixedKey(Guards.asType(handle, methodType));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   315
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   316
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   317
        private MethodHandle bindToFixedKey(MethodHandle handle) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   318
            return fixedKey == null ? handle : MethodHandles.insertArguments(handle, 1, fixedKey);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   319
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   320
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   321
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   322
    private static MethodHandle RANGE_CHECK_ARRAY = findRangeCheck(Object.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   323
    private static MethodHandle RANGE_CHECK_LIST = findRangeCheck(List.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   324
    private static MethodHandle CONTAINS_MAP = Lookup.PUBLIC.findVirtual(Map.class, "containsKey",
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   325
            MethodType.methodType(boolean.class, Object.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   326
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   327
    private static MethodHandle findRangeCheck(Class<?> collectionType) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   328
        return Lookup.findOwnStatic(MethodHandles.lookup(), "rangeCheck", boolean.class, collectionType, Object.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   329
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   330
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   331
    @SuppressWarnings("unused")
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   332
    private static final boolean rangeCheck(Object array, Object index) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   333
        if(!(index instanceof Number)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   334
            return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   335
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   336
        final Number n = (Number)index;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   337
        final int intIndex = n.intValue();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   338
        final double doubleValue = n.doubleValue();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   339
        if(intIndex != doubleValue && !Double.isInfinite(doubleValue)) { // let infinite trigger IOOBE
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   340
            return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   341
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   342
        if(0 <= intIndex && intIndex < Array.getLength(array)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   343
            return true;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   344
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   345
        throw new ArrayIndexOutOfBoundsException("Array index out of range: " + n);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   346
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   347
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   348
    @SuppressWarnings("unused")
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   349
    private static final boolean rangeCheck(List<?> list, Object index) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   350
        if(!(index instanceof Number)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   351
            return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   352
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   353
        final Number n = (Number)index;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   354
        final int intIndex = n.intValue();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   355
        final double doubleValue = n.doubleValue();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   356
        if(intIndex != doubleValue && !Double.isInfinite(doubleValue)) { // let infinite trigger IOOBE
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   357
            return false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   358
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   359
        if(0 <= intIndex && intIndex < list.size()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   360
            return true;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   361
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   362
        throw new IndexOutOfBoundsException("Index: " + n + ", Size: " + list.size());
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   363
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   364
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   365
    private static MethodHandle SET_LIST_ELEMENT = Lookup.PUBLIC.findVirtual(List.class, "set",
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   366
            MethodType.methodType(Object.class, int.class, Object.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   367
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   368
    private static MethodHandle PUT_MAP_ELEMENT = Lookup.PUBLIC.findVirtual(Map.class, "put",
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   369
            MethodType.methodType(Object.class, Object.class, Object.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   370
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   371
    private GuardedInvocationComponent getElementSetter(CallSiteDescriptor callSiteDescriptor,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   372
            LinkerServices linkerServices, List<String> operations) throws Exception {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   373
        final MethodType callSiteType = callSiteDescriptor.getMethodType();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   374
        final Class<?> declaredType = callSiteType.parameterType(0);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   375
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   376
        final GuardedInvocationComponent gic;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   377
        // If declared type of receiver at the call site is already an array, a list or map, bind without guard. Thing
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   378
        // is, it'd be quite stupid of a call site creator to go though invokedynamic when it knows in advance they're
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   379
        // dealing with an array, or a list or map, but hey...
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   380
        // Note that for arrays and lists, using LinkerServices.asType() will ensure that any language specific linkers
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   381
        // in use will get a chance to perform any (if there's any) implicit conversion to integer for the indices.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   382
        final boolean isMap;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   383
        if(declaredType.isArray()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   384
            gic = new GuardedInvocationComponent(MethodHandles.arrayElementSetter(declaredType));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   385
            isMap = false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   386
        } else if(List.class.isAssignableFrom(declaredType)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   387
            gic = new GuardedInvocationComponent(SET_LIST_ELEMENT);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   388
            isMap = false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   389
        } else if(Map.class.isAssignableFrom(declaredType)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   390
            gic = new GuardedInvocationComponent(PUT_MAP_ELEMENT);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   391
            isMap = true;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   392
        } else if(clazz.isArray()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   393
            gic = getClassGuardedInvocationComponent(MethodHandles.arrayElementSetter(clazz), callSiteType);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   394
            isMap = false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   395
        } else if(List.class.isAssignableFrom(clazz)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   396
            gic = new GuardedInvocationComponent(SET_LIST_ELEMENT, Guards.asType(LIST_GUARD, callSiteType), List.class,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   397
                    ValidationType.INSTANCE_OF);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   398
            isMap = false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   399
        } else if(Map.class.isAssignableFrom(clazz)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   400
            gic = new GuardedInvocationComponent(PUT_MAP_ELEMENT, Guards.asType(MAP_GUARD, callSiteType), Map.class,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   401
                    ValidationType.INSTANCE_OF);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   402
            isMap = true;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   403
        } else {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   404
            // Can't set elements for objects that are neither arrays, nor list, nor maps.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   405
            gic = null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   406
            isMap = false;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   407
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   408
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   409
        // In contrast to, say, getElementGetter, we only compute the nextComponent if the target object is not a map,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   410
        // as maps will always succeed in setting the element and will never need to fall back to the next component
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   411
        // operation.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   412
        final GuardedInvocationComponent nextComponent = isMap ? null : getGuardedInvocationComponent(
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   413
                callSiteDescriptor, linkerServices, operations);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   414
        if(gic == null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   415
            return nextComponent;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   416
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   417
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   418
        // We can have "dyn:setElem:foo", especially in composites, i.e. "dyn:setElem|setProp:foo"
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   419
        final String fixedKey = getFixedKey(callSiteDescriptor);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   420
        // Convert the key to a number if we're working with a list or array
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   421
        final Object typedFixedKey;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   422
        if(!isMap && fixedKey != null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   423
            typedFixedKey = convertKeyToInteger(fixedKey, linkerServices);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   424
            if(typedFixedKey == null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   425
                // key is not numeric, it can never succeed
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   426
                return nextComponent;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   427
            }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   428
        } else {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   429
            typedFixedKey = fixedKey;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   430
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   431
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   432
        final GuardedInvocation gi = gic.getGuardedInvocation();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   433
        final Binder binder = new Binder(linkerServices, callSiteType, typedFixedKey);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   434
        final MethodHandle invocation = gi.getInvocation();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   435
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   436
        if(nextComponent == null) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   437
            return gic.replaceInvocation(binder.bind(invocation));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   438
        }
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   439
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   440
        final MethodHandle checkGuard = convertArgToInt(invocation == SET_LIST_ELEMENT ? RANGE_CHECK_LIST :
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   441
            RANGE_CHECK_ARRAY, linkerServices, callSiteDescriptor);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   442
        final MethodPair matchedInvocations = matchReturnTypes(binder.bind(invocation),
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   443
                nextComponent.getGuardedInvocation().getInvocation());
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 16245
diff changeset
   444
        return nextComponent.compose(matchedInvocations.guardWithTest(binder.bindTest(checkGuard)), gi.getGuard(),
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   445
                gic.getValidatorClass(), gic.getValidationType());
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   446
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   447
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   448
    private static MethodHandle GET_ARRAY_LENGTH = Lookup.PUBLIC.findStatic(Array.class, "getLength",
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   449
            MethodType.methodType(int.class, Object.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   450
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   451
    private static MethodHandle GET_COLLECTION_LENGTH = Lookup.PUBLIC.findVirtual(Collection.class, "size",
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   452
            MethodType.methodType(int.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   453
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   454
    private static MethodHandle GET_MAP_LENGTH = Lookup.PUBLIC.findVirtual(Map.class, "size",
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   455
            MethodType.methodType(int.class));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   456
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   457
    private static MethodHandle COLLECTION_GUARD = Guards.getInstanceOfGuard(Collection.class);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   458
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   459
    private GuardedInvocationComponent getLengthGetter(CallSiteDescriptor callSiteDescriptor) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   460
        assertParameterCount(callSiteDescriptor, 1);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   461
        final MethodType callSiteType = callSiteDescriptor.getMethodType();
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   462
        final Class<?> declaredType = callSiteType.parameterType(0);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   463
        // If declared type of receiver at the call site is already an array, collection, or map, bind without guard.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   464
        // Thing is, it'd be quite stupid of a call site creator to go though invokedynamic when it knows in advance
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   465
        // they're dealing with an array, collection, or map, but hey...
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   466
        if(declaredType.isArray()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   467
            return new GuardedInvocationComponent(GET_ARRAY_LENGTH.asType(callSiteType));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   468
        } else if(Collection.class.isAssignableFrom(declaredType)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   469
            return new GuardedInvocationComponent(GET_COLLECTION_LENGTH.asType(callSiteType));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   470
        } else if(Map.class.isAssignableFrom(declaredType)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   471
            return new GuardedInvocationComponent(GET_MAP_LENGTH.asType(callSiteType));
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   472
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   473
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   474
        // Otherwise, create a binding based on the actual type of the argument with an appropriate guard.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   475
        if(clazz.isArray()) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   476
            return new GuardedInvocationComponent(GET_ARRAY_LENGTH.asType(callSiteType), Guards.isArray(0,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   477
                    callSiteType), ValidationType.IS_ARRAY);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   478
        } if(Collection.class.isAssignableFrom(clazz)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   479
            return new GuardedInvocationComponent(GET_COLLECTION_LENGTH.asType(callSiteType), Guards.asType(
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   480
                    COLLECTION_GUARD, callSiteType), Collection.class, ValidationType.INSTANCE_OF);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   481
        } if(Map.class.isAssignableFrom(clazz)) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   482
            return new GuardedInvocationComponent(GET_MAP_LENGTH.asType(callSiteType), Guards.asType(MAP_GUARD,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   483
                    callSiteType), Map.class, ValidationType.INSTANCE_OF);
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   484
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   485
        // Can't retrieve length for objects that are neither arrays, nor collections, nor maps.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   486
        return null;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   487
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   488
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   489
    private static void assertParameterCount(CallSiteDescriptor descriptor, int paramCount) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   490
        if(descriptor.getMethodType().parameterCount() != paramCount) {
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   491
            throw new BootstrapMethodError(descriptor.getName() + " must have exactly " + paramCount + " parameters.");
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   492
        }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   493
    }
16245
6a1c6c8bc113 8008371: Fix Dynalink compiler warnings and whitespace
attila
parents: 16234
diff changeset
   494
}