nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/JSObjectLinker.java
author hannesw
Mon, 15 Dec 2014 12:32:34 +0100
changeset 28125 118973efb1d3
parent 27361 86c4ddb4797b
child 29133 1cd7d8af99ba
permissions -rw-r--r--
8062030: Nashorn bug retrieving array property after key string concatenation Reviewed-by: sundar, lagergren, attila
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    26
package jdk.nashorn.internal.runtime.linker;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    28
import java.lang.invoke.MethodHandle;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
import java.lang.invoke.MethodHandles;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
import java.lang.invoke.MethodType;
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
    31
import java.util.HashMap;
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
    32
import java.util.Map;
24591
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    33
import javax.script.Bindings;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16151
diff changeset
    34
import jdk.internal.dynalink.CallSiteDescriptor;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16151
diff changeset
    35
import jdk.internal.dynalink.linker.GuardedInvocation;
22669
75563515567f 8032681: Issues with Nashorn
attila
parents: 21453
diff changeset
    36
import jdk.internal.dynalink.linker.GuardedTypeConversion;
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
    37
import jdk.internal.dynalink.linker.GuardingTypeConverterFactory;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16151
diff changeset
    38
import jdk.internal.dynalink.linker.LinkRequest;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16151
diff changeset
    39
import jdk.internal.dynalink.linker.LinkerServices;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16151
diff changeset
    40
import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16151
diff changeset
    41
import jdk.internal.dynalink.support.CallSiteDescriptorFactory;
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
    42
import jdk.nashorn.api.scripting.JSObject;
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
    43
import jdk.nashorn.internal.lookup.MethodHandleFactory;
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
    44
import jdk.nashorn.internal.lookup.MethodHandleFunctionality;
28125
118973efb1d3 8062030: Nashorn bug retrieving array property after key string concatenation
hannesw
parents: 27361
diff changeset
    45
import jdk.nashorn.internal.runtime.ConsString;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
import jdk.nashorn.internal.runtime.JSType;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
 * A Dynalink linker to handle web browser built-in JS (DOM etc.) objects as well
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
 * as ScriptObjects from other Nashorn contexts.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    51
 */
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
    52
final class JSObjectLinker implements TypeBasedGuardingDynamicLinker, GuardingTypeConverterFactory {
24591
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    53
    private final NashornBeansLinker nashornBeansLinker;
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    54
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    55
    JSObjectLinker(final NashornBeansLinker nashornBeansLinker) {
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    56
        this.nashornBeansLinker = nashornBeansLinker;
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    57
    }
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    58
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
    public boolean canLinkType(final Class<?> type) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    61
        return canLinkTypeStatic(type);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
    static boolean canLinkTypeStatic(final Class<?> type) {
24591
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    65
        // can link JSObject also handles Map, Bindings to make
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    66
        // sure those are not JSObjects.
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    67
        return Map.class.isAssignableFrom(type) ||
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    68
               Bindings.class.isAssignableFrom(type) ||
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    69
               JSObject.class.isAssignableFrom(type);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
    public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices) throws Exception {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
        final LinkRequest requestWithoutContext = request.withoutRuntimeContext(); // Nashorn has no runtime context
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
        final Object self = requestWithoutContext.getReceiver();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
        final CallSiteDescriptor desc = requestWithoutContext.getCallSiteDescriptor();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    78
        if (desc.getNameTokenCount() < 2 || !"dyn".equals(desc.getNameToken(CallSiteDescriptor.SCHEME))) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    79
            // We only support standard "dyn:*[:*]" operations
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    80
            return null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
        final GuardedInvocation inv;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
        if (self instanceof JSObject) {
26237
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
    85
            inv = lookup(desc, request, linkerServices);
24591
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    86
        } else if (self instanceof Map || self instanceof Bindings) {
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    87
            // guard to make sure the Map or Bindings does not turn into JSObject later!
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    88
            final GuardedInvocation beanInv = nashornBeansLinker.getGuardedInvocation(request, linkerServices);
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    89
            inv = new GuardedInvocation(beanInv.getInvocation(),
67e22e0e8473 8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents: 22669
diff changeset
    90
                NashornGuards.combineGuards(beanInv.getGuard(), NashornGuards.getNotJSObjectGuard()));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
        } else {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    92
            throw new AssertionError(); // Should never reach here.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    93
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
    95
        return Bootstrap.asTypeSafeReturn(inv, linkerServices, desc);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
    98
    @Override
22669
75563515567f 8032681: Issues with Nashorn
attila
parents: 21453
diff changeset
    99
    public GuardedTypeConversion convertToType(final Class<?> sourceType, final Class<?> targetType) throws Exception {
21450
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   100
        final boolean sourceIsAlwaysJSObject = JSObject.class.isAssignableFrom(sourceType);
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   101
        if(!sourceIsAlwaysJSObject && !sourceType.isAssignableFrom(JSObject.class)) {
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   102
            return null;
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   103
        }
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   104
21450
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   105
        final MethodHandle converter = CONVERTERS.get(targetType);
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   106
        if(converter == null) {
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   107
            return null;
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   108
        }
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   109
22669
75563515567f 8032681: Issues with Nashorn
attila
parents: 21453
diff changeset
   110
        return new GuardedTypeConversion(new GuardedInvocation(converter, sourceIsAlwaysJSObject ? null : IS_JSOBJECT_GUARD).asType(MethodType.methodType(targetType, sourceType)), true);
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   111
    }
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   112
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   113
26237
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   114
    private GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request, final LinkerServices linkerServices) throws Exception {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
        final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   116
        final int c = desc.getNameTokenCount();
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   117
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
        switch (operator) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
            case "getProp":
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   120
            case "getElem":
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   121
            case "getMethod":
26237
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   122
                if (c > 2) {
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   123
                    return findGetMethod(desc);
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   124
                }
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26237
diff changeset
   125
            // For indexed get, we want get GuardedInvocation beans linker and pass it.
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26237
diff changeset
   126
            // JSObjectLinker.get uses this fallback getter for explicit signature method access.
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26237
diff changeset
   127
            return findGetIndexMethod(nashornBeansLinker.getGuardedInvocation(request, linkerServices));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   128
            case "setProp":
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   129
            case "setElem":
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
                return c > 2 ? findSetMethod(desc) : findSetIndexMethod();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
            case "call":
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   132
                return findCallMethod(desc);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
            case "new":
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17751
diff changeset
   134
                return findNewMethod(desc);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
            default:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
                return null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   138
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   139
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17751
diff changeset
   140
    private static GuardedInvocation findGetMethod(final CallSiteDescriptor desc) {
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   141
        final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   142
        final MethodHandle getter = MH.insertArguments(JSOBJECT_GETMEMBER, 1, name);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   143
        return new GuardedInvocation(getter, IS_JSOBJECT_GUARD);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   144
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   145
26237
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   146
    private static GuardedInvocation findGetIndexMethod(final GuardedInvocation inv) {
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   147
        final MethodHandle getter = MH.insertArguments(JSOBJECTLINKER_GET, 0, inv.getInvocation());
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   148
        return inv.replaceMethods(getter, inv.getGuard());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
    private static GuardedInvocation findSetMethod(final CallSiteDescriptor desc) {
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   152
        final MethodHandle getter = MH.insertArguments(JSOBJECT_SETMEMBER, 1, desc.getNameToken(2));
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   153
        return new GuardedInvocation(getter, IS_JSOBJECT_GUARD);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   154
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   155
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   156
    private static GuardedInvocation findSetIndexMethod() {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   157
        return new GuardedInvocation(JSOBJECTLINKER_PUT, IS_JSOBJECT_GUARD);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   158
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   160
    private static GuardedInvocation findCallMethod(final CallSiteDescriptor desc) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   161
        // TODO: if call site is already a vararg, don't do asCollector
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   162
        MethodHandle mh = JSOBJECT_CALL;
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   163
        if (NashornCallSiteDescriptor.isApplyToCall(desc)) {
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   164
            mh = MH.insertArguments(JSOBJECT_CALL_TO_APPLY, 0, JSOBJECT_CALL);
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   165
        }
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   166
        return new GuardedInvocation(MH.asCollector(mh, Object[].class, desc.getMethodType().parameterCount() - 2), IS_JSOBJECT_GUARD);
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   167
    }
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   168
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17751
diff changeset
   169
    private static GuardedInvocation findNewMethod(final CallSiteDescriptor desc) {
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   170
        final MethodHandle func = MH.asCollector(JSOBJECT_NEW, Object[].class, desc.getMethodType().parameterCount() - 1);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   171
        return new GuardedInvocation(func, IS_JSOBJECT_GUARD);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   172
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   173
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   174
    @SuppressWarnings("unused")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   175
    private static boolean isJSObject(final Object self) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   176
        return self instanceof JSObject;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   177
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   178
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
    @SuppressWarnings("unused")
26237
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   180
    private static Object get(final MethodHandle fallback, final Object jsobj, final Object key)
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   181
        throws Throwable {
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   182
        if (key instanceof Integer) {
20928
3ff39d5c8c08 8026137: Fix Issues with Binary Evaluation Order
lagergren
parents: 19889
diff changeset
   183
            return ((JSObject)jsobj).getSlot((Integer)key);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   184
        } else if (key instanceof Number) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   185
            final int index = getIndex((Number)key);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   186
            if (index > -1) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   187
                return ((JSObject)jsobj).getSlot(index);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   188
            }
28125
118973efb1d3 8062030: Nashorn bug retrieving array property after key string concatenation
hannesw
parents: 27361
diff changeset
   189
        } else if (key instanceof String || key instanceof ConsString) {
118973efb1d3 8062030: Nashorn bug retrieving array property after key string concatenation
hannesw
parents: 27361
diff changeset
   190
            final String name = key.toString();
26237
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   191
            // get with method name and signature. delegate it to beans linker!
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   192
            if (name.indexOf('(') != -1) {
28125
118973efb1d3 8062030: Nashorn bug retrieving array property after key string concatenation
hannesw
parents: 27361
diff changeset
   193
                return fallback.invokeExact(jsobj, (Object) name);
26237
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   194
            }
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 26237
diff changeset
   195
            return ((JSObject)jsobj).getMember(name);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   196
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   197
        return null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   198
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   199
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   200
    @SuppressWarnings("unused")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   201
    private static void put(final Object jsobj, final Object key, final Object value) {
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   202
        if (key instanceof Integer) {
20928
3ff39d5c8c08 8026137: Fix Issues with Binary Evaluation Order
lagergren
parents: 19889
diff changeset
   203
            ((JSObject)jsobj).setSlot((Integer)key, value);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   204
        } else if (key instanceof Number) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   205
            ((JSObject)jsobj).setSlot(getIndex((Number)key), value);
28125
118973efb1d3 8062030: Nashorn bug retrieving array property after key string concatenation
hannesw
parents: 27361
diff changeset
   206
        } else if (key instanceof String || key instanceof ConsString) {
118973efb1d3 8062030: Nashorn bug retrieving array property after key string concatenation
hannesw
parents: 27361
diff changeset
   207
            ((JSObject)jsobj).setMember(key.toString(), value);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   208
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   209
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   210
21450
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   211
    @SuppressWarnings("unused")
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   212
    private static int toInt32(final JSObject obj) {
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   213
        return JSType.toInt32(toNumber(obj));
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   214
    }
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   215
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   216
    @SuppressWarnings("unused")
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   217
    private static long toLong(final JSObject obj) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   218
        return JSType.toLong(toNumber(obj));
21450
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   219
    }
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   220
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   221
    private static double toNumber(final JSObject obj) {
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   222
        return obj == null ? 0 : obj.toNumber();
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   223
    }
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   224
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   225
    @SuppressWarnings("unused")
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   226
    private static boolean toBoolean(final JSObject obj) {
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   227
        return obj != null;
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   228
    }
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   229
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   230
    private static int getIndex(final Number n) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   231
        final double value = n.doubleValue();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   232
        return JSType.isRepresentableAsInt(value) ? (int)value : -1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   233
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   234
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   235
    @SuppressWarnings("unused")
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   236
    private static Object callToApply(final MethodHandle mh, final JSObject obj, final Object thiz, final Object... args) {
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   237
        assert args.length >= 2;
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   238
        final Object   receiver  = args[0];
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   239
        final Object[] arguments = new Object[args.length - 1];
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   240
        System.arraycopy(args, 1, arguments, 0, arguments.length);
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   241
        try {
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   242
            return mh.invokeExact(obj, thiz, new Object[] { receiver, arguments });
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   243
        } catch (final RuntimeException | Error e) {
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   244
            throw e;
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   245
        } catch (final Throwable e) {
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   246
            throw new RuntimeException(e);
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   247
        }
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   248
    }
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   249
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   250
    private static final MethodHandleFunctionality MH = MethodHandleFactory.getFunctionality();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   251
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   252
    // method handles of the current class
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   253
    private static final MethodHandle IS_JSOBJECT_GUARD  = findOwnMH_S("isJSObject", boolean.class, Object.class);
26237
b6509e37ce64 8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents: 25865
diff changeset
   254
    private static final MethodHandle JSOBJECTLINKER_GET = findOwnMH_S("get", Object.class, MethodHandle.class, Object.class, Object.class);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   255
    private static final MethodHandle JSOBJECTLINKER_PUT = findOwnMH_S("put", Void.TYPE, Object.class, Object.class, Object.class);
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   256
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   257
    // method handles of JSObject class
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   258
    private static final MethodHandle JSOBJECT_GETMEMBER     = findJSObjectMH_V("getMember", Object.class, String.class);
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   259
    private static final MethodHandle JSOBJECT_SETMEMBER     = findJSObjectMH_V("setMember", Void.TYPE, String.class, Object.class);
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   260
    private static final MethodHandle JSOBJECT_CALL          = findJSObjectMH_V("call", Object.class, Object.class, Object[].class);
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   261
    private static final MethodHandle JSOBJECT_CALL_TO_APPLY = findOwnMH_S("callToApply", Object.class, MethodHandle.class, JSObject.class, Object.class, Object[].class);
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
   262
    private static final MethodHandle JSOBJECT_NEW           = findJSObjectMH_V("newObject", Object.class, Object[].class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   263
21450
419e5d51f319 8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents: 21449
diff changeset
   264
    private static final Map<Class<?>, MethodHandle> CONVERTERS = new HashMap<>();
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   265
    static {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   266
        CONVERTERS.put(boolean.class, findOwnMH_S("toBoolean", boolean.class, JSObject.class));
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   267
        CONVERTERS.put(int.class,     findOwnMH_S("toInt32", int.class, JSObject.class));
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   268
        CONVERTERS.put(long.class,    findOwnMH_S("toLong", long.class, JSObject.class));
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   269
        CONVERTERS.put(double.class,  findOwnMH_S("toNumber", double.class, JSObject.class));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   270
    }
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   271
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   272
    private static MethodHandle findJSObjectMH_V(final String name, final Class<?> rtype, final Class<?>... types) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   273
        return MH.findVirtual(MethodHandles.lookup(), JSObject.class, name, MH.type(rtype, types));
21449
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   274
    }
72d51df5ed85 8027031: complete merging of loads and converts
attila
parents: 20928
diff changeset
   275
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   276
    private static MethodHandle findOwnMH_S(final String name, final Class<?> rtype, final Class<?>... types) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22669
diff changeset
   277
        return MH.findStatic(MethodHandles.lookup(), JSObjectLinker.class, name, MH.type(rtype, types));
19889
63af9358d0dc 8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents: 18614
diff changeset
   278
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   279
}